1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Jsh 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/.
30 #include <gdk/gdkkeysyms.h>
32 #include "claws-marshal.h"
33 #include "gtkcmoptionmenu.h"
37 #define CHILD_LEFT_SPACING 4
38 #define CHILD_RIGHT_SPACING 1
39 #define CHILD_TOP_SPACING 1
40 #define CHILD_BOTTOM_SPACING 1
42 typedef struct _GtkCMOptionMenuProps GtkCMOptionMenuProps
;
44 struct _GtkCMOptionMenuProps
46 gboolean interior_focus
;
47 GtkRequisition indicator_size
;
48 GtkBorder indicator_spacing
;
53 static const GtkCMOptionMenuProps default_props
= {
56 { 7, 5, 2, 2 }, /* Left, right, top, bottom */
61 static void gtk_cmoption_menu_destroy (GtkObject
*object
);
62 static void gtk_cmoption_menu_set_property (GObject
*object
,
66 static void gtk_cmoption_menu_get_property (GObject
*object
,
70 static void gtk_cmoption_menu_size_request (GtkWidget
*widget
,
71 GtkRequisition
*requisition
);
72 static void gtk_cmoption_menu_size_allocate (GtkWidget
*widget
,
73 GtkAllocation
*allocation
);
74 static void gtk_cmoption_menu_paint (GtkWidget
*widget
,
76 static gint
gtk_cmoption_menu_expose (GtkWidget
*widget
,
77 GdkEventExpose
*event
);
78 static gint
gtk_cmoption_menu_button_press (GtkWidget
*widget
,
79 GdkEventButton
*event
);
80 static gint
gtk_cmoption_menu_key_press (GtkWidget
*widget
,
82 static void gtk_cmoption_menu_selection_done (GtkMenuShell
*menu_shell
,
83 GtkCMOptionMenu
*option_menu
);
84 static void gtk_cmoption_menu_update_contents (GtkCMOptionMenu
*option_menu
);
85 static void gtk_cmoption_menu_remove_contents (GtkCMOptionMenu
*option_menu
);
86 static void gtk_cmoption_menu_calc_size (GtkCMOptionMenu
*option_menu
);
87 static void gtk_cmoption_menu_position (GtkMenu
*menu
,
92 static void gtk_cmoption_menu_show_all (GtkWidget
*widget
);
93 static void gtk_cmoption_menu_hide_all (GtkWidget
*widget
);
94 static gboolean
gtk_cmoption_menu_mnemonic_activate (GtkWidget
*widget
,
95 gboolean group_cycling
);
96 static GType
gtk_cmoption_menu_child_type (GtkContainer
*container
);
97 static gint
gtk_cmoption_menu_scroll_event (GtkWidget
*widget
,
98 GdkEventScroll
*event
);
113 static guint signals
[LAST_SIGNAL
] = { 0 };
115 G_DEFINE_TYPE (GtkCMOptionMenu
, gtk_cmoption_menu
, GTK_TYPE_BUTTON
)
118 gtk_cmoption_menu_class_init (GtkCMOptionMenuClass
*class)
120 GObjectClass
*gobject_class
;
121 GtkObjectClass
*object_class
;
122 GtkWidgetClass
*widget_class
;
123 GtkContainerClass
*container_class
;
125 gobject_class
= (GObjectClass
*) class;
126 object_class
= (GtkObjectClass
*) class;
127 widget_class
= (GtkWidgetClass
*) class;
128 container_class
= (GtkContainerClass
*) class;
131 g_signal_new (("changed"),
132 G_OBJECT_CLASS_TYPE (class),
134 G_STRUCT_OFFSET (GtkCMOptionMenuClass
, changed
),
136 claws_marshal_VOID__VOID
,
139 gobject_class
->set_property
= gtk_cmoption_menu_set_property
;
140 gobject_class
->get_property
= gtk_cmoption_menu_get_property
;
141 object_class
->destroy
= gtk_cmoption_menu_destroy
;
143 widget_class
->size_request
= gtk_cmoption_menu_size_request
;
144 widget_class
->size_allocate
= gtk_cmoption_menu_size_allocate
;
145 widget_class
->expose_event
= gtk_cmoption_menu_expose
;
146 widget_class
->button_press_event
= gtk_cmoption_menu_button_press
;
147 widget_class
->key_press_event
= gtk_cmoption_menu_key_press
;
148 widget_class
->scroll_event
= gtk_cmoption_menu_scroll_event
;
149 widget_class
->show_all
= gtk_cmoption_menu_show_all
;
150 widget_class
->hide_all
= gtk_cmoption_menu_hide_all
;
151 widget_class
->mnemonic_activate
= gtk_cmoption_menu_mnemonic_activate
;
153 container_class
->child_type
= gtk_cmoption_menu_child_type
;
155 g_object_class_install_property (gobject_class
,
157 g_param_spec_object ("menu",
159 ("The menu of options"),
163 gtk_widget_class_install_style_property (widget_class
,
164 g_param_spec_boxed ("indicator-size",
166 ("Size of dropdown indicator"),
167 GTK_TYPE_REQUISITION
,
169 gtk_widget_class_install_style_property (widget_class
,
170 g_param_spec_boxed ("indicator-spacing",
171 ("Indicator Spacing"),
172 ("Spacing around indicator"),
178 gtk_cmoption_menu_child_type (GtkContainer
*container
)
184 gtk_cmoption_menu_init (GtkCMOptionMenu
*option_menu
)
186 gtk_widget_set_can_focus (GTK_WIDGET(option_menu
), TRUE
);
187 gtk_widget_set_can_default (GTK_WIDGET(option_menu
), FALSE
);
188 gtk_widget_set_receives_default (GTK_WIDGET(option_menu
), FALSE
);
190 option_menu
->menu
= NULL
;
191 option_menu
->menu_item
= NULL
;
192 option_menu
->width
= 0;
193 option_menu
->height
= 0;
197 gtk_cmoption_menu_new (void)
199 return g_object_new (GTK_TYPE_CMOPTION_MENU
, NULL
);
203 gtk_cmoption_menu_get_menu (GtkCMOptionMenu
*option_menu
)
205 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (option_menu
), NULL
);
207 return option_menu
->menu
;
211 gtk_cmoption_menu_detacher (GtkWidget
*widget
,
214 GtkCMOptionMenu
*option_menu
;
216 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget
));
218 option_menu
= GTK_CMOPTION_MENU (widget
);
219 cm_return_if_fail (option_menu
->menu
== (GtkWidget
*) menu
);
221 gtk_cmoption_menu_remove_contents (option_menu
);
222 g_signal_handlers_disconnect_by_func (option_menu
->menu
,
223 gtk_cmoption_menu_selection_done
,
225 g_signal_handlers_disconnect_by_func (option_menu
->menu
,
226 gtk_cmoption_menu_calc_size
,
229 option_menu
->menu
= NULL
;
230 g_object_notify (G_OBJECT (option_menu
), "menu");
234 gtk_cmoption_menu_set_menu (GtkCMOptionMenu
*option_menu
,
237 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
238 cm_return_if_fail (GTK_IS_MENU (menu
));
240 if (option_menu
->menu
!= menu
)
242 gtk_cmoption_menu_remove_menu (option_menu
);
244 option_menu
->menu
= menu
;
245 gtk_menu_attach_to_widget (GTK_MENU (menu
),
246 GTK_WIDGET (option_menu
),
247 gtk_cmoption_menu_detacher
);
249 gtk_cmoption_menu_calc_size (option_menu
);
251 g_signal_connect_after (option_menu
->menu
, "selection-done",
252 G_CALLBACK (gtk_cmoption_menu_selection_done
),
254 g_signal_connect_swapped (option_menu
->menu
, "size_request",
255 G_CALLBACK (gtk_cmoption_menu_calc_size
),
258 if (gtk_widget_get_parent (GTK_WIDGET (option_menu
)))
259 gtk_widget_queue_resize (GTK_WIDGET (option_menu
));
261 gtk_cmoption_menu_update_contents (option_menu
);
263 g_object_notify (G_OBJECT (option_menu
), "menu");
268 gtk_cmoption_menu_remove_menu (GtkCMOptionMenu
*option_menu
)
270 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
272 if (option_menu
->menu
)
274 if (option_menu
->menu_item
==
275 gtk_menu_get_active (GTK_MENU (option_menu
->menu
)))
276 gtk_menu_shell_cancel (GTK_MENU_SHELL (option_menu
->menu
));
278 gtk_menu_detach (GTK_MENU (option_menu
->menu
));
283 gtk_cmoption_menu_set_history (GtkCMOptionMenu
*option_menu
,
286 GtkWidget
*menu_item
;
288 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
290 if (option_menu
->menu
)
292 gtk_menu_set_active (GTK_MENU (option_menu
->menu
), index
);
293 menu_item
= gtk_menu_get_active (GTK_MENU (option_menu
->menu
));
295 if (menu_item
!= option_menu
->menu_item
)
296 gtk_cmoption_menu_update_contents (option_menu
);
301 * gtk_cmoption_menu_get_history:
302 * @option_menu: a #GtkCMOptionMenu
304 * Retrieves the index of the currently selected menu item. The menu
305 * items are numbered from top to bottom, starting with 0.
307 * Return value: index of the selected menu item, or -1 if there are no menu items
308 * Deprecated: 2.4: Use #GtkComboBox instead.
311 gtk_cmoption_menu_get_history (GtkCMOptionMenu
*option_menu
)
313 GtkWidget
*active_widget
;
317 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (option_menu
), -1);
319 if (option_menu
->menu
)
321 active_widget
= gtk_menu_get_active (GTK_MENU (option_menu
->menu
));
325 list
= gtk_container_get_children (GTK_CONTAINER
326 (GTK_MENU_SHELL (option_menu
->menu
)));
327 index
= g_list_index (list
, active_widget
);
340 gtk_cmoption_menu_set_property (GObject
*object
,
345 GtkCMOptionMenu
*option_menu
= GTK_CMOPTION_MENU (object
);
350 gtk_cmoption_menu_set_menu (option_menu
, g_value_get_object (value
));
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
360 gtk_cmoption_menu_get_property (GObject
*object
,
365 GtkCMOptionMenu
*option_menu
= GTK_CMOPTION_MENU (object
);
370 g_value_set_object (value
, option_menu
->menu
);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
380 gtk_cmoption_menu_destroy (GtkObject
*object
)
382 GtkCMOptionMenu
*option_menu
;
384 cm_return_if_fail (GTK_IS_CMOPTION_MENU (object
));
386 option_menu
= GTK_CMOPTION_MENU (object
);
388 if (option_menu
->menu
)
389 gtk_widget_destroy (option_menu
->menu
);
391 if (GTK_OBJECT_CLASS (gtk_cmoption_menu_parent_class
)->destroy
)
392 (* GTK_OBJECT_CLASS (gtk_cmoption_menu_parent_class
)->destroy
) (object
);
396 gtk_cmoption_menu_get_props (GtkCMOptionMenu
*option_menu
,
397 GtkCMOptionMenuProps
*props
)
399 GtkRequisition
*indicator_size
;
400 GtkBorder
*indicator_spacing
;
402 gtk_widget_style_get (GTK_WIDGET (option_menu
),
403 "indicator-size", &indicator_size
,
404 "indicator-spacing", &indicator_spacing
,
405 "interior-focus", &props
->interior_focus
,
406 "focus-line-width", &props
->focus_width
,
407 "focus-padding", &props
->focus_pad
,
411 props
->indicator_size
= *indicator_size
;
413 props
->indicator_size
= default_props
.indicator_size
;
415 if (indicator_spacing
)
416 props
->indicator_spacing
= *indicator_spacing
;
418 props
->indicator_spacing
= default_props
.indicator_spacing
;
420 gtk_requisition_free (indicator_size
);
421 gtk_border_free (indicator_spacing
);
425 gtk_cmoption_menu_size_request (GtkWidget
*widget
,
426 GtkRequisition
*requisition
)
428 GtkCMOptionMenu
*option_menu
= GTK_CMOPTION_MENU (widget
);
429 GtkCMOptionMenuProps props
;
432 GtkRequisition child_requisition
= { 0, 0 };
436 gtk_cmoption_menu_get_props (option_menu
, &props
);
438 child
= gtk_bin_get_child (GTK_BIN (option_menu
));
439 if (child
&& gtk_widget_get_visible (child
))
441 gtk_widget_size_request (child
, &child_requisition
);
443 requisition
->width
+= child_requisition
.width
;
444 requisition
->height
+= child_requisition
.height
;
447 border_width
= gtk_container_get_border_width (GTK_CONTAINER (widget
));
448 style
= gtk_widget_get_style (widget
);
449 requisition
->width
= ((border_width
+
450 style
->xthickness
+ props
.focus_pad
) * 2 +
451 MAX (child_requisition
.width
, option_menu
->width
) +
452 props
.indicator_size
.width
+
453 props
.indicator_spacing
.left
+ props
.indicator_spacing
.right
+
454 CHILD_LEFT_SPACING
+ CHILD_RIGHT_SPACING
+ props
.focus_width
* 2);
455 requisition
->height
= ((border_width
+
456 style
->ythickness
+ props
.focus_pad
) * 2 +
457 MAX (child_requisition
.height
, option_menu
->height
) +
458 CHILD_TOP_SPACING
+ CHILD_BOTTOM_SPACING
+ props
.focus_width
* 2);
460 tmp
= (requisition
->height
- MAX (child_requisition
.height
, option_menu
->height
) +
461 props
.indicator_size
.height
+ props
.indicator_spacing
.top
+ props
.indicator_spacing
.bottom
);
462 requisition
->height
= MAX (requisition
->height
, tmp
);
466 gtk_cmoption_menu_size_allocate (GtkWidget
*widget
,
467 GtkAllocation
*allocation
)
471 GtkButton
*button
= GTK_BUTTON (widget
);
472 GtkAllocation child_allocation
, widget_allocation
;
473 GtkCMOptionMenuProps props
;
476 gtk_cmoption_menu_get_props (GTK_CMOPTION_MENU (widget
), &props
);
477 border_width
= gtk_container_get_border_width (GTK_CONTAINER (widget
));
479 gtk_widget_set_allocation (widget
, allocation
);
480 if (gtk_widget_get_realized (widget
))
481 gdk_window_move_resize (gtk_button_get_event_window (button
),
482 allocation
->x
+ border_width
, allocation
->y
+ border_width
,
483 allocation
->width
- border_width
* 2, allocation
->height
- border_width
* 2);
485 child
= gtk_bin_get_child (GTK_BIN (widget
));
486 if (child
&& gtk_widget_get_visible (child
))
488 style
= gtk_widget_get_style (widget
);
489 gint xthickness
= style
->xthickness
;
490 gint ythickness
= style
->ythickness
;
492 gtk_widget_get_allocation (widget
, &widget_allocation
);
493 child_allocation
.x
= widget_allocation
.x
+ border_width
+ xthickness
+ props
.focus_width
+ props
.focus_pad
+ CHILD_LEFT_SPACING
;
494 child_allocation
.y
= widget_allocation
.y
+ border_width
+ ythickness
+ props
.focus_width
+ props
.focus_pad
+ CHILD_TOP_SPACING
;
495 child_allocation
.width
= MAX (1, allocation
->width
- (border_width
+ xthickness
+ props
.focus_width
+ props
.focus_pad
) * 2 -
496 props
.indicator_size
.width
- props
.indicator_spacing
.left
- props
.indicator_spacing
.right
-
497 CHILD_LEFT_SPACING
- CHILD_RIGHT_SPACING
);
498 child_allocation
.height
= MAX (1, allocation
->height
- (border_width
+ ythickness
+ props
.focus_width
+ props
.focus_pad
) * 2 -
499 CHILD_TOP_SPACING
- CHILD_BOTTOM_SPACING
);
501 if (gtk_widget_get_direction (GTK_WIDGET (widget
)) == GTK_TEXT_DIR_RTL
)
502 child_allocation
.x
+= props
.indicator_size
.width
+ props
.indicator_spacing
.left
+ props
.indicator_spacing
.right
;
504 gtk_widget_size_allocate (child
, &child_allocation
);
509 gtk_cmoption_menu_paint (GtkWidget
*widget
,
512 GtkAllocation allocation
;
514 GdkRectangle button_area
;
516 GtkCMOptionMenuProps props
;
520 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget
));
521 cm_return_if_fail (area
!= NULL
);
523 if (gtk_widget_is_drawable (widget
))
525 gtk_widget_get_allocation (widget
, &allocation
);
526 border_width
= gtk_container_get_border_width (GTK_CONTAINER (widget
));
527 gtk_cmoption_menu_get_props (GTK_CMOPTION_MENU (widget
), &props
);
529 button_area
.x
= allocation
.x
+ border_width
;
530 button_area
.y
= allocation
.y
+ border_width
;
531 button_area
.width
= allocation
.width
- 2 * border_width
;
532 button_area
.height
= allocation
.height
- 2 * border_width
;
534 if (!props
.interior_focus
&& gtk_widget_has_focus (widget
))
536 button_area
.x
+= props
.focus_width
+ props
.focus_pad
;
537 button_area
.y
+= props
.focus_width
+ props
.focus_pad
;
538 button_area
.width
-= 2 * (props
.focus_width
+ props
.focus_pad
);
539 button_area
.height
-= 2 * (props
.focus_width
+ props
.focus_pad
);
542 style
= gtk_widget_get_style (widget
);
543 window
= gtk_widget_get_window (widget
);
544 gtk_paint_box (style
, window
,
545 gtk_widget_get_state (widget
), GTK_SHADOW_OUT
,
546 area
, widget
, "optionmenu",
547 button_area
.x
, button_area
.y
,
548 button_area
.width
, button_area
.height
);
550 if (gtk_widget_get_direction (GTK_WIDGET (widget
)) == GTK_TEXT_DIR_RTL
)
551 tab_x
= button_area
.x
+ props
.indicator_spacing
.right
+
554 tab_x
= button_area
.x
+ button_area
.width
-
555 props
.indicator_size
.width
- props
.indicator_spacing
.right
-
558 gtk_paint_tab (style
, window
,
559 gtk_widget_get_state (widget
), GTK_SHADOW_OUT
,
560 area
, widget
, "optionmenutab",
562 button_area
.y
+ (button_area
.height
- props
.indicator_size
.height
) / 2,
563 props
.indicator_size
.width
, props
.indicator_size
.height
);
565 if (gtk_widget_has_focus (widget
))
567 if (props
.interior_focus
)
569 button_area
.x
+= style
->xthickness
+ props
.focus_pad
;
570 button_area
.y
+= style
->ythickness
+ props
.focus_pad
;
571 button_area
.width
-= 2 * (style
->xthickness
+ props
.focus_pad
) +
572 props
.indicator_spacing
.left
+
573 props
.indicator_spacing
.right
+
574 props
.indicator_size
.width
;
575 button_area
.height
-= 2 * (style
->ythickness
+ props
.focus_pad
);
576 if (gtk_widget_get_direction (GTK_WIDGET (widget
)) == GTK_TEXT_DIR_RTL
)
577 button_area
.x
+= props
.indicator_spacing
.left
+
578 props
.indicator_spacing
.right
+
579 props
.indicator_size
.width
;
583 button_area
.x
-= props
.focus_width
+ props
.focus_pad
;
584 button_area
.y
-= props
.focus_width
+ props
.focus_pad
;
585 button_area
.width
+= 2 * (props
.focus_width
+ props
.focus_pad
);
586 button_area
.height
+= 2 * (props
.focus_width
+ props
.focus_pad
);
589 gtk_paint_focus (style
, window
, gtk_widget_get_state (widget
),
590 area
, widget
, "button",
600 gtk_cmoption_menu_expose (GtkWidget
*widget
,
601 GdkEventExpose
*event
)
604 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (widget
), FALSE
);
605 cm_return_val_if_fail (event
!= NULL
, FALSE
);
607 if (gtk_widget_is_drawable (widget
))
609 gtk_cmoption_menu_paint (widget
, &event
->area
);
612 /* The following code tries to draw the child in two places at
613 * once. It fails miserably for several reasons
615 * - If the child is not no-window, removing generates
616 * more expose events. Bad, bad, bad.
618 * - Even if the child is no-window, removing it now (properly)
619 * clears the space where it was, so it does no good
623 remove_child
= FALSE
;
624 child
= GTK_BUTTON (widget
)->child
;
628 if (!GTK_CMOPTION_MENU (widget
)->menu
)
630 gtk_cmoption_menu_update_contents (GTK_CMOPTION_MENU (widget
));
631 child
= GTK_BUTTON (widget
)->child
;
637 child_event
= *event
;
639 if (GTK_WIDGET_NO_WINDOW (child
) &&
640 gtk_widget_intersect (child
, &event
->area
, &child_event
.area
))
641 gtk_widget_event (child
, (GdkEvent
*) &child_event
);
644 gtk_cmoption_menu_remove_contents (GTK_CMOPTION_MENU (widget
));
646 child
= gtk_bin_get_child (GTK_BIN (widget
));
648 gtk_container_propagate_expose (GTK_CONTAINER (widget
),
658 gtk_cmoption_menu_button_press (GtkWidget
*widget
,
659 GdkEventButton
*event
)
661 GtkCMOptionMenu
*option_menu
;
662 GtkWidget
*menu_item
;
664 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (widget
), FALSE
);
665 cm_return_val_if_fail (event
!= NULL
, FALSE
);
667 option_menu
= GTK_CMOPTION_MENU (widget
);
669 if ((event
->type
== GDK_BUTTON_PRESS
) &&
670 (event
->button
== 1))
672 gtk_cmoption_menu_remove_contents (option_menu
);
673 gtk_menu_popup (GTK_MENU (option_menu
->menu
), NULL
, NULL
,
674 gtk_cmoption_menu_position
, option_menu
,
675 event
->button
, event
->time
);
676 menu_item
= gtk_menu_get_active (GTK_MENU (option_menu
->menu
));
678 gtk_menu_shell_select_item (GTK_MENU_SHELL (option_menu
->menu
), menu_item
);
686 gtk_cmoption_menu_key_press (GtkWidget
*widget
,
689 GtkCMOptionMenu
*option_menu
;
690 GtkWidget
*menu_item
;
692 cm_return_val_if_fail (GTK_IS_CMOPTION_MENU (widget
), FALSE
);
693 cm_return_val_if_fail (event
!= NULL
, FALSE
);
695 option_menu
= GTK_CMOPTION_MENU (widget
);
697 switch (event
->keyval
)
699 case GDK_KEY_KP_Space
:
701 gtk_cmoption_menu_remove_contents (option_menu
);
702 gtk_menu_popup (GTK_MENU (option_menu
->menu
), NULL
, NULL
,
703 gtk_cmoption_menu_position
, option_menu
,
705 menu_item
= gtk_menu_get_active (GTK_MENU (option_menu
->menu
));
707 gtk_menu_shell_select_item (GTK_MENU_SHELL (option_menu
->menu
), menu_item
);
715 gtk_cmoption_menu_selection_done (GtkMenuShell
*menu_shell
,
716 GtkCMOptionMenu
*option_menu
)
718 cm_return_if_fail (menu_shell
!= NULL
);
719 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
721 gtk_cmoption_menu_update_contents (option_menu
);
725 gtk_cmoption_menu_changed (GtkCMOptionMenu
*option_menu
)
727 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
729 g_signal_emit (option_menu
, signals
[CHANGED
], 0);
733 gtk_cmoption_menu_select_first_sensitive (GtkCMOptionMenu
*option_menu
)
735 if (option_menu
->menu
)
737 GList
*children
= gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu
->menu
)));
742 if (gtk_widget_get_sensitive (children
->data
))
744 gtk_cmoption_menu_set_history (option_menu
, index
);
748 children
= children
->next
;
752 g_list_free (children
);
757 gtk_cmoption_menu_item_state_changed_cb (GtkWidget
*widget
,
758 GtkStateType previous_state
,
759 GtkCMOptionMenu
*option_menu
)
761 GtkWidget
*child
= gtk_bin_get_child (GTK_BIN (option_menu
));
763 if (child
&& gtk_widget_get_sensitive (child
) != gtk_widget_is_sensitive (widget
))
764 gtk_widget_set_sensitive (child
, gtk_widget_is_sensitive (widget
));
768 gtk_cmoption_menu_item_destroy_cb (GtkWidget
*widget
,
769 GtkCMOptionMenu
*option_menu
)
771 GtkWidget
*child
= gtk_bin_get_child (GTK_BIN (option_menu
));
775 g_object_ref (child
);
776 gtk_cmoption_menu_remove_contents (option_menu
);
777 gtk_widget_destroy (child
);
778 g_object_unref (child
);
780 gtk_cmoption_menu_select_first_sensitive (option_menu
);
785 gtk_cmoption_menu_update_contents (GtkCMOptionMenu
*option_menu
)
788 GtkRequisition child_requisition
;
789 GtkAllocation allocation
;
791 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
793 if (option_menu
->menu
)
795 GtkWidget
*old_item
= option_menu
->menu_item
;
797 gtk_cmoption_menu_remove_contents (option_menu
);
799 option_menu
->menu_item
= gtk_menu_get_active (GTK_MENU (option_menu
->menu
));
800 if (option_menu
->menu_item
)
802 g_object_ref (option_menu
->menu_item
);
803 child
= gtk_bin_get_child (GTK_BIN (option_menu
->menu_item
));
806 if (!gtk_widget_is_sensitive (option_menu
->menu_item
))
807 gtk_widget_set_sensitive (child
, FALSE
);
808 gtk_widget_reparent (child
, GTK_WIDGET (option_menu
));
811 g_signal_connect (option_menu
->menu_item
, "state_changed",
812 G_CALLBACK (gtk_cmoption_menu_item_state_changed_cb
), option_menu
);
813 g_signal_connect (option_menu
->menu_item
, "destroy",
814 G_CALLBACK (gtk_cmoption_menu_item_destroy_cb
), option_menu
);
816 gtk_widget_size_request (child
, &child_requisition
);
817 gtk_widget_get_allocation (GTK_WIDGET (option_menu
), &allocation
);
818 gtk_widget_size_allocate (GTK_WIDGET (option_menu
),
821 if (gtk_widget_is_drawable (GTK_WIDGET(option_menu
)))
822 gtk_widget_queue_draw (GTK_WIDGET (option_menu
));
825 if (old_item
!= option_menu
->menu_item
)
826 gtk_cmoption_menu_changed (option_menu
);
831 gtk_cmoption_menu_remove_contents (GtkCMOptionMenu
*option_menu
)
835 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
837 if (option_menu
->menu_item
)
839 child
= gtk_bin_get_child (GTK_BIN (option_menu
));
843 gtk_widget_set_sensitive (child
, TRUE
);
844 gtk_widget_set_state (child
, GTK_STATE_NORMAL
);
845 gtk_widget_reparent (child
, option_menu
->menu_item
);
848 g_signal_handlers_disconnect_by_func (option_menu
->menu_item
,
849 gtk_cmoption_menu_item_state_changed_cb
,
851 g_signal_handlers_disconnect_by_func (option_menu
->menu_item
,
852 gtk_cmoption_menu_item_destroy_cb
,
855 g_object_unref (option_menu
->menu_item
);
856 option_menu
->menu_item
= NULL
;
861 gtk_cmoption_menu_calc_size (GtkCMOptionMenu
*option_menu
)
864 GList
*children
, *walk
;
865 GtkRequisition child_requisition
;
866 gint old_width
= option_menu
->width
;
867 gint old_height
= option_menu
->height
;
869 cm_return_if_fail (GTK_IS_CMOPTION_MENU (option_menu
));
871 option_menu
->width
= 0;
872 option_menu
->height
= 0;
874 if (option_menu
->menu
)
876 children
= gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu
->menu
)));
883 if (gtk_widget_get_visible (child
))
885 GtkWidget
*inner
= gtk_bin_get_child (GTK_BIN (child
));
889 gtk_widget_size_request (inner
, &child_requisition
);
891 option_menu
->width
= MAX (option_menu
->width
, child_requisition
.width
);
892 option_menu
->height
= MAX (option_menu
->height
, child_requisition
.height
);
897 g_list_free(children
);
900 if (old_width
!= option_menu
->width
|| old_height
!= option_menu
->height
)
901 gtk_widget_queue_resize (GTK_WIDGET (option_menu
));
905 gtk_cmoption_menu_position (GtkMenu
*menu
,
911 GtkCMOptionMenu
*option_menu
;
915 GtkRequisition requisition
;
916 GtkAllocation allocation
;
917 GList
*children
, *cur
;
923 cm_return_if_fail (GTK_IS_CMOPTION_MENU (user_data
));
925 option_menu
= GTK_CMOPTION_MENU (user_data
);
926 widget
= GTK_WIDGET (option_menu
);
928 gtk_widget_get_child_requisition (GTK_WIDGET (menu
), &requisition
);
929 menu_width
= requisition
.width
;
931 active
= gtk_menu_get_active (GTK_MENU (option_menu
->menu
));
932 gdk_window_get_origin (gtk_widget_get_window (widget
), &menu_xpos
, &menu_ypos
);
934 gtk_widget_get_allocation (widget
, &allocation
);
935 menu_xpos
+= allocation
.x
;
936 menu_ypos
+= allocation
.y
+ allocation
.height
/ 2 - 2;
940 gtk_widget_get_child_requisition (active
, &requisition
);
941 menu_ypos
-= requisition
.height
/ 2;
944 children
= gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu
->menu
)));
945 for (cur
= children
; cur
&& cur
->data
; cur
= cur
->next
)
952 if (gtk_widget_get_visible (child
))
954 gtk_widget_get_child_requisition (child
, &requisition
);
955 menu_ypos
-= requisition
.height
;
959 g_list_free (children
);
961 if (gtk_widget_get_direction (widget
) == GTK_TEXT_DIR_RTL
)
962 menu_xpos
= menu_xpos
+ allocation
.width
- menu_width
;
964 /* Clamp the position on screen */
965 screen_width
= gdk_screen_get_width (gtk_widget_get_screen (widget
));
969 else if ((menu_xpos
+ menu_width
) > screen_width
)
970 menu_xpos
-= ((menu_xpos
+ menu_width
) - screen_width
);
979 gtk_cmoption_menu_show_all (GtkWidget
*widget
)
981 GtkContainer
*container
;
982 GtkCMOptionMenu
*option_menu
;
984 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget
));
985 container
= GTK_CONTAINER (widget
);
986 option_menu
= GTK_CMOPTION_MENU (widget
);
988 gtk_widget_show (widget
);
989 gtk_container_foreach (container
, (GtkCallback
) gtk_widget_show_all
, NULL
);
990 if (option_menu
->menu
)
991 gtk_widget_show_all (option_menu
->menu
);
992 if (option_menu
->menu_item
)
993 gtk_widget_show_all (option_menu
->menu_item
);
998 gtk_cmoption_menu_hide_all (GtkWidget
*widget
)
1000 GtkContainer
*container
;
1002 cm_return_if_fail (GTK_IS_CMOPTION_MENU (widget
));
1003 container
= GTK_CONTAINER (widget
);
1005 gtk_widget_hide (widget
);
1006 gtk_container_foreach (container
, (GtkCallback
) gtk_widget_hide
, NULL
);
1010 gtk_cmoption_menu_mnemonic_activate (GtkWidget
*widget
,
1011 gboolean group_cycling
)
1013 gtk_widget_grab_focus (widget
);
1018 gtk_cmoption_menu_scroll_event (GtkWidget
*widget
,
1019 GdkEventScroll
*event
)
1021 GtkCMOptionMenu
*option_menu
= GTK_CMOPTION_MENU (widget
);
1022 gint index
, n_children
;
1024 GList
*l
, *children
;
1027 index
= gtk_cmoption_menu_get_history (option_menu
);
1031 children
= gtk_container_get_children (GTK_CONTAINER (GTK_MENU_SHELL (option_menu
->menu
)));
1032 n_children
= (gint
)g_list_length (children
);
1034 if (event
->direction
== GDK_SCROLL_UP
)
1046 if (index
>= n_children
)
1049 l
= g_list_nth (children
, (guint
)index
);
1050 item
= GTK_MENU_ITEM (l
->data
);
1051 if (gtk_widget_get_visible (GTK_WIDGET(item
)) &&
1052 gtk_widget_is_sensitive (GTK_WIDGET(item
)))
1054 gtk_cmoption_menu_set_history (option_menu
, index
);
1055 gtk_menu_item_activate (GTK_MENU_ITEM (item
));
1061 g_list_free (children
);