Don't crash if key settings are set in a configuration file that are out of range...
[chocolate-doom.git] / textscreen / txt_radiobutton.c
blob7ede721106c62e3c0fb1326175e27309c282a837
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2006 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
22 #include <stdlib.h>
23 #include <string.h>
25 #include "doomkeys.h"
27 #include "txt_radiobutton.h"
28 #include "txt_gui.h"
29 #include "txt_io.h"
30 #include "txt_main.h"
31 #include "txt_window.h"
33 static void TXT_RadioButtonSizeCalc(TXT_UNCAST_ARG(radiobutton))
35 TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
37 // Minimum width is the string length + two spaces for padding
39 radiobutton->widget.w = strlen(radiobutton->label) + 6;
40 radiobutton->widget.h = 1;
43 static void TXT_RadioButtonDrawer(TXT_UNCAST_ARG(radiobutton), int selected)
45 TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
46 int i;
47 int w;
49 w = radiobutton->widget.w;
51 TXT_BGColor(TXT_COLOR_BLUE, 0);
52 TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
53 TXT_DrawString(" (");
55 TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
57 if (*radiobutton->variable == radiobutton->value)
59 TXT_DrawString("\x07");
61 else
63 TXT_DrawString(" ");
66 TXT_FGColor(TXT_COLOR_BRIGHT_CYAN);
68 TXT_DrawString(") ");
70 if (selected)
72 TXT_BGColor(TXT_COLOR_GREY, 0);
75 TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
77 TXT_DrawString(radiobutton->label);
79 for (i=strlen(radiobutton->label); i < w-6; ++i)
81 TXT_DrawString(" ");
85 static void TXT_RadioButtonDestructor(TXT_UNCAST_ARG(radiobutton))
87 TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
89 free(radiobutton->label);
92 static int TXT_RadioButtonKeyPress(TXT_UNCAST_ARG(radiobutton), int key)
94 TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
96 if (key == KEY_ENTER || key == ' ')
98 if (*radiobutton->variable != radiobutton->value)
100 *radiobutton->variable = radiobutton->value;
101 TXT_EmitSignal(radiobutton, "selected");
103 return 1;
106 return 0;
109 static void TXT_RadioButtonMousePress(TXT_UNCAST_ARG(radiobutton),
110 int x, int y, int b)
112 TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
114 if (b == TXT_MOUSE_LEFT)
116 // Equivalent to pressing enter
118 TXT_RadioButtonKeyPress(radiobutton, KEY_ENTER);
122 txt_widget_class_t txt_radiobutton_class =
124 TXT_RadioButtonSizeCalc,
125 TXT_RadioButtonDrawer,
126 TXT_RadioButtonKeyPress,
127 TXT_RadioButtonDestructor,
128 TXT_RadioButtonMousePress,
129 NULL,
132 txt_radiobutton_t *TXT_NewRadioButton(char *label, int *variable, int value)
134 txt_radiobutton_t *radiobutton;
136 radiobutton = malloc(sizeof(txt_radiobutton_t));
138 TXT_InitWidget(radiobutton, &txt_radiobutton_class);
139 radiobutton->label = strdup(label);
140 radiobutton->variable = variable;
141 radiobutton->value = value;
143 return radiobutton;
146 void TXT_SetRadioButtonLabel(txt_radiobutton_t *radiobutton, char *value)
148 free(radiobutton->label);
149 radiobutton->label = strdup(value);