1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
4 // Copyright(C) 2006 Simon Howard
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
27 #include "txt_radiobutton.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
);
49 w
= radiobutton
->widget
.w
;
51 TXT_BGColor(TXT_COLOR_BLUE
, 0);
52 TXT_FGColor(TXT_COLOR_BRIGHT_CYAN
);
55 TXT_FGColor(TXT_COLOR_BRIGHT_WHITE
);
57 if (*radiobutton
->variable
== radiobutton
->value
)
59 TXT_DrawString("\x07");
66 TXT_FGColor(TXT_COLOR_BRIGHT_CYAN
);
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
)
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");
109 static void TXT_RadioButtonMousePress(TXT_UNCAST_ARG(radiobutton
),
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
,
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
;
146 void TXT_SetRadioButtonLabel(txt_radiobutton_t
*radiobutton
, char *value
)
148 free(radiobutton
->label
);
149 radiobutton
->label
= strdup(value
);