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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtkaccellabel.h"
31 #include "gtkmarshalers.h"
32 #include "gtkmenuprivate.h"
33 #include "gtkmenushellprivate.h"
34 #include "gtkmenuitemprivate.h"
35 #include "gtkmenubar.h"
36 #include "gtkmenuprivate.h"
37 #include "gtkseparatormenuitem.h"
38 #include "gtkprivate.h"
39 #include "gtkbuildable.h"
40 #include "gtkactivatable.h"
41 #include "gtkwidgetprivate.h"
43 #include "gtksettings.h"
44 #include "gtktypebuiltins.h"
45 #include "a11y/gtkmenuitemaccessible.h"
50 * @Short_description: The widget used for item in menus
52 * @See_also: #GtkBin, #GtkMenuShell
54 * The #GtkMenuItem widget and the derived widgets are the only valid
55 * childs for menus. Their function is to correctly handle highlighting,
56 * alignment, events and submenus.
58 * As it derives from #GtkBin it can hold any valid child widget, altough
59 * only a few are really useful.
61 * <refsect2 id="GtkMenuItem-BUILDER-UI">
62 * <title>GtkMenuItem as GtkBuildable</title>
63 * The GtkMenuItem implementation of the GtkBuildable interface
64 * supports adding a submenu by specifying "submenu" as the "type"
65 * attribute of a <child> element.
67 * <title>A UI definition fragment with submenus</title>
68 * <programlisting><![CDATA[
69 * <object class="GtkMenuItem">
70 * <child type="submenu">
71 * <object class="GtkMenu"/>
74 * ]]></programlisting>
98 /* activatable properties */
99 PROP_ACTIVATABLE_RELATED_ACTION,
100 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
104 static void gtk_menu_item_dispose (GObject *object);
105 static void gtk_menu_item_set_property (GObject *object,
109 static void gtk_menu_item_get_property (GObject *object,
113 static void gtk_menu_item_destroy (GtkWidget *widget);
114 static void gtk_menu_item_size_allocate (GtkWidget *widget,
115 GtkAllocation *allocation);
116 static void gtk_menu_item_realize (GtkWidget *widget);
117 static void gtk_menu_item_unrealize (GtkWidget *widget);
118 static void gtk_menu_item_map (GtkWidget *widget);
119 static void gtk_menu_item_unmap (GtkWidget *widget);
120 static gboolean gtk_menu_item_enter (GtkWidget *widget,
121 GdkEventCrossing *event);
122 static gboolean gtk_menu_item_leave (GtkWidget *widget,
123 GdkEventCrossing *event);
124 static gboolean gtk_menu_item_draw (GtkWidget *widget,
126 static void gtk_menu_item_parent_set (GtkWidget *widget,
127 GtkWidget *previous_parent);
130 static void gtk_real_menu_item_select (GtkMenuItem *item);
131 static void gtk_real_menu_item_deselect (GtkMenuItem *item);
132 static void gtk_real_menu_item_activate (GtkMenuItem *item);
133 static void gtk_real_menu_item_activate_item (GtkMenuItem *item);
134 static void gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
136 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
138 static gboolean gtk_menu_item_mnemonic_activate (GtkWidget *widget,
139 gboolean group_cycling);
141 static void gtk_menu_item_ensure_label (GtkMenuItem *menu_item);
142 static gint gtk_menu_item_popup_timeout (gpointer data);
143 static void gtk_menu_item_position_menu (GtkMenu *menu,
148 static void gtk_menu_item_show_all (GtkWidget *widget);
149 static void gtk_menu_item_forall (GtkContainer *container,
150 gboolean include_internals,
151 GtkCallback callback,
152 gpointer callback_data);
153 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
156 static void gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
158 static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
160 static void gtk_menu_item_get_preferred_width (GtkWidget *widget,
163 static void gtk_menu_item_get_preferred_height (GtkWidget *widget,
166 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
171 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface);
172 static void gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
176 static void gtk_menu_item_buildable_custom_finished(GtkBuildable *buildable,
179 const gchar *tagname,
182 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface);
183 static void gtk_menu_item_update (GtkActivatable *activatable,
185 const gchar *property_name);
186 static void gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
188 static void gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
190 static void gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
191 gboolean use_appearance);
193 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
195 static GtkBuildableIface *parent_buildable_iface;
197 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
198 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
199 gtk_menu_item_buildable_interface_init)
200 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
201 gtk_menu_item_activatable_interface_init))
204 gtk_menu_item_class_init (GtkMenuItemClass *klass)
206 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
207 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
208 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
210 gobject_class->dispose = gtk_menu_item_dispose;
211 gobject_class->set_property = gtk_menu_item_set_property;
212 gobject_class->get_property = gtk_menu_item_get_property;
214 widget_class->destroy = gtk_menu_item_destroy;
215 widget_class->size_allocate = gtk_menu_item_size_allocate;
216 widget_class->draw = gtk_menu_item_draw;
217 widget_class->realize = gtk_menu_item_realize;
218 widget_class->unrealize = gtk_menu_item_unrealize;
219 widget_class->map = gtk_menu_item_map;
220 widget_class->unmap = gtk_menu_item_unmap;
221 widget_class->enter_notify_event = gtk_menu_item_enter;
222 widget_class->leave_notify_event = gtk_menu_item_leave;
223 widget_class->show_all = gtk_menu_item_show_all;
224 widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
225 widget_class->parent_set = gtk_menu_item_parent_set;
226 widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
227 widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
228 widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
229 widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
231 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
233 container_class->forall = gtk_menu_item_forall;
235 klass->activate = gtk_real_menu_item_activate;
236 klass->activate_item = gtk_real_menu_item_activate_item;
237 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
238 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
239 klass->set_label = gtk_real_menu_item_set_label;
240 klass->get_label = gtk_real_menu_item_get_label;
241 klass->select = gtk_real_menu_item_select;
242 klass->deselect = gtk_real_menu_item_deselect;
244 klass->hide_on_activate = TRUE;
247 * GtkMenuItem::activate:
248 * @menuitem: the object which received the signal.
250 * Emitted when the item is activated.
252 menu_item_signals[ACTIVATE] =
253 g_signal_new (I_("activate"),
254 G_OBJECT_CLASS_TYPE (gobject_class),
255 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
256 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
258 _gtk_marshal_VOID__VOID,
260 widget_class->activate_signal = menu_item_signals[ACTIVATE];
263 * GtkMenuItem::activate-item:
264 * @menuitem: the object which received the signal.
266 * Emitted when the item is activated, but also if the menu item has a
267 * submenu. For normal applications, the relevant signal is
268 * #GtkMenuItem::activate.
270 menu_item_signals[ACTIVATE_ITEM] =
271 g_signal_new (I_("activate-item"),
272 G_OBJECT_CLASS_TYPE (gobject_class),
274 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
276 _gtk_marshal_VOID__VOID,
279 menu_item_signals[TOGGLE_SIZE_REQUEST] =
280 g_signal_new (I_("toggle-size-request"),
281 G_OBJECT_CLASS_TYPE (gobject_class),
283 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
285 _gtk_marshal_VOID__POINTER,
289 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
290 g_signal_new (I_("toggle-size-allocate"),
291 G_OBJECT_CLASS_TYPE (gobject_class),
293 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
295 _gtk_marshal_VOID__INT,
299 menu_item_signals[SELECT] =
300 g_signal_new (I_("select"),
301 G_OBJECT_CLASS_TYPE (gobject_class),
303 G_STRUCT_OFFSET (GtkMenuItemClass, select),
305 _gtk_marshal_VOID__VOID,
308 menu_item_signals[DESELECT] =
309 g_signal_new (I_("deselect"),
310 G_OBJECT_CLASS_TYPE (gobject_class),
312 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
314 _gtk_marshal_VOID__VOID,
318 * GtkMenuItem:right-justified:
320 * Sets whether the menu item appears justified
321 * at the right side of a menu bar.
325 g_object_class_install_property (gobject_class,
326 PROP_RIGHT_JUSTIFIED,
327 g_param_spec_boolean ("right-justified",
328 P_("Right Justified"),
329 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
331 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
334 * GtkMenuItem:submenu:
336 * The submenu attached to the menu item, or %NULL if it has none.
340 g_object_class_install_property (gobject_class,
342 g_param_spec_object ("submenu",
344 P_("The submenu attached to the menu item, or NULL if it has none"),
346 GTK_PARAM_READWRITE));
349 * GtkMenuItem:accel-path:
351 * Sets the accelerator path of the menu item, through which runtime
352 * changes of the menu item's accelerator caused by the user can be
353 * identified and saved to persistant storage.
357 g_object_class_install_property (gobject_class,
359 g_param_spec_string ("accel-path",
361 P_("Sets the accelerator path of the menu item"),
363 GTK_PARAM_READWRITE));
368 * The text for the child label.
372 g_object_class_install_property (gobject_class,
374 g_param_spec_string ("label",
376 P_("The text for the child label"),
378 GTK_PARAM_READWRITE));
381 * GtkMenuItem:use-underline:
383 * %TRUE if underlines in the text indicate mnemonics.
387 g_object_class_install_property (gobject_class,
389 g_param_spec_boolean ("use-underline",
391 P_("If set, an underline in the text indicates "
392 "the next character should be used for the "
393 "mnemonic accelerator key"),
395 GTK_PARAM_READWRITE));
397 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
398 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
400 gtk_widget_class_install_style_property_parser (widget_class,
401 g_param_spec_enum ("selected-shadow-type",
402 "Selected Shadow Type",
403 "Shadow type when item is selected",
404 GTK_TYPE_SHADOW_TYPE,
407 gtk_rc_property_parse_enum);
409 gtk_widget_class_install_style_property (widget_class,
410 g_param_spec_int ("horizontal-padding",
411 "Horizontal Padding",
412 "Padding to left and right of the menu item",
416 GTK_PARAM_READABLE));
418 gtk_widget_class_install_style_property (widget_class,
419 g_param_spec_int ("toggle-spacing",
421 "Space between icon and label",
425 GTK_PARAM_READABLE));
427 gtk_widget_class_install_style_property (widget_class,
428 g_param_spec_int ("arrow-spacing",
430 "Space between label and arrow",
434 GTK_PARAM_READABLE));
436 gtk_widget_class_install_style_property (widget_class,
437 g_param_spec_float ("arrow-scaling",
439 P_("Amount of space used up by arrow, relative to the menu item's font size"),
441 GTK_PARAM_READABLE));
444 * GtkMenuItem:width-chars:
446 * The minimum desired width of the menu item in characters.
450 gtk_widget_class_install_style_property (widget_class,
451 g_param_spec_int ("width-chars",
452 P_("Width in Characters"),
453 P_("The minimum desired width of the menu item in characters"),
455 GTK_PARAM_READABLE));
457 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
461 gtk_menu_item_init (GtkMenuItem *menu_item)
463 GtkStyleContext *context;
464 GtkMenuItemPrivate *priv;
466 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
469 menu_item->priv = priv;
471 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
474 priv->use_action_appearance = TRUE;
476 menu_item->priv->submenu = NULL;
477 menu_item->priv->toggle_size = 0;
478 menu_item->priv->accelerator_width = 0;
479 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
480 priv->submenu_direction = GTK_DIRECTION_LEFT;
482 priv->submenu_direction = GTK_DIRECTION_RIGHT;
483 priv->submenu_placement = GTK_TOP_BOTTOM;
484 priv->right_justify = FALSE;
485 priv->use_action_appearance = TRUE;
489 context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
490 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
496 * Creates a new #GtkMenuItem.
498 * Returns: the newly created #GtkMenuItem
501 gtk_menu_item_new (void)
503 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
507 * gtk_menu_item_new_with_label:
508 * @label: the text for the label
510 * Creates a new #GtkMenuItem whose child is a #GtkLabel.
512 * Returns: the newly created #GtkMenuItem
515 gtk_menu_item_new_with_label (const gchar *label)
517 return g_object_new (GTK_TYPE_MENU_ITEM,
524 * gtk_menu_item_new_with_mnemonic:
525 * @label: The text of the button, with an underscore in front of the
528 * Creates a new #GtkMenuItem containing a label.
530 * The label will be created using gtk_label_new_with_mnemonic(),
531 * so underscores in @label indicate the mnemonic for the menu item.
533 * Returns: a new #GtkMenuItem
536 gtk_menu_item_new_with_mnemonic (const gchar *label)
538 return g_object_new (GTK_TYPE_MENU_ITEM,
539 "use-underline", TRUE,
545 gtk_menu_item_dispose (GObject *object)
547 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
548 GtkMenuItemPrivate *priv = menu_item->priv;
552 gtk_action_disconnect_accelerator (priv->action);
553 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
556 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
560 gtk_menu_item_do_set_right_justified (GtkMenuItem *menu_item,
561 gboolean right_justified)
563 GtkMenuItemPrivate *priv = menu_item->priv;
565 right_justified = right_justified != FALSE;
567 if (priv->right_justify != right_justified)
569 priv->right_justify = right_justified;
570 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
575 gtk_menu_item_set_property (GObject *object,
580 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
584 case PROP_RIGHT_JUSTIFIED:
585 gtk_menu_item_do_set_right_justified (menu_item, g_value_get_boolean (value));
588 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
590 case PROP_ACCEL_PATH:
591 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
594 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
596 case PROP_USE_UNDERLINE:
597 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
599 case PROP_ACTIVATABLE_RELATED_ACTION:
600 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
602 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
603 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
606 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
612 gtk_menu_item_get_property (GObject *object,
617 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
618 GtkMenuItemPrivate *priv = menu_item->priv;
622 case PROP_RIGHT_JUSTIFIED:
623 g_value_set_boolean (value, menu_item->priv->right_justify);
626 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
628 case PROP_ACCEL_PATH:
629 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
632 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
634 case PROP_USE_UNDERLINE:
635 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
637 case PROP_ACTIVATABLE_RELATED_ACTION:
638 g_value_set_object (value, priv->action);
640 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
641 g_value_set_boolean (value, priv->use_action_appearance);
644 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
650 gtk_menu_item_destroy (GtkWidget *widget)
652 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
653 GtkMenuItemPrivate *priv = menu_item->priv;
656 gtk_widget_destroy (priv->submenu);
658 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
662 gtk_menu_item_detacher (GtkWidget *widget,
665 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
666 GtkMenuItemPrivate *priv = menu_item->priv;
668 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
670 priv->submenu = NULL;
674 get_arrow_size (GtkWidget *widget,
679 GtkStyleContext *style_context;
681 PangoContext *context;
682 PangoFontMetrics *metrics;
683 gfloat arrow_scaling;
688 gtk_widget_style_get (widget,
689 "arrow-scaling", &arrow_scaling,
690 "arrow-spacing", &arrow_spacing,
694 *spacing = arrow_spacing;
696 context = gtk_widget_get_pango_context (child);
697 style_context = gtk_widget_get_style_context (child);
698 state = gtk_widget_get_state_flags (child);
700 metrics = pango_context_get_metrics (context,
701 gtk_style_context_get_font (style_context, state),
702 pango_context_get_language (context));
704 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
705 pango_font_metrics_get_descent (metrics)));
707 pango_font_metrics_unref (metrics);
709 *size = *size * arrow_scaling;
714 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
719 if (GTK_IS_ACCEL_LABEL (widget))
723 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
724 *width = MAX (*width, w);
726 else if (GTK_IS_CONTAINER (widget))
727 gtk_container_foreach (GTK_CONTAINER (widget),
728 gtk_menu_item_accel_width_foreach,
733 get_minimum_width (GtkWidget *widget)
735 GtkStyleContext *style_context;
737 PangoContext *context;
738 PangoFontMetrics *metrics;
742 context = gtk_widget_get_pango_context (widget);
743 style_context = gtk_widget_get_style_context (widget);
744 state = gtk_widget_get_state_flags (widget);
746 metrics = pango_context_get_metrics (context,
747 gtk_style_context_get_font (style_context, state),
748 pango_context_get_language (context));
750 width = pango_font_metrics_get_approximate_char_width (metrics);
752 pango_font_metrics_unref (metrics);
754 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
756 return PANGO_PIXELS (width_chars * width);
760 gtk_menu_item_get_preferred_width (GtkWidget *widget,
764 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
765 GtkMenuItemPrivate *priv = menu_item->priv;
770 guint horizontal_padding;
772 GtkPackDirection pack_dir;
773 GtkPackDirection child_pack_dir;
774 gint min_width, nat_width;
775 GtkStyleContext *context;
779 min_width = nat_width = 0;
781 gtk_widget_style_get (widget,
782 "horizontal-padding", &horizontal_padding,
785 bin = GTK_BIN (widget);
786 parent = gtk_widget_get_parent (widget);
788 if (GTK_IS_MENU_BAR (parent))
790 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
791 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
795 pack_dir = GTK_PACK_DIRECTION_LTR;
796 child_pack_dir = GTK_PACK_DIRECTION_LTR;
799 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
801 context = gtk_widget_get_style_context (widget);
802 state = gtk_widget_get_state_flags (widget);
803 gtk_style_context_get_padding (context, state, &padding);
805 min_width = (border_width * 2) + padding.left + padding.right;
807 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
808 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
809 min_width += 2 * horizontal_padding;
811 nat_width = min_width;
813 child = gtk_bin_get_child (bin);
815 if (child != NULL && gtk_widget_get_visible (child))
817 GtkMenuItemPrivate *priv = menu_item->priv;
818 gint child_min, child_nat;
820 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
822 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
824 gint arrow_spacing, arrow_size;
826 get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
828 min_width += arrow_size;
829 min_width += arrow_spacing;
831 min_width = MAX (min_width, get_minimum_width (widget));
832 nat_width = min_width;
835 min_width += child_min;
836 nat_width += child_nat;
840 gtk_container_foreach (GTK_CONTAINER (menu_item),
841 gtk_menu_item_accel_width_foreach,
843 priv->accelerator_width = accel_width;
846 *minimum_size = min_width;
849 *natural_size = nat_width;
853 gtk_menu_item_real_get_height (GtkWidget *widget,
858 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
859 GtkMenuItemPrivate *priv = menu_item->priv;
861 GtkStyleContext *context;
867 guint horizontal_padding;
869 GtkPackDirection pack_dir;
870 GtkPackDirection child_pack_dir;
871 gint min_height, nat_height;
874 min_height = nat_height = 0;
876 context = gtk_widget_get_style_context (widget);
877 state = gtk_widget_get_state_flags (widget);
878 gtk_style_context_get_padding (context, state, &padding);
880 gtk_widget_style_get (widget,
881 "horizontal-padding", &horizontal_padding,
884 bin = GTK_BIN (widget);
885 parent = gtk_widget_get_parent (widget);
887 if (GTK_IS_MENU_BAR (parent))
889 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
890 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
894 pack_dir = GTK_PACK_DIRECTION_LTR;
895 child_pack_dir = GTK_PACK_DIRECTION_LTR;
898 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
899 min_height = (border_width * 2) + padding.top + padding.bottom;
901 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
902 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
903 min_height += 2 * horizontal_padding;
907 avail_size = for_size;
908 avail_size -= (border_width * 2) + padding.left + padding.right;
910 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
911 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
912 avail_size -= 2 * horizontal_padding;
915 nat_height = min_height;
917 child = gtk_bin_get_child (bin);
919 if (child != NULL && gtk_widget_get_visible (child))
921 gint child_min, child_nat;
922 gint arrow_size = 0, arrow_spacing = 0;
924 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
925 get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
929 avail_size -= (arrow_size + arrow_spacing);
930 gtk_widget_get_preferred_height_for_width (child,
937 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
940 min_height += child_min;
941 nat_height += child_nat;
943 min_height = MAX (min_height, arrow_size);
944 nat_height = MAX (nat_height, arrow_size);
946 else /* separator item */
948 gboolean wide_separators;
949 gint separator_height;
951 gtk_widget_style_get (widget,
952 "wide-separators", &wide_separators,
953 "separator-height", &separator_height,
958 min_height += separator_height;
959 nat_height += separator_height;
963 /* force odd, so that we can have the same space above and
966 if (min_height % 2 == 0)
968 if (nat_height % 2 == 0)
974 gtk_container_foreach (GTK_CONTAINER (menu_item),
975 gtk_menu_item_accel_width_foreach,
977 priv->accelerator_width = accel_width;
980 *minimum_size = min_height;
983 *natural_size = nat_height;
987 gtk_menu_item_get_preferred_height (GtkWidget *widget,
991 gtk_menu_item_real_get_height (widget, -1, minimum_size, natural_size);
995 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
1000 gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size);
1004 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1006 parent_buildable_iface = g_type_interface_peek_parent (iface);
1007 iface->add_child = gtk_menu_item_buildable_add_child;
1008 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1012 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1013 GtkBuilder *builder,
1017 if (type && strcmp (type, "submenu") == 0)
1018 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1019 GTK_WIDGET (child));
1021 parent_buildable_iface->add_child (buildable, builder, child, type);
1026 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1027 GtkBuilder *builder,
1029 const gchar *tagname,
1032 GtkWidget *toplevel;
1034 if (strcmp (tagname, "accelerator") == 0)
1036 GtkMenuShell *menu_shell;
1039 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1042 while (GTK_IS_MENU (menu_shell) &&
1043 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1044 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1046 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1050 /* Fall back to something ... */
1051 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1053 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1054 gtk_buildable_get_name (buildable));
1057 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1058 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1061 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1066 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1068 iface->update = gtk_menu_item_update;
1069 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1073 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1077 child = gtk_bin_get_child (GTK_BIN (menu_item));
1079 if (GTK_IS_LABEL (child))
1083 label = gtk_action_get_label (action);
1084 gtk_menu_item_set_label (menu_item, label);
1088 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1091 gtk_menu_item_update (GtkActivatable *activatable,
1093 const gchar *property_name)
1095 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1096 GtkMenuItemPrivate *priv = menu_item->priv;
1098 if (strcmp (property_name, "visible") == 0)
1099 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1100 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1101 else if (strcmp (property_name, "sensitive") == 0)
1102 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1103 else if (priv->use_action_appearance)
1105 if (strcmp (property_name, "label") == 0)
1106 activatable_update_label (menu_item, action);
1111 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1114 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1115 GtkMenuItemPrivate *priv = menu_item->priv;
1118 if (!priv->use_action_appearance || !action)
1120 label = gtk_bin_get_child (GTK_BIN (menu_item));
1122 if (GTK_IS_ACCEL_LABEL (label))
1123 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1129 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1130 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1132 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1134 if (priv->use_action_appearance)
1136 label = gtk_bin_get_child (GTK_BIN (menu_item));
1138 /* make sure label is a label, deleting it otherwise */
1139 if (label && !GTK_IS_LABEL (label))
1141 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1144 /* Make sure that menu_item has a label and that any
1145 * accelerators are set */
1146 gtk_menu_item_ensure_label (menu_item);
1147 gtk_menu_item_set_use_underline (menu_item, TRUE);
1148 /* Make label point to the menu_item's label */
1149 label = gtk_bin_get_child (GTK_BIN (menu_item));
1151 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1153 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1154 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1155 gtk_action_get_accel_closure (action));
1158 activatable_update_label (menu_item, action);
1163 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1166 GtkMenuItemPrivate *priv = menu_item->priv;
1168 if (priv->action == action)
1173 gtk_action_disconnect_accelerator (priv->action);
1178 const gchar *accel_path;
1180 accel_path = gtk_action_get_accel_path (action);
1183 gtk_action_connect_accelerator (action);
1184 gtk_menu_item_set_accel_path (menu_item, accel_path);
1188 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1190 priv->action = action;
1194 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1195 gboolean use_appearance)
1197 GtkMenuItemPrivate *priv = menu_item->priv;
1199 if (priv->use_action_appearance != use_appearance)
1201 priv->use_action_appearance = use_appearance;
1203 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1209 * gtk_menu_item_set_submenu:
1210 * @menu_item: a #GtkMenuItem
1211 * @submenu: (allow-none): the submenu, or %NULL
1213 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1214 * submenu is passed.
1217 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1220 GtkMenuItemPrivate *priv = menu_item->priv;
1222 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1223 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1225 if (priv->submenu != submenu)
1228 gtk_menu_detach (GTK_MENU (priv->submenu));
1232 priv->submenu = submenu;
1233 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1234 GTK_WIDGET (menu_item),
1235 gtk_menu_item_detacher);
1238 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1239 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1241 g_object_notify (G_OBJECT (menu_item), "submenu");
1246 * gtk_menu_item_get_submenu:
1247 * @menu_item: a #GtkMenuItem
1249 * Gets the submenu underneath this menu item, if any.
1250 * See gtk_menu_item_set_submenu().
1252 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1255 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1257 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1259 return menu_item->priv->submenu;
1262 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1263 GtkSubmenuPlacement placement);
1266 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1267 GtkSubmenuPlacement placement)
1269 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1271 menu_item->priv->submenu_placement = placement;
1275 gtk_menu_item_select (GtkMenuItem *menu_item)
1279 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1281 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1283 /* Enable themeing of the parent menu item depending on whether
1284 * something is selected in its submenu
1286 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1287 if (GTK_IS_MENU (parent))
1289 GtkMenu *menu = GTK_MENU (parent);
1291 if (menu->priv->parent_menu_item)
1292 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1297 * gtk_menu_item_deselect:
1298 * @menu_item: the menu item
1300 * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1301 * exactly like #gtk_item_deselect.
1304 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1308 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1310 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1312 /* Enable themeing of the parent menu item depending on whether
1313 * something is selected in its submenu
1315 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1316 if (GTK_IS_MENU (parent))
1318 GtkMenu *menu = GTK_MENU (parent);
1320 if (menu->priv->parent_menu_item)
1321 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1326 * gtk_menu_item_activate:
1327 * @menu_item: the menu item
1329 * Emits the #GtkMenuItem::activate signal on the given item
1332 gtk_menu_item_activate (GtkMenuItem *menu_item)
1334 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1336 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1340 * gtk_menu_item_toggle_size_request:
1341 * @menu_item: the menu item
1342 * @requisition: the requisition to use as signal data.
1344 * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1347 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1350 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1352 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1356 * gtk_menu_item_toggle_size_allocate:
1357 * @menu_item: the menu item.
1358 * @allocation: the allocation to use as signal data.
1360 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1363 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1366 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1368 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1372 gtk_menu_item_size_allocate (GtkWidget *widget,
1373 GtkAllocation *allocation)
1375 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1376 GtkMenuItemPrivate *priv = menu_item->priv;
1378 GtkAllocation child_allocation;
1379 GtkTextDirection direction;
1380 GtkPackDirection pack_dir;
1381 GtkPackDirection child_pack_dir;
1385 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1386 g_return_if_fail (allocation != NULL);
1388 bin = GTK_BIN (widget);
1390 direction = gtk_widget_get_direction (widget);
1392 parent = gtk_widget_get_parent (widget);
1393 if (GTK_IS_MENU_BAR (parent))
1395 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1396 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1400 pack_dir = GTK_PACK_DIRECTION_LTR;
1401 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1404 gtk_widget_set_allocation (widget, allocation);
1406 child = gtk_bin_get_child (bin);
1409 GtkStyleContext *context;
1410 GtkStateFlags state;
1412 guint horizontal_padding;
1415 context = gtk_widget_get_style_context (widget);
1416 state = gtk_widget_get_state_flags (widget);
1417 gtk_style_context_get_padding (context, state, &padding);
1419 gtk_widget_style_get (widget,
1420 "horizontal-padding", &horizontal_padding,
1423 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1424 child_allocation.x = border_width + padding.left;
1425 child_allocation.y = border_width + padding.top;
1427 child_allocation.width = allocation->width - (border_width * 2) -
1428 padding.left - padding.right;
1429 child_allocation.height = allocation->height - (border_width * 2) -
1430 padding.top - padding.bottom;
1432 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1433 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1435 child_allocation.x += horizontal_padding;
1436 child_allocation.width -= 2 * horizontal_padding;
1438 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1439 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1441 child_allocation.y += horizontal_padding;
1442 child_allocation.height -= 2 * horizontal_padding;
1445 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1446 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1448 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1449 child_allocation.x += priv->toggle_size;
1450 child_allocation.width -= priv->toggle_size;
1454 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1455 child_allocation.y += priv->toggle_size;
1456 child_allocation.height -= priv->toggle_size;
1459 child_allocation.x += allocation->x;
1460 child_allocation.y += allocation->y;
1462 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1464 gint arrow_spacing, arrow_size;
1466 get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
1468 if (direction == GTK_TEXT_DIR_RTL)
1469 child_allocation.x += arrow_size + arrow_spacing;
1470 child_allocation.width -= arrow_size + arrow_spacing;
1473 if (child_allocation.width < 1)
1474 child_allocation.width = 1;
1476 gtk_widget_size_allocate (child, &child_allocation);
1479 if (gtk_widget_get_realized (widget))
1480 gdk_window_move_resize (priv->event_window,
1481 allocation->x, allocation->y,
1482 allocation->width, allocation->height);
1485 gtk_menu_reposition (GTK_MENU (priv->submenu));
1489 gtk_menu_item_realize (GtkWidget *widget)
1491 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1492 GtkMenuItemPrivate *priv = menu_item->priv;
1493 GtkAllocation allocation;
1495 GdkWindowAttr attributes;
1496 gint attributes_mask;
1498 gtk_widget_set_realized (widget, TRUE);
1500 window = gtk_widget_get_parent_window (widget);
1501 gtk_widget_set_window (widget, window);
1502 g_object_ref (window);
1504 gtk_widget_get_allocation (widget, &allocation);
1506 attributes.x = allocation.x;
1507 attributes.y = allocation.y;
1508 attributes.width = allocation.width;
1509 attributes.height = allocation.height;
1510 attributes.window_type = GDK_WINDOW_CHILD;
1511 attributes.wclass = GDK_INPUT_ONLY;
1512 attributes.event_mask = (gtk_widget_get_events (widget) |
1513 GDK_BUTTON_PRESS_MASK |
1514 GDK_BUTTON_RELEASE_MASK |
1515 GDK_ENTER_NOTIFY_MASK |
1516 GDK_LEAVE_NOTIFY_MASK |
1517 GDK_POINTER_MOTION_MASK);
1519 attributes_mask = GDK_WA_X | GDK_WA_Y;
1521 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1522 &attributes, attributes_mask);
1523 gdk_window_set_user_data (priv->event_window, widget);
1527 gtk_menu_item_unrealize (GtkWidget *widget)
1529 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1530 GtkMenuItemPrivate *priv = menu_item->priv;
1532 gdk_window_set_user_data (priv->event_window, NULL);
1533 gdk_window_destroy (priv->event_window);
1534 priv->event_window = NULL;
1536 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1540 gtk_menu_item_map (GtkWidget *widget)
1542 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1543 GtkMenuItemPrivate *priv = menu_item->priv;
1545 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1547 gdk_window_show (priv->event_window);
1551 gtk_menu_item_unmap (GtkWidget *widget)
1553 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1554 GtkMenuItemPrivate *priv = menu_item->priv;
1556 gdk_window_hide (priv->event_window);
1558 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1562 gtk_menu_item_enter (GtkWidget *widget,
1563 GdkEventCrossing *event)
1565 g_return_val_if_fail (event != NULL, FALSE);
1567 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1571 gtk_menu_item_leave (GtkWidget *widget,
1572 GdkEventCrossing *event)
1574 g_return_val_if_fail (event != NULL, FALSE);
1576 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1580 gtk_menu_item_draw (GtkWidget *widget,
1583 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1584 GtkMenuItemPrivate *priv = menu_item->priv;
1585 GtkStateFlags state;
1586 GtkStyleContext *context;
1588 GtkWidget *child, *parent;
1589 gint x, y, w, h, width, height;
1590 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1592 state = gtk_widget_get_state_flags (widget);
1593 context = gtk_widget_get_style_context (widget);
1594 width = gtk_widget_get_allocated_width (widget);
1595 height = gtk_widget_get_allocated_height (widget);
1599 w = width - border_width * 2;
1600 h = height - border_width * 2;
1602 child = gtk_bin_get_child (GTK_BIN (menu_item));
1603 parent = gtk_widget_get_parent (widget);
1605 gtk_style_context_get_padding (context, state, &padding);
1607 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1609 gtk_render_background (context, cr, x, y, w, h);
1610 gtk_render_frame (context, cr, x, y, w, h);
1613 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1615 gint arrow_x, arrow_y;
1617 guint horizontal_padding;
1618 GtkTextDirection direction;
1621 direction = gtk_widget_get_direction (widget);
1623 gtk_widget_style_get (widget,
1624 "horizontal-padding", &horizontal_padding,
1627 get_arrow_size (widget, child, &arrow_size, NULL);
1629 if (direction == GTK_TEXT_DIR_LTR)
1631 arrow_x = x + w - horizontal_padding - arrow_size;
1636 arrow_x = x + horizontal_padding;
1637 angle = (3 * G_PI) / 2;
1640 arrow_y = y + (h - arrow_size) / 2;
1642 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1646 gboolean wide_separators;
1647 gint separator_height;
1648 guint horizontal_padding;
1650 gtk_widget_style_get (widget,
1651 "wide-separators", &wide_separators,
1652 "separator-height", &separator_height,
1653 "horizontal-padding", &horizontal_padding,
1655 if (wide_separators)
1656 gtk_render_frame (context, cr,
1657 x + horizontal_padding + padding.left,
1659 w - (2 * horizontal_padding) - padding.left - padding.right,
1662 gtk_render_line (context, cr,
1663 x + horizontal_padding + padding.left,
1665 x + w - horizontal_padding - padding.right - 1,
1669 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1675 * gtk_menu_item_select:
1676 * @menu_item: the menu item
1678 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1679 * exactly like #gtk_item_select.
1682 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1684 GtkMenuItemPrivate *priv = menu_item->priv;
1685 GdkDevice *source_device = NULL;
1686 GdkEvent *current_event;
1688 current_event = gtk_get_current_event ();
1692 source_device = gdk_event_get_source_device (current_event);
1693 gdk_event_free (current_event);
1696 if ((!source_device ||
1697 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN) &&
1699 (!gtk_widget_get_mapped (priv->submenu) ||
1700 GTK_MENU (priv->submenu)->priv->tearoff_active))
1702 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1705 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1706 GTK_STATE_FLAG_PRELIGHT, FALSE);
1707 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1711 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1713 GtkMenuItemPrivate *priv = menu_item->priv;
1716 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1718 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1719 GTK_STATE_FLAG_PRELIGHT);
1720 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1724 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1725 gboolean group_cycling)
1729 parent = gtk_widget_get_parent (widget);
1731 if (GTK_IS_MENU_SHELL (parent))
1732 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1734 if (group_cycling &&
1736 GTK_IS_MENU_SHELL (parent) &&
1737 GTK_MENU_SHELL (parent)->priv->active)
1739 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1742 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1748 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1750 GtkMenuItemPrivate *priv = menu_item->priv;
1753 gtk_action_activate (priv->action);
1758 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1760 GtkMenuItemPrivate *priv = menu_item->priv;
1764 widget = GTK_WIDGET (menu_item);
1765 parent = gtk_widget_get_parent (widget);
1767 if (parent && GTK_IS_MENU_SHELL (parent))
1769 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1771 if (priv->submenu == NULL)
1772 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1775 gtk_menu_shell_select_item (menu_shell, widget);
1776 _gtk_menu_item_popup_submenu (widget, FALSE);
1778 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1784 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1787 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1793 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1796 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1798 menu_item->priv->toggle_size = allocation;
1802 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1807 gtk_menu_item_ensure_label (menu_item);
1809 child = gtk_bin_get_child (GTK_BIN (menu_item));
1810 if (GTK_IS_LABEL (child))
1812 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1814 g_object_notify (G_OBJECT (menu_item), "label");
1818 static const gchar *
1819 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1823 gtk_menu_item_ensure_label (menu_item);
1825 child = gtk_bin_get_child (GTK_BIN (menu_item));
1826 if (GTK_IS_LABEL (child))
1827 return gtk_label_get_label (GTK_LABEL (child));
1833 free_timeval (GTimeVal *val)
1835 g_slice_free (GTimeVal, val);
1839 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1840 gboolean remember_exact_time)
1842 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1843 GtkMenuItemPrivate *priv = menu_item->priv;
1846 parent = gtk_widget_get_parent (widget);
1848 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1850 gboolean take_focus;
1851 GtkMenuPositionFunc menu_position_func;
1853 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1854 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1856 if (remember_exact_time)
1858 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1860 g_get_current_time (popup_time);
1862 g_object_set_data_full (G_OBJECT (priv->submenu),
1863 "gtk-menu-exact-popup-time", popup_time,
1864 (GDestroyNotify) free_timeval);
1868 g_object_set_data (G_OBJECT (priv->submenu),
1869 "gtk-menu-exact-popup-time", NULL);
1872 /* gtk_menu_item_position_menu positions the submenu from the
1873 * menuitems position. If the menuitem doesn't have a window,
1874 * that doesn't work. In that case we use the default
1875 * positioning function instead which places the submenu at the
1878 if (gtk_widget_get_window (widget))
1879 menu_position_func = gtk_menu_item_position_menu;
1881 menu_position_func = NULL;
1883 gtk_menu_popup (GTK_MENU (priv->submenu),
1888 GTK_MENU_SHELL (parent)->priv->button,
1892 /* Enable themeing of the parent menu item depending on whether
1893 * its submenu is shown or not.
1895 gtk_widget_queue_draw (widget);
1899 gtk_menu_item_popup_timeout (gpointer data)
1901 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1902 GtkMenuItemPrivate *priv = menu_item->priv;
1905 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1907 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1908 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1910 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1911 if (priv->timer_from_keypress && priv->submenu)
1912 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1921 get_popup_delay (GtkWidget *widget)
1925 parent = gtk_widget_get_parent (widget);
1926 if (GTK_IS_MENU_SHELL (parent))
1928 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1934 g_object_get (gtk_widget_get_settings (widget),
1935 "gtk-menu-popup-delay", &popup_delay,
1943 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1944 gboolean with_delay)
1946 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1947 GtkMenuItemPrivate *priv = menu_item->priv;
1951 g_source_remove (priv->timer);
1958 gint popup_delay = get_popup_delay (widget);
1960 if (popup_delay > 0)
1962 GdkEvent *event = gtk_get_current_event ();
1964 priv->timer = gdk_threads_add_timeout (popup_delay,
1965 gtk_menu_item_popup_timeout,
1969 event->type != GDK_BUTTON_PRESS &&
1970 event->type != GDK_ENTER_NOTIFY)
1971 priv->timer_from_keypress = TRUE;
1973 priv->timer_from_keypress = FALSE;
1976 gdk_event_free (event);
1982 gtk_menu_item_real_popup_submenu (widget, FALSE);
1986 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1988 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1989 GtkMenuItemPrivate *priv = menu_item->priv;
1993 g_object_set_data (G_OBJECT (priv->submenu),
1994 "gtk-menu-exact-popup-time", NULL);
1998 g_source_remove (priv->timer);
2002 gtk_menu_popdown (GTK_MENU (priv->submenu));
2004 gtk_widget_queue_draw (widget);
2009 get_offsets (GtkMenu *menu,
2010 gint *horizontal_offset,
2011 gint *vertical_offset)
2013 gint vertical_padding;
2014 gint horizontal_padding;
2015 GtkStyleContext *context;
2016 GtkStateFlags state;
2019 gtk_widget_style_get (GTK_WIDGET (menu),
2020 "horizontal-offset", horizontal_offset,
2021 "vertical-offset", vertical_offset,
2022 "horizontal-padding", &horizontal_padding,
2023 "vertical-padding", &vertical_padding,
2026 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2027 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2028 gtk_style_context_get_padding (context, state, &padding);
2030 *vertical_offset -= padding.top;
2031 *vertical_offset -= vertical_padding;
2032 *horizontal_offset += horizontal_padding;
2036 gtk_menu_item_position_menu (GtkMenu *menu,
2042 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2043 GtkMenuItemPrivate *priv = menu_item->priv;
2044 GtkAllocation allocation;
2046 GtkMenuItem *parent_menu_item;
2049 gint twidth, theight;
2051 GtkTextDirection direction;
2052 GdkRectangle monitor;
2054 gint horizontal_offset;
2055 gint vertical_offset;
2056 gint available_left, available_right;
2057 GtkStyleContext *context;
2058 GtkStateFlags state;
2059 GtkBorder parent_padding;
2061 g_return_if_fail (menu != NULL);
2062 g_return_if_fail (x != NULL);
2063 g_return_if_fail (y != NULL);
2065 widget = GTK_WIDGET (user_data);
2070 direction = gtk_widget_get_direction (widget);
2072 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2073 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2075 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2076 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2077 if (monitor_num < 0)
2079 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2081 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2083 g_warning ("Menu not on screen");
2087 gtk_widget_get_allocation (widget, &allocation);
2092 get_offsets (menu, &horizontal_offset, &vertical_offset);
2094 available_left = tx - monitor.x;
2095 available_right = monitor.x + monitor.width - (tx + allocation.width);
2097 parent = gtk_widget_get_parent (widget);
2098 priv->from_menubar = GTK_IS_MENU_BAR (parent);
2100 switch (priv->submenu_placement)
2102 case GTK_TOP_BOTTOM:
2103 if (direction == GTK_TEXT_DIR_LTR)
2104 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2107 priv->submenu_direction = GTK_DIRECTION_LEFT;
2108 tx += allocation.width - twidth;
2110 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2111 ty += allocation.height;
2112 else if ((ty - theight) >= monitor.y)
2114 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2115 ty += allocation.height;
2120 case GTK_LEFT_RIGHT:
2121 if (GTK_IS_MENU (parent))
2122 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2124 parent_menu_item = NULL;
2126 context = gtk_widget_get_style_context (parent);
2127 state = gtk_widget_get_state_flags (parent);
2128 gtk_style_context_get_padding (context, state, &parent_padding);
2130 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2132 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2136 if (direction == GTK_TEXT_DIR_LTR)
2137 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2139 priv->submenu_direction = GTK_DIRECTION_LEFT;
2142 switch (priv->submenu_direction)
2144 case GTK_DIRECTION_LEFT:
2145 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2146 available_left >= available_right)
2147 tx -= twidth + parent_padding.left + horizontal_offset;
2150 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2151 tx += allocation.width + parent_padding.right + horizontal_offset;
2155 case GTK_DIRECTION_RIGHT:
2156 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2157 available_right >= available_left)
2158 tx += allocation.width + parent_padding.right + horizontal_offset;
2161 priv->submenu_direction = GTK_DIRECTION_LEFT;
2162 tx -= twidth + parent_padding.left + horizontal_offset;
2167 ty += vertical_offset;
2169 /* If the height of the menu doesn't fit we move it upward. */
2170 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2174 /* If we have negative, tx, here it is because we can't get
2175 * the menu all the way on screen. Favor the left portion.
2177 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2180 gtk_menu_set_monitor (menu, monitor_num);
2182 if (!gtk_widget_get_visible (menu->priv->toplevel))
2184 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2185 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2190 * gtk_menu_item_set_right_justified:
2191 * @menu_item: a #GtkMenuItem.
2192 * @right_justified: if %TRUE the menu item will appear at the
2193 * far right if added to a menu bar
2195 * Sets whether the menu item appears justified at the right
2196 * side of a menu bar. This was traditionally done for "Help"
2197 * menu items, but is now considered a bad idea. (If the widget
2198 * layout is reversed for a right-to-left language like Hebrew
2199 * or Arabic, right-justified-menu-items appear at the left.)
2201 * Deprecated: 3.2: If you insist on using it, use
2202 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2205 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2206 gboolean right_justified)
2208 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2210 gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2214 * gtk_menu_item_get_right_justified:
2215 * @menu_item: a #GtkMenuItem
2217 * Gets whether the menu item appears justified at the right
2218 * side of the menu bar.
2220 * Return value: %TRUE if the menu item will appear at the
2221 * far right if added to a menu bar.
2223 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2226 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2228 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2230 return menu_item->priv->right_justify;
2235 gtk_menu_item_show_all (GtkWidget *widget)
2237 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2238 GtkMenuItemPrivate *priv = menu_item->priv;
2240 /* show children including submenu */
2242 gtk_widget_show_all (priv->submenu);
2243 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2245 gtk_widget_show (widget);
2249 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2254 parent = gtk_widget_get_parent (widget);
2256 /* Chain to the parent GtkMenu for further checks */
2257 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2258 parent && gtk_widget_can_activate_accel (parent, signal_id));
2262 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2265 const gchar **path_p = data;
2269 if (GTK_IS_LABEL (widget))
2271 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2272 if (*path_p && (*path_p)[0] == 0)
2275 else if (GTK_IS_CONTAINER (widget))
2276 gtk_container_foreach (GTK_CONTAINER (widget),
2277 gtk_menu_item_accel_name_foreach,
2283 gtk_menu_item_parent_set (GtkWidget *widget,
2284 GtkWidget *previous_parent)
2286 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2290 parent = gtk_widget_get_parent (widget);
2291 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2294 _gtk_menu_item_refresh_accel_path (menu_item,
2295 menu->priv->accel_path,
2296 menu->priv->accel_group,
2299 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2300 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2304 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2305 const gchar *prefix,
2306 GtkAccelGroup *accel_group,
2307 gboolean group_changed)
2309 GtkMenuItemPrivate *priv = menu_item->priv;
2313 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2314 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2316 widget = GTK_WIDGET (menu_item);
2320 gtk_widget_set_accel_path (widget, NULL, NULL);
2324 path = _gtk_widget_get_accel_path (widget, NULL);
2325 if (!path) /* no active accel_path yet */
2327 path = priv->accel_path;
2328 if (!path && prefix)
2330 const gchar *postfix = NULL;
2333 /* try to construct one from label text */
2334 gtk_container_foreach (GTK_CONTAINER (menu_item),
2335 gtk_menu_item_accel_name_foreach,
2339 new_path = g_strconcat (prefix, "/", postfix, NULL);
2340 path = priv->accel_path = (char*)g_intern_string (new_path);
2345 gtk_widget_set_accel_path (widget, path, accel_group);
2347 else if (group_changed) /* reinstall accelerators */
2348 gtk_widget_set_accel_path (widget, path, accel_group);
2352 * gtk_menu_item_set_accel_path:
2353 * @menu_item: a valid #GtkMenuItem
2354 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2355 * item's functionality, or %NULL to unset the current path.
2357 * Set the accelerator path on @menu_item, through which runtime
2358 * changes of the menu item's accelerator caused by the user can be
2359 * identified and saved to persistent storage (see gtk_accel_map_save()
2360 * on this). To set up a default accelerator for this menu item, call
2361 * gtk_accel_map_add_entry() with the same @accel_path. See also
2362 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2363 * and gtk_menu_set_accel_path() for a more convenient variant of
2366 * This function is basically a convenience wrapper that handles
2367 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2368 * group for the menu item.
2370 * Note that you do need to set an accelerator on the parent menu with
2371 * gtk_menu_set_accel_group() for this to work.
2373 * Note that @accel_path string will be stored in a #GQuark.
2374 * Therefore, if you pass a static string, you can save some memory
2375 * by interning it first with g_intern_static_string().
2378 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2379 const gchar *accel_path)
2381 GtkMenuItemPrivate *priv = menu_item->priv;
2385 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2386 g_return_if_fail (accel_path == NULL ||
2387 (accel_path[0] == '<' && strchr (accel_path, '/')));
2389 widget = GTK_WIDGET (menu_item);
2391 /* store new path */
2392 priv->accel_path = (char*)g_intern_string (accel_path);
2394 /* forget accelerators associated with old path */
2395 gtk_widget_set_accel_path (widget, NULL, NULL);
2397 /* install accelerators associated with new path */
2398 parent = gtk_widget_get_parent (widget);
2399 if (GTK_IS_MENU (parent))
2401 GtkMenu *menu = GTK_MENU (parent);
2403 if (menu->priv->accel_group)
2404 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2406 menu->priv->accel_group,
2412 * gtk_menu_item_get_accel_path:
2413 * @menu_item: a valid #GtkMenuItem
2415 * Retrieve the accelerator path that was previously set on @menu_item.
2417 * See gtk_menu_item_set_accel_path() for details.
2419 * Returns: the accelerator path corresponding to this menu
2420 * item's functionality, or %NULL if not set
2425 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2427 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2429 return menu_item->priv->accel_path;
2433 gtk_menu_item_forall (GtkContainer *container,
2434 gboolean include_internals,
2435 GtkCallback callback,
2436 gpointer callback_data)
2440 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2441 g_return_if_fail (callback != NULL);
2443 child = gtk_bin_get_child (GTK_BIN (container));
2445 callback (child, callback_data);
2449 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2451 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2452 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2453 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2454 !gtk_widget_is_sensitive (menu_item) ||
2455 !gtk_widget_get_visible (menu_item))
2462 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2464 GtkWidget *accel_label;
2466 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2468 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2469 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2471 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2472 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2473 GTK_WIDGET (menu_item));
2474 gtk_widget_show (accel_label);
2479 * gtk_menu_item_set_label:
2480 * @menu_item: a #GtkMenuItem
2481 * @label: the text you want to set
2483 * Sets @text on the @menu_item label
2488 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2491 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2493 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2497 * gtk_menu_item_get_label:
2498 * @menu_item: a #GtkMenuItem
2500 * Sets @text on the @menu_item label
2502 * Returns: The text in the @menu_item label. This is the internal
2503 * string used by the label, and must not be modified.
2508 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2510 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2512 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2516 * gtk_menu_item_set_use_underline:
2517 * @menu_item: a #GtkMenuItem
2518 * @setting: %TRUE if underlines in the text indicate mnemonics
2520 * If true, an underline in the text indicates the next character
2521 * should be used for the mnemonic accelerator key.
2526 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2531 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2533 gtk_menu_item_ensure_label (menu_item);
2535 child = gtk_bin_get_child (GTK_BIN (menu_item));
2536 if (GTK_IS_LABEL (child))
2538 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2540 g_object_notify (G_OBJECT (menu_item), "use-underline");
2545 * gtk_menu_item_get_use_underline:
2546 * @menu_item: a #GtkMenuItem
2548 * Checks if an underline in the text indicates the next character
2549 * should be used for the mnemonic accelerator key.
2551 * Return value: %TRUE if an embedded underline in the label
2552 * indicates the mnemonic accelerator key.
2557 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2561 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2563 gtk_menu_item_ensure_label (menu_item);
2565 child = gtk_bin_get_child (GTK_BIN (menu_item));
2566 if (GTK_IS_LABEL (child))
2567 return gtk_label_get_use_underline (GTK_LABEL (child));
2573 * gtk_menu_item_set_reserve_indicator:
2574 * @menu_item: a #GtkMenuItem
2575 * @reserve: the new value
2577 * Sets whether the @menu_item should reserve space for
2578 * the submenu indicator, regardless if it actually has
2581 * There should be little need for applications to call
2587 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2590 GtkMenuItemPrivate *priv;
2592 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2594 priv = menu_item->priv;
2596 if (priv->reserve_indicator != reserve)
2598 priv->reserve_indicator = reserve;
2599 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2604 * gtk_menu_item_get_reserve_indicator:
2605 * @menu_item: a #GtkMenuItem
2607 * Returns whether the @menu_item reserves space for
2608 * the submenu indicator, regardless if it has a submenu
2611 * Returns: %TRUE if @menu_item always reserves space for the
2617 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2619 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2621 return menu_item->priv->reserve_indicator;