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_class_init (GtkMenuShellClass *klass);
146 static void gtk_menu_shell_init (GtkMenuShell *menu_shell);
147 static void gtk_menu_shell_set_property (GObject *object,
151 static void gtk_menu_shell_get_property (GObject *object,
155 static void gtk_menu_shell_realize (GtkWidget *widget);
156 static void gtk_menu_shell_finalize (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,
204 static GtkContainerClass *parent_class = NULL;
205 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
209 gtk_menu_shell_get_type (void)
211 static GType menu_shell_type = 0;
213 if (!menu_shell_type)
215 static const GTypeInfo menu_shell_info =
217 sizeof (GtkMenuShellClass),
218 NULL, /* base_init */
219 NULL, /* base_finalize */
220 (GClassInitFunc) gtk_menu_shell_class_init,
221 NULL, /* class_finalize */
222 NULL, /* class_data */
223 sizeof (GtkMenuShell),
225 (GInstanceInitFunc) gtk_menu_shell_init,
226 NULL, /* value_table */
230 g_type_register_static (GTK_TYPE_CONTAINER, "GtkMenuShell",
231 &menu_shell_info, G_TYPE_FLAG_ABSTRACT);
234 return menu_shell_type;
238 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
240 GObjectClass *object_class;
241 GtkWidgetClass *widget_class;
242 GtkContainerClass *container_class;
244 GtkBindingSet *binding_set;
246 object_class = (GObjectClass*) klass;
247 widget_class = (GtkWidgetClass*) klass;
248 container_class = (GtkContainerClass*) klass;
250 parent_class = g_type_class_peek_parent (klass);
252 object_class->set_property = gtk_menu_shell_set_property;
253 object_class->get_property = gtk_menu_shell_get_property;
254 object_class->finalize = gtk_menu_shell_finalize;
256 widget_class->realize = gtk_menu_shell_realize;
257 widget_class->button_press_event = gtk_menu_shell_button_press;
258 widget_class->button_release_event = gtk_menu_shell_button_release;
259 widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
260 widget_class->key_press_event = gtk_menu_shell_key_press;
261 widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
262 widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
263 widget_class->screen_changed = gtk_menu_shell_screen_changed;
265 container_class->add = gtk_menu_shell_add;
266 container_class->remove = gtk_menu_shell_remove;
267 container_class->forall = gtk_menu_shell_forall;
268 container_class->child_type = gtk_menu_shell_child_type;
270 klass->submenu_placement = GTK_TOP_BOTTOM;
271 klass->deactivate = gtk_real_menu_shell_deactivate;
272 klass->selection_done = NULL;
273 klass->move_current = gtk_real_menu_shell_move_current;
274 klass->activate_current = gtk_real_menu_shell_activate_current;
275 klass->cancel = gtk_real_menu_shell_cancel;
276 klass->select_item = gtk_menu_shell_real_select_item;
277 klass->insert = gtk_menu_shell_real_insert;
279 menu_shell_signals[DEACTIVATE] =
280 g_signal_new ("deactivate",
281 G_OBJECT_CLASS_TYPE (object_class),
283 G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
285 _gtk_marshal_VOID__VOID,
287 menu_shell_signals[SELECTION_DONE] =
288 g_signal_new ("selection-done",
289 G_OBJECT_CLASS_TYPE (object_class),
291 G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
293 _gtk_marshal_VOID__VOID,
295 menu_shell_signals[MOVE_CURRENT] =
296 g_signal_new ("move_current",
297 G_OBJECT_CLASS_TYPE (object_class),
298 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
299 G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
301 _gtk_marshal_VOID__ENUM,
303 GTK_TYPE_MENU_DIRECTION_TYPE);
304 menu_shell_signals[ACTIVATE_CURRENT] =
305 g_signal_new ("activate_current",
306 G_OBJECT_CLASS_TYPE (object_class),
307 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
308 G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
310 _gtk_marshal_VOID__BOOLEAN,
313 menu_shell_signals[CANCEL] =
314 g_signal_new ("cancel",
315 G_OBJECT_CLASS_TYPE (object_class),
316 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
317 G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
319 _gtk_marshal_VOID__VOID,
321 menu_shell_signals[CYCLE_FOCUS] =
322 _gtk_binding_signal_new ("cycle_focus",
323 G_OBJECT_CLASS_TYPE (object_class),
324 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
325 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
327 _gtk_marshal_VOID__ENUM,
329 GTK_TYPE_DIRECTION_TYPE);
332 binding_set = gtk_binding_set_by_class (klass);
333 gtk_binding_entry_add_signal (binding_set,
336 gtk_binding_entry_add_signal (binding_set,
338 "activate_current", 1,
341 gtk_binding_entry_add_signal (binding_set,
343 "activate_current", 1,
346 gtk_binding_entry_add_signal (binding_set,
348 "activate_current", 1,
351 gtk_binding_entry_add_signal (binding_set,
353 "activate_current", 1,
356 gtk_binding_entry_add_signal (binding_set,
359 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
360 gtk_binding_entry_add_signal (binding_set,
361 GDK_F10, GDK_SHIFT_MASK,
363 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
366 * GtkMenuShell:take-focus:
368 * A boolean that determines whether the menu and its submenus grab the
369 * keyboard focus. See gtk_menu_shell_set_take_focus() and
370 * gtk_menu_shell_get_take_focus().
374 g_object_class_install_property (object_class,
376 g_param_spec_boolean ("take-focus",
378 P_("A boolean that determines whether the menu grabs the keyboard focus"),
380 GTK_PARAM_READWRITE));
382 g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
386 gtk_menu_shell_child_type (GtkContainer *container)
388 return GTK_TYPE_MENU_ITEM;
392 gtk_menu_shell_init (GtkMenuShell *menu_shell)
394 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
396 menu_shell->children = NULL;
397 menu_shell->active_menu_item = NULL;
398 menu_shell->parent_menu_shell = NULL;
399 menu_shell->active = FALSE;
400 menu_shell->have_grab = FALSE;
401 menu_shell->have_xgrab = FALSE;
402 menu_shell->button = 0;
403 menu_shell->activate_time = 0;
405 priv->mnemonic_hash = NULL;
406 priv->key_hash = NULL;
407 priv->take_focus = TRUE;
411 gtk_menu_shell_set_property (GObject *object,
416 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
420 case PROP_TAKE_FOCUS:
421 gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
424 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430 gtk_menu_shell_get_property (GObject *object,
435 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
439 case PROP_TAKE_FOCUS:
440 g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
443 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
449 gtk_menu_shell_finalize (GObject *object)
451 GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
452 GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
454 if (priv->mnemonic_hash)
455 _gtk_mnemonic_hash_free (priv->mnemonic_hash);
457 _gtk_key_hash_free (priv->key_hash);
459 G_OBJECT_CLASS (parent_class)->finalize (object);
464 gtk_menu_shell_append (GtkMenuShell *menu_shell,
467 gtk_menu_shell_insert (menu_shell, child, -1);
471 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
474 gtk_menu_shell_insert (menu_shell, child, 0);
478 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
482 GtkMenuShellClass *class;
484 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
485 g_return_if_fail (GTK_IS_MENU_ITEM (child));
487 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
490 class->insert (menu_shell, child, position);
494 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
498 menu_shell->children = g_list_insert (menu_shell->children, child, position);
500 gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
504 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
506 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
508 g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
512 gtk_menu_shell_realize (GtkWidget *widget)
514 GdkWindowAttr attributes;
515 gint attributes_mask;
517 g_return_if_fail (GTK_IS_MENU_SHELL (widget));
519 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
521 attributes.x = widget->allocation.x;
522 attributes.y = widget->allocation.y;
523 attributes.width = widget->allocation.width;
524 attributes.height = widget->allocation.height;
525 attributes.window_type = GDK_WINDOW_CHILD;
526 attributes.wclass = GDK_INPUT_OUTPUT;
527 attributes.visual = gtk_widget_get_visual (widget);
528 attributes.colormap = gtk_widget_get_colormap (widget);
529 attributes.event_mask = gtk_widget_get_events (widget);
530 attributes.event_mask |= (GDK_EXPOSURE_MASK |
531 GDK_BUTTON_PRESS_MASK |
532 GDK_BUTTON_RELEASE_MASK |
534 GDK_ENTER_NOTIFY_MASK |
535 GDK_LEAVE_NOTIFY_MASK);
537 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
538 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
539 gdk_window_set_user_data (widget->window, widget);
541 widget->style = gtk_style_attach (widget->style, widget->window);
542 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
546 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
548 if (!menu_shell->active)
550 gtk_grab_add (GTK_WIDGET (menu_shell));
551 menu_shell->have_grab = TRUE;
552 menu_shell->active = TRUE;
557 gtk_menu_shell_button_press (GtkWidget *widget,
558 GdkEventButton *event)
560 GtkMenuShell *menu_shell;
561 GtkWidget *menu_item;
563 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
564 g_return_val_if_fail (event != NULL, FALSE);
566 if (event->type != GDK_BUTTON_PRESS)
569 menu_shell = GTK_MENU_SHELL (widget);
571 if (menu_shell->parent_menu_shell)
573 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
575 else if (!menu_shell->active || !menu_shell->button)
577 _gtk_menu_shell_activate (menu_shell);
579 menu_shell->button = event->button;
581 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
583 if (menu_item && _gtk_menu_item_is_selectable (menu_item))
585 if ((menu_item->parent == widget) &&
586 (menu_item != menu_shell->active_menu_item))
588 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
590 menu_shell->activate_time = event->time;
593 gtk_menu_shell_select_item (menu_shell, menu_item);
599 widget = gtk_get_event_widget ((GdkEvent*) event);
600 if (widget == GTK_WIDGET (menu_shell))
602 gtk_menu_shell_deactivate (menu_shell);
603 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
611 gtk_menu_shell_grab_broken (GtkWidget *widget,
612 GdkEventGrabBroken *event)
614 GtkMenuShell *menu_shell;
615 GtkWidget *menu_item;
618 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
619 g_return_val_if_fail (event != NULL, FALSE);
621 menu_shell = GTK_MENU_SHELL (widget);
622 if (menu_shell->active)
624 gtk_menu_shell_deactivate (menu_shell);
625 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
632 gtk_menu_shell_button_release (GtkWidget *widget,
633 GdkEventButton *event)
635 GtkMenuShell *menu_shell;
636 GtkWidget *menu_item;
639 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
640 g_return_val_if_fail (event != NULL, FALSE);
642 menu_shell = GTK_MENU_SHELL (widget);
643 if (menu_shell->active)
645 if (menu_shell->button && (event->button != menu_shell->button))
647 menu_shell->button = 0;
648 if (menu_shell->parent_menu_shell)
649 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
652 menu_shell->button = 0;
653 menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
657 if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
659 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
660 _gtk_menu_item_is_selectable (menu_item))
662 if (GTK_MENU_ITEM (menu_item)->submenu == NULL)
664 gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
667 else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
669 gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
673 else if (menu_item &&
674 !_gtk_menu_item_is_selectable (menu_item) &&
675 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
679 else if (menu_shell->parent_menu_shell)
681 menu_shell->active = TRUE;
682 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
686 /* If we ended up on an item with a submenu, leave the menu up.
688 if (menu_item && (menu_shell->active_menu_item == menu_item) &&
689 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
694 else /* a very fast press-release */
696 /* We only ever want to prevent deactivation on the first
697 * press/release. Setting the time to zero is a bit of a
698 * hack, since we could be being triggered in the first
699 * few fractions of a second after a server time wraparound.
700 * the chances of that happening are ~1/10^6, without
701 * serious harm if we lose.
703 menu_shell->activate_time = 0;
709 gtk_menu_shell_deactivate (menu_shell);
710 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
718 gtk_menu_shell_key_press (GtkWidget *widget,
721 GtkMenuShell *menu_shell;
723 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
724 g_return_val_if_fail (event != NULL, FALSE);
726 menu_shell = GTK_MENU_SHELL (widget);
728 if (!menu_shell->active_menu_item && menu_shell->parent_menu_shell)
729 return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
731 if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
734 return gtk_menu_shell_activate_mnemonic (menu_shell, event);
738 gtk_menu_shell_enter_notify (GtkWidget *widget,
739 GdkEventCrossing *event)
741 GtkMenuShell *menu_shell;
742 GtkWidget *menu_item;
744 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
745 g_return_val_if_fail (event != NULL, FALSE);
747 menu_shell = GTK_MENU_SHELL (widget);
749 if (menu_shell->active)
751 menu_item = gtk_get_event_widget ((GdkEvent*) event);
754 (GTK_IS_MENU_ITEM (menu_item) &&
755 !_gtk_menu_item_is_selectable (menu_item)))
758 if ((menu_item->parent == widget) &&
759 (menu_shell->active_menu_item != menu_item) &&
760 GTK_IS_MENU_ITEM (menu_item))
762 if (menu_shell->ignore_enter)
765 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
766 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_PRELIGHT))
768 gtk_menu_shell_select_item (menu_shell, menu_item);
771 else if (menu_shell->parent_menu_shell)
773 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
781 gtk_menu_shell_leave_notify (GtkWidget *widget,
782 GdkEventCrossing *event)
784 GtkMenuShell *menu_shell;
785 GtkMenuItem *menu_item;
786 GtkWidget *event_widget;
788 g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE);
789 g_return_val_if_fail (event != NULL, FALSE);
791 if (GTK_WIDGET_VISIBLE (widget))
793 menu_shell = GTK_MENU_SHELL (widget);
794 event_widget = gtk_get_event_widget ((GdkEvent*) event);
796 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
799 menu_item = GTK_MENU_ITEM (event_widget);
801 if (!_gtk_menu_item_is_selectable (event_widget))
804 if ((menu_shell->active_menu_item == event_widget) &&
805 (menu_item->submenu == NULL))
807 if ((event->detail != GDK_NOTIFY_INFERIOR) &&
808 (GTK_WIDGET_STATE (menu_item) != GTK_STATE_NORMAL))
810 gtk_menu_shell_deselect (menu_shell);
813 else if (menu_shell->parent_menu_shell)
815 gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
823 gtk_menu_shell_screen_changed (GtkWidget *widget,
824 GdkScreen *previous_screen)
826 gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
830 gtk_menu_shell_add (GtkContainer *container,
833 gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
837 gtk_menu_shell_remove (GtkContainer *container,
840 GtkMenuShell *menu_shell;
843 g_return_if_fail (GTK_IS_MENU_SHELL (container));
844 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
846 was_visible = GTK_WIDGET_VISIBLE (widget);
847 menu_shell = GTK_MENU_SHELL (container);
848 menu_shell->children = g_list_remove (menu_shell->children, widget);
850 if (widget == menu_shell->active_menu_item)
852 gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
853 menu_shell->active_menu_item = NULL;
856 gtk_widget_unparent (widget);
858 /* queue resize regardless of GTK_WIDGET_VISIBLE (container),
859 * since that's what is needed by toplevels.
862 gtk_widget_queue_resize (GTK_WIDGET (container));
866 gtk_menu_shell_forall (GtkContainer *container,
867 gboolean include_internals,
868 GtkCallback callback,
869 gpointer callback_data)
871 GtkMenuShell *menu_shell;
875 g_return_if_fail (GTK_IS_MENU_SHELL (container));
876 g_return_if_fail (callback != NULL);
878 menu_shell = GTK_MENU_SHELL (container);
880 children = menu_shell->children;
883 child = children->data;
884 children = children->next;
886 (* callback) (child, callback_data);
892 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
894 if (menu_shell->active)
896 menu_shell->button = 0;
897 menu_shell->active = FALSE;
898 menu_shell->activate_time = 0;
900 if (menu_shell->active_menu_item)
902 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
903 menu_shell->active_menu_item = NULL;
906 if (menu_shell->have_grab)
908 menu_shell->have_grab = FALSE;
909 gtk_grab_remove (GTK_WIDGET (menu_shell));
911 if (menu_shell->have_xgrab)
913 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu_shell));
915 menu_shell->have_xgrab = FALSE;
916 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
917 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
923 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
928 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
929 g_return_val_if_fail (child != NULL, FALSE);
931 parent = child->parent;
932 while (parent && GTK_IS_MENU_SHELL (parent))
934 if (parent == (GtkWidget*) menu_shell)
936 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
943 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
946 GtkWidget *menu_item;
948 menu_item = gtk_get_event_widget ((GdkEvent*) event);
950 while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
951 menu_item = menu_item->parent;
953 if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
959 /* Handlers for action signals */
962 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
963 GtkWidget *menu_item)
965 GtkMenuShellClass *class;
967 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
968 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
970 class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
972 if (class->select_item &&
973 !(menu_shell->active &&
974 menu_shell->active_menu_item == menu_item))
975 class->select_item (menu_shell, menu_item);
978 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
979 GtkSubmenuPlacement placement);
982 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
983 GtkWidget *menu_item)
985 GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
987 gtk_menu_shell_deselect (menu_shell);
989 if (!_gtk_menu_item_is_selectable (menu_item))
992 menu_shell->active_menu_item = menu_item;
993 if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
994 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
997 _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
998 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
999 gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1001 /* This allows the bizarre radio buttons-with-submenus-display-history
1004 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1005 gtk_widget_activate (menu_shell->active_menu_item);
1009 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1011 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1013 if (menu_shell->active_menu_item)
1015 gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1016 menu_shell->active_menu_item = NULL;
1021 gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
1022 GtkWidget *menu_item,
1023 gboolean force_deactivate)
1025 GSList *slist, *shells = NULL;
1026 gboolean deactivate = force_deactivate;
1028 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1029 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1032 deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1034 g_object_ref (menu_shell);
1035 g_object_ref (menu_item);
1039 GtkMenuShell *parent_menu_shell = menu_shell;
1043 g_object_ref (parent_menu_shell);
1044 shells = g_slist_prepend (shells, parent_menu_shell);
1045 parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1047 while (parent_menu_shell);
1048 shells = g_slist_reverse (shells);
1050 gtk_menu_shell_deactivate (menu_shell);
1052 /* flush the x-queue, so any grabs are removed and
1053 * the menu is actually taken down
1055 gdk_display_sync (gtk_widget_get_display (menu_item));
1058 gtk_widget_activate (menu_item);
1060 for (slist = shells; slist; slist = slist->next)
1062 g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1063 g_object_unref (slist->data);
1065 g_slist_free (shells);
1067 g_object_unref (menu_shell);
1068 g_object_unref (menu_item);
1071 /* Distance should be +/- 1 */
1073 gtk_menu_shell_move_selected (GtkMenuShell *menu_shell,
1076 if (menu_shell->active_menu_item)
1078 GList *node = g_list_find (menu_shell->children,
1079 menu_shell->active_menu_item);
1080 GList *start_node = node;
1085 while (node != start_node &&
1086 (!node || !_gtk_menu_item_is_selectable (node->data)))
1089 node = menu_shell->children;
1097 while (node != start_node &&
1098 (!node || !_gtk_menu_item_is_selectable (node->data)))
1101 node = g_list_last (menu_shell->children);
1108 gtk_menu_shell_select_item (menu_shell, node->data);
1113 * gtk_menu_shell_select_first:
1114 * @menu_shell: a #GtkMenuShell
1115 * @search_sensitive: if %TRUE, search for the first selectable
1116 * menu item, otherwise select nothing if
1117 * the first item isn't sensitive. This
1118 * should be %FALSE if the menu is being
1119 * popped up initially.
1121 * Select the first visible or selectable child of the menu shell;
1122 * don't select tearoff items unless the only item is a tearoff
1128 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1129 gboolean search_sensitive)
1131 GtkWidget *to_select = NULL;
1134 tmp_list = menu_shell->children;
1137 GtkWidget *child = tmp_list->data;
1139 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1140 _gtk_menu_item_is_selectable (child))
1143 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1147 tmp_list = tmp_list->next;
1151 gtk_menu_shell_select_item (menu_shell, to_select);
1155 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1156 gboolean search_sensitive)
1158 GtkWidget *to_select = NULL;
1161 tmp_list = g_list_last (menu_shell->children);
1164 GtkWidget *child = tmp_list->data;
1166 if ((!search_sensitive && GTK_WIDGET_VISIBLE (child)) ||
1167 _gtk_menu_item_is_selectable (child))
1170 if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1174 tmp_list = tmp_list->prev;
1178 gtk_menu_shell_select_item (menu_shell, to_select);
1182 gtk_menu_shell_select_submenu_first (GtkMenuShell *menu_shell)
1184 GtkMenuItem *menu_item;
1186 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1188 if (menu_item->submenu)
1190 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item));
1191 gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1192 if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1200 gtk_real_menu_shell_move_current (GtkMenuShell *menu_shell,
1201 GtkMenuDirectionType direction)
1203 GtkMenuShell *parent_menu_shell = NULL;
1204 gboolean had_selection;
1206 had_selection = menu_shell->active_menu_item != NULL;
1208 if (menu_shell->parent_menu_shell)
1209 parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1213 case GTK_MENU_DIR_PARENT:
1214 if (parent_menu_shell)
1216 if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1217 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1218 gtk_menu_shell_deselect (menu_shell);
1221 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1222 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1224 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1225 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1228 /* If there is no parent and the submenu is in the opposite direction
1229 * to the menu, then make the PARENT direction wrap around to
1230 * the bottom of the submenu.
1232 else if (menu_shell->active_menu_item &&
1233 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1234 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1236 GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1238 if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1239 GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1240 _gtk_menu_shell_select_last (submenu, TRUE);
1244 case GTK_MENU_DIR_CHILD:
1245 if (menu_shell->active_menu_item &&
1246 _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1247 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1249 if (gtk_menu_shell_select_submenu_first (menu_shell))
1253 /* Try to find a menu running the opposite direction */
1254 while (parent_menu_shell &&
1255 (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1256 GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1258 GtkWidget *tmp_widget = parent_menu_shell->parent_menu_shell;
1261 parent_menu_shell = GTK_MENU_SHELL (tmp_widget);
1263 parent_menu_shell = NULL;
1266 if (parent_menu_shell)
1268 if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1269 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1271 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1273 gtk_menu_shell_select_submenu_first (parent_menu_shell);
1277 case GTK_MENU_DIR_PREV:
1278 gtk_menu_shell_move_selected (menu_shell, -1);
1279 if (!had_selection &&
1280 !menu_shell->active_menu_item &&
1281 menu_shell->children)
1282 _gtk_menu_shell_select_last (menu_shell, TRUE);
1284 case GTK_MENU_DIR_NEXT:
1285 gtk_menu_shell_move_selected (menu_shell, 1);
1286 if (!had_selection &&
1287 !menu_shell->active_menu_item &&
1288 menu_shell->children)
1289 gtk_menu_shell_select_first (menu_shell, TRUE);
1295 gtk_real_menu_shell_activate_current (GtkMenuShell *menu_shell,
1296 gboolean force_hide)
1298 if (menu_shell->active_menu_item &&
1299 _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1302 if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1303 gtk_menu_shell_activate_item (menu_shell,
1304 menu_shell->active_menu_item,
1307 _gtk_menu_item_popup_submenu (menu_shell->active_menu_item);
1312 gtk_real_menu_shell_cancel (GtkMenuShell *menu_shell)
1314 /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1316 gtk_menu_shell_deselect (menu_shell);
1318 gtk_menu_shell_deactivate (menu_shell);
1319 g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1323 gtk_real_menu_shell_cycle_focus (GtkMenuShell *menu_shell,
1324 GtkDirectionType dir)
1326 while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1328 if (menu_shell->parent_menu_shell)
1329 menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1335 _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1339 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1341 GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1343 if (klass->get_popup_delay)
1345 return klass->get_popup_delay (menu_shell);
1350 GtkWidget *widget = GTK_WIDGET (menu_shell);
1352 g_object_get (gtk_widget_get_settings (widget),
1353 "gtk-menu-popup-delay", &popup_delay,
1361 * gtk_menu_shell_cancel:
1362 * @menu_shell: a #GtkMenuShell
1364 * Cancels the selection within the menu shell.
1369 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1371 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1373 g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1376 static GtkMnemonicHash *
1377 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1380 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1382 if (!private->mnemonic_hash && create)
1383 private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1385 return private->mnemonic_hash;
1389 menu_shell_add_mnemonic_foreach (guint keyval,
1393 GtkKeyHash *key_hash = data;
1395 _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1399 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1402 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1403 GtkWidget *widget = GTK_WIDGET (menu_shell);
1405 if (!private->key_hash && create && gtk_widget_has_screen (widget))
1407 GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1408 GdkScreen *screen = gtk_widget_get_screen (widget);
1409 GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1414 private->key_hash = _gtk_key_hash_new (keymap, NULL);
1416 _gtk_mnemonic_hash_foreach (mnemonic_hash,
1417 menu_shell_add_mnemonic_foreach,
1421 return private->key_hash;
1425 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1427 GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1429 if (private->key_hash)
1431 _gtk_key_hash_free (private->key_hash);
1432 private->key_hash = NULL;
1437 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1440 GtkMnemonicHash *mnemonic_hash;
1441 GtkKeyHash *key_hash;
1443 gboolean result = FALSE;
1445 mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1449 key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1453 entries = _gtk_key_hash_lookup (key_hash,
1454 event->hardware_keycode,
1456 gtk_accelerator_get_default_mod_mask (),
1460 result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1461 GPOINTER_TO_UINT (entries->data));
1467 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1471 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1472 g_return_if_fail (GTK_IS_WIDGET (target));
1474 _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1476 gtk_menu_shell_reset_key_hash (menu_shell);
1480 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1484 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1485 g_return_if_fail (GTK_IS_WIDGET (target));
1487 _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1489 gtk_menu_shell_reset_key_hash (menu_shell);
1493 * gtk_menu_shell_get_take_focus:
1494 * @menu_shell: a #GtkMenuShell
1496 * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1498 * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1503 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1505 GtkMenuShellPrivate *priv;
1507 g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1509 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1511 return priv->take_focus;
1515 * gtk_menu_shell_set_take_focus:
1516 * @menu_shell: a #GtkMenuShell
1517 * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1519 * If @take_focus is %TRUE (the default) the menu shell will take the keyboard
1520 * focus so that it will receive all keyboard events which is needed to enable
1521 * keyboard navigation in menus.
1523 * Setting @take_focus to %FALSE is useful only for special applications
1524 * like virtual keyboard implementations which should not take keyboard
1527 * The @take_focus state of a menu or menu bar is automatically propagated
1528 * to submenus whenever a submenu is popped up, so you don't have to worry
1529 * about recursively setting it for your entire menu hierarchy. Only when
1530 * programmatically picking a submenu and popping it up manually, the
1531 * @take_focus property of the submenu needs to be set explicitely.
1533 * Note that setting it to %FALSE has side-effects:
1535 * If the focus is in some other app, it keeps the focus and keynav in
1536 * the menu doesn't work. Consequently, keynav on the menu will only
1537 * work if the focus is on some toplevel owned by the onscreen keyboard.
1539 * To avoid confusing the user, menus with @take_focus set to %FALSE
1540 * should not display mnemonics or accelerators, since it cannot be
1541 * guaranteed that they will work.
1543 * See also gdk_keyboard_grab()
1548 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1549 gboolean take_focus)
1551 GtkMenuShellPrivate *priv;
1553 g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1555 priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1557 if (priv->take_focus != take_focus)
1559 priv->take_focus = take_focus;
1560 g_object_notify (G_OBJECT (menu_shell), "take-focus");
1564 #define __GTK_MENU_SHELL_C__
1565 #include "gtkaliasdef.c"