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/.
31 #include "gtkaccellabel.h"
33 #include "gtkmarshalers.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkmenuitemprivate.h"
37 #include "gtkmenubar.h"
38 #include "gtkmenuprivate.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtkprivate.h"
41 #include "gtkbuildable.h"
42 #include "gtkactivatable.h"
44 #include "gtktypebuiltins.h"
65 /* activatable properties */
66 PROP_ACTIVATABLE_RELATED_ACTION,
67 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
71 static void gtk_menu_item_dispose (GObject *object);
72 static void gtk_menu_item_set_property (GObject *object,
76 static void gtk_menu_item_get_property (GObject *object,
80 static void gtk_menu_item_destroy (GtkWidget *widget);
81 static void gtk_menu_item_size_allocate (GtkWidget *widget,
82 GtkAllocation *allocation);
83 static void gtk_menu_item_realize (GtkWidget *widget);
84 static void gtk_menu_item_unrealize (GtkWidget *widget);
85 static void gtk_menu_item_map (GtkWidget *widget);
86 static void gtk_menu_item_unmap (GtkWidget *widget);
87 static gboolean gtk_menu_item_enter (GtkWidget *widget,
88 GdkEventCrossing *event);
89 static gboolean gtk_menu_item_leave (GtkWidget *widget,
90 GdkEventCrossing *event);
91 static gboolean gtk_menu_item_draw (GtkWidget *widget,
93 static void gtk_menu_item_parent_set (GtkWidget *widget,
94 GtkWidget *previous_parent);
97 static void gtk_real_menu_item_select (GtkMenuItem *item);
98 static void gtk_real_menu_item_deselect (GtkMenuItem *item);
99 static void gtk_real_menu_item_activate (GtkMenuItem *item);
100 static void gtk_real_menu_item_activate_item (GtkMenuItem *item);
101 static void gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
103 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
105 static gboolean gtk_menu_item_mnemonic_activate (GtkWidget *widget,
106 gboolean group_cycling);
108 static void gtk_menu_item_ensure_label (GtkMenuItem *menu_item);
109 static gint gtk_menu_item_popup_timeout (gpointer data);
110 static void gtk_menu_item_position_menu (GtkMenu *menu,
115 static void gtk_menu_item_show_all (GtkWidget *widget);
116 static void gtk_menu_item_forall (GtkContainer *container,
117 gboolean include_internals,
118 GtkCallback callback,
119 gpointer callback_data);
120 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
123 static void gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
125 static G_CONST_RETURN gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
127 static void gtk_menu_item_get_preferred_width (GtkWidget *widget,
130 static void gtk_menu_item_get_preferred_height (GtkWidget *widget,
133 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
138 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface);
139 static void gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
143 static void gtk_menu_item_buildable_custom_finished(GtkBuildable *buildable,
146 const gchar *tagname,
149 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface);
150 static void gtk_menu_item_update (GtkActivatable *activatable,
152 const gchar *property_name);
153 static void gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
155 static void gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
157 static void gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
158 gboolean use_appearance);
161 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
163 static GtkBuildableIface *parent_buildable_iface;
165 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
166 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
167 gtk_menu_item_buildable_interface_init)
168 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
169 gtk_menu_item_activatable_interface_init))
173 gtk_menu_item_class_init (GtkMenuItemClass *klass)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
176 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
177 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
179 gobject_class->dispose = gtk_menu_item_dispose;
180 gobject_class->set_property = gtk_menu_item_set_property;
181 gobject_class->get_property = gtk_menu_item_get_property;
183 widget_class->destroy = gtk_menu_item_destroy;
184 widget_class->size_allocate = gtk_menu_item_size_allocate;
185 widget_class->draw = gtk_menu_item_draw;
186 widget_class->realize = gtk_menu_item_realize;
187 widget_class->unrealize = gtk_menu_item_unrealize;
188 widget_class->map = gtk_menu_item_map;
189 widget_class->unmap = gtk_menu_item_unmap;
190 widget_class->enter_notify_event = gtk_menu_item_enter;
191 widget_class->leave_notify_event = gtk_menu_item_leave;
192 widget_class->show_all = gtk_menu_item_show_all;
193 widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
194 widget_class->parent_set = gtk_menu_item_parent_set;
195 widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
196 widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
197 widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
198 widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
200 container_class->forall = gtk_menu_item_forall;
202 klass->activate = gtk_real_menu_item_activate;
203 klass->activate_item = gtk_real_menu_item_activate_item;
204 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
205 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
206 klass->set_label = gtk_real_menu_item_set_label;
207 klass->get_label = gtk_real_menu_item_get_label;
208 klass->select = gtk_real_menu_item_select;
209 klass->deselect = gtk_real_menu_item_deselect;
211 klass->hide_on_activate = TRUE;
213 menu_item_signals[ACTIVATE] =
214 g_signal_new (I_("activate"),
215 G_OBJECT_CLASS_TYPE (gobject_class),
216 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
217 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
219 _gtk_marshal_VOID__VOID,
221 widget_class->activate_signal = menu_item_signals[ACTIVATE];
223 menu_item_signals[ACTIVATE_ITEM] =
224 g_signal_new (I_("activate-item"),
225 G_OBJECT_CLASS_TYPE (gobject_class),
227 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
229 _gtk_marshal_VOID__VOID,
232 menu_item_signals[TOGGLE_SIZE_REQUEST] =
233 g_signal_new (I_("toggle-size-request"),
234 G_OBJECT_CLASS_TYPE (gobject_class),
236 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
238 _gtk_marshal_VOID__POINTER,
242 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
243 g_signal_new (I_("toggle-size-allocate"),
244 G_OBJECT_CLASS_TYPE (gobject_class),
246 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
248 _gtk_marshal_VOID__INT,
252 menu_item_signals[SELECT] =
253 g_signal_new (I_("select"),
254 G_OBJECT_CLASS_TYPE (gobject_class),
256 G_STRUCT_OFFSET (GtkMenuItemClass, select),
258 _gtk_marshal_VOID__VOID,
261 menu_item_signals[DESELECT] =
262 g_signal_new (I_("deselect"),
263 G_OBJECT_CLASS_TYPE (gobject_class),
265 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
267 _gtk_marshal_VOID__VOID,
271 * GtkMenuItem:right-justified:
273 * Sets whether the menu item appears justified
274 * at the right side of a menu bar.
278 g_object_class_install_property (gobject_class,
279 PROP_RIGHT_JUSTIFIED,
280 g_param_spec_boolean ("right-justified",
281 P_("Right Justified"),
282 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
284 GTK_PARAM_READWRITE));
287 * GtkMenuItem:submenu:
289 * The submenu attached to the menu item, or %NULL if it has none.
293 g_object_class_install_property (gobject_class,
295 g_param_spec_object ("submenu",
297 P_("The submenu attached to the menu item, or NULL if it has none"),
299 GTK_PARAM_READWRITE));
302 * GtkMenuItem:accel-path:
304 * Sets the accelerator path of the menu item, through which runtime
305 * changes of the menu item's accelerator caused by the user can be
306 * identified and saved to persistant storage.
310 g_object_class_install_property (gobject_class,
312 g_param_spec_string ("accel-path",
314 P_("Sets the accelerator path of the menu item"),
316 GTK_PARAM_READWRITE));
321 * The text for the child label.
325 g_object_class_install_property (gobject_class,
327 g_param_spec_string ("label",
329 P_("The text for the child label"),
331 GTK_PARAM_READWRITE));
334 * GtkMenuItem:use-underline:
336 * %TRUE if underlines in the text indicate mnemonics.
340 g_object_class_install_property (gobject_class,
342 g_param_spec_boolean ("use-underline",
344 P_("If set, an underline in the text indicates "
345 "the next character should be used for the "
346 "mnemonic accelerator key"),
348 GTK_PARAM_READWRITE));
350 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
351 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
353 gtk_widget_class_install_style_property_parser (widget_class,
354 g_param_spec_enum ("selected-shadow-type",
355 "Selected Shadow Type",
356 "Shadow type when item is selected",
357 GTK_TYPE_SHADOW_TYPE,
360 gtk_rc_property_parse_enum);
362 gtk_widget_class_install_style_property (widget_class,
363 g_param_spec_int ("horizontal-padding",
364 "Horizontal Padding",
365 "Padding to left and right of the menu item",
369 GTK_PARAM_READABLE));
371 gtk_widget_class_install_style_property (widget_class,
372 g_param_spec_int ("toggle-spacing",
374 "Space between icon and label",
378 GTK_PARAM_READABLE));
380 gtk_widget_class_install_style_property (widget_class,
381 g_param_spec_int ("arrow-spacing",
383 "Space between label and arrow",
387 GTK_PARAM_READABLE));
389 gtk_widget_class_install_style_property (widget_class,
390 g_param_spec_float ("arrow-scaling",
392 P_("Amount of space used up by arrow, relative to the menu item's font size"),
394 GTK_PARAM_READABLE));
397 * GtkMenuItem:width-chars:
399 * The minimum desired width of the menu item in characters.
403 gtk_widget_class_install_style_property (widget_class,
404 g_param_spec_int ("width-chars",
405 P_("Width in Characters"),
406 P_("The minimum desired width of the menu item in characters"),
408 GTK_PARAM_READABLE));
410 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
414 gtk_menu_item_init (GtkMenuItem *menu_item)
416 GtkMenuItemPrivate *priv;
418 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
421 menu_item->priv = priv;
423 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
426 priv->use_action_appearance = TRUE;
428 menu_item->priv->submenu = NULL;
429 menu_item->priv->toggle_size = 0;
430 menu_item->priv->accelerator_width = 0;
431 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
432 priv->submenu_direction = GTK_DIRECTION_LEFT;
434 priv->submenu_direction = GTK_DIRECTION_RIGHT;
435 priv->submenu_placement = GTK_TOP_BOTTOM;
436 priv->right_justify = FALSE;
437 priv->use_action_appearance = TRUE;
443 gtk_menu_item_new (void)
445 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
449 gtk_menu_item_new_with_label (const gchar *label)
451 return g_object_new (GTK_TYPE_MENU_ITEM,
458 * gtk_menu_item_new_with_mnemonic:
459 * @label: The text of the button, with an underscore in front of the
462 * Creates a new #GtkMenuItem containing a label.
464 * The label will be created using gtk_label_new_with_mnemonic(),
465 * so underscores in @label indicate the mnemonic for the menu item.
467 * Returns: a new #GtkMenuItem
470 gtk_menu_item_new_with_mnemonic (const gchar *label)
472 return g_object_new (GTK_TYPE_MENU_ITEM,
473 "use-underline", TRUE,
479 gtk_menu_item_dispose (GObject *object)
481 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
482 GtkMenuItemPrivate *priv = menu_item->priv;
486 gtk_action_disconnect_accelerator (priv->action);
487 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
490 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
494 gtk_menu_item_set_property (GObject *object,
499 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
503 case PROP_RIGHT_JUSTIFIED:
504 gtk_menu_item_set_right_justified (menu_item, g_value_get_boolean (value));
507 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
509 case PROP_ACCEL_PATH:
510 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
513 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
515 case PROP_USE_UNDERLINE:
516 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
518 case PROP_ACTIVATABLE_RELATED_ACTION:
519 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
521 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
522 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
525 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531 gtk_menu_item_get_property (GObject *object,
536 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
537 GtkMenuItemPrivate *priv = menu_item->priv;
541 case PROP_RIGHT_JUSTIFIED:
542 g_value_set_boolean (value, gtk_menu_item_get_right_justified (menu_item));
545 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
547 case PROP_ACCEL_PATH:
548 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
551 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
553 case PROP_USE_UNDERLINE:
554 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
556 case PROP_ACTIVATABLE_RELATED_ACTION:
557 g_value_set_object (value, priv->action);
559 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
560 g_value_set_boolean (value, priv->use_action_appearance);
563 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
569 gtk_menu_item_destroy (GtkWidget *widget)
571 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
572 GtkMenuItemPrivate *priv = menu_item->priv;
575 gtk_widget_destroy (priv->submenu);
577 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
581 gtk_menu_item_detacher (GtkWidget *widget,
584 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
585 GtkMenuItemPrivate *priv = menu_item->priv;
587 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
589 priv->submenu = NULL;
593 get_arrow_size (GtkWidget *widget,
597 GtkStyleContext *style_context;
599 PangoContext *context;
600 PangoFontMetrics *metrics;
601 gfloat arrow_scaling;
605 gtk_widget_style_get (widget,
606 "arrow-scaling", &arrow_scaling,
609 context = gtk_widget_get_pango_context (child);
610 style_context = gtk_widget_get_style_context (child);
611 state = gtk_widget_get_state_flags (child);
613 metrics = pango_context_get_metrics (context,
614 gtk_style_context_get_font (style_context, state),
615 pango_context_get_language (context));
617 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
618 pango_font_metrics_get_descent (metrics)));
620 pango_font_metrics_unref (metrics);
622 *size = *size * arrow_scaling;
627 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
632 if (GTK_IS_ACCEL_LABEL (widget))
636 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
637 *width = MAX (*width, w);
639 else if (GTK_IS_CONTAINER (widget))
640 gtk_container_foreach (GTK_CONTAINER (widget),
641 gtk_menu_item_accel_width_foreach,
646 get_minimum_width (GtkWidget *widget)
648 GtkStyleContext *style_context;
650 PangoContext *context;
651 PangoFontMetrics *metrics;
655 context = gtk_widget_get_pango_context (widget);
656 style_context = gtk_widget_get_style_context (widget);
657 state = gtk_widget_get_state_flags (widget);
659 metrics = pango_context_get_metrics (context,
660 gtk_style_context_get_font (style_context, state),
661 pango_context_get_language (context));
663 width = pango_font_metrics_get_approximate_char_width (metrics);
665 pango_font_metrics_unref (metrics);
667 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
669 return PANGO_PIXELS (width_chars * width);
673 gtk_menu_item_get_preferred_width (GtkWidget *widget,
677 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
678 GtkMenuItemPrivate *priv = menu_item->priv;
683 guint horizontal_padding;
685 GtkPackDirection pack_dir;
686 GtkPackDirection child_pack_dir;
687 gint min_width, nat_width;
688 GtkStyleContext *context;
690 GtkBorder padding, border;
692 min_width = nat_width = 0;
694 gtk_widget_style_get (widget,
695 "horizontal-padding", &horizontal_padding,
698 bin = GTK_BIN (widget);
699 parent = gtk_widget_get_parent (widget);
701 if (GTK_IS_MENU_BAR (parent))
703 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
704 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
708 pack_dir = GTK_PACK_DIRECTION_LTR;
709 child_pack_dir = GTK_PACK_DIRECTION_LTR;
712 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
714 context = gtk_widget_get_style_context (widget);
715 state = gtk_widget_get_state_flags (widget);
716 gtk_style_context_get_padding (context, state, &padding);
717 gtk_style_context_get_border (context, state, &border);
719 min_width = (border_width * 2) + padding.left + padding.right +
720 border.left + border.right;
722 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
723 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
724 min_width += 2 * horizontal_padding;
726 nat_width = min_width;
728 child = gtk_bin_get_child (bin);
730 if (child != NULL && gtk_widget_get_visible (child))
732 GtkMenuItemPrivate *priv = menu_item->priv;
733 gint child_min, child_nat;
735 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
737 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
742 gtk_widget_style_get (widget,
743 "arrow-spacing", &arrow_spacing,
746 get_arrow_size (widget, child, &arrow_size);
748 gtk_widget_style_get (widget,
749 "arrow-spacing", &arrow_spacing,
752 get_arrow_size (widget, child, &arrow_size);
754 min_width += arrow_size;
755 min_width += arrow_spacing;
757 min_width = MAX (min_width, get_minimum_width (widget));
758 nat_width = min_width;
761 min_width += child_min;
762 nat_width += child_nat;
766 gtk_container_foreach (GTK_CONTAINER (menu_item),
767 gtk_menu_item_accel_width_foreach,
769 priv->accelerator_width = accel_width;
772 *minimum_size = min_width;
775 *natural_size = nat_width;
779 gtk_menu_item_get_preferred_height (GtkWidget *widget,
783 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
784 GtkMenuItemPrivate *priv = menu_item->priv;
786 GtkStyleContext *context;
788 GtkBorder padding, border;
792 guint horizontal_padding;
794 GtkPackDirection pack_dir;
795 GtkPackDirection child_pack_dir;
796 gint min_height, nat_height;
798 min_height = nat_height = 0;
800 context = gtk_widget_get_style_context (widget);
801 state = gtk_widget_get_state_flags (widget);
802 gtk_style_context_get_padding (context, state, &padding);
803 gtk_style_context_get_border (context, state, &border);
805 gtk_widget_style_get (widget,
806 "horizontal-padding", &horizontal_padding,
809 bin = GTK_BIN (widget);
810 parent = gtk_widget_get_parent (widget);
812 if (GTK_IS_MENU_BAR (parent))
814 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
815 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
819 pack_dir = GTK_PACK_DIRECTION_LTR;
820 child_pack_dir = GTK_PACK_DIRECTION_LTR;
823 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
824 min_height = (border_width * 2) + padding.top + padding.bottom + border.top + border.bottom;
826 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
827 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
828 min_height += 2 * horizontal_padding;
830 nat_height = min_height;
832 child = gtk_bin_get_child (bin);
834 if (child != NULL && gtk_widget_get_visible (child))
836 GtkMenuItemPrivate *priv = menu_item->priv;
837 gint child_min, child_nat;
839 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
841 min_height += child_min;
842 nat_height += child_nat;
844 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
848 get_arrow_size (widget, child, &arrow_size);
850 min_height = MAX (min_height, arrow_size);
851 nat_height = MAX (nat_height, arrow_size);
854 else /* separator item */
856 gboolean wide_separators;
857 gint separator_height;
859 gtk_widget_style_get (widget,
860 "wide-separators", &wide_separators,
861 "separator-height", &separator_height,
865 min_height += separator_height + padding.top + border.top;
867 min_height += padding.top + padding.bottom + border.top + border.bottom;
869 nat_height = min_height;
873 gtk_container_foreach (GTK_CONTAINER (menu_item),
874 gtk_menu_item_accel_width_foreach,
876 priv->accelerator_width = accel_width;
879 *minimum_size = min_height;
882 *natural_size = nat_height;
886 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
891 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
892 GtkMenuItemPrivate *priv = menu_item->priv;
894 GtkStyleContext *context;
896 GtkBorder padding, border;
899 guint horizontal_padding;
901 GtkPackDirection pack_dir;
902 GtkPackDirection child_pack_dir;
903 gint min_height, nat_height;
906 min_height = nat_height = 0;
908 context = gtk_widget_get_style_context (widget);
909 state = gtk_widget_get_state_flags (widget);
910 gtk_style_context_get_padding (context, state, &padding);
911 gtk_style_context_get_border (context, state, &border);
913 gtk_widget_style_get (widget,
914 "horizontal-padding", &horizontal_padding,
917 bin = GTK_BIN (widget);
918 parent = gtk_widget_get_parent (widget);
920 if (GTK_IS_MENU_BAR (parent))
922 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
923 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
927 pack_dir = GTK_PACK_DIRECTION_LTR;
928 child_pack_dir = GTK_PACK_DIRECTION_LTR;
931 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
932 min_height = (border_width * 2) + padding.top + padding.bottom + border.top + border.bottom;
934 avail_size = for_size;
935 avail_size -= (border_width * 2) + padding.left + padding.right + border.left + border.right;
937 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
938 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
939 min_height += 2 * horizontal_padding;
941 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
942 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
943 avail_size -= 2 * horizontal_padding;
945 nat_height = min_height;
947 child = gtk_bin_get_child (bin);
949 if (child != NULL && gtk_widget_get_visible (child))
951 gint child_min, child_nat;
954 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
958 gtk_widget_style_get (widget,
959 "arrow-spacing", &arrow_spacing,
962 get_arrow_size (widget, child, &arrow_size);
964 avail_size -= arrow_size;
965 avail_size -= arrow_spacing;
968 gtk_widget_get_preferred_height_for_width (child,
973 min_height += child_min;
974 nat_height += child_nat;
976 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
978 min_height = MAX (min_height, arrow_size);
979 nat_height = MAX (nat_height, arrow_size);
982 else /* separator item */
984 gboolean wide_separators;
985 gint separator_height;
987 gtk_widget_style_get (widget,
988 "wide-separators", &wide_separators,
989 "separator-height", &separator_height,
993 min_height += separator_height + padding.top + border.top;
995 min_height += padding.top + padding.bottom + border.top + border.bottom;
997 nat_height = min_height;
1001 *minimum_size = min_height;
1004 *natural_size = nat_height;
1008 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1010 parent_buildable_iface = g_type_interface_peek_parent (iface);
1011 iface->add_child = gtk_menu_item_buildable_add_child;
1012 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1016 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1017 GtkBuilder *builder,
1021 if (type && strcmp (type, "submenu") == 0)
1022 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1023 GTK_WIDGET (child));
1025 parent_buildable_iface->add_child (buildable, builder, child, type);
1030 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1031 GtkBuilder *builder,
1033 const gchar *tagname,
1036 GtkWidget *toplevel;
1038 if (strcmp (tagname, "accelerator") == 0)
1040 GtkMenuShell *menu_shell;
1043 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1046 while (GTK_IS_MENU (menu_shell) &&
1047 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1048 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1050 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1054 /* Fall back to something ... */
1055 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1057 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1058 gtk_buildable_get_name (buildable));
1061 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1062 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1065 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1070 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1072 iface->update = gtk_menu_item_update;
1073 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1077 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1081 child = gtk_bin_get_child (GTK_BIN (menu_item));
1083 if (GTK_IS_LABEL (child))
1087 label = gtk_action_get_label (action);
1088 gtk_menu_item_set_label (menu_item, label);
1092 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1095 gtk_menu_item_update (GtkActivatable *activatable,
1097 const gchar *property_name)
1099 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1100 GtkMenuItemPrivate *priv = menu_item->priv;
1102 if (strcmp (property_name, "visible") == 0)
1103 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1104 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1105 else if (strcmp (property_name, "sensitive") == 0)
1106 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1107 else if (priv->use_action_appearance)
1109 if (strcmp (property_name, "label") == 0)
1110 activatable_update_label (menu_item, action);
1115 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1118 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1119 GtkMenuItemPrivate *priv = menu_item->priv;
1122 if (!priv->use_action_appearance || !action)
1124 label = gtk_bin_get_child (GTK_BIN (menu_item));
1126 if (GTK_IS_ACCEL_LABEL (label))
1127 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1133 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1134 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1136 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1138 if (priv->use_action_appearance)
1140 label = gtk_bin_get_child (GTK_BIN (menu_item));
1142 /* make sure label is a label, deleting it otherwise */
1143 if (label && !GTK_IS_LABEL (label))
1145 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1148 /* Make sure that menu_item has a label and that any
1149 * accelerators are set */
1150 gtk_menu_item_ensure_label (menu_item);
1151 gtk_menu_item_set_use_underline (menu_item, TRUE);
1152 /* Make label point to the menu_item's label */
1153 label = gtk_bin_get_child (GTK_BIN (menu_item));
1155 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1157 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1158 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1159 gtk_action_get_accel_closure (action));
1162 activatable_update_label (menu_item, action);
1167 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1170 GtkMenuItemPrivate *priv = menu_item->priv;
1172 if (priv->action == action)
1177 gtk_action_disconnect_accelerator (priv->action);
1182 const gchar *accel_path;
1184 accel_path = gtk_action_get_accel_path (action);
1187 gtk_action_connect_accelerator (action);
1188 gtk_menu_item_set_accel_path (menu_item, accel_path);
1192 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1194 priv->action = action;
1198 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1199 gboolean use_appearance)
1201 GtkMenuItemPrivate *priv = menu_item->priv;
1203 if (priv->use_action_appearance != use_appearance)
1205 priv->use_action_appearance = use_appearance;
1207 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1213 * gtk_menu_item_set_submenu:
1214 * @menu_item: a #GtkMenuItem
1215 * @submenu: (allow-none): the submenu, or %NULL
1217 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1218 * submenu is passed.
1221 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1224 GtkMenuItemPrivate *priv = menu_item->priv;
1226 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1227 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1229 if (priv->submenu != submenu)
1232 gtk_menu_detach (GTK_MENU (priv->submenu));
1236 priv->submenu = submenu;
1237 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1238 GTK_WIDGET (menu_item),
1239 gtk_menu_item_detacher);
1242 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1243 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1245 g_object_notify (G_OBJECT (menu_item), "submenu");
1250 * gtk_menu_item_get_submenu:
1251 * @menu_item: a #GtkMenuItem
1253 * Gets the submenu underneath this menu item, if any.
1254 * See gtk_menu_item_set_submenu().
1256 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1259 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1261 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1263 return menu_item->priv->submenu;
1266 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1267 GtkSubmenuPlacement placement);
1270 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1271 GtkSubmenuPlacement placement)
1273 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1275 menu_item->priv->submenu_placement = placement;
1279 gtk_menu_item_select (GtkMenuItem *menu_item)
1283 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1285 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1287 /* Enable themeing of the parent menu item depending on whether
1288 * something is selected in its submenu
1290 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1291 if (GTK_IS_MENU (parent))
1293 GtkMenu *menu = GTK_MENU (parent);
1295 if (menu->priv->parent_menu_item)
1296 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1301 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1305 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1307 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1309 /* Enable themeing of the parent menu item depending on whether
1310 * something is selected in its submenu
1312 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1313 if (GTK_IS_MENU (parent))
1315 GtkMenu *menu = GTK_MENU (parent);
1317 if (menu->priv->parent_menu_item)
1318 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1323 gtk_menu_item_activate (GtkMenuItem *menu_item)
1325 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1327 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1331 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1334 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1336 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1340 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1343 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1345 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1349 gtk_menu_item_size_allocate (GtkWidget *widget,
1350 GtkAllocation *allocation)
1352 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1353 GtkMenuItemPrivate *priv = menu_item->priv;
1355 GtkAllocation child_allocation;
1356 GtkTextDirection direction;
1357 GtkPackDirection pack_dir;
1358 GtkPackDirection child_pack_dir;
1362 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1363 g_return_if_fail (allocation != NULL);
1365 bin = GTK_BIN (widget);
1367 direction = gtk_widget_get_direction (widget);
1369 parent = gtk_widget_get_parent (widget);
1370 if (GTK_IS_MENU_BAR (parent))
1372 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1373 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1377 pack_dir = GTK_PACK_DIRECTION_LTR;
1378 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1381 gtk_widget_set_allocation (widget, allocation);
1383 child = gtk_bin_get_child (bin);
1386 GtkStyleContext *context;
1387 GtkStateFlags state;
1388 GtkBorder padding, border;
1389 guint horizontal_padding;
1392 context = gtk_widget_get_style_context (widget);
1393 state = gtk_widget_get_state_flags (widget);
1394 gtk_style_context_get_padding (context, state, &padding);
1395 gtk_style_context_get_border (context, state, &border);
1397 gtk_widget_style_get (widget,
1398 "horizontal-padding", &horizontal_padding,
1401 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1402 child_allocation.x = border_width + padding.left + border.left;
1403 child_allocation.y = border_width + padding.top + border.top;
1405 child_allocation.width = allocation->width - (border_width * 2) -
1406 padding.left - padding.right - border.left - border.right;
1407 child_allocation.height = allocation->height - (border_width * 2) -
1408 padding.top - padding.bottom - border.top - border.bottom;
1410 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1411 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1413 child_allocation.x += horizontal_padding;
1414 child_allocation.width -= 2 * horizontal_padding;
1416 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1417 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1419 child_allocation.y += horizontal_padding;
1420 child_allocation.height -= 2 * horizontal_padding;
1423 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1424 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1426 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1427 child_allocation.x += priv->toggle_size;
1428 child_allocation.width -= priv->toggle_size;
1432 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1433 child_allocation.y += priv->toggle_size;
1434 child_allocation.height -= priv->toggle_size;
1437 child_allocation.x += allocation->x;
1438 child_allocation.y += allocation->y;
1440 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1442 guint arrow_spacing;
1445 gtk_widget_style_get (widget,
1446 "arrow-spacing", &arrow_spacing,
1449 get_arrow_size (widget, child, &arrow_size);
1451 if (direction == GTK_TEXT_DIR_RTL)
1452 child_allocation.x += arrow_size + arrow_spacing;
1453 child_allocation.width -= arrow_size + arrow_spacing;
1456 if (child_allocation.width < 1)
1457 child_allocation.width = 1;
1459 gtk_widget_size_allocate (child, &child_allocation);
1462 if (gtk_widget_get_realized (widget))
1463 gdk_window_move_resize (priv->event_window,
1464 allocation->x, allocation->y,
1465 allocation->width, allocation->height);
1468 gtk_menu_reposition (GTK_MENU (priv->submenu));
1472 gtk_menu_item_realize (GtkWidget *widget)
1474 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1475 GtkMenuItemPrivate *priv = menu_item->priv;
1476 GtkAllocation allocation;
1478 GdkWindowAttr attributes;
1479 gint attributes_mask;
1481 gtk_widget_set_realized (widget, TRUE);
1483 window = gtk_widget_get_parent_window (widget);
1484 gtk_widget_set_window (widget, window);
1485 g_object_ref (window);
1487 gtk_widget_get_allocation (widget, &allocation);
1489 attributes.x = allocation.x;
1490 attributes.y = allocation.y;
1491 attributes.width = allocation.width;
1492 attributes.height = allocation.height;
1493 attributes.window_type = GDK_WINDOW_CHILD;
1494 attributes.wclass = GDK_INPUT_ONLY;
1495 attributes.event_mask = (gtk_widget_get_events (widget) |
1496 GDK_BUTTON_PRESS_MASK |
1497 GDK_BUTTON_RELEASE_MASK |
1498 GDK_ENTER_NOTIFY_MASK |
1499 GDK_LEAVE_NOTIFY_MASK |
1500 GDK_POINTER_MOTION_MASK);
1502 attributes_mask = GDK_WA_X | GDK_WA_Y;
1504 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1505 &attributes, attributes_mask);
1506 gdk_window_set_user_data (priv->event_window, widget);
1510 gtk_menu_item_unrealize (GtkWidget *widget)
1512 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1513 GtkMenuItemPrivate *priv = menu_item->priv;
1515 gdk_window_set_user_data (priv->event_window, NULL);
1516 gdk_window_destroy (priv->event_window);
1517 priv->event_window = NULL;
1519 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1523 gtk_menu_item_map (GtkWidget *widget)
1525 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1526 GtkMenuItemPrivate *priv = menu_item->priv;
1528 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1530 gdk_window_show (priv->event_window);
1534 gtk_menu_item_unmap (GtkWidget *widget)
1536 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1537 GtkMenuItemPrivate *priv = menu_item->priv;
1539 gdk_window_hide (priv->event_window);
1541 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1545 gtk_menu_item_enter (GtkWidget *widget,
1546 GdkEventCrossing *event)
1548 g_return_val_if_fail (event != NULL, FALSE);
1550 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1554 gtk_menu_item_leave (GtkWidget *widget,
1555 GdkEventCrossing *event)
1557 g_return_val_if_fail (event != NULL, FALSE);
1559 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1563 gtk_menu_item_draw (GtkWidget *widget,
1566 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1567 GtkMenuItemPrivate *priv = menu_item->priv;
1568 GtkStateFlags state;
1569 GtkStyleContext *context;
1571 GtkWidget *child, *parent;
1572 gint x, y, w, h, width, height;
1573 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1575 state = gtk_widget_get_state_flags (widget);
1576 context = gtk_widget_get_style_context (widget);
1577 width = gtk_widget_get_allocated_width (widget);
1578 height = gtk_widget_get_allocated_height (widget);
1582 w = width - border_width * 2;
1583 h = height - border_width * 2;
1585 child = gtk_bin_get_child (GTK_BIN (menu_item));
1586 parent = gtk_widget_get_parent (widget);
1588 gtk_style_context_save (context);
1589 gtk_style_context_set_state (context, state);
1591 gtk_style_context_get_padding (context, state, &padding);
1593 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1595 gtk_render_background (context, cr, x, y, w, h);
1596 gtk_render_frame (context, cr, x, y, w, h);
1599 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1601 gint arrow_x, arrow_y;
1603 guint horizontal_padding;
1604 GtkTextDirection direction;
1607 direction = gtk_widget_get_direction (widget);
1609 gtk_widget_style_get (widget,
1610 "horizontal-padding", &horizontal_padding,
1613 get_arrow_size (widget, child, &arrow_size);
1615 if (direction == GTK_TEXT_DIR_LTR)
1617 arrow_x = x + w - horizontal_padding - arrow_size;
1622 arrow_x = x + horizontal_padding;
1623 angle = (3 * G_PI) / 2;
1626 arrow_y = y + (h - arrow_size) / 2;
1628 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1632 gboolean wide_separators;
1633 gint separator_height;
1634 guint horizontal_padding;
1636 gtk_widget_style_get (widget,
1637 "wide-separators", &wide_separators,
1638 "separator-height", &separator_height,
1639 "horizontal-padding", &horizontal_padding,
1641 if (wide_separators)
1642 gtk_render_frame (context, cr,
1643 horizontal_padding + padding.left,
1644 (height - separator_height - padding.top) / 2,
1645 width - (2 * horizontal_padding) - padding.left - padding.right,
1648 gtk_render_line (context, cr,
1649 horizontal_padding + padding.left,
1650 (height - padding.top) / 2,
1651 width - horizontal_padding - padding.right - 1,
1652 (height - padding.top) / 2);
1655 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1657 gtk_style_context_restore (context);
1663 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1665 GtkMenuItemPrivate *priv = menu_item->priv;
1666 gboolean touchscreen_mode;
1668 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1669 "gtk-touchscreen-mode", &touchscreen_mode,
1672 if (!touchscreen_mode && priv->submenu &&
1673 (!gtk_widget_get_mapped (priv->submenu) ||
1674 GTK_MENU (priv->submenu)->priv->tearoff_active))
1676 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1679 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1680 GTK_STATE_FLAG_PRELIGHT, FALSE);
1681 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1685 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1687 GtkMenuItemPrivate *priv = menu_item->priv;
1690 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1692 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1693 GTK_STATE_FLAG_PRELIGHT);
1694 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1698 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1699 gboolean group_cycling)
1703 parent = gtk_widget_get_parent (widget);
1705 if (GTK_IS_MENU_SHELL (parent))
1706 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1708 if (group_cycling &&
1710 GTK_IS_MENU_SHELL (parent) &&
1711 GTK_MENU_SHELL (parent)->priv->active)
1713 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1716 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1722 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1724 GtkMenuItemPrivate *priv = menu_item->priv;
1727 gtk_action_activate (priv->action);
1732 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1734 GtkMenuItemPrivate *priv = menu_item->priv;
1738 widget = GTK_WIDGET (menu_item);
1739 parent = gtk_widget_get_parent (widget);
1741 if (parent && GTK_IS_MENU_SHELL (parent))
1743 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1745 if (priv->submenu == NULL)
1746 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1749 _gtk_menu_shell_activate (menu_shell);
1751 gtk_menu_shell_select_item (menu_shell, widget);
1752 _gtk_menu_item_popup_submenu (widget, FALSE);
1754 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1760 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1763 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1769 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1772 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1774 menu_item->priv->toggle_size = allocation;
1778 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1783 gtk_menu_item_ensure_label (menu_item);
1785 child = gtk_bin_get_child (GTK_BIN (menu_item));
1786 if (GTK_IS_LABEL (child))
1788 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1790 g_object_notify (G_OBJECT (menu_item), "label");
1794 static G_CONST_RETURN gchar *
1795 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1799 gtk_menu_item_ensure_label (menu_item);
1801 child = gtk_bin_get_child (GTK_BIN (menu_item));
1802 if (GTK_IS_LABEL (child))
1803 return gtk_label_get_label (GTK_LABEL (child));
1809 free_timeval (GTimeVal *val)
1811 g_slice_free (GTimeVal, val);
1815 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1816 gboolean remember_exact_time)
1818 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1819 GtkMenuItemPrivate *priv = menu_item->priv;
1822 parent = gtk_widget_get_parent (widget);
1824 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1826 gboolean take_focus;
1827 GtkMenuPositionFunc menu_position_func;
1829 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1830 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1832 if (remember_exact_time)
1834 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1836 g_get_current_time (popup_time);
1838 g_object_set_data_full (G_OBJECT (priv->submenu),
1839 "gtk-menu-exact-popup-time", popup_time,
1840 (GDestroyNotify) free_timeval);
1844 g_object_set_data (G_OBJECT (priv->submenu),
1845 "gtk-menu-exact-popup-time", NULL);
1848 /* gtk_menu_item_position_menu positions the submenu from the
1849 * menuitems position. If the menuitem doesn't have a window,
1850 * that doesn't work. In that case we use the default
1851 * positioning function instead which places the submenu at the
1854 if (gtk_widget_get_window (widget))
1855 menu_position_func = gtk_menu_item_position_menu;
1857 menu_position_func = NULL;
1859 gtk_menu_popup (GTK_MENU (priv->submenu),
1864 GTK_MENU_SHELL (parent)->priv->button,
1868 /* Enable themeing of the parent menu item depending on whether
1869 * its submenu is shown or not.
1871 gtk_widget_queue_draw (widget);
1875 gtk_menu_item_popup_timeout (gpointer data)
1877 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1878 GtkMenuItemPrivate *priv = menu_item->priv;
1881 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1883 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1884 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1886 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1887 if (priv->timer_from_keypress && priv->submenu)
1888 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1897 get_popup_delay (GtkWidget *widget)
1901 parent = gtk_widget_get_parent (widget);
1902 if (GTK_IS_MENU_SHELL (parent))
1904 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1910 g_object_get (gtk_widget_get_settings (widget),
1911 "gtk-menu-popup-delay", &popup_delay,
1919 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1920 gboolean with_delay)
1922 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1923 GtkMenuItemPrivate *priv = menu_item->priv;
1927 g_source_remove (priv->timer);
1934 gint popup_delay = get_popup_delay (widget);
1936 if (popup_delay > 0)
1938 GdkEvent *event = gtk_get_current_event ();
1940 priv->timer = gdk_threads_add_timeout (popup_delay,
1941 gtk_menu_item_popup_timeout,
1945 event->type != GDK_BUTTON_PRESS &&
1946 event->type != GDK_ENTER_NOTIFY)
1947 priv->timer_from_keypress = TRUE;
1949 priv->timer_from_keypress = FALSE;
1952 gdk_event_free (event);
1958 gtk_menu_item_real_popup_submenu (widget, FALSE);
1962 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1964 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1965 GtkMenuItemPrivate *priv = menu_item->priv;
1969 g_object_set_data (G_OBJECT (priv->submenu),
1970 "gtk-menu-exact-popup-time", NULL);
1974 g_source_remove (priv->timer);
1978 gtk_menu_popdown (GTK_MENU (priv->submenu));
1980 gtk_widget_queue_draw (widget);
1985 get_offsets (GtkMenu *menu,
1986 gint *horizontal_offset,
1987 gint *vertical_offset)
1989 gint vertical_padding;
1990 gint horizontal_padding;
1991 GtkStyleContext *context;
1992 GtkStateFlags state;
1995 gtk_widget_style_get (GTK_WIDGET (menu),
1996 "horizontal-offset", horizontal_offset,
1997 "vertical-offset", vertical_offset,
1998 "horizontal-padding", &horizontal_padding,
1999 "vertical-padding", &vertical_padding,
2002 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2003 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2004 gtk_style_context_get_padding (context, state, &padding);
2006 *vertical_offset -= padding.top;
2007 *vertical_offset -= vertical_padding;
2008 *horizontal_offset += horizontal_padding;
2012 gtk_menu_item_position_menu (GtkMenu *menu,
2018 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2019 GtkMenuItemPrivate *priv = menu_item->priv;
2020 GtkAllocation allocation;
2022 GtkMenuItem *parent_menu_item;
2023 GtkRequisition requisition;
2026 gint twidth, theight;
2028 GtkTextDirection direction;
2029 GdkRectangle monitor;
2031 gint horizontal_offset;
2032 gint vertical_offset;
2033 gint available_left, available_right;
2034 GtkStyleContext *context;
2035 GtkStateFlags state;
2036 GtkBorder parent_padding;
2038 g_return_if_fail (menu != NULL);
2039 g_return_if_fail (x != NULL);
2040 g_return_if_fail (y != NULL);
2042 widget = GTK_WIDGET (user_data);
2047 direction = gtk_widget_get_direction (widget);
2049 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &requisition, NULL);
2050 twidth = requisition.width;
2051 theight = requisition.height;
2053 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2054 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2055 if (monitor_num < 0)
2057 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2059 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2061 g_warning ("Menu not on screen");
2065 gtk_widget_get_allocation (widget, &allocation);
2070 get_offsets (menu, &horizontal_offset, &vertical_offset);
2072 available_left = tx - monitor.x;
2073 available_right = monitor.x + monitor.width - (tx + allocation.width);
2075 parent = gtk_widget_get_parent (widget);
2076 if (GTK_IS_MENU_BAR (parent))
2078 priv->from_menubar = TRUE;
2080 else if (GTK_IS_MENU (parent))
2082 if (GTK_MENU (parent)->priv->parent_menu_item)
2083 priv->from_menubar = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item)->priv->from_menubar;
2085 priv->from_menubar = FALSE;
2089 priv->from_menubar = FALSE;
2092 switch (priv->submenu_placement)
2094 case GTK_TOP_BOTTOM:
2095 if (direction == GTK_TEXT_DIR_LTR)
2096 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2099 priv->submenu_direction = GTK_DIRECTION_LEFT;
2100 tx += allocation.width - twidth;
2102 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2103 ty += allocation.height;
2104 else if ((ty - theight) >= monitor.y)
2106 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2107 ty += allocation.height;
2112 case GTK_LEFT_RIGHT:
2113 if (GTK_IS_MENU (parent))
2114 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2116 parent_menu_item = NULL;
2118 context = gtk_widget_get_style_context (parent);
2119 state = gtk_widget_get_state_flags (parent);
2120 gtk_style_context_get_padding (context, state, &parent_padding);
2122 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2124 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2128 if (direction == GTK_TEXT_DIR_LTR)
2129 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2131 priv->submenu_direction = GTK_DIRECTION_LEFT;
2134 switch (priv->submenu_direction)
2136 case GTK_DIRECTION_LEFT:
2137 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2138 available_left >= available_right)
2139 tx -= twidth + parent_padding.left + horizontal_offset;
2142 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2143 tx += allocation.width + parent_padding.right + horizontal_offset;
2147 case GTK_DIRECTION_RIGHT:
2148 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2149 available_right >= available_left)
2150 tx += allocation.width + parent_padding.right + horizontal_offset;
2153 priv->submenu_direction = GTK_DIRECTION_LEFT;
2154 tx -= twidth + parent_padding.left + horizontal_offset;
2159 ty += vertical_offset;
2161 /* If the height of the menu doesn't fit we move it upward. */
2162 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2166 /* If we have negative, tx, here it is because we can't get
2167 * the menu all the way on screen. Favor the left portion.
2169 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2172 gtk_menu_set_monitor (menu, monitor_num);
2174 if (!gtk_widget_get_visible (menu->priv->toplevel))
2176 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2177 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2182 * gtk_menu_item_set_right_justified:
2183 * @menu_item: a #GtkMenuItem.
2184 * @right_justified: if %TRUE the menu item will appear at the
2185 * far right if added to a menu bar
2187 * Sets whether the menu item appears justified at the right
2188 * side of a menu bar. This was traditionally done for "Help"
2189 * menu items, but is now considered a bad idea. (If the widget
2190 * layout is reversed for a right-to-left language like Hebrew
2191 * or Arabic, right-justified-menu-items appear at the left.)
2194 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2195 gboolean right_justified)
2197 GtkMenuItemPrivate *priv = menu_item->priv;
2199 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2201 right_justified = right_justified != FALSE;
2203 if (priv->right_justify != right_justified)
2205 priv->right_justify = right_justified;
2206 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2211 * gtk_menu_item_get_right_justified:
2212 * @menu_item: a #GtkMenuItem
2214 * Gets whether the menu item appears justified at the right
2215 * side of the menu bar.
2217 * Return value: %TRUE if the menu item will appear at the
2218 * far right if added to a menu bar.
2221 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2223 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2225 return menu_item->priv->right_justify;
2230 gtk_menu_item_show_all (GtkWidget *widget)
2232 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2233 GtkMenuItemPrivate *priv = menu_item->priv;
2235 /* show children including submenu */
2237 gtk_widget_show_all (priv->submenu);
2238 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2240 gtk_widget_show (widget);
2244 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2249 parent = gtk_widget_get_parent (widget);
2251 /* Chain to the parent GtkMenu for further checks */
2252 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2253 parent && gtk_widget_can_activate_accel (parent, signal_id));
2257 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2260 const gchar **path_p = data;
2264 if (GTK_IS_LABEL (widget))
2266 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2267 if (*path_p && (*path_p)[0] == 0)
2270 else if (GTK_IS_CONTAINER (widget))
2271 gtk_container_foreach (GTK_CONTAINER (widget),
2272 gtk_menu_item_accel_name_foreach,
2278 gtk_menu_item_parent_set (GtkWidget *widget,
2279 GtkWidget *previous_parent)
2281 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2285 parent = gtk_widget_get_parent (widget);
2286 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2289 _gtk_menu_item_refresh_accel_path (menu_item,
2290 menu->priv->accel_path,
2291 menu->priv->accel_group,
2294 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2295 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2299 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2300 const gchar *prefix,
2301 GtkAccelGroup *accel_group,
2302 gboolean group_changed)
2304 GtkMenuItemPrivate *priv = menu_item->priv;
2308 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2309 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2311 widget = GTK_WIDGET (menu_item);
2315 gtk_widget_set_accel_path (widget, NULL, NULL);
2319 path = _gtk_widget_get_accel_path (widget, NULL);
2320 if (!path) /* no active accel_path yet */
2322 path = priv->accel_path;
2323 if (!path && prefix)
2325 const gchar *postfix = NULL;
2328 /* try to construct one from label text */
2329 gtk_container_foreach (GTK_CONTAINER (menu_item),
2330 gtk_menu_item_accel_name_foreach,
2334 new_path = g_strconcat (prefix, "/", postfix, NULL);
2335 path = priv->accel_path = (char*)g_intern_string (new_path);
2340 gtk_widget_set_accel_path (widget, path, accel_group);
2342 else if (group_changed) /* reinstall accelerators */
2343 gtk_widget_set_accel_path (widget, path, accel_group);
2347 * gtk_menu_item_set_accel_path
2348 * @menu_item: a valid #GtkMenuItem
2349 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2350 * item's functionality, or %NULL to unset the current path.
2352 * Set the accelerator path on @menu_item, through which runtime
2353 * changes of the menu item's accelerator caused by the user can be
2354 * identified and saved to persistent storage (see gtk_accel_map_save()
2355 * on this). To set up a default accelerator for this menu item, call
2356 * gtk_accel_map_add_entry() with the same @accel_path. See also
2357 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2358 * and gtk_menu_set_accel_path() for a more convenient variant of
2361 * This function is basically a convenience wrapper that handles
2362 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2363 * group for the menu item.
2365 * Note that you do need to set an accelerator on the parent menu with
2366 * gtk_menu_set_accel_group() for this to work.
2368 * Note that @accel_path string will be stored in a #GQuark.
2369 * Therefore, if you pass a static string, you can save some memory
2370 * by interning it first with g_intern_static_string().
2373 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2374 const gchar *accel_path)
2376 GtkMenuItemPrivate *priv = menu_item->priv;
2380 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2381 g_return_if_fail (accel_path == NULL ||
2382 (accel_path[0] == '<' && strchr (accel_path, '/')));
2384 widget = GTK_WIDGET (menu_item);
2386 /* store new path */
2387 priv->accel_path = (char*)g_intern_string (accel_path);
2389 /* forget accelerators associated with old path */
2390 gtk_widget_set_accel_path (widget, NULL, NULL);
2392 /* install accelerators associated with new path */
2393 parent = gtk_widget_get_parent (widget);
2394 if (GTK_IS_MENU (parent))
2396 GtkMenu *menu = GTK_MENU (parent);
2398 if (menu->priv->accel_group)
2399 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2401 menu->priv->accel_group,
2407 * gtk_menu_item_get_accel_path
2408 * @menu_item: a valid #GtkMenuItem
2410 * Retrieve the accelerator path that was previously set on @menu_item.
2412 * See gtk_menu_item_set_accel_path() for details.
2414 * Returns: the accelerator path corresponding to this menu
2415 * item's functionality, or %NULL if not set
2419 G_CONST_RETURN gchar *
2420 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2422 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2424 return menu_item->priv->accel_path;
2428 gtk_menu_item_forall (GtkContainer *container,
2429 gboolean include_internals,
2430 GtkCallback callback,
2431 gpointer callback_data)
2435 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2436 g_return_if_fail (callback != NULL);
2438 child = gtk_bin_get_child (GTK_BIN (container));
2440 callback (child, callback_data);
2444 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2446 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2447 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2448 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2449 !gtk_widget_is_sensitive (menu_item) ||
2450 !gtk_widget_get_visible (menu_item))
2457 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2459 GtkWidget *accel_label;
2461 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2463 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2464 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2466 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2467 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2468 GTK_WIDGET (menu_item));
2469 gtk_widget_show (accel_label);
2474 * gtk_menu_item_set_label:
2475 * @menu_item: a #GtkMenuItem
2476 * @label: the text you want to set
2478 * Sets @text on the @menu_item label
2483 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2486 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2488 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2492 * gtk_menu_item_get_label:
2493 * @menu_item: a #GtkMenuItem
2495 * Sets @text on the @menu_item label
2497 * Returns: The text in the @menu_item label. This is the internal
2498 * string used by the label, and must not be modified.
2502 G_CONST_RETURN gchar *
2503 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2505 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2507 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2511 * gtk_menu_item_set_use_underline:
2512 * @menu_item: a #GtkMenuItem
2513 * @setting: %TRUE if underlines in the text indicate mnemonics
2515 * If true, an underline in the text indicates the next character
2516 * should be used for the mnemonic accelerator key.
2521 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2526 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2528 gtk_menu_item_ensure_label (menu_item);
2530 child = gtk_bin_get_child (GTK_BIN (menu_item));
2531 if (GTK_IS_LABEL (child))
2533 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2535 g_object_notify (G_OBJECT (menu_item), "use-underline");
2540 * gtk_menu_item_get_use_underline:
2541 * @menu_item: a #GtkMenuItem
2543 * Checks if an underline in the text indicates the next character
2544 * should be used for the mnemonic accelerator key.
2546 * Return value: %TRUE if an embedded underline in the label
2547 * indicates the mnemonic accelerator key.
2552 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2556 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2558 gtk_menu_item_ensure_label (menu_item);
2560 child = gtk_bin_get_child (GTK_BIN (menu_item));
2561 if (GTK_IS_LABEL (child))
2562 return gtk_label_get_use_underline (GTK_LABEL (child));
2568 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2571 GtkMenuItemPrivate *priv;
2573 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2575 priv = menu_item->priv;
2577 if (priv->reserve_indicator != reserve)
2579 priv->reserve_indicator = reserve;
2580 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2585 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2587 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2589 return menu_item->priv->reserve_indicator;