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