console/libgnt/gntbox.c

changeset 13958
bd1c3bb3f33d
parent 13950
bd8d39b3a9e2
child 13969
0541f389442e
equal deleted inserted replaced
13957:c6ca17e312aa 13958:bd1c3bb3f33d
8 }; 8 };
9 9
10 static GntWidgetClass *parent_class = NULL; 10 static GntWidgetClass *parent_class = NULL;
11 static guint signals[SIGS] = { 0 }; 11 static guint signals[SIGS] = { 0 };
12 12
13 static GntWidget * find_focusable_widget(GntBox *box);
14
13 static void 15 static void
14 gnt_box_draw(GntWidget *widget) 16 gnt_box_draw(GntWidget *widget)
15 { 17 {
16 GntBox *box = GNT_BOX(widget); 18 GntBox *box = GNT_BOX(widget);
17 19
32 else 34 else
33 { 35 {
34 /* XXX: Position of the title might be configurable */ 36 /* XXX: Position of the title might be configurable */
35 pos = (widget->priv.width - pos) / 2; 37 pos = (widget->priv.width - pos) / 2;
36 } 38 }
37 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_TITLE)); 39
40 if (gnt_widget_has_focus(widget))
41 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_TITLE));
42 else
43 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_TITLE_D));
38 mvwprintw(widget->window, 0, pos, title); 44 mvwprintw(widget->window, 0, pos, title);
39 g_free(title); 45 g_free(title);
40 } 46 }
41 47
42 DEBUG; 48 DEBUG;
137 143
138 static void 144 static void
139 gnt_box_map(GntWidget *widget) 145 gnt_box_map(GntWidget *widget)
140 { 146 {
141 if (widget->priv.width == 0 || widget->priv.height == 0) 147 if (widget->priv.width == 0 || widget->priv.height == 0)
148 {
142 gnt_widget_size_request(widget); 149 gnt_widget_size_request(widget);
150 find_focusable_widget(GNT_BOX(widget));
151 }
143 DEBUG; 152 DEBUG;
144 } 153 }
145 154
155 static GntWidget *
156 find_next_focus(GntBox *box)
157 {
158 GntWidget *w = box->active;
159 GList *iter;
160
161 while (w && !(iter = g_list_find(box->list, w)))
162 w = w->parent;
163
164 if (!w)
165 box->active = NULL;
166 else if (iter)
167 {
168 GntWidget *next = NULL;
169
170 do
171 {
172 next = find_next_focus(iter->data);
173 box->active = next;
174 iter = iter->next;
175 } while (!next && iter);
176 }
177
178 if (box->active == NULL && GNT_WIDGET(box)->parent == NULL)
179 {
180 box->active = find_focusable_widget(box);
181 }
182
183 if (box->active)
184 GNT_WIDGET_SET_FLAGS(box->active, GNT_WIDGET_HAS_FOCUS);
185
186 return box->active;
187 }
188
146 /* Ensures that the current widget can take focus */ 189 /* Ensures that the current widget can take focus */
147 static void 190 static GntWidget *
148 ensure_active(GntBox *box) 191 find_focusable_widget(GntBox *box)
149 { 192 {
150 int investigated = 0; 193 int investigated = 0;
151 int total; 194 int total;
152 195 GntWidget *w = NULL;
153 if (box->active == NULL) 196 GList *iter;
154 box->active = box->list; 197
198 for (iter = box->list; iter; iter = iter->next)
199 {
200 w = iter->data;
201 if (GNT_IS_BOX(w))
202 {
203 w = find_focusable_widget(GNT_BOX(w));
204 if (w)
205 break;
206 }
207 else if (GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_CAN_TAKE_FOCUS))
208 break;
209 }
210
211 if (iter)
212 box->active = w;
213 else
214 box->active = NULL;
215
216 if (box->active)
217 GNT_WIDGET_SET_FLAGS(box->active, GNT_WIDGET_HAS_FOCUS);
218
219 return box->active;
220
221 #if 0
222 if (box->active == NULL && box->list)
223 box->active = box->list->data;
224 else
225 w = box->active;
155 226
156 total = g_list_length(box->list); 227 total = g_list_length(box->list);
157 228
158 while (box->active && !GNT_WIDGET_IS_FLAG_SET(box->active->data, GNT_WIDGET_CAN_TAKE_FOCUS)) 229 while (box->active && !GNT_WIDGET_IS_FLAG_SET(box->active, GNT_WIDGET_CAN_TAKE_FOCUS))
159 { 230 {
160 box->active = box->active->next; 231 box->active = box->active->next;
161 investigated++; 232 investigated++;
162 } 233 }
163 234
169 { 240 {
170 box->active = box->active->next; 241 box->active = box->active->next;
171 investigated++; 242 investigated++;
172 } 243 }
173 } 244 }
245
246 if (box->active)
247 gnt_widget_set_focus(box->active->data, TRUE);
248 if (w && w != box->active->data)
249 gnt_widget_set_focus(w, FALSE);
250 #endif
174 } 251 }
175 252
176 static gboolean 253 static gboolean
177 gnt_box_key_pressed(GntWidget *widget, const char *text) 254 gnt_box_key_pressed(GntWidget *widget, const char *text)
178 { 255 {
179 GntBox *box = GNT_BOX(widget); 256 GntBox *box = GNT_BOX(widget);
180 257
181 ensure_active(box); 258 if (box->active == NULL && !find_focusable_widget(box))
182 if (box->active == NULL)
183 return FALSE; 259 return FALSE;
184 260
185 if (gnt_widget_key_pressed(box->active->data, text)) 261 if (gnt_widget_key_pressed(box->active, text))
186 return TRUE; 262 return TRUE;
187 263
188 if (text[0] == 27) 264 if (text[0] == 27)
189 { 265 {
266 #if 0
190 GList *now = NULL; 267 GList *now = NULL;
191 if (strcmp(text+1, GNT_KEY_LEFT) == 0) 268 if (strcmp(text+1, GNT_KEY_LEFT) == 0)
192 { 269 {
193 now = box->active->prev; 270 now = box->active->prev;
194 if (now == NULL) 271 if (now == NULL)
207 box->active = now; 284 box->active = now;
208 gnt_widget_set_focus(box->active->data, TRUE); 285 gnt_widget_set_focus(box->active->data, TRUE);
209 286
210 return TRUE; 287 return TRUE;
211 } 288 }
289 #endif
212 } 290 }
213 291
214 return FALSE; 292 return FALSE;
215 } 293 }
216 294
235 } 313 }
236 } 314 }
237 return NULL; 315 return NULL;
238 } 316 }
239 317
240 static void 318 #if 0
241 gnt_box_lost_focus(GntWidget *widget) 319 static void
320 gnt_box_set_focus(GntWidget *widget, gboolean set)
242 { 321 {
243 GntWidget *p = widget; 322 GntWidget *p = widget;
244 323
245 while (p->parent) 324 while (p->parent)
246 p = p->parent; 325 p = p->parent;
247 326
248 p = find_focused_widget(GNT_BOX(p)); 327 p = find_focused_widget(GNT_BOX(p));
249 if (p) 328 if (p)
250 gnt_widget_set_focus(p, FALSE); 329 gnt_widget_set_focus(p, set);
251 } 330 gnt_widget_draw(widget);
331 }
332
333 static void
334 gnt_box_lost_focus(GntWidget *widget)
335 {
336 gnt_box_set_focus(widget, FALSE);
337 }
338
339 static void
340 gnt_box_gained_focus(GntWidget *widget)
341 {
342 GntWidget *p;
343
344 while (widget->parent)
345 widget = widget->parent;
346
347 p = find_focused_widget(GNT_BOX(widget));
348 GNT_BOX(widget)->active = g_list_find(GNT_BOX(widget)->list, p);
349 if (p)
350 gnt_widget_draw(p);
351 }
352 #endif
252 353
253 static void 354 static void
254 gnt_box_destroy(GntWidget *w) 355 gnt_box_destroy(GntWidget *w)
255 { 356 {
256 GntBox *box = GNT_BOX(w); 357 GntBox *box = GNT_BOX(w);
284 parent_class->expose = gnt_box_expose; 385 parent_class->expose = gnt_box_expose;
285 parent_class->map = gnt_box_map; 386 parent_class->map = gnt_box_map;
286 parent_class->size_request = gnt_box_size_request; 387 parent_class->size_request = gnt_box_size_request;
287 parent_class->set_position = gnt_box_set_position; 388 parent_class->set_position = gnt_box_set_position;
288 parent_class->key_pressed = gnt_box_key_pressed; 389 parent_class->key_pressed = gnt_box_key_pressed;
390 #if 0
391 /* We are going to need this when there are multiple focusble widgets in a box */
289 parent_class->lost_focus = gnt_box_lost_focus; 392 parent_class->lost_focus = gnt_box_lost_focus;
393 parent_class->gained_focus = gnt_box_gained_focus;
394 #endif
290 395
291 DEBUG; 396 DEBUG;
292 } 397 }
293 398
294 static void 399 static void

mercurial