]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
menubar: Query only the necessary size from the child
[~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 get_preferred_size_for_size (GtkWidget      *widget,
295                              GtkOrientation  orientation,
296                              gint            size,
297                              gint           *minimum,
298                              gint           *natural)
299 {
300   if (orientation == GTK_ORIENTATION_HORIZONTAL)
301     if (size < 0)
302       gtk_widget_get_preferred_width (widget, minimum, natural);
303     else
304       gtk_widget_get_preferred_width_for_height (widget, size, minimum, natural);
305   else
306     if (size < 0)
307       gtk_widget_get_preferred_height (widget, minimum, natural);
308     else
309       gtk_widget_get_preferred_height_for_width (widget, size, minimum, natural);
310 }
311
312 static void
313 gtk_menu_bar_size_request (GtkWidget      *widget,
314                            GtkOrientation  orientation,
315                            gint           *minimum,
316                            gint           *natural)
317 {
318   GtkMenuBar *menu_bar;
319   GtkMenuBarPrivate *priv;
320   GtkMenuShell *menu_shell;
321   GtkWidget *child;
322   GList *children;
323   gint ipadding;
324   guint border_width;
325   gboolean use_toggle_size, use_maximize;
326   gint child_size, size = 0;
327
328   menu_bar = GTK_MENU_BAR (widget);
329   menu_shell = GTK_MENU_SHELL (widget);
330   priv = menu_bar->priv;
331
332   children = menu_shell->priv->children;
333
334   if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
335       priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
336     use_toggle_size = (orientation == GTK_ORIENTATION_HORIZONTAL);
337   else
338     use_toggle_size = (orientation == GTK_ORIENTATION_VERTICAL);
339
340   if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
341       priv->pack_direction == GTK_PACK_DIRECTION_RTL)
342     use_maximize = (orientation == GTK_ORIENTATION_VERTICAL);
343   else
344     use_maximize = (orientation == GTK_ORIENTATION_HORIZONTAL);
345
346   while (children)
347     {
348       child = children->data;
349       children = children->next;
350
351       if (gtk_widget_get_visible (child))
352         {
353           get_preferred_size_for_size (child, orientation, -1, &child_size, NULL);
354
355           if (use_toggle_size)
356             {
357               gint toggle_size;
358
359               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
360                                                  &toggle_size);
361
362               child_size += toggle_size;
363             }
364
365           if (use_maximize)
366             size = MAX (size, child_size);
367           else
368             size += child_size;
369         }
370     }
371
372   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
373
374   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
375   size += (border_width + ipadding + BORDER_SPACING) * 2;
376
377   if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
378     {
379       GtkStyleContext *context;
380       GtkBorder *border;
381
382       context = gtk_widget_get_style_context (widget);
383
384       gtk_style_context_get (context, 0,
385                              "border-width", &border,
386                              NULL);
387
388       if (orientation == GTK_ORIENTATION_HORIZONTAL)
389         size += border->left + border->right;
390       else
391         size += border->top + border->bottom;
392       gtk_border_free (border);
393     }
394
395   *minimum = *natural = size;
396 }
397
398 static void
399 gtk_menu_bar_get_preferred_width (GtkWidget *widget,
400                                   gint      *minimum,
401                                   gint      *natural)
402 {
403   gtk_menu_bar_size_request (widget, GTK_ORIENTATION_HORIZONTAL, minimum, natural);
404 }
405
406 static void
407 gtk_menu_bar_get_preferred_height (GtkWidget *widget,
408                                    gint      *minimum,
409                                    gint      *natural)
410 {
411   gtk_menu_bar_size_request (widget, GTK_ORIENTATION_VERTICAL, minimum, natural);
412 }
413
414 static void
415 gtk_menu_bar_size_allocate (GtkWidget     *widget,
416                             GtkAllocation *allocation)
417 {
418   GtkMenuBar *menu_bar;
419   GtkMenuShell *menu_shell;
420   GtkMenuBarPrivate *priv;
421   GtkWidget *child;
422   GList *children;
423   GtkAllocation child_allocation;
424   GtkRequisition child_requisition;
425   guint offset;
426   GtkTextDirection direction;
427   gint ltr_x, ltr_y;
428   gint ipadding;
429   guint border_width;
430
431   g_return_if_fail (GTK_IS_MENU_BAR (widget));
432   g_return_if_fail (allocation != NULL);
433
434   menu_bar = GTK_MENU_BAR (widget);
435   menu_shell = GTK_MENU_SHELL (widget);
436   priv = menu_bar->priv;
437
438   direction = gtk_widget_get_direction (widget);
439
440   gtk_widget_set_allocation (widget, allocation);
441
442   if (gtk_widget_get_realized (widget))
443     gdk_window_move_resize (gtk_widget_get_window (widget),
444                             allocation->x, allocation->y,
445                             allocation->width, allocation->height);
446
447   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
448   
449   if (menu_shell->priv->children)
450     {
451       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
452       child_allocation.x = (border_width +
453                             ipadding + 
454                             BORDER_SPACING);
455       child_allocation.y = (border_width +
456                             BORDER_SPACING);
457       
458       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
459         {
460           GtkStyleContext *context;
461           GtkBorder *border;
462
463           context = gtk_widget_get_style_context (widget);
464           gtk_style_context_get (context, 0,
465                                  "border-width", &border,
466                                  NULL);
467
468           child_allocation.x += border->left;
469           child_allocation.y += border->top;
470
471           gtk_border_free (border);
472         }
473       
474       if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
475           priv->pack_direction == GTK_PACK_DIRECTION_RTL)
476         {
477           child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
478
479           offset = child_allocation.x;  /* Window edge to menubar start */
480           ltr_x = child_allocation.x;
481
482           children = menu_shell->priv->children;
483           while (children)
484             {
485               gint toggle_size;
486
487               child = children->data;
488               children = children->next;
489               
490               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
491                                                  &toggle_size);
492               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
493
494               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
495                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
496                 child_requisition.width += toggle_size;
497               else
498                 child_requisition.height += toggle_size;
499
500               /* Support for the right justified help menu */
501               if (children == NULL &&
502                   GTK_IS_MENU_ITEM (child) &&
503                   GTK_MENU_ITEM (child)->priv->right_justify)
504                 {
505                   ltr_x = allocation->width -
506                     child_requisition.width - offset;
507                 }
508               if (gtk_widget_get_visible (child))
509                 {
510                   if ((direction == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR))
511                     child_allocation.x = ltr_x;
512                   else
513                     child_allocation.x = allocation->width -
514                       child_requisition.width - ltr_x; 
515                   
516                   child_allocation.width = child_requisition.width;
517                   
518                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
519                                                       toggle_size);
520                   gtk_widget_size_allocate (child, &child_allocation);
521                   
522                   ltr_x += child_allocation.width;
523                 }
524             }
525         }
526       else
527         {
528           child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
529
530           offset = child_allocation.y;  /* Window edge to menubar start */
531           ltr_y = child_allocation.y;
532
533           children = menu_shell->priv->children;
534           while (children)
535             {
536               gint toggle_size;
537
538               child = children->data;
539               children = children->next;
540               
541               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
542                                                  &toggle_size);
543               gtk_widget_get_preferred_size (child, &child_requisition, NULL);
544
545               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
546                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
547                 child_requisition.width += toggle_size;
548               else
549                 child_requisition.height += toggle_size;
550
551               /* Support for the right justified help menu */
552               if (children == NULL &&
553                   GTK_IS_MENU_ITEM (child) &&
554                   GTK_MENU_ITEM (child)->priv->right_justify)
555                 {
556                   ltr_y = allocation->height -
557                     child_requisition.height - offset;
558                 }
559               if (gtk_widget_get_visible (child))
560                 {
561                   if ((direction == GTK_TEXT_DIR_LTR) ==
562                       (priv->pack_direction == GTK_PACK_DIRECTION_TTB))
563                     child_allocation.y = ltr_y;
564                   else
565                     child_allocation.y = allocation->height -
566                       child_requisition.height - ltr_y; 
567                   child_allocation.height = child_requisition.height;
568                   
569                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
570                                                       toggle_size);
571                   gtk_widget_size_allocate (child, &child_allocation);
572                   
573                   ltr_y += child_allocation.height;
574                 }
575             }
576         }
577     }
578 }
579
580 static gint
581 gtk_menu_bar_draw (GtkWidget *widget,
582                    cairo_t   *cr)
583 {
584   GtkStyleContext *context;
585   GtkStateFlags state;
586   int border;
587
588   border = gtk_container_get_border_width (GTK_CONTAINER (widget));
589   context = gtk_widget_get_style_context (widget);
590
591   state = gtk_widget_get_state_flags (widget);
592   gtk_style_context_set_state (context, state);
593
594   if (get_shadow_type (GTK_MENU_BAR (widget)) != GTK_SHADOW_NONE)
595     gtk_render_background (context, cr,
596                            border, border,
597                            gtk_widget_get_allocated_width (widget) - border * 2,
598                            gtk_widget_get_allocated_height (widget) - border * 2);
599
600   gtk_render_frame (context, cr,
601                     border, border,
602                     gtk_widget_get_allocated_width (widget) - border * 2,
603                     gtk_widget_get_allocated_height (widget) - border * 2);
604
605   GTK_WIDGET_CLASS (gtk_menu_bar_parent_class)->draw (widget, cr);
606
607   return FALSE;
608 }
609
610 static GList *
611 get_menu_bars (GtkWindow *window)
612 {
613   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
614 }
615
616 static GList *
617 get_viewable_menu_bars (GtkWindow *window)
618 {
619   GList *menu_bars;
620   GList *viewable_menu_bars = NULL;
621
622   for (menu_bars = get_menu_bars (window);
623        menu_bars;
624        menu_bars = menu_bars->next)
625     {
626       GtkWidget *widget = menu_bars->data;
627       gboolean viewable = TRUE;
628       
629       while (widget)
630         {
631           if (!gtk_widget_get_mapped (widget))
632             viewable = FALSE;
633
634           widget = gtk_widget_get_parent (widget);
635         }
636
637       if (viewable)
638         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
639     }
640
641   return g_list_reverse (viewable_menu_bars);
642 }
643
644 static void
645 set_menu_bars (GtkWindow *window,
646                GList     *menubars)
647 {
648   g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
649 }
650
651 static gboolean
652 window_key_press_handler (GtkWidget   *widget,
653                           GdkEventKey *event,
654                           gpointer     data)
655 {
656   gchar *accel = NULL;
657   gboolean retval = FALSE;
658   
659   g_object_get (gtk_widget_get_settings (widget),
660                 "gtk-menu-bar-accel", &accel,
661                 NULL);
662
663   if (accel && *accel)
664     {
665       guint keyval = 0;
666       GdkModifierType mods = 0;
667
668       gtk_accelerator_parse (accel, &keyval, &mods);
669
670       if (keyval == 0)
671         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
672
673       /* FIXME this is wrong, needs to be in the global accel resolution
674        * thing, to properly consider i18n etc., but that probably requires
675        * AccelGroup changes etc.
676        */
677       if (event->keyval == keyval &&
678           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
679            (mods & gtk_accelerator_get_default_mod_mask ())))
680         {
681           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
682           GList *menubars;
683
684           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
685                                                 GTK_DIR_TAB_FORWARD, NULL);
686           g_list_free (tmp_menubars);
687           
688           if (menubars)
689             {
690               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
691
692               _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
693               gtk_menu_shell_select_first (menu_shell, FALSE);
694               
695               g_list_free (menubars);
696               
697               retval = TRUE;          
698             }
699         }
700     }
701
702   g_free (accel);
703
704   return retval;
705 }
706
707 static void
708 add_to_window (GtkWindow  *window,
709                GtkMenuBar *menubar)
710 {
711   GList *menubars = get_menu_bars (window);
712
713   if (!menubars)
714     {
715       g_signal_connect (window,
716                         "key-press-event",
717                         G_CALLBACK (window_key_press_handler),
718                         NULL);
719     }
720
721   set_menu_bars (window, g_list_prepend (menubars, menubar));
722 }
723
724 static void
725 remove_from_window (GtkWindow  *window,
726                     GtkMenuBar *menubar)
727 {
728   GList *menubars = get_menu_bars (window);
729
730   menubars = g_list_remove (menubars, menubar);
731
732   if (!menubars)
733     {
734       g_signal_handlers_disconnect_by_func (window,
735                                             window_key_press_handler,
736                                             NULL);
737     }
738
739   set_menu_bars (window, menubars);
740 }
741
742 static void
743 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
744                                 GtkWidget *old_toplevel)
745 {
746   GtkWidget *toplevel;  
747   GtkMenuBar *menubar;
748
749   menubar = GTK_MENU_BAR (widget);
750
751   toplevel = gtk_widget_get_toplevel (widget);
752
753   if (old_toplevel)
754     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
755   
756   if (gtk_widget_is_toplevel (toplevel))
757     add_to_window (GTK_WINDOW (toplevel), menubar);
758 }
759
760 /**
761  * _gtk_menu_bar_cycle_focus:
762  * @menubar: a #GtkMenuBar
763  * @dir: direction in which to cycle the focus
764  * 
765  * Move the focus between menubars in the toplevel.
766  **/
767 void
768 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
769                            GtkDirectionType  dir)
770 {
771   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
772   GtkMenuItem *to_activate = NULL;
773
774   if (gtk_widget_is_toplevel (toplevel))
775     {
776       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
777       GList *menubars;
778       GList *current;
779
780       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
781                                             dir, GTK_WIDGET (menubar));
782       g_list_free (tmp_menubars);
783
784       if (menubars)
785         {
786           current = g_list_find (menubars, menubar);
787
788           if (current && current->next)
789             {
790               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
791               if (new_menushell->priv->children)
792                 to_activate = new_menushell->priv->children->data;
793             }
794         }
795           
796       g_list_free (menubars);
797     }
798
799   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
800
801   if (to_activate)
802     g_signal_emit_by_name (to_activate, "activate_item");
803 }
804
805 static GtkShadowType
806 get_shadow_type (GtkMenuBar *menubar)
807 {
808   GtkShadowType shadow_type = GTK_SHADOW_OUT;
809   
810   gtk_widget_style_get (GTK_WIDGET (menubar),
811                         "shadow-type", &shadow_type,
812                         NULL);
813
814   return shadow_type;
815 }
816
817 static gint
818 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
819 {
820   gint popup_delay;
821   
822   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
823                 "gtk-menu-bar-popup-delay", &popup_delay,
824                 NULL);
825
826   return popup_delay;
827 }
828
829 static void
830 gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
831                            GtkMenuDirectionType  direction)
832 {
833   GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
834   GtkTextDirection text_dir;
835   GtkPackDirection pack_dir;
836
837   text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
838   pack_dir = gtk_menu_bar_get_pack_direction (menubar);
839   
840   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
841      {
842       if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
843         {
844           switch (direction) 
845             {      
846             case GTK_MENU_DIR_PREV:
847               direction = GTK_MENU_DIR_NEXT;
848               break;
849             case GTK_MENU_DIR_NEXT:
850               direction = GTK_MENU_DIR_PREV;
851               break;
852             default: ;
853             }
854         }
855     }
856   else
857     {
858       switch (direction) 
859         {
860         case GTK_MENU_DIR_PARENT:
861           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
862             direction = GTK_MENU_DIR_PREV;
863           else
864             direction = GTK_MENU_DIR_NEXT;
865           break;
866         case GTK_MENU_DIR_CHILD:
867           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
868             direction = GTK_MENU_DIR_NEXT;
869           else
870             direction = GTK_MENU_DIR_PREV;
871           break;
872         case GTK_MENU_DIR_PREV:
873           if (text_dir == GTK_TEXT_DIR_RTL)       
874             direction = GTK_MENU_DIR_CHILD;
875           else
876             direction = GTK_MENU_DIR_PARENT;
877           break;
878         case GTK_MENU_DIR_NEXT:
879           if (text_dir == GTK_TEXT_DIR_RTL)       
880             direction = GTK_MENU_DIR_PARENT;
881           else
882             direction = GTK_MENU_DIR_CHILD;
883           break;
884         default: ;
885         }
886     }
887   
888   GTK_MENU_SHELL_CLASS (gtk_menu_bar_parent_class)->move_current (menu_shell, direction);
889 }
890
891 /**
892  * gtk_menu_bar_get_pack_direction:
893  * @menubar: a #GtkMenuBar
894  * 
895  * Retrieves the current pack direction of the menubar. 
896  * See gtk_menu_bar_set_pack_direction().
897  *
898  * Return value: the pack direction
899  *
900  * Since: 2.8
901  */
902 GtkPackDirection
903 gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
904 {
905   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
906                         GTK_PACK_DIRECTION_LTR);
907
908   return menubar->priv->pack_direction;
909 }
910
911 /**
912  * gtk_menu_bar_set_pack_direction:
913  * @menubar: a #GtkMenuBar
914  * @pack_dir: a new #GtkPackDirection
915  * 
916  * Sets how items should be packed inside a menubar.
917  * 
918  * Since: 2.8
919  */
920 void
921 gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
922                                  GtkPackDirection  pack_dir)
923 {
924   GtkMenuBarPrivate *priv;
925   GList *l;
926
927   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
928
929   priv = menubar->priv;
930
931   if (priv->pack_direction != pack_dir)
932     {
933       priv->pack_direction = pack_dir;
934
935       gtk_widget_queue_resize (GTK_WIDGET (menubar));
936
937       for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
938         gtk_widget_queue_resize (GTK_WIDGET (l->data));
939
940       g_object_notify (G_OBJECT (menubar), "pack-direction");
941     }
942 }
943
944 /**
945  * gtk_menu_bar_get_child_pack_direction:
946  * @menubar: a #GtkMenuBar
947  * 
948  * Retrieves the current child pack direction of the menubar.
949  * See gtk_menu_bar_set_child_pack_direction().
950  *
951  * Return value: the child pack direction
952  *
953  * Since: 2.8
954  */
955 GtkPackDirection
956 gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
957 {
958   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
959                         GTK_PACK_DIRECTION_LTR);
960
961   return menubar->priv->child_pack_direction;
962 }
963
964 /**
965  * gtk_menu_bar_set_child_pack_direction:
966  * @menubar: a #GtkMenuBar
967  * @child_pack_dir: a new #GtkPackDirection
968  * 
969  * Sets how widgets should be packed inside the children of a menubar.
970  * 
971  * Since: 2.8
972  */
973 void
974 gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
975                                        GtkPackDirection  child_pack_dir)
976 {
977   GtkMenuBarPrivate *priv;
978   GList *l;
979
980   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
981
982   priv = menubar->priv;
983
984   if (priv->child_pack_direction != child_pack_dir)
985     {
986       priv->child_pack_direction = child_pack_dir;
987
988       gtk_widget_queue_resize (GTK_WIDGET (menubar));
989
990       for (l = GTK_MENU_SHELL (menubar)->priv->children; l; l = l->next)
991         gtk_widget_queue_resize (GTK_WIDGET (l->data));
992
993       g_object_notify (G_OBJECT (menubar), "child-pack-direction");
994     }
995 }