This source file includes following definitions.
- radio_execute_cmd
- radio_key
- radio_callback
- radio_mouse_callback
- radio_new
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 #include <config.h>
36
37 #include <stdlib.h>
38
39 #include "lib/global.h"
40
41 #include "lib/tty/tty.h"
42 #include "lib/widget.h"
43
44
45
46 const global_keymap_t *radio_map = NULL;
47
48
49
50
51
52
53
54
55
56
57
58 static cb_ret_t
59 radio_execute_cmd (WRadio * r, long command)
60 {
61 cb_ret_t ret = MSG_HANDLED;
62 Widget *w = WIDGET (r);
63
64 switch (command)
65 {
66 case CK_Up:
67 case CK_Top:
68 if (r->pos == 0)
69 return MSG_NOT_HANDLED;
70
71 if (command == CK_Top)
72 r->pos = 0;
73 else
74 r->pos--;
75 widget_draw (w);
76 return MSG_HANDLED;
77
78 case CK_Down:
79 case CK_Bottom:
80 if (r->pos == r->count - 1)
81 return MSG_NOT_HANDLED;
82
83 if (command == CK_Bottom)
84 r->pos = r->count - 1;
85 else
86 r->pos++;
87 widget_draw (w);
88 return MSG_HANDLED;
89
90 case CK_Select:
91 r->sel = r->pos;
92 widget_set_state (w, WST_FOCUSED, TRUE);
93 send_message (w->owner, w, MSG_NOTIFY, 0, NULL);
94 return MSG_HANDLED;
95
96 default:
97 ret = MSG_NOT_HANDLED;
98 break;
99 }
100
101 return ret;
102 }
103
104
105
106
107 static cb_ret_t
108 radio_key (WRadio * r, int key)
109 {
110 long command;
111
112 command = widget_lookup_key (WIDGET (r), key);
113 if (command == CK_IgnoreKey)
114 return MSG_NOT_HANDLED;
115 return radio_execute_cmd (r, command);
116 }
117
118
119
120 static cb_ret_t
121 radio_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
122 {
123 WRadio *r = RADIO (w);
124 int i;
125
126 switch (msg)
127 {
128 case MSG_HOTKEY:
129 for (i = 0; i < r->count; i++)
130 {
131 if (r->texts[i].hotkey != NULL)
132 {
133 int c;
134
135 c = g_ascii_tolower ((gchar) r->texts[i].hotkey[0]);
136 if (c != parm)
137 continue;
138 r->pos = i;
139
140
141 send_message (w, sender, MSG_ACTION, CK_Select, data);
142 return MSG_HANDLED;
143 }
144 }
145 return MSG_NOT_HANDLED;
146
147 case MSG_KEY:
148 return radio_key (r, parm);
149
150 case MSG_ACTION:
151 return radio_execute_cmd (r, parm);
152
153 case MSG_CURSOR:
154 widget_gotoyx (r, r->pos, 1);
155 return MSG_HANDLED;
156
157 case MSG_DRAW:
158 {
159 gboolean focused;
160
161 focused = widget_get_state (w, WST_FOCUSED);
162
163 for (i = 0; i < r->count; i++)
164 {
165 widget_selectcolor (w, i == r->pos && focused, FALSE);
166 widget_gotoyx (w, i, 0);
167 tty_draw_hline (w->rect.y + i, w->rect.x, ' ', w->rect.cols);
168 tty_print_string ((r->sel == i) ? "(*) " : "( ) ");
169 hotkey_draw (w, r->texts[i], i == r->pos && focused);
170 }
171
172 return MSG_HANDLED;
173 }
174
175 case MSG_DESTROY:
176 for (i = 0; i < r->count; i++)
177 hotkey_free (r->texts[i]);
178 g_free (r->texts);
179 return MSG_HANDLED;
180
181 default:
182 return widget_default_callback (w, sender, msg, parm, data);
183 }
184 }
185
186
187
188 static void
189 radio_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
190 {
191 switch (msg)
192 {
193 case MSG_MOUSE_DOWN:
194 RADIO (w)->pos = event->y;
195 widget_select (w);
196 break;
197
198 case MSG_MOUSE_CLICK:
199 RADIO (w)->pos = event->y;
200 send_message (w, NULL, MSG_ACTION, CK_Select, NULL);
201 send_message (w->owner, w, MSG_POST_KEY, ' ', NULL);
202 break;
203
204 default:
205 break;
206 }
207 }
208
209
210
211
212
213 WRadio *
214 radio_new (int y, int x, int count, const char **texts)
215 {
216 WRect r0 = { y, x, count, 1 };
217 WRadio *r;
218 Widget *w;
219 int i, wmax = 0;
220
221 r = g_new (WRadio, 1);
222 w = WIDGET (r);
223
224
225 r->texts = g_new (hotkey_t, count);
226
227 for (i = 0; i < count; i++)
228 {
229 int width;
230
231 r->texts[i] = hotkey_new (texts[i]);
232 width = hotkey_width (r->texts[i]);
233 wmax = MAX (width, wmax);
234 }
235
236
237 r0.cols = 4 + wmax;
238 widget_init (w, &r0, radio_callback, radio_mouse_callback);
239 w->options |= WOP_SELECTABLE | WOP_WANT_CURSOR | WOP_WANT_HOTKEY;
240 w->keymap = radio_map;
241
242 r->pos = 0;
243 r->sel = 0;
244 r->count = count;
245
246 return r;
247 }
248
249