1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkaccellabel.h"
33 #include "gtkmarshalers.h"
34 #include "gtkmenuprivate.h"
35 #include "gtkmenushellprivate.h"
36 #include "gtkmenuitemprivate.h"
37 #include "gtkmenubar.h"
38 #include "gtkmenuprivate.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtkprivate.h"
41 #include "gtkbuildable.h"
42 #include "gtkactivatable.h"
43 #include "gtkwidgetprivate.h"
45 #include "gtktypebuiltins.h"
46 #include "a11y/gtkmenuitemaccessible.h"
51 * @Short_description: The widget used for item in menus
53 * @See_also: #GtkBin, #GtkMenuShell
55 * The #GtkMenuItem widget and the derived widgets are the only valid
56 * childs for menus. Their function is to correctly handle highlighting,
57 * alignment, events and submenus.
59 * As it derives from #GtkBin it can hold any valid child widget, altough
60 * only a few are really useful.
62 * <refsect2 id="GtkMenuItem-BUILDER-UI">
63 * <title>GtkMenuItem as GtkBuildable</title>
64 * The GtkMenuItem implementation of the GtkBuildable interface
65 * supports adding a submenu by specifying "submenu" as the "type"
66 * attribute of a <child> element.
68 * <title>A UI definition fragment with submenus</title>
69 * <programlisting><![CDATA[
70 * <object class="GtkMenuItem">
71 * <child type="submenu">
72 * <object class="GtkMenu"/>
75 * ]]></programlisting>
99 /* activatable properties */
100 PROP_ACTIVATABLE_RELATED_ACTION,
101 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
105 static void gtk_menu_item_dispose (GObject *object);
106 static void gtk_menu_item_set_property (GObject *object,
110 static void gtk_menu_item_get_property (GObject *object,
114 static void gtk_menu_item_destroy (GtkWidget *widget);
115 static void gtk_menu_item_size_allocate (GtkWidget *widget,
116 GtkAllocation *allocation);
117 static void gtk_menu_item_realize (GtkWidget *widget);
118 static void gtk_menu_item_unrealize (GtkWidget *widget);
119 static void gtk_menu_item_map (GtkWidget *widget);
120 static void gtk_menu_item_unmap (GtkWidget *widget);
121 static gboolean gtk_menu_item_enter (GtkWidget *widget,
122 GdkEventCrossing *event);
123 static gboolean gtk_menu_item_leave (GtkWidget *widget,
124 GdkEventCrossing *event);
125 static gboolean gtk_menu_item_draw (GtkWidget *widget,
127 static void gtk_menu_item_parent_set (GtkWidget *widget,
128 GtkWidget *previous_parent);
131 static void gtk_real_menu_item_select (GtkMenuItem *item);
132 static void gtk_real_menu_item_deselect (GtkMenuItem *item);
133 static void gtk_real_menu_item_activate (GtkMenuItem *item);
134 static void gtk_real_menu_item_activate_item (GtkMenuItem *item);
135 static void gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
137 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
139 static gboolean gtk_menu_item_mnemonic_activate (GtkWidget *widget,
140 gboolean group_cycling);
142 static void gtk_menu_item_ensure_label (GtkMenuItem *menu_item);
143 static gint gtk_menu_item_popup_timeout (gpointer data);
144 static void gtk_menu_item_position_menu (GtkMenu *menu,
149 static void gtk_menu_item_show_all (GtkWidget *widget);
150 static void gtk_menu_item_forall (GtkContainer *container,
151 gboolean include_internals,
152 GtkCallback callback,
153 gpointer callback_data);
154 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
157 static void gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
159 static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
161 static void gtk_menu_item_get_preferred_width (GtkWidget *widget,
164 static void gtk_menu_item_get_preferred_height (GtkWidget *widget,
167 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
172 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface);
173 static void gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
177 static void gtk_menu_item_buildable_custom_finished(GtkBuildable *buildable,
180 const gchar *tagname,
183 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface);
184 static void gtk_menu_item_update (GtkActivatable *activatable,
186 const gchar *property_name);
187 static void gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
189 static void gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
191 static void gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
192 gboolean use_appearance);
194 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
196 static GtkBuildableIface *parent_buildable_iface;
198 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
199 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
200 gtk_menu_item_buildable_interface_init)
201 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
202 gtk_menu_item_activatable_interface_init))
205 gtk_menu_item_class_init (GtkMenuItemClass *klass)
207 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
209 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
211 gobject_class->dispose = gtk_menu_item_dispose;
212 gobject_class->set_property = gtk_menu_item_set_property;
213 gobject_class->get_property = gtk_menu_item_get_property;
215 widget_class->destroy = gtk_menu_item_destroy;
216 widget_class->size_allocate = gtk_menu_item_size_allocate;
217 widget_class->draw = gtk_menu_item_draw;
218 widget_class->realize = gtk_menu_item_realize;
219 widget_class->unrealize = gtk_menu_item_unrealize;
220 widget_class->map = gtk_menu_item_map;
221 widget_class->unmap = gtk_menu_item_unmap;
222 widget_class->enter_notify_event = gtk_menu_item_enter;
223 widget_class->leave_notify_event = gtk_menu_item_leave;
224 widget_class->show_all = gtk_menu_item_show_all;
225 widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
226 widget_class->parent_set = gtk_menu_item_parent_set;
227 widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
228 widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
229 widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
230 widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
232 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
234 container_class->forall = gtk_menu_item_forall;
236 klass->activate = gtk_real_menu_item_activate;
237 klass->activate_item = gtk_real_menu_item_activate_item;
238 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
239 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
240 klass->set_label = gtk_real_menu_item_set_label;
241 klass->get_label = gtk_real_menu_item_get_label;
242 klass->select = gtk_real_menu_item_select;
243 klass->deselect = gtk_real_menu_item_deselect;
245 klass->hide_on_activate = TRUE;
248 * GtkMenuItem::activate:
249 * @menuitem: the object which received the signal.
251 * Emitted when the item is activated.
253 menu_item_signals[ACTIVATE] =
254 g_signal_new (I_("activate"),
255 G_OBJECT_CLASS_TYPE (gobject_class),
256 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
257 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
259 _gtk_marshal_VOID__VOID,
261 widget_class->activate_signal = menu_item_signals[ACTIVATE];
264 * GtkMenuItem::activate-item:
265 * @menuitem: the object which received the signal.
267 * Emitted when the item is activated, but also if the menu item has a
268 * submenu. For normal applications, the relevant signal is
269 * #GtkMenuItem::activate.
271 menu_item_signals[ACTIVATE_ITEM] =
272 g_signal_new (I_("activate-item"),
273 G_OBJECT_CLASS_TYPE (gobject_class),
275 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
277 _gtk_marshal_VOID__VOID,
280 menu_item_signals[TOGGLE_SIZE_REQUEST] =
281 g_signal_new (I_("toggle-size-request"),
282 G_OBJECT_CLASS_TYPE (gobject_class),
284 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
286 _gtk_marshal_VOID__POINTER,
290 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
291 g_signal_new (I_("toggle-size-allocate"),
292 G_OBJECT_CLASS_TYPE (gobject_class),
294 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
296 _gtk_marshal_VOID__INT,
300 menu_item_signals[SELECT] =
301 g_signal_new (I_("select"),
302 G_OBJECT_CLASS_TYPE (gobject_class),
304 G_STRUCT_OFFSET (GtkMenuItemClass, select),
306 _gtk_marshal_VOID__VOID,
309 menu_item_signals[DESELECT] =
310 g_signal_new (I_("deselect"),
311 G_OBJECT_CLASS_TYPE (gobject_class),
313 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
315 _gtk_marshal_VOID__VOID,
319 * GtkMenuItem:right-justified:
321 * Sets whether the menu item appears justified
322 * at the right side of a menu bar.
326 g_object_class_install_property (gobject_class,
327 PROP_RIGHT_JUSTIFIED,
328 g_param_spec_boolean ("right-justified",
329 P_("Right Justified"),
330 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
332 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
335 * GtkMenuItem:submenu:
337 * The submenu attached to the menu item, or %NULL if it has none.
341 g_object_class_install_property (gobject_class,
343 g_param_spec_object ("submenu",
345 P_("The submenu attached to the menu item, or NULL if it has none"),
347 GTK_PARAM_READWRITE));
350 * GtkMenuItem:accel-path:
352 * Sets the accelerator path of the menu item, through which runtime
353 * changes of the menu item's accelerator caused by the user can be
354 * identified and saved to persistant storage.
358 g_object_class_install_property (gobject_class,
360 g_param_spec_string ("accel-path",
362 P_("Sets the accelerator path of the menu item"),
364 GTK_PARAM_READWRITE));
369 * The text for the child label.
373 g_object_class_install_property (gobject_class,
375 g_param_spec_string ("label",
377 P_("The text for the child label"),
379 GTK_PARAM_READWRITE));
382 * GtkMenuItem:use-underline:
384 * %TRUE if underlines in the text indicate mnemonics.
388 g_object_class_install_property (gobject_class,
390 g_param_spec_boolean ("use-underline",
392 P_("If set, an underline in the text indicates "
393 "the next character should be used for the "
394 "mnemonic accelerator key"),
396 GTK_PARAM_READWRITE));
398 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
399 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
401 gtk_widget_class_install_style_property_parser (widget_class,
402 g_param_spec_enum ("selected-shadow-type",
403 "Selected Shadow Type",
404 "Shadow type when item is selected",
405 GTK_TYPE_SHADOW_TYPE,
408 gtk_rc_property_parse_enum);
410 gtk_widget_class_install_style_property (widget_class,
411 g_param_spec_int ("horizontal-padding",
412 "Horizontal Padding",
413 "Padding to left and right of the menu item",
417 GTK_PARAM_READABLE));
419 gtk_widget_class_install_style_property (widget_class,
420 g_param_spec_int ("toggle-spacing",
422 "Space between icon and label",
426 GTK_PARAM_READABLE));
428 gtk_widget_class_install_style_property (widget_class,
429 g_param_spec_int ("arrow-spacing",
431 "Space between label and arrow",
435 GTK_PARAM_READABLE));
437 gtk_widget_class_install_style_property (widget_class,
438 g_param_spec_float ("arrow-scaling",
440 P_("Amount of space used up by arrow, relative to the menu item's font size"),
442 GTK_PARAM_READABLE));
445 * GtkMenuItem:width-chars:
447 * The minimum desired width of the menu item in characters.
451 gtk_widget_class_install_style_property (widget_class,
452 g_param_spec_int ("width-chars",
453 P_("Width in Characters"),
454 P_("The minimum desired width of the menu item in characters"),
456 GTK_PARAM_READABLE));
458 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
462 gtk_menu_item_init (GtkMenuItem *menu_item)
464 GtkStyleContext *context;
465 GtkMenuItemPrivate *priv;
467 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
470 menu_item->priv = priv;
472 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
475 priv->use_action_appearance = TRUE;
477 menu_item->priv->submenu = NULL;
478 menu_item->priv->toggle_size = 0;
479 menu_item->priv->accelerator_width = 0;
480 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
481 priv->submenu_direction = GTK_DIRECTION_LEFT;
483 priv->submenu_direction = GTK_DIRECTION_RIGHT;
484 priv->submenu_placement = GTK_TOP_BOTTOM;
485 priv->right_justify = FALSE;
486 priv->use_action_appearance = TRUE;
490 context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
491 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
497 * Creates a new #GtkMenuItem.
499 * Returns: the newly created #GtkMenuItem
502 gtk_menu_item_new (void)
504 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
508 * gtk_menu_item_new_with_label:
509 * @label: the text for the label
511 * Creates a new #GtkMenuItem whose child is a #GtkLabel.
513 * Returns: the newly created #GtkMenuItem
516 gtk_menu_item_new_with_label (const gchar *label)
518 return g_object_new (GTK_TYPE_MENU_ITEM,
525 * gtk_menu_item_new_with_mnemonic:
526 * @label: The text of the button, with an underscore in front of the
529 * Creates a new #GtkMenuItem containing a label.
531 * The label will be created using gtk_label_new_with_mnemonic(),
532 * so underscores in @label indicate the mnemonic for the menu item.
534 * Returns: a new #GtkMenuItem
537 gtk_menu_item_new_with_mnemonic (const gchar *label)
539 return g_object_new (GTK_TYPE_MENU_ITEM,
540 "use-underline", TRUE,
546 gtk_menu_item_dispose (GObject *object)
548 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
549 GtkMenuItemPrivate *priv = menu_item->priv;
553 gtk_action_disconnect_accelerator (priv->action);
554 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
557 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
561 gtk_menu_item_do_set_right_justified (GtkMenuItem *menu_item,
562 gboolean right_justified)
564 GtkMenuItemPrivate *priv = menu_item->priv;
566 right_justified = right_justified != FALSE;
568 if (priv->right_justify != right_justified)
570 priv->right_justify = right_justified;
571 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
576 gtk_menu_item_set_property (GObject *object,
581 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
585 case PROP_RIGHT_JUSTIFIED:
586 gtk_menu_item_do_set_right_justified (menu_item, g_value_get_boolean (value));
589 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
591 case PROP_ACCEL_PATH:
592 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
595 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
597 case PROP_USE_UNDERLINE:
598 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
600 case PROP_ACTIVATABLE_RELATED_ACTION:
601 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
603 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
604 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
607 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
613 gtk_menu_item_get_property (GObject *object,
618 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
619 GtkMenuItemPrivate *priv = menu_item->priv;
623 case PROP_RIGHT_JUSTIFIED:
624 g_value_set_boolean (value, menu_item->priv->right_justify);
627 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
629 case PROP_ACCEL_PATH:
630 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
633 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
635 case PROP_USE_UNDERLINE:
636 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
638 case PROP_ACTIVATABLE_RELATED_ACTION:
639 g_value_set_object (value, priv->action);
641 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
642 g_value_set_boolean (value, priv->use_action_appearance);
645 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651 gtk_menu_item_destroy (GtkWidget *widget)
653 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
654 GtkMenuItemPrivate *priv = menu_item->priv;
657 gtk_widget_destroy (priv->submenu);
659 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
663 gtk_menu_item_detacher (GtkWidget *widget,
666 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
667 GtkMenuItemPrivate *priv = menu_item->priv;
669 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
671 priv->submenu = NULL;
675 get_arrow_size (GtkWidget *widget,
679 GtkStyleContext *style_context;
681 PangoContext *context;
682 PangoFontMetrics *metrics;
683 gfloat arrow_scaling;
687 gtk_widget_style_get (widget,
688 "arrow-scaling", &arrow_scaling,
691 context = gtk_widget_get_pango_context (child);
692 style_context = gtk_widget_get_style_context (child);
693 state = gtk_widget_get_state_flags (child);
695 metrics = pango_context_get_metrics (context,
696 gtk_style_context_get_font (style_context, state),
697 pango_context_get_language (context));
699 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
700 pango_font_metrics_get_descent (metrics)));
702 pango_font_metrics_unref (metrics);
704 *size = *size * arrow_scaling;
709 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
714 if (GTK_IS_ACCEL_LABEL (widget))
718 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
719 *width = MAX (*width, w);
721 else if (GTK_IS_CONTAINER (widget))
722 gtk_container_foreach (GTK_CONTAINER (widget),
723 gtk_menu_item_accel_width_foreach,
728 get_minimum_width (GtkWidget *widget)
730 GtkStyleContext *style_context;
732 PangoContext *context;
733 PangoFontMetrics *metrics;
737 context = gtk_widget_get_pango_context (widget);
738 style_context = gtk_widget_get_style_context (widget);
739 state = gtk_widget_get_state_flags (widget);
741 metrics = pango_context_get_metrics (context,
742 gtk_style_context_get_font (style_context, state),
743 pango_context_get_language (context));
745 width = pango_font_metrics_get_approximate_char_width (metrics);
747 pango_font_metrics_unref (metrics);
749 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
751 return PANGO_PIXELS (width_chars * width);
755 gtk_menu_item_get_preferred_width (GtkWidget *widget,
759 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
760 GtkMenuItemPrivate *priv = menu_item->priv;
765 guint horizontal_padding;
767 GtkPackDirection pack_dir;
768 GtkPackDirection child_pack_dir;
769 gint min_width, nat_width;
770 GtkStyleContext *context;
774 min_width = nat_width = 0;
776 gtk_widget_style_get (widget,
777 "horizontal-padding", &horizontal_padding,
780 bin = GTK_BIN (widget);
781 parent = gtk_widget_get_parent (widget);
783 if (GTK_IS_MENU_BAR (parent))
785 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
786 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
790 pack_dir = GTK_PACK_DIRECTION_LTR;
791 child_pack_dir = GTK_PACK_DIRECTION_LTR;
794 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
796 context = gtk_widget_get_style_context (widget);
797 state = gtk_widget_get_state_flags (widget);
798 gtk_style_context_get_padding (context, state, &padding);
800 min_width = (border_width * 2) + padding.left + padding.right;
802 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
803 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
804 min_width += 2 * horizontal_padding;
806 nat_width = min_width;
808 child = gtk_bin_get_child (bin);
810 if (child != NULL && gtk_widget_get_visible (child))
812 GtkMenuItemPrivate *priv = menu_item->priv;
813 gint child_min, child_nat;
815 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
817 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
822 gtk_widget_style_get (widget,
823 "arrow-spacing", &arrow_spacing,
826 get_arrow_size (widget, child, &arrow_size);
828 gtk_widget_style_get (widget,
829 "arrow-spacing", &arrow_spacing,
832 get_arrow_size (widget, child, &arrow_size);
834 min_width += arrow_size;
835 min_width += arrow_spacing;
837 min_width = MAX (min_width, get_minimum_width (widget));
838 nat_width = min_width;
841 min_width += child_min;
842 nat_width += child_nat;
846 gtk_container_foreach (GTK_CONTAINER (menu_item),
847 gtk_menu_item_accel_width_foreach,
849 priv->accelerator_width = accel_width;
852 *minimum_size = min_width;
855 *natural_size = nat_width;
859 gtk_menu_item_get_preferred_height (GtkWidget *widget,
863 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
864 GtkMenuItemPrivate *priv = menu_item->priv;
866 GtkStyleContext *context;
872 guint horizontal_padding;
874 GtkPackDirection pack_dir;
875 GtkPackDirection child_pack_dir;
876 gint min_height, nat_height;
878 min_height = nat_height = 0;
880 context = gtk_widget_get_style_context (widget);
881 state = gtk_widget_get_state_flags (widget);
882 gtk_style_context_get_padding (context, state, &padding);
884 gtk_widget_style_get (widget,
885 "horizontal-padding", &horizontal_padding,
888 bin = GTK_BIN (widget);
889 parent = gtk_widget_get_parent (widget);
891 if (GTK_IS_MENU_BAR (parent))
893 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
894 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
898 pack_dir = GTK_PACK_DIRECTION_LTR;
899 child_pack_dir = GTK_PACK_DIRECTION_LTR;
902 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
903 min_height = (border_width * 2) + padding.top + padding.bottom;
905 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
906 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
907 min_height += 2 * horizontal_padding;
909 nat_height = min_height;
911 child = gtk_bin_get_child (bin);
913 if (child != NULL && gtk_widget_get_visible (child))
915 GtkMenuItemPrivate *priv = menu_item->priv;
916 gint child_min, child_nat;
918 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
920 min_height += child_min;
921 nat_height += child_nat;
923 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
927 get_arrow_size (widget, child, &arrow_size);
929 min_height = MAX (min_height, arrow_size);
930 nat_height = MAX (nat_height, arrow_size);
933 else /* separator item */
935 gboolean wide_separators;
936 gint separator_height;
938 gtk_widget_style_get (widget,
939 "wide-separators", &wide_separators,
940 "separator-height", &separator_height,
945 min_height += separator_height;
946 nat_height += separator_height;
950 /* force odd, so that we can have the same space above and
953 if (min_height % 2 == 0)
955 if (nat_height % 2 == 0)
961 gtk_container_foreach (GTK_CONTAINER (menu_item),
962 gtk_menu_item_accel_width_foreach,
964 priv->accelerator_width = accel_width;
967 *minimum_size = min_height;
970 *natural_size = nat_height;
974 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
979 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
980 GtkMenuItemPrivate *priv = menu_item->priv;
982 GtkStyleContext *context;
987 guint horizontal_padding;
989 GtkPackDirection pack_dir;
990 GtkPackDirection child_pack_dir;
991 gint min_height, nat_height;
994 min_height = nat_height = 0;
996 context = gtk_widget_get_style_context (widget);
997 state = gtk_widget_get_state_flags (widget);
998 gtk_style_context_get_padding (context, state, &padding);
1000 gtk_widget_style_get (widget,
1001 "horizontal-padding", &horizontal_padding,
1004 bin = GTK_BIN (widget);
1005 parent = gtk_widget_get_parent (widget);
1007 if (GTK_IS_MENU_BAR (parent))
1009 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1010 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1014 pack_dir = GTK_PACK_DIRECTION_LTR;
1015 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1018 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1019 min_height = (border_width * 2) + padding.top + padding.bottom;
1021 avail_size = for_size;
1022 avail_size -= (border_width * 2) + padding.left + padding.right;
1024 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1025 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1026 min_height += 2 * horizontal_padding;
1028 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1029 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1030 avail_size -= 2 * horizontal_padding;
1032 nat_height = min_height;
1034 child = gtk_bin_get_child (bin);
1036 if (child != NULL && gtk_widget_get_visible (child))
1038 gint child_min, child_nat;
1039 gint arrow_size = 0;
1041 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1043 guint arrow_spacing;
1045 gtk_widget_style_get (widget,
1046 "arrow-spacing", &arrow_spacing,
1049 get_arrow_size (widget, child, &arrow_size);
1051 avail_size -= arrow_size;
1052 avail_size -= arrow_spacing;
1055 gtk_widget_get_preferred_height_for_width (child,
1060 min_height += child_min;
1061 nat_height += child_nat;
1063 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1065 min_height = MAX (min_height, arrow_size);
1066 nat_height = MAX (nat_height, arrow_size);
1069 else /* separator item */
1071 gboolean wide_separators;
1072 gint separator_height;
1074 gtk_widget_style_get (widget,
1075 "wide-separators", &wide_separators,
1076 "separator-height", &separator_height,
1079 if (wide_separators)
1081 min_height += separator_height;
1082 nat_height += separator_height;
1086 /* force odd, so that we can have the same space above and
1089 if (min_height % 2 == 0)
1091 if (nat_height % 2 == 0)
1097 *minimum_size = min_height;
1100 *natural_size = nat_height;
1104 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1106 parent_buildable_iface = g_type_interface_peek_parent (iface);
1107 iface->add_child = gtk_menu_item_buildable_add_child;
1108 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1112 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1113 GtkBuilder *builder,
1117 if (type && strcmp (type, "submenu") == 0)
1118 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1119 GTK_WIDGET (child));
1121 parent_buildable_iface->add_child (buildable, builder, child, type);
1126 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1127 GtkBuilder *builder,
1129 const gchar *tagname,
1132 GtkWidget *toplevel;
1134 if (strcmp (tagname, "accelerator") == 0)
1136 GtkMenuShell *menu_shell;
1139 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1142 while (GTK_IS_MENU (menu_shell) &&
1143 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1144 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1146 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1150 /* Fall back to something ... */
1151 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1153 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1154 gtk_buildable_get_name (buildable));
1157 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1158 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1161 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1166 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1168 iface->update = gtk_menu_item_update;
1169 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1173 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1177 child = gtk_bin_get_child (GTK_BIN (menu_item));
1179 if (GTK_IS_LABEL (child))
1183 label = gtk_action_get_label (action);
1184 gtk_menu_item_set_label (menu_item, label);
1188 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1191 gtk_menu_item_update (GtkActivatable *activatable,
1193 const gchar *property_name)
1195 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1196 GtkMenuItemPrivate *priv = menu_item->priv;
1198 if (strcmp (property_name, "visible") == 0)
1199 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1200 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1201 else if (strcmp (property_name, "sensitive") == 0)
1202 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1203 else if (priv->use_action_appearance)
1205 if (strcmp (property_name, "label") == 0)
1206 activatable_update_label (menu_item, action);
1211 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1214 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1215 GtkMenuItemPrivate *priv = menu_item->priv;
1218 if (!priv->use_action_appearance || !action)
1220 label = gtk_bin_get_child (GTK_BIN (menu_item));
1222 if (GTK_IS_ACCEL_LABEL (label))
1223 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1229 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1230 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1232 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1234 if (priv->use_action_appearance)
1236 label = gtk_bin_get_child (GTK_BIN (menu_item));
1238 /* make sure label is a label, deleting it otherwise */
1239 if (label && !GTK_IS_LABEL (label))
1241 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1244 /* Make sure that menu_item has a label and that any
1245 * accelerators are set */
1246 gtk_menu_item_ensure_label (menu_item);
1247 gtk_menu_item_set_use_underline (menu_item, TRUE);
1248 /* Make label point to the menu_item's label */
1249 label = gtk_bin_get_child (GTK_BIN (menu_item));
1251 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1253 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1254 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1255 gtk_action_get_accel_closure (action));
1258 activatable_update_label (menu_item, action);
1263 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1266 GtkMenuItemPrivate *priv = menu_item->priv;
1268 if (priv->action == action)
1273 gtk_action_disconnect_accelerator (priv->action);
1278 const gchar *accel_path;
1280 accel_path = gtk_action_get_accel_path (action);
1283 gtk_action_connect_accelerator (action);
1284 gtk_menu_item_set_accel_path (menu_item, accel_path);
1288 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1290 priv->action = action;
1294 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1295 gboolean use_appearance)
1297 GtkMenuItemPrivate *priv = menu_item->priv;
1299 if (priv->use_action_appearance != use_appearance)
1301 priv->use_action_appearance = use_appearance;
1303 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1309 * gtk_menu_item_set_submenu:
1310 * @menu_item: a #GtkMenuItem
1311 * @submenu: (allow-none): the submenu, or %NULL
1313 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1314 * submenu is passed.
1317 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1320 GtkMenuItemPrivate *priv = menu_item->priv;
1322 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1323 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1325 if (priv->submenu != submenu)
1328 gtk_menu_detach (GTK_MENU (priv->submenu));
1332 priv->submenu = submenu;
1333 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1334 GTK_WIDGET (menu_item),
1335 gtk_menu_item_detacher);
1338 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1339 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1341 g_object_notify (G_OBJECT (menu_item), "submenu");
1346 * gtk_menu_item_get_submenu:
1347 * @menu_item: a #GtkMenuItem
1349 * Gets the submenu underneath this menu item, if any.
1350 * See gtk_menu_item_set_submenu().
1352 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1355 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1357 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1359 return menu_item->priv->submenu;
1362 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1363 GtkSubmenuPlacement placement);
1366 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1367 GtkSubmenuPlacement placement)
1369 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1371 menu_item->priv->submenu_placement = placement;
1375 gtk_menu_item_select (GtkMenuItem *menu_item)
1379 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1381 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1383 /* Enable themeing of the parent menu item depending on whether
1384 * something is selected in its submenu
1386 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1387 if (GTK_IS_MENU (parent))
1389 GtkMenu *menu = GTK_MENU (parent);
1391 if (menu->priv->parent_menu_item)
1392 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1397 * gtk_menu_item_deselect:
1398 * @menu_item: the menu item
1400 * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1401 * exactly like #gtk_item_deselect.
1404 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1408 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1410 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1412 /* Enable themeing of the parent menu item depending on whether
1413 * something is selected in its submenu
1415 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1416 if (GTK_IS_MENU (parent))
1418 GtkMenu *menu = GTK_MENU (parent);
1420 if (menu->priv->parent_menu_item)
1421 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1426 * gtk_menu_item_activate:
1427 * @menu_item: the menu item
1429 * Emits the #GtkMenuItem::activate signal on the given item
1432 gtk_menu_item_activate (GtkMenuItem *menu_item)
1434 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1436 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1440 * gtk_menu_item_toggle_size_request:
1441 * @menu_item: the menu item
1442 * @requisition: the requisition to use as signal data.
1444 * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1447 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1450 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1452 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1456 * gtk_menu_item_toggle_size_allocate:
1457 * @menu_item: the menu item.
1458 * @allocation: the allocation to use as signal data.
1460 * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1463 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1466 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1468 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1472 gtk_menu_item_size_allocate (GtkWidget *widget,
1473 GtkAllocation *allocation)
1475 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1476 GtkMenuItemPrivate *priv = menu_item->priv;
1478 GtkAllocation child_allocation;
1479 GtkTextDirection direction;
1480 GtkPackDirection pack_dir;
1481 GtkPackDirection child_pack_dir;
1485 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1486 g_return_if_fail (allocation != NULL);
1488 bin = GTK_BIN (widget);
1490 direction = gtk_widget_get_direction (widget);
1492 parent = gtk_widget_get_parent (widget);
1493 if (GTK_IS_MENU_BAR (parent))
1495 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1496 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1500 pack_dir = GTK_PACK_DIRECTION_LTR;
1501 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1504 gtk_widget_set_allocation (widget, allocation);
1506 child = gtk_bin_get_child (bin);
1509 GtkStyleContext *context;
1510 GtkStateFlags state;
1512 guint horizontal_padding;
1515 context = gtk_widget_get_style_context (widget);
1516 state = gtk_widget_get_state_flags (widget);
1517 gtk_style_context_get_padding (context, state, &padding);
1519 gtk_widget_style_get (widget,
1520 "horizontal-padding", &horizontal_padding,
1523 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1524 child_allocation.x = border_width + padding.left;
1525 child_allocation.y = border_width + padding.top;
1527 child_allocation.width = allocation->width - (border_width * 2) -
1528 padding.left - padding.right;
1529 child_allocation.height = allocation->height - (border_width * 2) -
1530 padding.top - padding.bottom;
1532 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1533 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1535 child_allocation.x += horizontal_padding;
1536 child_allocation.width -= 2 * horizontal_padding;
1538 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1539 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1541 child_allocation.y += horizontal_padding;
1542 child_allocation.height -= 2 * horizontal_padding;
1545 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1546 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1548 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1549 child_allocation.x += priv->toggle_size;
1550 child_allocation.width -= priv->toggle_size;
1554 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1555 child_allocation.y += priv->toggle_size;
1556 child_allocation.height -= priv->toggle_size;
1559 child_allocation.x += allocation->x;
1560 child_allocation.y += allocation->y;
1562 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1564 guint arrow_spacing;
1567 gtk_widget_style_get (widget,
1568 "arrow-spacing", &arrow_spacing,
1571 get_arrow_size (widget, child, &arrow_size);
1573 if (direction == GTK_TEXT_DIR_RTL)
1574 child_allocation.x += arrow_size + arrow_spacing;
1575 child_allocation.width -= arrow_size + arrow_spacing;
1578 if (child_allocation.width < 1)
1579 child_allocation.width = 1;
1581 gtk_widget_size_allocate (child, &child_allocation);
1584 if (gtk_widget_get_realized (widget))
1585 gdk_window_move_resize (priv->event_window,
1586 allocation->x, allocation->y,
1587 allocation->width, allocation->height);
1590 gtk_menu_reposition (GTK_MENU (priv->submenu));
1594 gtk_menu_item_realize (GtkWidget *widget)
1596 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1597 GtkMenuItemPrivate *priv = menu_item->priv;
1598 GtkAllocation allocation;
1600 GdkWindowAttr attributes;
1601 gint attributes_mask;
1603 gtk_widget_set_realized (widget, TRUE);
1605 window = gtk_widget_get_parent_window (widget);
1606 gtk_widget_set_window (widget, window);
1607 g_object_ref (window);
1609 gtk_widget_get_allocation (widget, &allocation);
1611 attributes.x = allocation.x;
1612 attributes.y = allocation.y;
1613 attributes.width = allocation.width;
1614 attributes.height = allocation.height;
1615 attributes.window_type = GDK_WINDOW_CHILD;
1616 attributes.wclass = GDK_INPUT_ONLY;
1617 attributes.event_mask = (gtk_widget_get_events (widget) |
1618 GDK_BUTTON_PRESS_MASK |
1619 GDK_BUTTON_RELEASE_MASK |
1620 GDK_ENTER_NOTIFY_MASK |
1621 GDK_LEAVE_NOTIFY_MASK |
1622 GDK_POINTER_MOTION_MASK);
1624 attributes_mask = GDK_WA_X | GDK_WA_Y;
1626 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1627 &attributes, attributes_mask);
1628 gdk_window_set_user_data (priv->event_window, widget);
1632 gtk_menu_item_unrealize (GtkWidget *widget)
1634 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1635 GtkMenuItemPrivate *priv = menu_item->priv;
1637 gdk_window_set_user_data (priv->event_window, NULL);
1638 gdk_window_destroy (priv->event_window);
1639 priv->event_window = NULL;
1641 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1645 gtk_menu_item_map (GtkWidget *widget)
1647 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1648 GtkMenuItemPrivate *priv = menu_item->priv;
1650 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1652 gdk_window_show (priv->event_window);
1656 gtk_menu_item_unmap (GtkWidget *widget)
1658 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1659 GtkMenuItemPrivate *priv = menu_item->priv;
1661 gdk_window_hide (priv->event_window);
1663 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1667 gtk_menu_item_enter (GtkWidget *widget,
1668 GdkEventCrossing *event)
1670 g_return_val_if_fail (event != NULL, FALSE);
1672 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1676 gtk_menu_item_leave (GtkWidget *widget,
1677 GdkEventCrossing *event)
1679 g_return_val_if_fail (event != NULL, FALSE);
1681 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1685 gtk_menu_item_draw (GtkWidget *widget,
1688 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1689 GtkMenuItemPrivate *priv = menu_item->priv;
1690 GtkStateFlags state;
1691 GtkStyleContext *context;
1693 GtkWidget *child, *parent;
1694 gint x, y, w, h, width, height;
1695 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1697 state = gtk_widget_get_state_flags (widget);
1698 context = gtk_widget_get_style_context (widget);
1699 width = gtk_widget_get_allocated_width (widget);
1700 height = gtk_widget_get_allocated_height (widget);
1704 w = width - border_width * 2;
1705 h = height - border_width * 2;
1707 child = gtk_bin_get_child (GTK_BIN (menu_item));
1708 parent = gtk_widget_get_parent (widget);
1710 gtk_style_context_save (context);
1711 gtk_style_context_set_state (context, state);
1713 gtk_style_context_get_padding (context, state, &padding);
1715 if (GTK_IS_MENU_BAR (parent))
1716 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1718 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1720 gtk_render_background (context, cr, x, y, w, h);
1721 gtk_render_frame (context, cr, x, y, w, h);
1724 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1726 gint arrow_x, arrow_y;
1728 guint horizontal_padding;
1729 GtkTextDirection direction;
1732 direction = gtk_widget_get_direction (widget);
1734 gtk_widget_style_get (widget,
1735 "horizontal-padding", &horizontal_padding,
1738 get_arrow_size (widget, child, &arrow_size);
1740 if (direction == GTK_TEXT_DIR_LTR)
1742 arrow_x = x + w - horizontal_padding - arrow_size;
1747 arrow_x = x + horizontal_padding;
1748 angle = (3 * G_PI) / 2;
1751 arrow_y = y + (h - arrow_size) / 2;
1753 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1757 gboolean wide_separators;
1758 gint separator_height;
1759 guint horizontal_padding;
1761 gtk_widget_style_get (widget,
1762 "wide-separators", &wide_separators,
1763 "separator-height", &separator_height,
1764 "horizontal-padding", &horizontal_padding,
1766 if (wide_separators)
1767 gtk_render_frame (context, cr,
1768 x + horizontal_padding + padding.left,
1770 w - (2 * horizontal_padding) - padding.left - padding.right,
1773 gtk_render_line (context, cr,
1774 x + horizontal_padding + padding.left,
1776 x + w - horizontal_padding - padding.right - 1,
1780 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1782 gtk_style_context_restore (context);
1788 * gtk_menu_item_select:
1789 * @menu_item: the menu item
1791 * Emits the #GtkMenuItem::select signal on the given item. Behaves
1792 * exactly like #gtk_item_select.
1795 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1797 GtkMenuItemPrivate *priv = menu_item->priv;
1798 gboolean touchscreen_mode;
1800 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1801 "gtk-touchscreen-mode", &touchscreen_mode,
1804 if (!touchscreen_mode && priv->submenu &&
1805 (!gtk_widget_get_mapped (priv->submenu) ||
1806 GTK_MENU (priv->submenu)->priv->tearoff_active))
1808 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1811 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1812 GTK_STATE_FLAG_PRELIGHT, FALSE);
1813 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1817 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1819 GtkMenuItemPrivate *priv = menu_item->priv;
1822 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1824 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1825 GTK_STATE_FLAG_PRELIGHT);
1826 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1830 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1831 gboolean group_cycling)
1835 parent = gtk_widget_get_parent (widget);
1837 if (GTK_IS_MENU_SHELL (parent))
1838 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1840 if (group_cycling &&
1842 GTK_IS_MENU_SHELL (parent) &&
1843 GTK_MENU_SHELL (parent)->priv->active)
1845 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1848 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1854 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1856 GtkMenuItemPrivate *priv = menu_item->priv;
1859 gtk_action_activate (priv->action);
1864 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1866 GtkMenuItemPrivate *priv = menu_item->priv;
1870 widget = GTK_WIDGET (menu_item);
1871 parent = gtk_widget_get_parent (widget);
1873 if (parent && GTK_IS_MENU_SHELL (parent))
1875 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1877 if (priv->submenu == NULL)
1878 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1881 gtk_menu_shell_select_item (menu_shell, widget);
1882 _gtk_menu_item_popup_submenu (widget, FALSE);
1884 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1890 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1893 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1899 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1902 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1904 menu_item->priv->toggle_size = allocation;
1908 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1913 gtk_menu_item_ensure_label (menu_item);
1915 child = gtk_bin_get_child (GTK_BIN (menu_item));
1916 if (GTK_IS_LABEL (child))
1918 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1920 g_object_notify (G_OBJECT (menu_item), "label");
1924 static const gchar *
1925 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1929 gtk_menu_item_ensure_label (menu_item);
1931 child = gtk_bin_get_child (GTK_BIN (menu_item));
1932 if (GTK_IS_LABEL (child))
1933 return gtk_label_get_label (GTK_LABEL (child));
1939 free_timeval (GTimeVal *val)
1941 g_slice_free (GTimeVal, val);
1945 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1946 gboolean remember_exact_time)
1948 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1949 GtkMenuItemPrivate *priv = menu_item->priv;
1952 parent = gtk_widget_get_parent (widget);
1954 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1956 gboolean take_focus;
1957 GtkMenuPositionFunc menu_position_func;
1959 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1960 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1962 if (remember_exact_time)
1964 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1966 g_get_current_time (popup_time);
1968 g_object_set_data_full (G_OBJECT (priv->submenu),
1969 "gtk-menu-exact-popup-time", popup_time,
1970 (GDestroyNotify) free_timeval);
1974 g_object_set_data (G_OBJECT (priv->submenu),
1975 "gtk-menu-exact-popup-time", NULL);
1978 /* gtk_menu_item_position_menu positions the submenu from the
1979 * menuitems position. If the menuitem doesn't have a window,
1980 * that doesn't work. In that case we use the default
1981 * positioning function instead which places the submenu at the
1984 if (gtk_widget_get_window (widget))
1985 menu_position_func = gtk_menu_item_position_menu;
1987 menu_position_func = NULL;
1989 gtk_menu_popup (GTK_MENU (priv->submenu),
1994 GTK_MENU_SHELL (parent)->priv->button,
1998 /* Enable themeing of the parent menu item depending on whether
1999 * its submenu is shown or not.
2001 gtk_widget_queue_draw (widget);
2005 gtk_menu_item_popup_timeout (gpointer data)
2007 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
2008 GtkMenuItemPrivate *priv = menu_item->priv;
2011 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
2013 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
2014 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
2016 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
2017 if (priv->timer_from_keypress && priv->submenu)
2018 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
2027 get_popup_delay (GtkWidget *widget)
2031 parent = gtk_widget_get_parent (widget);
2032 if (GTK_IS_MENU_SHELL (parent))
2034 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
2040 g_object_get (gtk_widget_get_settings (widget),
2041 "gtk-menu-popup-delay", &popup_delay,
2049 _gtk_menu_item_popup_submenu (GtkWidget *widget,
2050 gboolean with_delay)
2052 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2053 GtkMenuItemPrivate *priv = menu_item->priv;
2057 g_source_remove (priv->timer);
2064 gint popup_delay = get_popup_delay (widget);
2066 if (popup_delay > 0)
2068 GdkEvent *event = gtk_get_current_event ();
2070 priv->timer = gdk_threads_add_timeout (popup_delay,
2071 gtk_menu_item_popup_timeout,
2075 event->type != GDK_BUTTON_PRESS &&
2076 event->type != GDK_ENTER_NOTIFY)
2077 priv->timer_from_keypress = TRUE;
2079 priv->timer_from_keypress = FALSE;
2082 gdk_event_free (event);
2088 gtk_menu_item_real_popup_submenu (widget, FALSE);
2092 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
2094 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2095 GtkMenuItemPrivate *priv = menu_item->priv;
2099 g_object_set_data (G_OBJECT (priv->submenu),
2100 "gtk-menu-exact-popup-time", NULL);
2104 g_source_remove (priv->timer);
2108 gtk_menu_popdown (GTK_MENU (priv->submenu));
2110 gtk_widget_queue_draw (widget);
2115 get_offsets (GtkMenu *menu,
2116 gint *horizontal_offset,
2117 gint *vertical_offset)
2119 gint vertical_padding;
2120 gint horizontal_padding;
2121 GtkStyleContext *context;
2122 GtkStateFlags state;
2125 gtk_widget_style_get (GTK_WIDGET (menu),
2126 "horizontal-offset", horizontal_offset,
2127 "vertical-offset", vertical_offset,
2128 "horizontal-padding", &horizontal_padding,
2129 "vertical-padding", &vertical_padding,
2132 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2133 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2134 gtk_style_context_get_padding (context, state, &padding);
2136 *vertical_offset -= padding.top;
2137 *vertical_offset -= vertical_padding;
2138 *horizontal_offset += horizontal_padding;
2142 gtk_menu_item_position_menu (GtkMenu *menu,
2148 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2149 GtkMenuItemPrivate *priv = menu_item->priv;
2150 GtkAllocation allocation;
2152 GtkMenuItem *parent_menu_item;
2155 gint twidth, theight;
2157 GtkTextDirection direction;
2158 GdkRectangle monitor;
2160 gint horizontal_offset;
2161 gint vertical_offset;
2162 gint available_left, available_right;
2163 GtkStyleContext *context;
2164 GtkStateFlags state;
2165 GtkBorder parent_padding;
2167 g_return_if_fail (menu != NULL);
2168 g_return_if_fail (x != NULL);
2169 g_return_if_fail (y != NULL);
2171 widget = GTK_WIDGET (user_data);
2176 direction = gtk_widget_get_direction (widget);
2178 twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2179 theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2181 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2182 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2183 if (monitor_num < 0)
2185 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2187 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2189 g_warning ("Menu not on screen");
2193 gtk_widget_get_allocation (widget, &allocation);
2198 get_offsets (menu, &horizontal_offset, &vertical_offset);
2200 available_left = tx - monitor.x;
2201 available_right = monitor.x + monitor.width - (tx + allocation.width);
2203 parent = gtk_widget_get_parent (widget);
2204 priv->from_menubar = GTK_IS_MENU_BAR (parent);
2206 switch (priv->submenu_placement)
2208 case GTK_TOP_BOTTOM:
2209 if (direction == GTK_TEXT_DIR_LTR)
2210 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2213 priv->submenu_direction = GTK_DIRECTION_LEFT;
2214 tx += allocation.width - twidth;
2216 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2217 ty += allocation.height;
2218 else if ((ty - theight) >= monitor.y)
2220 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2221 ty += allocation.height;
2226 case GTK_LEFT_RIGHT:
2227 if (GTK_IS_MENU (parent))
2228 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2230 parent_menu_item = NULL;
2232 context = gtk_widget_get_style_context (parent);
2233 state = gtk_widget_get_state_flags (parent);
2234 gtk_style_context_get_padding (context, state, &parent_padding);
2236 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2238 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2242 if (direction == GTK_TEXT_DIR_LTR)
2243 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2245 priv->submenu_direction = GTK_DIRECTION_LEFT;
2248 switch (priv->submenu_direction)
2250 case GTK_DIRECTION_LEFT:
2251 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2252 available_left >= available_right)
2253 tx -= twidth + parent_padding.left + horizontal_offset;
2256 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2257 tx += allocation.width + parent_padding.right + horizontal_offset;
2261 case GTK_DIRECTION_RIGHT:
2262 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2263 available_right >= available_left)
2264 tx += allocation.width + parent_padding.right + horizontal_offset;
2267 priv->submenu_direction = GTK_DIRECTION_LEFT;
2268 tx -= twidth + parent_padding.left + horizontal_offset;
2273 ty += vertical_offset;
2275 /* If the height of the menu doesn't fit we move it upward. */
2276 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2280 /* If we have negative, tx, here it is because we can't get
2281 * the menu all the way on screen. Favor the left portion.
2283 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2286 gtk_menu_set_monitor (menu, monitor_num);
2288 if (!gtk_widget_get_visible (menu->priv->toplevel))
2290 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2291 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2296 * gtk_menu_item_set_right_justified:
2297 * @menu_item: a #GtkMenuItem.
2298 * @right_justified: if %TRUE the menu item will appear at the
2299 * far right if added to a menu bar
2301 * Sets whether the menu item appears justified at the right
2302 * side of a menu bar. This was traditionally done for "Help"
2303 * menu items, but is now considered a bad idea. (If the widget
2304 * layout is reversed for a right-to-left language like Hebrew
2305 * or Arabic, right-justified-menu-items appear at the left.)
2307 * Deprecated: 3.2: If you insist on using it, use
2308 * gtk_widget_set_hexpand() and gtk_widget_set_halign().
2311 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2312 gboolean right_justified)
2314 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2316 gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2320 * gtk_menu_item_get_right_justified:
2321 * @menu_item: a #GtkMenuItem
2323 * Gets whether the menu item appears justified at the right
2324 * side of the menu bar.
2326 * Return value: %TRUE if the menu item will appear at the
2327 * far right if added to a menu bar.
2329 * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2332 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2334 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2336 return menu_item->priv->right_justify;
2341 gtk_menu_item_show_all (GtkWidget *widget)
2343 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2344 GtkMenuItemPrivate *priv = menu_item->priv;
2346 /* show children including submenu */
2348 gtk_widget_show_all (priv->submenu);
2349 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2351 gtk_widget_show (widget);
2355 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2360 parent = gtk_widget_get_parent (widget);
2362 /* Chain to the parent GtkMenu for further checks */
2363 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2364 parent && gtk_widget_can_activate_accel (parent, signal_id));
2368 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2371 const gchar **path_p = data;
2375 if (GTK_IS_LABEL (widget))
2377 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2378 if (*path_p && (*path_p)[0] == 0)
2381 else if (GTK_IS_CONTAINER (widget))
2382 gtk_container_foreach (GTK_CONTAINER (widget),
2383 gtk_menu_item_accel_name_foreach,
2389 gtk_menu_item_parent_set (GtkWidget *widget,
2390 GtkWidget *previous_parent)
2392 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2396 parent = gtk_widget_get_parent (widget);
2397 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2400 _gtk_menu_item_refresh_accel_path (menu_item,
2401 menu->priv->accel_path,
2402 menu->priv->accel_group,
2405 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2406 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2410 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2411 const gchar *prefix,
2412 GtkAccelGroup *accel_group,
2413 gboolean group_changed)
2415 GtkMenuItemPrivate *priv = menu_item->priv;
2419 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2420 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2422 widget = GTK_WIDGET (menu_item);
2426 gtk_widget_set_accel_path (widget, NULL, NULL);
2430 path = _gtk_widget_get_accel_path (widget, NULL);
2431 if (!path) /* no active accel_path yet */
2433 path = priv->accel_path;
2434 if (!path && prefix)
2436 const gchar *postfix = NULL;
2439 /* try to construct one from label text */
2440 gtk_container_foreach (GTK_CONTAINER (menu_item),
2441 gtk_menu_item_accel_name_foreach,
2445 new_path = g_strconcat (prefix, "/", postfix, NULL);
2446 path = priv->accel_path = (char*)g_intern_string (new_path);
2451 gtk_widget_set_accel_path (widget, path, accel_group);
2453 else if (group_changed) /* reinstall accelerators */
2454 gtk_widget_set_accel_path (widget, path, accel_group);
2458 * gtk_menu_item_set_accel_path
2459 * @menu_item: a valid #GtkMenuItem
2460 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2461 * item's functionality, or %NULL to unset the current path.
2463 * Set the accelerator path on @menu_item, through which runtime
2464 * changes of the menu item's accelerator caused by the user can be
2465 * identified and saved to persistent storage (see gtk_accel_map_save()
2466 * on this). To set up a default accelerator for this menu item, call
2467 * gtk_accel_map_add_entry() with the same @accel_path. See also
2468 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2469 * and gtk_menu_set_accel_path() for a more convenient variant of
2472 * This function is basically a convenience wrapper that handles
2473 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2474 * group for the menu item.
2476 * Note that you do need to set an accelerator on the parent menu with
2477 * gtk_menu_set_accel_group() for this to work.
2479 * Note that @accel_path string will be stored in a #GQuark.
2480 * Therefore, if you pass a static string, you can save some memory
2481 * by interning it first with g_intern_static_string().
2484 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2485 const gchar *accel_path)
2487 GtkMenuItemPrivate *priv = menu_item->priv;
2491 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2492 g_return_if_fail (accel_path == NULL ||
2493 (accel_path[0] == '<' && strchr (accel_path, '/')));
2495 widget = GTK_WIDGET (menu_item);
2497 /* store new path */
2498 priv->accel_path = (char*)g_intern_string (accel_path);
2500 /* forget accelerators associated with old path */
2501 gtk_widget_set_accel_path (widget, NULL, NULL);
2503 /* install accelerators associated with new path */
2504 parent = gtk_widget_get_parent (widget);
2505 if (GTK_IS_MENU (parent))
2507 GtkMenu *menu = GTK_MENU (parent);
2509 if (menu->priv->accel_group)
2510 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2512 menu->priv->accel_group,
2518 * gtk_menu_item_get_accel_path
2519 * @menu_item: a valid #GtkMenuItem
2521 * Retrieve the accelerator path that was previously set on @menu_item.
2523 * See gtk_menu_item_set_accel_path() for details.
2525 * Returns: the accelerator path corresponding to this menu
2526 * item's functionality, or %NULL if not set
2531 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2533 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2535 return menu_item->priv->accel_path;
2539 gtk_menu_item_forall (GtkContainer *container,
2540 gboolean include_internals,
2541 GtkCallback callback,
2542 gpointer callback_data)
2546 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2547 g_return_if_fail (callback != NULL);
2549 child = gtk_bin_get_child (GTK_BIN (container));
2551 callback (child, callback_data);
2555 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2557 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2558 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2559 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2560 !gtk_widget_is_sensitive (menu_item) ||
2561 !gtk_widget_get_visible (menu_item))
2568 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2570 GtkWidget *accel_label;
2572 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2574 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2575 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2577 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2578 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2579 GTK_WIDGET (menu_item));
2580 gtk_widget_show (accel_label);
2585 * gtk_menu_item_set_label:
2586 * @menu_item: a #GtkMenuItem
2587 * @label: the text you want to set
2589 * Sets @text on the @menu_item label
2594 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2597 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2599 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2603 * gtk_menu_item_get_label:
2604 * @menu_item: a #GtkMenuItem
2606 * Sets @text on the @menu_item label
2608 * Returns: The text in the @menu_item label. This is the internal
2609 * string used by the label, and must not be modified.
2614 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2616 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2618 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2622 * gtk_menu_item_set_use_underline:
2623 * @menu_item: a #GtkMenuItem
2624 * @setting: %TRUE if underlines in the text indicate mnemonics
2626 * If true, an underline in the text indicates the next character
2627 * should be used for the mnemonic accelerator key.
2632 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2637 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2639 gtk_menu_item_ensure_label (menu_item);
2641 child = gtk_bin_get_child (GTK_BIN (menu_item));
2642 if (GTK_IS_LABEL (child))
2644 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2646 g_object_notify (G_OBJECT (menu_item), "use-underline");
2651 * gtk_menu_item_get_use_underline:
2652 * @menu_item: a #GtkMenuItem
2654 * Checks if an underline in the text indicates the next character
2655 * should be used for the mnemonic accelerator key.
2657 * Return value: %TRUE if an embedded underline in the label
2658 * indicates the mnemonic accelerator key.
2663 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2667 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2669 gtk_menu_item_ensure_label (menu_item);
2671 child = gtk_bin_get_child (GTK_BIN (menu_item));
2672 if (GTK_IS_LABEL (child))
2673 return gtk_label_get_use_underline (GTK_LABEL (child));
2679 * gtk_menu_item_set_reserve_indicator:
2680 * @menu_item: a #GtkMenuItem
2681 * @reserve: the new value
2683 * Sets whether the @menu_item should reserve space for
2684 * the submenu indicator, regardless if it actually has
2687 * There should be little need for applications to call
2693 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2696 GtkMenuItemPrivate *priv;
2698 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2700 priv = menu_item->priv;
2702 if (priv->reserve_indicator != reserve)
2704 priv->reserve_indicator = reserve;
2705 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2710 * gtk_menu_item_get_reserve_indicator:
2711 * @menu_item: a #GtkMenuItem
2713 * Returns whether the @menu_item reserves space for
2714 * the submenu indicator, regardless if it has a submenu
2717 * Returns: %TRUE if @menu_item always reserves space for the
2723 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2725 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2727 return menu_item->priv->reserve_indicator;