shithub: choc

Download patch

ref: 992856ec4fe1a55a39e91a5a935d4c0a717f1b1a
parent: 9908aee5026d4cf74e29eb0536e9a7e532ad386c
author: Simon Howard <[email protected]>
date: Sun May 21 16:58:24 EDT 2006

Add radio button class.

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

--- a/textscreen/Makefile.am
+++ b/textscreen/Makefile.am
@@ -12,6 +12,7 @@
 	txt_main.c    txt_main.h       \
 	txt_button.c  txt_button.h     \
 	txt_label.c   txt_label.h      \
+	txt_radiobutton.c txt_radiobutton.h \
 	txt_separator.c txt_separator.h\
         txt_table.c   txt_table.h      \
 	txt_widget.c  txt_widget.h     \
--- a/textscreen/guitest.c
+++ b/textscreen/guitest.c
@@ -6,10 +6,19 @@
 #include "txt_checkbox.h"
 #include "txt_button.h"
 #include "txt_desktop.h"
+#include "txt_label.h"
+#include "txt_radiobutton.h"
 #include "txt_separator.h"
 #include "txt_table.h"
 #include "txt_window.h"
 
+enum 
+{
+    RADIO_VALUE_BADGER,
+    RADIO_VALUE_MUSHROOM,
+    RADIO_VALUE_SNAKE,
+};
+int radiobutton_value;
 txt_window_t *firstwin;
 int checkbox_value;
 
@@ -17,6 +26,7 @@
 {
     txt_window_t *window;
     txt_table_t *table;
+    txt_table_t *leftpane, *rightpane;
     char buf[100];
     int i;
     
@@ -41,8 +51,21 @@
     }
 
     TXT_AddWidget(window, TXT_NewLabel(""));
-    TXT_AddWidget(window, TXT_NewCheckBox("Checkbox", &checkbox_value));
 
+    table = TXT_NewTable(2);
+    TXT_AddWidget(window, table);
+
+    TXT_AddWidget(table, TXT_NewCheckBox("Checkbox", &checkbox_value));
+
+    rightpane = TXT_NewTable(1);
+    TXT_AddWidget(table, rightpane);
+    TXT_AddWidget(rightpane, TXT_NewRadioButton("Badger", &radiobutton_value,
+                                                RADIO_VALUE_BADGER));
+    TXT_AddWidget(rightpane, TXT_NewRadioButton("Mushroom", &radiobutton_value,
+                                                RADIO_VALUE_MUSHROOM));
+    TXT_AddWidget(rightpane, TXT_NewRadioButton("Snake", &radiobutton_value,
+                                                RADIO_VALUE_SNAKE));
+                                     
     firstwin = window;
 }
 
--- /dev/null
+++ b/textscreen/txt_radiobutton.c
@@ -1,0 +1,103 @@
+
+#include <string.h>
+
+#include "doomkeys.h"
+
+#include "txt_radiobutton.h"
+#include "txt_io.h"
+#include "txt_main.h"
+#include "txt_window.h"
+
+static void TXT_RadioButtonSizeCalc(txt_widget_t *widget, int *w, int *h)
+{
+    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+
+    // Minimum width is the string length + two spaces for padding
+
+    *w = strlen(radiobutton->label) + 6;
+    *h = 1;
+}
+
+static void TXT_RadioButtonDrawer(txt_widget_t *widget, int w, int selected)
+{
+    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+    int i;
+
+    TXT_BGColor(TXT_COLOR_BLUE, 0);
+    TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
+    TXT_DrawString(" (");
+
+    TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
+
+    if (*radiobutton->variable == radiobutton->value)
+    {
+        TXT_DrawString("\x07");
+    }
+    else
+    {
+        TXT_DrawString(" ");
+    }
+
+    TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
+
+    TXT_DrawString(") ");
+
+    if (selected)
+    {
+        TXT_BGColor(TXT_COLOR_GREY, 0);
+    }
+
+    TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
+
+    TXT_DrawString(radiobutton->label);
+    
+    for (i=strlen(radiobutton->label); i < w-6; ++i)
+    {
+        TXT_DrawString(" ");
+    }
+}
+
+static void TXT_RadioButtonDestructor(txt_widget_t *widget)
+{
+    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+
+    free(radiobutton->label);
+}
+
+static int TXT_RadioButtonKeyPress(txt_widget_t *widget, int key)
+{
+    txt_radiobutton_t *radiobutton = (txt_radiobutton_t *) widget;
+
+    if (key == KEY_ENTER || key == ' ')
+    {
+        *radiobutton->variable = radiobutton->value;
+        return 1;
+    }
+    
+    return 0;
+}
+
+txt_widget_class_t txt_radiobutton_class =
+{
+    TXT_RadioButtonSizeCalc,
+    TXT_RadioButtonDrawer,
+    TXT_RadioButtonKeyPress,
+    TXT_RadioButtonDestructor,
+};
+
+txt_radiobutton_t *TXT_NewRadioButton(char *label, int *variable, int value)
+{
+    txt_radiobutton_t *radiobutton;
+
+    radiobutton = malloc(sizeof(txt_radiobutton_t));
+
+    radiobutton->widget.widget_class = &txt_radiobutton_class;
+    radiobutton->widget.selectable = 1;
+    radiobutton->widget.visible = 1;
+    radiobutton->label = strdup(label);
+    radiobutton->variable = variable;
+    radiobutton->value = value;
+
+    return radiobutton;
+}
+
--- /dev/null
+++ b/textscreen/txt_radiobutton.h
@@ -1,0 +1,44 @@
+// Emacs style mode select   -*- C++ -*- 
+//-----------------------------------------------------------------------------
+//
+// $Id$
+//
+// Copyright(C) 1993-1996 Id Software, Inc.
+// Copyright(C) 2006 Simon Howard
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+// 02111-1307, USA.
+//
+
+#ifndef TXT_RADIOBUTTON_H
+#define TXT_RADIOBUTTON_H
+
+typedef struct txt_radiobutton_s txt_radiobutton_t;
+
+#include "txt_widget.h"
+
+struct txt_radiobutton_s
+{
+    txt_widget_t widget;
+    char *label;
+    int *variable;
+    int value;
+};
+
+txt_radiobutton_t *TXT_NewRadioButton(char *label, int *variable, int value);
+
+#endif /* #ifndef TXT_RADIOBUTTON_H */
+
+