shithub: choc

Download patch

ref: 56912a4dcca6f35f9a75fff2bccc59949acd2197
parent: 8741ed033996e3cfed28cfc97753ef0cd2c40b17
author: Simon Howard <[email protected]>
date: Sat May 20 17:01:04 EDT 2006

Make all windows be tables with one column.

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

--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -174,7 +174,7 @@
     free(column_widths);
 }
 
-void TXT_AddTableWidget(void *uncast_table, void *uncast_widget)
+void TXT_AddWidget(void *uncast_table, void *uncast_widget)
 {
     txt_widget_t *widget;
     txt_table_t *table;
--- a/textscreen/txt_table.h
+++ b/textscreen/txt_table.h
@@ -51,7 +51,7 @@
 
 txt_table_t *TXT_NewTable(int columns);
 void TXT_InitTable(txt_table_t *table, int columns);
-void TXT_AddTableWidget(void *table, void *widget);
+void TXT_AddWidget(void *table, void *widget);
 
 #endif /* #ifndef TXT_TABLE_T */
 
--- a/textscreen/txt_window.c
+++ b/textscreen/txt_window.c
@@ -39,15 +39,15 @@
 
     win = malloc(sizeof(txt_window_t));
 
+    TXT_InitTable(&win->table, 1);
+
     win->title = strdup(title);
     win->x = TXT_SCREEN_W / 2;
     win->y = TXT_SCREEN_H / 2;
     win->horiz_align = TXT_HORIZ_CENTER;
     win->vert_align = TXT_VERT_CENTER;
-    win->widgets = NULL;
-    win->num_widgets = 0;
-    win->selected = 0;
 
+    TXT_AddWidget(win, TXT_NewSeparator(NULL));
     TXT_AddDesktopWindow(win);
 
     return win;
@@ -55,52 +55,15 @@
 
 void TXT_CloseWindow(txt_window_t *window)
 {
-    int i;
+    free(window->title);
 
-    // Free all widgets
+    // Destroy table and window
 
-    for (i=0; i<window->num_widgets; ++i)
-    {
-        TXT_DestroyWidget(window->widgets[i]);
-    }
+    TXT_DestroyWidget((txt_widget_t *) window);
     
-    // Free window resources
-
-    free(window->widgets);
-    free(window->title);
-    free(window);
-
     TXT_RemoveDesktopWindow(window);
 }
 
-static void CalcWindowSize(txt_window_t *window, int *w, int *h)
-{
-    txt_widget_t *widget;
-    int max_width;
-    int height;
-    int i;
-    int ww, wh;
-
-    max_width = 10;
-    height = 0;
-
-    for (i=0; i<window->num_widgets; ++i)
-    {
-        TXT_CalcWidgetSize(window->widgets[i], &ww, &wh);
-
-        if (ww > max_width)
-            max_width = ww;
-
-        if (window->widgets[i]->visible)
-        {
-            height += wh;
-        }
-    }
-
-    *w = max_width;
-    *h = height;
-}
-
 static void CalcWindowPosition(txt_window_t *window,
                                int *x, int *y,
                                int w, int h)
@@ -141,7 +104,7 @@
     int i;
     int ww, wh;
     
-    CalcWindowSize(window, &widgets_w, &widgets_h);
+    TXT_CalcWidgetSize((txt_widget_t *) window, &widgets_w, &widgets_h);
 
     // Actual window size after padding
 
@@ -159,58 +122,14 @@
 
     // Draw all widgets
 
-    x = window_x + 1;
-    y = window_y + 2;
+    TXT_GotoXY(window_x + 1, window_y + 2);
+    TXT_DrawWidget((txt_widget_t *) window, widgets_w, 1);
 
-    for (i=0; i<window->num_widgets; ++i)
-    {
-        if (window->widgets[i]->visible)
-        {
-            TXT_GotoXY(x, y);
-            TXT_DrawWidget(window->widgets[i], 
-                           widgets_w, 
-                           i == window->selected);
-            TXT_CalcWidgetSize(window->widgets[i], &ww, &wh);
-            y += wh;
-        }
-    }
-
     // Separator for action area
 
-    TXT_DrawSeparator(window_x, y, window_w);
+    TXT_DrawSeparator(window_x, window_y + 2 + widgets_h, window_w);
 }
 
-void TXT_AddWidget(txt_window_t *window, void *uncast_widget)
-{
-    txt_widget_t *widget;
-
-    widget = (txt_widget_t *) uncast_widget;
-
-    if (window->num_widgets == 0)
-    {
-        // This is the first widget added.
-        //
-        // The first widget in a window should always be a separator.
-        // If we are not adding a separator now, add one in first.
-
-        if (widget->widget_class != &txt_separator_class)
-        {
-            txt_separator_t *separator;
-
-            separator = TXT_NewSeparator(NULL);
-
-            window->widgets = malloc(sizeof(txt_widget_t *));
-            window->widgets[0] = &separator->widget;
-            window->num_widgets = 1;
-        }
-    }
-    
-    window->widgets = realloc(window->widgets,
-                              sizeof(txt_widget_t *) * (window->num_widgets + 1));
-    window->widgets[window->num_widgets] = widget;
-    ++window->num_widgets;
-}
-
 void TXT_SetWindowPosition(txt_window_t *window,
                            txt_horiz_align_t horiz_align,
                            txt_vert_align_t vert_align,
@@ -226,50 +145,6 @@
 {
     // Send to the currently selected widget first
 
-    if (window->selected > 0 && window->selected <= window->num_widgets)
-    {
-        if (TXT_WidgetKeyPress(window->widgets[window->selected], c))
-        {
-            return;
-        }
-    }
-
-    if (c == KEY_DOWNARROW)
-    {
-        int newsel;
-
-        // Move cursor down to the next selectable widget
-
-        for (newsel = window->selected + 1;
-             newsel < window->num_widgets;
-             ++newsel)
-        {
-            if (window->widgets[newsel]->visible
-             && window->widgets[newsel]->selectable)
-            {
-                window->selected = newsel;
-                break;
-            }
-        } 
-    }
-
-    if (c == KEY_UPARROW)
-    {
-        int newsel;
-
-        // Move cursor down to the next selectable widget
-
-        for (newsel = window->selected - 1;
-             newsel >= 0;
-             --newsel)
-        {
-            if (window->widgets[newsel]->visible
-             && window->widgets[newsel]->selectable)
-            {
-                window->selected = newsel;
-                break;
-            }
-        } 
-    }
+    TXT_WidgetKeyPress((txt_widget_t *) window, c);
 }
 
--- a/textscreen/txt_window.h
+++ b/textscreen/txt_window.h
@@ -42,9 +42,14 @@
 } txt_horiz_align_t;
 
 #include "txt_widget.h" 
+#include "txt_table.h"
 
 struct txt_window_s
 {
+    // Base class: all windows are tables with one column.
+
+    txt_table_t table;
+    
     // Window title
 
     char *title;
@@ -54,20 +59,10 @@
     txt_vert_align_t vert_align;
     txt_horiz_align_t horiz_align;
     int x, y;
-
-    // Widgets in this window
-
-    txt_widget_t **widgets;
-    int num_widgets;
-
-    // Index of the current selected widget.
-
-    int selected;
 };
 
 txt_window_t *TXT_NewWindow(char *title);
 void TXT_CloseWindow(txt_window_t *window);
-void TXT_AddWidget(txt_window_t *window, void *widget);
 void TXT_SetWindowPosition(txt_window_t *window, 
                            txt_horiz_align_t horiz_align,
                            txt_vert_align_t vert_align,