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