shithub: choc

Download patch

ref: 42ebb5bebb6889de52c04a5eb65837b20b489445
parent: 76e770217095fa7b42e62ddbd4e09e369d43bec1
author: Simon Howard <fraggle@gmail.com>
date: Thu May 25 17:27:34 EDT 2006

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

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 530

--- a/textscreen/examples/calculator.c
+++ b/textscreen/examples/calculator.c
@@ -24,7 +24,7 @@
 {
     char buf[20];
 
-    sprintf(buf, " %i", input_value);
+    sprintf(buf, "  %i", input_value);
     TXT_SetLabel(input_box, buf);
 }
 
@@ -116,12 +116,11 @@
     
     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 @@
     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();
 }
 
--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -42,7 +42,10 @@
 
     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 @@
     free(table->widgets);
 }
 
-// -------------
-
 static int TableRows(txt_table_t *table)
 {
     return (table->num_widgets + table->columns - 1) / table->columns;
@@ -65,6 +66,7 @@
     int x, y;
     int rows;
     int ww, wh;
+    txt_widget_t *widget;
 
     rows = TableRows(table);
 
@@ -79,9 +81,20 @@
             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;
             if (ww > col_widths[x])
@@ -134,7 +147,8 @@
 
         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 @@
     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 @@
 
     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 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 @@
 
             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];
         }