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 (widget != NULL);
213 g_return_if_fail (GTK_IS_MENU_BAR (widget));
214 g_return_if_fail (requisition != NULL);
216 requisition->width = 0;
217 requisition->height = 0;
219 if (GTK_WIDGET_VISIBLE (widget))
221 menu_bar = GTK_MENU_BAR (widget);
222 menu_shell = GTK_MENU_SHELL (widget);
225 children = menu_shell->children;
229 child = children->data;
230 children = children->next;
232 if (GTK_WIDGET_VISIBLE (child))
236 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
237 gtk_widget_size_request (child, &child_requisition);
238 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
241 requisition->width += child_requisition.width;
242 requisition->width += toggle_size;
244 requisition->height = MAX (requisition->height, child_requisition.height);
245 /* Support for the right justified help menu */
246 if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
247 GTK_MENU_ITEM(child)->right_justify)
249 requisition->width += CHILD_SPACING;
256 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
258 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
259 widget->style->xthickness +
262 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
263 widget->style->ythickness +
268 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
273 gtk_menu_bar_size_allocate (GtkWidget *widget,
274 GtkAllocation *allocation)
276 GtkMenuBar *menu_bar;
277 GtkMenuShell *menu_shell;
280 GtkAllocation child_allocation;
281 GtkRequisition child_requisition;
285 g_return_if_fail (widget != NULL);
286 g_return_if_fail (GTK_IS_MENU_BAR (widget));
287 g_return_if_fail (allocation != NULL);
289 menu_bar = GTK_MENU_BAR (widget);
290 menu_shell = GTK_MENU_SHELL (widget);
292 widget->allocation = *allocation;
293 if (GTK_WIDGET_REALIZED (widget))
294 gdk_window_move_resize (widget->window,
295 allocation->x, allocation->y,
296 allocation->width, allocation->height);
298 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
300 if (menu_shell->children)
302 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
303 widget->style->xthickness +
306 offset = child_allocation.x; /* Window edge to menubar start */
308 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
309 widget->style->ythickness +
312 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
314 children = menu_shell->children;
319 child = children->data;
320 children = children->next;
322 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
324 gtk_widget_get_child_requisition (child, &child_requisition);
326 child_requisition.width += toggle_size;
328 /* Support for the right justified help menu */
329 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
330 && (GTK_MENU_ITEM(child)->right_justify))
332 child_allocation.x = allocation->width -
333 child_requisition.width - CHILD_SPACING - offset;
335 if (GTK_WIDGET_VISIBLE (child))
337 child_allocation.width = child_requisition.width;
339 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
341 gtk_widget_size_allocate (child, &child_allocation);
343 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
350 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
352 g_return_if_fail (widget != NULL);
353 g_return_if_fail (GTK_IS_MENU_BAR (widget));
355 if (GTK_WIDGET_DRAWABLE (widget))
359 border = GTK_CONTAINER (widget)->border_width;
361 gtk_paint_box (widget->style,
363 GTK_WIDGET_STATE (widget),
364 get_shadow_type (GTK_MENU_BAR (widget)),
365 area, widget, "menubar",
367 widget->allocation.width - border * 2,
368 widget->allocation.height - border * 2);
373 gtk_menu_bar_expose (GtkWidget *widget,
374 GdkEventExpose *event)
376 g_return_val_if_fail (widget != NULL, FALSE);
377 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
378 g_return_val_if_fail (event != NULL, FALSE);
380 if (GTK_WIDGET_DRAWABLE (widget))
382 gtk_menu_bar_paint (widget, &event->area);
384 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
391 window_key_press_handler (GtkWidget *widget,
396 gboolean retval = FALSE;
398 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
399 "gtk-menu-bar-accel",
406 GdkModifierType mods = 0;
408 gtk_accelerator_parse (accel, &keyval, &mods);
411 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
413 /* FIXME this is wrong, needs to be in the global accel resolution
414 * thing, to properly consider i18n etc., but that probably requires
415 * AccelGroup changes etc.
417 if (event->keyval == keyval &&
418 (mods & event->state) == mods)
421 GtkMenuShell *menushell;
423 menubar = GTK_MENU_BAR (data);
424 menushell = GTK_MENU_SHELL (menubar);
426 if (menushell->children)
428 gtk_signal_emit_by_name (GTK_OBJECT (menushell->children->data),
442 add_to_window (GtkWindow *window,
445 GtkMenuBar *old_menubar;
447 old_menubar = g_object_get_data (G_OBJECT (window),
451 return; /* ignore this case; app programmer on crack, but
452 * shouldn't spew stuff, just don't support the accel
456 g_object_set_data (G_OBJECT (window),
460 g_signal_connect (G_OBJECT (window),
462 G_CALLBACK (window_key_press_handler),
467 remove_from_window (GtkWindow *window,
470 g_signal_handlers_disconnect_by_func (G_OBJECT (window),
471 G_CALLBACK (window_key_press_handler),
476 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
477 GtkWidget *old_toplevel)
482 menubar = GTK_MENU_BAR (widget);
484 toplevel = gtk_widget_get_toplevel (widget);
487 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
489 if (GTK_WIDGET_TOPLEVEL (toplevel))
490 add_to_window (GTK_WINDOW (toplevel), menubar);
494 get_shadow_type (GtkMenuBar *menubar)
496 GtkShadowType shadow_type = GTK_SHADOW_OUT;
498 gtk_widget_style_get (GTK_WIDGET (menubar),
499 "shadow_type", &shadow_type,