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,
679 GtkStyleContext *style_context;
681 PangoContext *context;
682 PangoFontMetrics *metrics;
683 gfloat arrow_scaling;
687 gtk_widget_style_get (widget,
688 "arrow-scaling", &arrow_scaling,
691 context = gtk_widget_get_pango_context (child);
692 style_context = gtk_widget_get_style_context (child);
693 state = gtk_widget_get_state_flags (child);
695 metrics = pango_context_get_metrics (context,
696 gtk_style_context_get_font (style_context, state),
697 pango_context_get_language (context));
699 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
700 pango_font_metrics_get_descent (metrics)));
702 pango_font_metrics_unref (metrics);
704 *size = *size * arrow_scaling;
709 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
714 if (GTK_IS_ACCEL_LABEL (widget))
718 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
719 *width = MAX (*width, w);
721 else if (GTK_IS_CONTAINER (widget))
722 gtk_container_foreach (GTK_CONTAINER (widget),
723 gtk_menu_item_accel_width_foreach,
728 get_minimum_width (GtkWidget *widget)
730 GtkStyleContext *style_context;
732 PangoContext *context;
733 PangoFontMetrics *metrics;
737 context = gtk_widget_get_pango_context (widget);
738 style_context = gtk_widget_get_style_context (widget);
739 state = gtk_widget_get_state_flags (widget);
741 metrics = pango_context_get_metrics (context,
742 gtk_style_context_get_font (style_context, state),
743 pango_context_get_language (context));
745 width = pango_font_metrics_get_approximate_char_width (metrics);
747 pango_font_metrics_unref (metrics);
749 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
751 return PANGO_PIXELS (width_chars * width);
755 gtk_menu_item_get_preferred_width (GtkWidget *widget,
759 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
760 GtkMenuItemPrivate *priv = menu_item->priv;
765 guint horizontal_padding;
767 GtkPackDirection pack_dir;
768 GtkPackDirection child_pack_dir;
769 gint min_width, nat_width;
770 GtkStyleContext *context;
774 min_width = nat_width = 0;
776 gtk_widget_style_get (widget,
777 "horizontal-padding", &horizontal_padding,
780 bin = GTK_BIN (widget);
781 parent = gtk_widget_get_parent (widget);
783 if (GTK_IS_MENU_BAR (parent))
785 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
786 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
790 pack_dir = GTK_PACK_DIRECTION_LTR;
791 child_pack_dir = GTK_PACK_DIRECTION_LTR;
794 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
796 context = gtk_widget_get_style_context (widget);
797 state = gtk_widget_get_state_flags (widget);
798 gtk_style_context_get_padding (context, state, &padding);
800 min_width = (border_width * 2) + padding.left + padding.right;
802 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
803 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
804 min_width += 2 * horizontal_padding;
806 nat_width = min_width;
808 child = gtk_bin_get_child (bin);
810 if (child != NULL && gtk_widget_get_visible (child))
812 GtkMenuItemPrivate *priv = menu_item->priv;
813 gint child_min, child_nat;
815 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
817 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
822 gtk_widget_style_get (widget,
823 "arrow-spacing", &arrow_spacing,
826 get_arrow_size (widget, child, &arrow_size);
828 gtk_widget_style_get (widget,
829 "arrow-spacing", &arrow_spacing,
832 get_arrow_size (widget, child, &arrow_size);
834 min_width += arrow_size;
835 min_width += arrow_spacing;
837 min_width = MAX (min_width, get_minimum_width (widget));
838 nat_width = min_width;
841 min_width += child_min;
842 nat_width += child_nat;
846 gtk_container_foreach (GTK_CONTAINER (menu_item),
847 gtk_menu_item_accel_width_foreach,
849 priv->accelerator_width = accel_width;
852 *minimum_size = min_width;
855 *natural_size = nat_width;
859 gtk_menu_item_real_get_height (GtkWidget *widget,
864 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
865 GtkMenuItemPrivate *priv = menu_item->priv;
867 GtkStyleContext *context;
873 guint horizontal_padding;
875 GtkPackDirection pack_dir;
876 GtkPackDirection child_pack_dir;
877 gint min_height, nat_height;
880 min_height = nat_height = 0;
882 context = gtk_widget_get_style_context (widget);
883 state = gtk_widget_get_state_flags (widget);
884 gtk_style_context_get_padding (context, state, &padding);
886 gtk_widget_style_get (widget,
887 "horizontal-padding", &horizontal_padding,
890 bin = GTK_BIN (widget);
891 parent = gtk_widget_get_parent (widget);
893 if (GTK_IS_MENU_BAR (parent))
895 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
896 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
900 pack_dir = GTK_PACK_DIRECTION_LTR;
901 child_pack_dir = GTK_PACK_DIRECTION_LTR;
904 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
905 min_height = (border_width * 2) + padding.top + padding.bottom;
907 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
908 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
909 min_height += 2 * horizontal_padding;
913 avail_size = for_size;
914 avail_size -= (border_width * 2) + padding.left + padding.right;
916 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
917 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
918 avail_size -= 2 * horizontal_padding;
921 nat_height = min_height;
923 child = gtk_bin_get_child (bin);
925 if (child != NULL && gtk_widget_get_visible (child))
927 gint child_min, child_nat;
929 guint arrow_spacing = 0;
931 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
933 gtk_widget_style_get (widget,
934 "arrow-spacing", &arrow_spacing,
936 get_arrow_size (widget, child, &arrow_size);
941 avail_size -= arrow_size;
942 avail_size -= arrow_spacing;
944 gtk_widget_get_preferred_height_for_width (child,
951 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
954 min_height += child_min;
955 nat_height += child_nat;
957 min_height = MAX (min_height, arrow_size);
958 nat_height = MAX (nat_height, arrow_size);
960 else /* separator item */
962 gboolean wide_separators;
963 gint separator_height;
965 gtk_widget_style_get (widget,
966 "wide-separators", &wide_separators,
967 "separator-height", &separator_height,
972 min_height += separator_height;
973 nat_height += separator_height;
977 /* force odd, so that we can have the same space above and
980 if (min_height % 2 == 0)
982 if (nat_height % 2 == 0)
988 gtk_container_foreach (GTK_CONTAINER (menu_item),
989 gtk_menu_item_accel_width_foreach,
991 priv->accelerator_width = accel_width;
994 *minimum_size = min_height;
997 *natural_size = nat_height;
1001 gtk_menu_item_get_preferred_height (GtkWidget *widget,
1005 gtk_menu_item_real_get_height (widget, -1, minimum_size, natural_size);
1009 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
1014 gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size);
1018 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1020 parent_buildable_iface = g_type_interface_peek_parent (iface);
1021 iface->add_child = gtk_menu_item_buildable_add_child;
1022 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1026 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1027 GtkBuilder *builder,
1031 if (type && strcmp (type, "submenu") == 0)
1032 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1033 GTK_WIDGET (child));
1035 parent_buildable_iface->add_child (buildable, builder, child, type);
1040 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1041 GtkBuilder *builder,
1043 const gchar *tagname,
1046 GtkWidget *toplevel;
1048 if (strcmp (tagname, "accelerator") == 0)
1050 GtkMenuShell *menu_shell;
1053 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1056 while (GTK_IS_MENU (menu_shell) &&
1057 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1058 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1060 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1064 /* Fall back to something ... */
1065 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1067 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1068 gtk_buildable_get_name (buildable));
1071 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1072 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1075 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1080 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1082 iface->update = gtk_menu_item_update;
1083 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1087 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1091 child = gtk_bin_get_child (GTK_BIN (menu_item));
1093 if (GTK_IS_LABEL (child))
1097 label = gtk_action_get_label (action);
1098 gtk_menu_item_set_label (menu_item, label);
1102 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1105 gtk_menu_item_update (GtkActivatable *activatable,
1107 const gchar *property_name)
1109 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1110 GtkMenuItemPrivate *priv = menu_item->priv;
1112 if (strcmp (property_name, "visible") == 0)
1113 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1114 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1115 else if (strcmp (property_name, "sensitive") == 0)
1116 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1117 else if (priv->use_action_appearance)
1119 if (strcmp (property_name, "label") == 0)
1120 activatable_update_label (menu_item, action);
1125 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1128 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1129 GtkMenuItemPrivate *priv = menu_item->priv;
1132 if (!priv->use_action_appearance || !action)
1134 label = gtk_bin_get_child (GTK_BIN (menu_item));
1136 if (GTK_IS_ACCEL_LABEL (label))
1137 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1143 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1144 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1146 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1148 if (priv->use_action_appearance)
1150 label = gtk_bin_get_child (GTK_BIN (menu_item));
1152 /* make sure label is a label, deleting it otherwise */
1153 if (label && !GTK_IS_LABEL (label))
1155 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1158 /* Make sure that menu_item has a label and that any
1159 * accelerators are set */
1160 gtk_menu_item_ensure_label (menu_item);
1161 gtk_menu_item_set_use_underline (menu_item, TRUE);
1162 /* Make label point to the menu_item's label */
1163 label = gtk_bin_get_child (GTK_BIN (menu_item));
1165 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1167 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1168 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1169 gtk_action_get_accel_closure (action));
1172 activatable_update_label (menu_item, action);
1177 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1180 GtkMenuItemPrivate *priv = menu_item->priv;
1182 if (priv->action == action)
1187 gtk_action_disconnect_accelerator (priv->action);
1192 const gchar *accel_path;
1194 accel_path = gtk_action_get_accel_path (action);
1197 gtk_action_connect_accelerator (action);
1198 gtk_menu_item_set_accel_path (menu_item, accel_path);
1202 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1204 priv->action = action;
1208 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1209 gboolean use_appearance)
1211 GtkMenuItemPrivate *priv = menu_item->priv;
1213 if (priv->use_action_appearance != use_appearance)
1215 priv->use_action_appearance = use_appearance;
1217 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1223 * gtk_menu_item_set_submenu:
1224 * @menu_item: a #GtkMenuItem
1225 * @submenu: (allow-none): the submenu, or %NULL
1227 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1228 * submenu is passed.
1231 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1234 GtkMenuItemPrivate *priv = menu_item->priv;
1236 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1237 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1239 if (priv->submenu != submenu)
1242 gtk_menu_detach (GTK_MENU (priv->submenu));
1246 priv->submenu = submenu;
1247 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1248 GTK_WIDGET (menu_item),
1249 gtk_menu_item_detacher);
1252 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1253 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1255 g_object_notify (G_OBJECT (menu_item), "submenu");
1260 * gtk_menu_item_get_submenu:
1261 * @menu_item: a #GtkMenuItem
1263 * Gets the submenu underneath this menu item, if any.
1264 * See gtk_menu_item_set_submenu().
1266 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1269 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1271 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1273 return menu_item->priv->submenu;
1276 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1277 GtkSubmenuPlacement placement);
1280 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1281 GtkSubmenuPlacement placement)
1283 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1285 menu_item->priv->submenu_placement = placement;
1289 gtk_menu_item_select (GtkMenuItem *menu_item)
1293 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1295 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1297 /* Enable themeing of the parent menu item depending on whether
1298 * something is selected in its submenu
1300 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1301 if (GTK_IS_MENU (parent))
1303 GtkMenu *menu = GTK_MENU (parent);
1305 if (menu->priv->parent_menu_item)
1306 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1311 * gtk_menu_item_deselect:
1312 * @menu_item: the menu item
1314 * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1315 * exactly like #gtk_item_deselect.
1318 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1322 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1324 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1326 /* Enable themeing of the parent menu item depending on whether
1327 * something is selected in its submenu
1329 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1330 if (GTK_IS_MENU (parent))
1332 GtkMenu *menu = GTK_MENU (parent);
1334 if (menu->priv->parent_menu_item)
1335 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1340 * gtk_menu_item_activate:
1341 * @menu_item: the menu item
1343 * Emits the #GtkMenuItem::activate signal on the given item
1346 gtk_menu_item_activate (GtkMenuItem *menu_item)
1348 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1350 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1354 * gtk_menu_item_toggle_size_request:
1355 * @menu_item: the menu item
1356 * @requisition: the requisition to use as signal data.
1358 * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1361 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1364 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1366 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1370 * gtk_menu_item_toggle_size_allocate:
1371 * @menu_item: the menu item.
1372 * @allocation: the allocation to use as signal data.
1374 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1377 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1380 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1382 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1386 gtk_menu_item_size_allocate (GtkWidget *widget,
1387 GtkAllocation *allocation)
1389 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1390 GtkMenuItemPrivate *priv = menu_item->priv;
1392 GtkAllocation child_allocation;
1393 GtkTextDirection direction;
1394 GtkPackDirection pack_dir;
1395 GtkPackDirection child_pack_dir;
1399 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1400 g_return_if_fail (allocation != NULL);
1402 bin = GTK_BIN (widget);
1404 direction = gtk_widget_get_direction (widget);
1406 parent = gtk_widget_get_parent (widget);
1407 if (GTK_IS_MENU_BAR (parent))
1409 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1410 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1414 pack_dir = GTK_PACK_DIRECTION_LTR;
1415 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1418 gtk_widget_set_allocation (widget, allocation);
1420 child = gtk_bin_get_child (bin);
1423 GtkStyleContext *context;
1424 GtkStateFlags state;
1426 guint horizontal_padding;
1429 context = gtk_widget_get_style_context (widget);
1430 state = gtk_widget_get_state_flags (widget);
1431 gtk_style_context_get_padding (context, state, &padding);
1433 gtk_widget_style_get (widget,
1434 "horizontal-padding", &horizontal_padding,
1437 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1438 child_allocation.x = border_width + padding.left;
1439 child_allocation.y = border_width + padding.top;
1441 child_allocation.width = allocation->width - (border_width * 2) -
1442 padding.left - padding.right;
1443 child_allocation.height = allocation->height - (border_width * 2) -
1444 padding.top - padding.bottom;
1446 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1447 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1449 child_allocation.x += horizontal_padding;
1450 child_allocation.width -= 2 * horizontal_padding;
1452 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1453 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1455 child_allocation.y += horizontal_padding;
1456 child_allocation.height -= 2 * horizontal_padding;
1459 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1460 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1462 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1463 child_allocation.x += priv->toggle_size;
1464 child_allocation.width -= priv->toggle_size;
1468 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1469 child_allocation.y += priv->toggle_size;
1470 child_allocation.height -= priv->toggle_size;
1473 child_allocation.x += allocation->x;
1474 child_allocation.y += allocation->y;
1476 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1478 guint arrow_spacing;
1481 gtk_widget_style_get (widget,
1482 "arrow-spacing", &arrow_spacing,
1485 get_arrow_size (widget, child, &arrow_size);
1487 if (direction == GTK_TEXT_DIR_RTL)
1488 child_allocation.x += arrow_size + arrow_spacing;
1489 child_allocation.width -= arrow_size + arrow_spacing;
1492 if (child_allocation.width < 1)
1493 child_allocation.width = 1;
1495 gtk_widget_size_allocate (child, &child_allocation);
1498 if (gtk_widget_get_realized (widget))
1499 gdk_window_move_resize (priv->event_window,
1500 allocation->x, allocation->y,
1501 allocation->width, allocation->height);
1504 gtk_menu_reposition (GTK_MENU (priv->submenu));
1508 gtk_menu_item_realize (GtkWidget *widget)
1510 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1511 GtkMenuItemPrivate *priv = menu_item->priv;
1512 GtkAllocation allocation;
1514 GdkWindowAttr attributes;
1515 gint attributes_mask;
1517 gtk_widget_set_realized (widget, TRUE);
1519 window = gtk_widget_get_parent_window (widget);
1520 gtk_widget_set_window (widget, window);
1521 g_object_ref (window);
1523 gtk_widget_get_allocation (widget, &allocation);
1525 attributes.x = allocation.x;
1526 attributes.y = allocation.y;
1527 attributes.width = allocation.width;
1528 attributes.height = allocation.height;
1529 attributes.window_type = GDK_WINDOW_CHILD;
1530 attributes.wclass = GDK_INPUT_ONLY;
1531 attributes.event_mask = (gtk_widget_get_events (widget) |
1532 GDK_BUTTON_PRESS_MASK |
1533 GDK_BUTTON_RELEASE_MASK |
1534 GDK_ENTER_NOTIFY_MASK |
1535 GDK_LEAVE_NOTIFY_MASK |
1536 GDK_POINTER_MOTION_MASK);
1538 attributes_mask = GDK_WA_X | GDK_WA_Y;
1540 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1541 &attributes, attributes_mask);
1542 gdk_window_set_user_data (priv->event_window, widget);
1546 gtk_menu_item_unrealize (GtkWidget *widget)
1548 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1549 GtkMenuItemPrivate *priv = menu_item->priv;
1551 gdk_window_set_user_data (priv->event_window, NULL);
1552 gdk_window_destroy (priv->event_window);
1553 priv->event_window = NULL;
1555 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1559 gtk_menu_item_map (GtkWidget *widget)
1561 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1562 GtkMenuItemPrivate *priv = menu_item->priv;
1564 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1566 gdk_window_show (priv->event_window);
1570 gtk_menu_item_unmap (GtkWidget *widget)
1572 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1573 GtkMenuItemPrivate *priv = menu_item->priv;
1575 gdk_window_hide (priv->event_window);
1577 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1581 gtk_menu_item_enter (GtkWidget *widget,
1582 GdkEventCrossing *event)
1584 g_return_val_if_fail (event != NULL, FALSE);
1586 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1590 gtk_menu_item_leave (GtkWidget *widget,
1591 GdkEventCrossing *event)
1593 g_return_val_if_fail (event != NULL, FALSE);
1595 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1599 gtk_menu_item_draw (GtkWidget *widget,
1602 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1603 GtkMenuItemPrivate *priv = menu_item->priv;
1604 GtkStateFlags state;
1605 GtkStyleContext *context;
1607 GtkWidget *child, *parent;
1608 gint x, y, w, h, width, height;
1609 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1611 state = gtk_widget_get_state_flags (widget);
1612 context = gtk_widget_get_style_context (widget);
1613 width = gtk_widget_get_allocated_width (widget);
1614 height = gtk_widget_get_allocated_height (widget);
1618 w = width - border_width * 2;
1619 h = height - border_width * 2;
1621 child = gtk_bin_get_child (GTK_BIN (menu_item));
1622 parent = gtk_widget_get_parent (widget);
1624 gtk_style_context_save (context);
1625 gtk_style_context_set_state (context, state);
1627 gtk_style_context_get_padding (context, state, &padding);
1629 if (GTK_IS_MENU_BAR (parent))
1630 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1632 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1634 gtk_render_background (context, cr, x, y, w, h);
1635 gtk_render_frame (context, cr, x, y, w, h);
1638 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1640 gint arrow_x, arrow_y;
1642 guint horizontal_padding;
1643 GtkTextDirection direction;
1646 direction = gtk_widget_get_direction (widget);
1648 gtk_widget_style_get (widget,
1649 "horizontal-padding", &horizontal_padding,
1652 get_arrow_size (widget, child, &arrow_size);
1654 if (direction == GTK_TEXT_DIR_LTR)
1656 arrow_x = x + w - horizontal_padding - arrow_size;
1661 arrow_x = x + horizontal_padding;
1662 angle = (3 * G_PI) / 2;
1665 arrow_y = y + (h - arrow_size) / 2;
1667 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1671 gboolean wide_separators;
1672 gint separator_height;
1673 guint horizontal_padding;
1675 gtk_widget_style_get (widget,
1676 "wide-separators", &wide_separators,
1677 "separator-height", &separator_height,
1678 "horizontal-padding", &horizontal_padding,
1680 if (wide_separators)
1681 gtk_render_frame (context, cr,
1682 x + horizontal_padding + padding.left,
1684 w - (2 * horizontal_padding) - padding.left - padding.right,
1687 gtk_render_line (context, cr,
1688 x + horizontal_padding + padding.left,
1690 x + w - horizontal_padding - padding.right - 1,
1694 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1696 gtk_style_context_restore (context);
1702 * gtk_menu_item_select:
1703 * @menu_item: the menu item
1705 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1706 * exactly like #gtk_item_select.
1709 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1711 GtkMenuItemPrivate *priv = menu_item->priv;
1712 gboolean touchscreen_mode;
1714 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1715 "gtk-touchscreen-mode", &touchscreen_mode,
1718 if (!touchscreen_mode && priv->submenu &&
1719 (!gtk_widget_get_mapped (priv->submenu) ||
1720 GTK_MENU (priv->submenu)->priv->tearoff_active))
1722 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1725 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1726 GTK_STATE_FLAG_PRELIGHT, FALSE);
1727 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1731 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1733 GtkMenuItemPrivate *priv = menu_item->priv;
1736 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1738 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1739 GTK_STATE_FLAG_PRELIGHT);
1740 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1744 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1745 gboolean group_cycling)
1749 parent = gtk_widget_get_parent (widget);
1751 if (GTK_IS_MENU_SHELL (parent))
1752 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1754 if (group_cycling &&
1756 GTK_IS_MENU_SHELL (parent) &&
1757 GTK_MENU_SHELL (parent)->priv->active)
1759 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1762 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1768 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1770 GtkMenuItemPrivate *priv = menu_item->priv;
1773 gtk_action_activate (priv->action);
1778 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1780 GtkMenuItemPrivate *priv = menu_item->priv;
1784 widget = GTK_WIDGET (menu_item);
1785 parent = gtk_widget_get_parent (widget);
1787 if (parent && GTK_IS_MENU_SHELL (parent))
1789 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1791 if (priv->submenu == NULL)
1792 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1795 gtk_menu_shell_select_item (menu_shell, widget);
1796 _gtk_menu_item_popup_submenu (widget, FALSE);
1798 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1804 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1807 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1813 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1816 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1818 menu_item->priv->toggle_size = allocation;
1822 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1827 gtk_menu_item_ensure_label (menu_item);
1829 child = gtk_bin_get_child (GTK_BIN (menu_item));
1830 if (GTK_IS_LABEL (child))
1832 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1834 g_object_notify (G_OBJECT (menu_item), "label");
1838 static const gchar *
1839 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1843 gtk_menu_item_ensure_label (menu_item);
1845 child = gtk_bin_get_child (GTK_BIN (menu_item));
1846 if (GTK_IS_LABEL (child))
1847 return gtk_label_get_label (GTK_LABEL (child));
1853 free_timeval (GTimeVal *val)
1855 g_slice_free (GTimeVal, val);
1859 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1860 gboolean remember_exact_time)
1862 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1863 GtkMenuItemPrivate *priv = menu_item->priv;
1866 parent = gtk_widget_get_parent (widget);
1868 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1870 gboolean take_focus;
1871 GtkMenuPositionFunc menu_position_func;
1873 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1874 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1876 if (remember_exact_time)
1878 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1880 g_get_current_time (popup_time);
1882 g_object_set_data_full (G_OBJECT (priv->submenu),
1883 "gtk-menu-exact-popup-time", popup_time,
1884 (GDestroyNotify) free_timeval);
1888 g_object_set_data (G_OBJECT (priv->submenu),
1889 "gtk-menu-exact-popup-time", NULL);
1892 /* gtk_menu_item_position_menu positions the submenu from the
1893 * menuitems position. If the menuitem doesn't have a window,
1894 * that doesn't work. In that case we use the default
1895 * positioning function instead which places the submenu at the
1898 if (gtk_widget_get_window (widget))
1899 menu_position_func = gtk_menu_item_position_menu;
1901 menu_position_func = NULL;
1903 gtk_menu_popup (GTK_MENU (priv->submenu),
1908 GTK_MENU_SHELL (parent)->priv->button,
1912 /* Enable themeing of the parent menu item depending on whether
1913 * its submenu is shown or not.
1915 gtk_widget_queue_draw (widget);
1919 gtk_menu_item_popup_timeout (gpointer data)
1921 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1922 GtkMenuItemPrivate *priv = menu_item->priv;
1925 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1927 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1928 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1930 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1931 if (priv->timer_from_keypress && priv->submenu)
1932 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1941 get_popup_delay (GtkWidget *widget)
1945 parent = gtk_widget_get_parent (widget);
1946 if (GTK_IS_MENU_SHELL (parent))
1948 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1954 g_object_get (gtk_widget_get_settings (widget),
1955 "gtk-menu-popup-delay", &popup_delay,
1963 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1964 gboolean with_delay)
1966 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1967 GtkMenuItemPrivate *priv = menu_item->priv;
1971 g_source_remove (priv->timer);
1978 gint popup_delay = get_popup_delay (widget);
1980 if (popup_delay > 0)
1982 GdkEvent *event = gtk_get_current_event ();
1984 priv->timer = gdk_threads_add_timeout (popup_delay,
1985 gtk_menu_item_popup_timeout,
1989 event->type != GDK_BUTTON_PRESS &&
1990 event->type != GDK_ENTER_NOTIFY)
1991 priv->timer_from_keypress = TRUE;
1993 priv->timer_from_keypress = FALSE;
1996 gdk_event_free (event);
2002 gtk_menu_item_real_popup_submenu (widget, FALSE);
2006 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
2008 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2009 GtkMenuItemPrivate *priv = menu_item->priv;
2013 g_object_set_data (G_OBJECT (priv->submenu),
2014 "gtk-menu-exact-popup-time", NULL);
2018 g_source_remove (priv->timer);
2022 gtk_menu_popdown (GTK_MENU (priv->submenu));
2024 gtk_widget_queue_draw (widget);
2029 get_offsets (GtkMenu *menu,
2030 gint *horizontal_offset,
2031 gint *vertical_offset)
2033 gint vertical_padding;
2034 gint horizontal_padding;
2035 GtkStyleContext *context;
2036 GtkStateFlags state;
2039 gtk_widget_style_get (GTK_WIDGET (menu),
2040 "horizontal-offset", horizontal_offset,
2041 "vertical-offset", vertical_offset,
2042 "horizontal-padding", &horizontal_padding,
2043 "vertical-padding", &vertical_padding,
2046 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2047 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2048 gtk_style_context_get_padding (context, state, &padding);
2050 *vertical_offset -= padding.top;
2051 *vertical_offset -= vertical_padding;
2052 *horizontal_offset += horizontal_padding;
2056 gtk_menu_item_position_menu (GtkMenu *menu,
2062 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2063 GtkMenuItemPrivate *priv = menu_item->priv;
2064 GtkAllocation allocation;
2066 GtkMenuItem *parent_menu_item;
2069 gint twidth, theight;
2071 GtkTextDirection direction;
2072 GdkRectangle monitor;
2074 gint horizontal_offset;
2075 gint vertical_offset;
2076 gint available_left, available_right;
2077 GtkStyleContext *context;
2078 GtkStateFlags state;
2079 GtkBorder parent_padding;
2081 g_return_if_fail (menu != NULL);
2082 g_return_if_fail (x != NULL);
2083 g_return_if_fail (y != NULL);
2085 widget = GTK_WIDGET (user_data);
2090 direction = gtk_widget_get_direction (widget);
2092 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2093 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2095 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2096 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2097 if (monitor_num < 0)
2099 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2101 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2103 g_warning ("Menu not on screen");
2107 gtk_widget_get_allocation (widget, &allocation);
2112 get_offsets (menu, &horizontal_offset, &vertical_offset);
2114 available_left = tx - monitor.x;
2115 available_right = monitor.x + monitor.width - (tx + allocation.width);
2117 parent = gtk_widget_get_parent (widget);
2118 priv->from_menubar = GTK_IS_MENU_BAR (parent);
2120 switch (priv->submenu_placement)
2122 case GTK_TOP_BOTTOM:
2123 if (direction == GTK_TEXT_DIR_LTR)
2124 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2127 priv->submenu_direction = GTK_DIRECTION_LEFT;
2128 tx += allocation.width - twidth;
2130 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2131 ty += allocation.height;
2132 else if ((ty - theight) >= monitor.y)
2134 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2135 ty += allocation.height;
2140 case GTK_LEFT_RIGHT:
2141 if (GTK_IS_MENU (parent))
2142 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2144 parent_menu_item = NULL;
2146 context = gtk_widget_get_style_context (parent);
2147 state = gtk_widget_get_state_flags (parent);
2148 gtk_style_context_get_padding (context, state, &parent_padding);
2150 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2152 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2156 if (direction == GTK_TEXT_DIR_LTR)
2157 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2159 priv->submenu_direction = GTK_DIRECTION_LEFT;
2162 switch (priv->submenu_direction)
2164 case GTK_DIRECTION_LEFT:
2165 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2166 available_left >= available_right)
2167 tx -= twidth + parent_padding.left + horizontal_offset;
2170 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2171 tx += allocation.width + parent_padding.right + horizontal_offset;
2175 case GTK_DIRECTION_RIGHT:
2176 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2177 available_right >= available_left)
2178 tx += allocation.width + parent_padding.right + horizontal_offset;
2181 priv->submenu_direction = GTK_DIRECTION_LEFT;
2182 tx -= twidth + parent_padding.left + horizontal_offset;
2187 ty += vertical_offset;
2189 /* If the height of the menu doesn't fit we move it upward. */
2190 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2194 /* If we have negative, tx, here it is because we can't get
2195 * the menu all the way on screen. Favor the left portion.
2197 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2200 gtk_menu_set_monitor (menu, monitor_num);
2202 if (!gtk_widget_get_visible (menu->priv->toplevel))
2204 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2205 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2210 * gtk_menu_item_set_right_justified:
2211 * @menu_item: a #GtkMenuItem.
2212 * @right_justified: if %TRUE the menu item will appear at the
2213 * far right if added to a menu bar
2215 * Sets whether the menu item appears justified at the right
2216 * side of a menu bar. This was traditionally done for "Help"
2217 * menu items, but is now considered a bad idea. (If the widget
2218 * layout is reversed for a right-to-left language like Hebrew
2219 * or Arabic, right-justified-menu-items appear at the left.)
2221 * Deprecated: 3.2: If you insist on using it, use
2222 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2225 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2226 gboolean right_justified)
2228 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2230 gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2234 * gtk_menu_item_get_right_justified:
2235 * @menu_item: a #GtkMenuItem
2237 * Gets whether the menu item appears justified at the right
2238 * side of the menu bar.
2240 * Return value: %TRUE if the menu item will appear at the
2241 * far right if added to a menu bar.
2243 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2246 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2248 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2250 return menu_item->priv->right_justify;
2255 gtk_menu_item_show_all (GtkWidget *widget)
2257 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2258 GtkMenuItemPrivate *priv = menu_item->priv;
2260 /* show children including submenu */
2262 gtk_widget_show_all (priv->submenu);
2263 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2265 gtk_widget_show (widget);
2269 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2274 parent = gtk_widget_get_parent (widget);
2276 /* Chain to the parent GtkMenu for further checks */
2277 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2278 parent && gtk_widget_can_activate_accel (parent, signal_id));
2282 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2285 const gchar **path_p = data;
2289 if (GTK_IS_LABEL (widget))
2291 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2292 if (*path_p && (*path_p)[0] == 0)
2295 else if (GTK_IS_CONTAINER (widget))
2296 gtk_container_foreach (GTK_CONTAINER (widget),
2297 gtk_menu_item_accel_name_foreach,
2303 gtk_menu_item_parent_set (GtkWidget *widget,
2304 GtkWidget *previous_parent)
2306 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2310 parent = gtk_widget_get_parent (widget);
2311 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2314 _gtk_menu_item_refresh_accel_path (menu_item,
2315 menu->priv->accel_path,
2316 menu->priv->accel_group,
2319 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2320 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2324 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2325 const gchar *prefix,
2326 GtkAccelGroup *accel_group,
2327 gboolean group_changed)
2329 GtkMenuItemPrivate *priv = menu_item->priv;
2333 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2334 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2336 widget = GTK_WIDGET (menu_item);
2340 gtk_widget_set_accel_path (widget, NULL, NULL);
2344 path = _gtk_widget_get_accel_path (widget, NULL);
2345 if (!path) /* no active accel_path yet */
2347 path = priv->accel_path;
2348 if (!path && prefix)
2350 const gchar *postfix = NULL;
2353 /* try to construct one from label text */
2354 gtk_container_foreach (GTK_CONTAINER (menu_item),
2355 gtk_menu_item_accel_name_foreach,
2359 new_path = g_strconcat (prefix, "/", postfix, NULL);
2360 path = priv->accel_path = (char*)g_intern_string (new_path);
2365 gtk_widget_set_accel_path (widget, path, accel_group);
2367 else if (group_changed) /* reinstall accelerators */
2368 gtk_widget_set_accel_path (widget, path, accel_group);
2372 * gtk_menu_item_set_accel_path
2373 * @menu_item: a valid #GtkMenuItem
2374 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2375 * item's functionality, or %NULL to unset the current path.
2377 * Set the accelerator path on @menu_item, through which runtime
2378 * changes of the menu item's accelerator caused by the user can be
2379 * identified and saved to persistent storage (see gtk_accel_map_save()
2380 * on this). To set up a default accelerator for this menu item, call
2381 * gtk_accel_map_add_entry() with the same @accel_path. See also
2382 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2383 * and gtk_menu_set_accel_path() for a more convenient variant of
2386 * This function is basically a convenience wrapper that handles
2387 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2388 * group for the menu item.
2390 * Note that you do need to set an accelerator on the parent menu with
2391 * gtk_menu_set_accel_group() for this to work.
2393 * Note that @accel_path string will be stored in a #GQuark.
2394 * Therefore, if you pass a static string, you can save some memory
2395 * by interning it first with g_intern_static_string().
2398 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2399 const gchar *accel_path)
2401 GtkMenuItemPrivate *priv = menu_item->priv;
2405 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2406 g_return_if_fail (accel_path == NULL ||
2407 (accel_path[0] == '<' && strchr (accel_path, '/')));
2409 widget = GTK_WIDGET (menu_item);
2411 /* store new path */
2412 priv->accel_path = (char*)g_intern_string (accel_path);
2414 /* forget accelerators associated with old path */
2415 gtk_widget_set_accel_path (widget, NULL, NULL);
2417 /* install accelerators associated with new path */
2418 parent = gtk_widget_get_parent (widget);
2419 if (GTK_IS_MENU (parent))
2421 GtkMenu *menu = GTK_MENU (parent);
2423 if (menu->priv->accel_group)
2424 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2426 menu->priv->accel_group,
2432 * gtk_menu_item_get_accel_path
2433 * @menu_item: a valid #GtkMenuItem
2435 * Retrieve the accelerator path that was previously set on @menu_item.
2437 * See gtk_menu_item_set_accel_path() for details.
2439 * Returns: the accelerator path corresponding to this menu
2440 * item's functionality, or %NULL if not set
2445 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2447 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2449 return menu_item->priv->accel_path;
2453 gtk_menu_item_forall (GtkContainer *container,
2454 gboolean include_internals,
2455 GtkCallback callback,
2456 gpointer callback_data)
2460 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2461 g_return_if_fail (callback != NULL);
2463 child = gtk_bin_get_child (GTK_BIN (container));
2465 callback (child, callback_data);
2469 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2471 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2472 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2473 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2474 !gtk_widget_is_sensitive (menu_item) ||
2475 !gtk_widget_get_visible (menu_item))
2482 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2484 GtkWidget *accel_label;
2486 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2488 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2489 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2491 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2492 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2493 GTK_WIDGET (menu_item));
2494 gtk_widget_show (accel_label);
2499 * gtk_menu_item_set_label:
2500 * @menu_item: a #GtkMenuItem
2501 * @label: the text you want to set
2503 * Sets @text on the @menu_item label
2508 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2511 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2513 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2517 * gtk_menu_item_get_label:
2518 * @menu_item: a #GtkMenuItem
2520 * Sets @text on the @menu_item label
2522 * Returns: The text in the @menu_item label. This is the internal
2523 * string used by the label, and must not be modified.
2528 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2530 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2532 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2536 * gtk_menu_item_set_use_underline:
2537 * @menu_item: a #GtkMenuItem
2538 * @setting: %TRUE if underlines in the text indicate mnemonics
2540 * If true, an underline in the text indicates the next character
2541 * should be used for the mnemonic accelerator key.
2546 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2551 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2553 gtk_menu_item_ensure_label (menu_item);
2555 child = gtk_bin_get_child (GTK_BIN (menu_item));
2556 if (GTK_IS_LABEL (child))
2558 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2560 g_object_notify (G_OBJECT (menu_item), "use-underline");
2565 * gtk_menu_item_get_use_underline:
2566 * @menu_item: a #GtkMenuItem
2568 * Checks if an underline in the text indicates the next character
2569 * should be used for the mnemonic accelerator key.
2571 * Return value: %TRUE if an embedded underline in the label
2572 * indicates the mnemonic accelerator key.
2577 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2581 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2583 gtk_menu_item_ensure_label (menu_item);
2585 child = gtk_bin_get_child (GTK_BIN (menu_item));
2586 if (GTK_IS_LABEL (child))
2587 return gtk_label_get_use_underline (GTK_LABEL (child));
2593 * gtk_menu_item_set_reserve_indicator:
2594 * @menu_item: a #GtkMenuItem
2595 * @reserve: the new value
2597 * Sets whether the @menu_item should reserve space for
2598 * the submenu indicator, regardless if it actually has
2601 * There should be little need for applications to call
2607 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2610 GtkMenuItemPrivate *priv;
2612 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2614 priv = menu_item->priv;
2616 if (priv->reserve_indicator != reserve)
2618 priv->reserve_indicator = reserve;
2619 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2624 * gtk_menu_item_get_reserve_indicator:
2625 * @menu_item: a #GtkMenuItem
2627 * Returns whether the @menu_item reserves space for
2628 * the submenu indicator, regardless if it has a submenu
2631 * Returns: %TRUE if @menu_item always reserves space for the
2637 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2639 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2641 return menu_item->priv->reserve_indicator;