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 /* Declare deprecated function that need a declaration */
195 #ifdef GTK_DISABLE_DEPRECATED
196 void gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
197 gboolean right_justified);
198 gboolean gtk_menu_item_get_right_justified (GtkMenuItem *menu_item);
199 #endif /* GTK_DISABLE_DEPRECATED */
201 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
203 static GtkBuildableIface *parent_buildable_iface;
205 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
206 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
207 gtk_menu_item_buildable_interface_init)
208 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
209 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 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
241 container_class->forall = gtk_menu_item_forall;
243 klass->activate = gtk_real_menu_item_activate;
244 klass->activate_item = gtk_real_menu_item_activate_item;
245 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
246 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
247 klass->set_label = gtk_real_menu_item_set_label;
248 klass->get_label = gtk_real_menu_item_get_label;
249 klass->select = gtk_real_menu_item_select;
250 klass->deselect = gtk_real_menu_item_deselect;
252 klass->hide_on_activate = TRUE;
255 * GtkMenuItem::activate:
256 * @menuitem: the object which received the signal.
258 * Emitted when the item is activated.
260 menu_item_signals[ACTIVATE] =
261 g_signal_new (I_("activate"),
262 G_OBJECT_CLASS_TYPE (gobject_class),
263 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
264 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
266 _gtk_marshal_VOID__VOID,
268 widget_class->activate_signal = menu_item_signals[ACTIVATE];
271 * GtkMenuItem::activate-item:
272 * @menuitem: the object which received the signal.
274 * Emitted when the item is activated, but also if the menu item has a
275 * submenu. For normal applications, the relevant signal is
276 * #GtkMenuItem::activate.
278 menu_item_signals[ACTIVATE_ITEM] =
279 g_signal_new (I_("activate-item"),
280 G_OBJECT_CLASS_TYPE (gobject_class),
282 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
284 _gtk_marshal_VOID__VOID,
287 menu_item_signals[TOGGLE_SIZE_REQUEST] =
288 g_signal_new (I_("toggle-size-request"),
289 G_OBJECT_CLASS_TYPE (gobject_class),
291 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
293 _gtk_marshal_VOID__POINTER,
297 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
298 g_signal_new (I_("toggle-size-allocate"),
299 G_OBJECT_CLASS_TYPE (gobject_class),
301 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
303 _gtk_marshal_VOID__INT,
307 menu_item_signals[SELECT] =
308 g_signal_new (I_("select"),
309 G_OBJECT_CLASS_TYPE (gobject_class),
311 G_STRUCT_OFFSET (GtkMenuItemClass, select),
313 _gtk_marshal_VOID__VOID,
316 menu_item_signals[DESELECT] =
317 g_signal_new (I_("deselect"),
318 G_OBJECT_CLASS_TYPE (gobject_class),
320 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
322 _gtk_marshal_VOID__VOID,
326 * GtkMenuItem:right-justified:
328 * Sets whether the menu item appears justified
329 * at the right side of a menu bar.
333 g_object_class_install_property (gobject_class,
334 PROP_RIGHT_JUSTIFIED,
335 g_param_spec_boolean ("right-justified",
336 P_("Right Justified"),
337 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
339 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
342 * GtkMenuItem:submenu:
344 * The submenu attached to the menu item, or %NULL if it has none.
348 g_object_class_install_property (gobject_class,
350 g_param_spec_object ("submenu",
352 P_("The submenu attached to the menu item, or NULL if it has none"),
354 GTK_PARAM_READWRITE));
357 * GtkMenuItem:accel-path:
359 * Sets the accelerator path of the menu item, through which runtime
360 * changes of the menu item's accelerator caused by the user can be
361 * identified and saved to persistant storage.
365 g_object_class_install_property (gobject_class,
367 g_param_spec_string ("accel-path",
369 P_("Sets the accelerator path of the menu item"),
371 GTK_PARAM_READWRITE));
376 * The text for the child label.
380 g_object_class_install_property (gobject_class,
382 g_param_spec_string ("label",
384 P_("The text for the child label"),
386 GTK_PARAM_READWRITE));
389 * GtkMenuItem:use-underline:
391 * %TRUE if underlines in the text indicate mnemonics.
395 g_object_class_install_property (gobject_class,
397 g_param_spec_boolean ("use-underline",
399 P_("If set, an underline in the text indicates "
400 "the next character should be used for the "
401 "mnemonic accelerator key"),
403 GTK_PARAM_READWRITE));
405 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
406 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
408 gtk_widget_class_install_style_property_parser (widget_class,
409 g_param_spec_enum ("selected-shadow-type",
410 "Selected Shadow Type",
411 "Shadow type when item is selected",
412 GTK_TYPE_SHADOW_TYPE,
415 gtk_rc_property_parse_enum);
417 gtk_widget_class_install_style_property (widget_class,
418 g_param_spec_int ("horizontal-padding",
419 "Horizontal Padding",
420 "Padding to left and right of the menu item",
424 GTK_PARAM_READABLE));
426 gtk_widget_class_install_style_property (widget_class,
427 g_param_spec_int ("toggle-spacing",
429 "Space between icon and label",
433 GTK_PARAM_READABLE));
435 gtk_widget_class_install_style_property (widget_class,
436 g_param_spec_int ("arrow-spacing",
438 "Space between label and arrow",
442 GTK_PARAM_READABLE));
444 gtk_widget_class_install_style_property (widget_class,
445 g_param_spec_float ("arrow-scaling",
447 P_("Amount of space used up by arrow, relative to the menu item's font size"),
449 GTK_PARAM_READABLE));
452 * GtkMenuItem:width-chars:
454 * The minimum desired width of the menu item in characters.
458 gtk_widget_class_install_style_property (widget_class,
459 g_param_spec_int ("width-chars",
460 P_("Width in Characters"),
461 P_("The minimum desired width of the menu item in characters"),
463 GTK_PARAM_READABLE));
465 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
469 gtk_menu_item_init (GtkMenuItem *menu_item)
471 GtkStyleContext *context;
472 GtkMenuItemPrivate *priv;
474 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
477 menu_item->priv = priv;
479 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
482 priv->use_action_appearance = TRUE;
484 menu_item->priv->submenu = NULL;
485 menu_item->priv->toggle_size = 0;
486 menu_item->priv->accelerator_width = 0;
487 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
488 priv->submenu_direction = GTK_DIRECTION_LEFT;
490 priv->submenu_direction = GTK_DIRECTION_RIGHT;
491 priv->submenu_placement = GTK_TOP_BOTTOM;
492 priv->right_justify = FALSE;
493 priv->use_action_appearance = TRUE;
497 context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
498 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
504 * Creates a new #GtkMenuItem.
506 * Returns: the newly created #GtkMenuItem
509 gtk_menu_item_new (void)
511 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
515 * gtk_menu_item_new_with_label:
516 * @label: the text for the label
518 * Creates a new #GtkMenuItem whose child is a #GtkLabel.
520 * Returns: the newly created #GtkMenuItem
523 gtk_menu_item_new_with_label (const gchar *label)
525 return g_object_new (GTK_TYPE_MENU_ITEM,
532 * gtk_menu_item_new_with_mnemonic:
533 * @label: The text of the button, with an underscore in front of the
536 * Creates a new #GtkMenuItem containing a label.
538 * The label will be created using gtk_label_new_with_mnemonic(),
539 * so underscores in @label indicate the mnemonic for the menu item.
541 * Returns: a new #GtkMenuItem
544 gtk_menu_item_new_with_mnemonic (const gchar *label)
546 return g_object_new (GTK_TYPE_MENU_ITEM,
547 "use-underline", TRUE,
553 gtk_menu_item_dispose (GObject *object)
555 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
556 GtkMenuItemPrivate *priv = menu_item->priv;
560 gtk_action_disconnect_accelerator (priv->action);
561 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
564 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
568 gtk_menu_item_set_property (GObject *object,
573 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
577 case PROP_RIGHT_JUSTIFIED:
578 gtk_menu_item_set_right_justified (menu_item, g_value_get_boolean (value));
581 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
583 case PROP_ACCEL_PATH:
584 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
587 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
589 case PROP_USE_UNDERLINE:
590 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
592 case PROP_ACTIVATABLE_RELATED_ACTION:
593 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
595 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
596 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
599 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605 gtk_menu_item_get_property (GObject *object,
610 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
611 GtkMenuItemPrivate *priv = menu_item->priv;
615 case PROP_RIGHT_JUSTIFIED:
616 g_value_set_boolean (value, gtk_menu_item_get_right_justified (menu_item));
619 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
621 case PROP_ACCEL_PATH:
622 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
625 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
627 case PROP_USE_UNDERLINE:
628 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
630 case PROP_ACTIVATABLE_RELATED_ACTION:
631 g_value_set_object (value, priv->action);
633 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
634 g_value_set_boolean (value, priv->use_action_appearance);
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 gtk_menu_item_destroy (GtkWidget *widget)
645 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
646 GtkMenuItemPrivate *priv = menu_item->priv;
649 gtk_widget_destroy (priv->submenu);
651 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
655 gtk_menu_item_detacher (GtkWidget *widget,
658 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
659 GtkMenuItemPrivate *priv = menu_item->priv;
661 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
663 priv->submenu = NULL;
667 get_arrow_size (GtkWidget *widget,
671 GtkStyleContext *style_context;
673 PangoContext *context;
674 PangoFontMetrics *metrics;
675 gfloat arrow_scaling;
679 gtk_widget_style_get (widget,
680 "arrow-scaling", &arrow_scaling,
683 context = gtk_widget_get_pango_context (child);
684 style_context = gtk_widget_get_style_context (child);
685 state = gtk_widget_get_state_flags (child);
687 metrics = pango_context_get_metrics (context,
688 gtk_style_context_get_font (style_context, state),
689 pango_context_get_language (context));
691 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
692 pango_font_metrics_get_descent (metrics)));
694 pango_font_metrics_unref (metrics);
696 *size = *size * arrow_scaling;
701 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
706 if (GTK_IS_ACCEL_LABEL (widget))
710 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
711 *width = MAX (*width, w);
713 else if (GTK_IS_CONTAINER (widget))
714 gtk_container_foreach (GTK_CONTAINER (widget),
715 gtk_menu_item_accel_width_foreach,
720 get_minimum_width (GtkWidget *widget)
722 GtkStyleContext *style_context;
724 PangoContext *context;
725 PangoFontMetrics *metrics;
729 context = gtk_widget_get_pango_context (widget);
730 style_context = gtk_widget_get_style_context (widget);
731 state = gtk_widget_get_state_flags (widget);
733 metrics = pango_context_get_metrics (context,
734 gtk_style_context_get_font (style_context, state),
735 pango_context_get_language (context));
737 width = pango_font_metrics_get_approximate_char_width (metrics);
739 pango_font_metrics_unref (metrics);
741 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
743 return PANGO_PIXELS (width_chars * width);
747 gtk_menu_item_get_preferred_width (GtkWidget *widget,
751 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
752 GtkMenuItemPrivate *priv = menu_item->priv;
757 guint horizontal_padding;
759 GtkPackDirection pack_dir;
760 GtkPackDirection child_pack_dir;
761 gint min_width, nat_width;
762 GtkStyleContext *context;
766 min_width = nat_width = 0;
768 gtk_widget_style_get (widget,
769 "horizontal-padding", &horizontal_padding,
772 bin = GTK_BIN (widget);
773 parent = gtk_widget_get_parent (widget);
775 if (GTK_IS_MENU_BAR (parent))
777 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
778 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
782 pack_dir = GTK_PACK_DIRECTION_LTR;
783 child_pack_dir = GTK_PACK_DIRECTION_LTR;
786 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
788 context = gtk_widget_get_style_context (widget);
789 state = gtk_widget_get_state_flags (widget);
790 gtk_style_context_get_padding (context, state, &padding);
792 min_width = (border_width * 2) + padding.left + padding.right;
794 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
795 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
796 min_width += 2 * horizontal_padding;
798 nat_width = min_width;
800 child = gtk_bin_get_child (bin);
802 if (child != NULL && gtk_widget_get_visible (child))
804 GtkMenuItemPrivate *priv = menu_item->priv;
805 gint child_min, child_nat;
807 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
809 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
814 gtk_widget_style_get (widget,
815 "arrow-spacing", &arrow_spacing,
818 get_arrow_size (widget, child, &arrow_size);
820 gtk_widget_style_get (widget,
821 "arrow-spacing", &arrow_spacing,
824 get_arrow_size (widget, child, &arrow_size);
826 min_width += arrow_size;
827 min_width += arrow_spacing;
829 min_width = MAX (min_width, get_minimum_width (widget));
830 nat_width = min_width;
833 min_width += child_min;
834 nat_width += child_nat;
838 gtk_container_foreach (GTK_CONTAINER (menu_item),
839 gtk_menu_item_accel_width_foreach,
841 priv->accelerator_width = accel_width;
844 *minimum_size = min_width;
847 *natural_size = nat_width;
851 gtk_menu_item_get_preferred_height (GtkWidget *widget,
855 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
856 GtkMenuItemPrivate *priv = menu_item->priv;
858 GtkStyleContext *context;
864 guint horizontal_padding;
866 GtkPackDirection pack_dir;
867 GtkPackDirection child_pack_dir;
868 gint min_height, nat_height;
870 min_height = nat_height = 0;
872 context = gtk_widget_get_style_context (widget);
873 state = gtk_widget_get_state_flags (widget);
874 gtk_style_context_get_padding (context, state, &padding);
876 gtk_widget_style_get (widget,
877 "horizontal-padding", &horizontal_padding,
880 bin = GTK_BIN (widget);
881 parent = gtk_widget_get_parent (widget);
883 if (GTK_IS_MENU_BAR (parent))
885 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
886 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
890 pack_dir = GTK_PACK_DIRECTION_LTR;
891 child_pack_dir = GTK_PACK_DIRECTION_LTR;
894 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
895 min_height = (border_width * 2) + padding.top + padding.bottom;
897 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
898 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
899 min_height += 2 * horizontal_padding;
901 nat_height = min_height;
903 child = gtk_bin_get_child (bin);
905 if (child != NULL && gtk_widget_get_visible (child))
907 GtkMenuItemPrivate *priv = menu_item->priv;
908 gint child_min, child_nat;
910 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
912 min_height += child_min;
913 nat_height += child_nat;
915 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
919 get_arrow_size (widget, child, &arrow_size);
921 min_height = MAX (min_height, arrow_size);
922 nat_height = MAX (nat_height, arrow_size);
925 else /* separator item */
927 gboolean wide_separators;
928 gint separator_height;
930 gtk_widget_style_get (widget,
931 "wide-separators", &wide_separators,
932 "separator-height", &separator_height,
936 min_height += separator_height + padding.top;
938 min_height += padding.top + padding.bottom;
940 nat_height = min_height;
944 gtk_container_foreach (GTK_CONTAINER (menu_item),
945 gtk_menu_item_accel_width_foreach,
947 priv->accelerator_width = accel_width;
950 *minimum_size = min_height;
953 *natural_size = nat_height;
957 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
962 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
963 GtkMenuItemPrivate *priv = menu_item->priv;
965 GtkStyleContext *context;
970 guint horizontal_padding;
972 GtkPackDirection pack_dir;
973 GtkPackDirection child_pack_dir;
974 gint min_height, nat_height;
977 min_height = nat_height = 0;
979 context = gtk_widget_get_style_context (widget);
980 state = gtk_widget_get_state_flags (widget);
981 gtk_style_context_get_padding (context, state, &padding);
983 gtk_widget_style_get (widget,
984 "horizontal-padding", &horizontal_padding,
987 bin = GTK_BIN (widget);
988 parent = gtk_widget_get_parent (widget);
990 if (GTK_IS_MENU_BAR (parent))
992 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
993 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
997 pack_dir = GTK_PACK_DIRECTION_LTR;
998 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1001 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1002 min_height = (border_width * 2) + padding.top + padding.bottom;
1004 avail_size = for_size;
1005 avail_size -= (border_width * 2) + padding.left + padding.right;
1007 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1008 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1009 min_height += 2 * horizontal_padding;
1011 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1012 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1013 avail_size -= 2 * horizontal_padding;
1015 nat_height = min_height;
1017 child = gtk_bin_get_child (bin);
1019 if (child != NULL && gtk_widget_get_visible (child))
1021 gint child_min, child_nat;
1022 gint arrow_size = 0;
1024 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1026 guint arrow_spacing;
1028 gtk_widget_style_get (widget,
1029 "arrow-spacing", &arrow_spacing,
1032 get_arrow_size (widget, child, &arrow_size);
1034 avail_size -= arrow_size;
1035 avail_size -= arrow_spacing;
1038 gtk_widget_get_preferred_height_for_width (child,
1043 min_height += child_min;
1044 nat_height += child_nat;
1046 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1048 min_height = MAX (min_height, arrow_size);
1049 nat_height = MAX (nat_height, arrow_size);
1052 else /* separator item */
1054 gboolean wide_separators;
1055 gint separator_height;
1057 gtk_widget_style_get (widget,
1058 "wide-separators", &wide_separators,
1059 "separator-height", &separator_height,
1062 if (wide_separators)
1063 min_height += separator_height + padding.top;
1065 min_height += padding.top + padding.bottom;
1067 nat_height = min_height;
1071 *minimum_size = min_height;
1074 *natural_size = nat_height;
1078 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1080 parent_buildable_iface = g_type_interface_peek_parent (iface);
1081 iface->add_child = gtk_menu_item_buildable_add_child;
1082 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1086 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1087 GtkBuilder *builder,
1091 if (type && strcmp (type, "submenu") == 0)
1092 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1093 GTK_WIDGET (child));
1095 parent_buildable_iface->add_child (buildable, builder, child, type);
1100 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1101 GtkBuilder *builder,
1103 const gchar *tagname,
1106 GtkWidget *toplevel;
1108 if (strcmp (tagname, "accelerator") == 0)
1110 GtkMenuShell *menu_shell;
1113 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1116 while (GTK_IS_MENU (menu_shell) &&
1117 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1118 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1120 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1124 /* Fall back to something ... */
1125 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1127 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1128 gtk_buildable_get_name (buildable));
1131 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1132 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1135 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1140 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1142 iface->update = gtk_menu_item_update;
1143 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1147 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1151 child = gtk_bin_get_child (GTK_BIN (menu_item));
1153 if (GTK_IS_LABEL (child))
1157 label = gtk_action_get_label (action);
1158 gtk_menu_item_set_label (menu_item, label);
1162 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1165 gtk_menu_item_update (GtkActivatable *activatable,
1167 const gchar *property_name)
1169 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1170 GtkMenuItemPrivate *priv = menu_item->priv;
1172 if (strcmp (property_name, "visible") == 0)
1173 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1174 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1175 else if (strcmp (property_name, "sensitive") == 0)
1176 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1177 else if (priv->use_action_appearance)
1179 if (strcmp (property_name, "label") == 0)
1180 activatable_update_label (menu_item, action);
1185 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1188 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1189 GtkMenuItemPrivate *priv = menu_item->priv;
1192 if (!priv->use_action_appearance || !action)
1194 label = gtk_bin_get_child (GTK_BIN (menu_item));
1196 if (GTK_IS_ACCEL_LABEL (label))
1197 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1203 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1204 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1206 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1208 if (priv->use_action_appearance)
1210 label = gtk_bin_get_child (GTK_BIN (menu_item));
1212 /* make sure label is a label, deleting it otherwise */
1213 if (label && !GTK_IS_LABEL (label))
1215 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1218 /* Make sure that menu_item has a label and that any
1219 * accelerators are set */
1220 gtk_menu_item_ensure_label (menu_item);
1221 gtk_menu_item_set_use_underline (menu_item, TRUE);
1222 /* Make label point to the menu_item's label */
1223 label = gtk_bin_get_child (GTK_BIN (menu_item));
1225 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1227 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1228 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1229 gtk_action_get_accel_closure (action));
1232 activatable_update_label (menu_item, action);
1237 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1240 GtkMenuItemPrivate *priv = menu_item->priv;
1242 if (priv->action == action)
1247 gtk_action_disconnect_accelerator (priv->action);
1252 const gchar *accel_path;
1254 accel_path = gtk_action_get_accel_path (action);
1257 gtk_action_connect_accelerator (action);
1258 gtk_menu_item_set_accel_path (menu_item, accel_path);
1262 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1264 priv->action = action;
1268 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1269 gboolean use_appearance)
1271 GtkMenuItemPrivate *priv = menu_item->priv;
1273 if (priv->use_action_appearance != use_appearance)
1275 priv->use_action_appearance = use_appearance;
1277 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1283 * gtk_menu_item_set_submenu:
1284 * @menu_item: a #GtkMenuItem
1285 * @submenu: (allow-none): the submenu, or %NULL
1287 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1288 * submenu is passed.
1291 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1294 GtkMenuItemPrivate *priv = menu_item->priv;
1296 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1297 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1299 if (priv->submenu != submenu)
1302 gtk_menu_detach (GTK_MENU (priv->submenu));
1306 priv->submenu = submenu;
1307 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1308 GTK_WIDGET (menu_item),
1309 gtk_menu_item_detacher);
1312 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1313 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1315 g_object_notify (G_OBJECT (menu_item), "submenu");
1320 * gtk_menu_item_get_submenu:
1321 * @menu_item: a #GtkMenuItem
1323 * Gets the submenu underneath this menu item, if any.
1324 * See gtk_menu_item_set_submenu().
1326 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1329 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1331 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1333 return menu_item->priv->submenu;
1336 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1337 GtkSubmenuPlacement placement);
1340 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1341 GtkSubmenuPlacement placement)
1343 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1345 menu_item->priv->submenu_placement = placement;
1349 gtk_menu_item_select (GtkMenuItem *menu_item)
1353 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1355 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1357 /* Enable themeing of the parent menu item depending on whether
1358 * something is selected in its submenu
1360 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1361 if (GTK_IS_MENU (parent))
1363 GtkMenu *menu = GTK_MENU (parent);
1365 if (menu->priv->parent_menu_item)
1366 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1371 * gtk_menu_item_deselect:
1372 * @menu_item: the menu item
1374 * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1375 * exactly like #gtk_item_deselect.
1378 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1382 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1384 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1386 /* Enable themeing of the parent menu item depending on whether
1387 * something is selected in its submenu
1389 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1390 if (GTK_IS_MENU (parent))
1392 GtkMenu *menu = GTK_MENU (parent);
1394 if (menu->priv->parent_menu_item)
1395 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1400 * gtk_menu_item_activate:
1401 * @menu_item: the menu item
1403 * Emits the #GtkMenuItem::activate signal on the given item
1406 gtk_menu_item_activate (GtkMenuItem *menu_item)
1408 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1410 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1414 * gtk_menu_item_toggle_size_request:
1415 * @menu_item: the menu item
1416 * @requisition: the requisition to use as signal data.
1418 * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1421 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1424 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1426 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1430 * gtk_menu_item_toggle_size_allocate:
1431 * @menu_item: the menu item.
1432 * @allocation: the allocation to use as signal data.
1434 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1437 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1440 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1442 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1446 gtk_menu_item_size_allocate (GtkWidget *widget,
1447 GtkAllocation *allocation)
1449 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1450 GtkMenuItemPrivate *priv = menu_item->priv;
1452 GtkAllocation child_allocation;
1453 GtkTextDirection direction;
1454 GtkPackDirection pack_dir;
1455 GtkPackDirection child_pack_dir;
1459 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1460 g_return_if_fail (allocation != NULL);
1462 bin = GTK_BIN (widget);
1464 direction = gtk_widget_get_direction (widget);
1466 parent = gtk_widget_get_parent (widget);
1467 if (GTK_IS_MENU_BAR (parent))
1469 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1470 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1474 pack_dir = GTK_PACK_DIRECTION_LTR;
1475 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1478 gtk_widget_set_allocation (widget, allocation);
1480 child = gtk_bin_get_child (bin);
1483 GtkStyleContext *context;
1484 GtkStateFlags state;
1486 guint horizontal_padding;
1489 context = gtk_widget_get_style_context (widget);
1490 state = gtk_widget_get_state_flags (widget);
1491 gtk_style_context_get_padding (context, state, &padding);
1493 gtk_widget_style_get (widget,
1494 "horizontal-padding", &horizontal_padding,
1497 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1498 child_allocation.x = border_width + padding.left;
1499 child_allocation.y = border_width + padding.top;
1501 child_allocation.width = allocation->width - (border_width * 2) -
1502 padding.left - padding.right;
1503 child_allocation.height = allocation->height - (border_width * 2) -
1504 padding.top - padding.bottom;
1506 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1507 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1509 child_allocation.x += horizontal_padding;
1510 child_allocation.width -= 2 * horizontal_padding;
1512 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1513 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1515 child_allocation.y += horizontal_padding;
1516 child_allocation.height -= 2 * horizontal_padding;
1519 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1520 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1522 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1523 child_allocation.x += priv->toggle_size;
1524 child_allocation.width -= priv->toggle_size;
1528 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1529 child_allocation.y += priv->toggle_size;
1530 child_allocation.height -= priv->toggle_size;
1533 child_allocation.x += allocation->x;
1534 child_allocation.y += allocation->y;
1536 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1538 guint arrow_spacing;
1541 gtk_widget_style_get (widget,
1542 "arrow-spacing", &arrow_spacing,
1545 get_arrow_size (widget, child, &arrow_size);
1547 if (direction == GTK_TEXT_DIR_RTL)
1548 child_allocation.x += arrow_size + arrow_spacing;
1549 child_allocation.width -= arrow_size + arrow_spacing;
1552 if (child_allocation.width < 1)
1553 child_allocation.width = 1;
1555 gtk_widget_size_allocate (child, &child_allocation);
1558 if (gtk_widget_get_realized (widget))
1559 gdk_window_move_resize (priv->event_window,
1560 allocation->x, allocation->y,
1561 allocation->width, allocation->height);
1564 gtk_menu_reposition (GTK_MENU (priv->submenu));
1568 gtk_menu_item_realize (GtkWidget *widget)
1570 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1571 GtkMenuItemPrivate *priv = menu_item->priv;
1572 GtkAllocation allocation;
1574 GdkWindowAttr attributes;
1575 gint attributes_mask;
1577 gtk_widget_set_realized (widget, TRUE);
1579 window = gtk_widget_get_parent_window (widget);
1580 gtk_widget_set_window (widget, window);
1581 g_object_ref (window);
1583 gtk_widget_get_allocation (widget, &allocation);
1585 attributes.x = allocation.x;
1586 attributes.y = allocation.y;
1587 attributes.width = allocation.width;
1588 attributes.height = allocation.height;
1589 attributes.window_type = GDK_WINDOW_CHILD;
1590 attributes.wclass = GDK_INPUT_ONLY;
1591 attributes.event_mask = (gtk_widget_get_events (widget) |
1592 GDK_BUTTON_PRESS_MASK |
1593 GDK_BUTTON_RELEASE_MASK |
1594 GDK_ENTER_NOTIFY_MASK |
1595 GDK_LEAVE_NOTIFY_MASK |
1596 GDK_POINTER_MOTION_MASK);
1598 attributes_mask = GDK_WA_X | GDK_WA_Y;
1600 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1601 &attributes, attributes_mask);
1602 gdk_window_set_user_data (priv->event_window, widget);
1606 gtk_menu_item_unrealize (GtkWidget *widget)
1608 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1609 GtkMenuItemPrivate *priv = menu_item->priv;
1611 gdk_window_set_user_data (priv->event_window, NULL);
1612 gdk_window_destroy (priv->event_window);
1613 priv->event_window = NULL;
1615 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1619 gtk_menu_item_map (GtkWidget *widget)
1621 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1622 GtkMenuItemPrivate *priv = menu_item->priv;
1624 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1626 gdk_window_show (priv->event_window);
1630 gtk_menu_item_unmap (GtkWidget *widget)
1632 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1633 GtkMenuItemPrivate *priv = menu_item->priv;
1635 gdk_window_hide (priv->event_window);
1637 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1641 gtk_menu_item_enter (GtkWidget *widget,
1642 GdkEventCrossing *event)
1644 g_return_val_if_fail (event != NULL, FALSE);
1646 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1650 gtk_menu_item_leave (GtkWidget *widget,
1651 GdkEventCrossing *event)
1653 g_return_val_if_fail (event != NULL, FALSE);
1655 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1659 gtk_menu_item_draw (GtkWidget *widget,
1662 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1663 GtkMenuItemPrivate *priv = menu_item->priv;
1664 GtkStateFlags state;
1665 GtkStyleContext *context;
1667 GtkWidget *child, *parent;
1668 gint x, y, w, h, width, height;
1669 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1671 state = gtk_widget_get_state_flags (widget);
1672 context = gtk_widget_get_style_context (widget);
1673 width = gtk_widget_get_allocated_width (widget);
1674 height = gtk_widget_get_allocated_height (widget);
1678 w = width - border_width * 2;
1679 h = height - border_width * 2;
1681 child = gtk_bin_get_child (GTK_BIN (menu_item));
1682 parent = gtk_widget_get_parent (widget);
1684 gtk_style_context_save (context);
1685 gtk_style_context_set_state (context, state);
1687 gtk_style_context_get_padding (context, state, &padding);
1689 if (GTK_IS_MENU_BAR (parent))
1690 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1692 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1694 gtk_render_background (context, cr, x, y, w, h);
1695 gtk_render_frame (context, cr, x, y, w, h);
1698 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1700 gint arrow_x, arrow_y;
1702 guint horizontal_padding;
1703 GtkTextDirection direction;
1706 direction = gtk_widget_get_direction (widget);
1708 gtk_widget_style_get (widget,
1709 "horizontal-padding", &horizontal_padding,
1712 get_arrow_size (widget, child, &arrow_size);
1714 if (direction == GTK_TEXT_DIR_LTR)
1716 arrow_x = x + w - horizontal_padding - arrow_size;
1721 arrow_x = x + horizontal_padding;
1722 angle = (3 * G_PI) / 2;
1725 arrow_y = y + (h - arrow_size) / 2;
1727 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1731 gboolean wide_separators;
1732 gint separator_height;
1733 guint horizontal_padding;
1735 gtk_widget_style_get (widget,
1736 "wide-separators", &wide_separators,
1737 "separator-height", &separator_height,
1738 "horizontal-padding", &horizontal_padding,
1740 if (wide_separators)
1741 gtk_render_frame (context, cr,
1742 horizontal_padding + padding.left,
1743 (height - separator_height - padding.top) / 2,
1744 width - (2 * horizontal_padding) - padding.left - padding.right,
1747 gtk_render_line (context, cr,
1748 horizontal_padding + padding.left,
1749 (height - padding.top) / 2,
1750 width - horizontal_padding - padding.right - 1,
1751 (height - padding.top) / 2);
1754 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1756 gtk_style_context_restore (context);
1762 * gtk_menu_item_select:
1763 * @menu_item: the menu item
1765 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1766 * exactly like #gtk_item_select.
1769 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1771 GtkMenuItemPrivate *priv = menu_item->priv;
1772 gboolean touchscreen_mode;
1774 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1775 "gtk-touchscreen-mode", &touchscreen_mode,
1778 if (!touchscreen_mode && priv->submenu &&
1779 (!gtk_widget_get_mapped (priv->submenu) ||
1780 GTK_MENU (priv->submenu)->priv->tearoff_active))
1782 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1785 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1786 GTK_STATE_FLAG_PRELIGHT, FALSE);
1787 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1791 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1793 GtkMenuItemPrivate *priv = menu_item->priv;
1796 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1798 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1799 GTK_STATE_FLAG_PRELIGHT);
1800 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1804 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1805 gboolean group_cycling)
1809 parent = gtk_widget_get_parent (widget);
1811 if (GTK_IS_MENU_SHELL (parent))
1812 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1814 if (group_cycling &&
1816 GTK_IS_MENU_SHELL (parent) &&
1817 GTK_MENU_SHELL (parent)->priv->active)
1819 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1822 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1828 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1830 GtkMenuItemPrivate *priv = menu_item->priv;
1833 gtk_action_activate (priv->action);
1838 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1840 GtkMenuItemPrivate *priv = menu_item->priv;
1844 widget = GTK_WIDGET (menu_item);
1845 parent = gtk_widget_get_parent (widget);
1847 if (parent && GTK_IS_MENU_SHELL (parent))
1849 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1851 if (priv->submenu == NULL)
1852 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1855 gtk_menu_shell_select_item (menu_shell, widget);
1856 _gtk_menu_item_popup_submenu (widget, FALSE);
1858 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1864 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1867 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1873 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1876 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1878 menu_item->priv->toggle_size = allocation;
1882 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1887 gtk_menu_item_ensure_label (menu_item);
1889 child = gtk_bin_get_child (GTK_BIN (menu_item));
1890 if (GTK_IS_LABEL (child))
1892 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1894 g_object_notify (G_OBJECT (menu_item), "label");
1898 static const gchar *
1899 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1903 gtk_menu_item_ensure_label (menu_item);
1905 child = gtk_bin_get_child (GTK_BIN (menu_item));
1906 if (GTK_IS_LABEL (child))
1907 return gtk_label_get_label (GTK_LABEL (child));
1913 free_timeval (GTimeVal *val)
1915 g_slice_free (GTimeVal, val);
1919 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1920 gboolean remember_exact_time)
1922 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1923 GtkMenuItemPrivate *priv = menu_item->priv;
1926 parent = gtk_widget_get_parent (widget);
1928 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1930 gboolean take_focus;
1931 GtkMenuPositionFunc menu_position_func;
1933 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1934 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1936 if (remember_exact_time)
1938 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1940 g_get_current_time (popup_time);
1942 g_object_set_data_full (G_OBJECT (priv->submenu),
1943 "gtk-menu-exact-popup-time", popup_time,
1944 (GDestroyNotify) free_timeval);
1948 g_object_set_data (G_OBJECT (priv->submenu),
1949 "gtk-menu-exact-popup-time", NULL);
1952 /* gtk_menu_item_position_menu positions the submenu from the
1953 * menuitems position. If the menuitem doesn't have a window,
1954 * that doesn't work. In that case we use the default
1955 * positioning function instead which places the submenu at the
1958 if (gtk_widget_get_window (widget))
1959 menu_position_func = gtk_menu_item_position_menu;
1961 menu_position_func = NULL;
1963 gtk_menu_popup (GTK_MENU (priv->submenu),
1968 GTK_MENU_SHELL (parent)->priv->button,
1972 /* Enable themeing of the parent menu item depending on whether
1973 * its submenu is shown or not.
1975 gtk_widget_queue_draw (widget);
1979 gtk_menu_item_popup_timeout (gpointer data)
1981 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1982 GtkMenuItemPrivate *priv = menu_item->priv;
1985 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1987 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1988 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1990 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1991 if (priv->timer_from_keypress && priv->submenu)
1992 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
2001 get_popup_delay (GtkWidget *widget)
2005 parent = gtk_widget_get_parent (widget);
2006 if (GTK_IS_MENU_SHELL (parent))
2008 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
2014 g_object_get (gtk_widget_get_settings (widget),
2015 "gtk-menu-popup-delay", &popup_delay,
2023 _gtk_menu_item_popup_submenu (GtkWidget *widget,
2024 gboolean with_delay)
2026 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2027 GtkMenuItemPrivate *priv = menu_item->priv;
2031 g_source_remove (priv->timer);
2038 gint popup_delay = get_popup_delay (widget);
2040 if (popup_delay > 0)
2042 GdkEvent *event = gtk_get_current_event ();
2044 priv->timer = gdk_threads_add_timeout (popup_delay,
2045 gtk_menu_item_popup_timeout,
2049 event->type != GDK_BUTTON_PRESS &&
2050 event->type != GDK_ENTER_NOTIFY)
2051 priv->timer_from_keypress = TRUE;
2053 priv->timer_from_keypress = FALSE;
2056 gdk_event_free (event);
2062 gtk_menu_item_real_popup_submenu (widget, FALSE);
2066 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
2068 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2069 GtkMenuItemPrivate *priv = menu_item->priv;
2073 g_object_set_data (G_OBJECT (priv->submenu),
2074 "gtk-menu-exact-popup-time", NULL);
2078 g_source_remove (priv->timer);
2082 gtk_menu_popdown (GTK_MENU (priv->submenu));
2084 gtk_widget_queue_draw (widget);
2089 get_offsets (GtkMenu *menu,
2090 gint *horizontal_offset,
2091 gint *vertical_offset)
2093 gint vertical_padding;
2094 gint horizontal_padding;
2095 GtkStyleContext *context;
2096 GtkStateFlags state;
2099 gtk_widget_style_get (GTK_WIDGET (menu),
2100 "horizontal-offset", horizontal_offset,
2101 "vertical-offset", vertical_offset,
2102 "horizontal-padding", &horizontal_padding,
2103 "vertical-padding", &vertical_padding,
2106 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2107 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2108 gtk_style_context_get_padding (context, state, &padding);
2110 *vertical_offset -= padding.top;
2111 *vertical_offset -= vertical_padding;
2112 *horizontal_offset += horizontal_padding;
2116 gtk_menu_item_position_menu (GtkMenu *menu,
2122 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2123 GtkMenuItemPrivate *priv = menu_item->priv;
2124 GtkAllocation allocation;
2126 GtkMenuItem *parent_menu_item;
2129 gint twidth, theight;
2131 GtkTextDirection direction;
2132 GdkRectangle monitor;
2134 gint horizontal_offset;
2135 gint vertical_offset;
2136 gint available_left, available_right;
2137 GtkStyleContext *context;
2138 GtkStateFlags state;
2139 GtkBorder parent_padding;
2141 g_return_if_fail (menu != NULL);
2142 g_return_if_fail (x != NULL);
2143 g_return_if_fail (y != NULL);
2145 widget = GTK_WIDGET (user_data);
2150 direction = gtk_widget_get_direction (widget);
2152 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2153 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2155 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2156 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2157 if (monitor_num < 0)
2159 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2161 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2163 g_warning ("Menu not on screen");
2167 gtk_widget_get_allocation (widget, &allocation);
2172 get_offsets (menu, &horizontal_offset, &vertical_offset);
2174 available_left = tx - monitor.x;
2175 available_right = monitor.x + monitor.width - (tx + allocation.width);
2177 parent = gtk_widget_get_parent (widget);
2178 if (GTK_IS_MENU_BAR (parent))
2180 priv->from_menubar = TRUE;
2182 else if (GTK_IS_MENU (parent))
2184 if (GTK_MENU (parent)->priv->parent_menu_item)
2185 priv->from_menubar = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item)->priv->from_menubar;
2187 priv->from_menubar = FALSE;
2191 priv->from_menubar = FALSE;
2194 switch (priv->submenu_placement)
2196 case GTK_TOP_BOTTOM:
2197 if (direction == GTK_TEXT_DIR_LTR)
2198 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2201 priv->submenu_direction = GTK_DIRECTION_LEFT;
2202 tx += allocation.width - twidth;
2204 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2205 ty += allocation.height;
2206 else if ((ty - theight) >= monitor.y)
2208 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2209 ty += allocation.height;
2214 case GTK_LEFT_RIGHT:
2215 if (GTK_IS_MENU (parent))
2216 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2218 parent_menu_item = NULL;
2220 context = gtk_widget_get_style_context (parent);
2221 state = gtk_widget_get_state_flags (parent);
2222 gtk_style_context_get_padding (context, state, &parent_padding);
2224 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2226 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2230 if (direction == GTK_TEXT_DIR_LTR)
2231 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2233 priv->submenu_direction = GTK_DIRECTION_LEFT;
2236 switch (priv->submenu_direction)
2238 case GTK_DIRECTION_LEFT:
2239 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2240 available_left >= available_right)
2241 tx -= twidth + parent_padding.left + horizontal_offset;
2244 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2245 tx += allocation.width + parent_padding.right + horizontal_offset;
2249 case GTK_DIRECTION_RIGHT:
2250 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2251 available_right >= available_left)
2252 tx += allocation.width + parent_padding.right + horizontal_offset;
2255 priv->submenu_direction = GTK_DIRECTION_LEFT;
2256 tx -= twidth + parent_padding.left + horizontal_offset;
2261 ty += vertical_offset;
2263 /* If the height of the menu doesn't fit we move it upward. */
2264 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2268 /* If we have negative, tx, here it is because we can't get
2269 * the menu all the way on screen. Favor the left portion.
2271 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2274 gtk_menu_set_monitor (menu, monitor_num);
2276 if (!gtk_widget_get_visible (menu->priv->toplevel))
2278 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2279 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2284 * gtk_menu_item_set_right_justified:
2285 * @menu_item: a #GtkMenuItem.
2286 * @right_justified: if %TRUE the menu item will appear at the
2287 * far right if added to a menu bar
2289 * Sets whether the menu item appears justified at the right
2290 * side of a menu bar. This was traditionally done for "Help"
2291 * menu items, but is now considered a bad idea. (If the widget
2292 * layout is reversed for a right-to-left language like Hebrew
2293 * or Arabic, right-justified-menu-items appear at the left.)
2295 * Deprecated: 3.2: If you insist on using it, use
2296 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2299 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2300 gboolean right_justified)
2302 GtkMenuItemPrivate *priv = menu_item->priv;
2304 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2306 right_justified = right_justified != FALSE;
2308 if (priv->right_justify != right_justified)
2310 priv->right_justify = right_justified;
2311 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2316 * gtk_menu_item_get_right_justified:
2317 * @menu_item: a #GtkMenuItem
2319 * Gets whether the menu item appears justified at the right
2320 * side of the menu bar.
2322 * Return value: %TRUE if the menu item will appear at the
2323 * far right if added to a menu bar.
2325 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2328 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2330 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2332 return menu_item->priv->right_justify;
2337 gtk_menu_item_show_all (GtkWidget *widget)
2339 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2340 GtkMenuItemPrivate *priv = menu_item->priv;
2342 /* show children including submenu */
2344 gtk_widget_show_all (priv->submenu);
2345 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2347 gtk_widget_show (widget);
2351 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2356 parent = gtk_widget_get_parent (widget);
2358 /* Chain to the parent GtkMenu for further checks */
2359 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2360 parent && gtk_widget_can_activate_accel (parent, signal_id));
2364 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2367 const gchar **path_p = data;
2371 if (GTK_IS_LABEL (widget))
2373 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2374 if (*path_p && (*path_p)[0] == 0)
2377 else if (GTK_IS_CONTAINER (widget))
2378 gtk_container_foreach (GTK_CONTAINER (widget),
2379 gtk_menu_item_accel_name_foreach,
2385 gtk_menu_item_parent_set (GtkWidget *widget,
2386 GtkWidget *previous_parent)
2388 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2392 parent = gtk_widget_get_parent (widget);
2393 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2396 _gtk_menu_item_refresh_accel_path (menu_item,
2397 menu->priv->accel_path,
2398 menu->priv->accel_group,
2401 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2402 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2406 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2407 const gchar *prefix,
2408 GtkAccelGroup *accel_group,
2409 gboolean group_changed)
2411 GtkMenuItemPrivate *priv = menu_item->priv;
2415 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2416 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2418 widget = GTK_WIDGET (menu_item);
2422 gtk_widget_set_accel_path (widget, NULL, NULL);
2426 path = _gtk_widget_get_accel_path (widget, NULL);
2427 if (!path) /* no active accel_path yet */
2429 path = priv->accel_path;
2430 if (!path && prefix)
2432 const gchar *postfix = NULL;
2435 /* try to construct one from label text */
2436 gtk_container_foreach (GTK_CONTAINER (menu_item),
2437 gtk_menu_item_accel_name_foreach,
2441 new_path = g_strconcat (prefix, "/", postfix, NULL);
2442 path = priv->accel_path = (char*)g_intern_string (new_path);
2447 gtk_widget_set_accel_path (widget, path, accel_group);
2449 else if (group_changed) /* reinstall accelerators */
2450 gtk_widget_set_accel_path (widget, path, accel_group);
2454 * gtk_menu_item_set_accel_path
2455 * @menu_item: a valid #GtkMenuItem
2456 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2457 * item's functionality, or %NULL to unset the current path.
2459 * Set the accelerator path on @menu_item, through which runtime
2460 * changes of the menu item's accelerator caused by the user can be
2461 * identified and saved to persistent storage (see gtk_accel_map_save()
2462 * on this). To set up a default accelerator for this menu item, call
2463 * gtk_accel_map_add_entry() with the same @accel_path. See also
2464 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2465 * and gtk_menu_set_accel_path() for a more convenient variant of
2468 * This function is basically a convenience wrapper that handles
2469 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2470 * group for the menu item.
2472 * Note that you do need to set an accelerator on the parent menu with
2473 * gtk_menu_set_accel_group() for this to work.
2475 * Note that @accel_path string will be stored in a #GQuark.
2476 * Therefore, if you pass a static string, you can save some memory
2477 * by interning it first with g_intern_static_string().
2480 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2481 const gchar *accel_path)
2483 GtkMenuItemPrivate *priv = menu_item->priv;
2487 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2488 g_return_if_fail (accel_path == NULL ||
2489 (accel_path[0] == '<' && strchr (accel_path, '/')));
2491 widget = GTK_WIDGET (menu_item);
2493 /* store new path */
2494 priv->accel_path = (char*)g_intern_string (accel_path);
2496 /* forget accelerators associated with old path */
2497 gtk_widget_set_accel_path (widget, NULL, NULL);
2499 /* install accelerators associated with new path */
2500 parent = gtk_widget_get_parent (widget);
2501 if (GTK_IS_MENU (parent))
2503 GtkMenu *menu = GTK_MENU (parent);
2505 if (menu->priv->accel_group)
2506 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2508 menu->priv->accel_group,
2514 * gtk_menu_item_get_accel_path
2515 * @menu_item: a valid #GtkMenuItem
2517 * Retrieve the accelerator path that was previously set on @menu_item.
2519 * See gtk_menu_item_set_accel_path() for details.
2521 * Returns: the accelerator path corresponding to this menu
2522 * item's functionality, or %NULL if not set
2527 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2529 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2531 return menu_item->priv->accel_path;
2535 gtk_menu_item_forall (GtkContainer *container,
2536 gboolean include_internals,
2537 GtkCallback callback,
2538 gpointer callback_data)
2542 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2543 g_return_if_fail (callback != NULL);
2545 child = gtk_bin_get_child (GTK_BIN (container));
2547 callback (child, callback_data);
2551 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2553 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2554 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2555 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2556 !gtk_widget_is_sensitive (menu_item) ||
2557 !gtk_widget_get_visible (menu_item))
2564 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2566 GtkWidget *accel_label;
2568 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2570 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2571 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2573 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2574 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2575 GTK_WIDGET (menu_item));
2576 gtk_widget_show (accel_label);
2581 * gtk_menu_item_set_label:
2582 * @menu_item: a #GtkMenuItem
2583 * @label: the text you want to set
2585 * Sets @text on the @menu_item label
2590 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2593 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2595 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2599 * gtk_menu_item_get_label:
2600 * @menu_item: a #GtkMenuItem
2602 * Sets @text on the @menu_item label
2604 * Returns: The text in the @menu_item label. This is the internal
2605 * string used by the label, and must not be modified.
2610 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2612 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2614 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2618 * gtk_menu_item_set_use_underline:
2619 * @menu_item: a #GtkMenuItem
2620 * @setting: %TRUE if underlines in the text indicate mnemonics
2622 * If true, an underline in the text indicates the next character
2623 * should be used for the mnemonic accelerator key.
2628 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2633 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2635 gtk_menu_item_ensure_label (menu_item);
2637 child = gtk_bin_get_child (GTK_BIN (menu_item));
2638 if (GTK_IS_LABEL (child))
2640 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2642 g_object_notify (G_OBJECT (menu_item), "use-underline");
2647 * gtk_menu_item_get_use_underline:
2648 * @menu_item: a #GtkMenuItem
2650 * Checks if an underline in the text indicates the next character
2651 * should be used for the mnemonic accelerator key.
2653 * Return value: %TRUE if an embedded underline in the label
2654 * indicates the mnemonic accelerator key.
2659 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2663 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2665 gtk_menu_item_ensure_label (menu_item);
2667 child = gtk_bin_get_child (GTK_BIN (menu_item));
2668 if (GTK_IS_LABEL (child))
2669 return gtk_label_get_use_underline (GTK_LABEL (child));
2675 * gtk_menu_item_set_reserve_indicator:
2676 * @menu_item: a #GtkMenuItem
2677 * @reserve: the new value
2679 * Sets whether the @menu_item should reserve space for
2680 * the submenu indicator, regardless if it actually has
2683 * There should be little need for applications to call
2689 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2692 GtkMenuItemPrivate *priv;
2694 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2696 priv = menu_item->priv;
2698 if (priv->reserve_indicator != reserve)
2700 priv->reserve_indicator = reserve;
2701 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2706 * gtk_menu_item_get_reserve_indicator:
2707 * @menu_item: a #GtkMenuItem
2709 * Returns whether the @menu_item reserves space for
2710 * the submenu indicator, regardless if it has a submenu
2713 * Returns: %TRUE if @menu_item always reserves space for the
2719 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2721 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2723 return menu_item->priv->reserve_indicator;