]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Remove sealed members from GtkMenuShell
[~andy/gtk] / gtk / gtkmenubar.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 #include "config.h"
28
29 #include "gtkmenubar.h"
30
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbindings.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenuitem.h"
36 #include "gtkmenuprivate.h"
37 #include "gtkmenushellprivate.h"
38 #include "gtksettings.h"
39 #include "gtksizerequest.h"
40 #include "gtkwindow.h"
41
42 #include "gtkintl.h"
43 #include "gtkprivate.h"
44
45 #define BORDER_SPACING  0
46 #define DEFAULT_IPADDING 1
47
48 /* Properties */
49 enum {
50   PROP_0,
51   PROP_PACK_DIRECTION,
52   PROP_CHILD_PACK_DIRECTION
53 };
54
55 struct _GtkMenuBarPrivate
56 {
57   GtkPackDirection pack_direction;
58   GtkPackDirection child_pack_direction;
59 };
60
61
62 static void gtk_menu_bar_set_property      (GObject             *object,
63                                             guint                prop_id,
64                                             const GValue        *value,
65                                             GParamSpec          *pspec);
66 static void gtk_menu_bar_get_property      (GObject             *object,
67                                             guint                prop_id,
68                                             GValue              *value,
69                                             GParamSpec          *pspec);
70 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
71                                             GtkRequisition  *requisition);
72 static void gtk_menu_bar_get_preferred_width (GtkWidget     *widget,
73                                               gint          *minimum,
74                                               gint          *natural);
75 static void gtk_menu_bar_get_preferred_height (GtkWidget    *widget,
76                                                gint         *minimum,
77                                                gint         *natural);
78 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
79                                             GtkAllocation   *allocation);
80 static gint gtk_menu_bar_draw              (GtkWidget       *widget,
81                                             cairo_t         *cr);
82 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
83                                             GtkWidget       *old_toplevel);
84 static gint gtk_menu_bar_get_popup_delay   (GtkMenuShell    *menu_shell);
85 static void gtk_menu_bar_move_current      (GtkMenuShell     *menu_shell,
86                                             GtkMenuDirectionType direction);
87
88 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
89
90 G_DEFINE_TYPE (GtkMenuBar, gtk_menu_bar, GTK_TYPE_MENU_SHELL)
91
92 static void
93 gtk_menu_bar_class_init (GtkMenuBarClass *class)
94 {
95   GObjectClass *gobject_class;
96   GtkWidgetClass *widget_class;
97   GtkMenuShellClass *menu_shell_class;
98
99   GtkBindingSet *binding_set;
100
101   gobject_class = (GObjectClass*) class;
102   widget_class = (GtkWidgetClass*) class;
103   menu_shell_class = (GtkMenuShellClass*) class;
104
105   gobject_class->get_property = gtk_menu_bar_get_property;
106   gobject_class->set_property = gtk_menu_bar_set_property;
107
108   widget_class->get_preferred_width = gtk_menu_bar_get_preferred_width;
109   widget_class->get_preferred_height = gtk_menu_bar_get_preferred_height;
110   widget_class->size_allocate = gtk_menu_bar_size_allocate;
111   widget_class->draw = gtk_menu_bar_draw;
112   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
113   
114   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
115   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
116   menu_shell_class->move_current = gtk_menu_bar_move_current;
117
118   binding_set = gtk_binding_set_by_class (class);
119   gtk_binding_entry_add_signal (binding_set,
120                                 GDK_KEY_Left, 0,
121                                 "move-current", 1,
122                                 GTK_TYPE_MENU_DIRECTION_TYPE,
123                                 GTK_MENU_DIR_PREV);
124   gtk_binding_entry_add_signal (binding_set,
125                                 GDK_KEY_KP_Left, 0,
126                                 "move-current", 1,
127                                 GTK_TYPE_MENU_DIRECTION_TYPE,
128                                 GTK_MENU_DIR_PREV);
129   gtk_binding_entry_add_signal (binding_set,
130                                 GDK_KEY_Right, 0,
131                                 "move-current", 1,
132                                 GTK_TYPE_MENU_DIRECTION_TYPE,
133                                 GTK_MENU_DIR_NEXT);
134   gtk_binding_entry_add_signal (binding_set,
135                                 GDK_KEY_KP_Right, 0,
136                                 "move-current", 1,
137                                 GTK_TYPE_MENU_DIRECTION_TYPE,
138                                 GTK_MENU_DIR_NEXT);
139   gtk_binding_entry_add_signal (binding_set,
140                                 GDK_KEY_Up, 0,
141                                 "move-current", 1,
142                                 GTK_TYPE_MENU_DIRECTION_TYPE,
143                                 GTK_MENU_DIR_PARENT);
144   gtk_binding_entry_add_signal (binding_set,
145                                 GDK_KEY_KP_Up, 0,
146                                 "move-current", 1,
147                                 GTK_TYPE_MENU_DIRECTION_TYPE,
148                                 GTK_MENU_DIR_PARENT);
149   gtk_binding_entry_add_signal (binding_set,
150                                 GDK_KEY_Down, 0,
151                                 "move-current", 1,
152                                 GTK_TYPE_MENU_DIRECTION_TYPE,
153                                 GTK_MENU_DIR_CHILD);
154   gtk_binding_entry_add_signal (binding_set,
155                                 GDK_KEY_KP_Down, 0,
156                                 "move-current", 1,
157                                 GTK_TYPE_MENU_DIRECTION_TYPE,
158                                 GTK_MENU_DIR_CHILD);
159
160   /**
161    * GtkMenuBar:pack-direction:
162    *
163    * The pack direction of the menubar. It determines how
164    * menuitems are arranged in the menubar.
165    *
166    * Since: 2.8
167    */
168   g_object_class_install_property (gobject_class,
169                                    PROP_PACK_DIRECTION,
170                                    g_param_spec_enum ("pack-direction",
171                                                       P_("Pack direction"),
172                                                       P_("The pack direction of the menubar"),
173                                                       GTK_TYPE_PACK_DIRECTION,
174                                                       GTK_PACK_DIRECTION_LTR,
175                                                       GTK_PARAM_READWRITE));
176   
177   /**
178    * GtkMenuBar:child-pack-direction:
179    *
180    * The child pack direction of the menubar. It determines how
181    * the widgets contained in child menuitems are arranged.
182    *
183    * Since: 2.8
184    */
185   g_object_class_install_property (gobject_class,
186                                    PROP_CHILD_PACK_DIRECTION,
187                                    g_param_spec_enum ("child-pack-direction",
188                                                       P_("Child Pack direction"),
189                                                       P_("The child pack direction of the menubar"),
190                                                       GTK_TYPE_PACK_DIRECTION,
191                                                       GTK_PACK_DIRECTION_LTR,
192                                                       GTK_PARAM_READWRITE));
193   
194
195   gtk_widget_class_install_style_property (widget_class,
196                                            g_param_spec_enum ("shadow-type",
197                                                               P_("Shadow type"),
198                                                               P_("Style of bevel around the menubar"),
199                                                               GTK_TYPE_SHADOW_TYPE,
200                                                               GTK_SHADOW_OUT,
201                                                               GTK_PARAM_READABLE));
202
203   gtk_widget_class_install_style_property (widget_class,
204                                            g_param_spec_int ("internal-padding",
205                                                              P_("Internal padding"),
206                                                              P_("Amount of border space between the menubar shadow and the menu items"),
207                                                              0,
208                                                              G_MAXINT,
209                                                              DEFAULT_IPADDING,
210                                                              GTK_PARAM_READABLE));
211
212   g_type_class_add_private (gobject_class, sizeof (GtkMenuBarPrivate));
213 }
214
215 static void
216 gtk_menu_bar_init (GtkMenuBar *menu_bar)
217 {
218   GtkStyleContext *context;
219
220   menu_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_bar,
221                                                 GTK_TYPE_MENU_BAR,
222                                                 GtkMenuBarPrivate);
223
224   context = gtk_widget_get_style_context (GTK_WIDGET (menu_bar));
225   gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
226 }
227
228 GtkWidget*
229 gtk_menu_bar_new (void)
230 {
231   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
232 }
233
234 static void
235 gtk_menu_bar_set_property (GObject      *object,
236                            guint         prop_id,
237                            const GValue *value,
238                            GParamSpec   *pspec)
239 {
240   GtkMenuBar *menubar = GTK_MENU_BAR (object);
241   
242   switch (prop_id)
243     {
244     case PROP_PACK_DIRECTION:
245       gtk_menu_bar_set_pack_direction (menubar, g_value_get_enum (value));
246       break;
247     case PROP_CHILD_PACK_DIRECTION:
248       gtk_menu_bar_set_child_pack_direction (menubar, g_value_get_enum (value));
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253     }
254 }
255
256 static void
257 gtk_menu_bar_get_property (GObject    *object,
258                            guint       prop_id,
259                            GValue     *value,
260                            GParamSpec *pspec)
261 {
262   GtkMenuBar *menubar = GTK_MENU_BAR (object);
263   
264   switch (prop_id)
265     {
266     case PROP_PACK_DIRECTION:
267       g_value_set_enum (value, gtk_menu_bar_get_pack_direction (menubar));
268       break;
269     case PROP_CHILD_PACK_DIRECTION:
270       g_value_set_enum (value, gtk_menu_bar_get_child_pack_direction (menubar));
271       break;
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275     }
276 }
277
278 static void
279 gtk_menu_bar_size_request (GtkWidget      *widget,
280                            GtkRequisition *requisition)
281 {
282   GtkMenuBar *menu_bar;
283   GtkMenuBarPrivate *priv;
284   GtkMenuShell *menu_shell;
285   GtkWidget *child;
286   GList *children;
287   gint nchildren;
288   GtkRequisition child_requisition;
289   gint ipadding;
290   guint border_width;
291
292   g_return_if_fail (GTK_IS_MENU_BAR (widget));
293   g_return_if_fail (requisition != NULL);
294
295   requisition->width = 0;
296   requisition->height = 0;
297   
298   if (gtk_widget_get_visible (widget))
299     {
300       menu_bar = GTK_MENU_BAR (widget);
301       menu_shell = GTK_MENU_SHELL (widget);
302       priv = menu_bar->priv;
303
304       nchildren = 0;
305       children = menu_shell->priv->children;
306
307       while (children)
308         {
309           child = children->data;
310           children = children->next;
311
312           if (gtk_widget_get_visible (child))
313             {
314               gint toggle_size;
315
316               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
317               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
318               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
319                                                  &toggle_size);
320
321               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
322                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
323                 child_requisition.width += toggle_size;
324               else
325                 child_requisition.height += toggle_size;
326
327               if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
328                   priv->pack_direction == GTK_PACK_DIRECTION_RTL)
329                 {
330                   requisition->width += child_requisition.width;
331                   requisition->height = MAX (requisition->height, child_requisition.height);
332                 }
333               else
334                 {
335                   requisition->width = MAX (requisition->width, child_requisition.width);
336                   requisition->height += child_requisition.height;
337                 }
338               nchildren += 1;
339             }
340         }
341
342       gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
343
344       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
345       requisition->width += (border_width +
346                              ipadding + 
347                              BORDER_SPACING) * 2;
348       requisition->height += (border_width +
349                               ipadding +
350                               BORDER_SPACING) * 2;
351
352       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
353         {
354           GtkStyleContext *context;
355           GtkBorder *border;
356
357           context = gtk_widget_get_style_context (widget);
358
359           gtk_style_context_get (context, 0,
360                                  "border-width", &border,
361                                  NULL);
362
363           requisition->width += border->left + border->right;
364           requisition->height += border->top + border->bottom;
365           gtk_border_free (border);
366         }
367     }
368 }
369
370 static void
371 gtk_menu_bar_get_preferred_width (GtkWidget *widget,
372                                   gint      *minimum,
373                                   gint      *natural)
374 {
375   GtkRequisition requisition;
376
377   gtk_menu_bar_size_request (widget, &requisition);
378
379   *minimum = *natural = requisition.width;
380 }
381
382 static void
383 gtk_menu_bar_get_preferred_height (GtkWidget *widget,
384                                    gint      *minimum,
385                                    gint      *natural)
386 {
387   GtkRequisition requisition;
388
389   gtk_menu_bar_size_request (widget, &requisition);
390
391   *minimum = *natural = requisition.height;
392 }
393
394 static void
395 gtk_menu_bar_size_allocate (GtkWidget     *widget,
396                             GtkAllocation *allocation)
397 {
398   GtkMenuBar *menu_bar;
399   GtkMenuShell *menu_shell;
400   GtkMenuBarPrivate *priv;
401   GtkWidget *child;
402   GList *children;
403   GtkAllocation child_allocation;
404   GtkRequisition child_requisition;
405   guint offset;
406   GtkTextDirection direction;
407   gint ltr_x, ltr_y;
408   gint ipadding;
409   guint border_width;
410
411   g_return_if_fail (GTK_IS_MENU_BAR (widget));
412   g_return_if_fail (allocation != NULL);
413
414   menu_bar = GTK_MENU_BAR (widget);
415   menu_shell = GTK_MENU_SHELL (widget);
416   priv = menu_bar->priv;
417
418   direction = gtk_widget_get_direction (widget);
419
420   gtk_widget_set_allocation (widget, allocation);
421
422   if (gtk_widget_get_realized (widget))
423     gdk_window_move_resize (gtk_widget_get_window (widget),
424                             allocation->x, allocation->y,
425                             allocation->width, allocation->height);
426
427   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
428   
429   if (menu_shell->priv->children)
430     {
431       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
432       child_allocation.x = (border_width +
433                             ipadding + 
434                             BORDER_SPACING);
435       child_allocation.y = (border_width +
436                             BORDER_SPACING);
437       
438       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
439         {
440           GtkStyleContext *context;
441           GtkBorder *border;
442
443           context = gtk_widget_get_style_context (widget);
444           gtk_style_context_get (context, 0,
445                                  "border-width", &border,
446                                  NULL);
447
448           child_allocation.x += border->left;
449           child_allocation.y += border->top;
450
451           gtk_border_free (border);
452         }
453       
454       if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
455           priv->pack_direction == GTK_PACK_DIRECTION_RTL)
456         {
457           child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
458
459           offset = child_allocation.x;  /* Window edge to menubar start */
460           ltr_x = child_allocation.x;
461
462           children = menu_shell->priv->children;
463           while (children)
464             {
465               gint toggle_size;
466
467               child = children->data;
468               children = children->next;
469               
470               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
471                                                  &toggle_size);
472               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
473
474               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
475                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
476                 child_requisition.width += toggle_size;
477               else
478                 child_requisition.height += toggle_size;
479               
480               /* Support for the right justified help menu */
481               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
482                   && (GTK_MENU_ITEM(child)->right_justify)) 
483                 {
484                   ltr_x = allocation->width -
485                     child_requisition.width - offset;
486                 }
487               if (gtk_widget_get_visible (child))
488                 {
489                   if ((direction == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR))
490                     child_allocation.x = ltr_x;
491                   else
492                     child_allocation.x = allocation->width -
493                       child_requisition.width - ltr_x; 
494                   
495                   child_allocation.width = child_requisition.width;
496                   
497                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
498                                                       toggle_size);
499                   gtk_widget_size_allocate (child, &child_allocation);
500                   
501                   ltr_x += child_allocation.width;
502                 }
503             }
504         }
505       else
506         {
507           child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
508
509           offset = child_allocation.y;  /* Window edge to menubar start */
510           ltr_y = child_allocation.y;
511
512           children = menu_shell->priv->children;
513           while (children)
514             {
515               gint toggle_size;
516
517               child = children->data;
518               children = children->next;
519               
520               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
521                                                  &toggle_size);
522               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
523
524               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
525                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
526                 child_requisition.width += toggle_size;
527               else
528                 child_requisition.height += toggle_size;
529               
530               /* Support for the right justified help menu */
531               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
532                   && (GTK_MENU_ITEM(child)->right_justify)) 
533                 {
534                   ltr_y = allocation->height -
535                     child_requisition.height - offset;
536                 }
537               if (gtk_widget_get_visible (child))
538                 {
539                   if ((direction == GTK_TEXT_DIR_LTR) ==
540                       (priv->pack_direction == GTK_PACK_DIRECTION_TTB))
541                     child_allocation.y = ltr_y;
542                   else
543                     child_allocation.y = allocation->height -
544                       child_requisition.height - ltr_y; 
545                   child_allocation.height = child_requisition.height;
546                   
547                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
548                                                       toggle_size);
549                   gtk_widget_size_allocate (child, &child_allocation);
550                   
551                   ltr_y += child_allocation.height;
552                 }
553             }
554         }
555     }
556 }
557
558 static gint
559 gtk_menu_bar_draw (GtkWidget *widget,
560                    cairo_t   *cr)
561 {
562   GtkStyleContext *context;
563   GtkStateFlags state;
564   int border;
565
566   border = gtk_container_get_border_width (GTK_CONTAINER (widget));
567   context = gtk_widget_get_style_context (widget);
568
569   state = gtk_widget_get_state_flags (widget);
570   gtk_style_context_set_state (context, state);
571
572   if (get_shadow_type (GTK_MENU_BAR (widget)) != GTK_SHADOW_NONE)
573     gtk_render_background (context, cr,
574                            border, border,
575                            gtk_widget_get_allocated_width (widget) - border * 2,
576                            gtk_widget_get_allocated_height (widget) - border * 2);
577
578   gtk_render_frame (context, cr,
579                     border, border,
580                     gtk_widget_get_allocated_width (widget) - border * 2,
581                     gtk_widget_get_allocated_height (widget) - border * 2);
582
583   GTK_WIDGET_CLASS (gtk_menu_bar_parent_class)->draw (widget, cr);
584
585   return FALSE;
586 }
587
588 static GList *
589 get_menu_bars (GtkWindow *window)
590 {
591   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
592 }
593
594 static GList *
595 get_viewable_menu_bars (GtkWindow *window)
596 {
597   GList *menu_bars;
598   GList *viewable_menu_bars = NULL;
599
600   for (menu_bars = get_menu_bars (window);
601        menu_bars;
602        menu_bars = menu_bars->next)
603     {
604       GtkWidget *widget = menu_bars->data;
605       gboolean viewable = TRUE;
606       
607       while (widget)
608         {
609           if (!gtk_widget_get_mapped (widget))
610             viewable = FALSE;
611
612           widget = gtk_widget_get_parent (widget);
613         }
614
615       if (viewable)
616         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
617     }
618
619   return g_list_reverse (viewable_menu_bars);
620 }
621
622 static void
623 set_menu_bars (GtkWindow *window,
624                GList     *menubars)
625 {
626   g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
627 }
628
629 static gboolean
630 window_key_press_handler (GtkWidget   *widget,
631                           GdkEventKey *event,
632                           gpointer     data)
633 {
634   gchar *accel = NULL;
635   gboolean retval = FALSE;
636   
637   g_object_get (gtk_widget_get_settings (widget),
638                 "gtk-menu-bar-accel", &accel,
639                 NULL);
640
641   if (accel && *accel)
642     {
643       guint keyval = 0;
644       GdkModifierType mods = 0;
645
646       gtk_accelerator_parse (accel, &keyval, &mods);
647
648       if (keyval == 0)
649         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
650
651       /* FIXME this is wrong, needs to be in the global accel resolution
652        * thing, to properly consider i18n etc., but that probably requires
653        * AccelGroup changes etc.
654        */
655       if (event->keyval == keyval &&
656           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
657            (mods & gtk_accelerator_get_default_mod_mask ())))
658         {
659           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
660           GList *menubars;
661
662           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
663                                                 GTK_DIR_TAB_FORWARD, NULL);
664           g_list_free (tmp_menubars);
665           
666           if (menubars)
667             {
668               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
669
670               _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
671               _gtk_menu_shell_activate (menu_shell);
672               gtk_menu_shell_select_first (menu_shell, FALSE);
673               
674               g_list_free (menubars);
675               
676               retval = TRUE;          
677             }
678         }
679     }
680
681   g_free (accel);
682
683   return retval;
684 }
685
686 static void
687 add_to_window (GtkWindow  *window,
688                GtkMenuBar *menubar)
689 {
690   GList *menubars = get_menu_bars (window);
691
692   if (!menubars)
693     {
694       g_signal_connect (window,
695                         "key-press-event",
696                         G_CALLBACK (window_key_press_handler),
697                         NULL);
698     }
699
700   set_menu_bars (window, g_list_prepend (menubars, menubar));
701 }
702
703 static void
704 remove_from_window (GtkWindow  *window,
705                     GtkMenuBar *menubar)
706 {
707   GList *menubars = get_menu_bars (window);
708
709   menubars = g_list_remove (menubars, menubar);
710
711   if (!menubars)
712     {
713       g_signal_handlers_disconnect_by_func (window,
714                                             window_key_press_handler,
715                                             NULL);
716     }
717
718   set_menu_bars (window, menubars);
719 }
720
721 static void
722 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
723                                 GtkWidget *old_toplevel)
724 {
725   GtkWidget *toplevel;  
726   GtkMenuBar *menubar;
727
728   menubar = GTK_MENU_BAR (widget);
729
730   toplevel = gtk_widget_get_toplevel (widget);
731
732   if (old_toplevel)
733     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
734   
735   if (gtk_widget_is_toplevel (toplevel))
736     add_to_window (GTK_WINDOW (toplevel), menubar);
737 }
738
739 /**
740  * _gtk_menu_bar_cycle_focus:
741  * @menubar: a #GtkMenuBar
742  * @dir: direction in which to cycle the focus
743  * 
744  * Move the focus between menubars in the toplevel.
745  **/
746 void
747 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
748                            GtkDirectionType  dir)
749 {
750   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
751   GtkMenuItem *to_activate = NULL;
752
753   if (gtk_widget_is_toplevel (toplevel))
754     {
755       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
756       GList *menubars;
757       GList *current;
758
759       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
760                                             dir, GTK_WIDGET (menubar));
761       g_list_free (tmp_menubars);
762
763       if (menubars)
764         {
765           current = g_list_find (menubars, menubar);
766
767           if (current && current->next)
768             {
769               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
770               if (new_menushell->priv->children)
771                 to_activate = new_menushell->priv->children->data;
772             }
773         }
774           
775       g_list_free (menubars);
776     }
777
778   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
779
780   if (to_activate)
781     g_signal_emit_by_name (to_activate, "activate_item");
782 }
783
784 static GtkShadowType
785 get_shadow_type (GtkMenuBar *menubar)
786 {
787   GtkShadowType shadow_type = GTK_SHADOW_OUT;
788   
789   gtk_widget_style_get (GTK_WIDGET (menubar),
790                         "shadow-type", &shadow_type,
791                         NULL);
792
793   return shadow_type;
794 }
795
796 static gint
797 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
798 {
799   gint popup_delay;
800   
801   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
802                 "gtk-menu-bar-popup-delay", &popup_delay,
803                 NULL);
804
805   return popup_delay;
806 }
807
808 static void
809 gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
810                            GtkMenuDirectionType  direction)
811 {
812   GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
813   GtkTextDirection text_dir;
814   GtkPackDirection pack_dir;
815
816   text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
817   pack_dir = gtk_menu_bar_get_pack_direction (menubar);
818   
819   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
820      {
821       if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
822         {
823           switch (direction) 
824             {      
825             case GTK_MENU_DIR_PREV:
826               direction = GTK_MENU_DIR_NEXT;
827               break;
828             case GTK_MENU_DIR_NEXT:
829               direction = GTK_MENU_DIR_PREV;
830               break;
831             default: ;
832             }
833         }
834     }
835   else
836     {
837       switch (direction) 
838         {
839         case GTK_MENU_DIR_PARENT:
840           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
841             direction = GTK_MENU_DIR_PREV;
842           else
843             direction = GTK_MENU_DIR_NEXT;
844           break;
845         case GTK_MENU_DIR_CHILD:
846           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
847             direction = GTK_MENU_DIR_NEXT;
848           else
849             direction = GTK_MENU_DIR_PREV;
850           break;
851         case GTK_MENU_DIR_PREV:
852           if (text_dir == GTK_TEXT_DIR_RTL)       
853             direction = GTK_MENU_DIR_CHILD;
854           else
855             direction = GTK_MENU_DIR_PARENT;
856           break;
857         case GTK_MENU_DIR_NEXT:
858           if (text_dir == GTK_TEXT_DIR_RTL)       
859             direction = GTK_MENU_DIR_PARENT;
860           else
861             direction = GTK_MENU_DIR_CHILD;
862           break;
863         default: ;
864         }
865     }
866   
867   GTK_MENU_SHELL_CLASS (gtk_menu_bar_parent_class)->move_current (menu_shell, direction);
868 }
869
870 /**
871  * gtk_menu_bar_get_pack_direction:
872  * @menubar: a #GtkMenuBar
873  * 
874  * Retrieves the current pack direction of the menubar. 
875  * See gtk_menu_bar_set_pack_direction().
876  *
877  * Return value: the pack direction
878  *
879  * Since: 2.8
880  */
881 GtkPackDirection
882 gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
883 {
884   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
885                         GTK_PACK_DIRECTION_LTR);
886
887   return menubar->priv->pack_direction;
888 }
889
890 /**
891  * gtk_menu_bar_set_pack_direction:
892  * @menubar: a #GtkMenuBar
893  * @pack_dir: a new #GtkPackDirection
894  * 
895  * Sets how items should be packed inside a menubar.
896  * 
897  * Since: 2.8
898  */
899 void
900 gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
901                                  GtkPackDirection  pack_dir)
902 {
903   GtkMenuBarPrivate *priv;
904   GList *l;
905
906   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
907
908   priv = menubar->priv;
909
910   if (priv->pack_direction != pack_dir)
911     {
912       priv->pack_direction = pack_dir;
913
914       gtk_widget_queue_resize (GTK_WIDGET (menubar));
915
916       for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
917         gtk_widget_queue_resize (GTK_WIDGET (l->data));
918
919       g_object_notify (G_OBJECT (menubar), "pack-direction");
920     }
921 }
922
923 /**
924  * gtk_menu_bar_get_child_pack_direction:
925  * @menubar: a #GtkMenuBar
926  * 
927  * Retrieves the current child pack direction of the menubar.
928  * See gtk_menu_bar_set_child_pack_direction().
929  *
930  * Return value: the child pack direction
931  *
932  * Since: 2.8
933  */
934 GtkPackDirection
935 gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
936 {
937   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
938                         GTK_PACK_DIRECTION_LTR);
939
940   return menubar->priv->child_pack_direction;
941 }
942
943 /**
944  * gtk_menu_bar_set_child_pack_direction:
945  * @menubar: a #GtkMenuBar
946  * @child_pack_dir: a new #GtkPackDirection
947  * 
948  * Sets how widgets should be packed inside the children of a menubar.
949  * 
950  * Since: 2.8
951  */
952 void
953 gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
954                                        GtkPackDirection  child_pack_dir)
955 {
956   GtkMenuBarPrivate *priv;
957   GList *l;
958
959   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
960
961   priv = menubar->priv;
962
963   if (priv->child_pack_direction != child_pack_dir)
964     {
965       priv->child_pack_direction = child_pack_dir;
966
967       gtk_widget_queue_resize (GTK_WIDGET (menubar));
968
969       for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
970         gtk_widget_queue_resize (GTK_WIDGET (l->data));
971
972       g_object_notify (G_OBJECT (menubar), "child-pack-direction");
973     }
974 }