foxygit / doom Log in
commit 1d2fe63a94c8e0fcacb3482b822018ce1fc288c8
Author:     Simon Howard <fraggle@soulsphere.org>
AuthorDate: Wed Jan 25 21:08:53 2017 +0000
Commit:     Simon Howard <fraggle@soulsphere.org>
CommitDate: Wed Jan 25 21:08:53 2017 +0000

    textscreen: Add TXT_MakeTable().

    When creating a table widget is is often convenient to be able to create
    and populate it in a single function call. It's possible to do this with
    "horizontal boxes" already but there wasn't a more generic mechanism for
    making a table. So add one as TXT_MakeTable().
---
 textscreen/txt_table.c | 31 +++++++++++++++++++++++++++++++
 textscreen/txt_table.h | 12 ++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/textscreen/txt_table.c b/textscreen/txt_table.c
index 86537c92..5f2d61c0 100644
--- a/textscreen/txt_table.c
+++ b/textscreen/txt_table.c
@@ -906,6 +906,37 @@ txt_table_t *TXT_NewTable(int columns)
     return table;
 }

+// Alternative to TXT_NewTable() that allows a list of widgets to be
+// provided in its arguments.
+txt_table_t *TXT_MakeTable(int columns, TXT_UNCAST_ARG(first_widget), ...)
+{
+    TXT_CAST_ARG(txt_widget_t, first_widget);
+    txt_table_t *table;
+    va_list args;
+
+    table = TXT_NewTable(columns);
+    TXT_AddWidget(table, first_widget);
+
+    va_start(args, TXT_UNCAST_ARG_NAME(first_widget));
+
+    for (;;)
+    {
+        txt_widget_t *widget;
+        widget = va_arg(args, txt_widget_t *);
+
+        if (widget == NULL)
+        {
+            break;
+        }
+
+        TXT_AddWidget(table, widget);
+    }
+
+    va_end(args);
+
+    return table;
+}
+
 // Create a horizontal table from a list of widgets.

 txt_table_t *TXT_NewHorizBox(TXT_UNCAST_ARG(first_widget), ...)
diff --git a/textscreen/txt_table.h b/textscreen/txt_table.h
index 98eb30fa..f7ce7141 100644
--- a/textscreen/txt_table.h
+++ b/textscreen/txt_table.h
@@ -100,6 +100,18 @@ void TXT_InitTable(txt_table_t *table, int columns);

 txt_table_t *TXT_NewTable(int columns);

+/**
+ * Create a new table and populate it with provided widgets.
+ *
+ * The arguments to this function are variable. Each argument must be a
+ * pointer to a widget, and the list is terminated with a NULL.
+ *
+ * @param columns       The number of columns in the new table.
+ * @return              Pointer to the new table structure.
+ */
+
+txt_table_t *TXT_MakeTable(int columns, TXT_UNCAST_ARG(first_widget), ...);
+
 /**
  * Create a table containing the specified widgets packed horizontally,
  * from left to right.