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