comparison finch/libgnt/gntbox.c @ 25322:73e88188a7d4

Allow rebinding keys to change the focused widget.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Tue, 13 Jan 2009 23:13:32 +0000
parents d03b32530854
children c67d43408daa
comparison
equal deleted inserted replaced
25321:8c2631eb5da7 25322:73e88188a7d4
19 * along with this program; if not, write to the Free Software 19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 */ 21 */
22 22
23 #include "gntbox.h" 23 #include "gntbox.h"
24 #include "gntstyle.h"
24 #include "gntutils.h" 25 #include "gntutils.h"
25 26
26 #include <string.h> 27 #include <string.h>
27 28
28 enum 29 enum
302 303
303 static gboolean 304 static gboolean
304 gnt_box_key_pressed(GntWidget *widget, const char *text) 305 gnt_box_key_pressed(GntWidget *widget, const char *text)
305 { 306 {
306 GntBox *box = GNT_BOX(widget); 307 GntBox *box = GNT_BOX(widget);
307 GntWidget *now; 308 gboolean ret;
309
310 if (!GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_DISABLE_ACTIONS))
311 return FALSE;
308 312
309 if (box->active == NULL && !find_focusable_widget(box)) 313 if (box->active == NULL && !find_focusable_widget(box))
310 return FALSE; 314 return FALSE;
311 315
312 if (gnt_widget_key_pressed(box->active, text)) 316 if (gnt_widget_key_pressed(box->active, text))
313 return TRUE; 317 return TRUE;
314 318
319 /* This dance is necessary to make sure that the child widgets get a chance
320 to trigger their bindings first */
321 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_DISABLE_ACTIONS);
322 ret = gnt_widget_key_pressed(widget, text);
323 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_DISABLE_ACTIONS);
324 return ret;
325 }
326
327 static gboolean
328 box_focus_change(GntBox *box, gboolean next)
329 {
330 GntWidget *now;
315 now = box->active; 331 now = box->active;
316 332
317 if (text[0] == 27) 333 if (next) {
318 {
319 if (strcmp(text, GNT_KEY_LEFT) == 0)
320 {
321 find_prev_focus(box);
322 }
323 else if (strcmp(text, GNT_KEY_RIGHT) == 0)
324 {
325 find_next_focus(box);
326 }
327 else if (strcmp(text, GNT_KEY_BACK_TAB) == 0)
328 {
329 find_prev_focus(box);
330 }
331 }
332 else if (text[0] == '\t')
333 {
334 find_next_focus(box); 334 find_next_focus(box);
335 } 335 } else {
336 336 find_prev_focus(box);
337 if (now && now != box->active) 337 }
338 { 338
339 if (now && now != box->active) {
339 gnt_widget_set_focus(now, FALSE); 340 gnt_widget_set_focus(now, FALSE);
340 gnt_widget_set_focus(box->active, TRUE); 341 gnt_widget_set_focus(box->active, TRUE);
341 return TRUE; 342 return TRUE;
342 } 343 }
343 344
344 return FALSE; 345 return FALSE;
346 }
347
348 static gboolean
349 action_focus_next(GntBindable *bindable, GList *null)
350 {
351 return box_focus_change(GNT_BOX(bindable), TRUE);
352 }
353
354 static gboolean
355 action_focus_prev(GntBindable *bindable, GList *null)
356 {
357 return box_focus_change(GNT_BOX(bindable), FALSE);
345 } 358 }
346 359
347 static void 360 static void
348 gnt_box_lost_focus(GntWidget *widget) 361 gnt_box_lost_focus(GntWidget *widget)
349 { 362 {
554 } 567 }
555 568
556 static void 569 static void
557 gnt_box_class_init(GntBoxClass *klass) 570 gnt_box_class_init(GntBoxClass *klass)
558 { 571 {
572 GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
559 GObjectClass *gclass = G_OBJECT_CLASS(klass); 573 GObjectClass *gclass = G_OBJECT_CLASS(klass);
560 parent_class = GNT_WIDGET_CLASS(klass); 574 parent_class = GNT_WIDGET_CLASS(klass);
561 parent_class->destroy = gnt_box_destroy; 575 parent_class->destroy = gnt_box_destroy;
562 parent_class->draw = gnt_box_draw; 576 parent_class->draw = gnt_box_draw;
563 parent_class->expose = gnt_box_expose; 577 parent_class->expose = gnt_box_expose;
587 "Whether the child widgets in the box should have the same size.", 601 "Whether the child widgets in the box should have the same size.",
588 TRUE, 602 TRUE,
589 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB 603 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
590 ) 604 )
591 ); 605 );
606
607 gnt_bindable_class_register_action(bindable, "focus-next", action_focus_next,
608 "\t", NULL);
609 gnt_bindable_register_binding(bindable, "focus-next", GNT_KEY_RIGHT, NULL);
610 gnt_bindable_class_register_action(bindable, "focus-prev", action_focus_prev,
611 GNT_KEY_BACK_TAB, NULL);
612 gnt_bindable_register_binding(bindable, "focus-prev", GNT_KEY_LEFT, NULL);
613
614 gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable);
592 } 615 }
593 616
594 static void 617 static void
595 gnt_box_init(GTypeInstance *instance, gpointer class) 618 gnt_box_init(GTypeInstance *instance, gpointer class)
596 { 619 {
597 GntWidget *widget = GNT_WIDGET(instance); 620 GntWidget *widget = GNT_WIDGET(instance);
598 GntBox *box = GNT_BOX(widget); 621 GntBox *box = GNT_BOX(widget);
599 /* Initially make both the height and width resizable. 622 /* Initially make both the height and width resizable.
600 * Update the flags as necessary when widgets are added to it. */ 623 * Update the flags as necessary when widgets are added to it. */
601 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y); 624 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y);
602 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_CAN_TAKE_FOCUS); 625 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_CAN_TAKE_FOCUS | GNT_WIDGET_DISABLE_ACTIONS);
603 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW); 626 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
604 box->pad = 1; 627 box->pad = 1;
605 box->fill = TRUE; 628 box->fill = TRUE;
606 GNTDEBUG; 629 GNTDEBUG;
607 } 630 }