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