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_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 AtkStateSet* gail_menu_item_ref_state_set (AtkObject *obj);
37 static void gail_menu_item_finalize (GObject *object);
39 static void atk_action_interface_init (AtkActionIface *iface);
40 static gboolean gail_menu_item_do_action (AtkAction *action,
42 static gboolean idle_do_action (gpointer data);
43 static gint gail_menu_item_get_n_actions (AtkAction *action);
44 static G_CONST_RETURN gchar* gail_menu_item_get_description(AtkAction *action,
46 static G_CONST_RETURN gchar* gail_menu_item_get_name (AtkAction *action,
48 static G_CONST_RETURN gchar* gail_menu_item_get_keybinding (AtkAction *action,
50 static gboolean gail_menu_item_set_description(AtkAction *action,
53 static void menu_item_select (GtkItem *item);
54 static void menu_item_deselect (GtkItem *item);
55 static void menu_item_selection (GtkItem *item,
57 static gboolean find_accel (GtkAccelKey *key,
60 static gboolean find_accel_new (GtkAccelKey *key,
64 G_DEFINE_TYPE_WITH_CODE (GailMenuItem, gail_menu_item, GAIL_TYPE_ITEM,
65 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
68 gail_menu_item_class_init (GailMenuItemClass *klass)
70 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
71 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
73 gobject_class->finalize = gail_menu_item_finalize;
75 class->get_n_children = gail_menu_item_get_n_children;
76 class->ref_child = gail_menu_item_ref_child;
77 class->ref_state_set = gail_menu_item_ref_state_set;
78 class->initialize = gail_menu_item_real_initialize;
82 gail_menu_item_real_initialize (AtkObject *obj,
88 ATK_OBJECT_CLASS (gail_menu_item_parent_class)->initialize (obj, data);
90 g_signal_connect (data,
92 G_CALLBACK (menu_item_select),
94 g_signal_connect (data,
96 G_CALLBACK (menu_item_deselect),
98 widget = GTK_WIDGET (data);
99 parent = gtk_widget_get_parent (widget);
100 if (GTK_IS_MENU (parent))
102 GtkWidget *parent_widget;
104 parent_widget = gtk_menu_get_attach_widget (GTK_MENU (parent));
106 if (!GTK_IS_MENU_ITEM (parent_widget))
107 parent_widget = gtk_widget_get_parent (widget);
110 atk_object_set_parent (obj, gtk_widget_get_accessible (parent_widget));
113 g_object_set_data (G_OBJECT (obj), "atk-component-layer",
114 GINT_TO_POINTER (ATK_LAYER_POPUP));
116 if (GTK_IS_TEAROFF_MENU_ITEM (data))
117 obj->role = ATK_ROLE_TEAR_OFF_MENU_ITEM;
118 else if (GTK_IS_SEPARATOR_MENU_ITEM (data))
119 obj->role = ATK_ROLE_SEPARATOR;
121 obj->role = ATK_ROLE_MENU_ITEM;
125 gail_menu_item_init (GailMenuItem *menu_item)
127 menu_item->click_keybinding = NULL;
128 menu_item->click_description = NULL;
132 gail_menu_item_new (GtkWidget *widget)
135 AtkObject *accessible;
137 g_return_val_if_fail (GTK_IS_MENU_ITEM (widget), NULL);
139 if (gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget)))
140 return gail_sub_menu_item_new (widget);
142 object = g_object_new (GAIL_TYPE_MENU_ITEM, NULL);
144 accessible = ATK_OBJECT (object);
145 atk_object_initialize (accessible, widget);
151 get_children (GtkWidget *submenu)
155 children = gtk_container_get_children (GTK_CONTAINER (submenu));
156 if (g_list_length (children) == 0)
159 * If menu is empty it may be because the menu items are created only
160 * on demand. For example, in gnome-panel the menu items are created
161 * only when "show" signal is emitted on the menu.
163 * The following hack forces the menu items to be created.
165 if (!gtk_widget_get_visible (submenu))
167 GTK_WIDGET_SET_FLAGS (submenu, GTK_VISIBLE);
168 g_signal_emit_by_name (submenu, "show");
169 GTK_WIDGET_UNSET_FLAGS (submenu, GTK_VISIBLE);
171 g_list_free (children);
172 children = gtk_container_get_children (GTK_CONTAINER (submenu));
178 * If a menu item has a submenu return the items of the submenu as the
179 * accessible children; otherwise expose no accessible children.
182 gail_menu_item_get_n_children (AtkObject* obj)
188 g_return_val_if_fail (GAIL_IS_MENU_ITEM (obj), count);
190 widget = GTK_ACCESSIBLE (obj)->widget;
194 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
199 children = get_children (submenu);
200 count = g_list_length (children);
201 g_list_free (children);
207 gail_menu_item_ref_child (AtkObject *obj,
210 AtkObject *accessible;
214 g_return_val_if_fail (GAIL_IS_MENU_ITEM (obj), NULL);
215 g_return_val_if_fail ((i >= 0), NULL);
217 widget = GTK_ACCESSIBLE (obj)->widget;
221 submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
227 children = get_children (submenu);
228 tmp_list = g_list_nth (children, i);
231 g_list_free (children);
234 accessible = gtk_widget_get_accessible (GTK_WIDGET (tmp_list->data));
235 g_list_free (children);
236 g_object_ref (accessible);
245 gail_menu_item_ref_state_set (AtkObject *obj)
247 AtkObject *menu_item;
248 AtkStateSet *state_set, *parent_state_set;
250 state_set = ATK_OBJECT_CLASS (gail_menu_item_parent_class)->ref_state_set (obj);
252 menu_item = atk_object_get_parent (obj);
256 if (!GTK_IS_MENU_ITEM (GTK_ACCESSIBLE (menu_item)->widget))
259 parent_state_set = atk_object_ref_state_set (menu_item);
260 if (!atk_state_set_contains_state (parent_state_set, ATK_STATE_SELECTED))
262 atk_state_set_remove_state (state_set, ATK_STATE_FOCUSED);
263 atk_state_set_remove_state (state_set, ATK_STATE_SHOWING);
270 atk_action_interface_init (AtkActionIface *iface)
272 iface->do_action = gail_menu_item_do_action;
273 iface->get_n_actions = gail_menu_item_get_n_actions;
274 iface->get_description = gail_menu_item_get_description;
275 iface->get_name = gail_menu_item_get_name;
276 iface->get_keybinding = gail_menu_item_get_keybinding;
277 iface->set_description = gail_menu_item_set_description;
281 gail_menu_item_do_action (AtkAction *action,
287 GailMenuItem *gail_menu_item;
289 item = GTK_ACCESSIBLE (action)->widget;
291 /* State is defunct */
294 if (!gtk_widget_get_sensitive (item) || !gtk_widget_get_visible (item))
297 gail_menu_item = GAIL_MENU_ITEM (action);
298 if (gail_menu_item->action_idle_handler)
302 gail_menu_item->action_idle_handler =
303 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
305 g_object_ref (gail_menu_item),
306 (GDestroyNotify) g_object_unref);
315 ensure_menus_unposted (GailMenuItem *menu_item)
320 parent = atk_object_get_parent (ATK_OBJECT (menu_item));
323 if (GTK_IS_ACCESSIBLE (parent))
325 widget = GTK_ACCESSIBLE (parent)->widget;
326 if (GTK_IS_MENU (widget))
328 if (GTK_WIDGET_MAPPED (widget))
329 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
334 parent = atk_object_get_parent (parent);
339 idle_do_action (gpointer data)
342 GtkWidget *item_parent;
343 GailMenuItem *menu_item;
344 gboolean item_mapped;
346 menu_item = GAIL_MENU_ITEM (data);
347 menu_item->action_idle_handler = 0;
348 item = GTK_ACCESSIBLE (menu_item)->widget;
349 if (item == NULL /* State is defunct */ ||
350 !gtk_widget_get_sensitive (item) || !gtk_widget_get_visible (item))
353 item_parent = gtk_widget_get_parent (item);
354 gtk_menu_shell_select_item (GTK_MENU_SHELL (item_parent), item);
355 item_mapped = GTK_WIDGET_MAPPED (item);
357 * This is what is called when <Return> is pressed for a menu item
359 g_signal_emit_by_name (item_parent, "activate_current",
362 ensure_menus_unposted (menu_item);
368 gail_menu_item_get_n_actions (AtkAction *action)
371 * Menu item has 1 action
376 static G_CONST_RETURN gchar*
377 gail_menu_item_get_description (AtkAction *action,
384 item = GAIL_MENU_ITEM (action);
385 return item->click_description;
391 static G_CONST_RETURN gchar*
392 gail_menu_item_get_name (AtkAction *action,
401 static G_CONST_RETURN gchar*
402 gail_menu_item_get_keybinding (AtkAction *action,
406 * This function returns a string of the form A;B;C where
407 * A is the keybinding for the widget; B is the keybinding to traverse
408 * from the menubar and C is the accelerator.
409 * The items in the keybinding to traverse from the menubar are separated
412 GailMenuItem *gail_menu_item;
413 gchar *keybinding = NULL;
414 gchar *item_keybinding = NULL;
415 gchar *full_keybinding = NULL;
416 gchar *accelerator = NULL;
418 gail_menu_item = GAIL_MENU_ITEM (action);
422 GtkWidget *temp_item;
426 item = GTK_ACCESSIBLE (action)->widget;
428 /* State is defunct */
434 GdkModifierType mnemonic_modifier = 0;
436 gchar *key, *temp_keybinding;
438 child = gtk_bin_get_child (GTK_BIN (temp_item));
441 /* Possibly a tear off menu item; it could also be a menu
442 * separator generated by gtk_item_factory_create_items()
446 parent = gtk_widget_get_parent (temp_item);
450 * parent can be NULL when activating a window from the panel
454 g_return_val_if_fail (GTK_IS_MENU_SHELL (parent), NULL);
455 if (GTK_IS_MENU_BAR (parent))
459 toplevel = gtk_widget_get_toplevel (parent);
460 if (toplevel && GTK_IS_WINDOW (toplevel))
461 mnemonic_modifier = gtk_window_get_mnemonic_modifier (
462 GTK_WINDOW (toplevel));
464 if (GTK_IS_LABEL (child))
466 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (child));
467 if (key_val != GDK_VoidSymbol)
469 key = gtk_accelerator_name (key_val, mnemonic_modifier);
471 temp_keybinding = g_strconcat (key, ":", full_keybinding, NULL);
473 temp_keybinding = g_strconcat (key, NULL);
474 if (temp_item == item)
476 item_keybinding = g_strdup (key);
479 g_free (full_keybinding);
480 full_keybinding = temp_keybinding;
485 g_free (full_keybinding);
486 full_keybinding = NULL;
490 if (GTK_IS_MENU_BAR (parent))
491 /* We have reached the menu bar so we are finished */
493 g_return_val_if_fail (GTK_IS_MENU (parent), NULL);
494 temp_item = gtk_menu_get_attach_widget (GTK_MENU (parent));
495 if (!GTK_IS_MENU_ITEM (temp_item))
498 * Menu is attached to something other than a menu item;
499 * probably an option menu
501 g_free (full_keybinding);
502 full_keybinding = NULL;
507 parent = gtk_widget_get_parent (item);
508 if (GTK_IS_MENU (parent))
510 GtkAccelGroup *group;
513 group = gtk_menu_get_accel_group (GTK_MENU (parent));
517 key = gtk_accel_group_find (group, find_accel, item);
522 * If the menu item is created using GtkAction and GtkUIManager
526 child = GTK_BIN (item)->child;
527 if (GTK_IS_ACCEL_LABEL (child))
529 GtkAccelLabel *accel_label;
531 accel_label = GTK_ACCEL_LABEL (child);
532 if (accel_label->accel_closure)
534 key = gtk_accel_group_find (accel_label->accel_group,
536 accel_label->accel_closure);
544 accelerator = gtk_accelerator_name (key->accel_key,
550 * Concatenate the bindings
552 if (item_keybinding || full_keybinding || accelerator)
557 keybinding = g_strconcat (item_keybinding, KEYBINDING_SEPARATOR, NULL);
558 g_free (item_keybinding);
561 keybinding = g_strconcat (KEYBINDING_SEPARATOR, NULL);
565 temp = g_strconcat (keybinding, full_keybinding,
566 KEYBINDING_SEPARATOR, NULL);
567 g_free (full_keybinding);
570 temp = g_strconcat (keybinding, KEYBINDING_SEPARATOR, NULL);
576 temp = g_strconcat (keybinding, accelerator, NULL);
577 g_free (accelerator);
582 g_free (gail_menu_item->click_keybinding);
583 gail_menu_item->click_keybinding = keybinding;
588 gail_menu_item_set_description (AtkAction *action,
596 item = GAIL_MENU_ITEM (action);
597 g_free (item->click_description);
598 item->click_description = g_strdup (desc);
606 gail_menu_item_finalize (GObject *object)
608 GailMenuItem *menu_item = GAIL_MENU_ITEM (object);
610 g_free (menu_item->click_keybinding);
611 g_free (menu_item->click_description);
612 if (menu_item->action_idle_handler)
614 g_source_remove (menu_item->action_idle_handler);
615 menu_item->action_idle_handler = 0;
618 G_OBJECT_CLASS (gail_menu_item_parent_class)->finalize (object);
622 menu_item_select (GtkItem *item)
624 menu_item_selection (item, TRUE);
628 menu_item_deselect (GtkItem *item)
630 menu_item_selection (item, FALSE);
634 menu_item_selection (GtkItem *item,
637 AtkObject *obj, *parent;
640 obj = gtk_widget_get_accessible (GTK_WIDGET (item));
641 atk_object_notify_state_change (obj, ATK_STATE_SELECTED, selected);
643 for (i = 0; i < atk_object_get_n_accessible_children (obj); i++)
646 child = atk_object_ref_accessible_child (obj, i);
647 atk_object_notify_state_change (child, ATK_STATE_SHOWING, selected);
648 g_object_unref (child);
650 parent = atk_object_get_parent (obj);
651 g_signal_emit_by_name (parent, "selection_changed");
655 find_accel (GtkAccelKey *key,
660 * We assume that closure->data points to the widget
661 * pending gtk_widget_get_accel_closures being made public
663 return data == (gpointer) closure->data;
667 find_accel_new (GtkAccelKey *key,
671 return data == (gpointer) closure;