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.
20 #include "gtkmenuitem.h"
21 #include "gtkoptionmenu.h"
22 #include "gtksignal.h"
23 #include "gdk/gdkkeysyms.h"
26 #define CHILD_LEFT_SPACING 5
27 #define CHILD_RIGHT_SPACING 1
28 #define CHILD_TOP_SPACING 1
29 #define CHILD_BOTTOM_SPACING 1
30 #define OPTION_INDICATOR_WIDTH 12
31 #define OPTION_INDICATOR_HEIGHT 8
32 #define OPTION_INDICATOR_SPACING 2
35 static void gtk_option_menu_class_init (GtkOptionMenuClass *klass);
36 static void gtk_option_menu_init (GtkOptionMenu *option_menu);
37 static void gtk_option_menu_destroy (GtkObject *object);
38 static void gtk_option_menu_size_request (GtkWidget *widget,
39 GtkRequisition *requisition);
40 static void gtk_option_menu_size_allocate (GtkWidget *widget,
41 GtkAllocation *allocation);
42 static void gtk_option_menu_paint (GtkWidget *widget,
44 static void gtk_option_menu_draw (GtkWidget *widget,
46 static gint gtk_option_menu_expose (GtkWidget *widget,
47 GdkEventExpose *event);
48 static gint gtk_option_menu_button_press (GtkWidget *widget,
49 GdkEventButton *event);
50 static gint gtk_option_menu_key_press (GtkWidget *widget,
52 static void gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
53 GtkOptionMenu *option_menu);
54 static void gtk_option_menu_update_contents (GtkOptionMenu *option_menu);
55 static void gtk_option_menu_remove_contents (GtkOptionMenu *option_menu);
56 static void gtk_option_menu_calc_size (GtkOptionMenu *option_menu);
57 static void gtk_option_menu_position (GtkMenu *menu,
61 static void gtk_option_menu_show_all (GtkWidget *widget);
62 static void gtk_option_menu_hide_all (GtkWidget *widget);
63 static GtkType gtk_option_menu_child_type (GtkContainer *container);
67 static GtkButtonClass *parent_class = NULL;
71 gtk_option_menu_get_type (void)
73 static GtkType option_menu_type = 0;
75 if (!option_menu_type)
77 static const GtkTypeInfo option_menu_info =
80 sizeof (GtkOptionMenu),
81 sizeof (GtkOptionMenuClass),
82 (GtkClassInitFunc) gtk_option_menu_class_init,
83 (GtkObjectInitFunc) gtk_option_menu_init,
84 /* reserved_1 */ NULL,
85 /* reserved_2 */ NULL,
86 (GtkClassInitFunc) NULL,
89 option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
92 return option_menu_type;
96 gtk_option_menu_class_init (GtkOptionMenuClass *class)
98 GtkObjectClass *object_class;
99 GtkWidgetClass *widget_class;
100 GtkButtonClass *button_class;
101 GtkContainerClass *container_class;
103 object_class = (GtkObjectClass*) class;
104 widget_class = (GtkWidgetClass*) class;
105 button_class = (GtkButtonClass*) class;
106 container_class = (GtkContainerClass*) class;
108 parent_class = gtk_type_class (gtk_button_get_type ());
110 object_class->destroy = gtk_option_menu_destroy;
112 widget_class->draw = gtk_option_menu_draw;
113 widget_class->size_request = gtk_option_menu_size_request;
114 widget_class->size_allocate = gtk_option_menu_size_allocate;
115 widget_class->expose_event = gtk_option_menu_expose;
116 widget_class->button_press_event = gtk_option_menu_button_press;
117 widget_class->key_press_event = gtk_option_menu_key_press;
118 widget_class->show_all = gtk_option_menu_show_all;
119 widget_class->hide_all = gtk_option_menu_hide_all;
121 container_class->child_type = gtk_option_menu_child_type;
125 gtk_option_menu_child_type (GtkContainer *container)
127 return GTK_TYPE_NONE;
131 gtk_option_menu_init (GtkOptionMenu *option_menu)
133 GTK_WIDGET_SET_FLAGS (option_menu, GTK_CAN_FOCUS);
134 GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_DEFAULT | GTK_RECEIVES_DEFAULT);
136 option_menu->menu = NULL;
137 option_menu->menu_item = NULL;
138 option_menu->width = 0;
139 option_menu->height = 0;
143 gtk_option_menu_new (void)
145 return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
149 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
151 g_return_val_if_fail (option_menu != NULL, NULL);
152 g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
154 return option_menu->menu;
158 gtk_option_menu_detacher (GtkWidget *widget,
161 GtkOptionMenu *option_menu;
163 g_return_if_fail (widget != NULL);
164 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
166 option_menu = GTK_OPTION_MENU (widget);
167 g_return_if_fail (option_menu->menu == (GtkWidget*) menu);
169 gtk_option_menu_remove_contents (option_menu);
170 gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
173 option_menu->menu = NULL;
177 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
180 g_return_if_fail (option_menu != NULL);
181 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
182 g_return_if_fail (menu != NULL);
183 g_return_if_fail (GTK_IS_MENU (menu));
185 if (option_menu->menu != menu)
187 gtk_option_menu_remove_menu (option_menu);
189 option_menu->menu = menu;
190 gtk_menu_attach_to_widget (GTK_MENU (menu),
191 GTK_WIDGET (option_menu),
192 gtk_option_menu_detacher);
194 gtk_option_menu_calc_size (option_menu);
196 gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
197 (GtkSignalFunc) gtk_option_menu_deactivate,
200 if (GTK_WIDGET (option_menu)->parent)
201 gtk_widget_queue_resize (GTK_WIDGET (option_menu));
203 gtk_option_menu_update_contents (option_menu);
208 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
210 g_return_if_fail (option_menu != NULL);
211 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
213 if (option_menu->menu)
214 gtk_menu_detach (GTK_MENU (option_menu->menu));
218 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
221 GtkWidget *menu_item;
223 g_return_if_fail (option_menu != NULL);
224 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
226 if (option_menu->menu)
228 gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
229 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
231 if (menu_item != option_menu->menu_item)
233 gtk_option_menu_remove_contents (option_menu);
234 gtk_option_menu_update_contents (option_menu);
241 gtk_option_menu_destroy (GtkObject *object)
243 GtkOptionMenu *option_menu;
245 g_return_if_fail (object != NULL);
246 g_return_if_fail (GTK_IS_OPTION_MENU (object));
248 option_menu = GTK_OPTION_MENU (object);
250 if (option_menu->menu)
251 gtk_widget_destroy (option_menu->menu);
253 if (GTK_OBJECT_CLASS (parent_class)->destroy)
254 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
258 gtk_option_menu_size_request (GtkWidget *widget,
259 GtkRequisition *requisition)
261 GtkOptionMenu *option_menu;
264 g_return_if_fail (widget != NULL);
265 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
266 g_return_if_fail (requisition != NULL);
268 option_menu = GTK_OPTION_MENU (widget);
270 requisition->width = ((GTK_CONTAINER (widget)->border_width +
271 GTK_WIDGET (widget)->style->klass->xthickness) * 2 +
273 OPTION_INDICATOR_WIDTH +
274 OPTION_INDICATOR_SPACING * 5 +
275 CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING + 2);
276 requisition->height = ((GTK_CONTAINER (widget)->border_width +
277 GTK_WIDGET (widget)->style->klass->ythickness) * 2 +
278 option_menu->height +
279 CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING + 2);
281 tmp = (requisition->height - option_menu->height +
282 OPTION_INDICATOR_HEIGHT + OPTION_INDICATOR_SPACING * 2);
283 requisition->height = MAX (requisition->height, tmp);
287 gtk_option_menu_size_allocate (GtkWidget *widget,
288 GtkAllocation *allocation)
291 GtkAllocation child_allocation;
293 g_return_if_fail (widget != NULL);
294 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
295 g_return_if_fail (allocation != NULL);
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 child = GTK_BIN (widget)->child;
304 if (child && GTK_WIDGET_VISIBLE (child))
306 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
307 GTK_WIDGET (widget)->style->klass->xthickness) + 1;
308 child_allocation.y = (GTK_CONTAINER (widget)->border_width +
309 GTK_WIDGET (widget)->style->klass->ythickness) + 1;
310 child_allocation.width = (allocation->width - child_allocation.x * 2 -
311 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 5 -
312 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING) - 2;
313 child_allocation.height = (allocation->height - child_allocation.y * 2 -
314 CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING) - 2;
315 child_allocation.x += CHILD_LEFT_SPACING;
316 child_allocation.y += CHILD_RIGHT_SPACING;
318 gtk_widget_size_allocate (child, &child_allocation);
323 gtk_option_menu_paint (GtkWidget *widget,
326 GdkRectangle button_area;
328 g_return_if_fail (widget != NULL);
329 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
330 g_return_if_fail (area != NULL);
332 if (GTK_WIDGET_DRAWABLE (widget))
334 button_area.x = GTK_CONTAINER (widget)->border_width + 1;
335 button_area.y = GTK_CONTAINER (widget)->border_width + 1;
336 button_area.width = widget->allocation.width - button_area.x * 2;
337 button_area.height = widget->allocation.height - button_area.y * 2;
339 /* This is evil, and should be elimated here and in the button
340 * code. The point is to clear the focus, and make it
341 * sort of transparent if it isn't there.
343 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
344 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
346 gtk_paint_box(widget->style, widget->window,
347 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
348 area, widget, "optionmenu",
349 button_area.x, button_area.y,
350 button_area.width, button_area.height);
352 gtk_paint_tab (widget->style, widget->window,
353 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
354 area, widget, "optionmenutab",
355 button_area.x + button_area.width - button_area.x -
356 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 4,
357 button_area.y + (button_area.height - OPTION_INDICATOR_HEIGHT) / 2,
358 OPTION_INDICATOR_WIDTH, OPTION_INDICATOR_HEIGHT);
360 if (GTK_WIDGET_HAS_FOCUS (widget))
361 gtk_paint_focus (widget->style, widget->window,
362 area, widget, "button",
365 button_area.width + 1,
366 button_area.height + 1);
371 gtk_option_menu_draw (GtkWidget *widget,
375 GdkRectangle child_area;
377 g_return_if_fail (widget != NULL);
378 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
379 g_return_if_fail (area != NULL);
381 if (GTK_WIDGET_DRAWABLE (widget))
383 gtk_option_menu_paint (widget, area);
385 child = GTK_BIN (widget)->child;
386 if (child && gtk_widget_intersect (child, area, &child_area))
387 gtk_widget_draw (child, &child_area);
392 gtk_option_menu_expose (GtkWidget *widget,
393 GdkEventExpose *event)
396 GdkEventExpose child_event;
399 g_return_val_if_fail (widget != NULL, FALSE);
400 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
401 g_return_val_if_fail (event != NULL, FALSE);
403 if (GTK_WIDGET_DRAWABLE (widget))
405 gtk_option_menu_paint (widget, &event->area);
408 /* The following code tries to draw the child in two places at
409 * once. It fails miserably for several reasons
411 * - If the child is not no-window, removing generates
412 * more expose events. Bad, bad, bad.
414 * - Even if the child is no-window, removing it now (properly)
415 * clears the space where it was, so it does no good
419 remove_child = FALSE;
420 child = GTK_BUTTON (widget)->child;
424 if (!GTK_OPTION_MENU (widget)->menu)
426 gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
427 child = GTK_BUTTON (widget)->child;
433 child_event = *event;
435 if (GTK_WIDGET_NO_WINDOW (child) &&
436 gtk_widget_intersect (child, &event->area, &child_event.area))
437 gtk_widget_event (child, (GdkEvent*) &child_event);
440 gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
442 remove_child = FALSE;
443 child = GTK_BIN (widget)->child;
444 child_event = *event;
445 if (child && GTK_WIDGET_NO_WINDOW (child) &&
446 gtk_widget_intersect (child, &event->area, &child_event.area))
447 gtk_widget_event (child, (GdkEvent*) &child_event);
456 gtk_option_menu_button_press (GtkWidget *widget,
457 GdkEventButton *event)
459 GtkOptionMenu *option_menu;
461 g_return_val_if_fail (widget != NULL, FALSE);
462 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
463 g_return_val_if_fail (event != NULL, FALSE);
465 option_menu = GTK_OPTION_MENU (widget);
467 if ((event->type == GDK_BUTTON_PRESS) &&
468 (event->button == 1))
470 gtk_option_menu_remove_contents (option_menu);
471 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
472 gtk_option_menu_position, option_menu,
473 event->button, event->time);
481 gtk_option_menu_key_press (GtkWidget *widget,
484 GtkOptionMenu *option_menu;
486 g_return_val_if_fail (widget != NULL, FALSE);
487 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
488 g_return_val_if_fail (event != NULL, FALSE);
490 option_menu = GTK_OPTION_MENU (widget);
492 switch (event->keyval)
495 gtk_option_menu_remove_contents (option_menu);
496 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
497 gtk_option_menu_position, option_menu,
506 gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
507 GtkOptionMenu *option_menu)
509 g_return_if_fail (menu_shell != NULL);
510 g_return_if_fail (option_menu != NULL);
511 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
513 gtk_option_menu_update_contents (option_menu);
517 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
521 g_return_if_fail (option_menu != NULL);
522 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
524 if (option_menu->menu)
526 gtk_option_menu_remove_contents (option_menu);
528 option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
529 if (option_menu->menu_item)
531 gtk_widget_ref (option_menu->menu_item);
532 child = GTK_BIN (option_menu->menu_item)->child;
535 if (GTK_BIN (option_menu)->child)
536 gtk_container_remove (GTK_CONTAINER (option_menu),
537 GTK_BIN (option_menu)->child);
538 if (GTK_WIDGET (option_menu)->state != child->state)
539 gtk_widget_set_state (child, GTK_WIDGET (option_menu)->state);
540 gtk_widget_reparent (child, GTK_WIDGET (option_menu));
543 gtk_widget_size_request (child, &child->requisition);
544 gtk_widget_size_allocate (GTK_WIDGET (option_menu),
545 &(GTK_WIDGET (option_menu)->allocation));
547 if (GTK_WIDGET_DRAWABLE (option_menu))
548 gtk_widget_queue_draw (GTK_WIDGET (option_menu));
554 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
556 g_return_if_fail (option_menu != NULL);
557 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
559 if (GTK_BIN (option_menu)->child)
561 if (GTK_WIDGET (option_menu->menu_item)->state != GTK_BIN (option_menu)->child->state)
562 gtk_widget_set_state (GTK_BIN (option_menu)->child,
563 GTK_WIDGET (option_menu->menu_item)->state);
564 gtk_widget_reparent (GTK_BIN (option_menu)->child, option_menu->menu_item);
565 gtk_widget_unref (option_menu->menu_item);
566 option_menu->menu_item = NULL;
571 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
576 g_return_if_fail (option_menu != NULL);
577 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
579 option_menu->width = 0;
580 option_menu->height = 0;
582 if (option_menu->menu)
584 children = GTK_MENU_SHELL (option_menu->menu)->children;
587 child = children->data;
588 children = children->next;
590 if (GTK_WIDGET_VISIBLE (child))
592 gtk_widget_size_request (child, &child->requisition);
594 option_menu->width = MAX (option_menu->width, child->requisition.width);
595 option_menu->height = MAX (option_menu->height, child->requisition.height);
602 gtk_option_menu_position (GtkMenu *menu,
607 GtkOptionMenu *option_menu;
619 g_return_if_fail (user_data != NULL);
620 g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
622 option_menu = GTK_OPTION_MENU (user_data);
624 width = GTK_WIDGET (menu)->allocation.width;
625 height = GTK_WIDGET (menu)->allocation.height;
627 active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
628 children = GTK_MENU_SHELL (option_menu->menu)->children;
629 gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
631 menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
634 menu_ypos -= active->requisition.height / 2;
638 child = children->data;
643 if (GTK_WIDGET_VISIBLE (child))
644 menu_ypos -= child->allocation.height;
646 children = children->next;
649 screen_width = gdk_screen_width ();
650 screen_height = gdk_screen_height ();
658 else if ((menu_ypos + height) > screen_height)
660 menu_ypos -= ((menu_ypos + height) - screen_height);
666 if ((menu_xpos + GTK_WIDGET (option_menu)->allocation.width + width) <= screen_width)
667 menu_xpos += GTK_WIDGET (option_menu)->allocation.width;
674 else if ((menu_xpos + width) > screen_width)
675 menu_xpos -= ((menu_xpos + width) - screen_width);
683 gtk_option_menu_show_all (GtkWidget *widget)
685 GtkContainer *container;
686 GtkOptionMenu *option_menu;
688 g_return_if_fail (widget != NULL);
689 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
690 container = GTK_CONTAINER (widget);
691 option_menu = GTK_OPTION_MENU (widget);
693 gtk_widget_show (widget);
694 gtk_container_foreach (container, (GtkCallback) gtk_widget_show_all, NULL);
695 if (option_menu->menu)
696 gtk_widget_show_all (option_menu->menu);
697 if (option_menu->menu_item)
698 gtk_widget_show_all (option_menu->menu_item);
703 gtk_option_menu_hide_all (GtkWidget *widget)
705 GtkContainer *container;
707 g_return_if_fail (widget != NULL);
708 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
709 container = GTK_CONTAINER (widget);
711 gtk_widget_hide (widget);
712 gtk_container_foreach (container, (GtkCallback) gtk_widget_hide_all, NULL);