shithub: choc

Download patch

ref: 56824b130b786aab49876a71c6c768a17c5a4f1c
parent: 9caebe584ccb95239b2ed360d4bce3dffc5ecfea
author: Simon Howard <[email protected]>
date: Fri Dec 10 15:31:46 EST 2010

Replace txt_widget_t#selectable with a callback function to query
whether the widget is selectable. This stops the table code from
selecting things that aren't really selectable - eg. empty tables,
scrollpanes containing unselectable widgets, etc.
Fixes a bug with the warp menu (thanks Proteh).

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

--- a/setup/txt_joybinput.c
+++ b/setup/txt_joybinput.c
@@ -206,6 +206,7 @@
 
 txt_widget_class_t txt_joystick_input_class =
 {
+    TXT_AlwaysSelectable,
     TXT_JoystickInputSizeCalc,
     TXT_JoystickInputDrawer,
     TXT_JoystickInputKeyPress,
--- a/setup/txt_keyinput.c
+++ b/setup/txt_keyinput.c
@@ -171,6 +171,7 @@
 
 txt_widget_class_t txt_key_input_class =
 {
+    TXT_AlwaysSelectable,
     TXT_KeyInputSizeCalc,
     TXT_KeyInputDrawer,
     TXT_KeyInputKeyPress,
--- a/setup/txt_mouseinput.c
+++ b/setup/txt_mouseinput.c
@@ -164,6 +164,7 @@
 
 txt_widget_class_t txt_mouse_input_class =
 {
+    TXT_AlwaysSelectable,
     TXT_MouseInputSizeCalc,
     TXT_MouseInputDrawer,
     TXT_MouseInputKeyPress,
--- a/textscreen/examples/guitest.c
+++ b/textscreen/examples/guitest.c
@@ -163,8 +163,8 @@
 {
     txt_window_t *window;
     txt_table_t *table;
+    txt_table_t *unselectable_table;
     txt_scrollpane_t *scrollpane;
-    int i;
 
     window = TXT_NewWindow("Another test");
     TXT_SetWindowPosition(window, 
@@ -172,10 +172,13 @@
                           TXT_VERT_TOP, 
                           TXT_SCREEN_W - 1, 1);
 
-    for (i=0; i<5; ++i)
-    {
-        TXT_AddWidget(window, TXT_NewButton("hello there blah blah blah blah"));
-    }
+    TXT_AddWidgets(window,
+                   TXT_NewScrollPane(40, 1,
+                        TXT_NewLabel("* Unselectable scroll pane *")),
+                   unselectable_table = TXT_NewTable(1),
+                   NULL);
+
+    TXT_AddWidget(unselectable_table, TXT_NewLabel("* Unselectable table *"));
 
     TXT_AddWidget(window, TXT_NewSeparator("Input boxes"));
     table = TXT_NewTable(2);
--- a/textscreen/txt_button.c
+++ b/textscreen/txt_button.c
@@ -96,6 +96,7 @@
 
 txt_widget_class_t txt_button_class =
 {
+    TXT_AlwaysSelectable,
     TXT_ButtonSizeCalc,
     TXT_ButtonDrawer,
     TXT_ButtonKeyPress,
--- a/textscreen/txt_checkbox.c
+++ b/textscreen/txt_checkbox.c
@@ -117,6 +117,7 @@
 
 txt_widget_class_t txt_checkbox_class =
 {
+    TXT_AlwaysSelectable,
     TXT_CheckBoxSizeCalc,
     TXT_CheckBoxDrawer,
     TXT_CheckBoxKeyPress,
--- a/textscreen/txt_dropdown.c
+++ b/textscreen/txt_dropdown.c
@@ -262,6 +262,7 @@
 
 txt_widget_class_t txt_dropdown_list_class =
 {
+    TXT_AlwaysSelectable,
     TXT_DropdownListSizeCalc,
     TXT_DropdownListDrawer,
     TXT_DropdownListKeyPress,
--- a/textscreen/txt_inputbox.c
+++ b/textscreen/txt_inputbox.c
@@ -232,6 +232,7 @@
 
 txt_widget_class_t txt_inputbox_class =
 {
+    TXT_AlwaysSelectable,
     TXT_InputBoxSizeCalc,
     TXT_InputBoxDrawer,
     TXT_InputBoxKeyPress,
@@ -242,6 +243,7 @@
 
 txt_widget_class_t txt_int_inputbox_class =
 {
+    TXT_AlwaysSelectable,
     TXT_InputBoxSizeCalc,
     TXT_InputBoxDrawer,
     TXT_IntInputBoxKeyPress,
--- a/textscreen/txt_label.c
+++ b/textscreen/txt_label.c
@@ -104,6 +104,7 @@
 
 txt_widget_class_t txt_label_class =
 {
+    TXT_NeverSelectable,
     TXT_LabelSizeCalc,
     TXT_LabelDrawer,
     NULL,
@@ -170,7 +171,6 @@
     label = malloc(sizeof(txt_label_t));
 
     TXT_InitWidget(label, &txt_label_class);
-    label->widget.selectable = 0;
     label->label = NULL;
     label->lines = NULL;
 
--- a/textscreen/txt_radiobutton.c
+++ b/textscreen/txt_radiobutton.c
@@ -121,6 +121,7 @@
 
 txt_widget_class_t txt_radiobutton_class =
 {
+    TXT_AlwaysSelectable,
     TXT_RadioButtonSizeCalc,
     TXT_RadioButtonDrawer,
     TXT_RadioButtonKeyPress,
--- a/textscreen/txt_scrollpane.c
+++ b/textscreen/txt_scrollpane.c
@@ -138,7 +138,7 @@
     }
     if (scrollpane->expand_h)
     {
-        scrollpane->h = FullWidth(scrollpane);
+        scrollpane->h = FullHeight(scrollpane);
     }
 
     scrollpane->widget.w = scrollpane->w;
@@ -486,8 +486,26 @@
     }
 }
 
+static int TXT_ScrollPaneSelectable(TXT_UNCAST_ARG(scrollpane))
+{
+    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
+
+    // If scroll bars are displayed, the scroll pane must be selectable
+    // so that we can use the arrow keys to scroll around.
+
+    if (NeedsScrollbars(scrollpane))
+    {
+        return 1;
+    }
+
+    // Otherwise, whether this is selectable depends on the child widget.
+
+    return TXT_SelectableWidget(scrollpane->child);
+}
+
 txt_widget_class_t txt_scrollpane_class =
 {
+    TXT_ScrollPaneSelectable,
     TXT_ScrollPaneSizeCalc,
     TXT_ScrollPaneDrawer,
     TXT_ScrollPaneKeyPress,
--- a/textscreen/txt_separator.c
+++ b/textscreen/txt_separator.c
@@ -82,6 +82,7 @@
 
 txt_widget_class_t txt_separator_class =
 {
+    TXT_NeverSelectable,
     TXT_SeparatorSizeCalc,
     TXT_SeparatorDrawer,
     NULL,
@@ -97,7 +98,6 @@
     separator = malloc(sizeof(txt_separator_t));
 
     TXT_InitWidget(separator, &txt_separator_class);
-    separator->widget.selectable = 0;
 
     if (label != NULL)
     {
--- a/textscreen/txt_spinctrl.c
+++ b/textscreen/txt_spinctrl.c
@@ -358,6 +358,7 @@
 
 txt_widget_class_t txt_spincontrol_class =
 {
+    TXT_AlwaysSelectable,
     TXT_SpinControlSizeCalc,
     TXT_SpinControlDrawer,
     TXT_SpinControlKeyPress,
--- a/textscreen/txt_strut.c
+++ b/textscreen/txt_strut.c
@@ -55,6 +55,7 @@
 
 txt_widget_class_t txt_strut_class =
 {
+    TXT_NeverSelectable,
     TXT_StrutSizeCalc,
     TXT_StrutDrawer,
     TXT_StrutKeyPress,
@@ -70,7 +71,6 @@
     strut = malloc(sizeof(txt_strut_t));
 
     TXT_InitWidget(strut, &txt_strut_class);
-    strut->widget.selectable = 0;
     strut->width = width;
     strut->height = height;
 
--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -202,7 +202,7 @@
     va_end(args);
 }
 
-static int SelectableWidget(txt_table_t *table, int x, int y)
+static int SelectableCell(txt_table_t *table, int x, int y)
 {
     txt_widget_t *widget;
     int i;
@@ -217,7 +217,9 @@
     if (i >= 0 && i < table->num_widgets)
     {
         widget = table->widgets[i];
-        return widget != NULL && widget->selectable && widget->visible;
+        return widget != NULL
+            && TXT_SelectableWidget(widget)
+            && widget->visible;
     }
 
     return 0;
@@ -237,7 +239,7 @@
     {
         // Search to the right
 
-        if (SelectableWidget(table, start_col + x, row))
+        if (SelectableCell(table, start_col + x, row))
         {
             return start_col + x;
         }
@@ -244,7 +246,7 @@
 
         // Search to the left
 
-        if (SelectableWidget(table, start_col - x, row))
+        if (SelectableCell(table, start_col - x, row))
         {
             return start_col - x;
         }
@@ -270,7 +272,7 @@
     if (selected >= 0 && selected < table->num_widgets)
     {
         if (table->widgets[selected] != NULL
-         && table->widgets[selected]->selectable
+         && TXT_SelectableWidget(table->widgets[selected])
          && TXT_WidgetKeyPress(table->widgets[selected], key))
         {
             return 1;
@@ -329,7 +331,7 @@
 
         for (new_x = table->selected_x - 1; new_x >= 0; --new_x)
         {
-            if (SelectableWidget(table, new_x, table->selected_y))
+            if (SelectableCell(table, new_x, table->selected_y))
             {
                 // Found a selectable widget!
 
@@ -348,7 +350,7 @@
 
         for (new_x = table->selected_x + 1; new_x < table->columns; ++new_x)
         {
-            if (SelectableWidget(table, new_x, table->selected_y))
+            if (SelectableCell(table, new_x, table->selected_y))
             {
                 // Found a selectable widget!
 
@@ -547,7 +549,7 @@
 
                 // Select the cell if the widget is selectable
 
-                if (widget->selectable)
+                if (TXT_SelectableWidget(widget))
                 {
                     table->selected_x = i % table->columns;
                     table->selected_y = i / table->columns;
@@ -563,8 +565,41 @@
     }
 }
 
+// Determine whether the table is selectable.
+
+static int TXT_TableSelectable(TXT_UNCAST_ARG(table))
+{
+    TXT_CAST_ARG(txt_table_t, table);
+    int i;
+
+    // Is the currently-selected cell selectable?
+
+    if (SelectableCell(table, table->selected_x, table->selected_y))
+    {
+        return 1;
+    }
+
+    // Find the first selectable cell and set selected_x, selected_y.
+
+    for (i = 0; i < table->num_widgets; ++i)
+    {
+        if (table->widgets[i] != NULL
+         && TXT_SelectableWidget(table->widgets[i]))
+        {
+            table->selected_x = i % table->columns;
+            table->selected_y = i / table->columns;
+            return 1;
+        }
+    }
+
+    // No selectable widgets exist within the table.
+
+    return 0;
+}
+
 txt_widget_class_t txt_table_class =
 {
+    TXT_TableSelectable,
     TXT_CalcTableSize,
     TXT_TableDrawer,
     TXT_TableKeyPress,
--- a/textscreen/txt_widget.c
+++ b/textscreen/txt_widget.c
@@ -83,9 +83,8 @@
     widget->widget_class = widget_class;
     widget->callback_table = TXT_NewCallbackTable();
 
-    // Default values: visible and selectable
+    // Visible by default.
 
-    widget->selectable = 1;
     widget->visible = 1;
 
     // Align left by default
@@ -211,6 +210,30 @@
     if (widget->widget_class->layout != NULL)
     {
         widget->widget_class->layout(widget);
+    }
+}
+
+int TXT_AlwaysSelectable(TXT_UNCAST_ARG(widget))
+{
+    return 1;
+}
+
+int TXT_NeverSelectable(TXT_UNCAST_ARG(widget))
+{
+    return 0;
+}
+
+int TXT_SelectableWidget(TXT_UNCAST_ARG(widget))
+{
+    TXT_CAST_ARG(txt_widget_t, widget);
+
+    if (widget->widget_class->selectable != NULL)
+    {
+        return widget->widget_class->selectable(widget);
+    }
+    else
+    {
+        return 0;
     }
 }
 
--- a/textscreen/txt_widget.h
+++ b/textscreen/txt_widget.h
@@ -77,9 +77,11 @@
 typedef void (*TxtWidgetSignalFunc)(TXT_UNCAST_ARG(widget), void *user_data);
 typedef void (*TxtMousePressFunc)(TXT_UNCAST_ARG(widget), int x, int y, int b);
 typedef void (*TxtWidgetLayoutFunc)(TXT_UNCAST_ARG(widget));
+typedef int (*TxtWidgetSelectableFunc)(TXT_UNCAST_ARG(widget));
 
 struct txt_widget_class_s
 {
+    TxtWidgetSelectableFunc selectable;
     TxtWidgetSizeCalc size_calc;
     TxtWidgetDrawer drawer;
     TxtWidgetKeyPress key_press;
@@ -92,7 +94,6 @@
 {
     txt_widget_class_t *widget_class;
     txt_callback_table_t *callback_table;
-    int selectable;
     int visible;
     txt_horiz_align_t align;
 
@@ -111,6 +112,8 @@
 void TXT_WidgetMousePress(TXT_UNCAST_ARG(widget), int x, int y, int b);
 void TXT_DestroyWidget(TXT_UNCAST_ARG(widget));
 void TXT_LayoutWidget(TXT_UNCAST_ARG(widget));
+int TXT_AlwaysSelectable(TXT_UNCAST_ARG(widget));
+int TXT_NeverSelectable(TXT_UNCAST_ARG(widget));
 
 /**
  * Set a callback function to be invoked when a signal occurs.
@@ -133,6 +136,15 @@
  */
 
 void TXT_SetWidgetAlign(TXT_UNCAST_ARG(widget), txt_horiz_align_t horiz_align);
+
+/**
+ * Query whether a widget is selectable with the cursor.
+ *
+ * @param widget       The widget.
+ * @return             Non-zero if the widget is selectable.
+ */
+
+int TXT_SelectableWidget(TXT_UNCAST_ARG(widget));
 
 #endif /* #ifndef TXT_WIDGET_H */
 
--- a/textscreen/txt_window_action.c
+++ b/textscreen/txt_window_action.c
@@ -93,6 +93,7 @@
 
 txt_widget_class_t txt_window_action_class =
 {
+    TXT_AlwaysSelectable,
     TXT_WindowActionSizeCalc,
     TXT_WindowActionDrawer,
     TXT_WindowActionKeyPress,