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