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 gail_menu_item->action_idle_handler =
315 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
317 g_object_ref (gail_menu_item),
318 (GDestroyNotify) g_object_unref);
327 ensure_menus_unposted (GailMenuItem *menu_item)
332 parent = atk_object_get_parent (ATK_OBJECT (menu_item));
335 if (GTK_IS_ACCESSIBLE (parent))
337 widget = GTK_ACCESSIBLE (parent)->widget;
338 if (GTK_IS_MENU (widget))
340 if (GTK_WIDGET_MAPPED (widget))
341 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
346 parent = atk_object_get_parent (parent);
351 idle_do_action (gpointer data)
354 GtkWidget *item_parent;
355 GailMenuItem *menu_item;
356 gboolean item_mapped;
358 menu_item = GAIL_MENU_ITEM (data);
359 menu_item->action_idle_handler = 0;
360 item = GTK_ACCESSIBLE (menu_item)->widget;
361 if (item == NULL /* State is defunct */ ||
362 !GTK_WIDGET_SENSITIVE (item) || !GTK_WIDGET_VISIBLE (item))
365 item_parent = gtk_widget_get_parent (item);
366 gtk_menu_shell_select_item (GTK_MENU_SHELL (item_parent), item);
367 item_mapped = GTK_WIDGET_MAPPED (item);
369 * This is what is called when <Return> is pressed for a menu item
371 g_signal_emit_by_name (item_parent, "activate_current",
374 ensure_menus_unposted (menu_item);
380 gail_menu_item_get_n_actions (AtkAction *action)
383 * Menu item has 1 action
388 static G_CONST_RETURN gchar*
389 gail_menu_item_get_description (AtkAction *action,
396 item = GAIL_MENU_ITEM (action);
397 return item->click_description;
403 static G_CONST_RETURN gchar*
404 gail_menu_item_get_name (AtkAction *action,
413 static G_CONST_RETURN gchar*
414 gail_menu_item_get_keybinding (AtkAction *action,
418 * This function returns a string of the form A;B;C where
419 * A is the keybinding for the widget; B is the keybinding to traverse
420 * from the menubar and C is the accelerator.
421 * The items in the keybinding to traverse from the menubar are separated
424 GailMenuItem *gail_menu_item;
425 gchar *keybinding = NULL;
426 gchar *item_keybinding = NULL;
427 gchar *full_keybinding = NULL;
428 gchar *accelerator = NULL;
430 gail_menu_item = GAIL_MENU_ITEM (action);
434 GtkWidget *temp_item;
438 item = GTK_ACCESSIBLE (action)->widget;
440 /* State is defunct */
446 GdkModifierType mnemonic_modifier = 0;
448 gchar *key, *temp_keybinding;
450 child = gtk_bin_get_child (GTK_BIN (temp_item));
453 /* Possibly a tear off menu item; it could also be a menu
454 * separator generated by gtk_item_factory_create_items()
458 parent = gtk_widget_get_parent (temp_item);
462 * parent can be NULL when activating a window from the panel
466 g_return_val_if_fail (GTK_IS_MENU_SHELL (parent), NULL);
467 if (GTK_IS_MENU_BAR (parent))
471 toplevel = gtk_widget_get_toplevel (parent);
472 if (toplevel && GTK_IS_WINDOW (toplevel))
473 mnemonic_modifier = gtk_window_get_mnemonic_modifier (
474 GTK_WINDOW (toplevel));
476 if (GTK_IS_LABEL (child))
478 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (child));
479 if (key_val != GDK_VoidSymbol)
481 key = gtk_accelerator_name (key_val, mnemonic_modifier);
483 temp_keybinding = g_strconcat (key, ":", full_keybinding, NULL);
485 temp_keybinding = g_strconcat (key, NULL);
486 if (temp_item == item)
488 item_keybinding = g_strdup (key);
491 g_free (full_keybinding);
492 full_keybinding = temp_keybinding;
497 g_free (full_keybinding);
498 full_keybinding = NULL;
502 if (GTK_IS_MENU_BAR (parent))
503 /* We have reached the menu bar so we are finished */
505 g_return_val_if_fail (GTK_IS_MENU (parent), NULL);
506 temp_item = gtk_menu_get_attach_widget (GTK_MENU (parent));
507 if (!GTK_IS_MENU_ITEM (temp_item))
510 * Menu is attached to something other than a menu item;
511 * probably an option menu
513 g_free (full_keybinding);
514 full_keybinding = NULL;
519 parent = gtk_widget_get_parent (item);
520 if (GTK_IS_MENU (parent))
522 GtkAccelGroup *group;
525 group = gtk_menu_get_accel_group (GTK_MENU (parent));
529 key = gtk_accel_group_find (group, find_accel, item);
534 * If the menu item is created using GtkAction and GtkUIManager
538 child = GTK_BIN (item)->child;
539 if (GTK_IS_ACCEL_LABEL (child))
541 GtkAccelLabel *accel_label;
543 accel_label = GTK_ACCEL_LABEL (child);
544 if (accel_label->accel_closure)
546 key = gtk_accel_group_find (accel_label->accel_group,
548 accel_label->accel_closure);
556 accelerator = gtk_accelerator_name (key->accel_key,
562 * Concatenate the bindings
564 if (item_keybinding || full_keybinding || accelerator)
569 keybinding = g_strconcat (item_keybinding, KEYBINDING_SEPARATOR, NULL);
570 g_free (item_keybinding);
573 keybinding = g_strconcat (KEYBINDING_SEPARATOR, NULL);
577 temp = g_strconcat (keybinding, full_keybinding,
578 KEYBINDING_SEPARATOR, NULL);
579 g_free (full_keybinding);
582 temp = g_strconcat (keybinding, KEYBINDING_SEPARATOR, NULL);
588 temp = g_strconcat (keybinding, accelerator, NULL);
589 g_free (accelerator);
594 g_free (gail_menu_item->click_keybinding);
595 gail_menu_item->click_keybinding = keybinding;
600 gail_menu_item_set_description (AtkAction *action,
608 item = GAIL_MENU_ITEM (action);
609 g_free (item->click_description);
610 item->click_description = g_strdup (desc);
618 gail_menu_item_finalize (GObject *object)
620 GailMenuItem *menu_item = GAIL_MENU_ITEM (object);
622 g_free (menu_item->click_keybinding);
623 g_free (menu_item->click_description);
624 if (menu_item->action_idle_handler)
626 g_source_remove (menu_item->action_idle_handler);
627 menu_item->action_idle_handler = 0;
630 G_OBJECT_CLASS (parent_class)->finalize (object);
634 menu_item_select (GtkItem *item)
636 menu_item_selection (item, TRUE);
640 menu_item_deselect (GtkItem *item)
642 menu_item_selection (item, FALSE);
646 menu_item_selection (GtkItem *item,
649 AtkObject *obj, *parent;
651 obj = gtk_widget_get_accessible (GTK_WIDGET (item));
652 atk_object_notify_state_change (obj, ATK_STATE_SELECTED, selected);
654 parent = atk_object_get_parent (obj);
655 g_signal_emit_by_name (parent, "selection_changed");
659 find_accel (GtkAccelKey *key,
664 * We assume that closure->data points to the widget
665 * pending gtk_widget_get_accel_closures being made public
667 return data == (gpointer) closure->data;
671 find_accel_new (GtkAccelKey *key,
675 return data == (gpointer) closure;