1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gdk/gdkkeysyms.h"
28 #include "gtkbindings.h"
30 #include "gtkmenubar.h"
31 #include "gtkmenuitem.h"
32 #include "gtksettings.h"
34 #include "gtkwindow.h"
35 #include "gtksignal.h"
38 #define BORDER_SPACING 0
39 #define CHILD_SPACING 3
40 #define DEFAULT_IPADDING 1
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,
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);
54 static GtkMenuShellClass *parent_class = NULL;
57 gtk_menu_bar_get_type (void)
59 static GtkType menu_bar_type = 0;
63 static const GtkTypeInfo menu_bar_info =
67 sizeof (GtkMenuBarClass),
68 (GtkClassInitFunc) gtk_menu_bar_class_init,
69 (GtkObjectInitFunc) NULL,
70 /* reserved_1 */ NULL,
71 /* reserved_2 */ NULL,
72 (GtkClassInitFunc) NULL,
75 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
82 gtk_menu_bar_class_init (GtkMenuBarClass *class)
84 GtkObjectClass *object_class;
85 GtkWidgetClass *widget_class;
86 GtkMenuShellClass *menu_shell_class;
88 GtkBindingSet *binding_set;
90 parent_class = g_type_class_peek_parent (class);
92 object_class = (GtkObjectClass*) class;
93 widget_class = (GtkWidgetClass*) class;
94 menu_shell_class = (GtkMenuShellClass*) class;
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;
101 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
103 binding_set = gtk_binding_set_by_class (class);
104 gtk_binding_entry_add_signal (binding_set,
107 GTK_TYPE_MENU_DIRECTION_TYPE,
109 gtk_binding_entry_add_signal (binding_set,
112 GTK_TYPE_MENU_DIRECTION_TYPE,
114 gtk_binding_entry_add_signal (binding_set,
117 GTK_TYPE_MENU_DIRECTION_TYPE,
118 GTK_MENU_DIR_PARENT);
119 gtk_binding_entry_add_signal (binding_set,
122 GTK_TYPE_MENU_DIRECTION_TYPE,
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"),
132 gtk_widget_class_install_style_property (widget_class,
133 g_param_spec_enum ("shadow_type",
135 _("Style of bevel around the menubar"),
136 GTK_TYPE_SHADOW_TYPE,
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"),
152 gtk_menu_bar_new (void)
154 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
158 gtk_menu_bar_append (GtkMenuBar *menu_bar,
161 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
165 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
168 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
172 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
176 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
181 gtk_menu_bar_size_request (GtkWidget *widget,
182 GtkRequisition *requisition)
184 GtkMenuBar *menu_bar;
185 GtkMenuShell *menu_shell;
189 GtkRequisition child_requisition;
192 g_return_if_fail (widget != NULL);
193 g_return_if_fail (GTK_IS_MENU_BAR (widget));
194 g_return_if_fail (requisition != NULL);
196 requisition->width = 0;
197 requisition->height = 0;
199 if (GTK_WIDGET_VISIBLE (widget))
201 menu_bar = GTK_MENU_BAR (widget);
202 menu_shell = GTK_MENU_SHELL (widget);
205 children = menu_shell->children;
209 child = children->data;
210 children = children->next;
212 if (GTK_WIDGET_VISIBLE (child))
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),
221 requisition->width += child_requisition.width;
222 requisition->width += toggle_size;
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)
229 requisition->width += CHILD_SPACING;
236 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
238 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
239 widget->style->xthickness +
242 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
243 widget->style->ythickness +
248 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
253 gtk_menu_bar_size_allocate (GtkWidget *widget,
254 GtkAllocation *allocation)
256 GtkMenuBar *menu_bar;
257 GtkMenuShell *menu_shell;
260 GtkAllocation child_allocation;
261 GtkRequisition child_requisition;
265 g_return_if_fail (widget != NULL);
266 g_return_if_fail (GTK_IS_MENU_BAR (widget));
267 g_return_if_fail (allocation != NULL);
269 menu_bar = GTK_MENU_BAR (widget);
270 menu_shell = GTK_MENU_SHELL (widget);
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);
278 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
280 if (menu_shell->children)
282 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
283 widget->style->xthickness +
286 offset = child_allocation.x; /* Window edge to menubar start */
288 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
289 widget->style->ythickness +
292 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
294 children = menu_shell->children;
299 child = children->data;
300 children = children->next;
302 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
304 gtk_widget_get_child_requisition (child, &child_requisition);
306 child_requisition.width += toggle_size;
308 /* Support for the right justified help menu */
309 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
310 && (GTK_MENU_ITEM(child)->right_justify))
312 child_allocation.x = allocation->width -
313 child_requisition.width - CHILD_SPACING - offset;
315 if (GTK_WIDGET_VISIBLE (child))
317 child_allocation.width = child_requisition.width;
319 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
321 gtk_widget_size_allocate (child, &child_allocation);
323 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
330 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
332 g_return_if_fail (widget != NULL);
333 g_return_if_fail (GTK_IS_MENU_BAR (widget));
335 if (GTK_WIDGET_DRAWABLE (widget))
339 border = GTK_CONTAINER (widget)->border_width;
341 gtk_paint_box (widget->style,
343 GTK_WIDGET_STATE (widget),
344 get_shadow_type (GTK_MENU_BAR (widget)),
345 area, widget, "menubar",
347 widget->allocation.width - border * 2,
348 widget->allocation.height - border * 2);
353 gtk_menu_bar_expose (GtkWidget *widget,
354 GdkEventExpose *event)
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);
360 if (GTK_WIDGET_DRAWABLE (widget))
362 gtk_menu_bar_paint (widget, &event->area);
364 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
371 window_key_press_handler (GtkWidget *widget,
376 gboolean retval = FALSE;
378 g_object_get (G_OBJECT (gtk_settings_get_global ()),
379 "gtk-menu-bar-accel",
386 GdkModifierType mods = 0;
388 gtk_accelerator_parse (accel, &keyval, &mods);
391 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
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.
397 if (event->keyval == keyval &&
398 (mods & event->state) == mods)
401 GtkMenuShell *menushell;
403 menubar = GTK_MENU_BAR (data);
404 menushell = GTK_MENU_SHELL (menubar);
406 if (menushell->children)
408 gtk_signal_emit_by_name (GTK_OBJECT (menushell->children->data),
422 add_to_window (GtkWindow *window,
425 GtkMenuBar *old_menubar;
427 old_menubar = g_object_get_data (G_OBJECT (window),
431 return; /* ignore this case; app programmer on crack, but
432 * shouldn't spew stuff, just don't support the accel
436 g_object_set_data (G_OBJECT (window),
440 g_signal_connect_data (G_OBJECT (window),
442 G_CALLBACK (window_key_press_handler),
446 menubar->toplevel = GTK_WIDGET (window);
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)
453 remove_from_window (GtkWindow *window,
456 g_return_if_fail (menubar->toplevel == GTK_WIDGET (window));
458 g_signal_handlers_disconnect_by_func (G_OBJECT (window),
459 G_CALLBACK (window_key_press_handler),
462 /* dnotify zeroes menubar->toplevel */
463 g_object_set_data (G_OBJECT (window),
467 menubar->toplevel = NULL;
471 gtk_menu_bar_hierarchy_changed (GtkWidget *widget)
476 menubar = GTK_MENU_BAR (widget);
478 toplevel = gtk_widget_get_toplevel (widget);
480 if (menubar->toplevel &&
481 toplevel != menubar->toplevel)
483 remove_from_window (GTK_WINDOW (menubar->toplevel),
488 GTK_IS_WINDOW (toplevel))
490 add_to_window (GTK_WINDOW (toplevel),
496 get_shadow_type (GtkMenuBar *menubar)
498 GtkShadowType shadow_type = GTK_SHADOW_OUT;
500 gtk_widget_style_get (GTK_WIDGET (menubar),
501 "shadow_type", &shadow_type,