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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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 void gtk_menu_bar_draw (GtkWidget *widget,
59 static gint gtk_menu_bar_expose (GtkWidget *widget,
60 GdkEventExpose *event);
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 object_class = (GtkObjectClass*) class;
98 widget_class = (GtkWidgetClass*) class;
99 menu_shell_class = (GtkMenuShellClass*) class;
101 gtk_object_add_arg_type ("GtkMenuBar::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
103 object_class->set_arg = gtk_menu_bar_set_arg;
104 object_class->get_arg = gtk_menu_bar_get_arg;
106 widget_class->draw = gtk_menu_bar_draw;
107 widget_class->size_request = gtk_menu_bar_size_request;
108 widget_class->size_allocate = gtk_menu_bar_size_allocate;
109 widget_class->expose_event = gtk_menu_bar_expose;
111 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
113 binding_set = gtk_binding_set_by_class (class);
114 gtk_binding_entry_add_signal (binding_set,
117 GTK_TYPE_MENU_DIRECTION_TYPE,
119 gtk_binding_entry_add_signal (binding_set,
122 GTK_TYPE_MENU_DIRECTION_TYPE,
124 gtk_binding_entry_add_signal (binding_set,
127 GTK_TYPE_MENU_DIRECTION_TYPE,
128 GTK_MENU_DIR_PARENT);
129 gtk_binding_entry_add_signal (binding_set,
132 GTK_TYPE_MENU_DIRECTION_TYPE,
137 gtk_menu_bar_init (GtkMenuBar *menu_bar)
139 menu_bar->shadow_type = GTK_SHADOW_OUT;
143 gtk_menu_bar_set_arg (GtkObject *object,
147 GtkMenuBar *menu_bar;
149 menu_bar = GTK_MENU_BAR (object);
154 gtk_menu_bar_set_shadow_type (menu_bar, GTK_VALUE_ENUM (*arg));
162 gtk_menu_bar_get_arg (GtkObject *object,
166 GtkMenuBar *menu_bar;
168 menu_bar = GTK_MENU_BAR (object);
173 GTK_VALUE_ENUM (*arg) = menu_bar->shadow_type;
176 arg->type = GTK_TYPE_INVALID;
182 gtk_menu_bar_new (void)
184 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
188 gtk_menu_bar_append (GtkMenuBar *menu_bar,
191 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
195 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
198 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
202 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
206 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
211 gtk_menu_bar_size_request (GtkWidget *widget,
212 GtkRequisition *requisition)
214 GtkMenuBar *menu_bar;
215 GtkMenuShell *menu_shell;
219 GtkRequisition child_requisition;
221 g_return_if_fail (widget != NULL);
222 g_return_if_fail (GTK_IS_MENU_BAR (widget));
223 g_return_if_fail (requisition != NULL);
225 requisition->width = 0;
226 requisition->height = 0;
228 if (GTK_WIDGET_VISIBLE (widget))
230 menu_bar = GTK_MENU_BAR (widget);
231 menu_shell = GTK_MENU_SHELL (widget);
234 children = menu_shell->children;
238 child = children->data;
239 children = children->next;
241 if (GTK_WIDGET_VISIBLE (child))
243 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
244 gtk_widget_size_request (child, &child_requisition);
246 requisition->width += child_requisition.width;
247 requisition->height = MAX (requisition->height, child_requisition.height);
248 /* Support for the right justified help menu */
249 if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
250 GTK_MENU_ITEM(child)->right_justify)
252 requisition->width += CHILD_SPACING;
259 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
260 widget->style->klass->xthickness +
262 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
263 widget->style->klass->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;
283 g_return_if_fail (widget != NULL);
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 if (menu_shell->children)
298 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
299 widget->style->klass->xthickness +
301 offset = child_allocation.x; /* Window edge to menubar start */
303 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
304 widget->style->klass->ythickness +
306 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
308 children = menu_shell->children;
311 child = children->data;
312 children = children->next;
314 gtk_widget_get_child_requisition (child, &child_requisition);
316 /* Support for the right justified help menu */
317 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
318 && (GTK_MENU_ITEM(child)->right_justify))
320 child_allocation.x = allocation->width -
321 child_requisition.width - CHILD_SPACING - offset;
323 if (GTK_WIDGET_VISIBLE (child))
325 child_allocation.width = child_requisition.width;
327 gtk_widget_size_allocate (child, &child_allocation);
329 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
336 gtk_menu_bar_set_shadow_type (GtkMenuBar *menu_bar,
339 g_return_if_fail (menu_bar != NULL);
340 g_return_if_fail (GTK_IS_MENU_BAR (menu_bar));
342 if ((GtkShadowType) menu_bar->shadow_type != type)
344 menu_bar->shadow_type = type;
346 if (GTK_WIDGET_DRAWABLE (menu_bar))
348 gtk_widget_queue_clear (GTK_WIDGET (menu_bar));
350 gtk_widget_queue_resize (GTK_WIDGET (menu_bar));
355 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
357 g_return_if_fail (widget != NULL);
358 g_return_if_fail (GTK_IS_MENU_BAR (widget));
360 if (GTK_WIDGET_DRAWABLE (widget))
362 gtk_paint_box (widget->style,
365 GTK_MENU_BAR (widget)->shadow_type,
366 area, widget, "menubar",
373 gtk_menu_bar_draw (GtkWidget *widget,
376 GtkMenuShell *menu_shell;
378 GdkRectangle child_area;
381 g_return_if_fail (widget != NULL);
382 g_return_if_fail (GTK_IS_MENU_BAR (widget));
383 g_return_if_fail (area != NULL);
385 if (GTK_WIDGET_DRAWABLE (widget))
387 gtk_menu_bar_paint (widget, area);
389 menu_shell = GTK_MENU_SHELL (widget);
391 children = menu_shell->children;
394 child = children->data;
395 children = children->next;
397 if (gtk_widget_intersect (child, area, &child_area))
398 gtk_widget_draw (child, &child_area);
404 gtk_menu_bar_expose (GtkWidget *widget,
405 GdkEventExpose *event)
407 GtkMenuShell *menu_shell;
408 GdkEventExpose child_event;
412 g_return_val_if_fail (widget != NULL, FALSE);
413 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
414 g_return_val_if_fail (event != NULL, FALSE);
416 if (GTK_WIDGET_DRAWABLE (widget))
418 gtk_menu_bar_paint (widget, &event->area);
420 menu_shell = GTK_MENU_SHELL (widget);
421 child_event = *event;
423 children = menu_shell->children;
426 child = children->data;
427 children = children->next;
429 if (GTK_WIDGET_NO_WINDOW (child) &&
430 gtk_widget_intersect (child, &event->area, &child_event.area))
431 gtk_widget_event (child, (GdkEvent*) &child_event);