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"
44 #include "gtktypebuiltins.h"
65 /* activatable properties */
66 PROP_ACTIVATABLE_RELATED_ACTION,
67 PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
71 static void gtk_menu_item_dispose (GObject *object);
72 static void gtk_menu_item_set_property (GObject *object,
76 static void gtk_menu_item_get_property (GObject *object,
80 static void gtk_menu_item_destroy (GtkWidget *widget);
81 static void gtk_menu_item_size_allocate (GtkWidget *widget,
82 GtkAllocation *allocation);
83 static void gtk_menu_item_realize (GtkWidget *widget);
84 static void gtk_menu_item_unrealize (GtkWidget *widget);
85 static void gtk_menu_item_map (GtkWidget *widget);
86 static void gtk_menu_item_unmap (GtkWidget *widget);
87 static gboolean gtk_menu_item_enter (GtkWidget *widget,
88 GdkEventCrossing *event);
89 static gboolean gtk_menu_item_leave (GtkWidget *widget,
90 GdkEventCrossing *event);
91 static gboolean gtk_menu_item_draw (GtkWidget *widget,
93 static void gtk_menu_item_parent_set (GtkWidget *widget,
94 GtkWidget *previous_parent);
97 static void gtk_real_menu_item_select (GtkMenuItem *item);
98 static void gtk_real_menu_item_deselect (GtkMenuItem *item);
99 static void gtk_real_menu_item_activate (GtkMenuItem *item);
100 static void gtk_real_menu_item_activate_item (GtkMenuItem *item);
101 static void gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
103 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
105 static gboolean gtk_menu_item_mnemonic_activate (GtkWidget *widget,
106 gboolean group_cycling);
108 static void gtk_menu_item_ensure_label (GtkMenuItem *menu_item);
109 static gint gtk_menu_item_popup_timeout (gpointer data);
110 static void gtk_menu_item_position_menu (GtkMenu *menu,
115 static void gtk_menu_item_show_all (GtkWidget *widget);
116 static void gtk_menu_item_forall (GtkContainer *container,
117 gboolean include_internals,
118 GtkCallback callback,
119 gpointer callback_data);
120 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
123 static void gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
125 static G_CONST_RETURN gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
127 static void gtk_menu_item_get_preferred_width (GtkWidget *widget,
130 static void gtk_menu_item_get_preferred_height (GtkWidget *widget,
133 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
138 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface);
139 static void gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
143 static void gtk_menu_item_buildable_custom_finished(GtkBuildable *buildable,
146 const gchar *tagname,
149 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface);
150 static void gtk_menu_item_update (GtkActivatable *activatable,
152 const gchar *property_name);
153 static void gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
155 static void gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
157 static void gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
158 gboolean use_appearance);
161 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
163 static GtkBuildableIface *parent_buildable_iface;
165 G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
166 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
167 gtk_menu_item_buildable_interface_init)
168 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
169 gtk_menu_item_activatable_interface_init))
173 gtk_menu_item_class_init (GtkMenuItemClass *klass)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
176 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
177 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
179 gobject_class->dispose = gtk_menu_item_dispose;
180 gobject_class->set_property = gtk_menu_item_set_property;
181 gobject_class->get_property = gtk_menu_item_get_property;
183 widget_class->destroy = gtk_menu_item_destroy;
184 widget_class->size_allocate = gtk_menu_item_size_allocate;
185 widget_class->draw = gtk_menu_item_draw;
186 widget_class->realize = gtk_menu_item_realize;
187 widget_class->unrealize = gtk_menu_item_unrealize;
188 widget_class->map = gtk_menu_item_map;
189 widget_class->unmap = gtk_menu_item_unmap;
190 widget_class->enter_notify_event = gtk_menu_item_enter;
191 widget_class->leave_notify_event = gtk_menu_item_leave;
192 widget_class->show_all = gtk_menu_item_show_all;
193 widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
194 widget_class->parent_set = gtk_menu_item_parent_set;
195 widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
196 widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
197 widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
198 widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
200 container_class->forall = gtk_menu_item_forall;
202 klass->activate = gtk_real_menu_item_activate;
203 klass->activate_item = gtk_real_menu_item_activate_item;
204 klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
205 klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
206 klass->set_label = gtk_real_menu_item_set_label;
207 klass->get_label = gtk_real_menu_item_get_label;
208 klass->select = gtk_real_menu_item_select;
209 klass->deselect = gtk_real_menu_item_deselect;
211 klass->hide_on_activate = TRUE;
213 menu_item_signals[ACTIVATE] =
214 g_signal_new (I_("activate"),
215 G_OBJECT_CLASS_TYPE (gobject_class),
216 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
217 G_STRUCT_OFFSET (GtkMenuItemClass, activate),
219 _gtk_marshal_VOID__VOID,
221 widget_class->activate_signal = menu_item_signals[ACTIVATE];
223 menu_item_signals[ACTIVATE_ITEM] =
224 g_signal_new (I_("activate-item"),
225 G_OBJECT_CLASS_TYPE (gobject_class),
227 G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
229 _gtk_marshal_VOID__VOID,
232 menu_item_signals[TOGGLE_SIZE_REQUEST] =
233 g_signal_new (I_("toggle-size-request"),
234 G_OBJECT_CLASS_TYPE (gobject_class),
236 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
238 _gtk_marshal_VOID__POINTER,
242 menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
243 g_signal_new (I_("toggle-size-allocate"),
244 G_OBJECT_CLASS_TYPE (gobject_class),
246 G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
248 _gtk_marshal_VOID__INT,
252 menu_item_signals[SELECT] =
253 g_signal_new (I_("select"),
254 G_OBJECT_CLASS_TYPE (gobject_class),
256 G_STRUCT_OFFSET (GtkMenuItemClass, select),
258 _gtk_marshal_VOID__VOID,
261 menu_item_signals[DESELECT] =
262 g_signal_new (I_("deselect"),
263 G_OBJECT_CLASS_TYPE (gobject_class),
265 G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
267 _gtk_marshal_VOID__VOID,
271 * GtkMenuItem:right-justified:
273 * Sets whether the menu item appears justified
274 * at the right side of a menu bar.
278 g_object_class_install_property (gobject_class,
279 PROP_RIGHT_JUSTIFIED,
280 g_param_spec_boolean ("right-justified",
281 P_("Right Justified"),
282 P_("Sets whether the menu item appears justified at the right side of a menu bar"),
284 GTK_PARAM_READWRITE));
287 * GtkMenuItem:submenu:
289 * The submenu attached to the menu item, or %NULL if it has none.
293 g_object_class_install_property (gobject_class,
295 g_param_spec_object ("submenu",
297 P_("The submenu attached to the menu item, or NULL if it has none"),
299 GTK_PARAM_READWRITE));
302 * GtkMenuItem:accel-path:
304 * Sets the accelerator path of the menu item, through which runtime
305 * changes of the menu item's accelerator caused by the user can be
306 * identified and saved to persistant storage.
310 g_object_class_install_property (gobject_class,
312 g_param_spec_string ("accel-path",
314 P_("Sets the accelerator path of the menu item"),
316 GTK_PARAM_READWRITE));
321 * The text for the child label.
325 g_object_class_install_property (gobject_class,
327 g_param_spec_string ("label",
329 P_("The text for the child label"),
331 GTK_PARAM_READWRITE));
334 * GtkMenuItem:use-underline:
336 * %TRUE if underlines in the text indicate mnemonics.
340 g_object_class_install_property (gobject_class,
342 g_param_spec_boolean ("use-underline",
344 P_("If set, an underline in the text indicates "
345 "the next character should be used for the "
346 "mnemonic accelerator key"),
348 GTK_PARAM_READWRITE));
350 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
351 g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
353 gtk_widget_class_install_style_property_parser (widget_class,
354 g_param_spec_enum ("selected-shadow-type",
355 "Selected Shadow Type",
356 "Shadow type when item is selected",
357 GTK_TYPE_SHADOW_TYPE,
360 gtk_rc_property_parse_enum);
362 gtk_widget_class_install_style_property (widget_class,
363 g_param_spec_int ("horizontal-padding",
364 "Horizontal Padding",
365 "Padding to left and right of the menu item",
369 GTK_PARAM_READABLE));
371 gtk_widget_class_install_style_property (widget_class,
372 g_param_spec_int ("toggle-spacing",
374 "Space between icon and label",
378 GTK_PARAM_READABLE));
380 gtk_widget_class_install_style_property (widget_class,
381 g_param_spec_int ("arrow-spacing",
383 "Space between label and arrow",
387 GTK_PARAM_READABLE));
389 gtk_widget_class_install_style_property (widget_class,
390 g_param_spec_float ("arrow-scaling",
392 P_("Amount of space used up by arrow, relative to the menu item's font size"),
394 GTK_PARAM_READABLE));
397 * GtkMenuItem:width-chars:
399 * The minimum desired width of the menu item in characters.
403 gtk_widget_class_install_style_property (widget_class,
404 g_param_spec_int ("width-chars",
405 P_("Width in Characters"),
406 P_("The minimum desired width of the menu item in characters"),
408 GTK_PARAM_READABLE));
410 g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
414 gtk_menu_item_init (GtkMenuItem *menu_item)
416 GtkStyleContext *context;
417 GtkMenuItemPrivate *priv;
419 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
422 menu_item->priv = priv;
424 gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
427 priv->use_action_appearance = TRUE;
429 menu_item->priv->submenu = NULL;
430 menu_item->priv->toggle_size = 0;
431 menu_item->priv->accelerator_width = 0;
432 if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
433 priv->submenu_direction = GTK_DIRECTION_LEFT;
435 priv->submenu_direction = GTK_DIRECTION_RIGHT;
436 priv->submenu_placement = GTK_TOP_BOTTOM;
437 priv->right_justify = FALSE;
438 priv->use_action_appearance = TRUE;
442 context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
443 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
447 gtk_menu_item_new (void)
449 return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
453 gtk_menu_item_new_with_label (const gchar *label)
455 return g_object_new (GTK_TYPE_MENU_ITEM,
462 * gtk_menu_item_new_with_mnemonic:
463 * @label: The text of the button, with an underscore in front of the
466 * Creates a new #GtkMenuItem containing a label.
468 * The label will be created using gtk_label_new_with_mnemonic(),
469 * so underscores in @label indicate the mnemonic for the menu item.
471 * Returns: a new #GtkMenuItem
474 gtk_menu_item_new_with_mnemonic (const gchar *label)
476 return g_object_new (GTK_TYPE_MENU_ITEM,
477 "use-underline", TRUE,
483 gtk_menu_item_dispose (GObject *object)
485 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
486 GtkMenuItemPrivate *priv = menu_item->priv;
490 gtk_action_disconnect_accelerator (priv->action);
491 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
494 G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
498 gtk_menu_item_set_property (GObject *object,
503 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
507 case PROP_RIGHT_JUSTIFIED:
508 gtk_menu_item_set_right_justified (menu_item, g_value_get_boolean (value));
511 gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
513 case PROP_ACCEL_PATH:
514 gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
517 gtk_menu_item_set_label (menu_item, g_value_get_string (value));
519 case PROP_USE_UNDERLINE:
520 gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
522 case PROP_ACTIVATABLE_RELATED_ACTION:
523 gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
525 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
526 gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
535 gtk_menu_item_get_property (GObject *object,
540 GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
541 GtkMenuItemPrivate *priv = menu_item->priv;
545 case PROP_RIGHT_JUSTIFIED:
546 g_value_set_boolean (value, gtk_menu_item_get_right_justified (menu_item));
549 g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
551 case PROP_ACCEL_PATH:
552 g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
555 g_value_set_string (value, gtk_menu_item_get_label (menu_item));
557 case PROP_USE_UNDERLINE:
558 g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
560 case PROP_ACTIVATABLE_RELATED_ACTION:
561 g_value_set_object (value, priv->action);
563 case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
564 g_value_set_boolean (value, priv->use_action_appearance);
567 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
573 gtk_menu_item_destroy (GtkWidget *widget)
575 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
576 GtkMenuItemPrivate *priv = menu_item->priv;
579 gtk_widget_destroy (priv->submenu);
581 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
585 gtk_menu_item_detacher (GtkWidget *widget,
588 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
589 GtkMenuItemPrivate *priv = menu_item->priv;
591 g_return_if_fail (priv->submenu == (GtkWidget*) menu);
593 priv->submenu = NULL;
597 get_arrow_size (GtkWidget *widget,
601 GtkStyleContext *style_context;
603 PangoContext *context;
604 PangoFontMetrics *metrics;
605 gfloat arrow_scaling;
609 gtk_widget_style_get (widget,
610 "arrow-scaling", &arrow_scaling,
613 context = gtk_widget_get_pango_context (child);
614 style_context = gtk_widget_get_style_context (child);
615 state = gtk_widget_get_state_flags (child);
617 metrics = pango_context_get_metrics (context,
618 gtk_style_context_get_font (style_context, state),
619 pango_context_get_language (context));
621 *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
622 pango_font_metrics_get_descent (metrics)));
624 pango_font_metrics_unref (metrics);
626 *size = *size * arrow_scaling;
631 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
636 if (GTK_IS_ACCEL_LABEL (widget))
640 w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
641 *width = MAX (*width, w);
643 else if (GTK_IS_CONTAINER (widget))
644 gtk_container_foreach (GTK_CONTAINER (widget),
645 gtk_menu_item_accel_width_foreach,
650 get_minimum_width (GtkWidget *widget)
652 GtkStyleContext *style_context;
654 PangoContext *context;
655 PangoFontMetrics *metrics;
659 context = gtk_widget_get_pango_context (widget);
660 style_context = gtk_widget_get_style_context (widget);
661 state = gtk_widget_get_state_flags (widget);
663 metrics = pango_context_get_metrics (context,
664 gtk_style_context_get_font (style_context, state),
665 pango_context_get_language (context));
667 width = pango_font_metrics_get_approximate_char_width (metrics);
669 pango_font_metrics_unref (metrics);
671 gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
673 return PANGO_PIXELS (width_chars * width);
677 gtk_menu_item_get_preferred_width (GtkWidget *widget,
681 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
682 GtkMenuItemPrivate *priv = menu_item->priv;
687 guint horizontal_padding;
689 GtkPackDirection pack_dir;
690 GtkPackDirection child_pack_dir;
691 gint min_width, nat_width;
692 GtkStyleContext *context;
694 GtkBorder padding, border;
696 min_width = nat_width = 0;
698 gtk_widget_style_get (widget,
699 "horizontal-padding", &horizontal_padding,
702 bin = GTK_BIN (widget);
703 parent = gtk_widget_get_parent (widget);
705 if (GTK_IS_MENU_BAR (parent))
707 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
708 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
712 pack_dir = GTK_PACK_DIRECTION_LTR;
713 child_pack_dir = GTK_PACK_DIRECTION_LTR;
716 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
718 context = gtk_widget_get_style_context (widget);
719 state = gtk_widget_get_state_flags (widget);
720 gtk_style_context_get_padding (context, state, &padding);
721 gtk_style_context_get_border (context, state, &border);
723 min_width = (border_width * 2) + padding.left + padding.right +
724 border.left + border.right;
726 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
727 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
728 min_width += 2 * horizontal_padding;
730 nat_width = min_width;
732 child = gtk_bin_get_child (bin);
734 if (child != NULL && gtk_widget_get_visible (child))
736 GtkMenuItemPrivate *priv = menu_item->priv;
737 gint child_min, child_nat;
739 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
741 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
746 gtk_widget_style_get (widget,
747 "arrow-spacing", &arrow_spacing,
750 get_arrow_size (widget, child, &arrow_size);
752 gtk_widget_style_get (widget,
753 "arrow-spacing", &arrow_spacing,
756 get_arrow_size (widget, child, &arrow_size);
758 min_width += arrow_size;
759 min_width += arrow_spacing;
761 min_width = MAX (min_width, get_minimum_width (widget));
762 nat_width = min_width;
765 min_width += child_min;
766 nat_width += child_nat;
770 gtk_container_foreach (GTK_CONTAINER (menu_item),
771 gtk_menu_item_accel_width_foreach,
773 priv->accelerator_width = accel_width;
776 *minimum_size = min_width;
779 *natural_size = nat_width;
783 gtk_menu_item_get_preferred_height (GtkWidget *widget,
787 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
788 GtkMenuItemPrivate *priv = menu_item->priv;
790 GtkStyleContext *context;
792 GtkBorder padding, border;
796 guint horizontal_padding;
798 GtkPackDirection pack_dir;
799 GtkPackDirection child_pack_dir;
800 gint min_height, nat_height;
802 min_height = nat_height = 0;
804 context = gtk_widget_get_style_context (widget);
805 state = gtk_widget_get_state_flags (widget);
806 gtk_style_context_get_padding (context, state, &padding);
807 gtk_style_context_get_border (context, state, &border);
809 gtk_widget_style_get (widget,
810 "horizontal-padding", &horizontal_padding,
813 bin = GTK_BIN (widget);
814 parent = gtk_widget_get_parent (widget);
816 if (GTK_IS_MENU_BAR (parent))
818 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
819 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
823 pack_dir = GTK_PACK_DIRECTION_LTR;
824 child_pack_dir = GTK_PACK_DIRECTION_LTR;
827 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
828 min_height = (border_width * 2) + padding.top + padding.bottom + border.top + border.bottom;
830 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
831 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
832 min_height += 2 * horizontal_padding;
834 nat_height = min_height;
836 child = gtk_bin_get_child (bin);
838 if (child != NULL && gtk_widget_get_visible (child))
840 GtkMenuItemPrivate *priv = menu_item->priv;
841 gint child_min, child_nat;
843 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
845 min_height += child_min;
846 nat_height += child_nat;
848 if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
852 get_arrow_size (widget, child, &arrow_size);
854 min_height = MAX (min_height, arrow_size);
855 nat_height = MAX (nat_height, arrow_size);
858 else /* separator item */
860 gboolean wide_separators;
861 gint separator_height;
863 gtk_widget_style_get (widget,
864 "wide-separators", &wide_separators,
865 "separator-height", &separator_height,
869 min_height += separator_height + padding.top + border.top;
871 min_height += padding.top + padding.bottom + border.top + border.bottom;
873 nat_height = min_height;
877 gtk_container_foreach (GTK_CONTAINER (menu_item),
878 gtk_menu_item_accel_width_foreach,
880 priv->accelerator_width = accel_width;
883 *minimum_size = min_height;
886 *natural_size = nat_height;
890 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
895 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
896 GtkMenuItemPrivate *priv = menu_item->priv;
898 GtkStyleContext *context;
900 GtkBorder padding, border;
903 guint horizontal_padding;
905 GtkPackDirection pack_dir;
906 GtkPackDirection child_pack_dir;
907 gint min_height, nat_height;
910 min_height = nat_height = 0;
912 context = gtk_widget_get_style_context (widget);
913 state = gtk_widget_get_state_flags (widget);
914 gtk_style_context_get_padding (context, state, &padding);
915 gtk_style_context_get_border (context, state, &border);
917 gtk_widget_style_get (widget,
918 "horizontal-padding", &horizontal_padding,
921 bin = GTK_BIN (widget);
922 parent = gtk_widget_get_parent (widget);
924 if (GTK_IS_MENU_BAR (parent))
926 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
927 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
931 pack_dir = GTK_PACK_DIRECTION_LTR;
932 child_pack_dir = GTK_PACK_DIRECTION_LTR;
935 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
936 min_height = (border_width * 2) + padding.top + padding.bottom + border.top + border.bottom;
938 avail_size = for_size;
939 avail_size -= (border_width * 2) + padding.left + padding.right + border.left + border.right;
941 if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
942 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
943 min_height += 2 * horizontal_padding;
945 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
946 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
947 avail_size -= 2 * horizontal_padding;
949 nat_height = min_height;
951 child = gtk_bin_get_child (bin);
953 if (child != NULL && gtk_widget_get_visible (child))
955 gint child_min, child_nat;
958 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
962 gtk_widget_style_get (widget,
963 "arrow-spacing", &arrow_spacing,
966 get_arrow_size (widget, child, &arrow_size);
968 avail_size -= arrow_size;
969 avail_size -= arrow_spacing;
972 gtk_widget_get_preferred_height_for_width (child,
977 min_height += child_min;
978 nat_height += child_nat;
980 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
982 min_height = MAX (min_height, arrow_size);
983 nat_height = MAX (nat_height, arrow_size);
986 else /* separator item */
988 gboolean wide_separators;
989 gint separator_height;
991 gtk_widget_style_get (widget,
992 "wide-separators", &wide_separators,
993 "separator-height", &separator_height,
997 min_height += separator_height + padding.top + border.top;
999 min_height += padding.top + padding.bottom + border.top + border.bottom;
1001 nat_height = min_height;
1005 *minimum_size = min_height;
1008 *natural_size = nat_height;
1012 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1014 parent_buildable_iface = g_type_interface_peek_parent (iface);
1015 iface->add_child = gtk_menu_item_buildable_add_child;
1016 iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1020 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1021 GtkBuilder *builder,
1025 if (type && strcmp (type, "submenu") == 0)
1026 gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1027 GTK_WIDGET (child));
1029 parent_buildable_iface->add_child (buildable, builder, child, type);
1034 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1035 GtkBuilder *builder,
1037 const gchar *tagname,
1040 GtkWidget *toplevel;
1042 if (strcmp (tagname, "accelerator") == 0)
1044 GtkMenuShell *menu_shell;
1047 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1050 while (GTK_IS_MENU (menu_shell) &&
1051 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1052 menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1054 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1058 /* Fall back to something ... */
1059 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1061 g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1062 gtk_buildable_get_name (buildable));
1065 /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1066 _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1069 parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1074 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1076 iface->update = gtk_menu_item_update;
1077 iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1081 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1085 child = gtk_bin_get_child (GTK_BIN (menu_item));
1087 if (GTK_IS_LABEL (child))
1091 label = gtk_action_get_label (action);
1092 gtk_menu_item_set_label (menu_item, label);
1096 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1099 gtk_menu_item_update (GtkActivatable *activatable,
1101 const gchar *property_name)
1103 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1104 GtkMenuItemPrivate *priv = menu_item->priv;
1106 if (strcmp (property_name, "visible") == 0)
1107 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1108 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1109 else if (strcmp (property_name, "sensitive") == 0)
1110 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1111 else if (priv->use_action_appearance)
1113 if (strcmp (property_name, "label") == 0)
1114 activatable_update_label (menu_item, action);
1119 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1122 GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1123 GtkMenuItemPrivate *priv = menu_item->priv;
1126 if (!priv->use_action_appearance || !action)
1128 label = gtk_bin_get_child (GTK_BIN (menu_item));
1130 if (GTK_IS_ACCEL_LABEL (label))
1131 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1137 _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1138 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1140 gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1142 if (priv->use_action_appearance)
1144 label = gtk_bin_get_child (GTK_BIN (menu_item));
1146 /* make sure label is a label, deleting it otherwise */
1147 if (label && !GTK_IS_LABEL (label))
1149 gtk_container_remove (GTK_CONTAINER (menu_item), label);
1152 /* Make sure that menu_item has a label and that any
1153 * accelerators are set */
1154 gtk_menu_item_ensure_label (menu_item);
1155 gtk_menu_item_set_use_underline (menu_item, TRUE);
1156 /* Make label point to the menu_item's label */
1157 label = gtk_bin_get_child (GTK_BIN (menu_item));
1159 if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1161 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1162 gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1163 gtk_action_get_accel_closure (action));
1166 activatable_update_label (menu_item, action);
1171 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1174 GtkMenuItemPrivate *priv = menu_item->priv;
1176 if (priv->action == action)
1181 gtk_action_disconnect_accelerator (priv->action);
1186 const gchar *accel_path;
1188 accel_path = gtk_action_get_accel_path (action);
1191 gtk_action_connect_accelerator (action);
1192 gtk_menu_item_set_accel_path (menu_item, accel_path);
1196 gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1198 priv->action = action;
1202 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1203 gboolean use_appearance)
1205 GtkMenuItemPrivate *priv = menu_item->priv;
1207 if (priv->use_action_appearance != use_appearance)
1209 priv->use_action_appearance = use_appearance;
1211 gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1217 * gtk_menu_item_set_submenu:
1218 * @menu_item: a #GtkMenuItem
1219 * @submenu: (allow-none): the submenu, or %NULL
1221 * Sets or replaces the menu item's submenu, or removes it when a %NULL
1222 * submenu is passed.
1225 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1228 GtkMenuItemPrivate *priv = menu_item->priv;
1230 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1231 g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1233 if (priv->submenu != submenu)
1236 gtk_menu_detach (GTK_MENU (priv->submenu));
1240 priv->submenu = submenu;
1241 gtk_menu_attach_to_widget (GTK_MENU (submenu),
1242 GTK_WIDGET (menu_item),
1243 gtk_menu_item_detacher);
1246 if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1247 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1249 g_object_notify (G_OBJECT (menu_item), "submenu");
1254 * gtk_menu_item_get_submenu:
1255 * @menu_item: a #GtkMenuItem
1257 * Gets the submenu underneath this menu item, if any.
1258 * See gtk_menu_item_set_submenu().
1260 * Return value: (transfer none): submenu for this menu item, or %NULL if none
1263 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1265 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1267 return menu_item->priv->submenu;
1270 void _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1271 GtkSubmenuPlacement placement);
1274 _gtk_menu_item_set_placement (GtkMenuItem *menu_item,
1275 GtkSubmenuPlacement placement)
1277 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1279 menu_item->priv->submenu_placement = placement;
1283 gtk_menu_item_select (GtkMenuItem *menu_item)
1287 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1289 g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1291 /* Enable themeing of the parent menu item depending on whether
1292 * something is selected in its submenu
1294 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1295 if (GTK_IS_MENU (parent))
1297 GtkMenu *menu = GTK_MENU (parent);
1299 if (menu->priv->parent_menu_item)
1300 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1305 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1309 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1311 g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1313 /* Enable themeing of the parent menu item depending on whether
1314 * something is selected in its submenu
1316 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1317 if (GTK_IS_MENU (parent))
1319 GtkMenu *menu = GTK_MENU (parent);
1321 if (menu->priv->parent_menu_item)
1322 gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1327 gtk_menu_item_activate (GtkMenuItem *menu_item)
1329 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1331 g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1335 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1338 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1340 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1344 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1347 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1349 g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1353 gtk_menu_item_size_allocate (GtkWidget *widget,
1354 GtkAllocation *allocation)
1356 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1357 GtkMenuItemPrivate *priv = menu_item->priv;
1359 GtkAllocation child_allocation;
1360 GtkTextDirection direction;
1361 GtkPackDirection pack_dir;
1362 GtkPackDirection child_pack_dir;
1366 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1367 g_return_if_fail (allocation != NULL);
1369 bin = GTK_BIN (widget);
1371 direction = gtk_widget_get_direction (widget);
1373 parent = gtk_widget_get_parent (widget);
1374 if (GTK_IS_MENU_BAR (parent))
1376 pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1377 child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1381 pack_dir = GTK_PACK_DIRECTION_LTR;
1382 child_pack_dir = GTK_PACK_DIRECTION_LTR;
1385 gtk_widget_set_allocation (widget, allocation);
1387 child = gtk_bin_get_child (bin);
1390 GtkStyleContext *context;
1391 GtkStateFlags state;
1392 GtkBorder padding, border;
1393 guint horizontal_padding;
1396 context = gtk_widget_get_style_context (widget);
1397 state = gtk_widget_get_state_flags (widget);
1398 gtk_style_context_get_padding (context, state, &padding);
1399 gtk_style_context_get_border (context, state, &border);
1401 gtk_widget_style_get (widget,
1402 "horizontal-padding", &horizontal_padding,
1405 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1406 child_allocation.x = border_width + padding.left + border.left;
1407 child_allocation.y = border_width + padding.top + border.top;
1409 child_allocation.width = allocation->width - (border_width * 2) -
1410 padding.left - padding.right - border.left - border.right;
1411 child_allocation.height = allocation->height - (border_width * 2) -
1412 padding.top - padding.bottom - border.top - border.bottom;
1414 if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1415 (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1417 child_allocation.x += horizontal_padding;
1418 child_allocation.width -= 2 * horizontal_padding;
1420 else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1421 (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1423 child_allocation.y += horizontal_padding;
1424 child_allocation.height -= 2 * horizontal_padding;
1427 if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1428 child_pack_dir == GTK_PACK_DIRECTION_RTL)
1430 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1431 child_allocation.x += priv->toggle_size;
1432 child_allocation.width -= priv->toggle_size;
1436 if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1437 child_allocation.y += priv->toggle_size;
1438 child_allocation.height -= priv->toggle_size;
1441 child_allocation.x += allocation->x;
1442 child_allocation.y += allocation->y;
1444 if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1446 guint arrow_spacing;
1449 gtk_widget_style_get (widget,
1450 "arrow-spacing", &arrow_spacing,
1453 get_arrow_size (widget, child, &arrow_size);
1455 if (direction == GTK_TEXT_DIR_RTL)
1456 child_allocation.x += arrow_size + arrow_spacing;
1457 child_allocation.width -= arrow_size + arrow_spacing;
1460 if (child_allocation.width < 1)
1461 child_allocation.width = 1;
1463 gtk_widget_size_allocate (child, &child_allocation);
1466 if (gtk_widget_get_realized (widget))
1467 gdk_window_move_resize (priv->event_window,
1468 allocation->x, allocation->y,
1469 allocation->width, allocation->height);
1472 gtk_menu_reposition (GTK_MENU (priv->submenu));
1476 gtk_menu_item_realize (GtkWidget *widget)
1478 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1479 GtkMenuItemPrivate *priv = menu_item->priv;
1480 GtkAllocation allocation;
1482 GdkWindowAttr attributes;
1483 gint attributes_mask;
1485 gtk_widget_set_realized (widget, TRUE);
1487 window = gtk_widget_get_parent_window (widget);
1488 gtk_widget_set_window (widget, window);
1489 g_object_ref (window);
1491 gtk_widget_get_allocation (widget, &allocation);
1493 attributes.x = allocation.x;
1494 attributes.y = allocation.y;
1495 attributes.width = allocation.width;
1496 attributes.height = allocation.height;
1497 attributes.window_type = GDK_WINDOW_CHILD;
1498 attributes.wclass = GDK_INPUT_ONLY;
1499 attributes.event_mask = (gtk_widget_get_events (widget) |
1500 GDK_BUTTON_PRESS_MASK |
1501 GDK_BUTTON_RELEASE_MASK |
1502 GDK_ENTER_NOTIFY_MASK |
1503 GDK_LEAVE_NOTIFY_MASK |
1504 GDK_POINTER_MOTION_MASK);
1506 attributes_mask = GDK_WA_X | GDK_WA_Y;
1508 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1509 &attributes, attributes_mask);
1510 gdk_window_set_user_data (priv->event_window, widget);
1514 gtk_menu_item_unrealize (GtkWidget *widget)
1516 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1517 GtkMenuItemPrivate *priv = menu_item->priv;
1519 gdk_window_set_user_data (priv->event_window, NULL);
1520 gdk_window_destroy (priv->event_window);
1521 priv->event_window = NULL;
1523 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1527 gtk_menu_item_map (GtkWidget *widget)
1529 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1530 GtkMenuItemPrivate *priv = menu_item->priv;
1532 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1534 gdk_window_show (priv->event_window);
1538 gtk_menu_item_unmap (GtkWidget *widget)
1540 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1541 GtkMenuItemPrivate *priv = menu_item->priv;
1543 gdk_window_hide (priv->event_window);
1545 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1549 gtk_menu_item_enter (GtkWidget *widget,
1550 GdkEventCrossing *event)
1552 g_return_val_if_fail (event != NULL, FALSE);
1554 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1558 gtk_menu_item_leave (GtkWidget *widget,
1559 GdkEventCrossing *event)
1561 g_return_val_if_fail (event != NULL, FALSE);
1563 return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1567 gtk_menu_item_draw (GtkWidget *widget,
1570 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1571 GtkMenuItemPrivate *priv = menu_item->priv;
1572 GtkStateFlags state;
1573 GtkStyleContext *context;
1575 GtkWidget *child, *parent;
1576 gint x, y, w, h, width, height;
1577 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1579 state = gtk_widget_get_state_flags (widget);
1580 context = gtk_widget_get_style_context (widget);
1581 width = gtk_widget_get_allocated_width (widget);
1582 height = gtk_widget_get_allocated_height (widget);
1586 w = width - border_width * 2;
1587 h = height - border_width * 2;
1589 child = gtk_bin_get_child (GTK_BIN (menu_item));
1590 parent = gtk_widget_get_parent (widget);
1592 gtk_style_context_save (context);
1593 gtk_style_context_set_state (context, state);
1595 gtk_style_context_get_padding (context, state, &padding);
1597 if (GTK_IS_MENU_BAR (parent))
1598 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1600 if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1602 gtk_render_background (context, cr, x, y, w, h);
1603 gtk_render_frame (context, cr, x, y, w, h);
1606 if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1608 gint arrow_x, arrow_y;
1610 guint horizontal_padding;
1611 GtkTextDirection direction;
1614 direction = gtk_widget_get_direction (widget);
1616 gtk_widget_style_get (widget,
1617 "horizontal-padding", &horizontal_padding,
1620 get_arrow_size (widget, child, &arrow_size);
1622 if (direction == GTK_TEXT_DIR_LTR)
1624 arrow_x = x + w - horizontal_padding - arrow_size;
1629 arrow_x = x + horizontal_padding;
1630 angle = (3 * G_PI) / 2;
1633 arrow_y = y + (h - arrow_size) / 2;
1635 gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1639 gboolean wide_separators;
1640 gint separator_height;
1641 guint horizontal_padding;
1643 gtk_widget_style_get (widget,
1644 "wide-separators", &wide_separators,
1645 "separator-height", &separator_height,
1646 "horizontal-padding", &horizontal_padding,
1648 if (wide_separators)
1649 gtk_render_frame (context, cr,
1650 horizontal_padding + padding.left,
1651 (height - separator_height - padding.top) / 2,
1652 width - (2 * horizontal_padding) - padding.left - padding.right,
1655 gtk_render_line (context, cr,
1656 horizontal_padding + padding.left,
1657 (height - padding.top) / 2,
1658 width - horizontal_padding - padding.right - 1,
1659 (height - padding.top) / 2);
1662 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1664 gtk_style_context_restore (context);
1670 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1672 GtkMenuItemPrivate *priv = menu_item->priv;
1673 gboolean touchscreen_mode;
1675 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1676 "gtk-touchscreen-mode", &touchscreen_mode,
1679 if (!touchscreen_mode && priv->submenu &&
1680 (!gtk_widget_get_mapped (priv->submenu) ||
1681 GTK_MENU (priv->submenu)->priv->tearoff_active))
1683 _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1686 gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1687 GTK_STATE_FLAG_PRELIGHT, FALSE);
1688 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1692 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1694 GtkMenuItemPrivate *priv = menu_item->priv;
1697 _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1699 gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1700 GTK_STATE_FLAG_PRELIGHT);
1701 gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1705 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1706 gboolean group_cycling)
1710 parent = gtk_widget_get_parent (widget);
1712 if (GTK_IS_MENU_SHELL (parent))
1713 _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1715 if (group_cycling &&
1717 GTK_IS_MENU_SHELL (parent) &&
1718 GTK_MENU_SHELL (parent)->priv->active)
1720 gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1723 g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1729 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1731 GtkMenuItemPrivate *priv = menu_item->priv;
1734 gtk_action_activate (priv->action);
1739 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1741 GtkMenuItemPrivate *priv = menu_item->priv;
1745 widget = GTK_WIDGET (menu_item);
1746 parent = gtk_widget_get_parent (widget);
1748 if (parent && GTK_IS_MENU_SHELL (parent))
1750 GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1752 if (priv->submenu == NULL)
1753 gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1756 _gtk_menu_shell_activate (menu_shell);
1758 gtk_menu_shell_select_item (menu_shell, widget);
1759 _gtk_menu_item_popup_submenu (widget, FALSE);
1761 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1767 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1770 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1776 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1779 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1781 menu_item->priv->toggle_size = allocation;
1785 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1790 gtk_menu_item_ensure_label (menu_item);
1792 child = gtk_bin_get_child (GTK_BIN (menu_item));
1793 if (GTK_IS_LABEL (child))
1795 gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1797 g_object_notify (G_OBJECT (menu_item), "label");
1801 static G_CONST_RETURN gchar *
1802 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1806 gtk_menu_item_ensure_label (menu_item);
1808 child = gtk_bin_get_child (GTK_BIN (menu_item));
1809 if (GTK_IS_LABEL (child))
1810 return gtk_label_get_label (GTK_LABEL (child));
1816 free_timeval (GTimeVal *val)
1818 g_slice_free (GTimeVal, val);
1822 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1823 gboolean remember_exact_time)
1825 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1826 GtkMenuItemPrivate *priv = menu_item->priv;
1829 parent = gtk_widget_get_parent (widget);
1831 if (gtk_widget_is_sensitive (priv->submenu) && parent)
1833 gboolean take_focus;
1834 GtkMenuPositionFunc menu_position_func;
1836 take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1837 gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1839 if (remember_exact_time)
1841 GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1843 g_get_current_time (popup_time);
1845 g_object_set_data_full (G_OBJECT (priv->submenu),
1846 "gtk-menu-exact-popup-time", popup_time,
1847 (GDestroyNotify) free_timeval);
1851 g_object_set_data (G_OBJECT (priv->submenu),
1852 "gtk-menu-exact-popup-time", NULL);
1855 /* gtk_menu_item_position_menu positions the submenu from the
1856 * menuitems position. If the menuitem doesn't have a window,
1857 * that doesn't work. In that case we use the default
1858 * positioning function instead which places the submenu at the
1861 if (gtk_widget_get_window (widget))
1862 menu_position_func = gtk_menu_item_position_menu;
1864 menu_position_func = NULL;
1866 gtk_menu_popup (GTK_MENU (priv->submenu),
1871 GTK_MENU_SHELL (parent)->priv->button,
1875 /* Enable themeing of the parent menu item depending on whether
1876 * its submenu is shown or not.
1878 gtk_widget_queue_draw (widget);
1882 gtk_menu_item_popup_timeout (gpointer data)
1884 GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1885 GtkMenuItemPrivate *priv = menu_item->priv;
1888 parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1890 if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1891 (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1893 gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1894 if (priv->timer_from_keypress && priv->submenu)
1895 GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1904 get_popup_delay (GtkWidget *widget)
1908 parent = gtk_widget_get_parent (widget);
1909 if (GTK_IS_MENU_SHELL (parent))
1911 return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1917 g_object_get (gtk_widget_get_settings (widget),
1918 "gtk-menu-popup-delay", &popup_delay,
1926 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1927 gboolean with_delay)
1929 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1930 GtkMenuItemPrivate *priv = menu_item->priv;
1934 g_source_remove (priv->timer);
1941 gint popup_delay = get_popup_delay (widget);
1943 if (popup_delay > 0)
1945 GdkEvent *event = gtk_get_current_event ();
1947 priv->timer = gdk_threads_add_timeout (popup_delay,
1948 gtk_menu_item_popup_timeout,
1952 event->type != GDK_BUTTON_PRESS &&
1953 event->type != GDK_ENTER_NOTIFY)
1954 priv->timer_from_keypress = TRUE;
1956 priv->timer_from_keypress = FALSE;
1959 gdk_event_free (event);
1965 gtk_menu_item_real_popup_submenu (widget, FALSE);
1969 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1971 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1972 GtkMenuItemPrivate *priv = menu_item->priv;
1976 g_object_set_data (G_OBJECT (priv->submenu),
1977 "gtk-menu-exact-popup-time", NULL);
1981 g_source_remove (priv->timer);
1985 gtk_menu_popdown (GTK_MENU (priv->submenu));
1987 gtk_widget_queue_draw (widget);
1992 get_offsets (GtkMenu *menu,
1993 gint *horizontal_offset,
1994 gint *vertical_offset)
1996 gint vertical_padding;
1997 gint horizontal_padding;
1998 GtkStyleContext *context;
1999 GtkStateFlags state;
2002 gtk_widget_style_get (GTK_WIDGET (menu),
2003 "horizontal-offset", horizontal_offset,
2004 "vertical-offset", vertical_offset,
2005 "horizontal-padding", &horizontal_padding,
2006 "vertical-padding", &vertical_padding,
2009 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2010 state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2011 gtk_style_context_get_padding (context, state, &padding);
2013 *vertical_offset -= padding.top;
2014 *vertical_offset -= vertical_padding;
2015 *horizontal_offset += horizontal_padding;
2019 gtk_menu_item_position_menu (GtkMenu *menu,
2025 GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2026 GtkMenuItemPrivate *priv = menu_item->priv;
2027 GtkAllocation allocation;
2029 GtkMenuItem *parent_menu_item;
2030 GtkRequisition requisition;
2033 gint twidth, theight;
2035 GtkTextDirection direction;
2036 GdkRectangle monitor;
2038 gint horizontal_offset;
2039 gint vertical_offset;
2040 gint available_left, available_right;
2041 GtkStyleContext *context;
2042 GtkStateFlags state;
2043 GtkBorder parent_padding;
2045 g_return_if_fail (menu != NULL);
2046 g_return_if_fail (x != NULL);
2047 g_return_if_fail (y != NULL);
2049 widget = GTK_WIDGET (user_data);
2054 direction = gtk_widget_get_direction (widget);
2056 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &requisition, NULL);
2057 twidth = requisition.width;
2058 theight = requisition.height;
2060 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2061 monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2062 if (monitor_num < 0)
2064 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2066 if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2068 g_warning ("Menu not on screen");
2072 gtk_widget_get_allocation (widget, &allocation);
2077 get_offsets (menu, &horizontal_offset, &vertical_offset);
2079 available_left = tx - monitor.x;
2080 available_right = monitor.x + monitor.width - (tx + allocation.width);
2082 parent = gtk_widget_get_parent (widget);
2083 if (GTK_IS_MENU_BAR (parent))
2085 priv->from_menubar = TRUE;
2087 else if (GTK_IS_MENU (parent))
2089 if (GTK_MENU (parent)->priv->parent_menu_item)
2090 priv->from_menubar = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item)->priv->from_menubar;
2092 priv->from_menubar = FALSE;
2096 priv->from_menubar = FALSE;
2099 switch (priv->submenu_placement)
2101 case GTK_TOP_BOTTOM:
2102 if (direction == GTK_TEXT_DIR_LTR)
2103 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2106 priv->submenu_direction = GTK_DIRECTION_LEFT;
2107 tx += allocation.width - twidth;
2109 if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2110 ty += allocation.height;
2111 else if ((ty - theight) >= monitor.y)
2113 else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2114 ty += allocation.height;
2119 case GTK_LEFT_RIGHT:
2120 if (GTK_IS_MENU (parent))
2121 parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2123 parent_menu_item = NULL;
2125 context = gtk_widget_get_style_context (parent);
2126 state = gtk_widget_get_state_flags (parent);
2127 gtk_style_context_get_padding (context, state, &parent_padding);
2129 if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2131 priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2135 if (direction == GTK_TEXT_DIR_LTR)
2136 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2138 priv->submenu_direction = GTK_DIRECTION_LEFT;
2141 switch (priv->submenu_direction)
2143 case GTK_DIRECTION_LEFT:
2144 if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2145 available_left >= available_right)
2146 tx -= twidth + parent_padding.left + horizontal_offset;
2149 priv->submenu_direction = GTK_DIRECTION_RIGHT;
2150 tx += allocation.width + parent_padding.right + horizontal_offset;
2154 case GTK_DIRECTION_RIGHT:
2155 if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2156 available_right >= available_left)
2157 tx += allocation.width + parent_padding.right + horizontal_offset;
2160 priv->submenu_direction = GTK_DIRECTION_LEFT;
2161 tx -= twidth + parent_padding.left + horizontal_offset;
2166 ty += vertical_offset;
2168 /* If the height of the menu doesn't fit we move it upward. */
2169 ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2173 /* If we have negative, tx, here it is because we can't get
2174 * the menu all the way on screen. Favor the left portion.
2176 *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2179 gtk_menu_set_monitor (menu, monitor_num);
2181 if (!gtk_widget_get_visible (menu->priv->toplevel))
2183 gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2184 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2189 * gtk_menu_item_set_right_justified:
2190 * @menu_item: a #GtkMenuItem.
2191 * @right_justified: if %TRUE the menu item will appear at the
2192 * far right if added to a menu bar
2194 * Sets whether the menu item appears justified at the right
2195 * side of a menu bar. This was traditionally done for "Help"
2196 * menu items, but is now considered a bad idea. (If the widget
2197 * layout is reversed for a right-to-left language like Hebrew
2198 * or Arabic, right-justified-menu-items appear at the left.)
2201 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2202 gboolean right_justified)
2204 GtkMenuItemPrivate *priv = menu_item->priv;
2206 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2208 right_justified = right_justified != FALSE;
2210 if (priv->right_justify != right_justified)
2212 priv->right_justify = right_justified;
2213 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2218 * gtk_menu_item_get_right_justified:
2219 * @menu_item: a #GtkMenuItem
2221 * Gets whether the menu item appears justified at the right
2222 * side of the menu bar.
2224 * Return value: %TRUE if the menu item will appear at the
2225 * far right if added to a menu bar.
2228 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2230 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2232 return menu_item->priv->right_justify;
2237 gtk_menu_item_show_all (GtkWidget *widget)
2239 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2240 GtkMenuItemPrivate *priv = menu_item->priv;
2242 /* show children including submenu */
2244 gtk_widget_show_all (priv->submenu);
2245 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2247 gtk_widget_show (widget);
2251 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2256 parent = gtk_widget_get_parent (widget);
2258 /* Chain to the parent GtkMenu for further checks */
2259 return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2260 parent && gtk_widget_can_activate_accel (parent, signal_id));
2264 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2267 const gchar **path_p = data;
2271 if (GTK_IS_LABEL (widget))
2273 *path_p = gtk_label_get_text (GTK_LABEL (widget));
2274 if (*path_p && (*path_p)[0] == 0)
2277 else if (GTK_IS_CONTAINER (widget))
2278 gtk_container_foreach (GTK_CONTAINER (widget),
2279 gtk_menu_item_accel_name_foreach,
2285 gtk_menu_item_parent_set (GtkWidget *widget,
2286 GtkWidget *previous_parent)
2288 GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2292 parent = gtk_widget_get_parent (widget);
2293 menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2296 _gtk_menu_item_refresh_accel_path (menu_item,
2297 menu->priv->accel_path,
2298 menu->priv->accel_group,
2301 if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2302 GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2306 _gtk_menu_item_refresh_accel_path (GtkMenuItem *menu_item,
2307 const gchar *prefix,
2308 GtkAccelGroup *accel_group,
2309 gboolean group_changed)
2311 GtkMenuItemPrivate *priv = menu_item->priv;
2315 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2316 g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2318 widget = GTK_WIDGET (menu_item);
2322 gtk_widget_set_accel_path (widget, NULL, NULL);
2326 path = _gtk_widget_get_accel_path (widget, NULL);
2327 if (!path) /* no active accel_path yet */
2329 path = priv->accel_path;
2330 if (!path && prefix)
2332 const gchar *postfix = NULL;
2335 /* try to construct one from label text */
2336 gtk_container_foreach (GTK_CONTAINER (menu_item),
2337 gtk_menu_item_accel_name_foreach,
2341 new_path = g_strconcat (prefix, "/", postfix, NULL);
2342 path = priv->accel_path = (char*)g_intern_string (new_path);
2347 gtk_widget_set_accel_path (widget, path, accel_group);
2349 else if (group_changed) /* reinstall accelerators */
2350 gtk_widget_set_accel_path (widget, path, accel_group);
2354 * gtk_menu_item_set_accel_path
2355 * @menu_item: a valid #GtkMenuItem
2356 * @accel_path: (allow-none): accelerator path, corresponding to this menu
2357 * item's functionality, or %NULL to unset the current path.
2359 * Set the accelerator path on @menu_item, through which runtime
2360 * changes of the menu item's accelerator caused by the user can be
2361 * identified and saved to persistent storage (see gtk_accel_map_save()
2362 * on this). To set up a default accelerator for this menu item, call
2363 * gtk_accel_map_add_entry() with the same @accel_path. See also
2364 * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2365 * and gtk_menu_set_accel_path() for a more convenient variant of
2368 * This function is basically a convenience wrapper that handles
2369 * calling gtk_widget_set_accel_path() with the appropriate accelerator
2370 * group for the menu item.
2372 * Note that you do need to set an accelerator on the parent menu with
2373 * gtk_menu_set_accel_group() for this to work.
2375 * Note that @accel_path string will be stored in a #GQuark.
2376 * Therefore, if you pass a static string, you can save some memory
2377 * by interning it first with g_intern_static_string().
2380 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2381 const gchar *accel_path)
2383 GtkMenuItemPrivate *priv = menu_item->priv;
2387 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2388 g_return_if_fail (accel_path == NULL ||
2389 (accel_path[0] == '<' && strchr (accel_path, '/')));
2391 widget = GTK_WIDGET (menu_item);
2393 /* store new path */
2394 priv->accel_path = (char*)g_intern_string (accel_path);
2396 /* forget accelerators associated with old path */
2397 gtk_widget_set_accel_path (widget, NULL, NULL);
2399 /* install accelerators associated with new path */
2400 parent = gtk_widget_get_parent (widget);
2401 if (GTK_IS_MENU (parent))
2403 GtkMenu *menu = GTK_MENU (parent);
2405 if (menu->priv->accel_group)
2406 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2408 menu->priv->accel_group,
2414 * gtk_menu_item_get_accel_path
2415 * @menu_item: a valid #GtkMenuItem
2417 * Retrieve the accelerator path that was previously set on @menu_item.
2419 * See gtk_menu_item_set_accel_path() for details.
2421 * Returns: the accelerator path corresponding to this menu
2422 * item's functionality, or %NULL if not set
2426 G_CONST_RETURN gchar *
2427 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2429 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2431 return menu_item->priv->accel_path;
2435 gtk_menu_item_forall (GtkContainer *container,
2436 gboolean include_internals,
2437 GtkCallback callback,
2438 gpointer callback_data)
2442 g_return_if_fail (GTK_IS_MENU_ITEM (container));
2443 g_return_if_fail (callback != NULL);
2445 child = gtk_bin_get_child (GTK_BIN (container));
2447 callback (child, callback_data);
2451 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2453 if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2454 G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2455 GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2456 !gtk_widget_is_sensitive (menu_item) ||
2457 !gtk_widget_get_visible (menu_item))
2464 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2466 GtkWidget *accel_label;
2468 if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2470 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2471 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2473 gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2474 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2475 GTK_WIDGET (menu_item));
2476 gtk_widget_show (accel_label);
2481 * gtk_menu_item_set_label:
2482 * @menu_item: a #GtkMenuItem
2483 * @label: the text you want to set
2485 * Sets @text on the @menu_item label
2490 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2493 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2495 GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2499 * gtk_menu_item_get_label:
2500 * @menu_item: a #GtkMenuItem
2502 * Sets @text on the @menu_item label
2504 * Returns: The text in the @menu_item label. This is the internal
2505 * string used by the label, and must not be modified.
2509 G_CONST_RETURN gchar *
2510 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2512 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2514 return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2518 * gtk_menu_item_set_use_underline:
2519 * @menu_item: a #GtkMenuItem
2520 * @setting: %TRUE if underlines in the text indicate mnemonics
2522 * If true, an underline in the text indicates the next character
2523 * should be used for the mnemonic accelerator key.
2528 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2533 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2535 gtk_menu_item_ensure_label (menu_item);
2537 child = gtk_bin_get_child (GTK_BIN (menu_item));
2538 if (GTK_IS_LABEL (child))
2540 gtk_label_set_use_underline (GTK_LABEL (child), setting);
2542 g_object_notify (G_OBJECT (menu_item), "use-underline");
2547 * gtk_menu_item_get_use_underline:
2548 * @menu_item: a #GtkMenuItem
2550 * Checks if an underline in the text indicates the next character
2551 * should be used for the mnemonic accelerator key.
2553 * Return value: %TRUE if an embedded underline in the label
2554 * indicates the mnemonic accelerator key.
2559 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2563 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2565 gtk_menu_item_ensure_label (menu_item);
2567 child = gtk_bin_get_child (GTK_BIN (menu_item));
2568 if (GTK_IS_LABEL (child))
2569 return gtk_label_get_use_underline (GTK_LABEL (child));
2575 * gtk_menu_item_set_reserve_indicator:
2576 * @menu_item: a #GtkMenuItem
2577 * @reserve: the new value
2579 * Sets whether the @menu_item should reserve space for
2580 * the submenu indicator, regardless if it actually has
2583 * There should be little need for applications to call
2589 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2592 GtkMenuItemPrivate *priv;
2594 g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2596 priv = menu_item->priv;
2598 if (priv->reserve_indicator != reserve)
2600 priv->reserve_indicator = reserve;
2601 gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2606 * gtk_menu_item_get_reserve_indicator:
2607 * @menu_item: a #GtkMenuItem
2609 * Returns whether the @menu_item reserves space for
2610 * the submenu indicator, regardless if it has a submenu
2613 * Returns: %TRUE if @menu_item always reserves space for the
2619 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2621 g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2623 return menu_item->priv->reserve_indicator;