]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Fixed a small typo, it should be GTK_WINDOW_GROUP_GET_CLASS and not
[~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 "gdk/gdkkeysyms.h"
28 #include "gtkbindings.h"
29 #include "gtkmain.h"
30 #include "gtkmenubar.h"
31 #include "gtkmenuitem.h"
32 #include "gtksettings.h"
33 #include "gtkintl.h"
34 #include "gtkwindow.h"
35 #include "gtksignal.h"
36
37
38 #define BORDER_SPACING  0
39 #define CHILD_SPACING   3
40 #define DEFAULT_IPADDING 1
41
42 static void gtk_menu_bar_class_init    (GtkMenuBarClass *klass);
43 static void gtk_menu_bar_size_request  (GtkWidget       *widget,
44                                         GtkRequisition  *requisition);
45 static void gtk_menu_bar_size_allocate (GtkWidget       *widget,
46                                         GtkAllocation   *allocation);
47 static void gtk_menu_bar_paint         (GtkWidget       *widget,
48                                         GdkRectangle    *area);
49 static gint gtk_menu_bar_expose        (GtkWidget       *widget,
50                                         GdkEventExpose  *event);
51 static void gtk_menu_bar_hierarchy_changed (GtkWidget   *widget);                                            
52 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
53
54 static GtkMenuShellClass *parent_class = NULL;
55
56 GtkType
57 gtk_menu_bar_get_type (void)
58 {
59   static GtkType menu_bar_type = 0;
60
61   if (!menu_bar_type)
62     {
63       static const GtkTypeInfo menu_bar_info =
64       {
65         "GtkMenuBar",
66         sizeof (GtkMenuBar),
67         sizeof (GtkMenuBarClass),
68         (GtkClassInitFunc) gtk_menu_bar_class_init,
69         (GtkObjectInitFunc) NULL,
70         /* reserved_1 */ NULL,
71         /* reserved_2 */ NULL,
72         (GtkClassInitFunc) NULL,
73       };
74
75       menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
76     }
77
78   return menu_bar_type;
79 }
80
81 static void
82 gtk_menu_bar_class_init (GtkMenuBarClass *class)
83 {
84   GtkObjectClass *object_class;
85   GtkWidgetClass *widget_class;
86   GtkMenuShellClass *menu_shell_class;
87
88   GtkBindingSet *binding_set;
89
90   parent_class = g_type_class_peek_parent (class);
91   
92   object_class = (GtkObjectClass*) class;
93   widget_class = (GtkWidgetClass*) class;
94   menu_shell_class = (GtkMenuShellClass*) class;
95
96   widget_class->size_request = gtk_menu_bar_size_request;
97   widget_class->size_allocate = gtk_menu_bar_size_allocate;
98   widget_class->expose_event = gtk_menu_bar_expose;
99   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
100   
101   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
102
103   binding_set = gtk_binding_set_by_class (class);
104   gtk_binding_entry_add_signal (binding_set,
105                                 GDK_Left, 0,
106                                 "move_current", 1,
107                                 GTK_TYPE_MENU_DIRECTION_TYPE,
108                                 GTK_MENU_DIR_PREV);
109   gtk_binding_entry_add_signal (binding_set,
110                                 GDK_Right, 0,
111                                 "move_current", 1,
112                                 GTK_TYPE_MENU_DIRECTION_TYPE,
113                                 GTK_MENU_DIR_NEXT);
114   gtk_binding_entry_add_signal (binding_set,
115                                 GDK_Up, 0,
116                                 "move_current", 1,
117                                 GTK_TYPE_MENU_DIRECTION_TYPE,
118                                 GTK_MENU_DIR_PARENT);
119   gtk_binding_entry_add_signal (binding_set,
120                                 GDK_Down, 0,
121                                 "move_current", 1,
122                                 GTK_TYPE_MENU_DIRECTION_TYPE,
123                                 GTK_MENU_DIR_CHILD);
124
125   gtk_settings_install_property (gtk_settings_get_global (),
126                                  g_param_spec_string ("gtk-menu-bar-accel",
127                                                       _("Menu bar accelerator"),
128                                                       _("Keybinding to activate the menu bar"),
129                                                       "F10",
130                                                       G_PARAM_READWRITE));
131
132   gtk_widget_class_install_style_property (widget_class,
133                                            g_param_spec_enum ("shadow_type",
134                                                               _("Shadow type"),
135                                                               _("Style of bevel around the menubar"),
136                                                               GTK_TYPE_SHADOW_TYPE,
137                                                               GTK_SHADOW_OUT,
138                                                               G_PARAM_READABLE));
139
140   gtk_widget_class_install_style_property (widget_class,
141                                            g_param_spec_int ("internal_padding",
142                                                              _("Internal padding"),
143                                                              _("Amount of border space between the menubar shadow and the menu items"),
144                                                              0,
145                                                              G_MAXINT,
146                                                              DEFAULT_IPADDING,
147                                                              G_PARAM_READABLE));
148
149 }
150  
151 GtkWidget*
152 gtk_menu_bar_new (void)
153 {
154   return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
155 }
156
157 void
158 gtk_menu_bar_append (GtkMenuBar *menu_bar,
159                      GtkWidget  *child)
160 {
161   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
162 }
163
164 void
165 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
166                       GtkWidget  *child)
167 {
168   gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
169 }
170
171 void
172 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
173                      GtkWidget  *child,
174                      gint        position)
175 {
176   gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
177 }
178
179
180 static void
181 gtk_menu_bar_size_request (GtkWidget      *widget,
182                            GtkRequisition *requisition)
183 {
184   GtkMenuBar *menu_bar;
185   GtkMenuShell *menu_shell;
186   GtkWidget *child;
187   GList *children;
188   gint nchildren;
189   GtkRequisition child_requisition;
190   gint ipadding;
191
192   g_return_if_fail (widget != NULL);
193   g_return_if_fail (GTK_IS_MENU_BAR (widget));
194   g_return_if_fail (requisition != NULL);
195
196   requisition->width = 0;
197   requisition->height = 0;
198   
199   if (GTK_WIDGET_VISIBLE (widget))
200     {
201       menu_bar = GTK_MENU_BAR (widget);
202       menu_shell = GTK_MENU_SHELL (widget);
203
204       nchildren = 0;
205       children = menu_shell->children;
206
207       while (children)
208         {
209           child = children->data;
210           children = children->next;
211
212           if (GTK_WIDGET_VISIBLE (child))
213             {
214               gint toggle_size;
215               
216               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
217               gtk_widget_size_request (child, &child_requisition);
218               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
219                                                  &toggle_size);
220               
221               requisition->width += child_requisition.width;
222               requisition->width += toggle_size;
223               
224               requisition->height = MAX (requisition->height, child_requisition.height);
225               /* Support for the right justified help menu */
226               if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
227                   GTK_MENU_ITEM(child)->right_justify)
228                 {
229                   requisition->width += CHILD_SPACING;
230                 }
231
232               nchildren += 1;
233             }
234         }
235
236       gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
237       
238       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
239                              widget->style->xthickness +
240                              ipadding + 
241                              BORDER_SPACING) * 2;
242       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
243                               widget->style->ythickness +
244                               ipadding +
245                               BORDER_SPACING) * 2;
246
247       if (nchildren > 0)
248         requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
249     }
250 }
251
252 static void
253 gtk_menu_bar_size_allocate (GtkWidget     *widget,
254                             GtkAllocation *allocation)
255 {
256   GtkMenuBar *menu_bar;
257   GtkMenuShell *menu_shell;
258   GtkWidget *child;
259   GList *children;
260   GtkAllocation child_allocation;
261   GtkRequisition child_requisition;
262   guint offset;
263   gint ipadding;
264   
265   g_return_if_fail (widget != NULL);
266   g_return_if_fail (GTK_IS_MENU_BAR (widget));
267   g_return_if_fail (allocation != NULL);
268
269   menu_bar = GTK_MENU_BAR (widget);
270   menu_shell = GTK_MENU_SHELL (widget);
271
272   widget->allocation = *allocation;
273   if (GTK_WIDGET_REALIZED (widget))
274     gdk_window_move_resize (widget->window,
275                             allocation->x, allocation->y,
276                             allocation->width, allocation->height);
277
278   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
279   
280   if (menu_shell->children)
281     {
282       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
283                             widget->style->xthickness +
284                             ipadding + 
285                             BORDER_SPACING);
286       offset = child_allocation.x;      /* Window edge to menubar start */
287
288       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
289                             widget->style->ythickness +
290                             ipadding +
291                             BORDER_SPACING);
292       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
293
294       children = menu_shell->children;
295       while (children)
296         {
297           gint toggle_size;          
298
299           child = children->data;
300           children = children->next;
301
302           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
303                                              &toggle_size);
304           gtk_widget_get_child_requisition (child, &child_requisition);
305
306           child_requisition.width += toggle_size;
307           
308           /* Support for the right justified help menu */
309           if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
310               && (GTK_MENU_ITEM(child)->right_justify)) 
311             {
312               child_allocation.x = allocation->width -
313                   child_requisition.width - CHILD_SPACING - offset;
314             }
315           if (GTK_WIDGET_VISIBLE (child))
316             {
317               child_allocation.width = child_requisition.width;
318
319               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
320                                                   toggle_size);
321               gtk_widget_size_allocate (child, &child_allocation);
322
323               child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
324             }
325         }
326     }
327 }
328
329 static void
330 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
331 {
332   g_return_if_fail (widget != NULL);
333   g_return_if_fail (GTK_IS_MENU_BAR (widget));
334
335   if (GTK_WIDGET_DRAWABLE (widget))
336     {
337       gint border;
338
339       border = GTK_CONTAINER (widget)->border_width;
340       
341       gtk_paint_box (widget->style,
342                      widget->window,
343                      GTK_WIDGET_STATE (widget),
344                      get_shadow_type (GTK_MENU_BAR (widget)),
345                      area, widget, "menubar",
346                      border, border,
347                      widget->allocation.width - border * 2,
348                      widget->allocation.height - border * 2);
349     }
350 }
351
352 static gint
353 gtk_menu_bar_expose (GtkWidget      *widget,
354                      GdkEventExpose *event)
355 {
356   g_return_val_if_fail (widget != NULL, FALSE);
357   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
358   g_return_val_if_fail (event != NULL, FALSE);
359
360   if (GTK_WIDGET_DRAWABLE (widget))
361     {
362       gtk_menu_bar_paint (widget, &event->area);
363
364       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
365     }
366
367   return FALSE;
368 }
369
370 static gboolean
371 window_key_press_handler (GtkWidget   *widget,
372                           GdkEventKey *event,
373                           gpointer     data)
374 {
375   gchar *accel = NULL;
376   gboolean retval = FALSE;
377   
378   g_object_get (G_OBJECT (gtk_settings_get_global ()),
379                 "gtk-menu-bar-accel",
380                 &accel,
381                 NULL);
382
383   if (accel)
384     {
385       guint keyval = 0;
386       GdkModifierType mods = 0;
387
388       gtk_accelerator_parse (accel, &keyval, &mods);
389
390       if (keyval == 0)
391         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
392
393       /* FIXME this is wrong, needs to be in the global accel resolution
394        * thing, to properly consider i18n etc., but that probably requires
395        * AccelGroup changes etc.
396        */
397       if (event->keyval == keyval &&
398           (mods & event->state) == mods)
399         {
400           GtkMenuBar *menubar;
401           GtkMenuShell *menushell;
402           
403           menubar = GTK_MENU_BAR (data);
404           menushell = GTK_MENU_SHELL (menubar);
405
406           if (menushell->children)
407             {
408               gtk_signal_emit_by_name (GTK_OBJECT (menushell->children->data),
409                                        "activate_item");
410               
411               retval = TRUE;
412             }
413         }
414
415       g_free (accel);
416     }
417
418   return retval;
419 }
420
421 static void
422 add_to_window (GtkWindow  *window,
423                GtkMenuBar *menubar)
424 {
425   GtkMenuBar *old_menubar;
426
427   old_menubar = g_object_get_data (G_OBJECT (window),
428                                    "gtk-menu-bar");
429   
430   if (old_menubar)
431     return; /* ignore this case; app programmer on crack, but
432              * shouldn't spew stuff, just don't support the accel
433              * for menubar #2
434              */
435
436   g_object_set_data (G_OBJECT (window),
437                      "gtk-menu-bar",
438                      menubar);
439
440   g_signal_connect_data (G_OBJECT (window),
441                          "key_press_event",
442                          G_CALLBACK (window_key_press_handler),
443                          menubar,
444                          NULL, FALSE, FALSE);
445
446   menubar->toplevel = GTK_WIDGET (window);
447 }
448
449 /* Hack-around */
450 #define g_signal_handlers_disconnect_by_func(obj, func, data) g_signal_handlers_disconnect_matched (obj, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL, func, data)
451
452 static void
453 remove_from_window (GtkWindow  *window,
454                     GtkMenuBar *menubar)
455 {
456   g_return_if_fail (menubar->toplevel == GTK_WIDGET (window));
457
458   g_signal_handlers_disconnect_by_func (G_OBJECT (window),
459                                         G_CALLBACK (window_key_press_handler),
460                                         menubar);
461
462   /* dnotify zeroes menubar->toplevel */
463   g_object_set_data (G_OBJECT (window),
464                      "gtk-menu-bar",
465                      NULL);
466
467   menubar->toplevel = NULL;
468 }
469
470 static void
471 gtk_menu_bar_hierarchy_changed (GtkWidget *widget)
472 {
473   GtkWidget *toplevel;  
474   GtkMenuBar *menubar;
475
476   menubar = GTK_MENU_BAR (widget);
477
478   toplevel = gtk_widget_get_toplevel (widget);
479
480   if (menubar->toplevel &&
481       toplevel != menubar->toplevel)
482     {
483       remove_from_window (GTK_WINDOW (menubar->toplevel),
484                           menubar);
485     }
486   
487   if (toplevel &&
488       GTK_IS_WINDOW (toplevel))
489     {
490       add_to_window (GTK_WINDOW (toplevel),
491                      menubar);
492     }
493 }
494
495 static GtkShadowType
496 get_shadow_type (GtkMenuBar *menubar)
497 {
498   GtkShadowType shadow_type = GTK_SHADOW_OUT;
499   
500   gtk_widget_style_get (GTK_WIDGET (menubar),
501                         "shadow_type", &shadow_type,
502                         NULL);
503
504
505   return shadow_type;
506 }