1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <gdk/gdkkeysyms.h>
22 #include "gailmenuitem.h"
23 #include "gailsubmenuitem.h"
25 #define KEYBINDING_SEPARATOR ";"
27 static void gail_menu_item_class_init (GailMenuItemClass *klass);
28 static void gail_menu_item_object_init (GailMenuItem *menu_item);
30 static void gail_menu_item_real_initialize
33 static gint gail_menu_item_get_n_children (AtkObject *obj);
34 static AtkObject* gail_menu_item_ref_child (AtkObject *obj,
36 static void gail_menu_item_finalize (GObject *object);
38 static void atk_action_interface_init (AtkActionIface *iface);
39 static gboolean gail_menu_item_do_action (AtkAction *action,
41 static gboolean idle_do_action (gpointer data);
42 static gint gail_menu_item_get_n_actions (AtkAction *action);
43 static G_CONST_RETURN gchar* gail_menu_item_get_description(AtkAction *action,
45 static G_CONST_RETURN gchar* gail_menu_item_get_name (AtkAction *action,
47 static G_CONST_RETURN gchar* gail_menu_item_get_keybinding (AtkAction *action,
49 static gboolean gail_menu_item_set_description(AtkAction *action,
52 static void menu_item_select (GtkItem *item);
53 static void menu_item_deselect (GtkItem *item);
54 static void menu_item_selection (GtkItem *item,
56 static gboolean find_accel (GtkAccelKey *key,
59 static gboolean find_accel_new (GtkAccelKey *key,
63 static gpointer parent_class = NULL;
66 gail_menu_item_get_type (void)
68 static GType type = 0;
72 static const GTypeInfo tinfo =
74 sizeof (GailMenuItemClass),
75 (GBaseInitFunc) NULL, /* base init */
76 (GBaseFinalizeFunc) NULL, /* base finalize */
77 (GClassInitFunc) gail_menu_item_class_init, /* class init */
78 (GClassFinalizeFunc) NULL, /* class finalize */
79 NULL, /* class data */
80 sizeof (GailMenuItem), /* instance size */
82 (GInstanceInitFunc) gail_menu_item_object_init, /* instance init */
83 NULL /* value table */
86 static const GInterfaceInfo atk_action_info =
88 (GInterfaceInitFunc) atk_action_interface_init,
89 (GInterfaceFinalizeFunc) NULL,
93 type = g_type_register_static (GAIL_TYPE_ITEM,
94 "GailMenuItem", &tinfo, 0);
95 g_type_add_interface_static (type, ATK_TYPE_ACTION,
102 gail_menu_item_class_init (GailMenuItemClass *klass)
104 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
105 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
107 gobject_class->finalize = gail_menu_item_finalize;
109 class->get_n_children = gail_menu_item_get_n_children;
110 class->ref_child = gail_menu_item_ref_child;
111 class->initialize = gail_menu_item_real_initialize;
113 parent_class = g_type_class_peek_parent (klass);
117 gail_menu_item_real_initialize (AtkObject *obj,
123 ATK_OBJECT_CLASS (parent_class)->initialize (obj, data);
125 g_signal_connect (data,
127 G_CALLBACK (menu_item_select),
129 g_signal_connect (data,
131 G_CALLBACK (menu_item_deselect),
133 widget = GTK_WIDGET (data);
134 parent = gtk_widget_get_parent (widget);
135 if (GTK_IS_MENU (parent))
137 GtkWidget *parent_widget;
139 parent_widget = gtk_menu_get_attach_widget (GTK_MENU (parent));
141 if (!GTK_IS_MENU_ITEM (parent_widget))
142 parent_widget = gtk_widget_get_parent (widget);
145 atk_object_set_parent (obj, gtk_widget_get_accessible (parent_widget));
148 g_object_set_data (G_OBJECT (obj), "atk-component-layer",
149 GINT_TO_POINTER (ATK_LAYER_POPUP));
151 if (GTK_IS_TEAROFF_MENU_ITEM (data))
152 obj->role = ATK_ROLE_TEAR_OFF_MENU_ITEM;
153 else if (GTK_IS_SEPARATOR_MENU_ITEM (data))
154 obj->role = ATK_ROLE_SEPARATOR;
156 obj->role = ATK_ROLE_MENU_ITEM;
160 gail_menu_item_object_init (GailMenuItem *menu_item)
162 menu_item->click_keybinding = NULL;
163 menu_item->click_description = NULL;
167 gail_menu_item_new (GtkWidget *widget)
170 AtkObject *accessible;
172 g_return_val_if_fail (GTK_IS_MENU_ITEM (widget), NULL);
174 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget)))
175 return gail_sub_menu_item_new (widget);
177 object = g_object_new (GAIL_TYPE_MENU_ITEM, NULL);
179 accessible = ATK_OBJECT (object);
180 atk_object_initialize (accessible, widget);
186 get_children (GtkWidget *submenu)
190 children = gtk_container_get_children (GTK_CONTAINER (submenu));
191 if (g_list_length (children) == 0)
194 * If menu is empty it may be because the menu items are created only
195 * on demand. For example, in gnome-panel the menu items are created
196 * only when "show" signal is emitted on the menu.
198 * The following hack forces the menu items to be created.
200 if (!GTK_WIDGET_VISIBLE (submenu))
202 GTK_WIDGET_SET_FLAGS (submenu, GTK_VISIBLE);
203 g_signal_emit_by_name (submenu, "show");
204 GTK_WIDGET_UNSET_FLAGS (submenu, GTK_VISIBLE);
206 g_list_free (children);
207 children = gtk_container_get_children (GTK_CONTAINER (submenu));
213 * If a menu item has a submenu return the items of the submenu as the
214 * accessible children; otherwise expose no accessible children.
217 gail_menu_item_get_n_children (AtkObject* obj)
223 g_return_val_if_fail (GAIL_IS_MENU_ITEM (obj), count);
225 widget = GTK_ACCESSIBLE (obj)->widget;
229 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
234 children = get_children (submenu);
235 count = g_list_length (children);
236 g_list_free (children);
242 gail_menu_item_ref_child (AtkObject *obj,
245 AtkObject *accessible;
249 g_return_val_if_fail (GAIL_IS_MENU_ITEM (obj), NULL);
250 g_return_val_if_fail ((i >= 0), NULL);
252 widget = GTK_ACCESSIBLE (obj)->widget;
256 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
262 children = get_children (submenu);
263 tmp_list = g_list_nth (children, i);
266 g_list_free (children);
269 accessible = gtk_widget_get_accessible (GTK_WIDGET (tmp_list->data));
270 g_list_free (children);
271 g_object_ref (accessible);
280 atk_action_interface_init (AtkActionIface *iface)
282 g_return_if_fail (iface != NULL);
284 iface->do_action = gail_menu_item_do_action;
285 iface->get_n_actions = gail_menu_item_get_n_actions;
286 iface->get_description = gail_menu_item_get_description;
287 iface->get_name = gail_menu_item_get_name;
288 iface->get_keybinding = gail_menu_item_get_keybinding;
289 iface->set_description = gail_menu_item_set_description;
293 gail_menu_item_do_action (AtkAction *action,
299 GailMenuItem *gail_menu_item;
301 item = GTK_ACCESSIBLE (action)->widget;
303 /* State is defunct */
306 if (!GTK_WIDGET_SENSITIVE (item) || !GTK_WIDGET_VISIBLE (item))
309 gail_menu_item = GAIL_MENU_ITEM (action);
310 if (gail_menu_item->action_idle_handler)
314 g_object_ref (gail_menu_item);
315 gail_menu_item->action_idle_handler = g_idle_add (idle_do_action, gail_menu_item);
324 ensure_menus_unposted (GailMenuItem *menu_item)
329 parent = atk_object_get_parent (ATK_OBJECT (menu_item));
332 if (GTK_IS_ACCESSIBLE (parent))
334 widget = GTK_ACCESSIBLE (parent)->widget;
335 if (GTK_IS_MENU (widget))
337 if (GTK_WIDGET_MAPPED (widget))
338 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
343 parent = atk_object_get_parent (parent);
348 idle_do_action (gpointer data)
351 GtkWidget *item_parent;
352 GailMenuItem *menu_item;
353 gboolean item_mapped;
355 GDK_THREADS_ENTER ();
357 menu_item = GAIL_MENU_ITEM (data);
358 menu_item->action_idle_handler = 0;
359 item = GTK_ACCESSIBLE (menu_item)->widget;
360 if (item == NULL /* State is defunct */ ||
361 !GTK_WIDGET_SENSITIVE (item) || !GTK_WIDGET_VISIBLE (item))
363 g_object_unref (menu_item);
364 GDK_THREADS_LEAVE ();
368 item_parent = gtk_widget_get_parent (item);
369 gtk_menu_shell_select_item (GTK_MENU_SHELL (item_parent), item);
370 item_mapped = GTK_WIDGET_MAPPED (item);
372 * This is what is called when <Return> is pressed for a menu item
374 g_signal_emit_by_name (item_parent, "activate_current",
377 ensure_menus_unposted (menu_item);
379 g_object_unref (menu_item);
380 GDK_THREADS_LEAVE ();
386 gail_menu_item_get_n_actions (AtkAction *action)
389 * Menu item has 1 action
394 static G_CONST_RETURN gchar*
395 gail_menu_item_get_description (AtkAction *action,
402 item = GAIL_MENU_ITEM (action);
403 return item->click_description;
409 static G_CONST_RETURN gchar*
410 gail_menu_item_get_name (AtkAction *action,
419 static G_CONST_RETURN gchar*
420 gail_menu_item_get_keybinding (AtkAction *action,
424 * This function returns a string of the form A;B;C where
425 * A is the keybinding for the widget; B is the keybinding to traverse
426 * from the menubar and C is the accelerator.
427 * The items in the keybinding to traverse from the menubar are separated
430 GailMenuItem *gail_menu_item;
431 gchar *keybinding = NULL;
432 gchar *item_keybinding = NULL;
433 gchar *full_keybinding = NULL;
434 gchar *accelerator = NULL;
436 gail_menu_item = GAIL_MENU_ITEM (action);
440 GtkWidget *temp_item;
444 item = GTK_ACCESSIBLE (action)->widget;
446 /* State is defunct */
452 GdkModifierType mnemonic_modifier = 0;
454 gchar *key, *temp_keybinding;
456 child = gtk_bin_get_child (GTK_BIN (temp_item));
459 /* Possibly a tear off menu item; it could also be a menu
460 * separator generated by gtk_item_factory_create_items()
464 parent = gtk_widget_get_parent (temp_item);
468 * parent can be NULL when activating a window from the panel
472 g_return_val_if_fail (GTK_IS_MENU_SHELL (parent), NULL);
473 if (GTK_IS_MENU_BAR (parent))
477 toplevel = gtk_widget_get_toplevel (parent);
478 if (toplevel && GTK_IS_WINDOW (toplevel))
479 mnemonic_modifier = gtk_window_get_mnemonic_modifier (
480 GTK_WINDOW (toplevel));
482 if (GTK_IS_LABEL (child))
484 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (child));
485 if (key_val != GDK_VoidSymbol)
487 key = gtk_accelerator_name (key_val, mnemonic_modifier);
489 temp_keybinding = g_strconcat (key, ":", full_keybinding, NULL);
491 temp_keybinding = g_strconcat (key, NULL);
492 if (temp_item == item)
494 item_keybinding = g_strdup (key);
497 g_free (full_keybinding);
498 full_keybinding = temp_keybinding;
503 g_free (full_keybinding);
504 full_keybinding = NULL;
508 if (GTK_IS_MENU_BAR (parent))
509 /* We have reached the menu bar so we are finished */
511 g_return_val_if_fail (GTK_IS_MENU (parent), NULL);
512 temp_item = gtk_menu_get_attach_widget (GTK_MENU (parent));
513 if (!GTK_IS_MENU_ITEM (temp_item))
516 * Menu is attached to something other than a menu item;
517 * probably an option menu
519 g_free (full_keybinding);
520 full_keybinding = NULL;
525 parent = gtk_widget_get_parent (item);
526 if (GTK_IS_MENU (parent))
528 GtkAccelGroup *group;
531 group = gtk_menu_get_accel_group (GTK_MENU (parent));
535 key = gtk_accel_group_find (group, find_accel, item);
540 * If the menu item is created using GtkAction and GtkUIManager
544 child = GTK_BIN (item)->child;
545 if (GTK_IS_ACCEL_LABEL (child))
547 GtkAccelLabel *accel_label;
549 accel_label = GTK_ACCEL_LABEL (child);
550 if (accel_label->accel_closure)
552 key = gtk_accel_group_find (accel_label->accel_group,
554 accel_label->accel_closure);
562 accelerator = gtk_accelerator_name (key->accel_key,
568 * Concatenate the bindings
570 if (item_keybinding || full_keybinding || accelerator)
575 keybinding = g_strconcat (item_keybinding, KEYBINDING_SEPARATOR, NULL);
576 g_free (item_keybinding);
579 keybinding = g_strconcat (KEYBINDING_SEPARATOR, NULL);
583 temp = g_strconcat (keybinding, full_keybinding,
584 KEYBINDING_SEPARATOR, NULL);
585 g_free (full_keybinding);
588 temp = g_strconcat (keybinding, KEYBINDING_SEPARATOR, NULL);
594 temp = g_strconcat (keybinding, accelerator, NULL);
595 g_free (accelerator);
600 g_free (gail_menu_item->click_keybinding);
601 gail_menu_item->click_keybinding = keybinding;
606 gail_menu_item_set_description (AtkAction *action,
614 item = GAIL_MENU_ITEM (action);
615 g_free (item->click_description);
616 item->click_description = g_strdup (desc);
624 gail_menu_item_finalize (GObject *object)
626 GailMenuItem *menu_item = GAIL_MENU_ITEM (object);
628 g_free (menu_item->click_keybinding);
629 g_free (menu_item->click_description);
630 if (menu_item->action_idle_handler)
632 g_source_remove (menu_item->action_idle_handler);
633 menu_item->action_idle_handler = 0;
636 G_OBJECT_CLASS (parent_class)->finalize (object);
640 menu_item_select (GtkItem *item)
642 menu_item_selection (item, TRUE);
646 menu_item_deselect (GtkItem *item)
648 menu_item_selection (item, FALSE);
652 menu_item_selection (GtkItem *item,
655 AtkObject *obj, *parent;
657 obj = gtk_widget_get_accessible (GTK_WIDGET (item));
658 atk_object_notify_state_change (obj, ATK_STATE_SELECTED, selected);
660 parent = atk_object_get_parent (obj);
661 g_signal_emit_by_name (parent, "selection_changed");
665 find_accel (GtkAccelKey *key,
670 * We assume that closure->data points to the widget
671 * pending gtk_widget_get_accel_closures being made public
673 return data == (gpointer) closure->data;
677 find_accel_new (GtkAccelKey *key,
681 return data == (gpointer) closure;