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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "gtkmenuitem.h"
20 #include "gtkoptionmenu.h"
21 #include "gtksignal.h"
24 #define CHILD_LEFT_SPACING 5
25 #define CHILD_RIGHT_SPACING 1
26 #define CHILD_TOP_SPACING 1
27 #define CHILD_BOTTOM_SPACING 1
28 #define OPTION_INDICATOR_WIDTH 12
29 #define OPTION_INDICATOR_HEIGHT 8
30 #define OPTION_INDICATOR_SPACING 2
33 static void gtk_option_menu_class_init (GtkOptionMenuClass *klass);
34 static void gtk_option_menu_init (GtkOptionMenu *option_menu);
35 static void gtk_option_menu_destroy (GtkObject *object);
36 static void gtk_option_menu_size_request (GtkWidget *widget,
37 GtkRequisition *requisition);
38 static void gtk_option_menu_size_allocate (GtkWidget *widget,
39 GtkAllocation *allocation);
40 static void gtk_option_menu_paint (GtkWidget *widget,
42 static void gtk_option_menu_draw (GtkWidget *widget,
44 static gint gtk_option_menu_expose (GtkWidget *widget,
45 GdkEventExpose *event);
46 static gint gtk_option_menu_button_press (GtkWidget *widget,
47 GdkEventButton *event);
48 static void gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
49 GtkOptionMenu *option_menu);
50 static void gtk_option_menu_update_contents (GtkOptionMenu *option_menu);
51 static void gtk_option_menu_remove_contents (GtkOptionMenu *option_menu);
52 static void gtk_option_menu_calc_size (GtkOptionMenu *option_menu);
53 static void gtk_option_menu_position (GtkMenu *menu,
59 static GtkButtonClass *parent_class = NULL;
63 gtk_option_menu_get_type ()
65 static guint option_menu_type = 0;
67 if (!option_menu_type)
69 GtkTypeInfo option_menu_info =
72 sizeof (GtkOptionMenu),
73 sizeof (GtkOptionMenuClass),
74 (GtkClassInitFunc) gtk_option_menu_class_init,
75 (GtkObjectInitFunc) gtk_option_menu_init,
79 option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
82 return option_menu_type;
86 gtk_option_menu_class_init (GtkOptionMenuClass *class)
88 GtkObjectClass *object_class;
89 GtkWidgetClass *widget_class;
90 GtkButtonClass *button_class;
92 object_class = (GtkObjectClass*) class;
93 widget_class = (GtkWidgetClass*) class;
94 button_class = (GtkButtonClass*) class;
96 parent_class = gtk_type_class (gtk_button_get_type ());
98 object_class->destroy = gtk_option_menu_destroy;
100 widget_class->draw = gtk_option_menu_draw;
101 widget_class->draw_focus = NULL;
102 widget_class->size_request = gtk_option_menu_size_request;
103 widget_class->size_allocate = gtk_option_menu_size_allocate;
104 widget_class->expose_event = gtk_option_menu_expose;
105 widget_class->button_press_event = gtk_option_menu_button_press;
109 gtk_option_menu_init (GtkOptionMenu *option_menu)
111 GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_FOCUS);
113 option_menu->menu = NULL;
114 option_menu->menu_item = NULL;
115 option_menu->width = 0;
116 option_menu->height = 0;
120 gtk_option_menu_new ()
122 return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
126 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
128 g_return_val_if_fail (option_menu != NULL, NULL);
129 g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
131 return option_menu->menu;
135 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
138 g_return_if_fail (option_menu != NULL);
139 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
140 g_return_if_fail (menu != NULL);
141 g_return_if_fail (GTK_IS_MENU (menu));
143 gtk_option_menu_remove_menu (option_menu);
145 option_menu->menu = menu;
146 gtk_object_ref (GTK_OBJECT (option_menu->menu));
148 gtk_option_menu_calc_size (option_menu);
150 gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
151 (GtkSignalFunc) gtk_option_menu_deactivate,
154 if (GTK_WIDGET (option_menu)->parent)
155 gtk_widget_queue_resize (GTK_WIDGET (option_menu));
157 gtk_option_menu_update_contents (option_menu);
161 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
163 g_return_if_fail (option_menu != NULL);
164 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
166 if (option_menu->menu)
168 gtk_option_menu_remove_contents (option_menu);
169 gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
172 gtk_object_unref (GTK_OBJECT (option_menu->menu));
173 option_menu->menu = NULL;
178 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
181 GtkWidget *menu_item;
183 g_return_if_fail (option_menu != NULL);
184 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
186 if (option_menu->menu)
188 gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
189 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
191 if (menu_item != option_menu->menu_item)
193 gtk_option_menu_remove_contents (option_menu);
194 gtk_option_menu_update_contents (option_menu);
201 gtk_option_menu_destroy (GtkObject *object)
203 GtkOptionMenu *option_menu;
205 g_return_if_fail (object != NULL);
206 g_return_if_fail (GTK_IS_OPTION_MENU (object));
208 option_menu = GTK_OPTION_MENU (object);
210 gtk_option_menu_remove_contents (option_menu);
211 if (option_menu->menu)
213 gtk_object_unref (GTK_OBJECT (option_menu->menu));
214 gtk_widget_destroy (option_menu->menu);
217 if (GTK_OBJECT_CLASS (parent_class)->destroy)
218 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
222 gtk_option_menu_size_request (GtkWidget *widget,
223 GtkRequisition *requisition)
225 GtkOptionMenu *option_menu;
228 g_return_if_fail (widget != NULL);
229 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
230 g_return_if_fail (requisition != NULL);
232 option_menu = GTK_OPTION_MENU (widget);
234 requisition->width = ((GTK_CONTAINER (widget)->border_width +
235 GTK_WIDGET (widget)->style->klass->xthickness) * 2 +
237 OPTION_INDICATOR_WIDTH +
238 OPTION_INDICATOR_SPACING * 5 +
239 CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING);
240 requisition->height = ((GTK_CONTAINER (widget)->border_width +
241 GTK_WIDGET (widget)->style->klass->ythickness) * 2 +
242 option_menu->height +
243 CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING);
245 tmp = (requisition->height - option_menu->height +
246 OPTION_INDICATOR_HEIGHT + OPTION_INDICATOR_SPACING * 2);
247 requisition->height = MAX (requisition->height, tmp);
251 gtk_option_menu_size_allocate (GtkWidget *widget,
252 GtkAllocation *allocation)
255 GtkAllocation child_allocation;
257 g_return_if_fail (widget != NULL);
258 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
259 g_return_if_fail (allocation != NULL);
261 widget->allocation = *allocation;
262 if (GTK_WIDGET_REALIZED (widget))
263 gdk_window_move_resize (widget->window,
264 allocation->x, allocation->y,
265 allocation->width, allocation->height);
267 child = GTK_BUTTON (widget)->child;
268 if (child && GTK_WIDGET_VISIBLE (child))
270 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
271 GTK_WIDGET (widget)->style->klass->xthickness);
272 child_allocation.y = (GTK_CONTAINER (widget)->border_width +
273 GTK_WIDGET (widget)->style->klass->ythickness);
274 child_allocation.width = (allocation->width - child_allocation.x * 2 -
275 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 5 -
276 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING);
277 child_allocation.height = (allocation->height - child_allocation.y * 2 -
278 CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING);
279 child_allocation.x += CHILD_LEFT_SPACING;
280 child_allocation.y += CHILD_RIGHT_SPACING;
282 gtk_widget_size_allocate (child, &child_allocation);
287 gtk_option_menu_paint (GtkWidget *widget,
290 GdkRectangle restrict_area;
291 GdkRectangle new_area;
293 g_return_if_fail (widget != NULL);
294 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
295 g_return_if_fail (area != NULL);
297 if (GTK_WIDGET_DRAWABLE (widget))
299 restrict_area.x = GTK_CONTAINER (widget)->border_width;
300 restrict_area.y = GTK_CONTAINER (widget)->border_width;
301 restrict_area.width = widget->allocation.width - restrict_area.x * 2;
302 restrict_area.height = widget->allocation.height - restrict_area.y * 2;
304 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
306 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (widget));
307 gdk_window_clear_area (widget->window,
308 new_area.x, new_area.y,
309 new_area.width, new_area.height);
311 gtk_draw_shadow (widget->style, widget->window,
312 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
313 restrict_area.x, restrict_area.y,
314 restrict_area.width, restrict_area.height);
316 gtk_draw_shadow (widget->style, widget->window,
317 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
318 restrict_area.x + restrict_area.width - restrict_area.x -
319 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 4,
320 restrict_area.y + (restrict_area.height - OPTION_INDICATOR_HEIGHT) / 2,
321 OPTION_INDICATOR_WIDTH, OPTION_INDICATOR_HEIGHT);
327 gtk_option_menu_draw (GtkWidget *widget,
331 GdkRectangle child_area;
333 g_return_if_fail (widget != NULL);
334 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
335 g_return_if_fail (area != NULL);
337 if (GTK_WIDGET_DRAWABLE (widget))
339 gtk_option_menu_paint (widget, area);
341 child = GTK_BUTTON (widget)->child;
342 if (child && gtk_widget_intersect (child, area, &child_area))
343 gtk_widget_draw (child, &child_area);
348 gtk_option_menu_expose (GtkWidget *widget,
349 GdkEventExpose *event)
352 GdkEventExpose child_event;
355 g_return_val_if_fail (widget != NULL, FALSE);
356 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
357 g_return_val_if_fail (event != NULL, FALSE);
359 if (GTK_WIDGET_DRAWABLE (widget))
361 gtk_option_menu_paint (widget, &event->area);
363 remove_child = FALSE;
364 child = GTK_BUTTON (widget)->child;
368 if (!GTK_OPTION_MENU (widget)->menu)
370 gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
371 child = GTK_BUTTON (widget)->child;
377 child_event = *event;
379 if (GTK_WIDGET_NO_WINDOW (child) &&
380 gtk_widget_intersect (child, &event->area, &child_event.area))
381 gtk_widget_event (child, (GdkEvent*) &child_event);
384 gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
391 gtk_option_menu_button_press (GtkWidget *widget,
392 GdkEventButton *event)
394 GtkOptionMenu *option_menu;
396 g_return_val_if_fail (widget != NULL, FALSE);
397 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
398 g_return_val_if_fail (event != NULL, FALSE);
400 if ((event->type == GDK_BUTTON_PRESS) &&
401 (event->button == 1))
403 option_menu = GTK_OPTION_MENU (widget);
404 gtk_option_menu_remove_contents (option_menu);
405 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
406 gtk_option_menu_position, option_menu,
407 event->button, event->time);
414 gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
415 GtkOptionMenu *option_menu)
417 g_return_if_fail (menu_shell != NULL);
418 g_return_if_fail (option_menu != NULL);
419 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
421 gtk_option_menu_update_contents (option_menu);
425 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
429 g_return_if_fail (option_menu != NULL);
430 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
432 if (option_menu->menu)
434 gtk_option_menu_remove_contents (option_menu);
436 option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
437 if (option_menu->menu_item)
439 child = GTK_BIN (option_menu->menu_item)->child;
442 gtk_container_block_resize (GTK_CONTAINER (option_menu));
443 if (GTK_WIDGET (option_menu)->state != child->state)
444 gtk_widget_set_state (child, GTK_WIDGET (option_menu)->state);
445 gtk_widget_reparent (child, GTK_WIDGET (option_menu));
446 gtk_container_unblock_resize (GTK_CONTAINER (option_menu));
449 gtk_widget_size_allocate (GTK_WIDGET (option_menu),
450 &(GTK_WIDGET (option_menu)->allocation));
452 if (GTK_WIDGET_DRAWABLE (option_menu))
453 gtk_widget_queue_draw (GTK_WIDGET (option_menu));
459 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
461 g_return_if_fail (option_menu != NULL);
462 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
464 if (GTK_BUTTON (option_menu)->child)
466 gtk_container_block_resize (GTK_CONTAINER (option_menu));
467 if (GTK_WIDGET (option_menu->menu_item)->state != GTK_BUTTON (option_menu)->child->state)
468 gtk_widget_set_state (GTK_BUTTON (option_menu)->child,
469 GTK_WIDGET (option_menu->menu_item)->state);
470 GTK_WIDGET_UNSET_FLAGS (GTK_BUTTON (option_menu)->child, GTK_MAPPED | GTK_REALIZED);
471 gtk_widget_reparent (GTK_BUTTON (option_menu)->child, option_menu->menu_item);
472 gtk_container_unblock_resize (GTK_CONTAINER (option_menu));
473 option_menu->menu_item = NULL;
478 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
483 g_return_if_fail (option_menu != NULL);
484 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
486 option_menu->width = 0;
487 option_menu->height = 0;
489 if (option_menu->menu)
491 children = GTK_MENU_SHELL (option_menu->menu)->children;
494 child = children->data;
495 children = children->next;
497 if (GTK_WIDGET_VISIBLE (child))
499 gtk_widget_size_request (child, &child->requisition);
501 option_menu->width = MAX (option_menu->width, child->requisition.width);
502 option_menu->height = MAX (option_menu->height, child->requisition.height);
509 gtk_option_menu_position (GtkMenu *menu,
514 GtkOptionMenu *option_menu;
526 g_return_if_fail (user_data != NULL);
527 g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
529 option_menu = GTK_OPTION_MENU (user_data);
531 width = GTK_WIDGET (menu)->allocation.width;
532 height = GTK_WIDGET (menu)->allocation.height;
534 active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
535 children = GTK_MENU_SHELL (option_menu->menu)->children;
536 gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
538 menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
541 menu_ypos -= active->requisition.height / 2;
545 child = children->data;
550 menu_ypos -= child->allocation.height;
551 children = children->next;
554 screen_width = gdk_screen_width ();
555 screen_height = gdk_screen_height ();
563 else if ((menu_ypos + height) > screen_height)
565 menu_ypos -= ((menu_ypos + height) - screen_height);
571 if ((menu_xpos + GTK_WIDGET (option_menu)->allocation.width + width) <= screen_width)
572 menu_xpos += GTK_WIDGET (option_menu)->allocation.width;
579 else if ((menu_xpos + width) > screen_width)
580 menu_xpos -= ((menu_xpos + width) - screen_width);