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"
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 "gtkwindow.h"
53 #include "gtkprivate.h"
55 #include "gtktypebuiltins.h"
57 #include "deprecated/gtktearoffmenuitem.h"
59 #include "a11y/gtkmenushellaccessible.h"
62 #define MENU_SHELL_TIMEOUT 500
64 #define PACK_DIRECTION(m) \
65 (GTK_IS_MENU_BAR (m) \
66 ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
67 : GTK_PACK_DIRECTION_LTR)
88 * A menu item can be "selected", this means that it is displayed
89 * in the prelight state, and if it has a submenu, that submenu
92 * A menu is "active" when it is visible onscreen and the user
93 * is selecting from it. A menubar is not active until the user
94 * clicks on one of its menuitems. When a menu is active,
95 * passing the mouse over a submenu will pop it up.
97 * menu_shell->active_menu_item, is however, not an "active"
98 * menu item (there is no such thing) but rather, the selected
99 * menu item in that MenuShell, if there is one.
101 * There is also is a concept of the current menu and a current
102 * menu item. The current menu item is the selected menu item
103 * that is furthest down in the hierarchy. (Every active menu_shell
104 * does not necessarily contain a selected menu item, but if
105 * it does, then menu_shell->parent_menu_shell must also contain
106 * a selected menu item. The current menu is the menu that
107 * contains the current menu_item. It will always have a GTK
108 * grab and receive all key presses.
113 * ::move_current (GtkMenuDirection *dir)
114 * Moves the current menu item in direction 'dir':
116 * GTK_MENU_DIR_PARENT: To the parent menu shell
117 * GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
119 * GTK_MENU_DIR_NEXT/PREV: To the next or previous item
122 * As a a bit of a hack to get movement between menus and
123 * menubars working, if submenu_placement is different for
124 * the menu and its MenuShell then the following apply:
126 * - For 'parent' the current menu is not just moved to
127 * the parent, but moved to the previous entry in the parent
128 * - For 'child', if there is no child, then current is
129 * moved to the next item in the parent.
131 * Note that the above explanation of ::move_current was written
132 * before menus and menubars had support for RTL flipping and
133 * different packing directions, and therefore only applies for
134 * when text direction and packing direction are both left-to-right.
136 * ::activate_current (GBoolean *force_hide)
137 * Activate the current item. If 'force_hide' is true, hide
138 * the current menu item always. Otherwise, only hide
139 * it if menu_item->klass->hide_on_activate is true.
142 * Cancels the current selection
146 static void gtk_menu_shell_set_property (GObject *object,
150 static void gtk_menu_shell_get_property (GObject *object,
154 static void gtk_menu_shell_realize (GtkWidget *widget);
155 static void gtk_menu_shell_finalize (GObject *object);
156 static void gtk_menu_shell_dispose (GObject *object);
157 static gint gtk_menu_shell_button_press (GtkWidget *widget,
158 GdkEventButton *event);
159 static gint gtk_menu_shell_button_release (GtkWidget *widget,
160 GdkEventButton *event);
161 static gint gtk_menu_shell_key_press (GtkWidget *widget,
163 static gint gtk_menu_shell_enter_notify (GtkWidget *widget,
164 GdkEventCrossing *event);
165 static gint gtk_menu_shell_leave_notify (GtkWidget *widget,
166 GdkEventCrossing *event);
167 static void gtk_menu_shell_screen_changed (GtkWidget *widget,
168 GdkScreen *previous_screen);
169 static gboolean gtk_menu_shell_grab_broken (GtkWidget *widget,
170 GdkEventGrabBroken *event);
171 static void gtk_menu_shell_add (GtkContainer *container,
173 static void gtk_menu_shell_remove (GtkContainer *container,
175 static void gtk_menu_shell_forall (GtkContainer *container,
176 gboolean include_internals,
177 GtkCallback callback,
178 gpointer callback_data);
179 static void gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
182 static void gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell);
183 static gint gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
185 static GtkWidget *gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
187 static GType gtk_menu_shell_child_type (GtkContainer *container);
188 static void gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
189 GtkWidget *menu_item);
190 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell);
192 static void gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
193 GtkMenuDirectionType direction);
194 static void gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
195 gboolean force_hide);
196 static void gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell);
197 static void gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
198 GtkDirectionType dir);
200 static void gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell);
201 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
203 static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
206 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
208 G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
211 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
213 GObjectClass *object_class;
214 GtkWidgetClass *widget_class;
215 GtkContainerClass *container_class;
217 GtkBindingSet *binding_set;
219 object_class = (GObjectClass*) klass;
220 widget_class = (GtkWidgetClass*) klass;
221 container_class = (GtkContainerClass*) klass;
223 object_class->set_property = gtk_menu_shell_set_property;
224 object_class->get_property = gtk_menu_shell_get_property;
225 object_class->finalize = gtk_menu_shell_finalize;
226 object_class->dispose = gtk_menu_shell_dispose;
228 widget_class->realize = gtk_menu_shell_realize;
229 widget_class->button_press_event = gtk_menu_shell_button_press;
230 widget_class->button_release_event = gtk_menu_shell_button_release;
231 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
232 widget_class->key_press_event = gtk_menu_shell_key_press;
233 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
234 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
235 widget_class->screen_changed = gtk_menu_shell_screen_changed;
237 container_class->add = gtk_menu_shell_add;
238 container_class->remove = gtk_menu_shell_remove;
239 container_class->forall = gtk_menu_shell_forall;
240 container_class->child_type = gtk_menu_shell_child_type;
242 klass->submenu_placement = GTK_TOP_BOTTOM;
243 klass->deactivate = gtk_real_menu_shell_deactivate;
244 klass->selection_done = NULL;
245 klass->move_current = gtk_real_menu_shell_move_current;
246 klass->activate_current = gtk_real_menu_shell_activate_current;
247 klass->cancel = gtk_real_menu_shell_cancel;
248 klass->select_item = gtk_menu_shell_real_select_item;
249 klass->insert = gtk_menu_shell_real_insert;
250 klass->move_selected = gtk_menu_shell_real_move_selected;
253 * GtkMenuShell::deactivate:
254 * @menushell: the object which received the signal
256 * This signal is emitted when a menu shell is deactivated.
258 menu_shell_signals[DEACTIVATE] =
259 g_signal_new (I_("deactivate"),
260 G_OBJECT_CLASS_TYPE (object_class),
262 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
264 _gtk_marshal_VOID__VOID,
268 * GtkMenuShell::selection-done:
269 * @menushell: the object which received the signal
271 * This signal is emitted when a selection has been
272 * completed within a menu shell.
274 menu_shell_signals[SELECTION_DONE] =
275 g_signal_new (I_("selection-done"),
276 G_OBJECT_CLASS_TYPE (object_class),
278 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
280 _gtk_marshal_VOID__VOID,
284 * GtkMenuShell::move-current:
285 * @menushell: the object which received the signal
286 * @direction: the direction to move
288 * An keybinding signal which moves the current menu item
289 * in the direction specified by @direction.
291 menu_shell_signals[MOVE_CURRENT] =
292 g_signal_new (I_("move-current"),
293 G_OBJECT_CLASS_TYPE (object_class),
294 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
295 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
297 _gtk_marshal_VOID__ENUM,
299 GTK_TYPE_MENU_DIRECTION_TYPE);
302 * GtkMenuShell::activate-current:
303 * @menushell: the object which received the signal
304 * @force_hide: if %TRUE, hide the menu after activating the menu item
306 * An action signal that activates the current menu item within
309 menu_shell_signals[ACTIVATE_CURRENT] =
310 g_signal_new (I_("activate-current"),
311 G_OBJECT_CLASS_TYPE (object_class),
312 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
313 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
315 _gtk_marshal_VOID__BOOLEAN,
320 * GtkMenuShell::cancel:
321 * @menushell: the object which received the signal
323 * An action signal which cancels the selection within the menu shell.
324 * Causes the #GtkMenuShell::selection-done signal to be emitted.
326 menu_shell_signals[CANCEL] =
327 g_signal_new (I_("cancel"),
328 G_OBJECT_CLASS_TYPE (object_class),
329 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
330 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
332 _gtk_marshal_VOID__VOID,
336 * GtkMenuShell::cycle-focus:
337 * @menushell: the object which received the signal
338 * @direction: the direction to cycle in
340 * A keybinding signal which moves the focus in the
343 menu_shell_signals[CYCLE_FOCUS] =
344 g_signal_new_class_handler (I_("cycle-focus"),
345 G_OBJECT_CLASS_TYPE (object_class),
346 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
347 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
349 _gtk_marshal_VOID__ENUM,
351 GTK_TYPE_DIRECTION_TYPE);
354 * GtkMenuShell::move-selected:
355 * @menu_shell: the object on which the signal is emitted
356 * @distance: +1 to move to the next item, -1 to move to the previous
358 * The ::move-selected signal is emitted to move the selection to
361 * Returns: %TRUE to stop the signal emission, %FALSE to continue
365 menu_shell_signals[MOVE_SELECTED] =
366 g_signal_new (I_("move-selected"),
367 G_OBJECT_CLASS_TYPE (object_class),
369 G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
370 _gtk_boolean_handled_accumulator, NULL,
371 _gtk_marshal_BOOLEAN__INT,
376 * GtkMenuShell::insert:
377 * @menu_shell: the object on which the signal is emitted
378 * @child: the #GtkMenuItem that is being inserted
379 * @position: the position at which the insert occurs
381 * The ::insert signal is emitted when a new #GtkMenuItem is added to
382 * a #GtkMenuShell. A separate signal is used instead of
383 * GtkContainer::add because of the need for an additional position
386 * The inverse of this signal is the GtkContainer::removed signal.
390 menu_shell_signals[INSERT] =
391 g_signal_new (I_("insert"),
392 G_OBJECT_CLASS_TYPE (object_class),
394 G_STRUCT_OFFSET (GtkMenuShellClass, insert),
396 _gtk_marshal_VOID__OBJECT_INT,
397 G_TYPE_NONE, 2, GTK_TYPE_WIDGET, G_TYPE_INT);
400 binding_set = gtk_binding_set_by_class (klass);
401 gtk_binding_entry_add_signal (binding_set,
404 gtk_binding_entry_add_signal (binding_set,
406 "activate-current", 1,
409 gtk_binding_entry_add_signal (binding_set,
410 GDK_KEY_ISO_Enter, 0,
411 "activate-current", 1,
414 gtk_binding_entry_add_signal (binding_set,
416 "activate-current", 1,
419 gtk_binding_entry_add_signal (binding_set,
421 "activate-current", 1,
424 gtk_binding_entry_add_signal (binding_set,
426 "activate-current", 1,
429 gtk_binding_entry_add_signal (binding_set,
432 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
433 gtk_binding_entry_add_signal (binding_set,
434 GDK_KEY_F10, GDK_SHIFT_MASK,
436 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
439 * GtkMenuShell:take-focus:
441 * A boolean that determines whether the menu and its submenus grab the
442 * keyboard focus. See gtk_menu_shell_set_take_focus() and
443 * gtk_menu_shell_get_take_focus().
447 g_object_class_install_property (object_class,
449 g_param_spec_boolean ("take-focus",
451 P_("A boolean that determines whether the menu grabs the keyboard focus"),
453 GTK_PARAM_READWRITE));
455 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_SHELL_ACCESSIBLE);
457 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
461 gtk_menu_shell_child_type (GtkContainer *container)
463 return GTK_TYPE_MENU_ITEM;
467 gtk_menu_shell_init (GtkMenuShell *menu_shell)
469 GtkMenuShellPrivate *priv;
471 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_shell,
473 GtkMenuShellPrivate);
474 menu_shell->priv = priv;
475 priv->take_focus = TRUE;
479 gtk_menu_shell_set_property (GObject *object,
484 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
488 case PROP_TAKE_FOCUS:
489 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gtk_menu_shell_get_property (GObject *object,
503 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
507 case PROP_TAKE_FOCUS:
508 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
511 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 gtk_menu_shell_finalize (GObject *object)
519 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
520 GtkMenuShellPrivate *priv = menu_shell->priv;
522 if (priv->mnemonic_hash)
523 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
525 _gtk_key_hash_free (priv->key_hash);
527 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
532 gtk_menu_shell_dispose (GObject *object)
534 gtk_menu_shell_deactivate (GTK_MENU_SHELL (object));
536 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->dispose (object);
540 * gtk_menu_shell_append:
541 * @menu_shell: a #GtkMenuShell
542 * @child: The #GtkMenuItem to add
544 * Adds a new #GtkMenuItem to the end of the menu shell's
548 gtk_menu_shell_append (GtkMenuShell *menu_shell,
551 gtk_menu_shell_insert (menu_shell, child, -1);
555 * gtk_menu_shell_prepend:
556 * @menu_shell: a #GtkMenuShell
557 * @child: The #GtkMenuItem to add
559 * Adds a new #GtkMenuItem to the beginning of the menu shell's
563 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
566 gtk_menu_shell_insert (menu_shell, child, 0);
570 * gtk_menu_shell_insert:
571 * @menu_shell: a #GtkMenuShell
572 * @child: The #GtkMenuItem to add
573 * @position: The position in the item list where @child
574 * is added. Positions are numbered from 0 to n-1
576 * Adds a new #GtkMenuItem to the menu shell's item list
577 * at the position indicated by @position.
580 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
584 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
585 g_return_if_fail (GTK_IS_MENU_ITEM (child));
587 g_signal_emit (menu_shell, menu_shell_signals[INSERT], 0, child, position);
591 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
595 GtkMenuShellPrivate *priv = menu_shell->priv;
597 priv->children = g_list_insert (priv->children, child, position);
599 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
603 * gtk_menu_shell_deactivate:
604 * @menu_shell: a #GtkMenuShell
606 * Deactivates the menu shell.
608 * Typically this results in the menu shell being erased
612 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
614 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
616 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
620 gtk_menu_shell_realize (GtkWidget *widget)
622 GtkAllocation allocation;
624 GdkWindowAttr attributes;
625 gint attributes_mask;
626 GtkStyleContext *context;
628 gtk_widget_set_realized (widget, TRUE);
630 gtk_widget_get_allocation (widget, &allocation);
632 attributes.x = allocation.x;
633 attributes.y = allocation.y;
634 attributes.width = allocation.width;
635 attributes.height = allocation.height;
636 attributes.window_type = GDK_WINDOW_CHILD;
637 attributes.wclass = GDK_INPUT_OUTPUT;
638 attributes.visual = gtk_widget_get_visual (widget);
639 attributes.event_mask = gtk_widget_get_events (widget);
640 attributes.event_mask |= (GDK_EXPOSURE_MASK |
641 GDK_BUTTON_PRESS_MASK |
642 GDK_BUTTON_RELEASE_MASK |
644 GDK_ENTER_NOTIFY_MASK |
645 GDK_LEAVE_NOTIFY_MASK);
647 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
649 window = gdk_window_new (gtk_widget_get_parent_window (widget),
650 &attributes, attributes_mask);
651 gtk_widget_set_window (widget, window);
652 gdk_window_set_user_data (window, widget);
654 context = gtk_widget_get_style_context (widget);
655 gtk_style_context_set_background (context, window);
659 gtk_menu_shell_activate (GtkMenuShell *menu_shell)
661 GtkMenuShellPrivate *priv = menu_shell->priv;
667 device = gtk_get_current_event_device ();
669 _gtk_menu_shell_set_grab_device (menu_shell, device);
670 gtk_device_grab_add (GTK_WIDGET (menu_shell), device, TRUE);
672 priv->have_grab = TRUE;
678 gtk_menu_shell_button_press (GtkWidget *widget,
679 GdkEventButton *event)
681 GtkMenuShell *menu_shell;
682 GtkMenuShellPrivate *priv;
683 GtkWidget *menu_item;
686 if (event->type != GDK_BUTTON_PRESS)
689 menu_shell = GTK_MENU_SHELL (widget);
690 priv = menu_shell->priv;
692 if (priv->parent_menu_shell)
693 return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
695 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
697 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
699 parent = gtk_widget_get_parent (menu_item);
701 if (menu_item != GTK_MENU_SHELL (parent)->priv->active_menu_item)
703 /* select the menu item *before* activating the shell, so submenus
704 * which might be open are closed the friendly way. If we activate
705 * (and thus grab) this menu shell first, we might get grab_broken
706 * events which will close the entire menu hierarchy. Selecting the
707 * menu item also fixes up the state as if enter_notify() would
708 * have run before (which normally selects the item).
710 if (GTK_MENU_SHELL_GET_CLASS (parent)->submenu_placement != GTK_TOP_BOTTOM)
711 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), menu_item);
715 if (!priv->active || !priv->button)
717 gboolean initially_active = priv->active;
719 priv->button = event->button;
723 if (_gtk_menu_item_is_selectable (menu_item) &&
724 gtk_widget_get_parent (menu_item) == widget &&
725 menu_item != priv->active_menu_item)
727 gtk_menu_shell_activate (menu_shell);
728 priv->button = event->button;
730 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
732 priv->activate_time = event->time;
733 gtk_menu_shell_select_item (menu_shell, menu_item);
739 if (!initially_active)
741 gboolean window_drag = FALSE;
743 gtk_widget_style_get (widget,
744 "window-dragging", &window_drag,
749 gtk_menu_shell_deactivate (menu_shell);
750 gtk_window_begin_move_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
761 widget = gtk_get_event_widget ((GdkEvent*) event);
762 if (widget == GTK_WIDGET (menu_shell))
764 gtk_menu_shell_deactivate (menu_shell);
765 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
770 _gtk_menu_item_is_selectable (menu_item) &&
771 GTK_MENU_ITEM (menu_item)->priv->submenu != NULL &&
772 !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
774 _gtk_menu_item_popup_submenu (menu_item, FALSE);
775 priv->activated_submenu = TRUE;
782 gtk_menu_shell_grab_broken (GtkWidget *widget,
783 GdkEventGrabBroken *event)
785 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
786 GtkMenuShellPrivate *priv = menu_shell->priv;
788 if (priv->have_xgrab && event->grab_window == NULL)
790 /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
791 gtk_menu_shell_deselect (menu_shell);
792 gtk_menu_shell_deactivate (menu_shell);
793 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
800 gtk_menu_shell_button_release (GtkWidget *widget,
801 GdkEventButton *event)
803 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
804 GtkMenuShellPrivate *priv = menu_shell->priv;
808 GtkWidget *menu_item;
809 gboolean deactivate = TRUE;
811 if (priv->button && (event->button != priv->button))
814 if (priv->parent_menu_shell)
815 return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
819 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
821 if ((event->time - priv->activate_time) > MENU_SHELL_TIMEOUT)
823 if (menu_item && (priv->active_menu_item == menu_item) &&
824 _gtk_menu_item_is_selectable (menu_item))
826 GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->priv->submenu;
830 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
833 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
834 priv->activated_submenu)
837 GTimeVal *popup_time;
838 gint64 usec_since_popup = 0;
840 g_object_get (gtk_widget_get_settings (widget),
841 "gtk-menu-popdown-delay", &popdown_delay,
844 popup_time = g_object_get_data (G_OBJECT (submenu),
845 "gtk-menu-exact-popup-time");
849 GTimeVal current_time;
851 g_get_current_time (¤t_time);
853 usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
854 (gint64) current_time.tv_usec -
855 (gint64) popup_time->tv_sec * 1000 * 1000 -
856 (gint64) popup_time->tv_usec);
858 g_object_set_data (G_OBJECT (submenu),
859 "gtk-menu-exact-popup-time", NULL);
862 /* Only close the submenu on click if we opened the
863 * menu explicitely (usec_since_popup == 0) or
864 * enough time has passed since it was opened by
865 * GtkMenuItem's timeout (usec_since_popup > delay).
867 if (!priv->activated_submenu &&
868 (usec_since_popup == 0 ||
869 usec_since_popup > popdown_delay * 1000))
871 _gtk_menu_item_popdown_submenu (menu_item);
875 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
881 else if (menu_item &&
882 !_gtk_menu_item_is_selectable (menu_item) &&
883 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
887 else if (priv->parent_menu_shell)
890 gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
894 /* If we ended up on an item with a submenu, leave the menu up. */
896 (priv->active_menu_item == menu_item) &&
897 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
902 else /* a very fast press-release */
904 /* We only ever want to prevent deactivation on the first
905 * press/release. Setting the time to zero is a bit of a
906 * hack, since we could be being triggered in the first
907 * few fractions of a second after a server time wraparound.
908 * the chances of that happening are ~1/10^6, without
909 * serious harm if we lose.
911 priv->activate_time = 0;
917 gtk_menu_shell_deactivate (menu_shell);
918 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
921 priv->activated_submenu = FALSE;
928 _gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
929 gboolean keyboard_mode)
931 menu_shell->priv->keyboard_mode = keyboard_mode;
935 _gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
937 return menu_shell->priv->keyboard_mode;
941 _gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
943 GtkMenuShell *target;
944 gboolean auto_mnemonics;
946 gboolean mnemonics_visible;
948 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
949 "gtk-auto-mnemonics", &auto_mnemonics,
959 GtkMenuShellPrivate *priv = target->priv;
960 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));
962 /* The idea with keyboard mode is that once you start using
963 * the keyboard to navigate the menus, we show mnemonics
964 * until the menu navigation is over. To that end, we spread
965 * the keyboard mode upwards in the menu hierarchy here.
966 * Also see gtk_menu_popup, where we inherit it downwards.
968 if (menu_shell->priv->keyboard_mode)
969 target->priv->keyboard_mode = TRUE;
971 /* While navigating menus, the first parent menu with an active
972 * item is the one where mnemonics are effective, as can be seen
973 * in gtk_menu_shell_key_press below.
974 * We also show mnemonics in context menus. The grab condition is
975 * necessary to ensure we remove underlines from menu bars when
978 mnemonics_visible = target->priv->keyboard_mode &&
979 (((target->priv->active_menu_item || priv->in_unselectable_item) && !found) ||
980 (target == menu_shell &&
981 !target->priv->parent_menu_shell &&
982 gtk_widget_has_grab (GTK_WIDGET (target))));
984 /* While menus are up, only show underlines inside the menubar,
985 * not in the entire window.
987 if (GTK_IS_MENU_BAR (target))
989 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
990 _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
994 gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
996 if (target->priv->active_menu_item || priv->in_unselectable_item)
999 target = GTK_MENU_SHELL (target->priv->parent_menu_shell);
1004 gtk_menu_shell_key_press (GtkWidget *widget,
1007 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1008 GtkMenuShellPrivate *priv = menu_shell->priv;
1009 gboolean enable_mnemonics;
1011 priv->keyboard_mode = TRUE;
1013 if (!(priv->active_menu_item || priv->in_unselectable_item) &&
1014 priv->parent_menu_shell)
1015 return gtk_widget_event (priv->parent_menu_shell, (GdkEvent *)event);
1017 if (gtk_bindings_activate_event (G_OBJECT (widget), event))
1020 g_object_get (gtk_widget_get_settings (widget),
1021 "gtk-enable-mnemonics", &enable_mnemonics,
1024 if (enable_mnemonics)
1025 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
1031 gtk_menu_shell_enter_notify (GtkWidget *widget,
1032 GdkEventCrossing *event)
1034 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1035 GtkMenuShellPrivate *priv = menu_shell->priv;
1037 if (event->mode == GDK_CROSSING_GTK_GRAB ||
1038 event->mode == GDK_CROSSING_GTK_UNGRAB ||
1039 event->mode == GDK_CROSSING_STATE_CHANGED)
1044 GtkWidget *menu_item;
1047 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1052 if (GTK_IS_MENU_ITEM (menu_item) &&
1053 !_gtk_menu_item_is_selectable (menu_item))
1055 priv->in_unselectable_item = TRUE;
1059 parent = gtk_widget_get_parent (menu_item);
1060 if (parent == widget &&
1061 GTK_IS_MENU_ITEM (menu_item))
1063 if (priv->ignore_enter)
1066 if (event->detail != GDK_NOTIFY_INFERIOR)
1068 if ((gtk_widget_get_state_flags (menu_item) & GTK_STATE_FLAG_PRELIGHT) == 0)
1069 gtk_menu_shell_select_item (menu_shell, menu_item);
1071 /* If any mouse button is down, and there is a submenu
1072 * that is not yet visible, activate it. It's sufficient
1073 * to check for any button's mask (not only the one
1074 * matching menu_shell->button), because there is no
1075 * situation a mouse button could be pressed while
1076 * entering a menu item where we wouldn't want to show
1079 if ((event->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK)) &&
1080 GTK_MENU_ITEM (menu_item)->priv->submenu != NULL)
1082 GTK_MENU_SHELL (parent)->priv->activated_submenu = TRUE;
1084 if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
1086 gboolean touchscreen_mode;
1088 g_object_get (gtk_widget_get_settings (widget),
1089 "gtk-touchscreen-mode", &touchscreen_mode,
1092 if (touchscreen_mode)
1093 _gtk_menu_item_popup_submenu (menu_item, TRUE);
1098 else if (priv->parent_menu_shell)
1100 gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
1108 gtk_menu_shell_leave_notify (GtkWidget *widget,
1109 GdkEventCrossing *event)
1111 if (event->mode == GDK_CROSSING_GTK_GRAB ||
1112 event->mode == GDK_CROSSING_GTK_GRAB ||
1113 event->mode == GDK_CROSSING_STATE_CHANGED)
1116 if (gtk_widget_get_visible (widget))
1118 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1119 GtkMenuShellPrivate *priv = menu_shell->priv;
1120 GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
1121 GtkMenuItem *menu_item;
1123 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1126 menu_item = GTK_MENU_ITEM (event_widget);
1128 if (!_gtk_menu_item_is_selectable (event_widget))
1130 priv->in_unselectable_item = TRUE;
1134 if ((priv->active_menu_item == event_widget) &&
1135 (menu_item->priv->submenu == NULL))
1137 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1138 (gtk_widget_get_state_flags (GTK_WIDGET (menu_item)) & GTK_STATE_FLAG_PRELIGHT) != 0)
1140 gtk_menu_shell_deselect (menu_shell);
1143 else if (priv->parent_menu_shell)
1145 gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
1153 gtk_menu_shell_screen_changed (GtkWidget *widget,
1154 GdkScreen *previous_screen)
1156 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
1160 gtk_menu_shell_add (GtkContainer *container,
1163 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
1167 gtk_menu_shell_remove (GtkContainer *container,
1170 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1171 GtkMenuShellPrivate *priv = menu_shell->priv;
1174 was_visible = gtk_widget_get_visible (widget);
1175 priv->children = g_list_remove (priv->children, widget);
1177 if (widget == priv->active_menu_item)
1179 g_signal_emit_by_name (priv->active_menu_item, "deselect");
1180 priv->active_menu_item = NULL;
1183 gtk_widget_unparent (widget);
1185 /* Queue resize regardless of gtk_widget_get_visible (container),
1186 * since that's what is needed by toplevels.
1189 gtk_widget_queue_resize (GTK_WIDGET (container));
1193 gtk_menu_shell_forall (GtkContainer *container,
1194 gboolean include_internals,
1195 GtkCallback callback,
1196 gpointer callback_data)
1198 GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1202 children = menu_shell->priv->children;
1205 child = children->data;
1206 children = children->next;
1208 (* callback) (child, callback_data);
1214 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
1216 GtkMenuShellPrivate *priv = menu_shell->priv;
1221 priv->active = FALSE;
1222 priv->activate_time = 0;
1224 if (priv->active_menu_item)
1226 gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
1227 priv->active_menu_item = NULL;
1230 if (priv->have_grab)
1232 priv->have_grab = FALSE;
1233 gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1235 if (priv->have_xgrab)
1237 GdkDevice *keyboard;
1239 gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1240 keyboard = gdk_device_get_associated_device (priv->grab_pointer);
1243 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1245 priv->have_xgrab = FALSE;
1248 priv->keyboard_mode = FALSE;
1249 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1251 _gtk_menu_shell_update_mnemonics (menu_shell);
1256 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1261 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1262 g_return_val_if_fail (child != NULL, FALSE);
1264 parent = gtk_widget_get_parent (child);
1265 while (GTK_IS_MENU_SHELL (parent))
1267 if (parent == (GtkWidget*) menu_shell)
1269 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1276 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1279 GtkWidget *menu_item;
1281 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1283 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1284 menu_item = gtk_widget_get_parent (menu_item);
1286 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1292 /* Handlers for action signals */
1295 * gtk_menu_shell_select_item:
1296 * @menu_shell: a #GtkMenuShell
1297 * @menu_item: The #GtkMenuItem to select
1299 * Selects the menu item from the menu shell.
1302 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1303 GtkWidget *menu_item)
1305 GtkMenuShellPrivate *priv = menu_shell->priv;
1306 GtkMenuShellClass *class;
1308 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1309 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1311 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1313 if (class->select_item &&
1315 priv->active_menu_item == menu_item))
1316 class->select_item (menu_shell, menu_item);
1319 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1320 GtkSubmenuPlacement placement);
1323 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1324 GtkWidget *menu_item)
1326 GtkMenuShellPrivate *priv = menu_shell->priv;
1327 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1329 if (priv->active_menu_item)
1331 gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
1332 priv->active_menu_item = NULL;
1335 if (!_gtk_menu_item_is_selectable (menu_item))
1337 priv->in_unselectable_item = TRUE;
1338 _gtk_menu_shell_update_mnemonics (menu_shell);
1342 gtk_menu_shell_activate (menu_shell);
1344 priv->active_menu_item = menu_item;
1345 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1346 _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
1349 _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
1350 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1351 gtk_menu_item_select (GTK_MENU_ITEM (priv->active_menu_item));
1353 _gtk_menu_shell_update_mnemonics (menu_shell);
1355 /* This allows the bizarre radio buttons-with-submenus-display-history
1358 if (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1359 gtk_widget_activate (priv->active_menu_item);
1363 * gtk_menu_shell_deselect:
1364 * @menu_shell: a #GtkMenuShell
1366 * Deselects the currently selected item from the menu shell,
1370 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1372 GtkMenuShellPrivate *priv = menu_shell->priv;
1374 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1376 if (priv->active_menu_item)
1378 gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
1379 priv->active_menu_item = NULL;
1380 _gtk_menu_shell_update_mnemonics (menu_shell);
1385 * gtk_menu_shell_activate_item:
1386 * @menu_shell: a #GtkMenuShell
1387 * @menu_item: the #GtkMenuItem to activate
1388 * @force_deactivate: if %TRUE, force the deactivation of the
1389 * menu shell after the menu item is activated
1391 * Activates the menu item within the menu shell.
1394 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
1395 GtkWidget *menu_item,
1396 gboolean force_deactivate)
1398 GSList *slist, *shells = NULL;
1399 gboolean deactivate = force_deactivate;
1401 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1402 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1405 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1407 g_object_ref (menu_shell);
1408 g_object_ref (menu_item);
1412 GtkMenuShell *parent_menu_shell = menu_shell;
1416 g_object_ref (parent_menu_shell);
1417 shells = g_slist_prepend (shells, parent_menu_shell);
1418 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->priv->parent_menu_shell;
1420 while (parent_menu_shell);
1421 shells = g_slist_reverse (shells);
1423 gtk_menu_shell_deactivate (menu_shell);
1425 /* Flush the x-queue, so any grabs are removed and
1426 * the menu is actually taken down
1428 gdk_display_sync (gtk_widget_get_display (menu_item));
1431 gtk_widget_activate (menu_item);
1433 for (slist = shells; slist; slist = slist->next)
1435 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1436 g_object_unref (slist->data);
1438 g_slist_free (shells);
1440 g_object_unref (menu_shell);
1441 g_object_unref (menu_item);
1444 /* Distance should be +/- 1 */
1446 gtk_menu_shell_real_move_selected (GtkMenuShell *menu_shell,
1449 GtkMenuShellPrivate *priv = menu_shell->priv;
1451 if (priv->active_menu_item)
1453 GList *node = g_list_find (priv->children, priv->active_menu_item);
1454 GList *start_node = node;
1455 gboolean wrap_around;
1457 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1458 "gtk-keynav-wrap-around", &wrap_around,
1464 while (node != start_node &&
1465 (!node || !_gtk_menu_item_is_selectable (node->data)))
1469 else if (wrap_around)
1470 node = priv->children;
1473 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1481 while (node != start_node &&
1482 (!node || !_gtk_menu_item_is_selectable (node->data)))
1486 else if (wrap_around)
1487 node = g_list_last (priv->children);
1490 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1497 gtk_menu_shell_select_item (menu_shell, node->data);
1503 /* Distance should be +/- 1 */
1505 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1508 gboolean handled = FALSE;
1510 g_signal_emit (menu_shell, menu_shell_signals[MOVE_SELECTED], 0,
1511 distance, &handled);
1515 * gtk_menu_shell_select_first:
1516 * @menu_shell: a #GtkMenuShell
1517 * @search_sensitive: if %TRUE, search for the first selectable
1518 * menu item, otherwise select nothing if
1519 * the first item isn't sensitive. This
1520 * should be %FALSE if the menu is being
1521 * popped up initially.
1523 * Select the first visible or selectable child of the menu shell;
1524 * don't select tearoff items unless the only item is a tearoff
1530 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1531 gboolean search_sensitive)
1533 GtkMenuShellPrivate *priv = menu_shell->priv;
1534 GtkWidget *to_select = NULL;
1537 tmp_list = priv->children;
1540 GtkWidget *child = tmp_list->data;
1542 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1543 _gtk_menu_item_is_selectable (child))
1546 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1550 tmp_list = tmp_list->next;
1554 gtk_menu_shell_select_item (menu_shell, to_select);
1558 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1559 gboolean search_sensitive)
1561 GtkMenuShellPrivate *priv = menu_shell->priv;
1562 GtkWidget *to_select = NULL;
1565 tmp_list = g_list_last (priv->children);
1568 GtkWidget *child = tmp_list->data;
1570 if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1571 _gtk_menu_item_is_selectable (child))
1574 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1578 tmp_list = tmp_list->prev;
1582 gtk_menu_shell_select_item (menu_shell, to_select);
1586 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1588 GtkMenuShellPrivate *priv = menu_shell->priv;
1589 GtkMenuItem *menu_item;
1591 if (priv->active_menu_item == NULL)
1594 menu_item = GTK_MENU_ITEM (priv->active_menu_item);
1596 if (menu_item->priv->submenu)
1598 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1599 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->priv->submenu), TRUE);
1600 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active_menu_item)
1608 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1609 GtkMenuDirectionType direction)
1611 GtkMenuShellPrivate *priv = menu_shell->priv;
1612 GtkMenuShell *parent_menu_shell = NULL;
1613 gboolean had_selection;
1614 gboolean touchscreen_mode;
1616 priv->in_unselectable_item = FALSE;
1618 had_selection = priv->active_menu_item != NULL;
1620 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1621 "gtk-touchscreen-mode", &touchscreen_mode,
1624 if (priv->parent_menu_shell)
1625 parent_menu_shell = GTK_MENU_SHELL (priv->parent_menu_shell);
1629 case GTK_MENU_DIR_PARENT:
1630 if (touchscreen_mode &&
1631 priv->active_menu_item &&
1632 GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu &&
1633 gtk_widget_get_visible (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu))
1635 /* if we are on a menu item that has an open submenu but the
1636 * focus is not in that submenu (e.g. because it's empty or
1637 * has only insensitive items), close that submenu instead of
1638 * running into the code below which would close *this* menu.
1640 _gtk_menu_item_popdown_submenu (priv->active_menu_item);
1641 _gtk_menu_shell_update_mnemonics (menu_shell);
1643 else if (parent_menu_shell)
1645 if (touchscreen_mode)
1647 /* close menu when returning from submenu. */
1648 _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->priv->parent_menu_item);
1649 _gtk_menu_shell_update_mnemonics (parent_menu_shell);
1653 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1654 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1655 gtk_menu_shell_deselect (menu_shell);
1658 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1659 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1661 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1662 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1665 /* If there is no parent and the submenu is in the opposite direction
1666 * to the menu, then make the PARENT direction wrap around to
1667 * the bottom of the submenu.
1669 else if (priv->active_menu_item &&
1670 _gtk_menu_item_is_selectable (priv->active_menu_item) &&
1671 GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1673 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu);
1675 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1676 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1677 _gtk_menu_shell_select_last (submenu, TRUE);
1681 case GTK_MENU_DIR_CHILD:
1682 if (priv->active_menu_item &&
1683 _gtk_menu_item_is_selectable (priv->active_menu_item) &&
1684 GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1686 if (gtk_menu_shell_select_submenu_first (menu_shell))
1690 /* Try to find a menu running the opposite direction */
1691 while (parent_menu_shell &&
1692 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1693 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1695 parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->priv->parent_menu_shell);
1698 if (parent_menu_shell)
1700 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1701 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1703 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1705 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1709 case GTK_MENU_DIR_PREV:
1710 gtk_menu_shell_move_selected (menu_shell, -1);
1711 if (!had_selection && !priv->active_menu_item && priv->children)
1712 _gtk_menu_shell_select_last (menu_shell, TRUE);
1715 case GTK_MENU_DIR_NEXT:
1716 gtk_menu_shell_move_selected (menu_shell, 1);
1717 if (!had_selection && !priv->active_menu_item && priv->children)
1718 gtk_menu_shell_select_first (menu_shell, TRUE);
1724 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1725 gboolean force_hide)
1727 GtkMenuShellPrivate *priv = menu_shell->priv;
1729 if (priv->active_menu_item &&
1730 _gtk_menu_item_is_selectable (priv->active_menu_item))
1732 if (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu == NULL)
1733 gtk_menu_shell_activate_item (menu_shell,
1734 priv->active_menu_item,
1737 _gtk_menu_item_popup_submenu (priv->active_menu_item, FALSE);
1742 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1744 /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
1745 gtk_menu_shell_deselect (menu_shell);
1746 gtk_menu_shell_deactivate (menu_shell);
1747 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1751 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1752 GtkDirectionType dir)
1754 GtkMenuShellPrivate *priv = menu_shell->priv;
1756 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1758 if (priv->parent_menu_shell)
1759 menu_shell = GTK_MENU_SHELL (priv->parent_menu_shell);
1765 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1769 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1771 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1773 if (klass->get_popup_delay)
1775 return klass->get_popup_delay (menu_shell);
1780 GtkWidget *widget = GTK_WIDGET (menu_shell);
1782 g_object_get (gtk_widget_get_settings (widget),
1783 "gtk-menu-popup-delay", &popup_delay,
1791 * gtk_menu_shell_cancel:
1792 * @menu_shell: a #GtkMenuShell
1794 * Cancels the selection within the menu shell.
1799 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1801 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1803 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1806 static GtkMnemonicHash *
1807 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1810 GtkMenuShellPrivate *priv = menu_shell->priv;
1812 if (!priv->mnemonic_hash && create)
1813 priv->mnemonic_hash = _gtk_mnemonic_hash_new ();
1815 return priv->mnemonic_hash;
1819 menu_shell_add_mnemonic_foreach (guint keyval,
1823 GtkKeyHash *key_hash = data;
1825 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1829 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1832 GtkMenuShellPrivate *priv = menu_shell->priv;
1833 GtkWidget *widget = GTK_WIDGET (menu_shell);
1835 if (!priv->key_hash && create && gtk_widget_has_screen (widget))
1837 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1838 GdkScreen *screen = gtk_widget_get_screen (widget);
1839 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1844 priv->key_hash = _gtk_key_hash_new (keymap, NULL);
1846 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1847 menu_shell_add_mnemonic_foreach,
1851 return priv->key_hash;
1855 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1857 GtkMenuShellPrivate *priv = menu_shell->priv;
1861 _gtk_key_hash_free (priv->key_hash);
1862 priv->key_hash = NULL;
1867 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1870 GtkMnemonicHash *mnemonic_hash;
1871 GtkKeyHash *key_hash;
1873 gboolean result = FALSE;
1875 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1879 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1883 entries = _gtk_key_hash_lookup (key_hash,
1884 event->hardware_keycode,
1886 gtk_accelerator_get_default_mod_mask (),
1891 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1892 GPOINTER_TO_UINT (entries->data));
1893 g_slist_free (entries);
1900 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1904 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1905 g_return_if_fail (GTK_IS_WIDGET (target));
1907 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1909 gtk_menu_shell_reset_key_hash (menu_shell);
1913 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1917 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1918 g_return_if_fail (GTK_IS_WIDGET (target));
1920 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1922 gtk_menu_shell_reset_key_hash (menu_shell);
1926 _gtk_menu_shell_set_grab_device (GtkMenuShell *menu_shell,
1929 GtkMenuShellPrivate *priv = menu_shell->priv;
1931 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1932 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1935 priv->grab_pointer = NULL;
1936 else if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1937 priv->grab_pointer = gdk_device_get_associated_device (device);
1939 priv->grab_pointer = device;
1943 _gtk_menu_shell_get_grab_device (GtkMenuShell *menu_shell)
1945 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
1947 return menu_shell->priv->grab_pointer;
1951 * gtk_menu_shell_get_take_focus:
1952 * @menu_shell: a #GtkMenuShell
1954 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1956 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1961 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1963 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1965 return menu_shell->priv->take_focus;
1969 * gtk_menu_shell_set_take_focus:
1970 * @menu_shell: a #GtkMenuShell
1971 * @take_focus: %TRUE if the menu shell should take the keyboard
1974 * If @take_focus is %TRUE (the default) the menu shell will take
1975 * the keyboard focus so that it will receive all keyboard events
1976 * which is needed to enable keyboard navigation in menus.
1978 * Setting @take_focus to %FALSE is useful only for special applications
1979 * like virtual keyboard implementations which should not take keyboard
1982 * The @take_focus state of a menu or menu bar is automatically
1983 * propagated to submenus whenever a submenu is popped up, so you
1984 * don't have to worry about recursively setting it for your entire
1985 * menu hierarchy. Only when programmatically picking a submenu and
1986 * popping it up manually, the @take_focus property of the submenu
1987 * needs to be set explicitely.
1989 * Note that setting it to %FALSE has side-effects:
1991 * If the focus is in some other app, it keeps the focus and keynav in
1992 * the menu doesn't work. Consequently, keynav on the menu will only
1993 * work if the focus is on some toplevel owned by the onscreen keyboard.
1995 * To avoid confusing the user, menus with @take_focus set to %FALSE
1996 * should not display mnemonics or accelerators, since it cannot be
1997 * guaranteed that they will work.
1999 * See also gdk_keyboard_grab()
2004 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
2005 gboolean take_focus)
2007 GtkMenuShellPrivate *priv = menu_shell->priv;
2009 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
2011 if (priv->take_focus != take_focus)
2013 priv->take_focus = take_focus;
2014 g_object_notify (G_OBJECT (menu_shell), "take-focus");
2019 * gtk_menu_shell_get_selected_item:
2020 * @menu_shell: a #GtkMenuShell
2022 * Gets the currently selected item.
2024 * Returns: (transfer none): the currently selected item
2029 gtk_menu_shell_get_selected_item (GtkMenuShell *menu_shell)
2031 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
2033 return menu_shell->priv->active_menu_item;
2037 * gtk_menu_shell_get_parent_shell:
2038 * @menu_shell: a #GtkMenuShell
2040 * Gets the parent menu shell.
2042 * The parent menu shell of a submenu is the #GtkMenu or #GtkMenuBar
2043 * from which it was opened up.
2045 * Returns: (transfer none): the parent #GtkMenuShell
2050 gtk_menu_shell_get_parent_shell (GtkMenuShell *menu_shell)
2052 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL);
2054 return menu_shell->priv->parent_menu_shell;