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/.
27 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkkeyhash.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenubar.h"
36 #include "gtkmenuitem.h"
37 #include "gtkmenushell.h"
38 #include "gtkmnemonichash.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
41 #include "gtkprivate.h"
45 #define MENU_SHELL_TIMEOUT 500
47 #define PACK_DIRECTION(m) \
48 (GTK_IS_MENU_BAR (m) \
49 ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
50 : GTK_PACK_DIRECTION_LTR)
67 typedef void (*GtkMenuShellSignal1) (GtkObject *object,
68 GtkMenuDirectionType arg1,
70 typedef void (*GtkMenuShellSignal2) (GtkObject *object,
76 * A menu item can be "selected", this means that it is displayed
77 * in the prelight state, and if it has a submenu, that submenu
80 * A menu is "active" when it is visible onscreen and the user
81 * is selecting from it. A menubar is not active until the user
82 * clicks on one of its menuitems. When a menu is active,
83 * passing the mouse over a submenu will pop it up.
85 * menu_shell->active_menu_item, is however, not an "active"
86 * menu item (there is no such thing) but rather, the selected
87 * menu item in that MenuShell, if there is one.
89 * There is also is a concept of the current menu and a current
90 * menu item. The current menu item is the selected menu item
91 * that is furthest down in the heirarchy. (Every active menu_shell
92 * does not necessarily contain a selected menu item, but if
93 * it does, then menu_shell->parent_menu_shell must also contain
94 * a selected menu item. The current menu is the menu that
95 * contains the current menu_item. It will always have a GTK
96 * grab and receive all key presses.
101 * ::move_current (GtkMenuDirection *dir)
102 * Moves the current menu item in direction 'dir':
104 * GTK_MENU_DIR_PARENT: To the parent menu shell
105 * GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
107 * GTK_MENU_DIR_NEXT/PREV: To the next or previous item
110 * As a a bit of a hack to get movement between menus and
111 * menubars working, if submenu_placement is different for
112 * the menu and its MenuShell then the following apply:
114 * - For 'parent' the current menu is not just moved to
115 * the parent, but moved to the previous entry in the parent
116 * - For 'child', if there is no child, then current is
117 * moved to the next item in the parent.
119 * Note that the above explanation of ::move_current was written
120 * before menus and menubars had support for RTL flipping and
121 * different packing directions, and therefore only applies for
122 * when text direction and packing direction are both left-to-right.
124 * ::activate_current (GBoolean *force_hide)
125 * Activate the current item. If 'force_hide' is true, hide
126 * the current menu item always. Otherwise, only hide
127 * it if menu_item->klass->hide_on_activate is true.
130 * Cancels the current selection
133 #define GTK_MENU_SHELL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MENU_SHELL, GtkMenuShellPrivate))
135 typedef struct _GtkMenuShellPrivate GtkMenuShellPrivate;
137 struct _GtkMenuShellPrivate
139 GtkMnemonicHash *mnemonic_hash;
140 GtkKeyHash *key_hash;
145 static void gtk_menu_shell_set_property (GObject *object,
149 static void gtk_menu_shell_get_property (GObject *object,
153 static void gtk_menu_shell_realize (GtkWidget *widget);
154 static void gtk_menu_shell_finalize (GObject *object);
155 static gint gtk_menu_shell_button_press (GtkWidget *widget,
156 GdkEventButton *event);
157 static gint gtk_menu_shell_button_release (GtkWidget *widget,
158 GdkEventButton *event);
159 static gint gtk_menu_shell_key_press (GtkWidget *widget,
161 static gint gtk_menu_shell_enter_notify (GtkWidget *widget,
162 GdkEventCrossing *event);
163 static gint gtk_menu_shell_leave_notify (GtkWidget *widget,
164 GdkEventCrossing *event);
165 static void gtk_menu_shell_screen_changed (GtkWidget *widget,
166 GdkScreen *previous_screen);
167 static gboolean gtk_menu_shell_grab_broken (GtkWidget *widget,
168 GdkEventGrabBroken *event);
169 static void gtk_menu_shell_add (GtkContainer *container,
171 static void gtk_menu_shell_remove (GtkContainer *container,
173 static void gtk_menu_shell_forall (GtkContainer *container,
174 gboolean include_internals,
175 GtkCallback callback,
176 gpointer callback_data);
177 static void gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
180 static void gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell);
181 static gint gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
183 static GtkWidget *gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
185 static GType gtk_menu_shell_child_type (GtkContainer *container);
186 static void gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
187 GtkWidget *menu_item);
188 static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell);
190 static void gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
191 GtkMenuDirectionType direction);
192 static void gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
193 gboolean force_hide);
194 static void gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell);
195 static void gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
196 GtkDirectionType dir);
198 static void gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell);
199 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
202 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
204 G_DEFINE_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
207 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
209 GObjectClass *object_class;
210 GtkWidgetClass *widget_class;
211 GtkContainerClass *container_class;
213 GtkBindingSet *binding_set;
215 object_class = (GObjectClass*) klass;
216 widget_class = (GtkWidgetClass*) klass;
217 container_class = (GtkContainerClass*) klass;
219 object_class->set_property = gtk_menu_shell_set_property;
220 object_class->get_property = gtk_menu_shell_get_property;
221 object_class->finalize = gtk_menu_shell_finalize;
223 widget_class->realize = gtk_menu_shell_realize;
224 widget_class->button_press_event = gtk_menu_shell_button_press;
225 widget_class->button_release_event = gtk_menu_shell_button_release;
226 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
227 widget_class->key_press_event = gtk_menu_shell_key_press;
228 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
229 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
230 widget_class->screen_changed = gtk_menu_shell_screen_changed;
232 container_class->add = gtk_menu_shell_add;
233 container_class->remove = gtk_menu_shell_remove;
234 container_class->forall = gtk_menu_shell_forall;
235 container_class->child_type = gtk_menu_shell_child_type;
237 klass->submenu_placement = GTK_TOP_BOTTOM;
238 klass->deactivate = gtk_real_menu_shell_deactivate;
239 klass->selection_done = NULL;
240 klass->move_current = gtk_real_menu_shell_move_current;
241 klass->activate_current = gtk_real_menu_shell_activate_current;
242 klass->cancel = gtk_real_menu_shell_cancel;
243 klass->select_item = gtk_menu_shell_real_select_item;
244 klass->insert = gtk_menu_shell_real_insert;
246 menu_shell_signals[DEACTIVATE] =
247 g_signal_new (I_("deactivate"),
248 G_OBJECT_CLASS_TYPE (object_class),
250 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
252 _gtk_marshal_VOID__VOID,
254 menu_shell_signals[SELECTION_DONE] =
255 g_signal_new (I_("selection-done"),
256 G_OBJECT_CLASS_TYPE (object_class),
258 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
260 _gtk_marshal_VOID__VOID,
262 menu_shell_signals[MOVE_CURRENT] =
263 g_signal_new (I_("move_current"),
264 G_OBJECT_CLASS_TYPE (object_class),
265 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
266 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
268 _gtk_marshal_VOID__ENUM,
270 GTK_TYPE_MENU_DIRECTION_TYPE);
271 menu_shell_signals[ACTIVATE_CURRENT] =
272 g_signal_new (I_("activate_current"),
273 G_OBJECT_CLASS_TYPE (object_class),
274 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
275 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
277 _gtk_marshal_VOID__BOOLEAN,
280 menu_shell_signals[CANCEL] =
281 g_signal_new (I_("cancel"),
282 G_OBJECT_CLASS_TYPE (object_class),
283 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
284 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
286 _gtk_marshal_VOID__VOID,
288 menu_shell_signals[CYCLE_FOCUS] =
289 _gtk_binding_signal_new (I_("cycle_focus"),
290 G_OBJECT_CLASS_TYPE (object_class),
291 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
292 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
294 _gtk_marshal_VOID__ENUM,
296 GTK_TYPE_DIRECTION_TYPE);
299 binding_set = gtk_binding_set_by_class (klass);
300 gtk_binding_entry_add_signal (binding_set,
303 gtk_binding_entry_add_signal (binding_set,
305 "activate_current", 1,
308 gtk_binding_entry_add_signal (binding_set,
310 "activate_current", 1,
313 gtk_binding_entry_add_signal (binding_set,
315 "activate_current", 1,
318 gtk_binding_entry_add_signal (binding_set,
320 "activate_current", 1,
323 gtk_binding_entry_add_signal (binding_set,
326 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
327 gtk_binding_entry_add_signal (binding_set,
328 GDK_F10, GDK_SHIFT_MASK,
330 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
333 * GtkMenuShell:take-focus:
335 * A boolean that determines whether the menu and its submenus grab the
336 * keyboard focus. See gtk_menu_shell_set_take_focus() and
337 * gtk_menu_shell_get_take_focus().
341 g_object_class_install_property (object_class,
343 g_param_spec_boolean ("take-focus",
345 P_("A boolean that determines whether the menu grabs the keyboard focus"),
347 GTK_PARAM_READWRITE));
349 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
353 gtk_menu_shell_child_type (GtkContainer *container)
355 return GTK_TYPE_MENU_ITEM;
359 gtk_menu_shell_init (GtkMenuShell *menu_shell)
361 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
363 menu_shell->children = NULL;
364 menu_shell->active_menu_item = NULL;
365 menu_shell->parent_menu_shell = NULL;
366 menu_shell->active = FALSE;
367 menu_shell->have_grab = FALSE;
368 menu_shell->have_xgrab = FALSE;
369 menu_shell->button = 0;
370 menu_shell->activate_time = 0;
372 priv->mnemonic_hash = NULL;
373 priv->key_hash = NULL;
374 priv->take_focus = TRUE;
378 gtk_menu_shell_set_property (GObject *object,
383 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
387 case PROP_TAKE_FOCUS:
388 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397 gtk_menu_shell_get_property (GObject *object,
402 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
406 case PROP_TAKE_FOCUS:
407 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
410 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 gtk_menu_shell_finalize (GObject *object)
418 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
419 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
421 if (priv->mnemonic_hash)
422 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
424 _gtk_key_hash_free (priv->key_hash);
426 G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
431 gtk_menu_shell_append (GtkMenuShell *menu_shell,
434 gtk_menu_shell_insert (menu_shell, child, -1);
438 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
441 gtk_menu_shell_insert (menu_shell, child, 0);
445 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
449 GtkMenuShellClass *class;
451 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
452 g_return_if_fail (GTK_IS_MENU_ITEM (child));
454 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
457 class->insert (menu_shell, child, position);
461 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
465 menu_shell->children = g_list_insert (menu_shell->children, child, position);
467 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
471 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
473 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
475 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
479 gtk_menu_shell_realize (GtkWidget *widget)
481 GdkWindowAttr attributes;
482 gint attributes_mask;
484 g_return_if_fail (GTK_IS_MENU_SHELL (widget));
486 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
488 attributes.x = widget->allocation.x;
489 attributes.y = widget->allocation.y;
490 attributes.width = widget->allocation.width;
491 attributes.height = widget->allocation.height;
492 attributes.window_type = GDK_WINDOW_CHILD;
493 attributes.wclass = GDK_INPUT_OUTPUT;
494 attributes.visual = gtk_widget_get_visual (widget);
495 attributes.colormap = gtk_widget_get_colormap (widget);
496 attributes.event_mask = gtk_widget_get_events (widget);
497 attributes.event_mask |= (GDK_EXPOSURE_MASK |
498 GDK_BUTTON_PRESS_MASK |
499 GDK_BUTTON_RELEASE_MASK |
501 GDK_ENTER_NOTIFY_MASK |
502 GDK_LEAVE_NOTIFY_MASK);
504 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
505 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
506 gdk_window_set_user_data (widget->window, widget);
508 widget->style = gtk_style_attach (widget->style, widget->window);
509 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
513 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
515 if (!menu_shell->active)
517 gtk_grab_add (GTK_WIDGET (menu_shell));
518 menu_shell->have_grab = TRUE;
519 menu_shell->active = TRUE;
524 gtk_menu_shell_button_press (GtkWidget *widget,
525 GdkEventButton *event)
527 GtkMenuShell *menu_shell;
528 GtkWidget *menu_item;
530 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
531 g_return_val_if_fail (event != NULL, FALSE);
533 if (event->type != GDK_BUTTON_PRESS)
536 menu_shell = GTK_MENU_SHELL (widget);
538 if (menu_shell->parent_menu_shell)
540 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
542 else if (!menu_shell->active || !menu_shell->button)
544 _gtk_menu_shell_activate (menu_shell);
546 menu_shell->button = event->button;
548 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
550 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
552 if ((menu_item->parent == widget) &&
553 (menu_item != menu_shell->active_menu_item))
555 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
557 menu_shell->activate_time = event->time;
560 gtk_menu_shell_select_item (menu_shell, menu_item);
566 widget = gtk_get_event_widget ((GdkEvent*) event);
567 if (widget == GTK_WIDGET (menu_shell))
569 gtk_menu_shell_deactivate (menu_shell);
570 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
578 gtk_menu_shell_grab_broken (GtkWidget *widget,
579 GdkEventGrabBroken *event)
581 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
583 if (menu_shell->have_xgrab && event->grab_window == NULL)
585 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
588 gtk_menu_shell_deselect (menu_shell);
590 gtk_menu_shell_deactivate (menu_shell);
591 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
598 gtk_menu_shell_button_release (GtkWidget *widget,
599 GdkEventButton *event)
601 GtkMenuShell *menu_shell;
602 GtkWidget *menu_item;
605 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
606 g_return_val_if_fail (event != NULL, FALSE);
608 menu_shell = GTK_MENU_SHELL (widget);
609 if (menu_shell->active)
611 if (menu_shell->button && (event->button != menu_shell->button))
613 menu_shell->button = 0;
614 if (menu_shell->parent_menu_shell)
615 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
618 menu_shell->button = 0;
619 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
623 if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
625 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
626 _gtk_menu_item_is_selectable (menu_item))
628 if (GTK_MENU_ITEM (menu_item)->submenu == NULL)
630 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
633 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
635 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
639 else if (menu_item &&
640 !_gtk_menu_item_is_selectable (menu_item) &&
641 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
645 else if (menu_shell->parent_menu_shell)
647 menu_shell->active = TRUE;
648 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
652 /* If we ended up on an item with a submenu, leave the menu up.
654 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
655 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
660 else /* a very fast press-release */
662 /* We only ever want to prevent deactivation on the first
663 * press/release. Setting the time to zero is a bit of a
664 * hack, since we could be being triggered in the first
665 * few fractions of a second after a server time wraparound.
666 * the chances of that happening are ~1/10^6, without
667 * serious harm if we lose.
669 menu_shell->activate_time = 0;
675 gtk_menu_shell_deactivate (menu_shell);
676 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
684 gtk_menu_shell_key_press (GtkWidget *widget,
687 GtkMenuShell *menu_shell;
688 gboolean enable_mnemonics;
690 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
691 g_return_val_if_fail (event != NULL, FALSE);
693 menu_shell = GTK_MENU_SHELL (widget);
695 if (!menu_shell->active_menu_item && menu_shell->parent_menu_shell)
696 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
698 if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
701 g_object_get (gtk_widget_get_settings (widget),
702 "gtk-enable-mnemonics", &enable_mnemonics,
705 if (enable_mnemonics)
706 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
712 gtk_menu_shell_enter_notify (GtkWidget *widget,
713 GdkEventCrossing *event)
715 GtkMenuShell *menu_shell;
716 GtkWidget *menu_item;
718 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
719 g_return_val_if_fail (event != NULL, FALSE);
721 menu_shell = GTK_MENU_SHELL (widget);
723 if (menu_shell->active)
725 menu_item = gtk_get_event_widget ((GdkEvent*) event);
728 (GTK_IS_MENU_ITEM (menu_item) &&
729 !_gtk_menu_item_is_selectable (menu_item)))
732 if ((menu_item->parent == widget) &&
733 (menu_shell->active_menu_item != menu_item) &&
734 GTK_IS_MENU_ITEM (menu_item))
736 if (menu_shell->ignore_enter)
739 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
740 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
742 gtk_menu_shell_select_item (menu_shell, menu_item);
745 else if (menu_shell->parent_menu_shell)
747 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
755 gtk_menu_shell_leave_notify (GtkWidget *widget,
756 GdkEventCrossing *event)
758 GtkMenuShell *menu_shell;
759 GtkMenuItem *menu_item;
760 GtkWidget *event_widget;
762 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
763 g_return_val_if_fail (event != NULL, FALSE);
765 if (GTK_WIDGET_VISIBLE (widget))
767 menu_shell = GTK_MENU_SHELL (widget);
768 event_widget = gtk_get_event_widget ((GdkEvent*) event);
770 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
773 menu_item = GTK_MENU_ITEM (event_widget);
775 if (!_gtk_menu_item_is_selectable (event_widget))
778 if ((menu_shell->active_menu_item == event_widget) &&
779 (menu_item->submenu == NULL))
781 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
782 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
784 gtk_menu_shell_deselect (menu_shell);
787 else if (menu_shell->parent_menu_shell)
789 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
797 gtk_menu_shell_screen_changed (GtkWidget *widget,
798 GdkScreen *previous_screen)
800 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
804 gtk_menu_shell_add (GtkContainer *container,
807 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
811 gtk_menu_shell_remove (GtkContainer *container,
814 GtkMenuShell *menu_shell;
817 g_return_if_fail (GTK_IS_MENU_SHELL (container));
818 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
820 was_visible = GTK_WIDGET_VISIBLE (widget);
821 menu_shell = GTK_MENU_SHELL (container);
822 menu_shell->children = g_list_remove (menu_shell->children, widget);
824 if (widget == menu_shell->active_menu_item)
826 gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
827 menu_shell->active_menu_item = NULL;
830 gtk_widget_unparent (widget);
832 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
833 * since that's what is needed by toplevels.
836 gtk_widget_queue_resize (GTK_WIDGET (container));
840 gtk_menu_shell_forall (GtkContainer *container,
841 gboolean include_internals,
842 GtkCallback callback,
843 gpointer callback_data)
845 GtkMenuShell *menu_shell;
849 g_return_if_fail (GTK_IS_MENU_SHELL (container));
850 g_return_if_fail (callback != NULL);
852 menu_shell = GTK_MENU_SHELL (container);
854 children = menu_shell->children;
857 child = children->data;
858 children = children->next;
860 (* callback) (child, callback_data);
866 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
868 if (menu_shell->active)
870 menu_shell->button = 0;
871 menu_shell->active = FALSE;
872 menu_shell->activate_time = 0;
874 if (menu_shell->active_menu_item)
876 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
877 menu_shell->active_menu_item = NULL;
880 if (menu_shell->have_grab)
882 menu_shell->have_grab = FALSE;
883 gtk_grab_remove (GTK_WIDGET (menu_shell));
885 if (menu_shell->have_xgrab)
887 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
889 menu_shell->have_xgrab = FALSE;
890 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
891 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
897 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
902 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
903 g_return_val_if_fail (child != NULL, FALSE);
905 parent = child->parent;
906 while (parent && GTK_IS_MENU_SHELL (parent))
908 if (parent == (GtkWidget*) menu_shell)
910 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
917 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
920 GtkWidget *menu_item;
922 menu_item = gtk_get_event_widget ((GdkEvent*) event);
924 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
925 menu_item = menu_item->parent;
927 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
933 /* Handlers for action signals */
936 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
937 GtkWidget *menu_item)
939 GtkMenuShellClass *class;
941 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
942 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
944 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
946 if (class->select_item &&
947 !(menu_shell->active &&
948 menu_shell->active_menu_item == menu_item))
949 class->select_item (menu_shell, menu_item);
952 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
953 GtkSubmenuPlacement placement);
956 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
957 GtkWidget *menu_item)
959 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
961 gtk_menu_shell_deselect (menu_shell);
963 if (!_gtk_menu_item_is_selectable (menu_item))
966 menu_shell->active_menu_item = menu_item;
967 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
968 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
971 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
972 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
973 gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
975 /* This allows the bizarre radio buttons-with-submenus-display-history
978 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
979 gtk_widget_activate (menu_shell->active_menu_item);
983 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
985 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
987 if (menu_shell->active_menu_item)
989 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
990 menu_shell->active_menu_item = NULL;
995 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
996 GtkWidget *menu_item,
997 gboolean force_deactivate)
999 GSList *slist, *shells = NULL;
1000 gboolean deactivate = force_deactivate;
1002 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1003 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1006 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1008 g_object_ref (menu_shell);
1009 g_object_ref (menu_item);
1013 GtkMenuShell *parent_menu_shell = menu_shell;
1017 g_object_ref (parent_menu_shell);
1018 shells = g_slist_prepend (shells, parent_menu_shell);
1019 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1021 while (parent_menu_shell);
1022 shells = g_slist_reverse (shells);
1024 gtk_menu_shell_deactivate (menu_shell);
1026 /* flush the x-queue, so any grabs are removed and
1027 * the menu is actually taken down
1029 gdk_display_sync (gtk_widget_get_display (menu_item));
1032 gtk_widget_activate (menu_item);
1034 for (slist = shells; slist; slist = slist->next)
1036 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1037 g_object_unref (slist->data);
1039 g_slist_free (shells);
1041 g_object_unref (menu_shell);
1042 g_object_unref (menu_item);
1045 /* Distance should be +/- 1 */
1047 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1050 if (menu_shell->active_menu_item)
1052 GList *node = g_list_find (menu_shell->children,
1053 menu_shell->active_menu_item);
1054 GList *start_node = node;
1055 gboolean wrap_around;
1057 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1058 "gtk-keynav-wrap-around", &wrap_around,
1064 while (node != start_node &&
1065 (!node || !_gtk_menu_item_is_selectable (node->data)))
1069 else if (wrap_around)
1070 node = menu_shell->children;
1073 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1081 while (node != start_node &&
1082 (!node || !_gtk_menu_item_is_selectable (node->data)))
1086 else if (wrap_around)
1087 node = g_list_last (menu_shell->children);
1090 gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1097 gtk_menu_shell_select_item (menu_shell, node->data);
1102 * gtk_menu_shell_select_first:
1103 * @menu_shell: a #GtkMenuShell
1104 * @search_sensitive: if %TRUE, search for the first selectable
1105 * menu item, otherwise select nothing if
1106 * the first item isn't sensitive. This
1107 * should be %FALSE if the menu is being
1108 * popped up initially.
1110 * Select the first visible or selectable child of the menu shell;
1111 * don't select tearoff items unless the only item is a tearoff
1117 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1118 gboolean search_sensitive)
1120 GtkWidget *to_select = NULL;
1123 tmp_list = menu_shell->children;
1126 GtkWidget *child = tmp_list->data;
1128 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1129 _gtk_menu_item_is_selectable (child))
1132 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1136 tmp_list = tmp_list->next;
1140 gtk_menu_shell_select_item (menu_shell, to_select);
1144 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1145 gboolean search_sensitive)
1147 GtkWidget *to_select = NULL;
1150 tmp_list = g_list_last (menu_shell->children);
1153 GtkWidget *child = tmp_list->data;
1155 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1156 _gtk_menu_item_is_selectable (child))
1159 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1163 tmp_list = tmp_list->prev;
1167 gtk_menu_shell_select_item (menu_shell, to_select);
1171 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1173 GtkMenuItem *menu_item;
1175 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1177 if (menu_item->submenu)
1179 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item));
1180 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1181 if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1189 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1190 GtkMenuDirectionType direction)
1192 GtkMenuShell *parent_menu_shell = NULL;
1193 gboolean had_selection;
1195 had_selection = menu_shell->active_menu_item != NULL;
1197 if (menu_shell->parent_menu_shell)
1198 parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1202 case GTK_MENU_DIR_PARENT:
1203 if (parent_menu_shell)
1205 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1206 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1207 gtk_menu_shell_deselect (menu_shell);
1210 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1211 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1213 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1214 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1217 /* If there is no parent and the submenu is in the opposite direction
1218 * to the menu, then make the PARENT direction wrap around to
1219 * the bottom of the submenu.
1221 else if (menu_shell->active_menu_item &&
1222 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1223 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1225 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1227 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1228 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1229 _gtk_menu_shell_select_last (submenu, TRUE);
1233 case GTK_MENU_DIR_CHILD:
1234 if (menu_shell->active_menu_item &&
1235 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1236 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1238 if (gtk_menu_shell_select_submenu_first (menu_shell))
1242 /* Try to find a menu running the opposite direction */
1243 while (parent_menu_shell &&
1244 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1245 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1247 GtkWidget *tmp_widget = parent_menu_shell->parent_menu_shell;
1250 parent_menu_shell = GTK_MENU_SHELL (tmp_widget);
1252 parent_menu_shell = NULL;
1255 if (parent_menu_shell)
1257 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1258 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1260 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1262 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1266 case GTK_MENU_DIR_PREV:
1267 gtk_menu_shell_move_selected (menu_shell, -1);
1268 if (!had_selection &&
1269 !menu_shell->active_menu_item &&
1270 menu_shell->children)
1271 _gtk_menu_shell_select_last (menu_shell, TRUE);
1273 case GTK_MENU_DIR_NEXT:
1274 gtk_menu_shell_move_selected (menu_shell, 1);
1275 if (!had_selection &&
1276 !menu_shell->active_menu_item &&
1277 menu_shell->children)
1278 gtk_menu_shell_select_first (menu_shell, TRUE);
1284 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1285 gboolean force_hide)
1287 if (menu_shell->active_menu_item &&
1288 _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1291 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1292 gtk_menu_shell_activate_item (menu_shell,
1293 menu_shell->active_menu_item,
1296 _gtk_menu_item_popup_submenu (menu_shell->active_menu_item);
1301 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1303 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1305 gtk_menu_shell_deselect (menu_shell);
1307 gtk_menu_shell_deactivate (menu_shell);
1308 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1312 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1313 GtkDirectionType dir)
1315 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1317 if (menu_shell->parent_menu_shell)
1318 menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1324 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1328 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1330 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1332 if (klass->get_popup_delay)
1334 return klass->get_popup_delay (menu_shell);
1339 GtkWidget *widget = GTK_WIDGET (menu_shell);
1341 g_object_get (gtk_widget_get_settings (widget),
1342 "gtk-menu-popup-delay", &popup_delay,
1350 * gtk_menu_shell_cancel:
1351 * @menu_shell: a #GtkMenuShell
1353 * Cancels the selection within the menu shell.
1358 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1360 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1362 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1365 static GtkMnemonicHash *
1366 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1369 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1371 if (!private->mnemonic_hash && create)
1372 private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1374 return private->mnemonic_hash;
1378 menu_shell_add_mnemonic_foreach (guint keyval,
1382 GtkKeyHash *key_hash = data;
1384 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1388 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1391 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1392 GtkWidget *widget = GTK_WIDGET (menu_shell);
1394 if (!private->key_hash && create && gtk_widget_has_screen (widget))
1396 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1397 GdkScreen *screen = gtk_widget_get_screen (widget);
1398 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1403 private->key_hash = _gtk_key_hash_new (keymap, NULL);
1405 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1406 menu_shell_add_mnemonic_foreach,
1410 return private->key_hash;
1414 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1416 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1418 if (private->key_hash)
1420 _gtk_key_hash_free (private->key_hash);
1421 private->key_hash = NULL;
1426 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1429 GtkMnemonicHash *mnemonic_hash;
1430 GtkKeyHash *key_hash;
1432 gboolean result = FALSE;
1434 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1438 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1442 entries = _gtk_key_hash_lookup (key_hash,
1443 event->hardware_keycode,
1445 gtk_accelerator_get_default_mod_mask (),
1449 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1450 GPOINTER_TO_UINT (entries->data));
1456 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1460 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1461 g_return_if_fail (GTK_IS_WIDGET (target));
1463 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1465 gtk_menu_shell_reset_key_hash (menu_shell);
1469 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1473 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1474 g_return_if_fail (GTK_IS_WIDGET (target));
1476 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1478 gtk_menu_shell_reset_key_hash (menu_shell);
1482 * gtk_menu_shell_get_take_focus:
1483 * @menu_shell: a #GtkMenuShell
1485 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1487 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1492 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1494 GtkMenuShellPrivate *priv;
1496 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1498 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1500 return priv->take_focus;
1504 * gtk_menu_shell_set_take_focus:
1505 * @menu_shell: a #GtkMenuShell
1506 * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1508 * If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1509 * focus so that it will receive all keyboard events which is needed to enable
1510 * keyboard navigation in menus.
1512 * Setting @take_focus to %FALSE is useful only for special applications
1513 * like virtual keyboard implementations which should not take keyboard
1516 * The @take_focus state of a menu or menu bar is automatically propagated
1517 * to submenus whenever a submenu is popped up, so you don't have to worry
1518 * about recursively setting it for your entire menu hierarchy. Only when
1519 * programmatically picking a submenu and popping it up manually, the
1520 * @take_focus property of the submenu needs to be set explicitely.
1522 * Note that setting it to %FALSE has side-effects:
1524 * If the focus is in some other app, it keeps the focus and keynav in
1525 * the menu doesn't work. Consequently, keynav on the menu will only
1526 * work if the focus is on some toplevel owned by the onscreen keyboard.
1528 * To avoid confusing the user, menus with @take_focus set to %FALSE
1529 * should not display mnemonics or accelerators, since it cannot be
1530 * guaranteed that they will work.
1532 * See also gdk_keyboard_grab()
1537 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1538 gboolean take_focus)
1540 GtkMenuShellPrivate *priv;
1542 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1544 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1546 if (priv->take_focus != take_focus)
1548 priv->take_focus = take_focus;
1549 g_object_notify (G_OBJECT (menu_shell), "take-focus");
1553 #define __GTK_MENU_SHELL_C__
1554 #include "gtkaliasdef.c"