shithub: choc

Download patch

ref: d3d4f6587f48f4047099bfcae2aba84a634edcf4
parent: 0755f5ac88fa3ea3074e1d1e46b5e101de59abdb
author: Simon Howard <[email protected]>
date: Tue Sep 26 19:48:23 EDT 2006

Add TXT_NewButton2 for creating a button with a callback (for convenience).

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

--- a/textscreen/examples/calculator.c
+++ b/textscreen/examples/calculator.c
@@ -44,7 +44,6 @@
 
 void AddNumberButton(txt_table_t *table, int value)
 {
-    txt_button_t *button;
     char buf[10];
     int *val_copy;
 
@@ -53,9 +52,7 @@
 
     sprintf(buf, "  %i  ", value);
 
-    button = TXT_NewButton(buf);
-    TXT_AddWidget(table, button);
-    TXT_SignalConnect(button, "pressed", InsertNumber, val_copy);
+    TXT_AddWidget(table, TXT_NewButton2(buf, InsertNumber, val_copy));
 }
 
 void Operator(TXT_UNCAST_ARG(button), TXT_UNCAST_ARG(op))
@@ -69,7 +66,6 @@
 
 void AddOperatorButton(txt_table_t *table, char *label, operator_t op)
 {
-    txt_button_t *button;
     char buf[10];
     operator_t *op_copy;
 
@@ -77,10 +73,8 @@
     *op_copy = op;
 
     sprintf(buf, "  %s  ", label);
-    button = TXT_NewButton(buf);
 
-    TXT_AddWidget(table, button);
-    TXT_SignalConnect(button, "pressed", Operator, op_copy);
+    TXT_AddWidget(table, TXT_NewButton2(buf, Operator, op_copy));
 }
 
 void Calculate(TXT_UNCAST_ARG(button), void *unused)
@@ -113,7 +107,6 @@
 {
     txt_window_t *window;
     txt_table_t *table;
-    txt_button_t *equals_button;
     
     window = TXT_NewWindow("Calculator");
 
@@ -141,9 +134,8 @@
     AddOperatorButton(table, "+", OP_PLUS);
     AddNumberButton(table, 0);
     TXT_AddWidget(table, NULL);
-    equals_button = TXT_NewButton("  =  ");
-    TXT_SignalConnect(equals_button, "pressed", Calculate, NULL);
-    TXT_AddWidget(table, equals_button);
+
+    TXT_AddWidget(table, TXT_NewButton2("  =  ", Calculate, NULL));
     AddOperatorButton(table, "/", OP_DIV);
     
     TXT_AddWidget(window, TXT_NewStrut(0, 1));
--- a/textscreen/examples/guitest.c
+++ b/textscreen/examples/guitest.c
@@ -65,7 +65,6 @@
     txt_window_t *window;
     txt_table_t *table;
     txt_table_t *rightpane;
-    txt_button_t *button;
     txt_checkbox_t *cheesy_checkbox;
     txt_window_action_t *pwn;
     txt_label_t *toplabel;
@@ -121,10 +120,7 @@
 
     UpdateLabel(NULL, NULL);
                                      
-    button = TXT_NewButton("Close Window");
-    TXT_AddWidget(window, button);
-
-    TXT_SignalConnect(button, "pressed", CloseWindow, NULL);
+    TXT_AddWidget(window, TXT_NewButton2("Close Window", CloseWindow, NULL));
 
     pwn = TXT_NewWindowAction(KEY_F1, "PWN!");
     TXT_SetWindowAction(window, TXT_HORIZ_CENTER, pwn);
--- a/textscreen/txt_button.c
+++ b/textscreen/txt_button.c
@@ -122,3 +122,17 @@
     return button;
 }
 
+// Button with a callback set automatically
+
+txt_button_t *TXT_NewButton2(char *label, TxtWidgetSignalFunc func,
+                             void *user_data)
+{
+    txt_button_t *button;
+
+    button = TXT_NewButton(label);
+
+    TXT_SignalConnect(button, "pressed", func, user_data);
+
+    return button;
+}
+
--- a/textscreen/txt_button.h
+++ b/textscreen/txt_button.h
@@ -33,6 +33,8 @@
 };
 
 txt_button_t *TXT_NewButton(char *label);
+txt_button_t *TXT_NewButton2(char *label, TxtWidgetSignalFunc func, 
+                             void *user_data);
 void TXT_SetButtonLabel(txt_button_t *button, char *label);
 
 #endif /* #ifndef TXT_BUTTON_H */