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"
50 * @Short_description: The widget used for item in menus
52 * @See_also: #GtkBin, #GtkItem, #GtkMenuShell
54 * The #GtkMenuItem widget and the derived widgets are the only valid
55 * childs for menus. Their function is to correctly handle highlighting,
56 * alignment, events and submenus.
58 * As it derives from #GtkBin it can hold any valid child widget, altough
59 * only a few are really useful.
61 * <refsect2 id="GtkMenuItem-BUILDER-UI">
62 * <title>GtkMenuItem as GtkBuildable</title>
63 * The GtkMenuItem implementation of the GtkBuildable interface
64 * supports adding a submenu by specifying "submenu" as the "type"
65 * attribute of a <child> element.
67 * <title>A UI definition fragment with submenus</title>
68 * <programlisting><![CDATA[
69 * <object class="GtkMenuItem">
70 * <child type="submenu">
71 * <object class="GtkMenu"/>
74 * ]]></programlisting>
98 /* activatable properties */
99 PROP_ACTIVATABLE_RELATED_ACTION,
100 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
104 static void gtk_menu_item_dispose (GObject *object);
105 static void gtk_menu_item_set_property (GObject *object,
109 static void gtk_menu_item_get_property (GObject *object,
113 static void gtk_menu_item_destroy (GtkWidget *widget);
114 static void gtk_menu_item_size_allocate (GtkWidget *widget,
115 GtkAllocation *allocation);
116 static void gtk_menu_item_realize (GtkWidget *widget);
117 static void gtk_menu_item_unrealize (GtkWidget *widget);
118 static void gtk_menu_item_map (GtkWidget *widget);
119 static void gtk_menu_item_unmap (GtkWidget *widget);
120 static gboolean gtk_menu_item_enter (GtkWidget *widget,
121 GdkEventCrossing *event);
122 static gboolean gtk_menu_item_leave (GtkWidget *widget,
123 GdkEventCrossing *event);
124 static gboolean gtk_menu_item_draw (GtkWidget *widget,
126 static void gtk_menu_item_parent_set (GtkWidget *widget,
127 GtkWidget *previous_parent);
130 static void gtk_real_menu_item_select (GtkMenuItem *item);
131 static void gtk_real_menu_item_deselect (GtkMenuItem *item);
132 static void gtk_real_menu_item_activate (GtkMenuItem *item);
133 static void gtk_real_menu_item_activate_item (GtkMenuItem *item);
134 static void gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
136 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
138 static gboolean gtk_menu_item_mnemonic_activate (GtkWidget *widget,
139 gboolean group_cycling);
141 static void gtk_menu_item_ensure_label (GtkMenuItem *menu_item);
142 static gint gtk_menu_item_popup_timeout (gpointer data);
143 static void gtk_menu_item_position_menu (GtkMenu *menu,
148 static void gtk_menu_item_show_all (GtkWidget *widget);
149 static void gtk_menu_item_forall (GtkContainer *container,
150 gboolean include_internals,
151 GtkCallback callback,
152 gpointer callback_data);
153 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
156 static void gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
158 static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
160 static void gtk_menu_item_get_preferred_width (GtkWidget *widget,
163 static void gtk_menu_item_get_preferred_height (GtkWidget *widget,
166 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
171 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface);
172 static void gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
176 static void gtk_menu_item_buildable_custom_finished(GtkBuildable *buildable,
179 const gchar *tagname,
182 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface);
183 static void gtk_menu_item_update (GtkActivatable *activatable,
185 const gchar *property_name);
186 static void gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
188 static void gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
190 static void gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
191 gboolean use_appearance);
193 /* Declare deprecated function that need a declaration */
194 #ifdef GTK_DISABLE_DEPRECATED
195 void gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
196 gboolean right_justified);
197 gboolean gtk_menu_item_get_right_justified (GtkMenuItem *menu_item);
198 #endif /* GTK_DISABLE_DEPRECATED */
200 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
202 static GtkBuildableIface *parent_buildable_iface;
204 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
205 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
206 gtk_menu_item_buildable_interface_init)
207 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
208 gtk_menu_item_activatable_interface_init))
212 gtk_menu_item_class_init (GtkMenuItemClass *klass)
214 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
215 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
216 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
218 gobject_class->dispose = gtk_menu_item_dispose;
219 gobject_class->set_property = gtk_menu_item_set_property;
220 gobject_class->get_property = gtk_menu_item_get_property;
222 widget_class->destroy = gtk_menu_item_destroy;
223 widget_class->size_allocate = gtk_menu_item_size_allocate;
224 widget_class->draw = gtk_menu_item_draw;
225 widget_class->realize = gtk_menu_item_realize;
226 widget_class->unrealize = gtk_menu_item_unrealize;
227 widget_class->map = gtk_menu_item_map;
228 widget_class->unmap = gtk_menu_item_unmap;
229 widget_class->enter_notify_event = gtk_menu_item_enter;
230 widget_class->leave_notify_event = gtk_menu_item_leave;
231 widget_class->show_all = gtk_menu_item_show_all;
232 widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
233 widget_class->parent_set = gtk_menu_item_parent_set;
234 widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
235 widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
236 widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
237 widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
239 container_class->forall = gtk_menu_item_forall;
241 klass->activate = gtk_real_menu_item_activate;
242 klass->activate_item = gtk_real_menu_item_activate_item;
243 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
244 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
245 klass->set_label = gtk_real_menu_item_set_label;
246 klass->get_label = gtk_real_menu_item_get_label;
247 klass->select = gtk_real_menu_item_select;
248 klass->deselect = gtk_real_menu_item_deselect;
250 klass->hide_on_activate = TRUE;
253 * GtkMenuItem::activate:
254 * @menuitem: the object which received the signal.
256 * Emitted when the item is activated.
258 menu_item_signals[ACTIVATE] =
259 g_signal_new (I_("activate"),
260 G_OBJECT_CLASS_TYPE (gobject_class),
261 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
262 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
264 _gtk_marshal_VOID__VOID,
266 widget_class->activate_signal = menu_item_signals[ACTIVATE];
269 * GtkMenuItem::activate-item:
270 * @menuitem: the object which received the signal.
272 * Emitted when the item is activated, but also if the menu item has a
273 * submenu. For normal applications, the relevant signal is
274 * #GtkMenuItem::activate.
276 menu_item_signals[ACTIVATE_ITEM] =
277 g_signal_new (I_("activate-item"),
278 G_OBJECT_CLASS_TYPE (gobject_class),
280 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
282 _gtk_marshal_VOID__VOID,
285 menu_item_signals[TOGGLE_SIZE_REQUEST] =
286 g_signal_new (I_("toggle-size-request"),
287 G_OBJECT_CLASS_TYPE (gobject_class),
289 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
291 _gtk_marshal_VOID__POINTER,
295 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
296 g_signal_new (I_("toggle-size-allocate"),
297 G_OBJECT_CLASS_TYPE (gobject_class),
299 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
301 _gtk_marshal_VOID__INT,
305 menu_item_signals[SELECT] =
306 g_signal_new (I_("select"),
307 G_OBJECT_CLASS_TYPE (gobject_class),
309 G_STRUCT_OFFSET (GtkMenuItemClass, select),
311 _gtk_marshal_VOID__VOID,
314 menu_item_signals[DESELECT] =
315 g_signal_new (I_("deselect"),
316 G_OBJECT_CLASS_TYPE (gobject_class),
318 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
320 _gtk_marshal_VOID__VOID,
324 * GtkMenuItem:right-justified:
326 * Sets whether the menu item appears justified
327 * at the right side of a menu bar.
331 g_object_class_install_property (gobject_class,
332 PROP_RIGHT_JUSTIFIED,
333 g_param_spec_boolean ("right-justified",
334 P_("Right Justified"),
335 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
337 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
340 * GtkMenuItem:submenu:
342 * The submenu attached to the menu item, or %NULL if it has none.
346 g_object_class_install_property (gobject_class,
348 g_param_spec_object ("submenu",
350 P_("The submenu attached to the menu item, or NULL if it has none"),
352 GTK_PARAM_READWRITE));
355 * GtkMenuItem:accel-path:
357 * Sets the accelerator path of the menu item, through which runtime
358 * changes of the menu item's accelerator caused by the user can be
359 * identified and saved to persistant storage.
363 g_object_class_install_property (gobject_class,
365 g_param_spec_string ("accel-path",
367 P_("Sets the accelerator path of the menu item"),
369 GTK_PARAM_READWRITE));
374 * The text for the child label.
378 g_object_class_install_property (gobject_class,
380 g_param_spec_string ("label",
382 P_("The text for the child label"),
384 GTK_PARAM_READWRITE));
387 * GtkMenuItem:use-underline:
389 * %TRUE if underlines in the text indicate mnemonics.
393 g_object_class_install_property (gobject_class,
395 g_param_spec_boolean ("use-underline",
397 P_("If set, an underline in the text indicates "
398 "the next character should be used for the "
399 "mnemonic accelerator key"),
401 GTK_PARAM_READWRITE));
403 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
404 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
406 gtk_widget_class_install_style_property_parser (widget_class,
407 g_param_spec_enum ("selected-shadow-type",
408 "Selected Shadow Type",
409 "Shadow type when item is selected",
410 GTK_TYPE_SHADOW_TYPE,
413 gtk_rc_property_parse_enum);
415 gtk_widget_class_install_style_property (widget_class,
416 g_param_spec_int ("horizontal-padding",
417 "Horizontal Padding",
418 "Padding to left and right of the menu item",
422 GTK_PARAM_READABLE));
424 gtk_widget_class_install_style_property (widget_class,
425 g_param_spec_int ("toggle-spacing",
427 "Space between icon and label",
431 GTK_PARAM_READABLE));
433 gtk_widget_class_install_style_property (widget_class,
434 g_param_spec_int ("arrow-spacing",
436 "Space between label and arrow",
440 GTK_PARAM_READABLE));
442 gtk_widget_class_install_style_property (widget_class,
443 g_param_spec_float ("arrow-scaling",
445 P_("Amount of space used up by arrow, relative to the menu item's font size"),
447 GTK_PARAM_READABLE));
450 * GtkMenuItem:width-chars:
452 * The minimum desired width of the menu item in characters.
456 gtk_widget_class_install_style_property (widget_class,
457 g_param_spec_int ("width-chars",
458 P_("Width in Characters"),
459 P_("The minimum desired width of the menu item in characters"),
461 GTK_PARAM_READABLE));
463 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
467 gtk_menu_item_init (GtkMenuItem *menu_item)
469 GtkStyleContext *context;
470 GtkMenuItemPrivate *priv;
472 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
475 menu_item->priv = priv;
477 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
480 priv->use_action_appearance = TRUE;
482 menu_item->priv->submenu = NULL;
483 menu_item->priv->toggle_size = 0;
484 menu_item->priv->accelerator_width = 0;
485 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
486 priv->submenu_direction = GTK_DIRECTION_LEFT;
488 priv->submenu_direction = GTK_DIRECTION_RIGHT;
489 priv->submenu_placement = GTK_TOP_BOTTOM;
490 priv->right_justify = FALSE;
491 priv->use_action_appearance = TRUE;
495 context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
496 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
502 * Creates a new #GtkMenuItem.
504 * Returns: the newly created #GtkMenuItem
507 gtk_menu_item_new (void)
509 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
513 * gtk_menu_item_new_with_label:
514 * @label: the text for the label
516 * Creates a new #GtkMenuItem whose child is a #GtkLabel.
518 * Returns: the newly created #GtkMenuItem
521 gtk_menu_item_new_with_label (const gchar *label)
523 return g_object_new (GTK_TYPE_MENU_ITEM,
530 * gtk_menu_item_new_with_mnemonic:
531 * @label: The text of the button, with an underscore in front of the
534 * Creates a new #GtkMenuItem containing a label.
536 * The label will be created using gtk_label_new_with_mnemonic(),
537 * so underscores in @label indicate the mnemonic for the menu item.
539 * Returns: a new #GtkMenuItem
542 gtk_menu_item_new_with_mnemonic (const gchar *label)
544 return g_object_new (GTK_TYPE_MENU_ITEM,
545 "use-underline", TRUE,
551 gtk_menu_item_dispose (GObject *object)
553 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
554 GtkMenuItemPrivate *priv = menu_item->priv;
558 gtk_action_disconnect_accelerator (priv->action);
559 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
562 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
566 gtk_menu_item_set_property (GObject *object,
571 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
575 case PROP_RIGHT_JUSTIFIED:
576 gtk_menu_item_set_right_justified (menu_item, g_value_get_boolean (value));
579 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
581 case PROP_ACCEL_PATH:
582 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
585 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
587 case PROP_USE_UNDERLINE:
588 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
590 case PROP_ACTIVATABLE_RELATED_ACTION:
591 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
593 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
594 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
597 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
603 gtk_menu_item_get_property (GObject *object,
608 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
609 GtkMenuItemPrivate *priv = menu_item->priv;
613 case PROP_RIGHT_JUSTIFIED:
614 g_value_set_boolean (value, gtk_menu_item_get_right_justified (menu_item));
617 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
619 case PROP_ACCEL_PATH:
620 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
623 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
625 case PROP_USE_UNDERLINE:
626 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
628 case PROP_ACTIVATABLE_RELATED_ACTION:
629 g_value_set_object (value, priv->action);
631 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
632 g_value_set_boolean (value, priv->use_action_appearance);
635 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
641 gtk_menu_item_destroy (GtkWidget *widget)
643 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
644 GtkMenuItemPrivate *priv = menu_item->priv;
647 gtk_widget_destroy (priv->submenu);
649 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
653 gtk_menu_item_detacher (GtkWidget *widget,
656 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
657 GtkMenuItemPrivate *priv = menu_item->priv;
659 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
661 priv->submenu = NULL;
665 get_arrow_size (GtkWidget *widget,
669 GtkStyleContext *style_context;
671 PangoContext *context;
672 PangoFontMetrics *metrics;
673 gfloat arrow_scaling;
677 gtk_widget_style_get (widget,
678 "arrow-scaling", &arrow_scaling,
681 context = gtk_widget_get_pango_context (child);
682 style_context = gtk_widget_get_style_context (child);
683 state = gtk_widget_get_state_flags (child);
685 metrics = pango_context_get_metrics (context,
686 gtk_style_context_get_font (style_context, state),
687 pango_context_get_language (context));
689 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
690 pango_font_metrics_get_descent (metrics)));
692 pango_font_metrics_unref (metrics);
694 *size = *size * arrow_scaling;
699 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
704 if (GTK_IS_ACCEL_LABEL (widget))
708 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
709 *width = MAX (*width, w);
711 else if (GTK_IS_CONTAINER (widget))
712 gtk_container_foreach (GTK_CONTAINER (widget),
713 gtk_menu_item_accel_width_foreach,
718 get_minimum_width (GtkWidget *widget)
720 GtkStyleContext *style_context;
722 PangoContext *context;
723 PangoFontMetrics *metrics;
727 context = gtk_widget_get_pango_context (widget);
728 style_context = gtk_widget_get_style_context (widget);
729 state = gtk_widget_get_state_flags (widget);
731 metrics = pango_context_get_metrics (context,
732 gtk_style_context_get_font (style_context, state),
733 pango_context_get_language (context));
735 width = pango_font_metrics_get_approximate_char_width (metrics);
737 pango_font_metrics_unref (metrics);
739 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
741 return PANGO_PIXELS (width_chars * width);
745 gtk_menu_item_get_preferred_width (GtkWidget *widget,
749 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
750 GtkMenuItemPrivate *priv = menu_item->priv;
755 guint horizontal_padding;
757 GtkPackDirection pack_dir;
758 GtkPackDirection child_pack_dir;
759 gint min_width, nat_width;
760 GtkStyleContext *context;
764 min_width = nat_width = 0;
766 gtk_widget_style_get (widget,
767 "horizontal-padding", &horizontal_padding,
770 bin = GTK_BIN (widget);
771 parent = gtk_widget_get_parent (widget);
773 if (GTK_IS_MENU_BAR (parent))
775 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
776 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
780 pack_dir = GTK_PACK_DIRECTION_LTR;
781 child_pack_dir = GTK_PACK_DIRECTION_LTR;
784 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
786 context = gtk_widget_get_style_context (widget);
787 state = gtk_widget_get_state_flags (widget);
788 gtk_style_context_get_padding (context, state, &padding);
790 min_width = (border_width * 2) + padding.left + padding.right;
792 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
793 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
794 min_width += 2 * horizontal_padding;
796 nat_width = min_width;
798 child = gtk_bin_get_child (bin);
800 if (child != NULL && gtk_widget_get_visible (child))
802 GtkMenuItemPrivate *priv = menu_item->priv;
803 gint child_min, child_nat;
805 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
807 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
812 gtk_widget_style_get (widget,
813 "arrow-spacing", &arrow_spacing,
816 get_arrow_size (widget, child, &arrow_size);
818 gtk_widget_style_get (widget,
819 "arrow-spacing", &arrow_spacing,
822 get_arrow_size (widget, child, &arrow_size);
824 min_width += arrow_size;
825 min_width += arrow_spacing;
827 min_width = MAX (min_width, get_minimum_width (widget));
828 nat_width = min_width;
831 min_width += child_min;
832 nat_width += child_nat;
836 gtk_container_foreach (GTK_CONTAINER (menu_item),
837 gtk_menu_item_accel_width_foreach,
839 priv->accelerator_width = accel_width;
842 *minimum_size = min_width;
845 *natural_size = nat_width;
849 gtk_menu_item_get_preferred_height (GtkWidget *widget,
853 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
854 GtkMenuItemPrivate *priv = menu_item->priv;
856 GtkStyleContext *context;
862 guint horizontal_padding;
864 GtkPackDirection pack_dir;
865 GtkPackDirection child_pack_dir;
866 gint min_height, nat_height;
868 min_height = nat_height = 0;
870 context = gtk_widget_get_style_context (widget);
871 state = gtk_widget_get_state_flags (widget);
872 gtk_style_context_get_padding (context, state, &padding);
874 gtk_widget_style_get (widget,
875 "horizontal-padding", &horizontal_padding,
878 bin = GTK_BIN (widget);
879 parent = gtk_widget_get_parent (widget);
881 if (GTK_IS_MENU_BAR (parent))
883 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
884 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
888 pack_dir = GTK_PACK_DIRECTION_LTR;
889 child_pack_dir = GTK_PACK_DIRECTION_LTR;
892 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
893 min_height = (border_width * 2) + padding.top + padding.bottom;
895 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
896 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
897 min_height += 2 * horizontal_padding;
899 nat_height = min_height;
901 child = gtk_bin_get_child (bin);
903 if (child != NULL && gtk_widget_get_visible (child))
905 GtkMenuItemPrivate *priv = menu_item->priv;
906 gint child_min, child_nat;
908 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
910 min_height += child_min;
911 nat_height += child_nat;
913 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
917 get_arrow_size (widget, child, &arrow_size);
919 min_height = MAX (min_height, arrow_size);
920 nat_height = MAX (nat_height, arrow_size);
923 else /* separator item */
925 gboolean wide_separators;
926 gint separator_height;
928 gtk_widget_style_get (widget,
929 "wide-separators", &wide_separators,
930 "separator-height", &separator_height,
934 min_height += separator_height + padding.top;
936 min_height += padding.top + padding.bottom;
938 nat_height = min_height;
942 gtk_container_foreach (GTK_CONTAINER (menu_item),
943 gtk_menu_item_accel_width_foreach,
945 priv->accelerator_width = accel_width;
948 *minimum_size = min_height;
951 *natural_size = nat_height;
955 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
960 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
961 GtkMenuItemPrivate *priv = menu_item->priv;
963 GtkStyleContext *context;
968 guint horizontal_padding;
970 GtkPackDirection pack_dir;
971 GtkPackDirection child_pack_dir;
972 gint min_height, nat_height;
975 min_height = nat_height = 0;
977 context = gtk_widget_get_style_context (widget);
978 state = gtk_widget_get_state_flags (widget);
979 gtk_style_context_get_padding (context, state, &padding);
981 gtk_widget_style_get (widget,
982 "horizontal-padding", &horizontal_padding,
985 bin = GTK_BIN (widget);
986 parent = gtk_widget_get_parent (widget);
988 if (GTK_IS_MENU_BAR (parent))
990 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
991 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
995 pack_dir = GTK_PACK_DIRECTION_LTR;
996 child_pack_dir = GTK_PACK_DIRECTION_LTR;
999 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1000 min_height = (border_width * 2) + padding.top + padding.bottom;
1002 avail_size = for_size;
1003 avail_size -= (border_width * 2) + padding.left + padding.right;
1005 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1006 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1007 min_height += 2 * horizontal_padding;
1009 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1010 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1011 avail_size -= 2 * horizontal_padding;
1013 nat_height = min_height;
1015 child = gtk_bin_get_child (bin);
1017 if (child != NULL && gtk_widget_get_visible (child))
1019 gint child_min, child_nat;
1020 gint arrow_size = 0;
1022 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1024 guint arrow_spacing;
1026 gtk_widget_style_get (widget,
1027 "arrow-spacing", &arrow_spacing,
1030 get_arrow_size (widget, child, &arrow_size);
1032 avail_size -= arrow_size;
1033 avail_size -= arrow_spacing;
1036 gtk_widget_get_preferred_height_for_width (child,
1041 min_height += child_min;
1042 nat_height += child_nat;
1044 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1046 min_height = MAX (min_height, arrow_size);
1047 nat_height = MAX (nat_height, arrow_size);
1050 else /* separator item */
1052 gboolean wide_separators;
1053 gint separator_height;
1055 gtk_widget_style_get (widget,
1056 "wide-separators", &wide_separators,
1057 "separator-height", &separator_height,
1060 if (wide_separators)
1061 min_height += separator_height + padding.top;
1063 min_height += padding.top + padding.bottom;
1065 nat_height = min_height;
1069 *minimum_size = min_height;
1072 *natural_size = nat_height;
1076 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1078 parent_buildable_iface = g_type_interface_peek_parent (iface);
1079 iface->add_child = gtk_menu_item_buildable_add_child;
1080 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1084 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1085 GtkBuilder *builder,
1089 if (type && strcmp (type, "submenu") == 0)
1090 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1091 GTK_WIDGET (child));
1093 parent_buildable_iface->add_child (buildable, builder, child, type);
1098 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1099 GtkBuilder *builder,
1101 const gchar *tagname,
1104 GtkWidget *toplevel;
1106 if (strcmp (tagname, "accelerator") == 0)
1108 GtkMenuShell *menu_shell;
1111 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1114 while (GTK_IS_MENU (menu_shell) &&
1115 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1116 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1118 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1122 /* Fall back to something ... */
1123 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1125 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1126 gtk_buildable_get_name (buildable));
1129 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1130 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1133 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1138 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1140 iface->update = gtk_menu_item_update;
1141 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1145 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1149 child = gtk_bin_get_child (GTK_BIN (menu_item));
1151 if (GTK_IS_LABEL (child))
1155 label = gtk_action_get_label (action);
1156 gtk_menu_item_set_label (menu_item, label);
1160 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1163 gtk_menu_item_update (GtkActivatable *activatable,
1165 const gchar *property_name)
1167 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1168 GtkMenuItemPrivate *priv = menu_item->priv;
1170 if (strcmp (property_name, "visible") == 0)
1171 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1172 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1173 else if (strcmp (property_name, "sensitive") == 0)
1174 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1175 else if (priv->use_action_appearance)
1177 if (strcmp (property_name, "label") == 0)
1178 activatable_update_label (menu_item, action);
1183 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1186 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1187 GtkMenuItemPrivate *priv = menu_item->priv;
1190 if (!priv->use_action_appearance || !action)
1192 label = gtk_bin_get_child (GTK_BIN (menu_item));
1194 if (GTK_IS_ACCEL_LABEL (label))
1195 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1201 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1202 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1204 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1206 if (priv->use_action_appearance)
1208 label = gtk_bin_get_child (GTK_BIN (menu_item));
1210 /* make sure label is a label, deleting it otherwise */
1211 if (label && !GTK_IS_LABEL (label))
1213 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1216 /* Make sure that menu_item has a label and that any
1217 * accelerators are set */
1218 gtk_menu_item_ensure_label (menu_item);
1219 gtk_menu_item_set_use_underline (menu_item, TRUE);
1220 /* Make label point to the menu_item's label */
1221 label = gtk_bin_get_child (GTK_BIN (menu_item));
1223 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1225 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1226 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1227 gtk_action_get_accel_closure (action));
1230 activatable_update_label (menu_item, action);
1235 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1238 GtkMenuItemPrivate *priv = menu_item->priv;
1240 if (priv->action == action)
1245 gtk_action_disconnect_accelerator (priv->action);
1250 const gchar *accel_path;
1252 accel_path = gtk_action_get_accel_path (action);
1255 gtk_action_connect_accelerator (action);
1256 gtk_menu_item_set_accel_path (menu_item, accel_path);
1260 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1262 priv->action = action;
1266 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1267 gboolean use_appearance)
1269 GtkMenuItemPrivate *priv = menu_item->priv;
1271 if (priv->use_action_appearance != use_appearance)
1273 priv->use_action_appearance = use_appearance;
1275 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1281 * gtk_menu_item_set_submenu:
1282 * @menu_item: a #GtkMenuItem
1283 * @submenu: (allow-none): the submenu, or %NULL
1285 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1286 * submenu is passed.
1289 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1292 GtkMenuItemPrivate *priv = menu_item->priv;
1294 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1295 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1297 if (priv->submenu != submenu)
1300 gtk_menu_detach (GTK_MENU (priv->submenu));
1304 priv->submenu = submenu;
1305 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1306 GTK_WIDGET (menu_item),
1307 gtk_menu_item_detacher);
1310 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1311 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1313 g_object_notify (G_OBJECT (menu_item), "submenu");
1318 * gtk_menu_item_get_submenu:
1319 * @menu_item: a #GtkMenuItem
1321 * Gets the submenu underneath this menu item, if any.
1322 * See gtk_menu_item_set_submenu().
1324 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1327 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1329 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1331 return menu_item->priv->submenu;
1334 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1335 GtkSubmenuPlacement placement);
1338 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1339 GtkSubmenuPlacement placement)
1341 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1343 menu_item->priv->submenu_placement = placement;
1347 gtk_menu_item_select (GtkMenuItem *menu_item)
1351 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1353 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1355 /* Enable themeing of the parent menu item depending on whether
1356 * something is selected in its submenu
1358 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1359 if (GTK_IS_MENU (parent))
1361 GtkMenu *menu = GTK_MENU (parent);
1363 if (menu->priv->parent_menu_item)
1364 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1369 * gtk_menu_item_deselect:
1370 * @menu_item: the menu item
1372 * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1373 * exactly like #gtk_item_deselect.
1376 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1380 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1382 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1384 /* Enable themeing of the parent menu item depending on whether
1385 * something is selected in its submenu
1387 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1388 if (GTK_IS_MENU (parent))
1390 GtkMenu *menu = GTK_MENU (parent);
1392 if (menu->priv->parent_menu_item)
1393 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1398 * gtk_menu_item_activate:
1399 * @menu_item: the menu item
1401 * Emits the #GtkMenuItem::activate signal on the given item
1404 gtk_menu_item_activate (GtkMenuItem *menu_item)
1406 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1408 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1412 * gtk_menu_item_toggle_size_request:
1413 * @menu_item: the menu item
1414 * @requisition: the requisition to use as signal data.
1416 * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1419 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1422 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1424 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1428 * gtk_menu_item_toggle_size_allocate:
1429 * @menu_item: the menu item.
1430 * @allocation: the allocation to use as signal data.
1432 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1435 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1438 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1440 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1444 gtk_menu_item_size_allocate (GtkWidget *widget,
1445 GtkAllocation *allocation)
1447 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1448 GtkMenuItemPrivate *priv = menu_item->priv;
1450 GtkAllocation child_allocation;
1451 GtkTextDirection direction;
1452 GtkPackDirection pack_dir;
1453 GtkPackDirection child_pack_dir;
1457 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1458 g_return_if_fail (allocation != NULL);
1460 bin = GTK_BIN (widget);
1462 direction = gtk_widget_get_direction (widget);
1464 parent = gtk_widget_get_parent (widget);
1465 if (GTK_IS_MENU_BAR (parent))
1467 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1468 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1472 pack_dir = GTK_PACK_DIRECTION_LTR;
1473 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1476 gtk_widget_set_allocation (widget, allocation);
1478 child = gtk_bin_get_child (bin);
1481 GtkStyleContext *context;
1482 GtkStateFlags state;
1484 guint horizontal_padding;
1487 context = gtk_widget_get_style_context (widget);
1488 state = gtk_widget_get_state_flags (widget);
1489 gtk_style_context_get_padding (context, state, &padding);
1491 gtk_widget_style_get (widget,
1492 "horizontal-padding", &horizontal_padding,
1495 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1496 child_allocation.x = border_width + padding.left;
1497 child_allocation.y = border_width + padding.top;
1499 child_allocation.width = allocation->width - (border_width * 2) -
1500 padding.left - padding.right;
1501 child_allocation.height = allocation->height - (border_width * 2) -
1502 padding.top - padding.bottom;
1504 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1505 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1507 child_allocation.x += horizontal_padding;
1508 child_allocation.width -= 2 * horizontal_padding;
1510 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1511 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1513 child_allocation.y += horizontal_padding;
1514 child_allocation.height -= 2 * horizontal_padding;
1517 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1518 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1520 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1521 child_allocation.x += priv->toggle_size;
1522 child_allocation.width -= priv->toggle_size;
1526 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1527 child_allocation.y += priv->toggle_size;
1528 child_allocation.height -= priv->toggle_size;
1531 child_allocation.x += allocation->x;
1532 child_allocation.y += allocation->y;
1534 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1536 guint arrow_spacing;
1539 gtk_widget_style_get (widget,
1540 "arrow-spacing", &arrow_spacing,
1543 get_arrow_size (widget, child, &arrow_size);
1545 if (direction == GTK_TEXT_DIR_RTL)
1546 child_allocation.x += arrow_size + arrow_spacing;
1547 child_allocation.width -= arrow_size + arrow_spacing;
1550 if (child_allocation.width < 1)
1551 child_allocation.width = 1;
1553 gtk_widget_size_allocate (child, &child_allocation);
1556 if (gtk_widget_get_realized (widget))
1557 gdk_window_move_resize (priv->event_window,
1558 allocation->x, allocation->y,
1559 allocation->width, allocation->height);
1562 gtk_menu_reposition (GTK_MENU (priv->submenu));
1566 gtk_menu_item_realize (GtkWidget *widget)
1568 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1569 GtkMenuItemPrivate *priv = menu_item->priv;
1570 GtkAllocation allocation;
1572 GdkWindowAttr attributes;
1573 gint attributes_mask;
1575 gtk_widget_set_realized (widget, TRUE);
1577 window = gtk_widget_get_parent_window (widget);
1578 gtk_widget_set_window (widget, window);
1579 g_object_ref (window);
1581 gtk_widget_get_allocation (widget, &allocation);
1583 attributes.x = allocation.x;
1584 attributes.y = allocation.y;
1585 attributes.width = allocation.width;
1586 attributes.height = allocation.height;
1587 attributes.window_type = GDK_WINDOW_CHILD;
1588 attributes.wclass = GDK_INPUT_ONLY;
1589 attributes.event_mask = (gtk_widget_get_events (widget) |
1590 GDK_BUTTON_PRESS_MASK |
1591 GDK_BUTTON_RELEASE_MASK |
1592 GDK_ENTER_NOTIFY_MASK |
1593 GDK_LEAVE_NOTIFY_MASK |
1594 GDK_POINTER_MOTION_MASK);
1596 attributes_mask = GDK_WA_X | GDK_WA_Y;
1598 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1599 &attributes, attributes_mask);
1600 gdk_window_set_user_data (priv->event_window, widget);
1604 gtk_menu_item_unrealize (GtkWidget *widget)
1606 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1607 GtkMenuItemPrivate *priv = menu_item->priv;
1609 gdk_window_set_user_data (priv->event_window, NULL);
1610 gdk_window_destroy (priv->event_window);
1611 priv->event_window = NULL;
1613 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1617 gtk_menu_item_map (GtkWidget *widget)
1619 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1620 GtkMenuItemPrivate *priv = menu_item->priv;
1622 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1624 gdk_window_show (priv->event_window);
1628 gtk_menu_item_unmap (GtkWidget *widget)
1630 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1631 GtkMenuItemPrivate *priv = menu_item->priv;
1633 gdk_window_hide (priv->event_window);
1635 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1639 gtk_menu_item_enter (GtkWidget *widget,
1640 GdkEventCrossing *event)
1642 g_return_val_if_fail (event != NULL, FALSE);
1644 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1648 gtk_menu_item_leave (GtkWidget *widget,
1649 GdkEventCrossing *event)
1651 g_return_val_if_fail (event != NULL, FALSE);
1653 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1657 gtk_menu_item_draw (GtkWidget *widget,
1660 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1661 GtkMenuItemPrivate *priv = menu_item->priv;
1662 GtkStateFlags state;
1663 GtkStyleContext *context;
1665 GtkWidget *child, *parent;
1666 gint x, y, w, h, width, height;
1667 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1669 state = gtk_widget_get_state_flags (widget);
1670 context = gtk_widget_get_style_context (widget);
1671 width = gtk_widget_get_allocated_width (widget);
1672 height = gtk_widget_get_allocated_height (widget);
1676 w = width - border_width * 2;
1677 h = height - border_width * 2;
1679 child = gtk_bin_get_child (GTK_BIN (menu_item));
1680 parent = gtk_widget_get_parent (widget);
1682 gtk_style_context_save (context);
1683 gtk_style_context_set_state (context, state);
1685 gtk_style_context_get_padding (context, state, &padding);
1687 if (GTK_IS_MENU_BAR (parent))
1688 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1690 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1692 gtk_render_background (context, cr, x, y, w, h);
1693 gtk_render_frame (context, cr, x, y, w, h);
1696 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1698 gint arrow_x, arrow_y;
1700 guint horizontal_padding;
1701 GtkTextDirection direction;
1704 direction = gtk_widget_get_direction (widget);
1706 gtk_widget_style_get (widget,
1707 "horizontal-padding", &horizontal_padding,
1710 get_arrow_size (widget, child, &arrow_size);
1712 if (direction == GTK_TEXT_DIR_LTR)
1714 arrow_x = x + w - horizontal_padding - arrow_size;
1719 arrow_x = x + horizontal_padding;
1720 angle = (3 * G_PI) / 2;
1723 arrow_y = y + (h - arrow_size) / 2;
1725 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1729 gboolean wide_separators;
1730 gint separator_height;
1731 guint horizontal_padding;
1733 gtk_widget_style_get (widget,
1734 "wide-separators", &wide_separators,
1735 "separator-height", &separator_height,
1736 "horizontal-padding", &horizontal_padding,
1738 if (wide_separators)
1739 gtk_render_frame (context, cr,
1740 horizontal_padding + padding.left,
1741 (height - separator_height - padding.top) / 2,
1742 width - (2 * horizontal_padding) - padding.left - padding.right,
1745 gtk_render_line (context, cr,
1746 horizontal_padding + padding.left,
1747 (height - padding.top) / 2,
1748 width - horizontal_padding - padding.right - 1,
1749 (height - padding.top) / 2);
1752 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1754 gtk_style_context_restore (context);
1760 * gtk_menu_item_select:
1761 * @menu_item: the menu item
1763 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1764 * exactly like #gtk_item_select.
1767 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1769 GtkMenuItemPrivate *priv = menu_item->priv;
1770 gboolean touchscreen_mode;
1772 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1773 "gtk-touchscreen-mode", &touchscreen_mode,
1776 if (!touchscreen_mode && priv->submenu &&
1777 (!gtk_widget_get_mapped (priv->submenu) ||
1778 GTK_MENU (priv->submenu)->priv->tearoff_active))
1780 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1783 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1784 GTK_STATE_FLAG_PRELIGHT, FALSE);
1785 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1789 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1791 GtkMenuItemPrivate *priv = menu_item->priv;
1794 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1796 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1797 GTK_STATE_FLAG_PRELIGHT);
1798 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1802 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1803 gboolean group_cycling)
1807 parent = gtk_widget_get_parent (widget);
1809 if (GTK_IS_MENU_SHELL (parent))
1810 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1812 if (group_cycling &&
1814 GTK_IS_MENU_SHELL (parent) &&
1815 GTK_MENU_SHELL (parent)->priv->active)
1817 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1820 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1826 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1828 GtkMenuItemPrivate *priv = menu_item->priv;
1831 gtk_action_activate (priv->action);
1836 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1838 GtkMenuItemPrivate *priv = menu_item->priv;
1842 widget = GTK_WIDGET (menu_item);
1843 parent = gtk_widget_get_parent (widget);
1845 if (parent && GTK_IS_MENU_SHELL (parent))
1847 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1849 if (priv->submenu == NULL)
1850 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1853 gtk_menu_shell_select_item (menu_shell, widget);
1854 _gtk_menu_item_popup_submenu (widget, FALSE);
1856 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1862 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1865 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1871 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1874 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1876 menu_item->priv->toggle_size = allocation;
1880 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1885 gtk_menu_item_ensure_label (menu_item);
1887 child = gtk_bin_get_child (GTK_BIN (menu_item));
1888 if (GTK_IS_LABEL (child))
1890 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1892 g_object_notify (G_OBJECT (menu_item), "label");
1896 static const gchar *
1897 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1901 gtk_menu_item_ensure_label (menu_item);
1903 child = gtk_bin_get_child (GTK_BIN (menu_item));
1904 if (GTK_IS_LABEL (child))
1905 return gtk_label_get_label (GTK_LABEL (child));
1911 free_timeval (GTimeVal *val)
1913 g_slice_free (GTimeVal, val);
1917 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1918 gboolean remember_exact_time)
1920 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1921 GtkMenuItemPrivate *priv = menu_item->priv;
1924 parent = gtk_widget_get_parent (widget);
1926 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1928 gboolean take_focus;
1929 GtkMenuPositionFunc menu_position_func;
1931 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1932 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1934 if (remember_exact_time)
1936 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1938 g_get_current_time (popup_time);
1940 g_object_set_data_full (G_OBJECT (priv->submenu),
1941 "gtk-menu-exact-popup-time", popup_time,
1942 (GDestroyNotify) free_timeval);
1946 g_object_set_data (G_OBJECT (priv->submenu),
1947 "gtk-menu-exact-popup-time", NULL);
1950 /* gtk_menu_item_position_menu positions the submenu from the
1951 * menuitems position. If the menuitem doesn't have a window,
1952 * that doesn't work. In that case we use the default
1953 * positioning function instead which places the submenu at the
1956 if (gtk_widget_get_window (widget))
1957 menu_position_func = gtk_menu_item_position_menu;
1959 menu_position_func = NULL;
1961 gtk_menu_popup (GTK_MENU (priv->submenu),
1966 GTK_MENU_SHELL (parent)->priv->button,
1970 /* Enable themeing of the parent menu item depending on whether
1971 * its submenu is shown or not.
1973 gtk_widget_queue_draw (widget);
1977 gtk_menu_item_popup_timeout (gpointer data)
1979 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1980 GtkMenuItemPrivate *priv = menu_item->priv;
1983 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1985 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1986 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1988 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1989 if (priv->timer_from_keypress && priv->submenu)
1990 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1999 get_popup_delay (GtkWidget *widget)
2003 parent = gtk_widget_get_parent (widget);
2004 if (GTK_IS_MENU_SHELL (parent))
2006 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
2012 g_object_get (gtk_widget_get_settings (widget),
2013 "gtk-menu-popup-delay", &popup_delay,
2021 _gtk_menu_item_popup_submenu (GtkWidget *widget,
2022 gboolean with_delay)
2024 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2025 GtkMenuItemPrivate *priv = menu_item->priv;
2029 g_source_remove (priv->timer);
2036 gint popup_delay = get_popup_delay (widget);
2038 if (popup_delay > 0)
2040 GdkEvent *event = gtk_get_current_event ();
2042 priv->timer = gdk_threads_add_timeout (popup_delay,
2043 gtk_menu_item_popup_timeout,
2047 event->type != GDK_BUTTON_PRESS &&
2048 event->type != GDK_ENTER_NOTIFY)
2049 priv->timer_from_keypress = TRUE;
2051 priv->timer_from_keypress = FALSE;
2054 gdk_event_free (event);
2060 gtk_menu_item_real_popup_submenu (widget, FALSE);
2064 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
2066 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2067 GtkMenuItemPrivate *priv = menu_item->priv;
2071 g_object_set_data (G_OBJECT (priv->submenu),
2072 "gtk-menu-exact-popup-time", NULL);
2076 g_source_remove (priv->timer);
2080 gtk_menu_popdown (GTK_MENU (priv->submenu));
2082 gtk_widget_queue_draw (widget);
2087 get_offsets (GtkMenu *menu,
2088 gint *horizontal_offset,
2089 gint *vertical_offset)
2091 gint vertical_padding;
2092 gint horizontal_padding;
2093 GtkStyleContext *context;
2094 GtkStateFlags state;
2097 gtk_widget_style_get (GTK_WIDGET (menu),
2098 "horizontal-offset", horizontal_offset,
2099 "vertical-offset", vertical_offset,
2100 "horizontal-padding", &horizontal_padding,
2101 "vertical-padding", &vertical_padding,
2104 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2105 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2106 gtk_style_context_get_padding (context, state, &padding);
2108 *vertical_offset -= padding.top;
2109 *vertical_offset -= vertical_padding;
2110 *horizontal_offset += horizontal_padding;
2114 gtk_menu_item_position_menu (GtkMenu *menu,
2120 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2121 GtkMenuItemPrivate *priv = menu_item->priv;
2122 GtkAllocation allocation;
2124 GtkMenuItem *parent_menu_item;
2127 gint twidth, theight;
2129 GtkTextDirection direction;
2130 GdkRectangle monitor;
2132 gint horizontal_offset;
2133 gint vertical_offset;
2134 gint available_left, available_right;
2135 GtkStyleContext *context;
2136 GtkStateFlags state;
2137 GtkBorder parent_padding;
2139 g_return_if_fail (menu != NULL);
2140 g_return_if_fail (x != NULL);
2141 g_return_if_fail (y != NULL);
2143 widget = GTK_WIDGET (user_data);
2148 direction = gtk_widget_get_direction (widget);
2150 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2151 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2153 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2154 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2155 if (monitor_num < 0)
2157 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2159 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2161 g_warning ("Menu not on screen");
2165 gtk_widget_get_allocation (widget, &allocation);
2170 get_offsets (menu, &horizontal_offset, &vertical_offset);
2172 available_left = tx - monitor.x;
2173 available_right = monitor.x + monitor.width - (tx + allocation.width);
2175 parent = gtk_widget_get_parent (widget);
2176 if (GTK_IS_MENU_BAR (parent))
2178 priv->from_menubar = TRUE;
2180 else if (GTK_IS_MENU (parent))
2182 if (GTK_MENU (parent)->priv->parent_menu_item)
2183 priv->from_menubar = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item)->priv->from_menubar;
2185 priv->from_menubar = FALSE;
2189 priv->from_menubar = FALSE;
2192 switch (priv->submenu_placement)
2194 case GTK_TOP_BOTTOM:
2195 if (direction == GTK_TEXT_DIR_LTR)
2196 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2199 priv->submenu_direction = GTK_DIRECTION_LEFT;
2200 tx += allocation.width - twidth;
2202 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2203 ty += allocation.height;
2204 else if ((ty - theight) >= monitor.y)
2206 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2207 ty += allocation.height;
2212 case GTK_LEFT_RIGHT:
2213 if (GTK_IS_MENU (parent))
2214 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2216 parent_menu_item = NULL;
2218 context = gtk_widget_get_style_context (parent);
2219 state = gtk_widget_get_state_flags (parent);
2220 gtk_style_context_get_padding (context, state, &parent_padding);
2222 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2224 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2228 if (direction == GTK_TEXT_DIR_LTR)
2229 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2231 priv->submenu_direction = GTK_DIRECTION_LEFT;
2234 switch (priv->submenu_direction)
2236 case GTK_DIRECTION_LEFT:
2237 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2238 available_left >= available_right)
2239 tx -= twidth + parent_padding.left + horizontal_offset;
2242 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2243 tx += allocation.width + parent_padding.right + horizontal_offset;
2247 case GTK_DIRECTION_RIGHT:
2248 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2249 available_right >= available_left)
2250 tx += allocation.width + parent_padding.right + horizontal_offset;
2253 priv->submenu_direction = GTK_DIRECTION_LEFT;
2254 tx -= twidth + parent_padding.left + horizontal_offset;
2259 ty += vertical_offset;
2261 /* If the height of the menu doesn't fit we move it upward. */
2262 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2266 /* If we have negative, tx, here it is because we can't get
2267 * the menu all the way on screen. Favor the left portion.
2269 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2272 gtk_menu_set_monitor (menu, monitor_num);
2274 if (!gtk_widget_get_visible (menu->priv->toplevel))
2276 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2277 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2282 * gtk_menu_item_set_right_justified:
2283 * @menu_item: a #GtkMenuItem.
2284 * @right_justified: if %TRUE the menu item will appear at the
2285 * far right if added to a menu bar
2287 * Sets whether the menu item appears justified at the right
2288 * side of a menu bar. This was traditionally done for "Help"
2289 * menu items, but is now considered a bad idea. (If the widget
2290 * layout is reversed for a right-to-left language like Hebrew
2291 * or Arabic, right-justified-menu-items appear at the left.)
2293 * Deprecated: 3.2: If you insist on using it, use
2294 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2297 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2298 gboolean right_justified)
2300 GtkMenuItemPrivate *priv = menu_item->priv;
2302 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2304 right_justified = right_justified != FALSE;
2306 if (priv->right_justify != right_justified)
2308 priv->right_justify = right_justified;
2309 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2314 * gtk_menu_item_get_right_justified:
2315 * @menu_item: a #GtkMenuItem
2317 * Gets whether the menu item appears justified at the right
2318 * side of the menu bar.
2320 * Return value: %TRUE if the menu item will appear at the
2321 * far right if added to a menu bar.
2323 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2326 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2328 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2330 return menu_item->priv->right_justify;
2335 gtk_menu_item_show_all (GtkWidget *widget)
2337 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2338 GtkMenuItemPrivate *priv = menu_item->priv;
2340 /* show children including submenu */
2342 gtk_widget_show_all (priv->submenu);
2343 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2345 gtk_widget_show (widget);
2349 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2354 parent = gtk_widget_get_parent (widget);
2356 /* Chain to the parent GtkMenu for further checks */
2357 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2358 parent && gtk_widget_can_activate_accel (parent, signal_id));
2362 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2365 const gchar **path_p = data;
2369 if (GTK_IS_LABEL (widget))
2371 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2372 if (*path_p && (*path_p)[0] == 0)
2375 else if (GTK_IS_CONTAINER (widget))
2376 gtk_container_foreach (GTK_CONTAINER (widget),
2377 gtk_menu_item_accel_name_foreach,
2383 gtk_menu_item_parent_set (GtkWidget *widget,
2384 GtkWidget *previous_parent)
2386 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2390 parent = gtk_widget_get_parent (widget);
2391 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2394 _gtk_menu_item_refresh_accel_path (menu_item,
2395 menu->priv->accel_path,
2396 menu->priv->accel_group,
2399 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2400 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2404 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2405 const gchar *prefix,
2406 GtkAccelGroup *accel_group,
2407 gboolean group_changed)
2409 GtkMenuItemPrivate *priv = menu_item->priv;
2413 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2414 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2416 widget = GTK_WIDGET (menu_item);
2420 gtk_widget_set_accel_path (widget, NULL, NULL);
2424 path = _gtk_widget_get_accel_path (widget, NULL);
2425 if (!path) /* no active accel_path yet */
2427 path = priv->accel_path;
2428 if (!path && prefix)
2430 const gchar *postfix = NULL;
2433 /* try to construct one from label text */
2434 gtk_container_foreach (GTK_CONTAINER (menu_item),
2435 gtk_menu_item_accel_name_foreach,
2439 new_path = g_strconcat (prefix, "/", postfix, NULL);
2440 path = priv->accel_path = (char*)g_intern_string (new_path);
2445 gtk_widget_set_accel_path (widget, path, accel_group);
2447 else if (group_changed) /* reinstall accelerators */
2448 gtk_widget_set_accel_path (widget, path, accel_group);
2452 * gtk_menu_item_set_accel_path
2453 * @menu_item: a valid #GtkMenuItem
2454 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2455 * item's functionality, or %NULL to unset the current path.
2457 * Set the accelerator path on @menu_item, through which runtime
2458 * changes of the menu item's accelerator caused by the user can be
2459 * identified and saved to persistent storage (see gtk_accel_map_save()
2460 * on this). To set up a default accelerator for this menu item, call
2461 * gtk_accel_map_add_entry() with the same @accel_path. See also
2462 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2463 * and gtk_menu_set_accel_path() for a more convenient variant of
2466 * This function is basically a convenience wrapper that handles
2467 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2468 * group for the menu item.
2470 * Note that you do need to set an accelerator on the parent menu with
2471 * gtk_menu_set_accel_group() for this to work.
2473 * Note that @accel_path string will be stored in a #GQuark.
2474 * Therefore, if you pass a static string, you can save some memory
2475 * by interning it first with g_intern_static_string().
2478 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2479 const gchar *accel_path)
2481 GtkMenuItemPrivate *priv = menu_item->priv;
2485 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2486 g_return_if_fail (accel_path == NULL ||
2487 (accel_path[0] == '<' && strchr (accel_path, '/')));
2489 widget = GTK_WIDGET (menu_item);
2491 /* store new path */
2492 priv->accel_path = (char*)g_intern_string (accel_path);
2494 /* forget accelerators associated with old path */
2495 gtk_widget_set_accel_path (widget, NULL, NULL);
2497 /* install accelerators associated with new path */
2498 parent = gtk_widget_get_parent (widget);
2499 if (GTK_IS_MENU (parent))
2501 GtkMenu *menu = GTK_MENU (parent);
2503 if (menu->priv->accel_group)
2504 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2506 menu->priv->accel_group,
2512 * gtk_menu_item_get_accel_path
2513 * @menu_item: a valid #GtkMenuItem
2515 * Retrieve the accelerator path that was previously set on @menu_item.
2517 * See gtk_menu_item_set_accel_path() for details.
2519 * Returns: the accelerator path corresponding to this menu
2520 * item's functionality, or %NULL if not set
2525 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2527 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2529 return menu_item->priv->accel_path;
2533 gtk_menu_item_forall (GtkContainer *container,
2534 gboolean include_internals,
2535 GtkCallback callback,
2536 gpointer callback_data)
2540 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2541 g_return_if_fail (callback != NULL);
2543 child = gtk_bin_get_child (GTK_BIN (container));
2545 callback (child, callback_data);
2549 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2551 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2552 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2553 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2554 !gtk_widget_is_sensitive (menu_item) ||
2555 !gtk_widget_get_visible (menu_item))
2562 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2564 GtkWidget *accel_label;
2566 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2568 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2569 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
2570 gtk_widget_set_valign (accel_label, GTK_ALIGN_CENTER);
2572 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2573 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2574 GTK_WIDGET (menu_item));
2575 gtk_widget_show (accel_label);
2580 * gtk_menu_item_set_label:
2581 * @menu_item: a #GtkMenuItem
2582 * @label: the text you want to set
2584 * Sets @text on the @menu_item label
2589 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2592 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2594 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2598 * gtk_menu_item_get_label:
2599 * @menu_item: a #GtkMenuItem
2601 * Sets @text on the @menu_item label
2603 * Returns: The text in the @menu_item label. This is the internal
2604 * string used by the label, and must not be modified.
2609 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2611 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2613 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2617 * gtk_menu_item_set_use_underline:
2618 * @menu_item: a #GtkMenuItem
2619 * @setting: %TRUE if underlines in the text indicate mnemonics
2621 * If true, an underline in the text indicates the next character
2622 * should be used for the mnemonic accelerator key.
2627 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2632 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2634 gtk_menu_item_ensure_label (menu_item);
2636 child = gtk_bin_get_child (GTK_BIN (menu_item));
2637 if (GTK_IS_LABEL (child))
2639 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2641 g_object_notify (G_OBJECT (menu_item), "use-underline");
2646 * gtk_menu_item_get_use_underline:
2647 * @menu_item: a #GtkMenuItem
2649 * Checks if an underline in the text indicates the next character
2650 * should be used for the mnemonic accelerator key.
2652 * Return value: %TRUE if an embedded underline in the label
2653 * indicates the mnemonic accelerator key.
2658 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2662 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2664 gtk_menu_item_ensure_label (menu_item);
2666 child = gtk_bin_get_child (GTK_BIN (menu_item));
2667 if (GTK_IS_LABEL (child))
2668 return gtk_label_get_use_underline (GTK_LABEL (child));
2674 * gtk_menu_item_set_reserve_indicator:
2675 * @menu_item: a #GtkMenuItem
2676 * @reserve: the new value
2678 * Sets whether the @menu_item should reserve space for
2679 * the submenu indicator, regardless if it actually has
2682 * There should be little need for applications to call
2688 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2691 GtkMenuItemPrivate *priv;
2693 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2695 priv = menu_item->priv;
2697 if (priv->reserve_indicator != reserve)
2699 priv->reserve_indicator = reserve;
2700 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2705 * gtk_menu_item_get_reserve_indicator:
2706 * @menu_item: a #GtkMenuItem
2708 * Returns whether the @menu_item reserves space for
2709 * the submenu indicator, regardless if it has a submenu
2712 * Returns: %TRUE if @menu_item always reserves space for the
2718 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2720 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2722 return menu_item->priv->reserve_indicator;