foxygit / doom Log in
commit 42ebb5bebb6889de52c04a5eb65837b20b489445
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Thu May 25 21:27:34 2006 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Thu May 25 21:27:34 2006 +0000

    Allow NULL to be added to tables to specify a spacer (empty cell).

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 530
---
 textscreen/examples/calculator.c |  9 ++++----
 textscreen/txt_table.c           | 44 +++++++++++++++++++++++++++++-----------
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/textscreen/examples/calculator.c b/textscreen/examples/calculator.c
index 7c23252d..2cc1392a 100644
--- a/textscreen/examples/calculator.c
+++ b/textscreen/examples/calculator.c
@@ -24,7 +24,7 @@ void UpdateInputBox(void)
 {
     char buf[20];

-    sprintf(buf, " %i", input_value);
+    sprintf(buf, "  %i", input_value);
     TXT_SetLabel(input_box, buf);
 }

@@ -116,12 +116,11 @@ void BuildGUI()

     window = TXT_NewWindow("Calculator");

-    TXT_AddWidget(window, TXT_NewSeparator(NULL));
     input_box = TXT_NewLabel("asdf");
     TXT_SetBGColor(input_box, TXT_COLOR_BLACK);
     TXT_AddWidget(window, input_box);
     TXT_AddWidget(window, TXT_NewSeparator(NULL));
-    TXT_AddWidget(window, TXT_NewLabel(""));
+    TXT_AddWidget(window, NULL);

     table = TXT_NewTable(4);
     TXT_AddWidget(window, table);
@@ -139,13 +138,13 @@ void BuildGUI()
     AddNumberButton(table, 3);
     AddOperatorButton(table, "+", OP_PLUS);
     AddNumberButton(table, 0);
-    TXT_AddWidget(table, TXT_NewLabel(""));
+    TXT_AddWidget(table, NULL);
     equals_button = TXT_NewButton("  =  ");
     TXT_SignalConnect(equals_button, "pressed", Calculate, NULL);
     TXT_AddWidget(table, equals_button);
     AddOperatorButton(table, "/", OP_DIV);

-    TXT_AddWidget(window, TXT_NewLabel(""));
+    TXT_AddWidget(window, NULL);
     UpdateInputBox();
 }

diff --git a/textscreen/txt_table.c b/textscreen/txt_table.c
index 4c502c0c..b2055825 100644
--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -42,7 +42,10 @@ static void TXT_TableDestructor(TXT_UNCAST_ARG(table))

     for (i=0; i<table->num_widgets; ++i)
     {
-        TXT_DestroyWidget(table->widgets[i]);
+        if (table->widgets[i] != NULL)
+        {
+            TXT_DestroyWidget(table->widgets[i]);
+        }
     }

     // Free table resources
@@ -50,8 +53,6 @@ static void TXT_TableDestructor(TXT_UNCAST_ARG(table))
     free(table->widgets);
 }

-// -------------
-
 static int TableRows(txt_table_t *table)
 {
     return (table->num_widgets + table->columns - 1) / table->columns;
@@ -65,6 +66,7 @@ static void CalcRowColSizes(txt_table_t *table,
     int x, y;
     int rows;
     int ww, wh;
+    txt_widget_t *widget;

     rows = TableRows(table);

@@ -79,8 +81,19 @@ static void CalcRowColSizes(txt_table_t *table,
             if (y * table->columns + x >= table->num_widgets)
                 break;

-            TXT_CalcWidgetSize(table->widgets[y * table->columns + x],
-                               &ww, &wh);
+            widget = table->widgets[y * table->columns + x];
+
+            if (widget != NULL)
+            {
+                TXT_CalcWidgetSize(widget, &ww, &wh);
+            }
+            else
+            {
+                // Empty spacer if widget is NULL
+
+                ww = 0;
+                wh = 1;
+            }

             if (wh > row_heights[y])
                 row_heights[y] = wh;
@@ -134,7 +147,8 @@ void TXT_AddWidget(TXT_UNCAST_ARG(table), TXT_UNCAST_ARG(widget))

         last_widget = table->widgets[table->num_widgets - 1];

-        if (widget->widget_class == &txt_separator_class
+        if (widget != NULL && last_widget != NULL
+         && widget->widget_class == &txt_separator_class
          && last_widget->widget_class == &txt_separator_class)
         {
             // The previous widget added was a separator; replace
@@ -168,7 +182,7 @@ static int SelectableWidget(txt_table_t *table, int x, int y)
     if (i >= 0 && i < table->num_widgets)
     {
         widget = table->widgets[i];
-        return widget->selectable && widget->visible;
+        return widget != NULL && widget->selectable && widget->visible;
     }

     return 0;
@@ -221,7 +235,8 @@ static int TXT_TableKeyPress(TXT_UNCAST_ARG(table), int key)

     if (selected >= 0 && selected < table->num_widgets)
     {
-        if (TXT_WidgetKeyPress(table->widgets[selected], key))
+        if (table->widgets[selected] != NULL
+         && TXT_WidgetKeyPress(table->widgets[selected], key))
         {
             return 1;
         }
@@ -342,6 +357,7 @@ static void CheckValidSelection(txt_table_t *table)
 static void TXT_TableDrawer(TXT_UNCAST_ARG(table), int w, int selected)
 {
     TXT_CAST_ARG(txt_table_t, table);
+    txt_widget_t *widget;
     int *column_widths;
     int *row_heights;
     int origin_x, origin_y;
@@ -389,10 +405,14 @@ static void TXT_TableDrawer(TXT_UNCAST_ARG(table), int w, int selected)

             TXT_GotoXY(draw_x, draw_y);

-            TXT_DrawWidget(table->widgets[y * table->columns + x],
-                           column_widths[x],
-                           selected && x == table->selected_x
-                                    && y == table->selected_y);
+            widget = table->widgets[y * table->columns + x];
+
+            if (widget != NULL)
+            {
+                TXT_DrawWidget(widget, column_widths[x],
+                               selected && x == table->selected_x
+                                        && y == table->selected_y);
+            }

             draw_x += column_widths[x];
         }