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 GtkWidget *old_toplevel);
53 static GtkShadowType get_shadow_type (GtkMenuBar *menubar);
55 static GtkMenuShellClass *parent_class = NULL;
58 gtk_menu_bar_get_type (void)
60 static GtkType menu_bar_type = 0;
64 static const GtkTypeInfo menu_bar_info =
68 sizeof (GtkMenuBarClass),
69 (GtkClassInitFunc) gtk_menu_bar_class_init,
70 (GtkObjectInitFunc) NULL,
71 /* reserved_1 */ NULL,
72 /* reserved_2 */ NULL,
73 (GtkClassInitFunc) NULL,
76 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
83 gtk_menu_bar_class_init (GtkMenuBarClass *class)
85 GtkObjectClass *object_class;
86 GtkWidgetClass *widget_class;
87 GtkMenuShellClass *menu_shell_class;
89 GtkBindingSet *binding_set;
91 parent_class = g_type_class_peek_parent (class);
93 object_class = (GtkObjectClass*) class;
94 widget_class = (GtkWidgetClass*) class;
95 menu_shell_class = (GtkMenuShellClass*) class;
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;
102 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
104 binding_set = gtk_binding_set_by_class (class);
105 gtk_binding_entry_add_signal (binding_set,
108 GTK_TYPE_MENU_DIRECTION_TYPE,
110 gtk_binding_entry_add_signal (binding_set,
113 GTK_TYPE_MENU_DIRECTION_TYPE,
115 gtk_binding_entry_add_signal (binding_set,
118 GTK_TYPE_MENU_DIRECTION_TYPE,
120 gtk_binding_entry_add_signal (binding_set,
123 GTK_TYPE_MENU_DIRECTION_TYPE,
125 gtk_binding_entry_add_signal (binding_set,
128 GTK_TYPE_MENU_DIRECTION_TYPE,
129 GTK_MENU_DIR_PARENT);
130 gtk_binding_entry_add_signal (binding_set,
133 GTK_TYPE_MENU_DIRECTION_TYPE,
134 GTK_MENU_DIR_PARENT);
135 gtk_binding_entry_add_signal (binding_set,
138 GTK_TYPE_MENU_DIRECTION_TYPE,
140 gtk_binding_entry_add_signal (binding_set,
143 GTK_TYPE_MENU_DIRECTION_TYPE,
146 gtk_settings_install_property (g_param_spec_string ("gtk-menu-bar-accel",
147 _("Menu bar accelerator"),
148 _("Keybinding to activate the menu bar"),
152 gtk_widget_class_install_style_property (widget_class,
153 g_param_spec_enum ("shadow_type",
155 _("Style of bevel around the menubar"),
156 GTK_TYPE_SHADOW_TYPE,
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"),
172 gtk_menu_bar_new (void)
174 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
178 gtk_menu_bar_append (GtkMenuBar *menu_bar,
181 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
185 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
188 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
192 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
196 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
201 gtk_menu_bar_size_request (GtkWidget *widget,
202 GtkRequisition *requisition)
204 GtkMenuBar *menu_bar;
205 GtkMenuShell *menu_shell;
209 GtkRequisition child_requisition;
212 g_return_if_fail (GTK_IS_MENU_BAR (widget));
213 g_return_if_fail (requisition != NULL);
215 requisition->width = 0;
216 requisition->height = 0;
218 if (GTK_WIDGET_VISIBLE (widget))
220 menu_bar = GTK_MENU_BAR (widget);
221 menu_shell = GTK_MENU_SHELL (widget);
224 children = menu_shell->children;
228 child = children->data;
229 children = children->next;
231 if (GTK_WIDGET_VISIBLE (child))
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),
240 requisition->width += child_requisition.width;
241 requisition->width += toggle_size;
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)
248 requisition->width += CHILD_SPACING;
255 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
257 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
258 widget->style->xthickness +
261 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
262 widget->style->ythickness +
267 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
272 gtk_menu_bar_size_allocate (GtkWidget *widget,
273 GtkAllocation *allocation)
275 GtkMenuBar *menu_bar;
276 GtkMenuShell *menu_shell;
279 GtkAllocation child_allocation;
280 GtkRequisition child_requisition;
284 g_return_if_fail (GTK_IS_MENU_BAR (widget));
285 g_return_if_fail (allocation != NULL);
287 menu_bar = GTK_MENU_BAR (widget);
288 menu_shell = GTK_MENU_SHELL (widget);
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);
296 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
298 if (menu_shell->children)
300 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
301 widget->style->xthickness +
304 offset = child_allocation.x; /* Window edge to menubar start */
306 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
307 widget->style->ythickness +
310 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
312 children = menu_shell->children;
317 child = children->data;
318 children = children->next;
320 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
322 gtk_widget_get_child_requisition (child, &child_requisition);
324 child_requisition.width += toggle_size;
326 /* Support for the right justified help menu */
327 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
328 && (GTK_MENU_ITEM(child)->right_justify))
330 child_allocation.x = allocation->width -
331 child_requisition.width - CHILD_SPACING - offset;
333 if (GTK_WIDGET_VISIBLE (child))
335 child_allocation.width = child_requisition.width;
337 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
339 gtk_widget_size_allocate (child, &child_allocation);
341 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
348 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
350 g_return_if_fail (GTK_IS_MENU_BAR (widget));
352 if (GTK_WIDGET_DRAWABLE (widget))
356 border = GTK_CONTAINER (widget)->border_width;
358 gtk_paint_box (widget->style,
360 GTK_WIDGET_STATE (widget),
361 get_shadow_type (GTK_MENU_BAR (widget)),
362 area, widget, "menubar",
364 widget->allocation.width - border * 2,
365 widget->allocation.height - border * 2);
370 gtk_menu_bar_expose (GtkWidget *widget,
371 GdkEventExpose *event)
373 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
374 g_return_val_if_fail (event != NULL, FALSE);
376 if (GTK_WIDGET_DRAWABLE (widget))
378 gtk_menu_bar_paint (widget, &event->area);
380 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
387 window_key_press_handler (GtkWidget *widget,
392 gboolean retval = FALSE;
394 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
395 "gtk-menu-bar-accel",
402 GdkModifierType mods = 0;
404 gtk_accelerator_parse (accel, &keyval, &mods);
407 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
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.
413 if (event->keyval == keyval &&
414 (mods & event->state) == mods)
417 GtkMenuShell *menushell;
419 menubar = GTK_MENU_BAR (data);
420 menushell = GTK_MENU_SHELL (menubar);
422 if (menushell->children)
424 gtk_signal_emit_by_name (GTK_OBJECT (menushell->children->data),
438 add_to_window (GtkWindow *window,
441 GtkMenuBar *old_menubar;
443 old_menubar = g_object_get_data (G_OBJECT (window),
447 return; /* ignore this case; app programmer on crack, but
448 * shouldn't spew stuff, just don't support the accel
452 g_object_set_data (G_OBJECT (window),
456 g_signal_connect (G_OBJECT (window),
458 G_CALLBACK (window_key_press_handler),
463 remove_from_window (GtkWindow *window,
466 g_signal_handlers_disconnect_by_func (G_OBJECT (window),
467 G_CALLBACK (window_key_press_handler),
472 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
473 GtkWidget *old_toplevel)
478 menubar = GTK_MENU_BAR (widget);
480 toplevel = gtk_widget_get_toplevel (widget);
483 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
485 if (GTK_WIDGET_TOPLEVEL (toplevel))
486 add_to_window (GTK_WINDOW (toplevel), menubar);
490 get_shadow_type (GtkMenuBar *menubar)
492 GtkShadowType shadow_type = GTK_SHADOW_OUT;
494 gtk_widget_style_get (GTK_WIDGET (menubar),
495 "shadow_type", &shadow_type,