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_save (context);
1607 gtk_style_context_get_padding (context, state, &padding);
1609 if (GTK_IS_MENU_BAR (parent))
1610 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1612 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1614 gtk_render_background (context, cr, x, y, w, h);
1615 gtk_render_frame (context, cr, x, y, w, h);
1618 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1620 gint arrow_x, arrow_y;
1622 guint horizontal_padding;
1623 GtkTextDirection direction;
1626 direction = gtk_widget_get_direction (widget);
1628 gtk_widget_style_get (widget,
1629 "horizontal-padding", &horizontal_padding,
1632 get_arrow_size (widget, child, &arrow_size, NULL);
1634 if (direction == GTK_TEXT_DIR_LTR)
1636 arrow_x = x + w - horizontal_padding - arrow_size;
1641 arrow_x = x + horizontal_padding;
1642 angle = (3 * G_PI) / 2;
1645 arrow_y = y + (h - arrow_size) / 2;
1647 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1651 gboolean wide_separators;
1652 gint separator_height;
1653 guint horizontal_padding;
1655 gtk_widget_style_get (widget,
1656 "wide-separators", &wide_separators,
1657 "separator-height", &separator_height,
1658 "horizontal-padding", &horizontal_padding,
1660 if (wide_separators)
1661 gtk_render_frame (context, cr,
1662 x + horizontal_padding + padding.left,
1664 w - (2 * horizontal_padding) - padding.left - padding.right,
1667 gtk_render_line (context, cr,
1668 x + horizontal_padding + padding.left,
1670 x + w - horizontal_padding - padding.right - 1,
1674 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1676 gtk_style_context_restore (context);
1682 * gtk_menu_item_select:
1683 * @menu_item: the menu item
1685 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1686 * exactly like #gtk_item_select.
1689 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1691 GtkMenuItemPrivate *priv = menu_item->priv;
1692 GdkDevice *source_device = NULL;
1693 GdkEvent *current_event;
1695 current_event = gtk_get_current_event ();
1699 source_device = gdk_event_get_source_device (current_event);
1700 gdk_event_free (current_event);
1703 if ((!source_device ||
1704 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN) &&
1706 (!gtk_widget_get_mapped (priv->submenu) ||
1707 GTK_MENU (priv->submenu)->priv->tearoff_active))
1709 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1712 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1713 GTK_STATE_FLAG_PRELIGHT, FALSE);
1714 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1718 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1720 GtkMenuItemPrivate *priv = menu_item->priv;
1723 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1725 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1726 GTK_STATE_FLAG_PRELIGHT);
1727 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1731 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1732 gboolean group_cycling)
1736 parent = gtk_widget_get_parent (widget);
1738 if (GTK_IS_MENU_SHELL (parent))
1739 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1741 if (group_cycling &&
1743 GTK_IS_MENU_SHELL (parent) &&
1744 GTK_MENU_SHELL (parent)->priv->active)
1746 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1749 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1755 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1757 GtkMenuItemPrivate *priv = menu_item->priv;
1760 gtk_action_activate (priv->action);
1765 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1767 GtkMenuItemPrivate *priv = menu_item->priv;
1771 widget = GTK_WIDGET (menu_item);
1772 parent = gtk_widget_get_parent (widget);
1774 if (parent && GTK_IS_MENU_SHELL (parent))
1776 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1778 if (priv->submenu == NULL)
1779 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1782 gtk_menu_shell_select_item (menu_shell, widget);
1783 _gtk_menu_item_popup_submenu (widget, FALSE);
1785 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1791 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1794 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1800 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1803 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1805 menu_item->priv->toggle_size = allocation;
1809 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1814 gtk_menu_item_ensure_label (menu_item);
1816 child = gtk_bin_get_child (GTK_BIN (menu_item));
1817 if (GTK_IS_LABEL (child))
1819 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1821 g_object_notify (G_OBJECT (menu_item), "label");
1825 static const gchar *
1826 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1830 gtk_menu_item_ensure_label (menu_item);
1832 child = gtk_bin_get_child (GTK_BIN (menu_item));
1833 if (GTK_IS_LABEL (child))
1834 return gtk_label_get_label (GTK_LABEL (child));
1840 free_timeval (GTimeVal *val)
1842 g_slice_free (GTimeVal, val);
1846 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1847 gboolean remember_exact_time)
1849 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1850 GtkMenuItemPrivate *priv = menu_item->priv;
1853 parent = gtk_widget_get_parent (widget);
1855 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1857 gboolean take_focus;
1858 GtkMenuPositionFunc menu_position_func;
1860 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1861 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1863 if (remember_exact_time)
1865 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1867 g_get_current_time (popup_time);
1869 g_object_set_data_full (G_OBJECT (priv->submenu),
1870 "gtk-menu-exact-popup-time", popup_time,
1871 (GDestroyNotify) free_timeval);
1875 g_object_set_data (G_OBJECT (priv->submenu),
1876 "gtk-menu-exact-popup-time", NULL);
1879 /* gtk_menu_item_position_menu positions the submenu from the
1880 * menuitems position. If the menuitem doesn't have a window,
1881 * that doesn't work. In that case we use the default
1882 * positioning function instead which places the submenu at the
1885 if (gtk_widget_get_window (widget))
1886 menu_position_func = gtk_menu_item_position_menu;
1888 menu_position_func = NULL;
1890 gtk_menu_popup (GTK_MENU (priv->submenu),
1895 GTK_MENU_SHELL (parent)->priv->button,
1899 /* Enable themeing of the parent menu item depending on whether
1900 * its submenu is shown or not.
1902 gtk_widget_queue_draw (widget);
1906 gtk_menu_item_popup_timeout (gpointer data)
1908 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1909 GtkMenuItemPrivate *priv = menu_item->priv;
1912 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1914 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1915 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1917 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1918 if (priv->timer_from_keypress && priv->submenu)
1919 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1928 get_popup_delay (GtkWidget *widget)
1932 parent = gtk_widget_get_parent (widget);
1933 if (GTK_IS_MENU_SHELL (parent))
1935 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1941 g_object_get (gtk_widget_get_settings (widget),
1942 "gtk-menu-popup-delay", &popup_delay,
1950 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1951 gboolean with_delay)
1953 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1954 GtkMenuItemPrivate *priv = menu_item->priv;
1958 g_source_remove (priv->timer);
1965 gint popup_delay = get_popup_delay (widget);
1967 if (popup_delay > 0)
1969 GdkEvent *event = gtk_get_current_event ();
1971 priv->timer = gdk_threads_add_timeout (popup_delay,
1972 gtk_menu_item_popup_timeout,
1976 event->type != GDK_BUTTON_PRESS &&
1977 event->type != GDK_ENTER_NOTIFY)
1978 priv->timer_from_keypress = TRUE;
1980 priv->timer_from_keypress = FALSE;
1983 gdk_event_free (event);
1989 gtk_menu_item_real_popup_submenu (widget, FALSE);
1993 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1995 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1996 GtkMenuItemPrivate *priv = menu_item->priv;
2000 g_object_set_data (G_OBJECT (priv->submenu),
2001 "gtk-menu-exact-popup-time", NULL);
2005 g_source_remove (priv->timer);
2009 gtk_menu_popdown (GTK_MENU (priv->submenu));
2011 gtk_widget_queue_draw (widget);
2016 get_offsets (GtkMenu *menu,
2017 gint *horizontal_offset,
2018 gint *vertical_offset)
2020 gint vertical_padding;
2021 gint horizontal_padding;
2022 GtkStyleContext *context;
2023 GtkStateFlags state;
2026 gtk_widget_style_get (GTK_WIDGET (menu),
2027 "horizontal-offset", horizontal_offset,
2028 "vertical-offset", vertical_offset,
2029 "horizontal-padding", &horizontal_padding,
2030 "vertical-padding", &vertical_padding,
2033 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2034 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2035 gtk_style_context_get_padding (context, state, &padding);
2037 *vertical_offset -= padding.top;
2038 *vertical_offset -= vertical_padding;
2039 *horizontal_offset += horizontal_padding;
2043 gtk_menu_item_position_menu (GtkMenu *menu,
2049 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2050 GtkMenuItemPrivate *priv = menu_item->priv;
2051 GtkAllocation allocation;
2053 GtkMenuItem *parent_menu_item;
2056 gint twidth, theight;
2058 GtkTextDirection direction;
2059 GdkRectangle monitor;
2061 gint horizontal_offset;
2062 gint vertical_offset;
2063 gint available_left, available_right;
2064 GtkStyleContext *context;
2065 GtkStateFlags state;
2066 GtkBorder parent_padding;
2068 g_return_if_fail (menu != NULL);
2069 g_return_if_fail (x != NULL);
2070 g_return_if_fail (y != NULL);
2072 widget = GTK_WIDGET (user_data);
2077 direction = gtk_widget_get_direction (widget);
2079 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2080 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2082 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2083 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2084 if (monitor_num < 0)
2086 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2088 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2090 g_warning ("Menu not on screen");
2094 gtk_widget_get_allocation (widget, &allocation);
2099 get_offsets (menu, &horizontal_offset, &vertical_offset);
2101 available_left = tx - monitor.x;
2102 available_right = monitor.x + monitor.width - (tx + allocation.width);
2104 parent = gtk_widget_get_parent (widget);
2105 priv->from_menubar = GTK_IS_MENU_BAR (parent);
2107 switch (priv->submenu_placement)
2109 case GTK_TOP_BOTTOM:
2110 if (direction == GTK_TEXT_DIR_LTR)
2111 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2114 priv->submenu_direction = GTK_DIRECTION_LEFT;
2115 tx += allocation.width - twidth;
2117 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2118 ty += allocation.height;
2119 else if ((ty - theight) >= monitor.y)
2121 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2122 ty += allocation.height;
2127 case GTK_LEFT_RIGHT:
2128 if (GTK_IS_MENU (parent))
2129 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2131 parent_menu_item = NULL;
2133 context = gtk_widget_get_style_context (parent);
2134 state = gtk_widget_get_state_flags (parent);
2135 gtk_style_context_get_padding (context, state, &parent_padding);
2137 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2139 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2143 if (direction == GTK_TEXT_DIR_LTR)
2144 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2146 priv->submenu_direction = GTK_DIRECTION_LEFT;
2149 switch (priv->submenu_direction)
2151 case GTK_DIRECTION_LEFT:
2152 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2153 available_left >= available_right)
2154 tx -= twidth + parent_padding.left + horizontal_offset;
2157 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2158 tx += allocation.width + parent_padding.right + horizontal_offset;
2162 case GTK_DIRECTION_RIGHT:
2163 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2164 available_right >= available_left)
2165 tx += allocation.width + parent_padding.right + horizontal_offset;
2168 priv->submenu_direction = GTK_DIRECTION_LEFT;
2169 tx -= twidth + parent_padding.left + horizontal_offset;
2174 ty += vertical_offset;
2176 /* If the height of the menu doesn't fit we move it upward. */
2177 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2181 /* If we have negative, tx, here it is because we can't get
2182 * the menu all the way on screen. Favor the left portion.
2184 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2187 gtk_menu_set_monitor (menu, monitor_num);
2189 if (!gtk_widget_get_visible (menu->priv->toplevel))
2191 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2192 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2197 * gtk_menu_item_set_right_justified:
2198 * @menu_item: a #GtkMenuItem.
2199 * @right_justified: if %TRUE the menu item will appear at the
2200 * far right if added to a menu bar
2202 * Sets whether the menu item appears justified at the right
2203 * side of a menu bar. This was traditionally done for "Help"
2204 * menu items, but is now considered a bad idea. (If the widget
2205 * layout is reversed for a right-to-left language like Hebrew
2206 * or Arabic, right-justified-menu-items appear at the left.)
2208 * Deprecated: 3.2: If you insist on using it, use
2209 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2212 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2213 gboolean right_justified)
2215 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2217 gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2221 * gtk_menu_item_get_right_justified:
2222 * @menu_item: a #GtkMenuItem
2224 * Gets whether the menu item appears justified at the right
2225 * side of the menu bar.
2227 * Return value: %TRUE if the menu item will appear at the
2228 * far right if added to a menu bar.
2230 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2233 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2235 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2237 return menu_item->priv->right_justify;
2242 gtk_menu_item_show_all (GtkWidget *widget)
2244 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2245 GtkMenuItemPrivate *priv = menu_item->priv;
2247 /* show children including submenu */
2249 gtk_widget_show_all (priv->submenu);
2250 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2252 gtk_widget_show (widget);
2256 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2261 parent = gtk_widget_get_parent (widget);
2263 /* Chain to the parent GtkMenu for further checks */
2264 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2265 parent && gtk_widget_can_activate_accel (parent, signal_id));
2269 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2272 const gchar **path_p = data;
2276 if (GTK_IS_LABEL (widget))
2278 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2279 if (*path_p && (*path_p)[0] == 0)
2282 else if (GTK_IS_CONTAINER (widget))
2283 gtk_container_foreach (GTK_CONTAINER (widget),
2284 gtk_menu_item_accel_name_foreach,
2290 gtk_menu_item_parent_set (GtkWidget *widget,
2291 GtkWidget *previous_parent)
2293 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2297 parent = gtk_widget_get_parent (widget);
2298 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2301 _gtk_menu_item_refresh_accel_path (menu_item,
2302 menu->priv->accel_path,
2303 menu->priv->accel_group,
2306 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2307 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2311 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2312 const gchar *prefix,
2313 GtkAccelGroup *accel_group,
2314 gboolean group_changed)
2316 GtkMenuItemPrivate *priv = menu_item->priv;
2320 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2321 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2323 widget = GTK_WIDGET (menu_item);
2327 gtk_widget_set_accel_path (widget, NULL, NULL);
2331 path = _gtk_widget_get_accel_path (widget, NULL);
2332 if (!path) /* no active accel_path yet */
2334 path = priv->accel_path;
2335 if (!path && prefix)
2337 const gchar *postfix = NULL;
2340 /* try to construct one from label text */
2341 gtk_container_foreach (GTK_CONTAINER (menu_item),
2342 gtk_menu_item_accel_name_foreach,
2346 new_path = g_strconcat (prefix, "/", postfix, NULL);
2347 path = priv->accel_path = (char*)g_intern_string (new_path);
2352 gtk_widget_set_accel_path (widget, path, accel_group);
2354 else if (group_changed) /* reinstall accelerators */
2355 gtk_widget_set_accel_path (widget, path, accel_group);
2359 * gtk_menu_item_set_accel_path:
2360 * @menu_item: a valid #GtkMenuItem
2361 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2362 * item's functionality, or %NULL to unset the current path.
2364 * Set the accelerator path on @menu_item, through which runtime
2365 * changes of the menu item's accelerator caused by the user can be
2366 * identified and saved to persistent storage (see gtk_accel_map_save()
2367 * on this). To set up a default accelerator for this menu item, call
2368 * gtk_accel_map_add_entry() with the same @accel_path. See also
2369 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2370 * and gtk_menu_set_accel_path() for a more convenient variant of
2373 * This function is basically a convenience wrapper that handles
2374 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2375 * group for the menu item.
2377 * Note that you do need to set an accelerator on the parent menu with
2378 * gtk_menu_set_accel_group() for this to work.
2380 * Note that @accel_path string will be stored in a #GQuark.
2381 * Therefore, if you pass a static string, you can save some memory
2382 * by interning it first with g_intern_static_string().
2385 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2386 const gchar *accel_path)
2388 GtkMenuItemPrivate *priv = menu_item->priv;
2392 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2393 g_return_if_fail (accel_path == NULL ||
2394 (accel_path[0] == '<' && strchr (accel_path, '/')));
2396 widget = GTK_WIDGET (menu_item);
2398 /* store new path */
2399 priv->accel_path = (char*)g_intern_string (accel_path);
2401 /* forget accelerators associated with old path */
2402 gtk_widget_set_accel_path (widget, NULL, NULL);
2404 /* install accelerators associated with new path */
2405 parent = gtk_widget_get_parent (widget);
2406 if (GTK_IS_MENU (parent))
2408 GtkMenu *menu = GTK_MENU (parent);
2410 if (menu->priv->accel_group)
2411 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2413 menu->priv->accel_group,
2419 * gtk_menu_item_get_accel_path:
2420 * @menu_item: a valid #GtkMenuItem
2422 * Retrieve the accelerator path that was previously set on @menu_item.
2424 * See gtk_menu_item_set_accel_path() for details.
2426 * Returns: the accelerator path corresponding to this menu
2427 * item's functionality, or %NULL if not set
2432 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2434 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2436 return menu_item->priv->accel_path;
2440 gtk_menu_item_forall (GtkContainer *container,
2441 gboolean include_internals,
2442 GtkCallback callback,
2443 gpointer callback_data)
2447 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2448 g_return_if_fail (callback != NULL);
2450 child = gtk_bin_get_child (GTK_BIN (container));
2452 callback (child, callback_data);
2456 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2458 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2459 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2460 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2461 !gtk_widget_is_sensitive (menu_item) ||
2462 !gtk_widget_get_visible (menu_item))
2469 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2471 GtkWidget *accel_label;
2473 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2475 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2476 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2478 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2479 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2480 GTK_WIDGET (menu_item));
2481 gtk_widget_show (accel_label);
2486 * gtk_menu_item_set_label:
2487 * @menu_item: a #GtkMenuItem
2488 * @label: the text you want to set
2490 * Sets @text on the @menu_item label
2495 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2498 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2500 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2504 * gtk_menu_item_get_label:
2505 * @menu_item: a #GtkMenuItem
2507 * Sets @text on the @menu_item label
2509 * Returns: The text in the @menu_item label. This is the internal
2510 * string used by the label, and must not be modified.
2515 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2517 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2519 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2523 * gtk_menu_item_set_use_underline:
2524 * @menu_item: a #GtkMenuItem
2525 * @setting: %TRUE if underlines in the text indicate mnemonics
2527 * If true, an underline in the text indicates the next character
2528 * should be used for the mnemonic accelerator key.
2533 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2538 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2540 gtk_menu_item_ensure_label (menu_item);
2542 child = gtk_bin_get_child (GTK_BIN (menu_item));
2543 if (GTK_IS_LABEL (child))
2545 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2547 g_object_notify (G_OBJECT (menu_item), "use-underline");
2552 * gtk_menu_item_get_use_underline:
2553 * @menu_item: a #GtkMenuItem
2555 * Checks if an underline in the text indicates the next character
2556 * should be used for the mnemonic accelerator key.
2558 * Return value: %TRUE if an embedded underline in the label
2559 * indicates the mnemonic accelerator key.
2564 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2568 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2570 gtk_menu_item_ensure_label (menu_item);
2572 child = gtk_bin_get_child (GTK_BIN (menu_item));
2573 if (GTK_IS_LABEL (child))
2574 return gtk_label_get_use_underline (GTK_LABEL (child));
2580 * gtk_menu_item_set_reserve_indicator:
2581 * @menu_item: a #GtkMenuItem
2582 * @reserve: the new value
2584 * Sets whether the @menu_item should reserve space for
2585 * the submenu indicator, regardless if it actually has
2588 * There should be little need for applications to call
2594 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2597 GtkMenuItemPrivate *priv;
2599 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2601 priv = menu_item->priv;
2603 if (priv->reserve_indicator != reserve)
2605 priv->reserve_indicator = reserve;
2606 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2611 * gtk_menu_item_get_reserve_indicator:
2612 * @menu_item: a #GtkMenuItem
2614 * Returns whether the @menu_item reserves space for
2615 * the submenu indicator, regardless if it has a submenu
2618 * Returns: %TRUE if @menu_item always reserves space for the
2624 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2626 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2628 return menu_item->priv->reserve_indicator;