1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkaccellabel.h"
33 #include "gtkmarshalers.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkmenuitemprivate.h"
37 #include "gtkmenubar.h"
38 #include "gtkmenuprivate.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtkprivate.h"
41 #include "gtkbuildable.h"
42 #include "gtkactivatable.h"
43 #include "gtkwidgetprivate.h"
45 #include "gtktypebuiltins.h"
46 #include "a11y/gtkmenuitemaccessible.h"
51 * @Short_description: The widget used for item in menus
53 * @See_also: #GtkBin, #GtkMenuShell
55 * The #GtkMenuItem widget and the derived widgets are the only valid
56 * childs for menus. Their function is to correctly handle highlighting,
57 * alignment, events and submenus.
59 * As it derives from #GtkBin it can hold any valid child widget, altough
60 * only a few are really useful.
62 * <refsect2 id="GtkMenuItem-BUILDER-UI">
63 * <title>GtkMenuItem as GtkBuildable</title>
64 * The GtkMenuItem implementation of the GtkBuildable interface
65 * supports adding a submenu by specifying "submenu" as the "type"
66 * attribute of a <child> element.
68 * <title>A UI definition fragment with submenus</title>
69 * <programlisting><![CDATA[
70 * <object class="GtkMenuItem">
71 * <child type="submenu">
72 * <object class="GtkMenu"/>
75 * ]]></programlisting>
99 /* activatable properties */
100 PROP_ACTIVATABLE_RELATED_ACTION,
101 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
105 static void gtk_menu_item_dispose (GObject *object);
106 static void gtk_menu_item_set_property (GObject *object,
110 static void gtk_menu_item_get_property (GObject *object,
114 static void gtk_menu_item_destroy (GtkWidget *widget);
115 static void gtk_menu_item_size_allocate (GtkWidget *widget,
116 GtkAllocation *allocation);
117 static void gtk_menu_item_realize (GtkWidget *widget);
118 static void gtk_menu_item_unrealize (GtkWidget *widget);
119 static void gtk_menu_item_map (GtkWidget *widget);
120 static void gtk_menu_item_unmap (GtkWidget *widget);
121 static gboolean gtk_menu_item_enter (GtkWidget *widget,
122 GdkEventCrossing *event);
123 static gboolean gtk_menu_item_leave (GtkWidget *widget,
124 GdkEventCrossing *event);
125 static gboolean gtk_menu_item_draw (GtkWidget *widget,
127 static void gtk_menu_item_parent_set (GtkWidget *widget,
128 GtkWidget *previous_parent);
131 static void gtk_real_menu_item_select (GtkMenuItem *item);
132 static void gtk_real_menu_item_deselect (GtkMenuItem *item);
133 static void gtk_real_menu_item_activate (GtkMenuItem *item);
134 static void gtk_real_menu_item_activate_item (GtkMenuItem *item);
135 static void gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
137 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
139 static gboolean gtk_menu_item_mnemonic_activate (GtkWidget *widget,
140 gboolean group_cycling);
142 static void gtk_menu_item_ensure_label (GtkMenuItem *menu_item);
143 static gint gtk_menu_item_popup_timeout (gpointer data);
144 static void gtk_menu_item_position_menu (GtkMenu *menu,
149 static void gtk_menu_item_show_all (GtkWidget *widget);
150 static void gtk_menu_item_forall (GtkContainer *container,
151 gboolean include_internals,
152 GtkCallback callback,
153 gpointer callback_data);
154 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
157 static void gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
159 static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
161 static void gtk_menu_item_get_preferred_width (GtkWidget *widget,
164 static void gtk_menu_item_get_preferred_height (GtkWidget *widget,
167 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
172 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface);
173 static void gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
177 static void gtk_menu_item_buildable_custom_finished(GtkBuildable *buildable,
180 const gchar *tagname,
183 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface);
184 static void gtk_menu_item_update (GtkActivatable *activatable,
186 const gchar *property_name);
187 static void gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
189 static void gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
191 static void gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
192 gboolean use_appearance);
194 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
196 static GtkBuildableIface *parent_buildable_iface;
198 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
199 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
200 gtk_menu_item_buildable_interface_init)
201 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
202 gtk_menu_item_activatable_interface_init))
205 gtk_menu_item_class_init (GtkMenuItemClass *klass)
207 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
209 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
211 gobject_class->dispose = gtk_menu_item_dispose;
212 gobject_class->set_property = gtk_menu_item_set_property;
213 gobject_class->get_property = gtk_menu_item_get_property;
215 widget_class->destroy = gtk_menu_item_destroy;
216 widget_class->size_allocate = gtk_menu_item_size_allocate;
217 widget_class->draw = gtk_menu_item_draw;
218 widget_class->realize = gtk_menu_item_realize;
219 widget_class->unrealize = gtk_menu_item_unrealize;
220 widget_class->map = gtk_menu_item_map;
221 widget_class->unmap = gtk_menu_item_unmap;
222 widget_class->enter_notify_event = gtk_menu_item_enter;
223 widget_class->leave_notify_event = gtk_menu_item_leave;
224 widget_class->show_all = gtk_menu_item_show_all;
225 widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
226 widget_class->parent_set = gtk_menu_item_parent_set;
227 widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
228 widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
229 widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
230 widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
232 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
234 container_class->forall = gtk_menu_item_forall;
236 klass->activate = gtk_real_menu_item_activate;
237 klass->activate_item = gtk_real_menu_item_activate_item;
238 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
239 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
240 klass->set_label = gtk_real_menu_item_set_label;
241 klass->get_label = gtk_real_menu_item_get_label;
242 klass->select = gtk_real_menu_item_select;
243 klass->deselect = gtk_real_menu_item_deselect;
245 klass->hide_on_activate = TRUE;
248 * GtkMenuItem::activate:
249 * @menuitem: the object which received the signal.
251 * Emitted when the item is activated.
253 menu_item_signals[ACTIVATE] =
254 g_signal_new (I_("activate"),
255 G_OBJECT_CLASS_TYPE (gobject_class),
256 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
257 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
259 _gtk_marshal_VOID__VOID,
261 widget_class->activate_signal = menu_item_signals[ACTIVATE];
264 * GtkMenuItem::activate-item:
265 * @menuitem: the object which received the signal.
267 * Emitted when the item is activated, but also if the menu item has a
268 * submenu. For normal applications, the relevant signal is
269 * #GtkMenuItem::activate.
271 menu_item_signals[ACTIVATE_ITEM] =
272 g_signal_new (I_("activate-item"),
273 G_OBJECT_CLASS_TYPE (gobject_class),
275 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
277 _gtk_marshal_VOID__VOID,
280 menu_item_signals[TOGGLE_SIZE_REQUEST] =
281 g_signal_new (I_("toggle-size-request"),
282 G_OBJECT_CLASS_TYPE (gobject_class),
284 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
286 _gtk_marshal_VOID__POINTER,
290 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
291 g_signal_new (I_("toggle-size-allocate"),
292 G_OBJECT_CLASS_TYPE (gobject_class),
294 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
296 _gtk_marshal_VOID__INT,
300 menu_item_signals[SELECT] =
301 g_signal_new (I_("select"),
302 G_OBJECT_CLASS_TYPE (gobject_class),
304 G_STRUCT_OFFSET (GtkMenuItemClass, select),
306 _gtk_marshal_VOID__VOID,
309 menu_item_signals[DESELECT] =
310 g_signal_new (I_("deselect"),
311 G_OBJECT_CLASS_TYPE (gobject_class),
313 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
315 _gtk_marshal_VOID__VOID,
319 * GtkMenuItem:right-justified:
321 * Sets whether the menu item appears justified
322 * at the right side of a menu bar.
326 g_object_class_install_property (gobject_class,
327 PROP_RIGHT_JUSTIFIED,
328 g_param_spec_boolean ("right-justified",
329 P_("Right Justified"),
330 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
332 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
335 * GtkMenuItem:submenu:
337 * The submenu attached to the menu item, or %NULL if it has none.
341 g_object_class_install_property (gobject_class,
343 g_param_spec_object ("submenu",
345 P_("The submenu attached to the menu item, or NULL if it has none"),
347 GTK_PARAM_READWRITE));
350 * GtkMenuItem:accel-path:
352 * Sets the accelerator path of the menu item, through which runtime
353 * changes of the menu item's accelerator caused by the user can be
354 * identified and saved to persistant storage.
358 g_object_class_install_property (gobject_class,
360 g_param_spec_string ("accel-path",
362 P_("Sets the accelerator path of the menu item"),
364 GTK_PARAM_READWRITE));
369 * The text for the child label.
373 g_object_class_install_property (gobject_class,
375 g_param_spec_string ("label",
377 P_("The text for the child label"),
379 GTK_PARAM_READWRITE));
382 * GtkMenuItem:use-underline:
384 * %TRUE if underlines in the text indicate mnemonics.
388 g_object_class_install_property (gobject_class,
390 g_param_spec_boolean ("use-underline",
392 P_("If set, an underline in the text indicates "
393 "the next character should be used for the "
394 "mnemonic accelerator key"),
396 GTK_PARAM_READWRITE));
398 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
399 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
401 gtk_widget_class_install_style_property_parser (widget_class,
402 g_param_spec_enum ("selected-shadow-type",
403 "Selected Shadow Type",
404 "Shadow type when item is selected",
405 GTK_TYPE_SHADOW_TYPE,
408 gtk_rc_property_parse_enum);
410 gtk_widget_class_install_style_property (widget_class,
411 g_param_spec_int ("horizontal-padding",
412 "Horizontal Padding",
413 "Padding to left and right of the menu item",
417 GTK_PARAM_READABLE));
419 gtk_widget_class_install_style_property (widget_class,
420 g_param_spec_int ("toggle-spacing",
422 "Space between icon and label",
426 GTK_PARAM_READABLE));
428 gtk_widget_class_install_style_property (widget_class,
429 g_param_spec_int ("arrow-spacing",
431 "Space between label and arrow",
435 GTK_PARAM_READABLE));
437 gtk_widget_class_install_style_property (widget_class,
438 g_param_spec_float ("arrow-scaling",
440 P_("Amount of space used up by arrow, relative to the menu item's font size"),
442 GTK_PARAM_READABLE));
445 * GtkMenuItem:width-chars:
447 * The minimum desired width of the menu item in characters.
451 gtk_widget_class_install_style_property (widget_class,
452 g_param_spec_int ("width-chars",
453 P_("Width in Characters"),
454 P_("The minimum desired width of the menu item in characters"),
456 GTK_PARAM_READABLE));
458 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
462 gtk_menu_item_init (GtkMenuItem *menu_item)
464 GtkStyleContext *context;
465 GtkMenuItemPrivate *priv;
467 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
470 menu_item->priv = priv;
472 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
475 priv->use_action_appearance = TRUE;
477 menu_item->priv->submenu = NULL;
478 menu_item->priv->toggle_size = 0;
479 menu_item->priv->accelerator_width = 0;
480 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
481 priv->submenu_direction = GTK_DIRECTION_LEFT;
483 priv->submenu_direction = GTK_DIRECTION_RIGHT;
484 priv->submenu_placement = GTK_TOP_BOTTOM;
485 priv->right_justify = FALSE;
486 priv->use_action_appearance = TRUE;
490 context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
491 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
497 * Creates a new #GtkMenuItem.
499 * Returns: the newly created #GtkMenuItem
502 gtk_menu_item_new (void)
504 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
508 * gtk_menu_item_new_with_label:
509 * @label: the text for the label
511 * Creates a new #GtkMenuItem whose child is a #GtkLabel.
513 * Returns: the newly created #GtkMenuItem
516 gtk_menu_item_new_with_label (const gchar *label)
518 return g_object_new (GTK_TYPE_MENU_ITEM,
525 * gtk_menu_item_new_with_mnemonic:
526 * @label: The text of the button, with an underscore in front of the
529 * Creates a new #GtkMenuItem containing a label.
531 * The label will be created using gtk_label_new_with_mnemonic(),
532 * so underscores in @label indicate the mnemonic for the menu item.
534 * Returns: a new #GtkMenuItem
537 gtk_menu_item_new_with_mnemonic (const gchar *label)
539 return g_object_new (GTK_TYPE_MENU_ITEM,
540 "use-underline", TRUE,
546 gtk_menu_item_dispose (GObject *object)
548 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
549 GtkMenuItemPrivate *priv = menu_item->priv;
553 gtk_action_disconnect_accelerator (priv->action);
554 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
557 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
561 gtk_menu_item_do_set_right_justified (GtkMenuItem *menu_item,
562 gboolean right_justified)
564 GtkMenuItemPrivate *priv = menu_item->priv;
566 right_justified = right_justified != FALSE;
568 if (priv->right_justify != right_justified)
570 priv->right_justify = right_justified;
571 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
576 gtk_menu_item_set_property (GObject *object,
581 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
585 case PROP_RIGHT_JUSTIFIED:
586 gtk_menu_item_do_set_right_justified (menu_item, g_value_get_boolean (value));
589 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
591 case PROP_ACCEL_PATH:
592 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
595 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
597 case PROP_USE_UNDERLINE:
598 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
600 case PROP_ACTIVATABLE_RELATED_ACTION:
601 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
603 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
604 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
607 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613 gtk_menu_item_get_property (GObject *object,
618 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
619 GtkMenuItemPrivate *priv = menu_item->priv;
623 case PROP_RIGHT_JUSTIFIED:
624 g_value_set_boolean (value, menu_item->priv->right_justify);
627 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
629 case PROP_ACCEL_PATH:
630 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
633 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
635 case PROP_USE_UNDERLINE:
636 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
638 case PROP_ACTIVATABLE_RELATED_ACTION:
639 g_value_set_object (value, priv->action);
641 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
642 g_value_set_boolean (value, priv->use_action_appearance);
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651 gtk_menu_item_destroy (GtkWidget *widget)
653 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
654 GtkMenuItemPrivate *priv = menu_item->priv;
657 gtk_widget_destroy (priv->submenu);
659 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
663 gtk_menu_item_detacher (GtkWidget *widget,
666 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
667 GtkMenuItemPrivate *priv = menu_item->priv;
669 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
671 priv->submenu = NULL;
675 get_arrow_size (GtkWidget *widget,
680 GtkStyleContext *style_context;
682 PangoContext *context;
683 PangoFontMetrics *metrics;
684 gfloat arrow_scaling;
689 gtk_widget_style_get (widget,
690 "arrow-scaling", &arrow_scaling,
691 "arrow-spacing", &arrow_spacing,
695 *spacing = arrow_spacing;
697 context = gtk_widget_get_pango_context (child);
698 style_context = gtk_widget_get_style_context (child);
699 state = gtk_widget_get_state_flags (child);
701 metrics = pango_context_get_metrics (context,
702 gtk_style_context_get_font (style_context, state),
703 pango_context_get_language (context));
705 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
706 pango_font_metrics_get_descent (metrics)));
708 pango_font_metrics_unref (metrics);
710 *size = *size * arrow_scaling;
715 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
720 if (GTK_IS_ACCEL_LABEL (widget))
724 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
725 *width = MAX (*width, w);
727 else if (GTK_IS_CONTAINER (widget))
728 gtk_container_foreach (GTK_CONTAINER (widget),
729 gtk_menu_item_accel_width_foreach,
734 get_minimum_width (GtkWidget *widget)
736 GtkStyleContext *style_context;
738 PangoContext *context;
739 PangoFontMetrics *metrics;
743 context = gtk_widget_get_pango_context (widget);
744 style_context = gtk_widget_get_style_context (widget);
745 state = gtk_widget_get_state_flags (widget);
747 metrics = pango_context_get_metrics (context,
748 gtk_style_context_get_font (style_context, state),
749 pango_context_get_language (context));
751 width = pango_font_metrics_get_approximate_char_width (metrics);
753 pango_font_metrics_unref (metrics);
755 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
757 return PANGO_PIXELS (width_chars * width);
761 gtk_menu_item_get_preferred_width (GtkWidget *widget,
765 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
766 GtkMenuItemPrivate *priv = menu_item->priv;
771 guint horizontal_padding;
773 GtkPackDirection pack_dir;
774 GtkPackDirection child_pack_dir;
775 gint min_width, nat_width;
776 GtkStyleContext *context;
780 min_width = nat_width = 0;
782 gtk_widget_style_get (widget,
783 "horizontal-padding", &horizontal_padding,
786 bin = GTK_BIN (widget);
787 parent = gtk_widget_get_parent (widget);
789 if (GTK_IS_MENU_BAR (parent))
791 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
792 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
796 pack_dir = GTK_PACK_DIRECTION_LTR;
797 child_pack_dir = GTK_PACK_DIRECTION_LTR;
800 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
802 context = gtk_widget_get_style_context (widget);
803 state = gtk_widget_get_state_flags (widget);
804 gtk_style_context_get_padding (context, state, &padding);
806 min_width = (border_width * 2) + padding.left + padding.right;
808 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
809 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
810 min_width += 2 * horizontal_padding;
812 nat_width = min_width;
814 child = gtk_bin_get_child (bin);
816 if (child != NULL && gtk_widget_get_visible (child))
818 GtkMenuItemPrivate *priv = menu_item->priv;
819 gint child_min, child_nat;
821 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
823 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
825 gint arrow_spacing, arrow_size;
827 get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
829 min_width += arrow_size;
830 min_width += arrow_spacing;
832 min_width = MAX (min_width, get_minimum_width (widget));
833 nat_width = min_width;
836 min_width += child_min;
837 nat_width += child_nat;
841 gtk_container_foreach (GTK_CONTAINER (menu_item),
842 gtk_menu_item_accel_width_foreach,
844 priv->accelerator_width = accel_width;
847 *minimum_size = min_width;
850 *natural_size = nat_width;
854 gtk_menu_item_real_get_height (GtkWidget *widget,
859 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
860 GtkMenuItemPrivate *priv = menu_item->priv;
862 GtkStyleContext *context;
868 guint horizontal_padding;
870 GtkPackDirection pack_dir;
871 GtkPackDirection child_pack_dir;
872 gint min_height, nat_height;
875 min_height = nat_height = 0;
877 context = gtk_widget_get_style_context (widget);
878 state = gtk_widget_get_state_flags (widget);
879 gtk_style_context_get_padding (context, state, &padding);
881 gtk_widget_style_get (widget,
882 "horizontal-padding", &horizontal_padding,
885 bin = GTK_BIN (widget);
886 parent = gtk_widget_get_parent (widget);
888 if (GTK_IS_MENU_BAR (parent))
890 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
891 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
895 pack_dir = GTK_PACK_DIRECTION_LTR;
896 child_pack_dir = GTK_PACK_DIRECTION_LTR;
899 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
900 min_height = (border_width * 2) + padding.top + padding.bottom;
902 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
903 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
904 min_height += 2 * horizontal_padding;
908 avail_size = for_size;
909 avail_size -= (border_width * 2) + padding.left + padding.right;
911 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
912 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
913 avail_size -= 2 * horizontal_padding;
916 nat_height = min_height;
918 child = gtk_bin_get_child (bin);
920 if (child != NULL && gtk_widget_get_visible (child))
922 gint child_min, child_nat;
923 gint arrow_size = 0, arrow_spacing = 0;
925 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
926 get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
930 avail_size -= (arrow_size + arrow_spacing);
931 gtk_widget_get_preferred_height_for_width (child,
938 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
941 min_height += child_min;
942 nat_height += child_nat;
944 min_height = MAX (min_height, arrow_size);
945 nat_height = MAX (nat_height, arrow_size);
947 else /* separator item */
949 gboolean wide_separators;
950 gint separator_height;
952 gtk_widget_style_get (widget,
953 "wide-separators", &wide_separators,
954 "separator-height", &separator_height,
959 min_height += separator_height;
960 nat_height += separator_height;
964 /* force odd, so that we can have the same space above and
967 if (min_height % 2 == 0)
969 if (nat_height % 2 == 0)
975 gtk_container_foreach (GTK_CONTAINER (menu_item),
976 gtk_menu_item_accel_width_foreach,
978 priv->accelerator_width = accel_width;
981 *minimum_size = min_height;
984 *natural_size = nat_height;
988 gtk_menu_item_get_preferred_height (GtkWidget *widget,
992 gtk_menu_item_real_get_height (widget, -1, minimum_size, natural_size);
996 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
1001 gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size);
1005 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1007 parent_buildable_iface = g_type_interface_peek_parent (iface);
1008 iface->add_child = gtk_menu_item_buildable_add_child;
1009 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1013 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1014 GtkBuilder *builder,
1018 if (type && strcmp (type, "submenu") == 0)
1019 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1020 GTK_WIDGET (child));
1022 parent_buildable_iface->add_child (buildable, builder, child, type);
1027 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1028 GtkBuilder *builder,
1030 const gchar *tagname,
1033 GtkWidget *toplevel;
1035 if (strcmp (tagname, "accelerator") == 0)
1037 GtkMenuShell *menu_shell;
1040 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1043 while (GTK_IS_MENU (menu_shell) &&
1044 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1045 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1047 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1051 /* Fall back to something ... */
1052 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1054 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1055 gtk_buildable_get_name (buildable));
1058 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1059 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1062 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1067 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1069 iface->update = gtk_menu_item_update;
1070 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1074 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1078 child = gtk_bin_get_child (GTK_BIN (menu_item));
1080 if (GTK_IS_LABEL (child))
1084 label = gtk_action_get_label (action);
1085 gtk_menu_item_set_label (menu_item, label);
1089 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1092 gtk_menu_item_update (GtkActivatable *activatable,
1094 const gchar *property_name)
1096 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1097 GtkMenuItemPrivate *priv = menu_item->priv;
1099 if (strcmp (property_name, "visible") == 0)
1100 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1101 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1102 else if (strcmp (property_name, "sensitive") == 0)
1103 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1104 else if (priv->use_action_appearance)
1106 if (strcmp (property_name, "label") == 0)
1107 activatable_update_label (menu_item, action);
1112 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1115 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1116 GtkMenuItemPrivate *priv = menu_item->priv;
1119 if (!priv->use_action_appearance || !action)
1121 label = gtk_bin_get_child (GTK_BIN (menu_item));
1123 if (GTK_IS_ACCEL_LABEL (label))
1124 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1130 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1131 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1133 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1135 if (priv->use_action_appearance)
1137 label = gtk_bin_get_child (GTK_BIN (menu_item));
1139 /* make sure label is a label, deleting it otherwise */
1140 if (label && !GTK_IS_LABEL (label))
1142 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1145 /* Make sure that menu_item has a label and that any
1146 * accelerators are set */
1147 gtk_menu_item_ensure_label (menu_item);
1148 gtk_menu_item_set_use_underline (menu_item, TRUE);
1149 /* Make label point to the menu_item's label */
1150 label = gtk_bin_get_child (GTK_BIN (menu_item));
1152 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1154 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1155 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1156 gtk_action_get_accel_closure (action));
1159 activatable_update_label (menu_item, action);
1164 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1167 GtkMenuItemPrivate *priv = menu_item->priv;
1169 if (priv->action == action)
1174 gtk_action_disconnect_accelerator (priv->action);
1179 const gchar *accel_path;
1181 accel_path = gtk_action_get_accel_path (action);
1184 gtk_action_connect_accelerator (action);
1185 gtk_menu_item_set_accel_path (menu_item, accel_path);
1189 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1191 priv->action = action;
1195 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1196 gboolean use_appearance)
1198 GtkMenuItemPrivate *priv = menu_item->priv;
1200 if (priv->use_action_appearance != use_appearance)
1202 priv->use_action_appearance = use_appearance;
1204 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1210 * gtk_menu_item_set_submenu:
1211 * @menu_item: a #GtkMenuItem
1212 * @submenu: (allow-none): the submenu, or %NULL
1214 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1215 * submenu is passed.
1218 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1221 GtkMenuItemPrivate *priv = menu_item->priv;
1223 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1224 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1226 if (priv->submenu != submenu)
1229 gtk_menu_detach (GTK_MENU (priv->submenu));
1233 priv->submenu = submenu;
1234 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1235 GTK_WIDGET (menu_item),
1236 gtk_menu_item_detacher);
1239 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1240 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1242 g_object_notify (G_OBJECT (menu_item), "submenu");
1247 * gtk_menu_item_get_submenu:
1248 * @menu_item: a #GtkMenuItem
1250 * Gets the submenu underneath this menu item, if any.
1251 * See gtk_menu_item_set_submenu().
1253 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1256 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1258 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1260 return menu_item->priv->submenu;
1263 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1264 GtkSubmenuPlacement placement);
1267 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1268 GtkSubmenuPlacement placement)
1270 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1272 menu_item->priv->submenu_placement = placement;
1276 gtk_menu_item_select (GtkMenuItem *menu_item)
1280 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1282 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1284 /* Enable themeing of the parent menu item depending on whether
1285 * something is selected in its submenu
1287 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1288 if (GTK_IS_MENU (parent))
1290 GtkMenu *menu = GTK_MENU (parent);
1292 if (menu->priv->parent_menu_item)
1293 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1298 * gtk_menu_item_deselect:
1299 * @menu_item: the menu item
1301 * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1302 * exactly like #gtk_item_deselect.
1305 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1309 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1311 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1313 /* Enable themeing of the parent menu item depending on whether
1314 * something is selected in its submenu
1316 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1317 if (GTK_IS_MENU (parent))
1319 GtkMenu *menu = GTK_MENU (parent);
1321 if (menu->priv->parent_menu_item)
1322 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1327 * gtk_menu_item_activate:
1328 * @menu_item: the menu item
1330 * Emits the #GtkMenuItem::activate signal on the given item
1333 gtk_menu_item_activate (GtkMenuItem *menu_item)
1335 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1337 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1341 * gtk_menu_item_toggle_size_request:
1342 * @menu_item: the menu item
1343 * @requisition: the requisition to use as signal data.
1345 * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1348 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1351 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1353 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1357 * gtk_menu_item_toggle_size_allocate:
1358 * @menu_item: the menu item.
1359 * @allocation: the allocation to use as signal data.
1361 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1364 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1367 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1369 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1373 gtk_menu_item_size_allocate (GtkWidget *widget,
1374 GtkAllocation *allocation)
1376 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1377 GtkMenuItemPrivate *priv = menu_item->priv;
1379 GtkAllocation child_allocation;
1380 GtkTextDirection direction;
1381 GtkPackDirection pack_dir;
1382 GtkPackDirection child_pack_dir;
1386 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1387 g_return_if_fail (allocation != NULL);
1389 bin = GTK_BIN (widget);
1391 direction = gtk_widget_get_direction (widget);
1393 parent = gtk_widget_get_parent (widget);
1394 if (GTK_IS_MENU_BAR (parent))
1396 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1397 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1401 pack_dir = GTK_PACK_DIRECTION_LTR;
1402 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1405 gtk_widget_set_allocation (widget, allocation);
1407 child = gtk_bin_get_child (bin);
1410 GtkStyleContext *context;
1411 GtkStateFlags state;
1413 guint horizontal_padding;
1416 context = gtk_widget_get_style_context (widget);
1417 state = gtk_widget_get_state_flags (widget);
1418 gtk_style_context_get_padding (context, state, &padding);
1420 gtk_widget_style_get (widget,
1421 "horizontal-padding", &horizontal_padding,
1424 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1425 child_allocation.x = border_width + padding.left;
1426 child_allocation.y = border_width + padding.top;
1428 child_allocation.width = allocation->width - (border_width * 2) -
1429 padding.left - padding.right;
1430 child_allocation.height = allocation->height - (border_width * 2) -
1431 padding.top - padding.bottom;
1433 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1434 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1436 child_allocation.x += horizontal_padding;
1437 child_allocation.width -= 2 * horizontal_padding;
1439 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1440 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1442 child_allocation.y += horizontal_padding;
1443 child_allocation.height -= 2 * horizontal_padding;
1446 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1447 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1449 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1450 child_allocation.x += priv->toggle_size;
1451 child_allocation.width -= priv->toggle_size;
1455 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1456 child_allocation.y += priv->toggle_size;
1457 child_allocation.height -= priv->toggle_size;
1460 child_allocation.x += allocation->x;
1461 child_allocation.y += allocation->y;
1463 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1465 gint arrow_spacing, arrow_size;
1467 get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
1469 if (direction == GTK_TEXT_DIR_RTL)
1470 child_allocation.x += arrow_size + arrow_spacing;
1471 child_allocation.width -= arrow_size + arrow_spacing;
1474 if (child_allocation.width < 1)
1475 child_allocation.width = 1;
1477 gtk_widget_size_allocate (child, &child_allocation);
1480 if (gtk_widget_get_realized (widget))
1481 gdk_window_move_resize (priv->event_window,
1482 allocation->x, allocation->y,
1483 allocation->width, allocation->height);
1486 gtk_menu_reposition (GTK_MENU (priv->submenu));
1490 gtk_menu_item_realize (GtkWidget *widget)
1492 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1493 GtkMenuItemPrivate *priv = menu_item->priv;
1494 GtkAllocation allocation;
1496 GdkWindowAttr attributes;
1497 gint attributes_mask;
1499 gtk_widget_set_realized (widget, TRUE);
1501 window = gtk_widget_get_parent_window (widget);
1502 gtk_widget_set_window (widget, window);
1503 g_object_ref (window);
1505 gtk_widget_get_allocation (widget, &allocation);
1507 attributes.x = allocation.x;
1508 attributes.y = allocation.y;
1509 attributes.width = allocation.width;
1510 attributes.height = allocation.height;
1511 attributes.window_type = GDK_WINDOW_CHILD;
1512 attributes.wclass = GDK_INPUT_ONLY;
1513 attributes.event_mask = (gtk_widget_get_events (widget) |
1514 GDK_BUTTON_PRESS_MASK |
1515 GDK_BUTTON_RELEASE_MASK |
1516 GDK_ENTER_NOTIFY_MASK |
1517 GDK_LEAVE_NOTIFY_MASK |
1518 GDK_POINTER_MOTION_MASK);
1520 attributes_mask = GDK_WA_X | GDK_WA_Y;
1522 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1523 &attributes, attributes_mask);
1524 gdk_window_set_user_data (priv->event_window, widget);
1528 gtk_menu_item_unrealize (GtkWidget *widget)
1530 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1531 GtkMenuItemPrivate *priv = menu_item->priv;
1533 gdk_window_set_user_data (priv->event_window, NULL);
1534 gdk_window_destroy (priv->event_window);
1535 priv->event_window = NULL;
1537 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1541 gtk_menu_item_map (GtkWidget *widget)
1543 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1544 GtkMenuItemPrivate *priv = menu_item->priv;
1546 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1548 gdk_window_show (priv->event_window);
1552 gtk_menu_item_unmap (GtkWidget *widget)
1554 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1555 GtkMenuItemPrivate *priv = menu_item->priv;
1557 gdk_window_hide (priv->event_window);
1559 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1563 gtk_menu_item_enter (GtkWidget *widget,
1564 GdkEventCrossing *event)
1566 g_return_val_if_fail (event != NULL, FALSE);
1568 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1572 gtk_menu_item_leave (GtkWidget *widget,
1573 GdkEventCrossing *event)
1575 g_return_val_if_fail (event != NULL, FALSE);
1577 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1581 gtk_menu_item_draw (GtkWidget *widget,
1584 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1585 GtkMenuItemPrivate *priv = menu_item->priv;
1586 GtkStateFlags state;
1587 GtkStyleContext *context;
1589 GtkWidget *child, *parent;
1590 gint x, y, w, h, width, height;
1591 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1593 state = gtk_widget_get_state_flags (widget);
1594 context = gtk_widget_get_style_context (widget);
1595 width = gtk_widget_get_allocated_width (widget);
1596 height = gtk_widget_get_allocated_height (widget);
1600 w = width - border_width * 2;
1601 h = height - border_width * 2;
1603 child = gtk_bin_get_child (GTK_BIN (menu_item));
1604 parent = gtk_widget_get_parent (widget);
1606 gtk_style_context_save (context);
1608 gtk_style_context_get_padding (context, state, &padding);
1610 if (GTK_IS_MENU_BAR (parent))
1611 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1613 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1615 gtk_render_background (context, cr, x, y, w, h);
1616 gtk_render_frame (context, cr, x, y, w, h);
1619 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1621 gint arrow_x, arrow_y;
1623 guint horizontal_padding;
1624 GtkTextDirection direction;
1627 direction = gtk_widget_get_direction (widget);
1629 gtk_widget_style_get (widget,
1630 "horizontal-padding", &horizontal_padding,
1633 get_arrow_size (widget, child, &arrow_size, NULL);
1635 if (direction == GTK_TEXT_DIR_LTR)
1637 arrow_x = x + w - horizontal_padding - arrow_size;
1642 arrow_x = x + horizontal_padding;
1643 angle = (3 * G_PI) / 2;
1646 arrow_y = y + (h - arrow_size) / 2;
1648 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1652 gboolean wide_separators;
1653 gint separator_height;
1654 guint horizontal_padding;
1656 gtk_widget_style_get (widget,
1657 "wide-separators", &wide_separators,
1658 "separator-height", &separator_height,
1659 "horizontal-padding", &horizontal_padding,
1661 if (wide_separators)
1662 gtk_render_frame (context, cr,
1663 x + horizontal_padding + padding.left,
1665 w - (2 * horizontal_padding) - padding.left - padding.right,
1668 gtk_render_line (context, cr,
1669 x + horizontal_padding + padding.left,
1671 x + w - horizontal_padding - padding.right - 1,
1675 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1677 gtk_style_context_restore (context);
1683 * gtk_menu_item_select:
1684 * @menu_item: the menu item
1686 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1687 * exactly like #gtk_item_select.
1690 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1692 GtkMenuItemPrivate *priv = menu_item->priv;
1693 gboolean touchscreen_mode;
1695 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1696 "gtk-touchscreen-mode", &touchscreen_mode,
1699 if (!touchscreen_mode && priv->submenu &&
1700 (!gtk_widget_get_mapped (priv->submenu) ||
1701 GTK_MENU (priv->submenu)->priv->tearoff_active))
1703 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1706 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1707 GTK_STATE_FLAG_PRELIGHT, FALSE);
1708 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1712 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1714 GtkMenuItemPrivate *priv = menu_item->priv;
1717 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1719 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1720 GTK_STATE_FLAG_PRELIGHT);
1721 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1725 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1726 gboolean group_cycling)
1730 parent = gtk_widget_get_parent (widget);
1732 if (GTK_IS_MENU_SHELL (parent))
1733 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1735 if (group_cycling &&
1737 GTK_IS_MENU_SHELL (parent) &&
1738 GTK_MENU_SHELL (parent)->priv->active)
1740 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1743 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1749 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1751 GtkMenuItemPrivate *priv = menu_item->priv;
1754 gtk_action_activate (priv->action);
1759 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1761 GtkMenuItemPrivate *priv = menu_item->priv;
1765 widget = GTK_WIDGET (menu_item);
1766 parent = gtk_widget_get_parent (widget);
1768 if (parent && GTK_IS_MENU_SHELL (parent))
1770 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1772 if (priv->submenu == NULL)
1773 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1776 gtk_menu_shell_select_item (menu_shell, widget);
1777 _gtk_menu_item_popup_submenu (widget, FALSE);
1779 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1785 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1788 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1794 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1797 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1799 menu_item->priv->toggle_size = allocation;
1803 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1808 gtk_menu_item_ensure_label (menu_item);
1810 child = gtk_bin_get_child (GTK_BIN (menu_item));
1811 if (GTK_IS_LABEL (child))
1813 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1815 g_object_notify (G_OBJECT (menu_item), "label");
1819 static const gchar *
1820 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1824 gtk_menu_item_ensure_label (menu_item);
1826 child = gtk_bin_get_child (GTK_BIN (menu_item));
1827 if (GTK_IS_LABEL (child))
1828 return gtk_label_get_label (GTK_LABEL (child));
1834 free_timeval (GTimeVal *val)
1836 g_slice_free (GTimeVal, val);
1840 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1841 gboolean remember_exact_time)
1843 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1844 GtkMenuItemPrivate *priv = menu_item->priv;
1847 parent = gtk_widget_get_parent (widget);
1849 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1851 gboolean take_focus;
1852 GtkMenuPositionFunc menu_position_func;
1854 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1855 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1857 if (remember_exact_time)
1859 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1861 g_get_current_time (popup_time);
1863 g_object_set_data_full (G_OBJECT (priv->submenu),
1864 "gtk-menu-exact-popup-time", popup_time,
1865 (GDestroyNotify) free_timeval);
1869 g_object_set_data (G_OBJECT (priv->submenu),
1870 "gtk-menu-exact-popup-time", NULL);
1873 /* gtk_menu_item_position_menu positions the submenu from the
1874 * menuitems position. If the menuitem doesn't have a window,
1875 * that doesn't work. In that case we use the default
1876 * positioning function instead which places the submenu at the
1879 if (gtk_widget_get_window (widget))
1880 menu_position_func = gtk_menu_item_position_menu;
1882 menu_position_func = NULL;
1884 gtk_menu_popup (GTK_MENU (priv->submenu),
1889 GTK_MENU_SHELL (parent)->priv->button,
1893 /* Enable themeing of the parent menu item depending on whether
1894 * its submenu is shown or not.
1896 gtk_widget_queue_draw (widget);
1900 gtk_menu_item_popup_timeout (gpointer data)
1902 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1903 GtkMenuItemPrivate *priv = menu_item->priv;
1906 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1908 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1909 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1911 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1912 if (priv->timer_from_keypress && priv->submenu)
1913 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1922 get_popup_delay (GtkWidget *widget)
1926 parent = gtk_widget_get_parent (widget);
1927 if (GTK_IS_MENU_SHELL (parent))
1929 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1935 g_object_get (gtk_widget_get_settings (widget),
1936 "gtk-menu-popup-delay", &popup_delay,
1944 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1945 gboolean with_delay)
1947 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1948 GtkMenuItemPrivate *priv = menu_item->priv;
1952 g_source_remove (priv->timer);
1959 gint popup_delay = get_popup_delay (widget);
1961 if (popup_delay > 0)
1963 GdkEvent *event = gtk_get_current_event ();
1965 priv->timer = gdk_threads_add_timeout (popup_delay,
1966 gtk_menu_item_popup_timeout,
1970 event->type != GDK_BUTTON_PRESS &&
1971 event->type != GDK_ENTER_NOTIFY)
1972 priv->timer_from_keypress = TRUE;
1974 priv->timer_from_keypress = FALSE;
1977 gdk_event_free (event);
1983 gtk_menu_item_real_popup_submenu (widget, FALSE);
1987 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1989 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1990 GtkMenuItemPrivate *priv = menu_item->priv;
1994 g_object_set_data (G_OBJECT (priv->submenu),
1995 "gtk-menu-exact-popup-time", NULL);
1999 g_source_remove (priv->timer);
2003 gtk_menu_popdown (GTK_MENU (priv->submenu));
2005 gtk_widget_queue_draw (widget);
2010 get_offsets (GtkMenu *menu,
2011 gint *horizontal_offset,
2012 gint *vertical_offset)
2014 gint vertical_padding;
2015 gint horizontal_padding;
2016 GtkStyleContext *context;
2017 GtkStateFlags state;
2020 gtk_widget_style_get (GTK_WIDGET (menu),
2021 "horizontal-offset", horizontal_offset,
2022 "vertical-offset", vertical_offset,
2023 "horizontal-padding", &horizontal_padding,
2024 "vertical-padding", &vertical_padding,
2027 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2028 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2029 gtk_style_context_get_padding (context, state, &padding);
2031 *vertical_offset -= padding.top;
2032 *vertical_offset -= vertical_padding;
2033 *horizontal_offset += horizontal_padding;
2037 gtk_menu_item_position_menu (GtkMenu *menu,
2043 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2044 GtkMenuItemPrivate *priv = menu_item->priv;
2045 GtkAllocation allocation;
2047 GtkMenuItem *parent_menu_item;
2050 gint twidth, theight;
2052 GtkTextDirection direction;
2053 GdkRectangle monitor;
2055 gint horizontal_offset;
2056 gint vertical_offset;
2057 gint available_left, available_right;
2058 GtkStyleContext *context;
2059 GtkStateFlags state;
2060 GtkBorder parent_padding;
2062 g_return_if_fail (menu != NULL);
2063 g_return_if_fail (x != NULL);
2064 g_return_if_fail (y != NULL);
2066 widget = GTK_WIDGET (user_data);
2071 direction = gtk_widget_get_direction (widget);
2073 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2074 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2076 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2077 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2078 if (monitor_num < 0)
2080 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2082 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2084 g_warning ("Menu not on screen");
2088 gtk_widget_get_allocation (widget, &allocation);
2093 get_offsets (menu, &horizontal_offset, &vertical_offset);
2095 available_left = tx - monitor.x;
2096 available_right = monitor.x + monitor.width - (tx + allocation.width);
2098 parent = gtk_widget_get_parent (widget);
2099 priv->from_menubar = GTK_IS_MENU_BAR (parent);
2101 switch (priv->submenu_placement)
2103 case GTK_TOP_BOTTOM:
2104 if (direction == GTK_TEXT_DIR_LTR)
2105 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2108 priv->submenu_direction = GTK_DIRECTION_LEFT;
2109 tx += allocation.width - twidth;
2111 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2112 ty += allocation.height;
2113 else if ((ty - theight) >= monitor.y)
2115 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2116 ty += allocation.height;
2121 case GTK_LEFT_RIGHT:
2122 if (GTK_IS_MENU (parent))
2123 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2125 parent_menu_item = NULL;
2127 context = gtk_widget_get_style_context (parent);
2128 state = gtk_widget_get_state_flags (parent);
2129 gtk_style_context_get_padding (context, state, &parent_padding);
2131 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2133 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2137 if (direction == GTK_TEXT_DIR_LTR)
2138 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2140 priv->submenu_direction = GTK_DIRECTION_LEFT;
2143 switch (priv->submenu_direction)
2145 case GTK_DIRECTION_LEFT:
2146 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2147 available_left >= available_right)
2148 tx -= twidth + parent_padding.left + horizontal_offset;
2151 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2152 tx += allocation.width + parent_padding.right + horizontal_offset;
2156 case GTK_DIRECTION_RIGHT:
2157 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2158 available_right >= available_left)
2159 tx += allocation.width + parent_padding.right + horizontal_offset;
2162 priv->submenu_direction = GTK_DIRECTION_LEFT;
2163 tx -= twidth + parent_padding.left + horizontal_offset;
2168 ty += vertical_offset;
2170 /* If the height of the menu doesn't fit we move it upward. */
2171 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2175 /* If we have negative, tx, here it is because we can't get
2176 * the menu all the way on screen. Favor the left portion.
2178 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2181 gtk_menu_set_monitor (menu, monitor_num);
2183 if (!gtk_widget_get_visible (menu->priv->toplevel))
2185 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2186 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2191 * gtk_menu_item_set_right_justified:
2192 * @menu_item: a #GtkMenuItem.
2193 * @right_justified: if %TRUE the menu item will appear at the
2194 * far right if added to a menu bar
2196 * Sets whether the menu item appears justified at the right
2197 * side of a menu bar. This was traditionally done for "Help"
2198 * menu items, but is now considered a bad idea. (If the widget
2199 * layout is reversed for a right-to-left language like Hebrew
2200 * or Arabic, right-justified-menu-items appear at the left.)
2202 * Deprecated: 3.2: If you insist on using it, use
2203 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2206 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2207 gboolean right_justified)
2209 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2211 gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2215 * gtk_menu_item_get_right_justified:
2216 * @menu_item: a #GtkMenuItem
2218 * Gets whether the menu item appears justified at the right
2219 * side of the menu bar.
2221 * Return value: %TRUE if the menu item will appear at the
2222 * far right if added to a menu bar.
2224 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2227 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2229 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2231 return menu_item->priv->right_justify;
2236 gtk_menu_item_show_all (GtkWidget *widget)
2238 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2239 GtkMenuItemPrivate *priv = menu_item->priv;
2241 /* show children including submenu */
2243 gtk_widget_show_all (priv->submenu);
2244 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2246 gtk_widget_show (widget);
2250 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2255 parent = gtk_widget_get_parent (widget);
2257 /* Chain to the parent GtkMenu for further checks */
2258 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2259 parent && gtk_widget_can_activate_accel (parent, signal_id));
2263 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2266 const gchar **path_p = data;
2270 if (GTK_IS_LABEL (widget))
2272 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2273 if (*path_p && (*path_p)[0] == 0)
2276 else if (GTK_IS_CONTAINER (widget))
2277 gtk_container_foreach (GTK_CONTAINER (widget),
2278 gtk_menu_item_accel_name_foreach,
2284 gtk_menu_item_parent_set (GtkWidget *widget,
2285 GtkWidget *previous_parent)
2287 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2291 parent = gtk_widget_get_parent (widget);
2292 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2295 _gtk_menu_item_refresh_accel_path (menu_item,
2296 menu->priv->accel_path,
2297 menu->priv->accel_group,
2300 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2301 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2305 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2306 const gchar *prefix,
2307 GtkAccelGroup *accel_group,
2308 gboolean group_changed)
2310 GtkMenuItemPrivate *priv = menu_item->priv;
2314 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2315 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2317 widget = GTK_WIDGET (menu_item);
2321 gtk_widget_set_accel_path (widget, NULL, NULL);
2325 path = _gtk_widget_get_accel_path (widget, NULL);
2326 if (!path) /* no active accel_path yet */
2328 path = priv->accel_path;
2329 if (!path && prefix)
2331 const gchar *postfix = NULL;
2334 /* try to construct one from label text */
2335 gtk_container_foreach (GTK_CONTAINER (menu_item),
2336 gtk_menu_item_accel_name_foreach,
2340 new_path = g_strconcat (prefix, "/", postfix, NULL);
2341 path = priv->accel_path = (char*)g_intern_string (new_path);
2346 gtk_widget_set_accel_path (widget, path, accel_group);
2348 else if (group_changed) /* reinstall accelerators */
2349 gtk_widget_set_accel_path (widget, path, accel_group);
2353 * gtk_menu_item_set_accel_path
2354 * @menu_item: a valid #GtkMenuItem
2355 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2356 * item's functionality, or %NULL to unset the current path.
2358 * Set the accelerator path on @menu_item, through which runtime
2359 * changes of the menu item's accelerator caused by the user can be
2360 * identified and saved to persistent storage (see gtk_accel_map_save()
2361 * on this). To set up a default accelerator for this menu item, call
2362 * gtk_accel_map_add_entry() with the same @accel_path. See also
2363 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2364 * and gtk_menu_set_accel_path() for a more convenient variant of
2367 * This function is basically a convenience wrapper that handles
2368 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2369 * group for the menu item.
2371 * Note that you do need to set an accelerator on the parent menu with
2372 * gtk_menu_set_accel_group() for this to work.
2374 * Note that @accel_path string will be stored in a #GQuark.
2375 * Therefore, if you pass a static string, you can save some memory
2376 * by interning it first with g_intern_static_string().
2379 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2380 const gchar *accel_path)
2382 GtkMenuItemPrivate *priv = menu_item->priv;
2386 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2387 g_return_if_fail (accel_path == NULL ||
2388 (accel_path[0] == '<' && strchr (accel_path, '/')));
2390 widget = GTK_WIDGET (menu_item);
2392 /* store new path */
2393 priv->accel_path = (char*)g_intern_string (accel_path);
2395 /* forget accelerators associated with old path */
2396 gtk_widget_set_accel_path (widget, NULL, NULL);
2398 /* install accelerators associated with new path */
2399 parent = gtk_widget_get_parent (widget);
2400 if (GTK_IS_MENU (parent))
2402 GtkMenu *menu = GTK_MENU (parent);
2404 if (menu->priv->accel_group)
2405 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2407 menu->priv->accel_group,
2413 * gtk_menu_item_get_accel_path
2414 * @menu_item: a valid #GtkMenuItem
2416 * Retrieve the accelerator path that was previously set on @menu_item.
2418 * See gtk_menu_item_set_accel_path() for details.
2420 * Returns: the accelerator path corresponding to this menu
2421 * item's functionality, or %NULL if not set
2426 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2428 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2430 return menu_item->priv->accel_path;
2434 gtk_menu_item_forall (GtkContainer *container,
2435 gboolean include_internals,
2436 GtkCallback callback,
2437 gpointer callback_data)
2441 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2442 g_return_if_fail (callback != NULL);
2444 child = gtk_bin_get_child (GTK_BIN (container));
2446 callback (child, callback_data);
2450 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2452 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2453 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2454 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2455 !gtk_widget_is_sensitive (menu_item) ||
2456 !gtk_widget_get_visible (menu_item))
2463 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2465 GtkWidget *accel_label;
2467 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2469 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2470 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2472 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2473 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2474 GTK_WIDGET (menu_item));
2475 gtk_widget_show (accel_label);
2480 * gtk_menu_item_set_label:
2481 * @menu_item: a #GtkMenuItem
2482 * @label: the text you want to set
2484 * Sets @text on the @menu_item label
2489 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2492 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2494 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2498 * gtk_menu_item_get_label:
2499 * @menu_item: a #GtkMenuItem
2501 * Sets @text on the @menu_item label
2503 * Returns: The text in the @menu_item label. This is the internal
2504 * string used by the label, and must not be modified.
2509 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2511 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2513 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2517 * gtk_menu_item_set_use_underline:
2518 * @menu_item: a #GtkMenuItem
2519 * @setting: %TRUE if underlines in the text indicate mnemonics
2521 * If true, an underline in the text indicates the next character
2522 * should be used for the mnemonic accelerator key.
2527 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2532 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2534 gtk_menu_item_ensure_label (menu_item);
2536 child = gtk_bin_get_child (GTK_BIN (menu_item));
2537 if (GTK_IS_LABEL (child))
2539 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2541 g_object_notify (G_OBJECT (menu_item), "use-underline");
2546 * gtk_menu_item_get_use_underline:
2547 * @menu_item: a #GtkMenuItem
2549 * Checks if an underline in the text indicates the next character
2550 * should be used for the mnemonic accelerator key.
2552 * Return value: %TRUE if an embedded underline in the label
2553 * indicates the mnemonic accelerator key.
2558 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2562 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2564 gtk_menu_item_ensure_label (menu_item);
2566 child = gtk_bin_get_child (GTK_BIN (menu_item));
2567 if (GTK_IS_LABEL (child))
2568 return gtk_label_get_use_underline (GTK_LABEL (child));
2574 * gtk_menu_item_set_reserve_indicator:
2575 * @menu_item: a #GtkMenuItem
2576 * @reserve: the new value
2578 * Sets whether the @menu_item should reserve space for
2579 * the submenu indicator, regardless if it actually has
2582 * There should be little need for applications to call
2588 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2591 GtkMenuItemPrivate *priv;
2593 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2595 priv = menu_item->priv;
2597 if (priv->reserve_indicator != reserve)
2599 priv->reserve_indicator = reserve;
2600 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2605 * gtk_menu_item_get_reserve_indicator:
2606 * @menu_item: a #GtkMenuItem
2608 * Returns whether the @menu_item reserves space for
2609 * the submenu indicator, regardless if it has a submenu
2612 * Returns: %TRUE if @menu_item always reserves space for the
2618 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2620 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2622 return menu_item->priv->reserve_indicator;