shithub: choc

Download patch

ref: 7cd8727f6c074f84f42db895483258bc0ef43c1d
parent: 58978769f5a0da03a35e6cca91be8d9cbea60f3b
author: Simon Howard <[email protected]>
date: Thu Aug 31 14:09:34 EDT 2006

Add TXT_SelectWidget function to select a widget in a table,
TXT_SetColumnWidths to set table column widths. Don't send key presses
to unselectable widgets.

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

--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -22,6 +22,7 @@
 // 02111-1307, USA.
 //
 
+#include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -31,6 +32,7 @@
 #include "txt_gui.h"
 #include "txt_main.h"
 #include "txt_separator.h"
+#include "txt_strut.h"
 #include "txt_table.h"
 
 static void TXT_TableDestructor(TXT_UNCAST_ARG(table))
@@ -229,6 +231,7 @@
     if (selected >= 0 && selected < table->num_widgets)
     {
         if (table->widgets[selected] != NULL
+         && table->widgets[selected]->selectable
          && TXT_WidgetKeyPress(table->widgets[selected], key))
         {
             return 1;
@@ -535,6 +538,8 @@
 
 void TXT_InitTable(txt_table_t *table, int columns)
 {
+    int i;
+
     TXT_InitWidget(table, &txt_table_class);
     table->columns = columns;
     table->widgets = NULL;
@@ -541,6 +546,16 @@
     table->num_widgets = 0;
     table->selected_x = 0;
     table->selected_y = 0;
+
+    // Add a strut for each column at the start of the table. 
+    // These are used by the TXT_SetColumnWidths function below:
+    // the struts are created with widths of 0 each, but this
+    // function changes them.
+
+    for (i=0; i<columns; ++i)
+    {
+        TXT_AddWidget(table, TXT_NewStrut(0, 0));
+    }
 }
 
 txt_table_t *TXT_NewTable(int columns)
@@ -552,5 +567,68 @@
     TXT_InitTable(table, columns);
 
     return table;
+}
+
+// Selects a given widget in a table, recursively searching any tables
+// within this table.  Returns 1 if successful, 0 if unsuccessful.
+
+int TXT_SelectWidget(TXT_UNCAST_ARG(table), TXT_UNCAST_ARG(widget))
+{
+    TXT_CAST_ARG(txt_table_t, table);
+    TXT_CAST_ARG(txt_widget_t, widget);
+    int i;
+
+    for (i=0; i<table->num_widgets; ++i)
+    {
+        if (table->widgets[i] == widget)
+        {
+            // Found the item!  Select it and return.
+            
+            table->selected_x = i % table->columns;
+            table->selected_y = i / table->columns;
+
+            return 1;
+        }
+        
+        if (table->widgets[i]->widget_class == &txt_table_class)
+        {
+            // This item is a subtable.  Recursively search this table.
+
+            if (TXT_SelectWidget(table->widgets[i], widget))
+            {
+                // Found it in the subtable.  Select this subtable and return.
+
+                table->selected_x = i % table->columns;
+                table->selected_y = i / table->columns;
+
+                return 1;
+            }
+        }
+    }
+
+    // Not found.
+
+    return 0;
+}
+
+// Sets the widths of columns in a table.
+
+void TXT_SetColumnWidths(TXT_UNCAST_ARG(table), ...)
+{
+    TXT_CAST_ARG(txt_table_t, table);
+    va_list args;
+    txt_strut_t *strut;
+    int i;
+    int width;
+
+    va_start(args, TXT_UNCAST_ARG_NAME(table));
+
+    for (i=0; i<table->columns; ++i)
+    {
+        width = va_arg(args, int);
+
+        strut = (txt_strut_t *) table->widgets[i];
+        strut->width = width;
+    }
 }
 
--- a/textscreen/txt_table.h
+++ b/textscreen/txt_table.h
@@ -52,6 +52,8 @@
 txt_table_t *TXT_NewTable(int columns);
 void TXT_InitTable(txt_table_t *table, int columns);
 void TXT_AddWidget(TXT_UNCAST_ARG(table), TXT_UNCAST_ARG(widget));
+int TXT_SelectWidget(TXT_UNCAST_ARG(table), TXT_UNCAST_ARG(widget));
+void TXT_SetColumnWidths(TXT_UNCAST_ARG(table), ...);
 
 #endif /* #ifndef TXT_TABLE_T */
 
--- a/textscreen/txt_widget.h
+++ b/textscreen/txt_widget.h
@@ -27,7 +27,8 @@
 #ifndef TXT_WIDGET_H
 #define TXT_WIDGET_H
 
-#define TXT_UNCAST_ARG(name)   void *uncast_ ## name
+#define TXT_UNCAST_ARG_NAME(name) uncast_ ## name
+#define TXT_UNCAST_ARG(name)   void * TXT_UNCAST_ARG_NAME(name)
 #define TXT_CAST_ARG(type, name)  type *name = (type *) uncast_ ## name
 
 typedef enum