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"
39 #define BORDER_SPACING 0
40 #define CHILD_SPACING 3
43 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
44 static void gtk_menu_bar_init (GtkMenuBar *menu_bar);
45 static void gtk_menu_bar_set_arg (GtkObject *object,
48 static void gtk_menu_bar_get_arg (GtkObject *object,
51 static void gtk_menu_bar_size_request (GtkWidget *widget,
52 GtkRequisition *requisition);
53 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
54 GtkAllocation *allocation);
55 static void gtk_menu_bar_paint (GtkWidget *widget,
57 static gint gtk_menu_bar_expose (GtkWidget *widget,
58 GdkEventExpose *event);
61 static GtkMenuShellClass *parent_class = NULL;
64 gtk_menu_bar_get_type (void)
66 static GtkType menu_bar_type = 0;
70 static const GtkTypeInfo menu_bar_info =
74 sizeof (GtkMenuBarClass),
75 (GtkClassInitFunc) gtk_menu_bar_class_init,
76 (GtkObjectInitFunc) gtk_menu_bar_init,
77 /* reserved_1 */ NULL,
78 /* reserved_2 */ NULL,
79 (GtkClassInitFunc) NULL,
82 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
89 gtk_menu_bar_class_init (GtkMenuBarClass *class)
91 GtkObjectClass *object_class;
92 GtkWidgetClass *widget_class;
93 GtkMenuShellClass *menu_shell_class;
95 GtkBindingSet *binding_set;
97 parent_class = g_type_class_peek_parent (class);
99 object_class = (GtkObjectClass*) class;
100 widget_class = (GtkWidgetClass*) class;
101 menu_shell_class = (GtkMenuShellClass*) class;
103 gtk_object_add_arg_type ("GtkMenuBar::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
105 object_class->set_arg = gtk_menu_bar_set_arg;
106 object_class->get_arg = gtk_menu_bar_get_arg;
108 widget_class->size_request = gtk_menu_bar_size_request;
109 widget_class->size_allocate = gtk_menu_bar_size_allocate;
110 widget_class->expose_event = gtk_menu_bar_expose;
112 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
114 binding_set = gtk_binding_set_by_class (class);
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,
138 gtk_menu_bar_init (GtkMenuBar *menu_bar)
140 menu_bar->shadow_type = GTK_SHADOW_OUT;
144 gtk_menu_bar_set_arg (GtkObject *object,
148 GtkMenuBar *menu_bar;
150 menu_bar = GTK_MENU_BAR (object);
155 gtk_menu_bar_set_shadow_type (menu_bar, GTK_VALUE_ENUM (*arg));
163 gtk_menu_bar_get_arg (GtkObject *object,
167 GtkMenuBar *menu_bar;
169 menu_bar = GTK_MENU_BAR (object);
174 GTK_VALUE_ENUM (*arg) = menu_bar->shadow_type;
177 arg->type = GTK_TYPE_INVALID;
183 gtk_menu_bar_new (void)
185 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
189 gtk_menu_bar_append (GtkMenuBar *menu_bar,
192 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
196 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
199 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
203 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
207 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
212 gtk_menu_bar_size_request (GtkWidget *widget,
213 GtkRequisition *requisition)
215 GtkMenuBar *menu_bar;
216 GtkMenuShell *menu_shell;
220 GtkRequisition child_requisition;
222 g_return_if_fail (widget != NULL);
223 g_return_if_fail (GTK_IS_MENU_BAR (widget));
224 g_return_if_fail (requisition != NULL);
226 requisition->width = 0;
227 requisition->height = 0;
229 if (GTK_WIDGET_VISIBLE (widget))
231 menu_bar = GTK_MENU_BAR (widget);
232 menu_shell = GTK_MENU_SHELL (widget);
235 children = menu_shell->children;
239 child = children->data;
240 children = children->next;
242 if (GTK_WIDGET_VISIBLE (child))
246 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
247 gtk_widget_size_request (child, &child_requisition);
248 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
251 requisition->width += child_requisition.width;
252 requisition->width += toggle_size;
254 requisition->height = MAX (requisition->height, child_requisition.height);
255 /* Support for the right justified help menu */
256 if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
257 GTK_MENU_ITEM(child)->right_justify)
259 requisition->width += CHILD_SPACING;
266 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
267 widget->style->xthickness +
269 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
270 widget->style->ythickness +
274 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
279 gtk_menu_bar_size_allocate (GtkWidget *widget,
280 GtkAllocation *allocation)
282 GtkMenuBar *menu_bar;
283 GtkMenuShell *menu_shell;
286 GtkAllocation child_allocation;
287 GtkRequisition child_requisition;
290 g_return_if_fail (widget != NULL);
291 g_return_if_fail (GTK_IS_MENU_BAR (widget));
292 g_return_if_fail (allocation != NULL);
294 menu_bar = GTK_MENU_BAR (widget);
295 menu_shell = GTK_MENU_SHELL (widget);
297 widget->allocation = *allocation;
298 if (GTK_WIDGET_REALIZED (widget))
299 gdk_window_move_resize (widget->window,
300 allocation->x, allocation->y,
301 allocation->width, allocation->height);
303 if (menu_shell->children)
305 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
306 widget->style->xthickness +
308 offset = child_allocation.x; /* Window edge to menubar start */
310 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
311 widget->style->ythickness +
313 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
315 children = menu_shell->children;
320 child = children->data;
321 children = children->next;
323 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
325 gtk_widget_get_child_requisition (child, &child_requisition);
327 child_requisition.width += toggle_size;
329 /* Support for the right justified help menu */
330 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
331 && (GTK_MENU_ITEM(child)->right_justify))
333 child_allocation.x = allocation->width -
334 child_requisition.width - CHILD_SPACING - offset;
336 if (GTK_WIDGET_VISIBLE (child))
338 child_allocation.width = child_requisition.width;
340 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
342 gtk_widget_size_allocate (child, &child_allocation);
344 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
351 gtk_menu_bar_set_shadow_type (GtkMenuBar *menu_bar,
354 g_return_if_fail (menu_bar != NULL);
355 g_return_if_fail (GTK_IS_MENU_BAR (menu_bar));
357 if ((GtkShadowType) menu_bar->shadow_type != type)
359 menu_bar->shadow_type = type;
361 if (GTK_WIDGET_DRAWABLE (menu_bar))
363 gtk_widget_queue_clear (GTK_WIDGET (menu_bar));
365 gtk_widget_queue_resize (GTK_WIDGET (menu_bar));
370 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
372 g_return_if_fail (widget != NULL);
373 g_return_if_fail (GTK_IS_MENU_BAR (widget));
375 if (GTK_WIDGET_DRAWABLE (widget))
377 gtk_paint_box (widget->style,
380 GTK_MENU_BAR (widget)->shadow_type,
381 area, widget, "menubar",
388 gtk_menu_bar_expose (GtkWidget *widget,
389 GdkEventExpose *event)
391 g_return_val_if_fail (widget != NULL, FALSE);
392 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
393 g_return_val_if_fail (event != NULL, FALSE);
395 if (GTK_WIDGET_DRAWABLE (widget))
397 gtk_menu_bar_paint (widget, &event->area);
399 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);