1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 * SECTION:gtkmenushell
29 * @Title: GtkMenuShell
30 * @Short_description: A base class for menu objects
32 * A #GtkMenuShell is the abstract base class used to derive the
33 * #GtkMenu and #GtkMenuBar subclasses.
35 * A #GtkMenuShell is a container of #GtkMenuItem objects arranged
36 * in a list which can be navigated, selected, and activated by the
37 * user to perform application functions. A #GtkMenuItem can have a
38 * submenu associated with it, allowing for nested hierarchical menus.
42 #include "gtkbindings.h"
43 #include "gtkkeyhash.h"
45 #include "gtkmainprivate.h"
46 #include "gtkmarshalers.h"
47 #include "gtkmenubar.h"
48 #include "gtkmenuitemprivate.h"
49 #include "gtkmenushellprivate.h"
50 #include "gtkmenuprivate.h"
51 #include "gtkmnemonichash.h"
52 #include "gtktearoffmenuitem.h"
53 #include "gtkwindow.h"
54 #include "gtkprivate.h"
56 #include "gtktypebuiltins.h"
57 #include "a11y/gtkmenushellaccessible.h"
59 #define MENU_SHELL_TIMEOUT 500
61 #define PACK_DIRECTION(m) \
62 (GTK_IS_MENU_BAR (m) \
63 ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
64 : GTK_PACK_DIRECTION_LTR)
85 * A menu item can be "selected", this means that it is displayed
86 * in the prelight state, and if it has a submenu, that submenu
89 * A menu is "active" when it is visible onscreen and the user
90 * is selecting from it. A menubar is not active until the user
91 * clicks on one of its menuitems. When a menu is active,
92 * passing the mouse over a submenu will pop it up.
94 * menu_shell->active_menu_item, is however, not an "active"
95 * menu item (there is no such thing) but rather, the selected
96 * menu item in that MenuShell, if there is one.
98 * There is also is a concept of the current menu and a current
99 * menu item. The current menu item is the selected menu item
100 * that is furthest down in the hierarchy. (Every active menu_shell
101 * does not necessarily contain a selected menu item, but if
102 * it does, then menu_shell->parent_menu_shell must also contain
103 * a selected menu item. The current menu is the menu that
104 * contains the current menu_item. It will always have a GTK
105 * grab and receive all key presses.
110 * ::move_current (GtkMenuDirection *dir)
111 * Moves the current menu item in direction 'dir':
113 * GTK_MENU_DIR_PARENT: To the parent menu shell
114 * GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
116 * GTK_MENU_DIR_NEXT/PREV: To the next or previous item
119 * As a a bit of a hack to get movement between menus and
120 * menubars working, if submenu_placement is different for
121 * the menu and its MenuShell then the following apply:
123 * - For 'parent' the current menu is not just moved to
124 * the parent, but moved to the previous entry in the parent
125 * - For 'child', if there is no child, then current is
126 * moved to the next item in the parent.
128 * Note that the above explanation of ::move_current was written
129 * before menus and menubars had support for RTL flipping and
130 * different packing directions, and therefore only applies for
131 * when text direction and packing direction are both left-to-right.
133 * ::activate_current (GBoolean *force_hide)
134 * Activate the current item. If 'force_hide' is true, hide
135 * the current menu item always. Otherwise, only hide
136 * it if menu_item->klass->hide_on_activate is true.
139 * Cancels the current selection
143 static void gtk_menu_shell_set_property (GObject *object,
147 static void gtk_menu_shell_get_property (GObject *object,
151 static void gtk_menu_shell_realize (GtkWidget *widget);
152 static void gtk_menu_shell_finalize (GObject *object);
153 static void gtk_menu_shell_dispose (GObject *object);
154 static gint gtk_menu_shell_button_press (GtkWidget *widget,
155 GdkEventButton *event);
156 static gint gtk_menu_shell_button_release (GtkWidget *widget,
157 GdkEventButton *event);
158 static gint gtk_menu_shell_key_press (GtkWidget *widget,
160 static gint gtk_menu_shell_enter_notify (GtkWidget *widget,
161 GdkEventCrossing *event);
162 static gint gtk_menu_shell_leave_notify (GtkWidget *widget,
163 GdkEventCrossing *event);
164 static void gtk_menu_shell_screen_changed (GtkWidget *widget,
165 GdkScreen *previous_screen);
166 static gboolean gtk_menu_shell_grab_broken (GtkWidget *widget,
167 GdkEventGrabBroken *event);
168 static void gtk_menu_shell_add (GtkContainer *container,
170 static void gtk_menu_shell_remove (GtkContainer *container,
172 static void gtk_menu_shell_forall (GtkContainer *container,
173 gboolean include_internals,
174 GtkCallback callback,
175 gpointer callback_data);
176 static void gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
179 static void gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell);
180 static gint gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
182 static GtkWidget *gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
184 static GType gtk_menu_shell_child_type (GtkContainer *container);
185 static void gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
186 GtkWidget *menu_item);
187 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell);
189 static void gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
190 GtkMenuDirectionType direction);
191 static void gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
192 gboolean force_hide);
193 static void gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell);
194 static void gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
195 GtkDirectionType dir);
197 static void gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell);
198 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
200 static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
203 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
205 G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
208 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
210 GObjectClass *object_class;
211 GtkWidgetClass *widget_class;
212 GtkContainerClass *container_class;
214 GtkBindingSet *binding_set;
216 object_class = (GObjectClass*) klass;
217 widget_class = (GtkWidgetClass*) klass;
218 container_class = (GtkContainerClass*) klass;
220 object_class->set_property = gtk_menu_shell_set_property;
221 object_class->get_property = gtk_menu_shell_get_property;
222 object_class->finalize = gtk_menu_shell_finalize;
223 object_class->dispose = gtk_menu_shell_dispose;
225 widget_class->realize = gtk_menu_shell_realize;
226 widget_class->button_press_event = gtk_menu_shell_button_press;
227 widget_class->button_release_event = gtk_menu_shell_button_release;
228 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
229 widget_class->key_press_event = gtk_menu_shell_key_press;
230 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
231 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
232 widget_class->screen_changed = gtk_menu_shell_screen_changed;
234 container_class->add = gtk_menu_shell_add;
235 container_class->remove = gtk_menu_shell_remove;
236 container_class->forall = gtk_menu_shell_forall;
237 container_class->child_type = gtk_menu_shell_child_type;
239 klass->submenu_placement = GTK_TOP_BOTTOM;
240 klass->deactivate = gtk_real_menu_shell_deactivate;
241 klass->selection_done = NULL;
242 klass->move_current = gtk_real_menu_shell_move_current;
243 klass->activate_current = gtk_real_menu_shell_activate_current;
244 klass->cancel = gtk_real_menu_shell_cancel;
245 klass->select_item = gtk_menu_shell_real_select_item;
246 klass->insert = gtk_menu_shell_real_insert;
247 klass->move_selected = gtk_menu_shell_real_move_selected;
250 * GtkMenuShell::deactivate:
251 * @menushell: the object which received the signal
253 * This signal is emitted when a menu shell is deactivated.
255 menu_shell_signals[DEACTIVATE] =
256 g_signal_new (I_("deactivate"),
257 G_OBJECT_CLASS_TYPE (object_class),
259 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
261 _gtk_marshal_VOID__VOID,
265 * GtkMenuShell::selection-done:
266 * @menushell: the object which received the signal
268 * This signal is emitted when a selection has been
269 * completed within a menu shell.
271 menu_shell_signals[SELECTION_DONE] =
272 g_signal_new (I_("selection-done"),
273 G_OBJECT_CLASS_TYPE (object_class),
275 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
277 _gtk_marshal_VOID__VOID,
281 * GtkMenuShell::move-current:
282 * @menushell: the object which received the signal
283 * @direction: the direction to move
285 * An keybinding signal which moves the current menu item
286 * in the direction specified by @direction.
288 menu_shell_signals[MOVE_CURRENT] =
289 g_signal_new (I_("move-current"),
290 G_OBJECT_CLASS_TYPE (object_class),
291 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
292 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
294 _gtk_marshal_VOID__ENUM,
296 GTK_TYPE_MENU_DIRECTION_TYPE);
299 * GtkMenuShell::activate-current:
300 * @menushell: the object which received the signal
301 * @force_hide: if %TRUE, hide the menu after activating the menu item
303 * An action signal that activates the current menu item within
306 menu_shell_signals[ACTIVATE_CURRENT] =
307 g_signal_new (I_("activate-current"),
308 G_OBJECT_CLASS_TYPE (object_class),
309 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
310 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
312 _gtk_marshal_VOID__BOOLEAN,
317 * GtkMenuShell::cancel:
318 * @menushell: the object which received the signal
320 * An action signal which cancels the selection within the menu shell.
321 * Causes the #GtkMenuShell::selection-done signal to be emitted.
323 menu_shell_signals[CANCEL] =
324 g_signal_new (I_("cancel"),
325 G_OBJECT_CLASS_TYPE (object_class),
326 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
327 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
329 _gtk_marshal_VOID__VOID,
333 * GtkMenuShell::cycle-focus:
334 * @menushell: the object which received the signal
335 * @direction: the direction to cycle in
337 * A keybinding signal which moves the focus in the
340 menu_shell_signals[CYCLE_FOCUS] =
341 g_signal_new_class_handler (I_("cycle-focus"),
342 G_OBJECT_CLASS_TYPE (object_class),
343 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
344 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
346 _gtk_marshal_VOID__ENUM,
348 GTK_TYPE_DIRECTION_TYPE);
351 * GtkMenuShell::move-selected:
352 * @menu_shell: the object on which the signal is emitted
353 * @distance: +1 to move to the next item, -1 to move to the previous
355 * The ::move-selected signal is emitted to move the selection to
358 * Returns: %TRUE to stop the signal emission, %FALSE to continue
362 menu_shell_signals[MOVE_SELECTED] =
363 g_signal_new (I_("move-selected"),
364 G_OBJECT_CLASS_TYPE (object_class),
366 G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
367 _gtk_boolean_handled_accumulator, NULL,
368 _gtk_marshal_BOOLEAN__INT,
373 * GtkMenuShell::insert:
374 * @menu_shell: the object on which the signal is emitted
375 * @child: the #GtkMenuItem that is being inserted
376 * @position: the position at which the insert occurs
378 * The ::insert signal is emitted when a new #GtkMenuItem is added to
379 * a #GtkMenuShell. A separate signal is used instead of
380 * GtkContainer::add because of the need for an additional position
383 * The inverse of this signal is the GtkContainer::removed signal.
387 menu_shell_signals[INSERT] =
388 g_signal_new (I_("insert"),
389 G_OBJECT_CLASS_TYPE (object_class),
391 G_STRUCT_OFFSET (GtkMenuShellClass, insert),
393 _gtk_marshal_VOID__OBJECT_INT,
394 G_TYPE_NONE, 2, GTK_TYPE_WIDGET, G_TYPE_INT);
397 binding_set = gtk_binding_set_by_class (klass);
398 gtk_binding_entry_add_signal (binding_set,
401 gtk_binding_entry_add_signal (binding_set,
403 "activate-current", 1,
406 gtk_binding_entry_add_signal (binding_set,
407 GDK_KEY_ISO_Enter, 0,
408 "activate-current", 1,
411 gtk_binding_entry_add_signal (binding_set,
413 "activate-current", 1,
416 gtk_binding_entry_add_signal (binding_set,
418 "activate-current", 1,
421 gtk_binding_entry_add_signal (binding_set,
423 "activate-current", 1,
426 gtk_binding_entry_add_signal (binding_set,
429 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
430 gtk_binding_entry_add_signal (binding_set,
431 GDK_KEY_F10, GDK_SHIFT_MASK,
433 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
436 * GtkMenuShell:take-focus:
438 * A boolean that determines whether the menu and its submenus grab the
439 * keyboard focus. See gtk_menu_shell_set_take_focus() and
440 * gtk_menu_shell_get_take_focus().
444 g_object_class_install_property (object_class,
446 g_param_spec_boolean ("take-focus",
448 P_("A boolean that determines whether the menu grabs the keyboard focus"),
450 GTK_PARAM_READWRITE));
452 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_SHELL_ACCESSIBLE);
454 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
458 gtk_menu_shell_child_type (GtkContainer *container)
460 return GTK_TYPE_MENU_ITEM;
464 gtk_menu_shell_init (GtkMenuShell *menu_shell)
466 GtkMenuShellPrivate *priv;
468 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_shell,
470 GtkMenuShellPrivate);
471 menu_shell->priv = priv;
472 priv->take_focus = TRUE;
476 gtk_menu_shell_set_property (GObject *object,
481 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
485 case PROP_TAKE_FOCUS:
486 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gtk_menu_shell_get_property (GObject *object,
500 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
504 case PROP_TAKE_FOCUS:
505 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514 gtk_menu_shell_finalize (GObject *object)
516 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
517 GtkMenuShellPrivate *priv = menu_shell->priv;
519 if (priv->mnemonic_hash)
520 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
522 _gtk_key_hash_free (priv->key_hash);
524 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
529 gtk_menu_shell_dispose (GObject *object)
531 gtk_menu_shell_deactivate (GTK_MENU_SHELL (object));
533 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->dispose (object);
537 * gtk_menu_shell_append:
538 * @menu_shell: a #GtkMenuShell
539 * @child: The #GtkMenuItem to add
541 * Adds a new #GtkMenuItem to the end of the menu shell's
545 gtk_menu_shell_append (GtkMenuShell *menu_shell,
548 gtk_menu_shell_insert (menu_shell, child, -1);
552 * gtk_menu_shell_prepend:
553 * @menu_shell: a #GtkMenuShell
554 * @child: The #GtkMenuItem to add
556 * Adds a new #GtkMenuItem to the beginning of the menu shell's
560 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
563 gtk_menu_shell_insert (menu_shell, child, 0);
567 * gtk_menu_shell_insert:
568 * @menu_shell: a #GtkMenuShell
569 * @child: The #GtkMenuItem to add
570 * @position: The position in the item list where @child
571 * is added. Positions are numbered from 0 to n-1
573 * Adds a new #GtkMenuItem to the menu shell's item list
574 * at the position indicated by @position.
577 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
581 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
582 g_return_if_fail (GTK_IS_MENU_ITEM (child));
584 g_signal_emit (menu_shell, menu_shell_signals[INSERT], 0, child, position);
588 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
592 GtkMenuShellPrivate *priv = menu_shell->priv;
594 priv->children = g_list_insert (priv->children, child, position);
596 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
600 * gtk_menu_shell_deactivate:
601 * @menu_shell: a #GtkMenuShell
603 * Deactivates the menu shell.
605 * Typically this results in the menu shell being erased
609 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
611 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
613 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
617 gtk_menu_shell_realize (GtkWidget *widget)
619 GtkAllocation allocation;
621 GdkWindowAttr attributes;
622 gint attributes_mask;
623 GtkStyleContext *context;
625 gtk_widget_set_realized (widget, TRUE);
627 gtk_widget_get_allocation (widget, &allocation);
629 attributes.x = allocation.x;
630 attributes.y = allocation.y;
631 attributes.width = allocation.width;
632 attributes.height = allocation.height;
633 attributes.window_type = GDK_WINDOW_CHILD;
634 attributes.wclass = GDK_INPUT_OUTPUT;
635 attributes.visual = gtk_widget_get_visual (widget);
636 attributes.event_mask = gtk_widget_get_events (widget);
637 attributes.event_mask |= (GDK_EXPOSURE_MASK |
638 GDK_BUTTON_PRESS_MASK |
639 GDK_BUTTON_RELEASE_MASK |
641 GDK_ENTER_NOTIFY_MASK |
642 GDK_LEAVE_NOTIFY_MASK);
644 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
646 window = gdk_window_new (gtk_widget_get_parent_window (widget),
647 &attributes, attributes_mask);
648 gtk_widget_set_window (widget, window);
649 gdk_window_set_user_data (window, widget);
651 context = gtk_widget_get_style_context (widget);
652 gtk_style_context_set_background (context, window);
656 gtk_menu_shell_activate (GtkMenuShell *menu_shell)
658 GtkMenuShellPrivate *priv = menu_shell->priv;
664 device = gtk_get_current_event_device ();
666 _gtk_menu_shell_set_grab_device (menu_shell, device);
667 gtk_device_grab_add (GTK_WIDGET (menu_shell), device, TRUE);
669 priv->have_grab = TRUE;
675 gtk_menu_shell_button_press (GtkWidget *widget,
676 GdkEventButton *event)
678 GtkMenuShell *menu_shell;
679 GtkMenuShellPrivate *priv;
680 GtkWidget *menu_item;
683 if (event->type != GDK_BUTTON_PRESS)
686 menu_shell = GTK_MENU_SHELL (widget);
687 priv = menu_shell->priv;
689 if (priv->parent_menu_shell)
690 return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
692 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
694 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
696 parent = gtk_widget_get_parent (menu_item);
698 if (menu_item != GTK_MENU_SHELL (parent)->priv->active_menu_item)
700 /* select the menu item *before* activating the shell, so submenus
701 * which might be open are closed the friendly way. If we activate
702 * (and thus grab) this menu shell first, we might get grab_broken
703 * events which will close the entire menu hierarchy. Selecting the
704 * menu item also fixes up the state as if enter_notify() would
705 * have run before (which normally selects the item).
707 if (GTK_MENU_SHELL_GET_CLASS (parent)->submenu_placement != GTK_TOP_BOTTOM)
708 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), menu_item);
712 if (!priv->active || !priv->button)
714 gboolean initially_active = priv->active;
716 priv->button = event->button;
720 if (_gtk_menu_item_is_selectable (menu_item) &&
721 gtk_widget_get_parent (menu_item) == widget &&
722 menu_item != priv->active_menu_item)
724 gtk_menu_shell_activate (menu_shell);
725 priv->button = event->button;
727 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
729 priv->activate_time = event->time;
730 gtk_menu_shell_select_item (menu_shell, menu_item);
736 if (!initially_active)
738 gboolean window_drag = FALSE;
740 gtk_widget_style_get (widget,
741 "window-dragging", &window_drag,
746 gtk_menu_shell_deactivate (menu_shell);
747 gtk_window_begin_move_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
758 widget = gtk_get_event_widget ((GdkEvent*) event);
759 if (widget == GTK_WIDGET (menu_shell))
761 gtk_menu_shell_deactivate (menu_shell);
762 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
767 _gtk_menu_item_is_selectable (menu_item) &&
768 GTK_MENU_ITEM (menu_item)->priv->submenu != NULL &&
769 !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
771 _gtk_menu_item_popup_submenu (menu_item, FALSE);
772 priv->activated_submenu = TRUE;
779 gtk_menu_shell_grab_broken (GtkWidget *widget,
780 GdkEventGrabBroken *event)
782 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
783 GtkMenuShellPrivate *priv = menu_shell->priv;
785 if (priv->have_xgrab && event->grab_window == NULL)
787 /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
788 gtk_menu_shell_deselect (menu_shell);
789 gtk_menu_shell_deactivate (menu_shell);
790 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
797 gtk_menu_shell_button_release (GtkWidget *widget,
798 GdkEventButton *event)
800 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
801 GtkMenuShellPrivate *priv = menu_shell->priv;
805 GtkWidget *menu_item;
806 gboolean deactivate = TRUE;
808 if (priv->button && (event->button != priv->button))
811 if (priv->parent_menu_shell)
812 return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
816 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
818 if ((event->time - priv->activate_time) > MENU_SHELL_TIMEOUT)
820 if (menu_item && (priv->active_menu_item == menu_item) &&
821 _gtk_menu_item_is_selectable (menu_item))
823 GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->priv->submenu;
827 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
830 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
831 priv->activated_submenu)
834 GTimeVal *popup_time;
835 gint64 usec_since_popup = 0;
837 g_object_get (gtk_widget_get_settings (widget),
838 "gtk-menu-popdown-delay", &popdown_delay,
841 popup_time = g_object_get_data (G_OBJECT (submenu),
842 "gtk-menu-exact-popup-time");
846 GTimeVal current_time;
848 g_get_current_time (¤t_time);
850 usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
851 (gint64) current_time.tv_usec -
852 (gint64) popup_time->tv_sec * 1000 * 1000 -
853 (gint64) popup_time->tv_usec);
855 g_object_set_data (G_OBJECT (submenu),
856 "gtk-menu-exact-popup-time", NULL);
859 /* Only close the submenu on click if we opened the
860 * menu explicitely (usec_since_popup == 0) or
861 * enough time has passed since it was opened by
862 * GtkMenuItem's timeout (usec_since_popup > delay).
864 if (!priv->activated_submenu &&
865 (usec_since_popup == 0 ||
866 usec_since_popup > popdown_delay * 1000))
868 _gtk_menu_item_popdown_submenu (menu_item);
872 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
878 else if (menu_item &&
879 !_gtk_menu_item_is_selectable (menu_item) &&
880 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
884 else if (priv->parent_menu_shell)
887 gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
891 /* If we ended up on an item with a submenu, leave the menu up. */
893 (priv->active_menu_item == menu_item) &&
894 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
899 else /* a very fast press-release */
901 /* We only ever want to prevent deactivation on the first
902 * press/release. Setting the time to zero is a bit of a
903 * hack, since we could be being triggered in the first
904 * few fractions of a second after a server time wraparound.
905 * the chances of that happening are ~1/10^6, without
906 * serious harm if we lose.
908 priv->activate_time = 0;
914 gtk_menu_shell_deactivate (menu_shell);
915 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
918 priv->activated_submenu = FALSE;
925 _gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
926 gboolean keyboard_mode)
928 menu_shell->priv->keyboard_mode = keyboard_mode;
932 _gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
934 return menu_shell->priv->keyboard_mode;
938 _gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
940 GtkMenuShell *target;
941 gboolean auto_mnemonics;
943 gboolean mnemonics_visible;
945 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
946 "gtk-auto-mnemonics", &auto_mnemonics,
956 GtkMenuShellPrivate *priv = target->priv;
957 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));
959 /* The idea with keyboard mode is that once you start using
960 * the keyboard to navigate the menus, we show mnemonics
961 * until the menu navigation is over. To that end, we spread
962 * the keyboard mode upwards in the menu hierarchy here.
963 * Also see gtk_menu_popup, where we inherit it downwards.
965 if (menu_shell->priv->keyboard_mode)
966 target->priv->keyboard_mode = TRUE;
968 /* While navigating menus, the first parent menu with an active
969 * item is the one where mnemonics are effective, as can be seen
970 * in gtk_menu_shell_key_press below.
971 * We also show mnemonics in context menus. The grab condition is
972 * necessary to ensure we remove underlines from menu bars when
975 mnemonics_visible = target->priv->keyboard_mode &&
976 (((target->priv->active_menu_item || priv->in_unselectable_item) && !found) ||
977 (target == menu_shell &&
978 !target->priv->parent_menu_shell &&
979 gtk_widget_has_grab (GTK_WIDGET (target))));
981 /* While menus are up, only show underlines inside the menubar,
982 * not in the entire window.
984 if (GTK_IS_MENU_BAR (target))
986 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
987 _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
991 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
993 if (target->priv->active_menu_item || priv->in_unselectable_item)
996 target = GTK_MENU_SHELL (target->priv->parent_menu_shell);
1001 gtk_menu_shell_key_press (GtkWidget *widget,
1004 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1005 GtkMenuShellPrivate *priv = menu_shell->priv;
1006 gboolean enable_mnemonics;
1008 priv->keyboard_mode = TRUE;
1010 if (!(priv->active_menu_item || priv->in_unselectable_item) &&
1011 priv->parent_menu_shell)
1012 return gtk_widget_event (priv->parent_menu_shell, (GdkEvent *)event);
1014 if (gtk_bindings_activate_event (G_OBJECT (widget), event))
1017 g_object_get (gtk_widget_get_settings (widget),
1018 "gtk-enable-mnemonics", &enable_mnemonics,
1021 if (enable_mnemonics)
1022 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
1028 gtk_menu_shell_enter_notify (GtkWidget *widget,
1029 GdkEventCrossing *event)
1031 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1032 GtkMenuShellPrivate *priv = menu_shell->priv;
1034 if (event->mode == GDK_CROSSING_GTK_GRAB ||
1035 event->mode == GDK_CROSSING_GTK_UNGRAB ||
1036 event->mode == GDK_CROSSING_STATE_CHANGED)
1041 GtkWidget *menu_item;
1044 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1049 if (GTK_IS_MENU_ITEM (menu_item) &&
1050 !_gtk_menu_item_is_selectable (menu_item))
1052 priv->in_unselectable_item = TRUE;
1056 parent = gtk_widget_get_parent (menu_item);
1057 if (parent == widget &&
1058 GTK_IS_MENU_ITEM (menu_item))
1060 if (priv->ignore_enter)
1063 if (event->detail != GDK_NOTIFY_INFERIOR)
1065 if ((gtk_widget_get_state_flags (menu_item) & GTK_STATE_FLAG_PRELIGHT) == 0)
1066 gtk_menu_shell_select_item (menu_shell, menu_item);
1068 /* If any mouse button is down, and there is a submenu
1069 * that is not yet visible, activate it. It's sufficient
1070 * to check for any button's mask (not only the one
1071 * matching menu_shell->button), because there is no
1072 * situation a mouse button could be pressed while
1073 * entering a menu item where we wouldn't want to show
1076 if ((event->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK)) &&
1077 GTK_MENU_ITEM (menu_item)->priv->submenu != NULL)
1079 GTK_MENU_SHELL (parent)->priv->activated_submenu = TRUE;
1081 if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
1083 gboolean touchscreen_mode;
1085 g_object_get (gtk_widget_get_settings (widget),
1086 "gtk-touchscreen-mode", &touchscreen_mode,
1089 if (touchscreen_mode)
1090 _gtk_menu_item_popup_submenu (menu_item, TRUE);
1095 else if (priv->parent_menu_shell)
1097 gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
1105 gtk_menu_shell_leave_notify (GtkWidget *widget,
1106 GdkEventCrossing *event)
1108 if (event->mode == GDK_CROSSING_GTK_GRAB ||
1109 event->mode == GDK_CROSSING_GTK_GRAB ||
1110 event->mode == GDK_CROSSING_STATE_CHANGED)
1113 if (gtk_widget_get_visible (widget))
1115 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1116 GtkMenuShellPrivate *priv = menu_shell->priv;
1117 GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
1118 GtkMenuItem *menu_item;
1120 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1123 menu_item = GTK_MENU_ITEM (event_widget);
1125 if (!_gtk_menu_item_is_selectable (event_widget))
1127 priv->in_unselectable_item = TRUE;
1131 if ((priv->active_menu_item == event_widget) &&
1132 (menu_item->priv->submenu == NULL))
1134 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1135 (gtk_widget_get_state_flags (GTK_WIDGET (menu_item)) & GTK_STATE_FLAG_PRELIGHT) != 0)
1137 gtk_menu_shell_deselect (menu_shell);
1140 else if (priv->parent_menu_shell)
1142 gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
1150 gtk_menu_shell_screen_changed (GtkWidget *widget,
1151 GdkScreen *previous_screen)
1153 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
1157 gtk_menu_shell_add (GtkContainer *container,
1160 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
1164 gtk_menu_shell_remove (GtkContainer *container,
1167 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1168 GtkMenuShellPrivate *priv = menu_shell->priv;
1171 was_visible = gtk_widget_get_visible (widget);
1172 priv->children = g_list_remove (priv->children, widget);
1174 if (widget == priv->active_menu_item)
1176 g_signal_emit_by_name (priv->active_menu_item, "deselect");
1177 priv->active_menu_item = NULL;
1180 gtk_widget_unparent (widget);
1182 /* Queue resize regardless of gtk_widget_get_visible (container),
1183 * since that's what is needed by toplevels.
1186 gtk_widget_queue_resize (GTK_WIDGET (container));
1190 gtk_menu_shell_forall (GtkContainer *container,
1191 gboolean include_internals,
1192 GtkCallback callback,
1193 gpointer callback_data)
1195 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1199 children = menu_shell->priv->children;
1202 child = children->data;
1203 children = children->next;
1205 (* callback) (child, callback_data);
1211 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
1213 GtkMenuShellPrivate *priv = menu_shell->priv;
1218 priv->active = FALSE;
1219 priv->activate_time = 0;
1221 if (priv->active_menu_item)
1223 gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
1224 priv->active_menu_item = NULL;
1227 if (priv->have_grab)
1229 priv->have_grab = FALSE;
1230 gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1232 if (priv->have_xgrab)
1234 GdkDevice *keyboard;
1236 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1237 keyboard = gdk_device_get_associated_device (priv->grab_pointer);
1240 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1242 priv->have_xgrab = FALSE;
1245 priv->keyboard_mode = FALSE;
1246 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1248 _gtk_menu_shell_update_mnemonics (menu_shell);
1253 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1258 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1259 g_return_val_if_fail (child != NULL, FALSE);
1261 parent = gtk_widget_get_parent (child);
1262 while (GTK_IS_MENU_SHELL (parent))
1264 if (parent == (GtkWidget*) menu_shell)
1266 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1273 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1276 GtkWidget *menu_item;
1278 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1280 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1281 menu_item = gtk_widget_get_parent (menu_item);
1283 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1289 /* Handlers for action signals */
1292 * gtk_menu_shell_select_item:
1293 * @menu_shell: a #GtkMenuShell
1294 * @menu_item: The #GtkMenuItem to select
1296 * Selects the menu item from the menu shell.
1299 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1300 GtkWidget *menu_item)
1302 GtkMenuShellPrivate *priv = menu_shell->priv;
1303 GtkMenuShellClass *class;
1305 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1306 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1308 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1310 if (class->select_item &&
1312 priv->active_menu_item == menu_item))
1313 class->select_item (menu_shell, menu_item);
1316 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1317 GtkSubmenuPlacement placement);
1320 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1321 GtkWidget *menu_item)
1323 GtkMenuShellPrivate *priv = menu_shell->priv;
1324 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1326 if (priv->active_menu_item)
1328 gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
1329 priv->active_menu_item = NULL;
1332 if (!_gtk_menu_item_is_selectable (menu_item))
1334 priv->in_unselectable_item = TRUE;
1335 _gtk_menu_shell_update_mnemonics (menu_shell);
1339 gtk_menu_shell_activate (menu_shell);
1341 priv->active_menu_item = menu_item;
1342 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1343 _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
1346 _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
1347 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1348 gtk_menu_item_select (GTK_MENU_ITEM (priv->active_menu_item));
1350 _gtk_menu_shell_update_mnemonics (menu_shell);
1352 /* This allows the bizarre radio buttons-with-submenus-display-history
1355 if (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1356 gtk_widget_activate (priv->active_menu_item);
1360 * gtk_menu_shell_deselect:
1361 * @menu_shell: a #GtkMenuShell
1363 * Deselects the currently selected item from the menu shell,
1367 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1369 GtkMenuShellPrivate *priv = menu_shell->priv;
1371 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1373 if (priv->active_menu_item)
1375 gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
1376 priv->active_menu_item = NULL;
1377 _gtk_menu_shell_update_mnemonics (menu_shell);
1382 * gtk_menu_shell_activate_item:
1383 * @menu_shell: a #GtkMenuShell
1384 * @menu_item: the #GtkMenuItem to activate
1385 * @force_deactivate: if %TRUE, force the deactivation of the
1386 * menu shell after the menu item is activated
1388 * Activates the menu item within the menu shell.
1391 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
1392 GtkWidget *menu_item,
1393 gboolean force_deactivate)
1395 GSList *slist, *shells = NULL;
1396 gboolean deactivate = force_deactivate;
1398 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1399 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1402 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1404 g_object_ref (menu_shell);
1405 g_object_ref (menu_item);
1409 GtkMenuShell *parent_menu_shell = menu_shell;
1413 g_object_ref (parent_menu_shell);
1414 shells = g_slist_prepend (shells, parent_menu_shell);
1415 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->priv->parent_menu_shell;
1417 while (parent_menu_shell);
1418 shells = g_slist_reverse (shells);
1420 gtk_menu_shell_deactivate (menu_shell);
1422 /* Flush the x-queue, so any grabs are removed and
1423 * the menu is actually taken down
1425 gdk_display_sync (gtk_widget_get_display (menu_item));
1428 gtk_widget_activate (menu_item);
1430 for (slist = shells; slist; slist = slist->next)
1432 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1433 g_object_unref (slist->data);
1435 g_slist_free (shells);
1437 g_object_unref (menu_shell);
1438 g_object_unref (menu_item);
1441 /* Distance should be +/- 1 */
1443 gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
1446 GtkMenuShellPrivate *priv = menu_shell->priv;
1448 if (priv->active_menu_item)
1450 GList *node = g_list_find (priv->children, priv->active_menu_item);
1451 GList *start_node = node;
1452 gboolean wrap_around;
1454 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1455 "gtk-keynav-wrap-around", &wrap_around,
1461 while (node != start_node &&
1462 (!node || !_gtk_menu_item_is_selectable (node->data)))
1466 else if (wrap_around)
1467 node = priv->children;
1470 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1478 while (node != start_node &&
1479 (!node || !_gtk_menu_item_is_selectable (node->data)))
1483 else if (wrap_around)
1484 node = g_list_last (priv->children);
1487 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1494 gtk_menu_shell_select_item (menu_shell, node->data);
1500 /* Distance should be +/- 1 */
1502 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1505 gboolean handled = FALSE;
1507 g_signal_emit (menu_shell, menu_shell_signals[MOVE_SELECTED], 0,
1508 distance, &handled);
1512 * gtk_menu_shell_select_first:
1513 * @menu_shell: a #GtkMenuShell
1514 * @search_sensitive: if %TRUE, search for the first selectable
1515 * menu item, otherwise select nothing if
1516 * the first item isn't sensitive. This
1517 * should be %FALSE if the menu is being
1518 * popped up initially.
1520 * Select the first visible or selectable child of the menu shell;
1521 * don't select tearoff items unless the only item is a tearoff
1527 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1528 gboolean search_sensitive)
1530 GtkMenuShellPrivate *priv = menu_shell->priv;
1531 GtkWidget *to_select = NULL;
1534 tmp_list = priv->children;
1537 GtkWidget *child = tmp_list->data;
1539 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1540 _gtk_menu_item_is_selectable (child))
1543 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1547 tmp_list = tmp_list->next;
1551 gtk_menu_shell_select_item (menu_shell, to_select);
1555 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1556 gboolean search_sensitive)
1558 GtkMenuShellPrivate *priv = menu_shell->priv;
1559 GtkWidget *to_select = NULL;
1562 tmp_list = g_list_last (priv->children);
1565 GtkWidget *child = tmp_list->data;
1567 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1568 _gtk_menu_item_is_selectable (child))
1571 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1575 tmp_list = tmp_list->prev;
1579 gtk_menu_shell_select_item (menu_shell, to_select);
1583 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1585 GtkMenuShellPrivate *priv = menu_shell->priv;
1586 GtkMenuItem *menu_item;
1588 if (priv->active_menu_item == NULL)
1591 menu_item = GTK_MENU_ITEM (priv->active_menu_item);
1593 if (menu_item->priv->submenu)
1595 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1596 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->priv->submenu), TRUE);
1597 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active_menu_item)
1605 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1606 GtkMenuDirectionType direction)
1608 GtkMenuShellPrivate *priv = menu_shell->priv;
1609 GtkMenuShell *parent_menu_shell = NULL;
1610 gboolean had_selection;
1611 gboolean touchscreen_mode;
1613 priv->in_unselectable_item = FALSE;
1615 had_selection = priv->active_menu_item != NULL;
1617 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1618 "gtk-touchscreen-mode", &touchscreen_mode,
1621 if (priv->parent_menu_shell)
1622 parent_menu_shell = GTK_MENU_SHELL (priv->parent_menu_shell);
1626 case GTK_MENU_DIR_PARENT:
1627 if (touchscreen_mode &&
1628 priv->active_menu_item &&
1629 GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu &&
1630 gtk_widget_get_visible (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu))
1632 /* if we are on a menu item that has an open submenu but the
1633 * focus is not in that submenu (e.g. because it's empty or
1634 * has only insensitive items), close that submenu instead of
1635 * running into the code below which would close *this* menu.
1637 _gtk_menu_item_popdown_submenu (priv->active_menu_item);
1638 _gtk_menu_shell_update_mnemonics (menu_shell);
1640 else if (parent_menu_shell)
1642 if (touchscreen_mode)
1644 /* close menu when returning from submenu. */
1645 _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->priv->parent_menu_item);
1646 _gtk_menu_shell_update_mnemonics (parent_menu_shell);
1650 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1651 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1652 gtk_menu_shell_deselect (menu_shell);
1655 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1656 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1658 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1659 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1662 /* If there is no parent and the submenu is in the opposite direction
1663 * to the menu, then make the PARENT direction wrap around to
1664 * the bottom of the submenu.
1666 else if (priv->active_menu_item &&
1667 _gtk_menu_item_is_selectable (priv->active_menu_item) &&
1668 GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1670 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu);
1672 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1673 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1674 _gtk_menu_shell_select_last (submenu, TRUE);
1678 case GTK_MENU_DIR_CHILD:
1679 if (priv->active_menu_item &&
1680 _gtk_menu_item_is_selectable (priv->active_menu_item) &&
1681 GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1683 if (gtk_menu_shell_select_submenu_first (menu_shell))
1687 /* Try to find a menu running the opposite direction */
1688 while (parent_menu_shell &&
1689 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1690 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1692 parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->priv->parent_menu_shell);
1695 if (parent_menu_shell)
1697 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1698 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1700 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1702 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1706 case GTK_MENU_DIR_PREV:
1707 gtk_menu_shell_move_selected (menu_shell, -1);
1708 if (!had_selection && !priv->active_menu_item && priv->children)
1709 _gtk_menu_shell_select_last (menu_shell, TRUE);
1712 case GTK_MENU_DIR_NEXT:
1713 gtk_menu_shell_move_selected (menu_shell, 1);
1714 if (!had_selection && !priv->active_menu_item && priv->children)
1715 gtk_menu_shell_select_first (menu_shell, TRUE);
1721 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1722 gboolean force_hide)
1724 GtkMenuShellPrivate *priv = menu_shell->priv;
1726 if (priv->active_menu_item &&
1727 _gtk_menu_item_is_selectable (priv->active_menu_item))
1729 if (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu == NULL)
1730 gtk_menu_shell_activate_item (menu_shell,
1731 priv->active_menu_item,
1734 _gtk_menu_item_popup_submenu (priv->active_menu_item, FALSE);
1739 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1741 /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
1742 gtk_menu_shell_deselect (menu_shell);
1743 gtk_menu_shell_deactivate (menu_shell);
1744 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1748 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1749 GtkDirectionType dir)
1751 GtkMenuShellPrivate *priv = menu_shell->priv;
1753 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1755 if (priv->parent_menu_shell)
1756 menu_shell = GTK_MENU_SHELL (priv->parent_menu_shell);
1762 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1766 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1768 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1770 if (klass->get_popup_delay)
1772 return klass->get_popup_delay (menu_shell);
1777 GtkWidget *widget = GTK_WIDGET (menu_shell);
1779 g_object_get (gtk_widget_get_settings (widget),
1780 "gtk-menu-popup-delay", &popup_delay,
1788 * gtk_menu_shell_cancel:
1789 * @menu_shell: a #GtkMenuShell
1791 * Cancels the selection within the menu shell.
1796 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1798 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1800 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1803 static GtkMnemonicHash *
1804 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1807 GtkMenuShellPrivate *priv = menu_shell->priv;
1809 if (!priv->mnemonic_hash && create)
1810 priv->mnemonic_hash = _gtk_mnemonic_hash_new ();
1812 return priv->mnemonic_hash;
1816 menu_shell_add_mnemonic_foreach (guint keyval,
1820 GtkKeyHash *key_hash = data;
1822 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1826 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1829 GtkMenuShellPrivate *priv = menu_shell->priv;
1830 GtkWidget *widget = GTK_WIDGET (menu_shell);
1832 if (!priv->key_hash && create && gtk_widget_has_screen (widget))
1834 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1835 GdkScreen *screen = gtk_widget_get_screen (widget);
1836 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1841 priv->key_hash = _gtk_key_hash_new (keymap, NULL);
1843 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1844 menu_shell_add_mnemonic_foreach,
1848 return priv->key_hash;
1852 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1854 GtkMenuShellPrivate *priv = menu_shell->priv;
1858 _gtk_key_hash_free (priv->key_hash);
1859 priv->key_hash = NULL;
1864 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1867 GtkMnemonicHash *mnemonic_hash;
1868 GtkKeyHash *key_hash;
1870 gboolean result = FALSE;
1872 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1876 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1880 entries = _gtk_key_hash_lookup (key_hash,
1881 event->hardware_keycode,
1883 gtk_accelerator_get_default_mod_mask (),
1888 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1889 GPOINTER_TO_UINT (entries->data));
1890 g_slist_free (entries);
1897 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1901 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1902 g_return_if_fail (GTK_IS_WIDGET (target));
1904 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1906 gtk_menu_shell_reset_key_hash (menu_shell);
1910 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1914 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1915 g_return_if_fail (GTK_IS_WIDGET (target));
1917 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1919 gtk_menu_shell_reset_key_hash (menu_shell);
1923 _gtk_menu_shell_set_grab_device (GtkMenuShell *menu_shell,
1926 GtkMenuShellPrivate *priv = menu_shell->priv;
1928 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1929 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1932 priv->grab_pointer = NULL;
1933 else if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1934 priv->grab_pointer = gdk_device_get_associated_device (device);
1936 priv->grab_pointer = device;
1940 _gtk_menu_shell_get_grab_device (GtkMenuShell *menu_shell)
1942 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
1944 return menu_shell->priv->grab_pointer;
1948 * gtk_menu_shell_get_take_focus:
1949 * @menu_shell: a #GtkMenuShell
1951 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1953 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1958 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1960 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1962 return menu_shell->priv->take_focus;
1966 * gtk_menu_shell_set_take_focus:
1967 * @menu_shell: a #GtkMenuShell
1968 * @take_focus: %TRUE if the menu shell should take the keyboard
1971 * If @take_focus is %TRUE (the default) the menu shell will take
1972 * the keyboard focus so that it will receive all keyboard events
1973 * which is needed to enable keyboard navigation in menus.
1975 * Setting @take_focus to %FALSE is useful only for special applications
1976 * like virtual keyboard implementations which should not take keyboard
1979 * The @take_focus state of a menu or menu bar is automatically
1980 * propagated to submenus whenever a submenu is popped up, so you
1981 * don't have to worry about recursively setting it for your entire
1982 * menu hierarchy. Only when programmatically picking a submenu and
1983 * popping it up manually, the @take_focus property of the submenu
1984 * needs to be set explicitely.
1986 * Note that setting it to %FALSE has side-effects:
1988 * If the focus is in some other app, it keeps the focus and keynav in
1989 * the menu doesn't work. Consequently, keynav on the menu will only
1990 * work if the focus is on some toplevel owned by the onscreen keyboard.
1992 * To avoid confusing the user, menus with @take_focus set to %FALSE
1993 * should not display mnemonics or accelerators, since it cannot be
1994 * guaranteed that they will work.
1996 * See also gdk_keyboard_grab()
2001 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
2002 gboolean take_focus)
2004 GtkMenuShellPrivate *priv = menu_shell->priv;
2006 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
2008 if (priv->take_focus != take_focus)
2010 priv->take_focus = take_focus;
2011 g_object_notify (G_OBJECT (menu_shell), "take-focus");
2016 * gtk_menu_shell_get_selected_item:
2017 * @menu_shell: a #GtkMenuShell
2019 * Gets the currently selected item.
2021 * Returns: (transfer none): the currently selected item
2026 gtk_menu_shell_get_selected_item (GtkMenuShell *menu_shell)
2028 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
2030 return menu_shell->priv->active_menu_item;
2034 * gtk_menu_shell_get_parent_shell:
2035 * @menu_shell: a #GtkMenuShell
2037 * Gets the parent menu shell.
2039 * The parent menu shell of a submenu is the #GtkMenu or #GtkMenuBar
2040 * from which it was opened up.
2042 * Returns: (transfer none): the parent #GtkMenuShell
2047 gtk_menu_shell_get_parent_shell (GtkMenuShell *menu_shell)
2049 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
2051 return menu_shell->priv->parent_menu_shell;