1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#include <stdlib.h>
#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_UNCAST_ARG(radiobutton))
{
TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
// Minimum width is the string length + two spaces for padding
radiobutton->widget.w = strlen(radiobutton->label) + 6;
radiobutton->widget.h = 1;
}
static void TXT_RadioButtonDrawer(TXT_UNCAST_ARG(radiobutton), int selected)
{
TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
int i;
int w;
w = radiobutton->widget.w;
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_UNCAST_ARG(radiobutton))
{
TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
free(radiobutton->label);
}
static int TXT_RadioButtonKeyPress(TXT_UNCAST_ARG(radiobutton), int key)
{
TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
if (key == KEY_ENTER || key == ' ')
{
if (*radiobutton->variable != radiobutton->value)
{
*radiobutton->variable = radiobutton->value;
TXT_EmitSignal(radiobutton, "selected");
}
return 1;
}
return 0;
}
static void TXT_RadioButtonMousePress(TXT_UNCAST_ARG(radiobutton),
int x, int y, int b)
{
TXT_CAST_ARG(txt_radiobutton_t, radiobutton);
if (b == TXT_MOUSE_LEFT)
{
// Equivalent to pressing enter
TXT_RadioButtonKeyPress(radiobutton, KEY_ENTER);
}
}
txt_widget_class_t txt_radiobutton_class =
{
TXT_RadioButtonSizeCalc,
TXT_RadioButtonDrawer,
TXT_RadioButtonKeyPress,
TXT_RadioButtonDestructor,
TXT_RadioButtonMousePress,
};
txt_radiobutton_t *TXT_NewRadioButton(char *label, int *variable, int value)
{
txt_radiobutton_t *radiobutton;
radiobutton = malloc(sizeof(txt_radiobutton_t));
TXT_InitWidget(radiobutton, &txt_radiobutton_class);
radiobutton->label = strdup(label);
radiobutton->variable = variable;
radiobutton->value = value;
return radiobutton;
}
|