]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenushell.c
gtk/: fully remove gtkalias hacks
[~andy/gtk] / gtk / gtkmenushell.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #define GTK_MENU_INTERNALS
28
29 #include "config.h"
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbindings.h"
32 #include "gtkkeyhash.h"
33 #include "gtklabel.h"
34 #include "gtkmain.h"
35 #include "gtkmarshalers.h"
36 #include "gtkmenu.h"
37 #include "gtkmenubar.h"
38 #include "gtkmenuitem.h"
39 #include "gtkmenushell.h"
40 #include "gtkmnemonichash.h"
41 #include "gtktearoffmenuitem.h"
42 #include "gtkwindow.h"
43 #include "gtkprivate.h"
44 #include "gtkintl.h"
45
46 #define MENU_SHELL_TIMEOUT   500
47
48 #define PACK_DIRECTION(m)                                 \
49    (GTK_IS_MENU_BAR (m)                                   \
50      ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
51      : GTK_PACK_DIRECTION_LTR)
52
53 enum {
54   DEACTIVATE,
55   SELECTION_DONE,
56   MOVE_CURRENT,
57   ACTIVATE_CURRENT,
58   CANCEL,
59   CYCLE_FOCUS,
60   MOVE_SELECTED,
61   LAST_SIGNAL
62 };
63
64 enum {
65   PROP_0,
66   PROP_TAKE_FOCUS
67 };
68
69 /* Terminology:
70  * 
71  * A menu item can be "selected", this means that it is displayed
72  * in the prelight state, and if it has a submenu, that submenu
73  * will be popped up. 
74  * 
75  * A menu is "active" when it is visible onscreen and the user
76  * is selecting from it. A menubar is not active until the user
77  * clicks on one of its menuitems. When a menu is active,
78  * passing the mouse over a submenu will pop it up.
79  *
80  * menu_shell->active_menu_item, is however, not an "active"
81  * menu item (there is no such thing) but rather, the selected
82  * menu item in that MenuShell, if there is one.
83  *
84  * There is also is a concept of the current menu and a current
85  * menu item. The current menu item is the selected menu item
86  * that is furthest down in the hierarchy. (Every active menu_shell
87  * does not necessarily contain a selected menu item, but if
88  * it does, then menu_shell->parent_menu_shell must also contain
89  * a selected menu item. The current menu is the menu that 
90  * contains the current menu_item. It will always have a GTK
91  * grab and receive all key presses.
92  *
93  *
94  * Action signals:
95  *
96  *  ::move_current (GtkMenuDirection *dir)
97  *     Moves the current menu item in direction 'dir':
98  *
99  *       GTK_MENU_DIR_PARENT: To the parent menu shell
100  *       GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
101  *          a submenu.
102  *       GTK_MENU_DIR_NEXT/PREV: To the next or previous item
103  *          in this menu.
104  * 
105  *     As a a bit of a hack to get movement between menus and
106  *     menubars working, if submenu_placement is different for
107  *     the menu and its MenuShell then the following apply:
108  * 
109  *       - For 'parent' the current menu is not just moved to
110  *         the parent, but moved to the previous entry in the parent
111  *       - For 'child', if there is no child, then current is
112  *         moved to the next item in the parent.
113  *
114  *    Note that the above explanation of ::move_current was written
115  *    before menus and menubars had support for RTL flipping and
116  *    different packing directions, and therefore only applies for
117  *    when text direction and packing direction are both left-to-right.
118  * 
119  *  ::activate_current (GBoolean *force_hide)
120  *     Activate the current item. If 'force_hide' is true, hide
121  *     the current menu item always. Otherwise, only hide
122  *     it if menu_item->klass->hide_on_activate is true.
123  *
124  *  ::cancel ()
125  *     Cancels the current selection
126  */
127
128 #define GTK_MENU_SHELL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_MENU_SHELL, GtkMenuShellPrivate))
129
130 typedef struct _GtkMenuShellPrivate GtkMenuShellPrivate;
131
132 struct _GtkMenuShellPrivate
133 {
134   GtkMnemonicHash *mnemonic_hash;
135   GtkKeyHash *key_hash;
136
137   GdkDevice *grab_pointer;
138
139   guint take_focus : 1;
140   guint activated_submenu : 1;
141   /* This flag is a crutch to keep mnemonics in the same menu
142    * if the user moves the mouse over an unselectable menuitem.
143    */
144   guint in_unselectable_item : 1;
145 };
146
147 static void gtk_menu_shell_set_property      (GObject           *object,
148                                               guint              prop_id,
149                                               const GValue      *value,
150                                               GParamSpec        *pspec);
151 static void gtk_menu_shell_get_property      (GObject           *object,
152                                               guint              prop_id,
153                                               GValue            *value,
154                                               GParamSpec        *pspec);
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,
162                                               GdkEventKey       *event);
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,
172                                               GtkWidget         *widget);
173 static void gtk_menu_shell_remove            (GtkContainer      *container,
174                                               GtkWidget         *widget);
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,
180                                               GtkWidget    *child,
181                                               gint          position);
182 static void gtk_real_menu_shell_deactivate   (GtkMenuShell      *menu_shell);
183 static gint gtk_menu_shell_is_item           (GtkMenuShell      *menu_shell,
184                                               GtkWidget         *child);
185 static GtkWidget *gtk_menu_shell_get_item    (GtkMenuShell      *menu_shell,
186                                               GdkEvent          *event);
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); 
191
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);
199
200 static void     gtk_menu_shell_reset_key_hash    (GtkMenuShell *menu_shell);
201 static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
202                                                   GdkEventKey  *event);
203 static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell  *menu_shell, 
204                                                    gint           distance);
205
206 static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
207
208 G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
209
210 static void
211 gtk_menu_shell_class_init (GtkMenuShellClass *klass)
212 {
213   GObjectClass *object_class;
214   GtkWidgetClass *widget_class;
215   GtkContainerClass *container_class;
216
217   GtkBindingSet *binding_set;
218
219   object_class = (GObjectClass*) klass;
220   widget_class = (GtkWidgetClass*) klass;
221   container_class = (GtkContainerClass*) klass;
222
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
227   widget_class->realize = gtk_menu_shell_realize;
228   widget_class->button_press_event = gtk_menu_shell_button_press;
229   widget_class->button_release_event = gtk_menu_shell_button_release;
230   widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
231   widget_class->key_press_event = gtk_menu_shell_key_press;
232   widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
233   widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
234   widget_class->screen_changed = gtk_menu_shell_screen_changed;
235
236   container_class->add = gtk_menu_shell_add;
237   container_class->remove = gtk_menu_shell_remove;
238   container_class->forall = gtk_menu_shell_forall;
239   container_class->child_type = gtk_menu_shell_child_type;
240
241   klass->submenu_placement = GTK_TOP_BOTTOM;
242   klass->deactivate = gtk_real_menu_shell_deactivate;
243   klass->selection_done = NULL;
244   klass->move_current = gtk_real_menu_shell_move_current;
245   klass->activate_current = gtk_real_menu_shell_activate_current;
246   klass->cancel = gtk_real_menu_shell_cancel;
247   klass->select_item = gtk_menu_shell_real_select_item;
248   klass->insert = gtk_menu_shell_real_insert;
249   klass->move_selected = gtk_menu_shell_real_move_selected;
250
251   menu_shell_signals[DEACTIVATE] =
252     g_signal_new (I_("deactivate"),
253                   G_OBJECT_CLASS_TYPE (object_class),
254                   G_SIGNAL_RUN_FIRST,
255                   G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
256                   NULL, NULL,
257                   _gtk_marshal_VOID__VOID,
258                   G_TYPE_NONE, 0);
259
260   menu_shell_signals[SELECTION_DONE] =
261     g_signal_new (I_("selection-done"),
262                   G_OBJECT_CLASS_TYPE (object_class),
263                   G_SIGNAL_RUN_FIRST,
264                   G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
265                   NULL, NULL,
266                   _gtk_marshal_VOID__VOID,
267                   G_TYPE_NONE, 0);
268
269   menu_shell_signals[MOVE_CURRENT] =
270     g_signal_new (I_("move-current"),
271                   G_OBJECT_CLASS_TYPE (object_class),
272                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
273                   G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
274                   NULL, NULL,
275                   _gtk_marshal_VOID__ENUM,
276                   G_TYPE_NONE, 1,
277                   GTK_TYPE_MENU_DIRECTION_TYPE);
278
279   menu_shell_signals[ACTIVATE_CURRENT] =
280     g_signal_new (I_("activate-current"),
281                   G_OBJECT_CLASS_TYPE (object_class),
282                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
283                   G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
284                   NULL, NULL,
285                   _gtk_marshal_VOID__BOOLEAN,
286                   G_TYPE_NONE, 1,
287                   G_TYPE_BOOLEAN);
288
289   menu_shell_signals[CANCEL] =
290     g_signal_new (I_("cancel"),
291                   G_OBJECT_CLASS_TYPE (object_class),
292                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
293                   G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
294                   NULL, NULL,
295                   _gtk_marshal_VOID__VOID,
296                   G_TYPE_NONE, 0);
297
298   menu_shell_signals[CYCLE_FOCUS] =
299     g_signal_new_class_handler (I_("cycle-focus"),
300                                 G_OBJECT_CLASS_TYPE (object_class),
301                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
302                                 G_CALLBACK (gtk_real_menu_shell_cycle_focus),
303                                 NULL, NULL,
304                                 _gtk_marshal_VOID__ENUM,
305                                 G_TYPE_NONE, 1,
306                                 GTK_TYPE_DIRECTION_TYPE);
307
308   /**
309    * GtkMenuShell::move-selected:
310    * @menu_shell: the object on which the signal is emitted
311    * @distance: +1 to move to the next item, -1 to move to the previous
312    *
313    * The ::move-selected signal is emitted to move the selection to
314    * another item.
315    *
316    * Returns: %TRUE to stop the signal emission, %FALSE to continue
317    *
318    * Since: 2.12
319    */
320   menu_shell_signals[MOVE_SELECTED] =
321     g_signal_new (I_("move-selected"),
322                   G_OBJECT_CLASS_TYPE (object_class),
323                   G_SIGNAL_RUN_LAST,
324                   G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
325                   _gtk_boolean_handled_accumulator, NULL,
326                   _gtk_marshal_BOOLEAN__INT,
327                   G_TYPE_BOOLEAN, 1,
328                   G_TYPE_INT);
329
330   binding_set = gtk_binding_set_by_class (klass);
331   gtk_binding_entry_add_signal (binding_set,
332                                 GDK_Escape, 0,
333                                 "cancel", 0);
334   gtk_binding_entry_add_signal (binding_set,
335                                 GDK_Return, 0,
336                                 "activate-current", 1,
337                                 G_TYPE_BOOLEAN,
338                                 TRUE);
339   gtk_binding_entry_add_signal (binding_set,
340                                 GDK_ISO_Enter, 0,
341                                 "activate-current", 1,
342                                 G_TYPE_BOOLEAN,
343                                 TRUE);
344   gtk_binding_entry_add_signal (binding_set,
345                                 GDK_KP_Enter, 0,
346                                 "activate-current", 1,
347                                 G_TYPE_BOOLEAN,
348                                 TRUE);
349   gtk_binding_entry_add_signal (binding_set,
350                                 GDK_space, 0,
351                                 "activate-current", 1,
352                                 G_TYPE_BOOLEAN,
353                                 FALSE);
354   gtk_binding_entry_add_signal (binding_set,
355                                 GDK_KP_Space, 0,
356                                 "activate-current", 1,
357                                 G_TYPE_BOOLEAN,
358                                 FALSE);
359   gtk_binding_entry_add_signal (binding_set,
360                                 GDK_F10, 0,
361                                 "cycle-focus", 1,
362                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
363   gtk_binding_entry_add_signal (binding_set,
364                                 GDK_F10, GDK_SHIFT_MASK,
365                                 "cycle-focus", 1,
366                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
367
368   /**
369    * GtkMenuShell:take-focus:
370    *
371    * A boolean that determines whether the menu and its submenus grab the
372    * keyboard focus. See gtk_menu_shell_set_take_focus() and
373    * gtk_menu_shell_get_take_focus().
374    *
375    * Since: 2.8
376    **/
377   g_object_class_install_property (object_class,
378                                    PROP_TAKE_FOCUS,
379                                    g_param_spec_boolean ("take-focus",
380                                                          P_("Take Focus"),
381                                                          P_("A boolean that determines whether the menu grabs the keyboard focus"),
382                                                          TRUE,
383                                                          GTK_PARAM_READWRITE));
384
385   g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
386 }
387
388 static GType
389 gtk_menu_shell_child_type (GtkContainer     *container)
390 {
391   return GTK_TYPE_MENU_ITEM;
392 }
393
394 static void
395 gtk_menu_shell_init (GtkMenuShell *menu_shell)
396 {
397   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
398
399   menu_shell->children = NULL;
400   menu_shell->active_menu_item = NULL;
401   menu_shell->parent_menu_shell = NULL;
402   menu_shell->active = FALSE;
403   menu_shell->have_grab = FALSE;
404   menu_shell->have_xgrab = FALSE;
405   menu_shell->button = 0;
406   menu_shell->activate_time = 0;
407
408   priv->mnemonic_hash = NULL;
409   priv->key_hash = NULL;
410   priv->take_focus = TRUE;
411   priv->activated_submenu = FALSE;
412 }
413
414 static void
415 gtk_menu_shell_set_property (GObject      *object,
416                              guint         prop_id,
417                              const GValue *value,
418                              GParamSpec   *pspec)
419 {
420   GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
421
422   switch (prop_id)
423     {
424     case PROP_TAKE_FOCUS:
425       gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
426       break;
427     default:
428       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
429       break;
430     }
431 }
432
433 static void
434 gtk_menu_shell_get_property (GObject     *object,
435                              guint        prop_id,
436                              GValue      *value,
437                              GParamSpec  *pspec)
438 {
439   GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
440
441   switch (prop_id)
442     {
443     case PROP_TAKE_FOCUS:
444       g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
445       break;
446     default:
447       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448       break;
449     }
450 }
451
452 static void
453 gtk_menu_shell_finalize (GObject *object)
454 {
455   GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
456   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
457
458   if (priv->mnemonic_hash)
459     _gtk_mnemonic_hash_free (priv->mnemonic_hash);
460   if (priv->key_hash)
461     _gtk_key_hash_free (priv->key_hash);
462
463   G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
464 }
465
466
467 void
468 gtk_menu_shell_append (GtkMenuShell *menu_shell,
469                        GtkWidget    *child)
470 {
471   gtk_menu_shell_insert (menu_shell, child, -1);
472 }
473
474 void
475 gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
476                         GtkWidget    *child)
477 {
478   gtk_menu_shell_insert (menu_shell, child, 0);
479 }
480
481 void
482 gtk_menu_shell_insert (GtkMenuShell *menu_shell,
483                        GtkWidget    *child,
484                        gint          position)
485 {
486   GtkMenuShellClass *class;
487
488   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
489   g_return_if_fail (GTK_IS_MENU_ITEM (child));
490
491   class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
492
493   if (class->insert)
494     class->insert (menu_shell, child, position);
495 }
496
497 static void
498 gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
499                             GtkWidget    *child,
500                             gint          position)
501 {
502   menu_shell->children = g_list_insert (menu_shell->children, child, position);
503
504   gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
505 }
506
507 void
508 gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
509 {
510   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
511
512   g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
513 }
514
515 static void
516 gtk_menu_shell_realize (GtkWidget *widget)
517 {
518   GdkWindowAttr attributes;
519   gint attributes_mask;
520
521   gtk_widget_set_realized (widget, TRUE);
522
523   attributes.x = widget->allocation.x;
524   attributes.y = widget->allocation.y;
525   attributes.width = widget->allocation.width;
526   attributes.height = widget->allocation.height;
527   attributes.window_type = GDK_WINDOW_CHILD;
528   attributes.wclass = GDK_INPUT_OUTPUT;
529   attributes.visual = gtk_widget_get_visual (widget);
530   attributes.colormap = gtk_widget_get_colormap (widget);
531   attributes.event_mask = gtk_widget_get_events (widget);
532   attributes.event_mask |= (GDK_EXPOSURE_MASK |
533                             GDK_BUTTON_PRESS_MASK |
534                             GDK_BUTTON_RELEASE_MASK |
535                             GDK_KEY_PRESS_MASK |
536                             GDK_ENTER_NOTIFY_MASK |
537                             GDK_LEAVE_NOTIFY_MASK);
538
539   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
540   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
541   gdk_window_set_user_data (widget->window, widget);
542
543   widget->style = gtk_style_attach (widget->style, widget->window);
544   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
545 }
546
547 void
548 _gtk_menu_shell_activate (GtkMenuShell *menu_shell)
549 {
550   if (!menu_shell->active)
551     {
552       GdkDevice *device;
553
554       device = gtk_get_current_event_device ();
555
556       _gtk_menu_shell_set_grab_device (menu_shell, device);
557       gtk_device_grab_add (GTK_WIDGET (menu_shell), device, TRUE);
558
559       menu_shell->have_grab = TRUE;
560       menu_shell->active = TRUE;
561     }
562 }
563
564 static gint
565 gtk_menu_shell_button_press (GtkWidget      *widget,
566                              GdkEventButton *event)
567 {
568   GtkMenuShell *menu_shell;
569   GtkWidget *menu_item;
570
571   if (event->type != GDK_BUTTON_PRESS)
572     return FALSE;
573
574   menu_shell = GTK_MENU_SHELL (widget);
575
576   if (menu_shell->parent_menu_shell)
577     return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
578
579   menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);
580
581   if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
582       menu_item != GTK_MENU_SHELL (menu_item->parent)->active_menu_item)
583     {
584       /*  select the menu item *before* activating the shell, so submenus
585        *  which might be open are closed the friendly way. If we activate
586        *  (and thus grab) this menu shell first, we might get grab_broken
587        *  events which will close the entire menu hierarchy. Selecting the
588        *  menu item also fixes up the state as if enter_notify() would
589        *  have run before (which normally selects the item).
590        */
591       if (GTK_MENU_SHELL_GET_CLASS (menu_item->parent)->submenu_placement != GTK_TOP_BOTTOM)
592         {
593           gtk_menu_shell_select_item (GTK_MENU_SHELL (menu_item->parent), menu_item);
594         }
595     }
596
597   if (!menu_shell->active || !menu_shell->button)
598     {
599       _gtk_menu_shell_activate (menu_shell);
600
601       menu_shell->button = event->button;
602
603       if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
604           menu_item->parent == widget &&
605           menu_item != menu_shell->active_menu_item)
606         {
607           if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
608             {
609               menu_shell->activate_time = event->time;
610               gtk_menu_shell_select_item (menu_shell, menu_item);
611             }
612         }
613     }
614   else
615     {
616       widget = gtk_get_event_widget ((GdkEvent*) event);
617       if (widget == GTK_WIDGET (menu_shell))
618         {
619           gtk_menu_shell_deactivate (menu_shell);
620           g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
621         }
622     }
623
624   if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
625       GTK_MENU_ITEM (menu_item)->submenu != NULL &&
626       !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
627     {
628       GtkMenuShellPrivate *priv;
629
630       _gtk_menu_item_popup_submenu (menu_item, FALSE);
631
632       priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
633       priv->activated_submenu = TRUE;
634     }
635
636   return TRUE;
637 }
638
639 static gboolean
640 gtk_menu_shell_grab_broken (GtkWidget          *widget,
641                             GdkEventGrabBroken *event)
642 {
643   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
644
645   if (menu_shell->have_xgrab && event->grab_window == NULL)
646     {
647       /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
648        */
649       
650       gtk_menu_shell_deselect (menu_shell);
651       
652       gtk_menu_shell_deactivate (menu_shell);
653       g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
654     }
655
656   return TRUE;
657 }
658
659 static gint
660 gtk_menu_shell_button_release (GtkWidget      *widget,
661                                GdkEventButton *event)
662 {
663   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
664   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (widget);
665
666   if (menu_shell->active)
667     {
668       GtkWidget *menu_item;
669       gboolean   deactivate = TRUE;
670
671       if (menu_shell->button && (event->button != menu_shell->button))
672         {
673           menu_shell->button = 0;
674           if (menu_shell->parent_menu_shell)
675             return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
676         }
677
678       menu_shell->button = 0;
679       menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
680
681       if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
682         {
683           if (menu_item && (menu_shell->active_menu_item == menu_item) &&
684               _gtk_menu_item_is_selectable (menu_item))
685             {
686               GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->submenu;
687
688               if (submenu == NULL)
689                 {
690                   gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
691
692                   deactivate = FALSE;
693                 }
694               else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
695                        priv->activated_submenu)
696                 {
697                   gint popdown_delay;
698                   GTimeVal *popup_time;
699                   gint64 usec_since_popup = 0;
700
701                   g_object_get (gtk_widget_get_settings (widget),
702                                 "gtk-menu-popdown-delay", &popdown_delay,
703                                 NULL);
704
705                   popup_time = g_object_get_data (G_OBJECT (submenu),
706                                                   "gtk-menu-exact-popup-time");
707
708                   if (popup_time)
709                     {
710                       GTimeVal current_time;
711
712                       g_get_current_time (&current_time);
713
714                       usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
715                                           (gint64) current_time.tv_usec -
716                                           (gint64) popup_time->tv_sec * 1000 * 1000 -
717                                           (gint64) popup_time->tv_usec);
718
719                       g_object_set_data (G_OBJECT (submenu),
720                                          "gtk-menu-exact-popup-time", NULL);
721                     }
722
723                   /*  only close the submenu on click if we opened the
724                    *  menu explicitely (usec_since_popup == 0) or
725                    *  enough time has passed since it was opened by
726                    *  GtkMenuItem's timeout (usec_since_popup > delay).
727                    */
728                   if (!priv->activated_submenu &&
729                       (usec_since_popup == 0 ||
730                        usec_since_popup > popdown_delay * 1000))
731                     {
732                       _gtk_menu_item_popdown_submenu (menu_item);
733                     }
734                   else
735                     {
736                       gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
737                     }
738
739                   deactivate = FALSE;
740                 }
741             }
742           else if (menu_item &&
743                    !_gtk_menu_item_is_selectable (menu_item) &&
744                    GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
745             {
746               deactivate = FALSE;
747             }
748           else if (menu_shell->parent_menu_shell)
749             {
750               menu_shell->active = TRUE;
751               gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
752               deactivate = FALSE;
753             }
754
755           /* If we ended up on an item with a submenu, leave the menu up.
756            */
757           if (menu_item && (menu_shell->active_menu_item == menu_item) &&
758               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
759             {
760               deactivate = FALSE;
761             }
762         }
763       else /* a very fast press-release */
764         {
765           /* We only ever want to prevent deactivation on the first
766            * press/release. Setting the time to zero is a bit of a
767            * hack, since we could be being triggered in the first
768            * few fractions of a second after a server time wraparound.
769            * the chances of that happening are ~1/10^6, without
770            * serious harm if we lose.
771            */
772           menu_shell->activate_time = 0;
773           deactivate = FALSE;
774         }
775
776       if (deactivate)
777         {
778           gtk_menu_shell_deactivate (menu_shell);
779           g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
780         }
781
782       priv->activated_submenu = FALSE;
783     }
784
785   return TRUE;
786 }
787
788 void
789 _gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
790                                    gboolean      keyboard_mode)
791 {
792   menu_shell->keyboard_mode = keyboard_mode;
793 }
794
795 gboolean
796 _gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
797 {
798   return menu_shell->keyboard_mode;
799 }
800
801 void
802 _gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
803 {
804   GtkMenuShell *target;
805   gboolean auto_mnemonics;
806   gboolean found;
807   gboolean mnemonics_visible;
808
809   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
810                 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
811
812   if (!auto_mnemonics)
813     return;
814
815   target = menu_shell;
816   found = FALSE;
817   while (target)
818     {
819       GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (target);
820       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));
821
822       /* The idea with keyboard mode is that once you start using
823        * the keyboard to navigate the menus, we show mnemonics
824        * until the menu navigation is over. To that end, we spread
825        * the keyboard mode upwards in the menu hierarchy here.
826        * Also see gtk_menu_popup, where we inherit it downwards.
827        */
828       if (menu_shell->keyboard_mode)
829         target->keyboard_mode = TRUE;
830
831       /* While navigating menus, the first parent menu with an active
832        * item is the one where mnemonics are effective, as can be seen
833        * in gtk_menu_shell_key_press below.
834        * We also show mnemonics in context menus. The grab condition is
835        * necessary to ensure we remove underlines from menu bars when
836        * dismissing menus.
837        */
838       mnemonics_visible = target->keyboard_mode &&
839                           (((target->active_menu_item || priv->in_unselectable_item) && !found) ||
840                            (target == menu_shell &&
841                             !target->parent_menu_shell &&
842                             gtk_widget_has_grab (GTK_WIDGET (target))));
843
844       /* While menus are up, only show underlines inside the menubar,
845        * not in the entire window.
846        */
847       if (GTK_IS_MENU_BAR (target))
848         {
849           gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
850           _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
851                                                           mnemonics_visible);
852         }
853       else
854         gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
855
856       if (target->active_menu_item || priv->in_unselectable_item)
857         found = TRUE;
858
859       target = GTK_MENU_SHELL (target->parent_menu_shell);
860     }
861 }
862
863 static gint
864 gtk_menu_shell_key_press (GtkWidget   *widget,
865                           GdkEventKey *event)
866 {
867   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
868   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
869   gboolean enable_mnemonics;
870
871   menu_shell->keyboard_mode = TRUE;
872
873   if (!(menu_shell->active_menu_item || priv->in_unselectable_item) && menu_shell->parent_menu_shell)
874     return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
875
876   if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
877     return TRUE;
878
879   g_object_get (gtk_widget_get_settings (widget),
880                 "gtk-enable-mnemonics", &enable_mnemonics,
881                 NULL);
882
883   if (enable_mnemonics)
884     return gtk_menu_shell_activate_mnemonic (menu_shell, event);
885
886   return FALSE;
887 }
888
889 static gint
890 gtk_menu_shell_enter_notify (GtkWidget        *widget,
891                              GdkEventCrossing *event)
892 {
893   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
894
895   if (event->mode == GDK_CROSSING_GTK_GRAB ||
896       event->mode == GDK_CROSSING_GTK_UNGRAB ||
897       event->mode == GDK_CROSSING_STATE_CHANGED)
898     return TRUE;
899
900   if (menu_shell->active)
901     {
902       GtkWidget *menu_item;
903
904       menu_item = gtk_get_event_widget ((GdkEvent*) event);
905
906       if (!menu_item)
907         return TRUE;
908
909       if (GTK_IS_MENU_ITEM (menu_item) &&
910           !_gtk_menu_item_is_selectable (menu_item))
911         {
912           GtkMenuShellPrivate *priv;
913
914           priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
915           priv->in_unselectable_item = TRUE;
916
917           return TRUE;
918         }
919
920       if (menu_item->parent == widget &&
921           GTK_IS_MENU_ITEM (menu_item))
922         {
923           if (menu_shell->ignore_enter)
924             return TRUE;
925
926           if (event->detail != GDK_NOTIFY_INFERIOR)
927             {
928               if (gtk_widget_get_state (menu_item) != GTK_STATE_PRELIGHT)
929                 gtk_menu_shell_select_item (menu_shell, menu_item);
930
931               /* If any mouse button is down, and there is a submenu
932                * that is not yet visible, activate it. It's sufficient
933                * to check for any button's mask (not only the one
934                * matching menu_shell->button), because there is no
935                * situation a mouse button could be pressed while
936                * entering a menu item where we wouldn't want to show
937                * its submenu.
938                */
939               if ((event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) &&
940                   GTK_MENU_ITEM (menu_item)->submenu != NULL)
941                 {
942                   GtkMenuShellPrivate *priv;
943
944                   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
945                   priv->activated_submenu = TRUE;
946
947                   if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
948                     {
949                       gboolean touchscreen_mode;
950
951                       g_object_get (gtk_widget_get_settings (widget),
952                                     "gtk-touchscreen-mode", &touchscreen_mode,
953                                     NULL);
954
955                       if (touchscreen_mode)
956                         _gtk_menu_item_popup_submenu (menu_item, TRUE);
957                     }
958                 }
959             }
960         }
961       else if (menu_shell->parent_menu_shell)
962         {
963           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
964         }
965     }
966
967   return TRUE;
968 }
969
970 static gint
971 gtk_menu_shell_leave_notify (GtkWidget        *widget,
972                              GdkEventCrossing *event)
973 {
974   if (event->mode == GDK_CROSSING_GTK_GRAB ||
975       event->mode == GDK_CROSSING_GTK_GRAB ||
976       event->mode == GDK_CROSSING_STATE_CHANGED)
977     return TRUE;
978
979   if (gtk_widget_get_visible (widget))
980     {
981       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
982       GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
983       GtkMenuItem *menu_item;
984
985       if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
986         return TRUE;
987
988       menu_item = GTK_MENU_ITEM (event_widget);
989
990       if (!_gtk_menu_item_is_selectable (event_widget))
991         {
992           GtkMenuShellPrivate *priv;
993
994           priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
995           priv->in_unselectable_item = TRUE;
996
997           return TRUE;
998         }
999
1000       if ((menu_shell->active_menu_item == event_widget) &&
1001           (menu_item->submenu == NULL))
1002         {
1003           if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1004               (gtk_widget_get_state (GTK_WIDGET (menu_item)) != GTK_STATE_NORMAL))
1005             {
1006               gtk_menu_shell_deselect (menu_shell);
1007             }
1008         }
1009       else if (menu_shell->parent_menu_shell)
1010         {
1011           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
1012         }
1013     }
1014
1015   return TRUE;
1016 }
1017
1018 static void
1019 gtk_menu_shell_screen_changed (GtkWidget *widget,
1020                                GdkScreen *previous_screen)
1021 {
1022   gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
1023 }
1024
1025 static void
1026 gtk_menu_shell_add (GtkContainer *container,
1027                     GtkWidget    *widget)
1028 {
1029   gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
1030 }
1031
1032 static void
1033 gtk_menu_shell_remove (GtkContainer *container,
1034                        GtkWidget    *widget)
1035 {
1036   GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1037   gint was_visible;
1038
1039   was_visible = gtk_widget_get_visible (widget);
1040   menu_shell->children = g_list_remove (menu_shell->children, widget);
1041   
1042   if (widget == menu_shell->active_menu_item)
1043     {
1044       gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
1045       menu_shell->active_menu_item = NULL;
1046     }
1047
1048   gtk_widget_unparent (widget);
1049   
1050   /* queue resize regardless of gtk_widget_get_visible (container),
1051    * since that's what is needed by toplevels.
1052    */
1053   if (was_visible)
1054     gtk_widget_queue_resize (GTK_WIDGET (container));
1055 }
1056
1057 static void
1058 gtk_menu_shell_forall (GtkContainer *container,
1059                        gboolean      include_internals,
1060                        GtkCallback   callback,
1061                        gpointer      callback_data)
1062 {
1063   GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1064   GtkWidget *child;
1065   GList *children;
1066
1067   children = menu_shell->children;
1068   while (children)
1069     {
1070       child = children->data;
1071       children = children->next;
1072
1073       (* callback) (child, callback_data);
1074     }
1075 }
1076
1077
1078 static void
1079 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
1080 {
1081   if (menu_shell->active)
1082     {
1083       GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1084
1085       menu_shell->button = 0;
1086       menu_shell->active = FALSE;
1087       menu_shell->activate_time = 0;
1088
1089       if (menu_shell->active_menu_item)
1090         {
1091           gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1092           menu_shell->active_menu_item = NULL;
1093         }
1094
1095       if (menu_shell->have_grab)
1096         {
1097           menu_shell->have_grab = FALSE;
1098           gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1099         }
1100       if (menu_shell->have_xgrab)
1101         {
1102           GdkDevice *keyboard;
1103
1104           gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1105           keyboard = gdk_device_get_associated_device (priv->grab_pointer);
1106
1107           if (keyboard)
1108             gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1109
1110           menu_shell->have_xgrab = FALSE;
1111         }
1112
1113       menu_shell->keyboard_mode = FALSE;
1114       _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1115
1116       _gtk_menu_shell_update_mnemonics (menu_shell);
1117     }
1118 }
1119
1120 static gint
1121 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1122                         GtkWidget    *child)
1123 {
1124   GtkWidget *parent;
1125
1126   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1127   g_return_val_if_fail (child != NULL, FALSE);
1128
1129   parent = child->parent;
1130   while (GTK_IS_MENU_SHELL (parent))
1131     {
1132       if (parent == (GtkWidget*) menu_shell)
1133         return TRUE;
1134       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1135     }
1136
1137   return FALSE;
1138 }
1139
1140 static GtkWidget*
1141 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1142                          GdkEvent     *event)
1143 {
1144   GtkWidget *menu_item;
1145
1146   menu_item = gtk_get_event_widget ((GdkEvent*) event);
1147   
1148   while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1149     menu_item = menu_item->parent;
1150
1151   if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1152     return menu_item;
1153   else
1154     return NULL;
1155 }
1156
1157 /* Handlers for action signals */
1158
1159 void
1160 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1161                             GtkWidget    *menu_item)
1162 {
1163   GtkMenuShellClass *class;
1164
1165   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1166   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1167
1168   class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1169
1170   if (class->select_item &&
1171       !(menu_shell->active &&
1172         menu_shell->active_menu_item == menu_item))
1173     class->select_item (menu_shell, menu_item);
1174 }
1175
1176 void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1177                                    GtkSubmenuPlacement  placement);
1178
1179 static void
1180 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1181                                  GtkWidget    *menu_item)
1182 {
1183   GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1184
1185   if (menu_shell->active_menu_item)
1186     {
1187       gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1188       menu_shell->active_menu_item = NULL;
1189     }
1190
1191   if (!_gtk_menu_item_is_selectable (menu_item))
1192     {
1193       GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1194
1195       priv->in_unselectable_item = TRUE;
1196       _gtk_menu_shell_update_mnemonics (menu_shell);
1197
1198       return;
1199     }
1200
1201   menu_shell->active_menu_item = menu_item;
1202   if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1203     _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1204                                   GTK_LEFT_RIGHT);
1205   else
1206     _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1207                                   GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1208   gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1209
1210   _gtk_menu_shell_update_mnemonics (menu_shell);
1211
1212   /* This allows the bizarre radio buttons-with-submenus-display-history
1213    * behavior
1214    */
1215   if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1216     gtk_widget_activate (menu_shell->active_menu_item);
1217 }
1218
1219 void
1220 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1221 {
1222   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1223
1224   if (menu_shell->active_menu_item)
1225     {
1226       gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1227       menu_shell->active_menu_item = NULL;
1228       _gtk_menu_shell_update_mnemonics (menu_shell);
1229     }
1230 }
1231
1232 void
1233 gtk_menu_shell_activate_item (GtkMenuShell      *menu_shell,
1234                               GtkWidget         *menu_item,
1235                               gboolean           force_deactivate)
1236 {
1237   GSList *slist, *shells = NULL;
1238   gboolean deactivate = force_deactivate;
1239
1240   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1241   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1242
1243   if (!deactivate)
1244     deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1245
1246   g_object_ref (menu_shell);
1247   g_object_ref (menu_item);
1248
1249   if (deactivate)
1250     {
1251       GtkMenuShell *parent_menu_shell = menu_shell;
1252
1253       do
1254         {
1255           g_object_ref (parent_menu_shell);
1256           shells = g_slist_prepend (shells, parent_menu_shell);
1257           parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1258         }
1259       while (parent_menu_shell);
1260       shells = g_slist_reverse (shells);
1261
1262       gtk_menu_shell_deactivate (menu_shell);
1263   
1264       /* flush the x-queue, so any grabs are removed and
1265        * the menu is actually taken down
1266        */
1267       gdk_display_sync (gtk_widget_get_display (menu_item));
1268     }
1269
1270   gtk_widget_activate (menu_item);
1271
1272   for (slist = shells; slist; slist = slist->next)
1273     {
1274       g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1275       g_object_unref (slist->data);
1276     }
1277   g_slist_free (shells);
1278
1279   g_object_unref (menu_shell);
1280   g_object_unref (menu_item);
1281 }
1282
1283 /* Distance should be +/- 1 */
1284 static gboolean
1285 gtk_menu_shell_real_move_selected (GtkMenuShell  *menu_shell, 
1286                                    gint           distance)
1287 {
1288   if (menu_shell->active_menu_item)
1289     {
1290       GList *node = g_list_find (menu_shell->children,
1291                                  menu_shell->active_menu_item);
1292       GList *start_node = node;
1293       gboolean wrap_around;
1294
1295       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1296                     "gtk-keynav-wrap-around", &wrap_around,
1297                     NULL);
1298
1299       if (distance > 0)
1300         {
1301           node = node->next;
1302           while (node != start_node && 
1303                  (!node || !_gtk_menu_item_is_selectable (node->data)))
1304             {
1305               if (node)
1306                 node = node->next;
1307               else if (wrap_around)
1308                 node = menu_shell->children;
1309               else
1310                 {
1311                   gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1312                   break;
1313                 }
1314             }
1315         }
1316       else
1317         {
1318           node = node->prev;
1319           while (node != start_node &&
1320                  (!node || !_gtk_menu_item_is_selectable (node->data)))
1321             {
1322               if (node)
1323                 node = node->prev;
1324               else if (wrap_around)
1325                 node = g_list_last (menu_shell->children);
1326               else
1327                 {
1328                   gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1329                   break;
1330                 }
1331             }
1332         }
1333       
1334       if (node)
1335         gtk_menu_shell_select_item (menu_shell, node->data);
1336     }
1337
1338   return TRUE;
1339 }
1340
1341 /* Distance should be +/- 1 */
1342 static void
1343 gtk_menu_shell_move_selected (GtkMenuShell  *menu_shell, 
1344                               gint           distance)
1345 {
1346   gboolean handled = FALSE;
1347
1348   g_signal_emit (menu_shell, menu_shell_signals[MOVE_SELECTED], 0,
1349                  distance, &handled);
1350 }
1351
1352 /**
1353  * gtk_menu_shell_select_first:
1354  * @menu_shell: a #GtkMenuShell
1355  * @search_sensitive: if %TRUE, search for the first selectable
1356  *                    menu item, otherwise select nothing if
1357  *                    the first item isn't sensitive. This
1358  *                    should be %FALSE if the menu is being
1359  *                    popped up initially.
1360  * 
1361  * Select the first visible or selectable child of the menu shell;
1362  * don't select tearoff items unless the only item is a tearoff
1363  * item.
1364  *
1365  * Since: 2.2
1366  **/
1367 void
1368 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1369                              gboolean      search_sensitive)
1370 {
1371   GtkWidget *to_select = NULL;
1372   GList *tmp_list;
1373
1374   tmp_list = menu_shell->children;
1375   while (tmp_list)
1376     {
1377       GtkWidget *child = tmp_list->data;
1378       
1379       if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1380           _gtk_menu_item_is_selectable (child))
1381         {
1382           to_select = child;
1383           if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1384             break;
1385         }
1386       
1387       tmp_list = tmp_list->next;
1388     }
1389
1390   if (to_select)
1391     gtk_menu_shell_select_item (menu_shell, to_select);
1392 }
1393
1394 void
1395 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1396                              gboolean      search_sensitive)
1397 {
1398   GtkWidget *to_select = NULL;
1399   GList *tmp_list;
1400
1401   tmp_list = g_list_last (menu_shell->children);
1402   while (tmp_list)
1403     {
1404       GtkWidget *child = tmp_list->data;
1405       
1406       if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1407           _gtk_menu_item_is_selectable (child))
1408         {
1409           to_select = child;
1410           if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1411             break;
1412         }
1413       
1414       tmp_list = tmp_list->prev;
1415     }
1416
1417   if (to_select)
1418     gtk_menu_shell_select_item (menu_shell, to_select);
1419 }
1420
1421 static gboolean
1422 gtk_menu_shell_select_submenu_first (GtkMenuShell     *menu_shell)
1423 {
1424   GtkMenuItem *menu_item;
1425
1426   if (menu_shell->active_menu_item == NULL)
1427     return FALSE;
1428
1429   menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item); 
1430   
1431   if (menu_item->submenu)
1432     {
1433       _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1434       gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1435       if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1436         return TRUE;
1437     }
1438
1439   return FALSE;
1440 }
1441
1442 static void
1443 gtk_real_menu_shell_move_current (GtkMenuShell         *menu_shell,
1444                                   GtkMenuDirectionType  direction)
1445 {
1446   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1447   GtkMenuShell *parent_menu_shell = NULL;
1448   gboolean had_selection;
1449   gboolean touchscreen_mode;
1450
1451   priv->in_unselectable_item = FALSE;
1452
1453   had_selection = menu_shell->active_menu_item != NULL;
1454
1455   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1456                 "gtk-touchscreen-mode", &touchscreen_mode,
1457                 NULL);
1458
1459   if (menu_shell->parent_menu_shell)
1460     parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1461
1462   switch (direction)
1463     {
1464     case GTK_MENU_DIR_PARENT:
1465       if (touchscreen_mode &&
1466           menu_shell->active_menu_item &&
1467           GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
1468           gtk_widget_get_visible (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu))
1469         {
1470           /* if we are on a menu item that has an open submenu but the
1471            * focus is not in that submenu (e.g. because it's empty or
1472            * has only insensitive items), close that submenu instead
1473            * of running into the code below which would close *this*
1474            * menu.
1475            */
1476           _gtk_menu_item_popdown_submenu (menu_shell->active_menu_item);
1477           _gtk_menu_shell_update_mnemonics (menu_shell);
1478         }
1479       else if (parent_menu_shell)
1480         {
1481           if (touchscreen_mode)
1482             {
1483               /* close menu when returning from submenu. */
1484               _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->parent_menu_item);
1485               _gtk_menu_shell_update_mnemonics (parent_menu_shell);
1486               break;
1487             }
1488
1489           if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1490               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1491             gtk_menu_shell_deselect (menu_shell);
1492           else
1493             {
1494               if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1495                 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1496               else
1497                 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1498               gtk_menu_shell_select_submenu_first (parent_menu_shell);
1499             }
1500         }
1501       /* If there is no parent and the submenu is in the opposite direction
1502        * to the menu, then make the PARENT direction wrap around to
1503        * the bottom of the submenu.
1504        */
1505       else if (menu_shell->active_menu_item &&
1506                _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1507                GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1508         {
1509           GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1510
1511           if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1512               GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1513             _gtk_menu_shell_select_last (submenu, TRUE);
1514         }
1515       break;
1516
1517     case GTK_MENU_DIR_CHILD:
1518       if (menu_shell->active_menu_item &&
1519           _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1520           GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1521         {
1522           if (gtk_menu_shell_select_submenu_first (menu_shell))
1523             break;
1524         }
1525
1526       /* Try to find a menu running the opposite direction */
1527       while (parent_menu_shell &&
1528              (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1529               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1530         {
1531           parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->parent_menu_shell);
1532         }
1533
1534       if (parent_menu_shell)
1535         {
1536           if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1537             gtk_menu_shell_move_selected (parent_menu_shell, 1);
1538           else
1539             gtk_menu_shell_move_selected (parent_menu_shell, -1);
1540
1541           gtk_menu_shell_select_submenu_first (parent_menu_shell);
1542         }
1543       break;
1544
1545     case GTK_MENU_DIR_PREV:
1546       gtk_menu_shell_move_selected (menu_shell, -1);
1547       if (!had_selection &&
1548           !menu_shell->active_menu_item &&
1549           menu_shell->children)
1550         _gtk_menu_shell_select_last (menu_shell, TRUE);
1551       break;
1552
1553     case GTK_MENU_DIR_NEXT:
1554       gtk_menu_shell_move_selected (menu_shell, 1);
1555       if (!had_selection &&
1556           !menu_shell->active_menu_item &&
1557           menu_shell->children)
1558         gtk_menu_shell_select_first (menu_shell, TRUE);
1559       break;
1560     }
1561 }
1562
1563 static void
1564 gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
1565                                       gboolean           force_hide)
1566 {
1567   if (menu_shell->active_menu_item &&
1568       _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1569   {
1570     if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1571       gtk_menu_shell_activate_item (menu_shell,
1572                                     menu_shell->active_menu_item,
1573                                     force_hide);
1574     else
1575       _gtk_menu_item_popup_submenu (menu_shell->active_menu_item, FALSE);
1576   }
1577 }
1578
1579 static void
1580 gtk_real_menu_shell_cancel (GtkMenuShell      *menu_shell)
1581 {
1582   /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1583    */
1584   gtk_menu_shell_deselect (menu_shell);
1585   
1586   gtk_menu_shell_deactivate (menu_shell);
1587   g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1588 }
1589
1590 static void
1591 gtk_real_menu_shell_cycle_focus (GtkMenuShell      *menu_shell,
1592                                  GtkDirectionType   dir)
1593 {
1594   while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1595     {
1596       if (menu_shell->parent_menu_shell)
1597         menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1598       else
1599         menu_shell = NULL;
1600     }
1601
1602   if (menu_shell)
1603     _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1604 }
1605
1606 gint
1607 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1608 {
1609   GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1610   
1611   if (klass->get_popup_delay)
1612     {
1613       return klass->get_popup_delay (menu_shell);
1614     }
1615   else
1616     {
1617       gint popup_delay;
1618       GtkWidget *widget = GTK_WIDGET (menu_shell);
1619       
1620       g_object_get (gtk_widget_get_settings (widget),
1621                     "gtk-menu-popup-delay", &popup_delay,
1622                     NULL);
1623       
1624       return popup_delay;
1625     }
1626 }
1627
1628 /**
1629  * gtk_menu_shell_cancel:
1630  * @menu_shell: a #GtkMenuShell
1631  * 
1632  * Cancels the selection within the menu shell.  
1633  * 
1634  * Since: 2.4
1635  */
1636 void
1637 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1638 {
1639   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1640
1641   g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1642 }
1643
1644 static GtkMnemonicHash *
1645 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1646                                   gboolean      create)
1647 {
1648   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1649
1650   if (!private->mnemonic_hash && create)
1651     private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1652   
1653   return private->mnemonic_hash;
1654 }
1655
1656 static void
1657 menu_shell_add_mnemonic_foreach (guint    keyval,
1658                                  GSList  *targets,
1659                                  gpointer data)
1660 {
1661   GtkKeyHash *key_hash = data;
1662
1663   _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1664 }
1665
1666 static GtkKeyHash *
1667 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1668                              gboolean      create)
1669 {
1670   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1671   GtkWidget *widget = GTK_WIDGET (menu_shell);
1672
1673   if (!private->key_hash && create && gtk_widget_has_screen (widget))
1674     {
1675       GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1676       GdkScreen *screen = gtk_widget_get_screen (widget);
1677       GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1678
1679       if (!mnemonic_hash)
1680         return NULL;
1681       
1682       private->key_hash = _gtk_key_hash_new (keymap, NULL);
1683
1684       _gtk_mnemonic_hash_foreach (mnemonic_hash,
1685                                   menu_shell_add_mnemonic_foreach,
1686                                   private->key_hash);
1687     }
1688   
1689   return private->key_hash;
1690 }
1691
1692 static void
1693 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1694 {
1695   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1696
1697   if (private->key_hash)
1698     {
1699       _gtk_key_hash_free (private->key_hash);
1700       private->key_hash = NULL;
1701     }
1702 }
1703
1704 static gboolean
1705 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1706                                   GdkEventKey  *event)
1707 {
1708   GtkMnemonicHash *mnemonic_hash;
1709   GtkKeyHash *key_hash;
1710   GSList *entries;
1711   gboolean result = FALSE;
1712
1713   mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1714   if (!mnemonic_hash)
1715     return FALSE;
1716
1717   key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1718   if (!key_hash)
1719     return FALSE;
1720   
1721   entries = _gtk_key_hash_lookup (key_hash,
1722                                   event->hardware_keycode,
1723                                   event->state,
1724                                   gtk_accelerator_get_default_mod_mask (),
1725                                   event->group);
1726
1727   if (entries)
1728     result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1729                                           GPOINTER_TO_UINT (entries->data));
1730
1731   return result;
1732 }
1733
1734 void
1735 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1736                               guint      keyval,
1737                               GtkWidget *target)
1738 {
1739   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1740   g_return_if_fail (GTK_IS_WIDGET (target));
1741
1742   _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1743                           keyval, target);
1744   gtk_menu_shell_reset_key_hash (menu_shell);
1745 }
1746
1747 void
1748 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1749                                  guint      keyval,
1750                                  GtkWidget *target)
1751 {
1752   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1753   g_return_if_fail (GTK_IS_WIDGET (target));
1754   
1755   _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1756                              keyval, target);
1757   gtk_menu_shell_reset_key_hash (menu_shell);
1758 }
1759
1760 void
1761 _gtk_menu_shell_set_grab_device (GtkMenuShell *menu_shell,
1762                                  GdkDevice    *device)
1763 {
1764   GtkMenuShellPrivate *priv;
1765
1766   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1767   g_return_if_fail (!device || GDK_IS_DEVICE (device));
1768
1769   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1770
1771   if (!device)
1772     priv->grab_pointer = NULL;
1773   else if (device->source == GDK_SOURCE_KEYBOARD)
1774     priv->grab_pointer = gdk_device_get_associated_device (device);
1775   else
1776     priv->grab_pointer = device;
1777 }
1778
1779 GdkDevice *
1780 _gtk_menu_shell_get_grab_device (GtkMenuShell  *menu_shell)
1781 {
1782   GtkMenuShellPrivate *priv;
1783
1784   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1785
1786   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1787
1788   return priv->grab_pointer;
1789 }
1790
1791 /**
1792  * gtk_menu_shell_get_take_focus:
1793  * @menu_shell: a #GtkMenuShell
1794  *
1795  * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1796  *
1797  * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1798  *
1799  * Since: 2.8
1800  **/
1801 gboolean
1802 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1803 {
1804   GtkMenuShellPrivate *priv;
1805
1806   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1807
1808   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1809
1810   return priv->take_focus;
1811 }
1812
1813 /**
1814  * gtk_menu_shell_set_take_focus:
1815  * @menu_shell: a #GtkMenuShell
1816  * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1817  *
1818  * If @take_focus is %TRUE (the default) the menu shell will take the keyboard 
1819  * focus so that it will receive all keyboard events which is needed to enable
1820  * keyboard navigation in menus.
1821  *
1822  * Setting @take_focus to %FALSE is useful only for special applications
1823  * like virtual keyboard implementations which should not take keyboard
1824  * focus.
1825  *
1826  * The @take_focus state of a menu or menu bar is automatically propagated
1827  * to submenus whenever a submenu is popped up, so you don't have to worry
1828  * about recursively setting it for your entire menu hierarchy. Only when
1829  * programmatically picking a submenu and popping it up manually, the
1830  * @take_focus property of the submenu needs to be set explicitely.
1831  *
1832  * Note that setting it to %FALSE has side-effects:
1833  *
1834  * If the focus is in some other app, it keeps the focus and keynav in
1835  * the menu doesn't work. Consequently, keynav on the menu will only
1836  * work if the focus is on some toplevel owned by the onscreen keyboard.
1837  *
1838  * To avoid confusing the user, menus with @take_focus set to %FALSE
1839  * should not display mnemonics or accelerators, since it cannot be
1840  * guaranteed that they will work.
1841  *
1842  * See also gdk_keyboard_grab()
1843  *
1844  * Since: 2.8
1845  **/
1846 void
1847 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1848                                gboolean      take_focus)
1849 {
1850   GtkMenuShellPrivate *priv;
1851
1852   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1853
1854   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1855
1856   if (priv->take_focus != take_focus)
1857     {
1858       priv->take_focus = take_focus;
1859       g_object_notify (G_OBJECT (menu_shell), "take-focus");
1860     }
1861 }