]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenushell.c
Allow windows to be dragged by clicking on empty areas
[~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       gboolean initially_active = menu_shell->active;
600
601       menu_shell->button = event->button;
602
603       if (menu_item)
604         {
605           if (_gtk_menu_item_is_selectable (menu_item) &&
606               menu_item->parent == widget &&
607               menu_item != menu_shell->active_menu_item)
608             {
609               _gtk_menu_shell_activate (menu_shell);
610               menu_shell->button = event->button;
611
612               if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
613                 {
614                   menu_shell->activate_time = event->time;
615                   gtk_menu_shell_select_item (menu_shell, menu_item);
616                 }
617             }
618         }
619       else
620         {
621           if (!initially_active)
622             {
623               gboolean window_drag = FALSE;
624
625               gtk_widget_style_get (widget,
626                                     "window-dragging", &window_drag,
627                                     NULL);
628
629               if (window_drag)
630                 {
631                   gtk_menu_shell_deactivate (menu_shell);
632                   gtk_window_begin_move_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
633                                               event->button,
634                                               event->x_root,
635                                               event->y_root,
636                                               event->time);
637                 }
638             }
639         }
640     }
641   else
642     {
643       widget = gtk_get_event_widget ((GdkEvent*) event);
644       if (widget == GTK_WIDGET (menu_shell))
645         {
646           gtk_menu_shell_deactivate (menu_shell);
647           g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
648         }
649     }
650
651   if (menu_item && _gtk_menu_item_is_selectable (menu_item) &&
652       GTK_MENU_ITEM (menu_item)->submenu != NULL &&
653       !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
654     {
655       GtkMenuShellPrivate *priv;
656
657       _gtk_menu_item_popup_submenu (menu_item, FALSE);
658
659       priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
660       priv->activated_submenu = TRUE;
661     }
662
663   return TRUE;
664 }
665
666 static gboolean
667 gtk_menu_shell_grab_broken (GtkWidget          *widget,
668                             GdkEventGrabBroken *event)
669 {
670   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
671
672   if (menu_shell->have_xgrab && event->grab_window == NULL)
673     {
674       /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
675        */
676       
677       gtk_menu_shell_deselect (menu_shell);
678       
679       gtk_menu_shell_deactivate (menu_shell);
680       g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
681     }
682
683   return TRUE;
684 }
685
686 static gint
687 gtk_menu_shell_button_release (GtkWidget      *widget,
688                                GdkEventButton *event)
689 {
690   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
691   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (widget);
692
693   if (menu_shell->active)
694     {
695       GtkWidget *menu_item;
696       gboolean   deactivate = TRUE;
697
698       if (menu_shell->button && (event->button != menu_shell->button))
699         {
700           menu_shell->button = 0;
701           if (menu_shell->parent_menu_shell)
702             return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
703         }
704
705       menu_shell->button = 0;
706       menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
707
708       if ((event->time - menu_shell->activate_time) > MENU_SHELL_TIMEOUT)
709         {
710           if (menu_item && (menu_shell->active_menu_item == menu_item) &&
711               _gtk_menu_item_is_selectable (menu_item))
712             {
713               GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->submenu;
714
715               if (submenu == NULL)
716                 {
717                   gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
718
719                   deactivate = FALSE;
720                 }
721               else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
722                        priv->activated_submenu)
723                 {
724                   gint popdown_delay;
725                   GTimeVal *popup_time;
726                   gint64 usec_since_popup = 0;
727
728                   g_object_get (gtk_widget_get_settings (widget),
729                                 "gtk-menu-popdown-delay", &popdown_delay,
730                                 NULL);
731
732                   popup_time = g_object_get_data (G_OBJECT (submenu),
733                                                   "gtk-menu-exact-popup-time");
734
735                   if (popup_time)
736                     {
737                       GTimeVal current_time;
738
739                       g_get_current_time (&current_time);
740
741                       usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
742                                           (gint64) current_time.tv_usec -
743                                           (gint64) popup_time->tv_sec * 1000 * 1000 -
744                                           (gint64) popup_time->tv_usec);
745
746                       g_object_set_data (G_OBJECT (submenu),
747                                          "gtk-menu-exact-popup-time", NULL);
748                     }
749
750                   /*  only close the submenu on click if we opened the
751                    *  menu explicitely (usec_since_popup == 0) or
752                    *  enough time has passed since it was opened by
753                    *  GtkMenuItem's timeout (usec_since_popup > delay).
754                    */
755                   if (!priv->activated_submenu &&
756                       (usec_since_popup == 0 ||
757                        usec_since_popup > popdown_delay * 1000))
758                     {
759                       _gtk_menu_item_popdown_submenu (menu_item);
760                     }
761                   else
762                     {
763                       gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
764                     }
765
766                   deactivate = FALSE;
767                 }
768             }
769           else if (menu_item &&
770                    !_gtk_menu_item_is_selectable (menu_item) &&
771                    GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
772             {
773               deactivate = FALSE;
774             }
775           else if (menu_shell->parent_menu_shell)
776             {
777               menu_shell->active = TRUE;
778               gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
779               deactivate = FALSE;
780             }
781
782           /* If we ended up on an item with a submenu, leave the menu up.
783            */
784           if (menu_item && (menu_shell->active_menu_item == menu_item) &&
785               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
786             {
787               deactivate = FALSE;
788             }
789         }
790       else /* a very fast press-release */
791         {
792           /* We only ever want to prevent deactivation on the first
793            * press/release. Setting the time to zero is a bit of a
794            * hack, since we could be being triggered in the first
795            * few fractions of a second after a server time wraparound.
796            * the chances of that happening are ~1/10^6, without
797            * serious harm if we lose.
798            */
799           menu_shell->activate_time = 0;
800           deactivate = FALSE;
801         }
802
803       if (deactivate)
804         {
805           gtk_menu_shell_deactivate (menu_shell);
806           g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
807         }
808
809       priv->activated_submenu = FALSE;
810     }
811
812   return TRUE;
813 }
814
815 void
816 _gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
817                                    gboolean      keyboard_mode)
818 {
819   menu_shell->keyboard_mode = keyboard_mode;
820 }
821
822 gboolean
823 _gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
824 {
825   return menu_shell->keyboard_mode;
826 }
827
828 void
829 _gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
830 {
831   GtkMenuShell *target;
832   gboolean auto_mnemonics;
833   gboolean found;
834   gboolean mnemonics_visible;
835
836   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
837                 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
838
839   if (!auto_mnemonics)
840     return;
841
842   target = menu_shell;
843   found = FALSE;
844   while (target)
845     {
846       GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (target);
847       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));
848
849       /* The idea with keyboard mode is that once you start using
850        * the keyboard to navigate the menus, we show mnemonics
851        * until the menu navigation is over. To that end, we spread
852        * the keyboard mode upwards in the menu hierarchy here.
853        * Also see gtk_menu_popup, where we inherit it downwards.
854        */
855       if (menu_shell->keyboard_mode)
856         target->keyboard_mode = TRUE;
857
858       /* While navigating menus, the first parent menu with an active
859        * item is the one where mnemonics are effective, as can be seen
860        * in gtk_menu_shell_key_press below.
861        * We also show mnemonics in context menus. The grab condition is
862        * necessary to ensure we remove underlines from menu bars when
863        * dismissing menus.
864        */
865       mnemonics_visible = target->keyboard_mode &&
866                           (((target->active_menu_item || priv->in_unselectable_item) && !found) ||
867                            (target == menu_shell &&
868                             !target->parent_menu_shell &&
869                             gtk_widget_has_grab (GTK_WIDGET (target))));
870
871       /* While menus are up, only show underlines inside the menubar,
872        * not in the entire window.
873        */
874       if (GTK_IS_MENU_BAR (target))
875         {
876           gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
877           _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
878                                                           mnemonics_visible);
879         }
880       else
881         gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
882
883       if (target->active_menu_item || priv->in_unselectable_item)
884         found = TRUE;
885
886       target = GTK_MENU_SHELL (target->parent_menu_shell);
887     }
888 }
889
890 static gint
891 gtk_menu_shell_key_press (GtkWidget   *widget,
892                           GdkEventKey *event)
893 {
894   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
895   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
896   gboolean enable_mnemonics;
897
898   menu_shell->keyboard_mode = TRUE;
899
900   if (!(menu_shell->active_menu_item || priv->in_unselectable_item) && menu_shell->parent_menu_shell)
901     return gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent *)event);
902
903   if (gtk_bindings_activate_event (GTK_OBJECT (widget), event))
904     return TRUE;
905
906   g_object_get (gtk_widget_get_settings (widget),
907                 "gtk-enable-mnemonics", &enable_mnemonics,
908                 NULL);
909
910   if (enable_mnemonics)
911     return gtk_menu_shell_activate_mnemonic (menu_shell, event);
912
913   return FALSE;
914 }
915
916 static gint
917 gtk_menu_shell_enter_notify (GtkWidget        *widget,
918                              GdkEventCrossing *event)
919 {
920   GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
921
922   if (event->mode == GDK_CROSSING_GTK_GRAB ||
923       event->mode == GDK_CROSSING_GTK_UNGRAB ||
924       event->mode == GDK_CROSSING_STATE_CHANGED)
925     return TRUE;
926
927   if (menu_shell->active)
928     {
929       GtkWidget *menu_item;
930
931       menu_item = gtk_get_event_widget ((GdkEvent*) event);
932
933       if (!menu_item)
934         return TRUE;
935
936       if (GTK_IS_MENU_ITEM (menu_item) &&
937           !_gtk_menu_item_is_selectable (menu_item))
938         {
939           GtkMenuShellPrivate *priv;
940
941           priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
942           priv->in_unselectable_item = TRUE;
943
944           return TRUE;
945         }
946
947       if (menu_item->parent == widget &&
948           GTK_IS_MENU_ITEM (menu_item))
949         {
950           if (menu_shell->ignore_enter)
951             return TRUE;
952
953           if (event->detail != GDK_NOTIFY_INFERIOR)
954             {
955               if (gtk_widget_get_state (menu_item) != GTK_STATE_PRELIGHT)
956                 gtk_menu_shell_select_item (menu_shell, menu_item);
957
958               /* If any mouse button is down, and there is a submenu
959                * that is not yet visible, activate it. It's sufficient
960                * to check for any button's mask (not only the one
961                * matching menu_shell->button), because there is no
962                * situation a mouse button could be pressed while
963                * entering a menu item where we wouldn't want to show
964                * its submenu.
965                */
966               if ((event->state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)) &&
967                   GTK_MENU_ITEM (menu_item)->submenu != NULL)
968                 {
969                   GtkMenuShellPrivate *priv;
970
971                   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_item->parent);
972                   priv->activated_submenu = TRUE;
973
974                   if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->submenu))
975                     {
976                       gboolean touchscreen_mode;
977
978                       g_object_get (gtk_widget_get_settings (widget),
979                                     "gtk-touchscreen-mode", &touchscreen_mode,
980                                     NULL);
981
982                       if (touchscreen_mode)
983                         _gtk_menu_item_popup_submenu (menu_item, TRUE);
984                     }
985                 }
986             }
987         }
988       else if (menu_shell->parent_menu_shell)
989         {
990           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
991         }
992     }
993
994   return TRUE;
995 }
996
997 static gint
998 gtk_menu_shell_leave_notify (GtkWidget        *widget,
999                              GdkEventCrossing *event)
1000 {
1001   if (event->mode == GDK_CROSSING_GTK_GRAB ||
1002       event->mode == GDK_CROSSING_GTK_GRAB ||
1003       event->mode == GDK_CROSSING_STATE_CHANGED)
1004     return TRUE;
1005
1006   if (gtk_widget_get_visible (widget))
1007     {
1008       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
1009       GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
1010       GtkMenuItem *menu_item;
1011
1012       if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1013         return TRUE;
1014
1015       menu_item = GTK_MENU_ITEM (event_widget);
1016
1017       if (!_gtk_menu_item_is_selectable (event_widget))
1018         {
1019           GtkMenuShellPrivate *priv;
1020
1021           priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1022           priv->in_unselectable_item = TRUE;
1023
1024           return TRUE;
1025         }
1026
1027       if ((menu_shell->active_menu_item == event_widget) &&
1028           (menu_item->submenu == NULL))
1029         {
1030           if ((event->detail != GDK_NOTIFY_INFERIOR) &&
1031               (gtk_widget_get_state (GTK_WIDGET (menu_item)) != GTK_STATE_NORMAL))
1032             {
1033               gtk_menu_shell_deselect (menu_shell);
1034             }
1035         }
1036       else if (menu_shell->parent_menu_shell)
1037         {
1038           gtk_widget_event (menu_shell->parent_menu_shell, (GdkEvent*) event);
1039         }
1040     }
1041
1042   return TRUE;
1043 }
1044
1045 static void
1046 gtk_menu_shell_screen_changed (GtkWidget *widget,
1047                                GdkScreen *previous_screen)
1048 {
1049   gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
1050 }
1051
1052 static void
1053 gtk_menu_shell_add (GtkContainer *container,
1054                     GtkWidget    *widget)
1055 {
1056   gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
1057 }
1058
1059 static void
1060 gtk_menu_shell_remove (GtkContainer *container,
1061                        GtkWidget    *widget)
1062 {
1063   GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1064   gint was_visible;
1065
1066   was_visible = gtk_widget_get_visible (widget);
1067   menu_shell->children = g_list_remove (menu_shell->children, widget);
1068   
1069   if (widget == menu_shell->active_menu_item)
1070     {
1071       gtk_item_deselect (GTK_ITEM (menu_shell->active_menu_item));
1072       menu_shell->active_menu_item = NULL;
1073     }
1074
1075   gtk_widget_unparent (widget);
1076   
1077   /* queue resize regardless of gtk_widget_get_visible (container),
1078    * since that's what is needed by toplevels.
1079    */
1080   if (was_visible)
1081     gtk_widget_queue_resize (GTK_WIDGET (container));
1082 }
1083
1084 static void
1085 gtk_menu_shell_forall (GtkContainer *container,
1086                        gboolean      include_internals,
1087                        GtkCallback   callback,
1088                        gpointer      callback_data)
1089 {
1090   GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1091   GtkWidget *child;
1092   GList *children;
1093
1094   children = menu_shell->children;
1095   while (children)
1096     {
1097       child = children->data;
1098       children = children->next;
1099
1100       (* callback) (child, callback_data);
1101     }
1102 }
1103
1104
1105 static void
1106 gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
1107 {
1108   if (menu_shell->active)
1109     {
1110       GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1111
1112       menu_shell->button = 0;
1113       menu_shell->active = FALSE;
1114       menu_shell->activate_time = 0;
1115
1116       if (menu_shell->active_menu_item)
1117         {
1118           gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1119           menu_shell->active_menu_item = NULL;
1120         }
1121
1122       if (menu_shell->have_grab)
1123         {
1124           menu_shell->have_grab = FALSE;
1125           gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1126         }
1127       if (menu_shell->have_xgrab)
1128         {
1129           GdkDevice *keyboard;
1130
1131           gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1132           keyboard = gdk_device_get_associated_device (priv->grab_pointer);
1133
1134           if (keyboard)
1135             gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1136
1137           menu_shell->have_xgrab = FALSE;
1138         }
1139
1140       menu_shell->keyboard_mode = FALSE;
1141       _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1142
1143       _gtk_menu_shell_update_mnemonics (menu_shell);
1144     }
1145 }
1146
1147 static gint
1148 gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1149                         GtkWidget    *child)
1150 {
1151   GtkWidget *parent;
1152
1153   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1154   g_return_val_if_fail (child != NULL, FALSE);
1155
1156   parent = child->parent;
1157   while (GTK_IS_MENU_SHELL (parent))
1158     {
1159       if (parent == (GtkWidget*) menu_shell)
1160         return TRUE;
1161       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1162     }
1163
1164   return FALSE;
1165 }
1166
1167 static GtkWidget*
1168 gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1169                          GdkEvent     *event)
1170 {
1171   GtkWidget *menu_item;
1172
1173   menu_item = gtk_get_event_widget ((GdkEvent*) event);
1174   
1175   while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1176     menu_item = menu_item->parent;
1177
1178   if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
1179     return menu_item;
1180   else
1181     return NULL;
1182 }
1183
1184 /* Handlers for action signals */
1185
1186 void
1187 gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1188                             GtkWidget    *menu_item)
1189 {
1190   GtkMenuShellClass *class;
1191
1192   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1193   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1194
1195   class = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1196
1197   if (class->select_item &&
1198       !(menu_shell->active &&
1199         menu_shell->active_menu_item == menu_item))
1200     class->select_item (menu_shell, menu_item);
1201 }
1202
1203 void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1204                                    GtkSubmenuPlacement  placement);
1205
1206 static void
1207 gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1208                                  GtkWidget    *menu_item)
1209 {
1210   GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);
1211
1212   if (menu_shell->active_menu_item)
1213     {
1214       gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1215       menu_shell->active_menu_item = NULL;
1216     }
1217
1218   if (!_gtk_menu_item_is_selectable (menu_item))
1219     {
1220       GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1221
1222       priv->in_unselectable_item = TRUE;
1223       _gtk_menu_shell_update_mnemonics (menu_shell);
1224
1225       return;
1226     }
1227
1228   menu_shell->active_menu_item = menu_item;
1229   if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1230     _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1231                                   GTK_LEFT_RIGHT);
1232   else
1233     _gtk_menu_item_set_placement (GTK_MENU_ITEM (menu_shell->active_menu_item),
1234                                   GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
1235   gtk_menu_item_select (GTK_MENU_ITEM (menu_shell->active_menu_item));
1236
1237   _gtk_menu_shell_update_mnemonics (menu_shell);
1238
1239   /* This allows the bizarre radio buttons-with-submenus-display-history
1240    * behavior
1241    */
1242   if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1243     gtk_widget_activate (menu_shell->active_menu_item);
1244 }
1245
1246 void
1247 gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1248 {
1249   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1250
1251   if (menu_shell->active_menu_item)
1252     {
1253       gtk_menu_item_deselect (GTK_MENU_ITEM (menu_shell->active_menu_item));
1254       menu_shell->active_menu_item = NULL;
1255       _gtk_menu_shell_update_mnemonics (menu_shell);
1256     }
1257 }
1258
1259 void
1260 gtk_menu_shell_activate_item (GtkMenuShell      *menu_shell,
1261                               GtkWidget         *menu_item,
1262                               gboolean           force_deactivate)
1263 {
1264   GSList *slist, *shells = NULL;
1265   gboolean deactivate = force_deactivate;
1266
1267   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1268   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1269
1270   if (!deactivate)
1271     deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1272
1273   g_object_ref (menu_shell);
1274   g_object_ref (menu_item);
1275
1276   if (deactivate)
1277     {
1278       GtkMenuShell *parent_menu_shell = menu_shell;
1279
1280       do
1281         {
1282           g_object_ref (parent_menu_shell);
1283           shells = g_slist_prepend (shells, parent_menu_shell);
1284           parent_menu_shell = (GtkMenuShell*) parent_menu_shell->parent_menu_shell;
1285         }
1286       while (parent_menu_shell);
1287       shells = g_slist_reverse (shells);
1288
1289       gtk_menu_shell_deactivate (menu_shell);
1290   
1291       /* flush the x-queue, so any grabs are removed and
1292        * the menu is actually taken down
1293        */
1294       gdk_display_sync (gtk_widget_get_display (menu_item));
1295     }
1296
1297   gtk_widget_activate (menu_item);
1298
1299   for (slist = shells; slist; slist = slist->next)
1300     {
1301       g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
1302       g_object_unref (slist->data);
1303     }
1304   g_slist_free (shells);
1305
1306   g_object_unref (menu_shell);
1307   g_object_unref (menu_item);
1308 }
1309
1310 /* Distance should be +/- 1 */
1311 static gboolean
1312 gtk_menu_shell_real_move_selected (GtkMenuShell  *menu_shell, 
1313                                    gint           distance)
1314 {
1315   if (menu_shell->active_menu_item)
1316     {
1317       GList *node = g_list_find (menu_shell->children,
1318                                  menu_shell->active_menu_item);
1319       GList *start_node = node;
1320       gboolean wrap_around;
1321
1322       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1323                     "gtk-keynav-wrap-around", &wrap_around,
1324                     NULL);
1325
1326       if (distance > 0)
1327         {
1328           node = node->next;
1329           while (node != start_node && 
1330                  (!node || !_gtk_menu_item_is_selectable (node->data)))
1331             {
1332               if (node)
1333                 node = node->next;
1334               else if (wrap_around)
1335                 node = menu_shell->children;
1336               else
1337                 {
1338                   gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1339                   break;
1340                 }
1341             }
1342         }
1343       else
1344         {
1345           node = node->prev;
1346           while (node != start_node &&
1347                  (!node || !_gtk_menu_item_is_selectable (node->data)))
1348             {
1349               if (node)
1350                 node = node->prev;
1351               else if (wrap_around)
1352                 node = g_list_last (menu_shell->children);
1353               else
1354                 {
1355                   gtk_widget_error_bell (GTK_WIDGET (menu_shell));
1356                   break;
1357                 }
1358             }
1359         }
1360       
1361       if (node)
1362         gtk_menu_shell_select_item (menu_shell, node->data);
1363     }
1364
1365   return TRUE;
1366 }
1367
1368 /* Distance should be +/- 1 */
1369 static void
1370 gtk_menu_shell_move_selected (GtkMenuShell  *menu_shell, 
1371                               gint           distance)
1372 {
1373   gboolean handled = FALSE;
1374
1375   g_signal_emit (menu_shell, menu_shell_signals[MOVE_SELECTED], 0,
1376                  distance, &handled);
1377 }
1378
1379 /**
1380  * gtk_menu_shell_select_first:
1381  * @menu_shell: a #GtkMenuShell
1382  * @search_sensitive: if %TRUE, search for the first selectable
1383  *                    menu item, otherwise select nothing if
1384  *                    the first item isn't sensitive. This
1385  *                    should be %FALSE if the menu is being
1386  *                    popped up initially.
1387  * 
1388  * Select the first visible or selectable child of the menu shell;
1389  * don't select tearoff items unless the only item is a tearoff
1390  * item.
1391  *
1392  * Since: 2.2
1393  **/
1394 void
1395 gtk_menu_shell_select_first (GtkMenuShell *menu_shell,
1396                              gboolean      search_sensitive)
1397 {
1398   GtkWidget *to_select = NULL;
1399   GList *tmp_list;
1400
1401   tmp_list = 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->next;
1415     }
1416
1417   if (to_select)
1418     gtk_menu_shell_select_item (menu_shell, to_select);
1419 }
1420
1421 void
1422 _gtk_menu_shell_select_last (GtkMenuShell *menu_shell,
1423                              gboolean      search_sensitive)
1424 {
1425   GtkWidget *to_select = NULL;
1426   GList *tmp_list;
1427
1428   tmp_list = g_list_last (menu_shell->children);
1429   while (tmp_list)
1430     {
1431       GtkWidget *child = tmp_list->data;
1432       
1433       if ((!search_sensitive && gtk_widget_get_visible (child)) ||
1434           _gtk_menu_item_is_selectable (child))
1435         {
1436           to_select = child;
1437           if (!GTK_IS_TEAROFF_MENU_ITEM (child))
1438             break;
1439         }
1440       
1441       tmp_list = tmp_list->prev;
1442     }
1443
1444   if (to_select)
1445     gtk_menu_shell_select_item (menu_shell, to_select);
1446 }
1447
1448 static gboolean
1449 gtk_menu_shell_select_submenu_first (GtkMenuShell     *menu_shell)
1450 {
1451   GtkMenuItem *menu_item;
1452
1453   if (menu_shell->active_menu_item == NULL)
1454     return FALSE;
1455
1456   menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item); 
1457   
1458   if (menu_item->submenu)
1459     {
1460       _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), FALSE);
1461       gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1462       if (GTK_MENU_SHELL (menu_item->submenu)->active_menu_item)
1463         return TRUE;
1464     }
1465
1466   return FALSE;
1467 }
1468
1469 static void
1470 gtk_real_menu_shell_move_current (GtkMenuShell         *menu_shell,
1471                                   GtkMenuDirectionType  direction)
1472 {
1473   GtkMenuShellPrivate *priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1474   GtkMenuShell *parent_menu_shell = NULL;
1475   gboolean had_selection;
1476   gboolean touchscreen_mode;
1477
1478   priv->in_unselectable_item = FALSE;
1479
1480   had_selection = menu_shell->active_menu_item != NULL;
1481
1482   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
1483                 "gtk-touchscreen-mode", &touchscreen_mode,
1484                 NULL);
1485
1486   if (menu_shell->parent_menu_shell)
1487     parent_menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1488
1489   switch (direction)
1490     {
1491     case GTK_MENU_DIR_PARENT:
1492       if (touchscreen_mode &&
1493           menu_shell->active_menu_item &&
1494           GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
1495           gtk_widget_get_visible (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu))
1496         {
1497           /* if we are on a menu item that has an open submenu but the
1498            * focus is not in that submenu (e.g. because it's empty or
1499            * has only insensitive items), close that submenu instead
1500            * of running into the code below which would close *this*
1501            * menu.
1502            */
1503           _gtk_menu_item_popdown_submenu (menu_shell->active_menu_item);
1504           _gtk_menu_shell_update_mnemonics (menu_shell);
1505         }
1506       else if (parent_menu_shell)
1507         {
1508           if (touchscreen_mode)
1509             {
1510               /* close menu when returning from submenu. */
1511               _gtk_menu_item_popdown_submenu (GTK_MENU (menu_shell)->parent_menu_item);
1512               _gtk_menu_shell_update_mnemonics (parent_menu_shell);
1513               break;
1514             }
1515
1516           if (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1517               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement)
1518             gtk_menu_shell_deselect (menu_shell);
1519           else
1520             {
1521               if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1522                 gtk_menu_shell_move_selected (parent_menu_shell, -1);
1523               else
1524                 gtk_menu_shell_move_selected (parent_menu_shell, 1);
1525               gtk_menu_shell_select_submenu_first (parent_menu_shell);
1526             }
1527         }
1528       /* If there is no parent and the submenu is in the opposite direction
1529        * to the menu, then make the PARENT direction wrap around to
1530        * the bottom of the submenu.
1531        */
1532       else if (menu_shell->active_menu_item &&
1533                _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1534                GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1535         {
1536           GtkMenuShell *submenu = GTK_MENU_SHELL (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu);
1537
1538           if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement !=
1539               GTK_MENU_SHELL_GET_CLASS (submenu)->submenu_placement)
1540             _gtk_menu_shell_select_last (submenu, TRUE);
1541         }
1542       break;
1543
1544     case GTK_MENU_DIR_CHILD:
1545       if (menu_shell->active_menu_item &&
1546           _gtk_menu_item_is_selectable (menu_shell->active_menu_item) &&
1547           GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu)
1548         {
1549           if (gtk_menu_shell_select_submenu_first (menu_shell))
1550             break;
1551         }
1552
1553       /* Try to find a menu running the opposite direction */
1554       while (parent_menu_shell &&
1555              (GTK_MENU_SHELL_GET_CLASS (parent_menu_shell)->submenu_placement ==
1556               GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement))
1557         {
1558           parent_menu_shell = GTK_MENU_SHELL (parent_menu_shell->parent_menu_shell);
1559         }
1560
1561       if (parent_menu_shell)
1562         {
1563           if (PACK_DIRECTION (parent_menu_shell) == GTK_PACK_DIRECTION_LTR)
1564             gtk_menu_shell_move_selected (parent_menu_shell, 1);
1565           else
1566             gtk_menu_shell_move_selected (parent_menu_shell, -1);
1567
1568           gtk_menu_shell_select_submenu_first (parent_menu_shell);
1569         }
1570       break;
1571
1572     case GTK_MENU_DIR_PREV:
1573       gtk_menu_shell_move_selected (menu_shell, -1);
1574       if (!had_selection &&
1575           !menu_shell->active_menu_item &&
1576           menu_shell->children)
1577         _gtk_menu_shell_select_last (menu_shell, TRUE);
1578       break;
1579
1580     case GTK_MENU_DIR_NEXT:
1581       gtk_menu_shell_move_selected (menu_shell, 1);
1582       if (!had_selection &&
1583           !menu_shell->active_menu_item &&
1584           menu_shell->children)
1585         gtk_menu_shell_select_first (menu_shell, TRUE);
1586       break;
1587     }
1588 }
1589
1590 static void
1591 gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
1592                                       gboolean           force_hide)
1593 {
1594   if (menu_shell->active_menu_item &&
1595       _gtk_menu_item_is_selectable (menu_shell->active_menu_item))
1596   {
1597     if (GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL)
1598       gtk_menu_shell_activate_item (menu_shell,
1599                                     menu_shell->active_menu_item,
1600                                     force_hide);
1601     else
1602       _gtk_menu_item_popup_submenu (menu_shell->active_menu_item, FALSE);
1603   }
1604 }
1605
1606 static void
1607 gtk_real_menu_shell_cancel (GtkMenuShell      *menu_shell)
1608 {
1609   /* Unset the active menu item so gtk_menu_popdown() doesn't see it.
1610    */
1611   gtk_menu_shell_deselect (menu_shell);
1612   
1613   gtk_menu_shell_deactivate (menu_shell);
1614   g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
1615 }
1616
1617 static void
1618 gtk_real_menu_shell_cycle_focus (GtkMenuShell      *menu_shell,
1619                                  GtkDirectionType   dir)
1620 {
1621   while (menu_shell && !GTK_IS_MENU_BAR (menu_shell))
1622     {
1623       if (menu_shell->parent_menu_shell)
1624         menu_shell = GTK_MENU_SHELL (menu_shell->parent_menu_shell);
1625       else
1626         menu_shell = NULL;
1627     }
1628
1629   if (menu_shell)
1630     _gtk_menu_bar_cycle_focus (GTK_MENU_BAR (menu_shell), dir);
1631 }
1632
1633 gint
1634 _gtk_menu_shell_get_popup_delay (GtkMenuShell *menu_shell)
1635 {
1636   GtkMenuShellClass *klass = GTK_MENU_SHELL_GET_CLASS (menu_shell);
1637   
1638   if (klass->get_popup_delay)
1639     {
1640       return klass->get_popup_delay (menu_shell);
1641     }
1642   else
1643     {
1644       gint popup_delay;
1645       GtkWidget *widget = GTK_WIDGET (menu_shell);
1646       
1647       g_object_get (gtk_widget_get_settings (widget),
1648                     "gtk-menu-popup-delay", &popup_delay,
1649                     NULL);
1650       
1651       return popup_delay;
1652     }
1653 }
1654
1655 /**
1656  * gtk_menu_shell_cancel:
1657  * @menu_shell: a #GtkMenuShell
1658  * 
1659  * Cancels the selection within the menu shell.  
1660  * 
1661  * Since: 2.4
1662  */
1663 void
1664 gtk_menu_shell_cancel (GtkMenuShell *menu_shell)
1665 {
1666   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1667
1668   g_signal_emit (menu_shell, menu_shell_signals[CANCEL], 0);
1669 }
1670
1671 static GtkMnemonicHash *
1672 gtk_menu_shell_get_mnemonic_hash (GtkMenuShell *menu_shell,
1673                                   gboolean      create)
1674 {
1675   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1676
1677   if (!private->mnemonic_hash && create)
1678     private->mnemonic_hash = _gtk_mnemonic_hash_new ();
1679   
1680   return private->mnemonic_hash;
1681 }
1682
1683 static void
1684 menu_shell_add_mnemonic_foreach (guint    keyval,
1685                                  GSList  *targets,
1686                                  gpointer data)
1687 {
1688   GtkKeyHash *key_hash = data;
1689
1690   _gtk_key_hash_add_entry (key_hash, keyval, 0, GUINT_TO_POINTER (keyval));
1691 }
1692
1693 static GtkKeyHash *
1694 gtk_menu_shell_get_key_hash (GtkMenuShell *menu_shell,
1695                              gboolean      create)
1696 {
1697   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1698   GtkWidget *widget = GTK_WIDGET (menu_shell);
1699
1700   if (!private->key_hash && create && gtk_widget_has_screen (widget))
1701     {
1702       GtkMnemonicHash *mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1703       GdkScreen *screen = gtk_widget_get_screen (widget);
1704       GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_screen_get_display (screen));
1705
1706       if (!mnemonic_hash)
1707         return NULL;
1708       
1709       private->key_hash = _gtk_key_hash_new (keymap, NULL);
1710
1711       _gtk_mnemonic_hash_foreach (mnemonic_hash,
1712                                   menu_shell_add_mnemonic_foreach,
1713                                   private->key_hash);
1714     }
1715   
1716   return private->key_hash;
1717 }
1718
1719 static void
1720 gtk_menu_shell_reset_key_hash (GtkMenuShell *menu_shell)
1721 {
1722   GtkMenuShellPrivate *private = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1723
1724   if (private->key_hash)
1725     {
1726       _gtk_key_hash_free (private->key_hash);
1727       private->key_hash = NULL;
1728     }
1729 }
1730
1731 static gboolean
1732 gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
1733                                   GdkEventKey  *event)
1734 {
1735   GtkMnemonicHash *mnemonic_hash;
1736   GtkKeyHash *key_hash;
1737   GSList *entries;
1738   gboolean result = FALSE;
1739
1740   mnemonic_hash = gtk_menu_shell_get_mnemonic_hash (menu_shell, FALSE);
1741   if (!mnemonic_hash)
1742     return FALSE;
1743
1744   key_hash = gtk_menu_shell_get_key_hash (menu_shell, TRUE);
1745   if (!key_hash)
1746     return FALSE;
1747   
1748   entries = _gtk_key_hash_lookup (key_hash,
1749                                   event->hardware_keycode,
1750                                   event->state,
1751                                   gtk_accelerator_get_default_mod_mask (),
1752                                   event->group);
1753
1754   if (entries)
1755     result = _gtk_mnemonic_hash_activate (mnemonic_hash,
1756                                           GPOINTER_TO_UINT (entries->data));
1757
1758   return result;
1759 }
1760
1761 void
1762 _gtk_menu_shell_add_mnemonic (GtkMenuShell *menu_shell,
1763                               guint      keyval,
1764                               GtkWidget *target)
1765 {
1766   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1767   g_return_if_fail (GTK_IS_WIDGET (target));
1768
1769   _gtk_mnemonic_hash_add (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1770                           keyval, target);
1771   gtk_menu_shell_reset_key_hash (menu_shell);
1772 }
1773
1774 void
1775 _gtk_menu_shell_remove_mnemonic (GtkMenuShell *menu_shell,
1776                                  guint      keyval,
1777                                  GtkWidget *target)
1778 {
1779   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1780   g_return_if_fail (GTK_IS_WIDGET (target));
1781   
1782   _gtk_mnemonic_hash_remove (gtk_menu_shell_get_mnemonic_hash (menu_shell, TRUE),
1783                              keyval, target);
1784   gtk_menu_shell_reset_key_hash (menu_shell);
1785 }
1786
1787 void
1788 _gtk_menu_shell_set_grab_device (GtkMenuShell *menu_shell,
1789                                  GdkDevice    *device)
1790 {
1791   GtkMenuShellPrivate *priv;
1792
1793   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1794   g_return_if_fail (!device || GDK_IS_DEVICE (device));
1795
1796   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1797
1798   if (!device)
1799     priv->grab_pointer = NULL;
1800   else if (device->source == GDK_SOURCE_KEYBOARD)
1801     priv->grab_pointer = gdk_device_get_associated_device (device);
1802   else
1803     priv->grab_pointer = device;
1804 }
1805
1806 GdkDevice *
1807 _gtk_menu_shell_get_grab_device (GtkMenuShell  *menu_shell)
1808 {
1809   GtkMenuShellPrivate *priv;
1810
1811   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1812
1813   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1814
1815   return priv->grab_pointer;
1816 }
1817
1818 /**
1819  * gtk_menu_shell_get_take_focus:
1820  * @menu_shell: a #GtkMenuShell
1821  *
1822  * Returns %TRUE if the menu shell will take the keyboard focus on popup.
1823  *
1824  * Returns: %TRUE if the menu shell will take the keyboard focus on popup.
1825  *
1826  * Since: 2.8
1827  **/
1828 gboolean
1829 gtk_menu_shell_get_take_focus (GtkMenuShell *menu_shell)
1830 {
1831   GtkMenuShellPrivate *priv;
1832
1833   g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
1834
1835   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1836
1837   return priv->take_focus;
1838 }
1839
1840 /**
1841  * gtk_menu_shell_set_take_focus:
1842  * @menu_shell: a #GtkMenuShell
1843  * @take_focus: %TRUE if the menu shell should take the keyboard focus on popup.
1844  *
1845  * If @take_focus is %TRUE (the default) the menu shell will take the keyboard 
1846  * focus so that it will receive all keyboard events which is needed to enable
1847  * keyboard navigation in menus.
1848  *
1849  * Setting @take_focus to %FALSE is useful only for special applications
1850  * like virtual keyboard implementations which should not take keyboard
1851  * focus.
1852  *
1853  * The @take_focus state of a menu or menu bar is automatically propagated
1854  * to submenus whenever a submenu is popped up, so you don't have to worry
1855  * about recursively setting it for your entire menu hierarchy. Only when
1856  * programmatically picking a submenu and popping it up manually, the
1857  * @take_focus property of the submenu needs to be set explicitely.
1858  *
1859  * Note that setting it to %FALSE has side-effects:
1860  *
1861  * If the focus is in some other app, it keeps the focus and keynav in
1862  * the menu doesn't work. Consequently, keynav on the menu will only
1863  * work if the focus is on some toplevel owned by the onscreen keyboard.
1864  *
1865  * To avoid confusing the user, menus with @take_focus set to %FALSE
1866  * should not display mnemonics or accelerators, since it cannot be
1867  * guaranteed that they will work.
1868  *
1869  * See also gdk_keyboard_grab()
1870  *
1871  * Since: 2.8
1872  **/
1873 void
1874 gtk_menu_shell_set_take_focus (GtkMenuShell *menu_shell,
1875                                gboolean      take_focus)
1876 {
1877   GtkMenuShellPrivate *priv;
1878
1879   g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
1880
1881   priv = GTK_MENU_SHELL_GET_PRIVATE (menu_shell);
1882
1883   if (priv->take_focus != take_focus)
1884     {
1885       priv->take_focus = take_focus;
1886       g_object_notify (G_OBJECT (menu_shell), "take-focus");
1887     }
1888 }