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 /* FIXME GTK_WIDGET_SET_FLAGS (submenu, GTK_VISIBLE); */
168 g_signal_emit_by_name (submenu, "show");
169 /* FIXME 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_get_widget (GTK_ACCESSIBLE (obj));
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_get_widget (GTK_ACCESSIBLE (obj));
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_get_widget (GTK_ACCESSIBLE (menu_item))))
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_get_widget (GTK_ACCESSIBLE (action));
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 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
324 if (GTK_IS_MENU (widget))
326 if (gtk_widget_get_mapped (widget))
327 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
331 parent = atk_object_get_parent (parent);
336 idle_do_action (gpointer data)
339 GtkWidget *item_parent;
340 GailMenuItem *menu_item;
341 gboolean item_mapped;
343 menu_item = GAIL_MENU_ITEM (data);
344 menu_item->action_idle_handler = 0;
345 item = gtk_accessible_get_widget (GTK_ACCESSIBLE (menu_item));
346 if (item == NULL /* State is defunct */ ||
347 !gtk_widget_get_sensitive (item) || !gtk_widget_get_visible (item))
350 item_parent = gtk_widget_get_parent (item);
351 gtk_menu_shell_select_item (GTK_MENU_SHELL (item_parent), item);
352 item_mapped = gtk_widget_get_mapped (item);
354 * This is what is called when <Return> is pressed for a menu item
356 g_signal_emit_by_name (item_parent, "activate_current",
359 ensure_menus_unposted (menu_item);
365 gail_menu_item_get_n_actions (AtkAction *action)
368 * Menu item has 1 action
373 static G_CONST_RETURN gchar*
374 gail_menu_item_get_description (AtkAction *action,
381 item = GAIL_MENU_ITEM (action);
382 return item->click_description;
388 static G_CONST_RETURN gchar*
389 gail_menu_item_get_name (AtkAction *action,
398 static G_CONST_RETURN gchar*
399 gail_menu_item_get_keybinding (AtkAction *action,
403 * This function returns a string of the form A;B;C where
404 * A is the keybinding for the widget; B is the keybinding to traverse
405 * from the menubar and C is the accelerator.
406 * The items in the keybinding to traverse from the menubar are separated
409 GailMenuItem *gail_menu_item;
410 gchar *keybinding = NULL;
411 gchar *item_keybinding = NULL;
412 gchar *full_keybinding = NULL;
413 gchar *accelerator = NULL;
415 gail_menu_item = GAIL_MENU_ITEM (action);
419 GtkWidget *temp_item;
423 item = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
425 /* State is defunct */
431 GdkModifierType mnemonic_modifier = 0;
433 gchar *key, *temp_keybinding;
435 child = gtk_bin_get_child (GTK_BIN (temp_item));
438 /* Possibly a tear off menu item; it could also be a menu
439 * separator generated by gtk_item_factory_create_items()
443 parent = gtk_widget_get_parent (temp_item);
447 * parent can be NULL when activating a window from the panel
451 g_return_val_if_fail (GTK_IS_MENU_SHELL (parent), NULL);
452 if (GTK_IS_MENU_BAR (parent))
456 toplevel = gtk_widget_get_toplevel (parent);
457 if (toplevel && GTK_IS_WINDOW (toplevel))
458 mnemonic_modifier = gtk_window_get_mnemonic_modifier (
459 GTK_WINDOW (toplevel));
461 if (GTK_IS_LABEL (child))
463 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (child));
464 if (key_val != GDK_VoidSymbol)
466 key = gtk_accelerator_name (key_val, mnemonic_modifier);
468 temp_keybinding = g_strconcat (key, ":", full_keybinding, NULL);
470 temp_keybinding = g_strconcat (key, NULL);
471 if (temp_item == item)
473 item_keybinding = g_strdup (key);
476 g_free (full_keybinding);
477 full_keybinding = temp_keybinding;
482 g_free (full_keybinding);
483 full_keybinding = NULL;
487 if (GTK_IS_MENU_BAR (parent))
488 /* We have reached the menu bar so we are finished */
490 g_return_val_if_fail (GTK_IS_MENU (parent), NULL);
491 temp_item = gtk_menu_get_attach_widget (GTK_MENU (parent));
492 if (!GTK_IS_MENU_ITEM (temp_item))
495 * Menu is attached to something other than a menu item;
496 * probably an option menu
498 g_free (full_keybinding);
499 full_keybinding = NULL;
504 parent = gtk_widget_get_parent (item);
505 if (GTK_IS_MENU (parent))
507 GtkAccelGroup *group;
510 group = gtk_menu_get_accel_group (GTK_MENU (parent));
514 key = gtk_accel_group_find (group, find_accel, item);
519 * If the menu item is created using GtkAction and GtkUIManager
523 child = GTK_BIN (item)->child;
524 if (GTK_IS_ACCEL_LABEL (child))
526 GtkAccelLabel *accel_label;
527 GClosure *accel_closure;
529 accel_label = GTK_ACCEL_LABEL (child);
530 g_object_get (accel_label,
531 "accel-closure", &accel_closure,
535 key = gtk_accel_group_find (gtk_accel_group_from_accel_closure (accel_closure),
545 accelerator = gtk_accelerator_name (key->accel_key,
551 * Concatenate the bindings
553 if (item_keybinding || full_keybinding || accelerator)
558 keybinding = g_strconcat (item_keybinding, KEYBINDING_SEPARATOR, NULL);
559 g_free (item_keybinding);
562 keybinding = g_strconcat (KEYBINDING_SEPARATOR, NULL);
566 temp = g_strconcat (keybinding, full_keybinding,
567 KEYBINDING_SEPARATOR, NULL);
568 g_free (full_keybinding);
571 temp = g_strconcat (keybinding, KEYBINDING_SEPARATOR, NULL);
577 temp = g_strconcat (keybinding, accelerator, NULL);
578 g_free (accelerator);
583 g_free (gail_menu_item->click_keybinding);
584 gail_menu_item->click_keybinding = keybinding;
589 gail_menu_item_set_description (AtkAction *action,
597 item = GAIL_MENU_ITEM (action);
598 g_free (item->click_description);
599 item->click_description = g_strdup (desc);
607 gail_menu_item_finalize (GObject *object)
609 GailMenuItem *menu_item = GAIL_MENU_ITEM (object);
611 g_free (menu_item->click_keybinding);
612 g_free (menu_item->click_description);
613 if (menu_item->action_idle_handler)
615 g_source_remove (menu_item->action_idle_handler);
616 menu_item->action_idle_handler = 0;
619 G_OBJECT_CLASS (gail_menu_item_parent_class)->finalize (object);
623 menu_item_select (GtkItem *item)
625 menu_item_selection (item, TRUE);
629 menu_item_deselect (GtkItem *item)
631 menu_item_selection (item, FALSE);
635 menu_item_selection (GtkItem *item,
638 AtkObject *obj, *parent;
641 obj = gtk_widget_get_accessible (GTK_WIDGET (item));
642 atk_object_notify_state_change (obj, ATK_STATE_SELECTED, selected);
644 for (i = 0; i < atk_object_get_n_accessible_children (obj); i++)
647 child = atk_object_ref_accessible_child (obj, i);
648 atk_object_notify_state_change (child, ATK_STATE_SHOWING, selected);
649 g_object_unref (child);
651 parent = atk_object_get_parent (obj);
652 g_signal_emit_by_name (parent, "selection_changed");
656 find_accel (GtkAccelKey *key,
661 * We assume that closure->data points to the widget
662 * pending gtk_widget_get_accel_closures being made public
664 return data == (gpointer) closure->data;
668 find_accel_new (GtkAccelKey *key,
672 return data == (gpointer) closure;