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