]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenuitem.c
3aeb1aa48c93e56b0bc95e408492a5a8043e4274
[~andy/gtk] / gtk / gtkmenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "gtkaccellabel.h"
32 #include "gtkmain.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"
44 #include "gtkintl.h"
45 #include "gtktypebuiltins.h"
46 #include "a11y/gtkmenuitemaccessible.h"
47
48
49 /**
50  * SECTION:gtkmenuitem
51  * @Short_description: The widget used for item in menus
52  * @Title: GtkMenuItem
53  * @See_also: #GtkBin, #GtkMenuShell
54  *
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.
58  *
59  * As it derives from #GtkBin it can hold any valid child widget, altough
60  * only a few are really useful.
61  *
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 &lt;child&gt; element.
67  * <example>
68  * <title>A UI definition fragment with submenus</title>
69  * <programlisting><![CDATA[
70  * <object class="GtkMenuItem">
71  *   <child type="submenu">
72  *     <object class="GtkMenu"/>
73  *   </child>
74  * </object>
75  * ]]></programlisting>
76  * </example>
77  * </refsect2>
78  */
79
80
81 enum {
82   ACTIVATE,
83   ACTIVATE_ITEM,
84   TOGGLE_SIZE_REQUEST,
85   TOGGLE_SIZE_ALLOCATE,
86   SELECT,
87   DESELECT,
88   LAST_SIGNAL
89 };
90
91 enum {
92   PROP_0,
93   PROP_RIGHT_JUSTIFIED,
94   PROP_SUBMENU,
95   PROP_ACCEL_PATH,
96   PROP_LABEL,
97   PROP_USE_UNDERLINE,
98
99   /* activatable properties */
100   PROP_ACTIVATABLE_RELATED_ACTION,
101   PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
102 };
103
104
105 static void gtk_menu_item_dispose        (GObject          *object);
106 static void gtk_menu_item_set_property   (GObject          *object,
107                                           guint             prop_id,
108                                           const GValue     *value,
109                                           GParamSpec       *pspec);
110 static void gtk_menu_item_get_property   (GObject          *object,
111                                           guint             prop_id,
112                                           GValue           *value,
113                                           GParamSpec       *pspec);
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,
126                                           cairo_t          *cr);
127 static void gtk_menu_item_parent_set     (GtkWidget        *widget,
128                                           GtkWidget        *previous_parent);
129
130
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,
136                                                      gint        *requisition);
137 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
138                                                      gint         allocation);
139 static gboolean gtk_menu_item_mnemonic_activate     (GtkWidget   *widget,
140                                                      gboolean     group_cycling);
141
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,
145                                           gint             *x,
146                                           gint             *y,
147                                           gboolean         *push_in,
148                                           gpointer          user_data);
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,
155                                                   guint      signal_id);
156
157 static void gtk_real_menu_item_set_label (GtkMenuItem     *menu_item,
158                                           const gchar     *label);
159 static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
160
161 static void gtk_menu_item_get_preferred_width            (GtkWidget           *widget,
162                                                           gint                *minimum_size,
163                                                           gint                *natural_size);
164 static void gtk_menu_item_get_preferred_height           (GtkWidget           *widget,
165                                                           gint                *minimum_size,
166                                                           gint                *natural_size);
167 static void gtk_menu_item_get_preferred_height_for_width (GtkWidget           *widget,
168                                                           gint                 for_size,
169                                                           gint                *minimum_size,
170                                                           gint                *natural_size);
171
172 static void gtk_menu_item_buildable_interface_init (GtkBuildableIface   *iface);
173 static void gtk_menu_item_buildable_add_child      (GtkBuildable        *buildable,
174                                                     GtkBuilder          *builder,
175                                                     GObject             *child,
176                                                     const gchar         *type);
177 static void gtk_menu_item_buildable_custom_finished(GtkBuildable        *buildable,
178                                                     GtkBuilder          *builder,
179                                                     GObject             *child,
180                                                     const gchar         *tagname,
181                                                     gpointer             user_data);
182
183 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
184 static void gtk_menu_item_update                     (GtkActivatable       *activatable,
185                                                       GtkAction            *action,
186                                                       const gchar          *property_name);
187 static void gtk_menu_item_sync_action_properties     (GtkActivatable       *activatable,
188                                                       GtkAction            *action);
189 static void gtk_menu_item_set_related_action         (GtkMenuItem          *menu_item, 
190                                                       GtkAction            *action);
191 static void gtk_menu_item_set_use_action_appearance  (GtkMenuItem          *menu_item, 
192                                                       gboolean              use_appearance);
193
194 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
195
196 static GtkBuildableIface *parent_buildable_iface;
197
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))
203
204 static void
205 gtk_menu_item_class_init (GtkMenuItemClass *klass)
206 {
207   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
209   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
210
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;
214
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;
231
232   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
233
234   container_class->forall = gtk_menu_item_forall;
235
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;
244
245   klass->hide_on_activate = TRUE;
246
247   /**
248    * GtkMenuItem::activate:
249    * @menuitem: the object which received the signal.
250    *
251    * Emitted when the item is activated.
252    */
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),
258                   NULL, NULL,
259                   _gtk_marshal_VOID__VOID,
260                   G_TYPE_NONE, 0);
261   widget_class->activate_signal = menu_item_signals[ACTIVATE];
262
263   /**
264    * GtkMenuItem::activate-item:
265    * @menuitem: the object which received the signal.
266    *
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.
270    */
271   menu_item_signals[ACTIVATE_ITEM] =
272     g_signal_new (I_("activate-item"),
273                   G_OBJECT_CLASS_TYPE (gobject_class),
274                   G_SIGNAL_RUN_FIRST,
275                   G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
276                   NULL, NULL,
277                   _gtk_marshal_VOID__VOID,
278                   G_TYPE_NONE, 0);
279
280   menu_item_signals[TOGGLE_SIZE_REQUEST] =
281     g_signal_new (I_("toggle-size-request"),
282                   G_OBJECT_CLASS_TYPE (gobject_class),
283                   G_SIGNAL_RUN_FIRST,
284                   G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
285                   NULL, NULL,
286                   _gtk_marshal_VOID__POINTER,
287                   G_TYPE_NONE, 1,
288                   G_TYPE_POINTER);
289
290   menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
291     g_signal_new (I_("toggle-size-allocate"),
292                   G_OBJECT_CLASS_TYPE (gobject_class),
293                   G_SIGNAL_RUN_FIRST,
294                   G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
295                   NULL, NULL,
296                   _gtk_marshal_VOID__INT,
297                   G_TYPE_NONE, 1,
298                   G_TYPE_INT);
299
300   menu_item_signals[SELECT] =
301     g_signal_new (I_("select"),
302                   G_OBJECT_CLASS_TYPE (gobject_class),
303                   G_SIGNAL_RUN_FIRST,
304                   G_STRUCT_OFFSET (GtkMenuItemClass, select),
305                   NULL, NULL,
306                   _gtk_marshal_VOID__VOID,
307                   G_TYPE_NONE, 0);
308
309   menu_item_signals[DESELECT] =
310     g_signal_new (I_("deselect"),
311                   G_OBJECT_CLASS_TYPE (gobject_class),
312                   G_SIGNAL_RUN_FIRST,
313                   G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
314                   NULL, NULL,
315                   _gtk_marshal_VOID__VOID,
316                   G_TYPE_NONE, 0);
317
318   /**
319    * GtkMenuItem:right-justified:
320    *
321    * Sets whether the menu item appears justified
322    * at the right side of a menu bar.
323    *
324    * Since: 2.14
325    */
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"),
331                                                          FALSE,
332                                                          GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
333
334   /**
335    * GtkMenuItem:submenu:
336    *
337    * The submenu attached to the menu item, or %NULL if it has none.
338    *
339    * Since: 2.12
340    */
341   g_object_class_install_property (gobject_class,
342                                    PROP_SUBMENU,
343                                    g_param_spec_object ("submenu",
344                                                         P_("Submenu"),
345                                                         P_("The submenu attached to the menu item, or NULL if it has none"),
346                                                         GTK_TYPE_MENU,
347                                                         GTK_PARAM_READWRITE));
348
349   /**
350    * GtkMenuItem:accel-path:
351    *
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.
355    *
356    * Since: 2.14
357    */
358   g_object_class_install_property (gobject_class,
359                                    PROP_ACCEL_PATH,
360                                    g_param_spec_string ("accel-path",
361                                                         P_("Accel Path"),
362                                                         P_("Sets the accelerator path of the menu item"),
363                                                         NULL,
364                                                         GTK_PARAM_READWRITE));
365
366   /**
367    * GtkMenuItem:label:
368    *
369    * The text for the child label.
370    *
371    * Since: 2.16
372    */
373   g_object_class_install_property (gobject_class,
374                                    PROP_LABEL,
375                                    g_param_spec_string ("label",
376                                                         P_("Label"),
377                                                         P_("The text for the child label"),
378                                                         "",
379                                                         GTK_PARAM_READWRITE));
380
381   /**
382    * GtkMenuItem:use-underline:
383    *
384    * %TRUE if underlines in the text indicate mnemonics.
385    *
386    * Since: 2.16
387    */
388   g_object_class_install_property (gobject_class,
389                                    PROP_USE_UNDERLINE,
390                                    g_param_spec_boolean ("use-underline",
391                                                          P_("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"),
395                                                          FALSE,
396                                                          GTK_PARAM_READWRITE));
397
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");
400
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,
406                                                                      GTK_SHADOW_NONE,
407                                                                      GTK_PARAM_READABLE),
408                                                   gtk_rc_property_parse_enum);
409
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",
414                                                              0,
415                                                              G_MAXINT,
416                                                              3,
417                                                              GTK_PARAM_READABLE));
418
419   gtk_widget_class_install_style_property (widget_class,
420                                            g_param_spec_int ("toggle-spacing",
421                                                              "Icon Spacing",
422                                                              "Space between icon and label",
423                                                              0,
424                                                              G_MAXINT,
425                                                              5,
426                                                              GTK_PARAM_READABLE));
427
428   gtk_widget_class_install_style_property (widget_class,
429                                            g_param_spec_int ("arrow-spacing",
430                                                              "Arrow Spacing",
431                                                              "Space between label and arrow",
432                                                              0,
433                                                              G_MAXINT,
434                                                              10,
435                                                              GTK_PARAM_READABLE));
436
437   gtk_widget_class_install_style_property (widget_class,
438                                            g_param_spec_float ("arrow-scaling",
439                                                                P_("Arrow Scaling"),
440                                                                P_("Amount of space used up by arrow, relative to the menu item's font size"),
441                                                                0.0, 2.0, 0.8,
442                                                                GTK_PARAM_READABLE));
443
444   /**
445    * GtkMenuItem:width-chars:
446    *
447    * The minimum desired width of the menu item in characters.
448    *
449    * Since: 2.14
450    */
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"),
455                                                              0, G_MAXINT, 12,
456                                                              GTK_PARAM_READABLE));
457
458   g_type_class_add_private (klass, sizeof (GtkMenuItemPrivate));
459 }
460
461 static void
462 gtk_menu_item_init (GtkMenuItem *menu_item)
463 {
464   GtkStyleContext *context;
465   GtkMenuItemPrivate *priv;
466
467   priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_item,
468                                       GTK_TYPE_MENU_ITEM,
469                                       GtkMenuItemPrivate);
470   menu_item->priv = priv;
471
472   gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
473
474   priv->action = NULL;
475   priv->use_action_appearance = TRUE;
476   
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;
482   else
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;
487   priv->timer = 0;
488   priv->action = NULL;
489
490   context = gtk_widget_get_style_context (GTK_WIDGET (menu_item));
491   gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
492 }
493
494 /**
495  * gtk_menu_item_new:
496  *
497  * Creates a new #GtkMenuItem.
498  *
499  * Returns: the newly created #GtkMenuItem
500  */
501 GtkWidget*
502 gtk_menu_item_new (void)
503 {
504   return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
505 }
506
507 /**
508  * gtk_menu_item_new_with_label:
509  * @label: the text for the label
510  *
511  * Creates a new #GtkMenuItem whose child is a #GtkLabel.
512  *
513  * Returns: the newly created #GtkMenuItem
514  */
515 GtkWidget*
516 gtk_menu_item_new_with_label (const gchar *label)
517 {
518   return g_object_new (GTK_TYPE_MENU_ITEM,
519                        "label", label,
520                        NULL);
521 }
522
523
524 /**
525  * gtk_menu_item_new_with_mnemonic:
526  * @label: The text of the button, with an underscore in front of the
527  *     mnemonic character
528  *
529  * Creates a new #GtkMenuItem containing a label.
530  *
531  * The label will be created using gtk_label_new_with_mnemonic(),
532  * so underscores in @label indicate the mnemonic for the menu item.
533  *
534  * Returns: a new #GtkMenuItem
535  */
536 GtkWidget*
537 gtk_menu_item_new_with_mnemonic (const gchar *label)
538 {
539   return g_object_new (GTK_TYPE_MENU_ITEM,
540                        "use-underline", TRUE,
541                        "label", label,
542                        NULL);
543 }
544
545 static void
546 gtk_menu_item_dispose (GObject *object)
547 {
548   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
549   GtkMenuItemPrivate *priv = menu_item->priv;
550
551   if (priv->action)
552     {
553       gtk_action_disconnect_accelerator (priv->action);
554       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
555       priv->action = NULL;
556     }
557   G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
558 }
559
560 static void
561 gtk_menu_item_do_set_right_justified (GtkMenuItem *menu_item,
562                                       gboolean     right_justified)
563 {
564   GtkMenuItemPrivate *priv = menu_item->priv;
565
566   right_justified = right_justified != FALSE;
567
568   if (priv->right_justify != right_justified)
569     {
570       priv->right_justify = right_justified;
571       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
572     }
573 }
574
575 static void
576 gtk_menu_item_set_property (GObject      *object,
577                             guint         prop_id,
578                             const GValue *value,
579                             GParamSpec   *pspec)
580 {
581   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
582
583   switch (prop_id)
584     {
585     case PROP_RIGHT_JUSTIFIED:
586       gtk_menu_item_do_set_right_justified (menu_item, g_value_get_boolean (value));
587       break;
588     case PROP_SUBMENU:
589       gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
590       break;
591     case PROP_ACCEL_PATH:
592       gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
593       break;
594     case PROP_LABEL:
595       gtk_menu_item_set_label (menu_item, g_value_get_string (value));
596       break;
597     case PROP_USE_UNDERLINE:
598       gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
599       break;
600     case PROP_ACTIVATABLE_RELATED_ACTION:
601       gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
602       break;
603     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
604       gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
605       break;
606     default:
607       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
608       break;
609     }
610 }
611
612 static void
613 gtk_menu_item_get_property (GObject    *object,
614                             guint       prop_id,
615                             GValue     *value,
616                             GParamSpec *pspec)
617 {
618   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
619   GtkMenuItemPrivate *priv = menu_item->priv;
620
621   switch (prop_id)
622     {
623     case PROP_RIGHT_JUSTIFIED:
624       g_value_set_boolean (value, menu_item->priv->right_justify);
625       break;
626     case PROP_SUBMENU:
627       g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
628       break;
629     case PROP_ACCEL_PATH:
630       g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
631       break;
632     case PROP_LABEL:
633       g_value_set_string (value, gtk_menu_item_get_label (menu_item));
634       break;
635     case PROP_USE_UNDERLINE:
636       g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
637       break;
638     case PROP_ACTIVATABLE_RELATED_ACTION:
639       g_value_set_object (value, priv->action);
640       break;
641     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
642       g_value_set_boolean (value, priv->use_action_appearance);
643       break;
644     default:
645       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
646       break;
647     }
648 }
649
650 static void
651 gtk_menu_item_destroy (GtkWidget *widget)
652 {
653   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
654   GtkMenuItemPrivate *priv = menu_item->priv;
655
656   if (priv->submenu)
657     gtk_widget_destroy (priv->submenu);
658
659   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
660 }
661
662 static void
663 gtk_menu_item_detacher (GtkWidget *widget,
664                         GtkMenu   *menu)
665 {
666   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
667   GtkMenuItemPrivate *priv = menu_item->priv;
668
669   g_return_if_fail (priv->submenu == (GtkWidget*) menu);
670
671   priv->submenu = NULL;
672 }
673
674 static void
675 get_arrow_size (GtkWidget *widget,
676                 GtkWidget *child,
677                 gint      *size)
678 {
679   GtkStyleContext  *style_context;
680   GtkStateFlags     state;
681   PangoContext     *context;
682   PangoFontMetrics *metrics;
683   gfloat            arrow_scaling;
684
685   g_assert (size);
686
687   gtk_widget_style_get (widget,
688                         "arrow-scaling", &arrow_scaling,
689                         NULL);
690
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);
694
695   metrics = pango_context_get_metrics (context,
696                                        gtk_style_context_get_font (style_context, state),
697                                        pango_context_get_language (context));
698
699   *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
700                          pango_font_metrics_get_descent (metrics)));
701
702   pango_font_metrics_unref (metrics);
703
704   *size = *size * arrow_scaling;
705 }
706
707
708 static void
709 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
710                                    gpointer   data)
711 {
712   guint *width = data;
713
714   if (GTK_IS_ACCEL_LABEL (widget))
715     {
716       guint w;
717
718       w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
719       *width = MAX (*width, w);
720     }
721   else if (GTK_IS_CONTAINER (widget))
722     gtk_container_foreach (GTK_CONTAINER (widget),
723                            gtk_menu_item_accel_width_foreach,
724                            data);
725 }
726
727 static gint
728 get_minimum_width (GtkWidget *widget)
729 {
730   GtkStyleContext *style_context;
731   GtkStateFlags state;
732   PangoContext *context;
733   PangoFontMetrics *metrics;
734   gint width;
735   gint width_chars;
736
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);
740
741   metrics = pango_context_get_metrics (context,
742                                        gtk_style_context_get_font (style_context, state),
743                                        pango_context_get_language (context));
744
745   width = pango_font_metrics_get_approximate_char_width (metrics);
746
747   pango_font_metrics_unref (metrics);
748
749   gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
750
751   return PANGO_PIXELS (width_chars * width);
752 }
753
754 static void
755 gtk_menu_item_get_preferred_width (GtkWidget *widget,
756                                    gint      *minimum_size,
757                                    gint      *natural_size)
758 {
759   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
760   GtkMenuItemPrivate *priv = menu_item->priv;
761   GtkBin *bin;
762   GtkWidget *child;
763   GtkWidget *parent;
764   guint accel_width;
765   guint horizontal_padding;
766   guint border_width;
767   GtkPackDirection pack_dir;
768   GtkPackDirection child_pack_dir;
769   gint  min_width, nat_width;
770   GtkStyleContext *context;
771   GtkStateFlags state;
772   GtkBorder padding;
773
774   min_width = nat_width = 0;
775
776   gtk_widget_style_get (widget,
777                         "horizontal-padding", &horizontal_padding,
778                         NULL);
779
780   bin = GTK_BIN (widget);
781   parent = gtk_widget_get_parent (widget);
782
783   if (GTK_IS_MENU_BAR (parent))
784     {
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));
787     }
788   else
789     {
790       pack_dir = GTK_PACK_DIRECTION_LTR;
791       child_pack_dir = GTK_PACK_DIRECTION_LTR;
792     }
793
794   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
795
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);
799
800   min_width = (border_width * 2) + padding.left + padding.right;
801
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;
805
806   nat_width = min_width;
807           
808   child = gtk_bin_get_child (bin);
809
810   if (child != NULL && gtk_widget_get_visible (child))
811     {
812       GtkMenuItemPrivate *priv = menu_item->priv;
813       gint child_min, child_nat;
814
815       gtk_widget_get_preferred_width (child, &child_min, &child_nat);
816
817       if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
818         {
819           guint arrow_spacing;
820           gint  arrow_size;
821           
822           gtk_widget_style_get (widget,
823                                 "arrow-spacing", &arrow_spacing,
824                                 NULL);
825
826           get_arrow_size (widget, child, &arrow_size);
827
828           gtk_widget_style_get (widget,
829                                 "arrow-spacing", &arrow_spacing,
830                                 NULL);
831
832           get_arrow_size (widget, child, &arrow_size);
833
834           min_width += arrow_size;
835           min_width += arrow_spacing;
836
837           min_width = MAX (min_width, get_minimum_width (widget));
838           nat_width = min_width;
839         }
840
841       min_width += child_min;
842       nat_width += child_nat;
843     }
844
845   accel_width = 0;
846   gtk_container_foreach (GTK_CONTAINER (menu_item),
847                          gtk_menu_item_accel_width_foreach,
848                          &accel_width);
849   priv->accelerator_width = accel_width;
850
851   if (minimum_size)
852     *minimum_size = min_width;
853
854   if (natural_size)
855     *natural_size = nat_width;
856 }
857
858 static void
859 gtk_menu_item_real_get_height (GtkWidget *widget,
860                                gint       for_size,
861                                gint      *minimum_size,
862                                gint      *natural_size)
863 {
864   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
865   GtkMenuItemPrivate *priv = menu_item->priv;
866   GtkBin *bin;
867   GtkStyleContext *context;
868   GtkStateFlags state;
869   GtkBorder padding;
870   GtkWidget *child;
871   GtkWidget *parent;
872   guint accel_width;
873   guint horizontal_padding;
874   guint border_width;
875   GtkPackDirection pack_dir;
876   GtkPackDirection child_pack_dir;
877   gint min_height, nat_height;
878   gint avail_size;
879
880   min_height = nat_height = 0;
881
882   context = gtk_widget_get_style_context (widget);
883   state = gtk_widget_get_state_flags (widget);
884   gtk_style_context_get_padding (context, state, &padding);
885
886   gtk_widget_style_get (widget,
887                         "horizontal-padding", &horizontal_padding,
888                         NULL);
889
890   bin = GTK_BIN (widget);
891   parent = gtk_widget_get_parent (widget);
892
893   if (GTK_IS_MENU_BAR (parent))
894     {
895       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
896       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
897     }
898   else
899     {
900       pack_dir = GTK_PACK_DIRECTION_LTR;
901       child_pack_dir = GTK_PACK_DIRECTION_LTR;
902     }
903
904   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
905   min_height   = (border_width * 2) + padding.top + padding.bottom;
906
907   if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
908       (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
909     min_height += 2 * horizontal_padding;
910
911   if (for_size != -1)
912     {
913       avail_size = for_size;
914       avail_size -= (border_width * 2) + padding.left + padding.right;
915
916       if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
917           (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
918         avail_size -= 2 * horizontal_padding;
919     }
920
921   nat_height = min_height;
922
923   child = gtk_bin_get_child (bin);
924
925   if (child != NULL && gtk_widget_get_visible (child))
926     {
927       gint child_min, child_nat;
928       gint arrow_size = 0;
929       guint arrow_spacing = 0;
930
931       if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
932         {
933           gtk_widget_style_get (widget,
934                                 "arrow-spacing", &arrow_spacing,
935                                 NULL);
936           get_arrow_size (widget, child, &arrow_size);
937         }
938
939       if (for_size != -1)
940         {
941           avail_size -= arrow_size;
942           avail_size -= arrow_spacing;
943
944           gtk_widget_get_preferred_height_for_width (child,
945                                                      avail_size,
946                                                      &child_min,
947                                                      &child_nat);
948         }
949       else
950         {
951           gtk_widget_get_preferred_height (child, &child_min, &child_nat);
952         }
953
954       min_height += child_min;
955       nat_height += child_nat;
956
957       min_height = MAX (min_height, arrow_size);
958       nat_height = MAX (nat_height, arrow_size);
959     }
960   else /* separator item */
961     {
962       gboolean wide_separators;
963       gint     separator_height;
964
965       gtk_widget_style_get (widget,
966                             "wide-separators",  &wide_separators,
967                             "separator-height", &separator_height,
968                             NULL);
969
970       if (wide_separators)
971         {
972           min_height += separator_height;
973           nat_height += separator_height;
974         }
975       else
976         {
977           /* force odd, so that we can have the same space above and
978            * below the line.
979            */
980           if (min_height % 2 == 0)
981             min_height += 1;
982           if (nat_height % 2 == 0)
983             nat_height += 1;
984         }
985     }
986
987   accel_width = 0;
988   gtk_container_foreach (GTK_CONTAINER (menu_item),
989                          gtk_menu_item_accel_width_foreach,
990                          &accel_width);
991   priv->accelerator_width = accel_width;
992
993   if (minimum_size)
994     *minimum_size = min_height;
995
996   if (natural_size)
997     *natural_size = nat_height;
998 }
999
1000 static void
1001 gtk_menu_item_get_preferred_height (GtkWidget *widget,
1002                                     gint      *minimum_size,
1003                                     gint      *natural_size)
1004 {
1005   gtk_menu_item_real_get_height (widget, -1, minimum_size, natural_size);
1006 }
1007
1008 static void
1009 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
1010                                               gint       for_size,
1011                                               gint      *minimum_size,
1012                                               gint      *natural_size)
1013 {
1014   gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size);
1015 }
1016
1017 static void
1018 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1019 {
1020   parent_buildable_iface = g_type_interface_peek_parent (iface);
1021   iface->add_child = gtk_menu_item_buildable_add_child;
1022   iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1023 }
1024
1025 static void
1026 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1027                                    GtkBuilder   *builder,
1028                                    GObject      *child,
1029                                    const gchar  *type)
1030 {
1031   if (type && strcmp (type, "submenu") == 0)
1032         gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1033                                    GTK_WIDGET (child));
1034   else
1035     parent_buildable_iface->add_child (buildable, builder, child, type);
1036 }
1037
1038
1039 static void
1040 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1041                                          GtkBuilder   *builder,
1042                                          GObject      *child,
1043                                          const gchar  *tagname,
1044                                          gpointer      user_data)
1045 {
1046   GtkWidget *toplevel;
1047
1048   if (strcmp (tagname, "accelerator") == 0)
1049     {
1050       GtkMenuShell *menu_shell;
1051       GtkWidget *attach;
1052
1053       menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1054       if (menu_shell)
1055         {
1056           while (GTK_IS_MENU (menu_shell) &&
1057                  (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1058             menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1059
1060           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1061         }
1062       else
1063         {
1064           /* Fall back to something ... */
1065           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1066
1067           g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1068                      gtk_buildable_get_name (buildable));
1069         }
1070
1071       /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1072       _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1073     }
1074   else
1075     parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1076 }
1077
1078
1079 static void
1080 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1081 {
1082   iface->update = gtk_menu_item_update;
1083   iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1084 }
1085
1086 static void
1087 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1088 {
1089   GtkWidget *child;
1090
1091   child = gtk_bin_get_child (GTK_BIN (menu_item));
1092
1093   if (GTK_IS_LABEL (child))
1094     {
1095       const gchar *label;
1096
1097       label = gtk_action_get_label (action);
1098       gtk_menu_item_set_label (menu_item, label);
1099     }
1100 }
1101
1102 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1103
1104 static void
1105 gtk_menu_item_update (GtkActivatable *activatable,
1106                       GtkAction      *action,
1107                       const gchar    *property_name)
1108 {
1109   GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1110   GtkMenuItemPrivate *priv = menu_item->priv;
1111
1112   if (strcmp (property_name, "visible") == 0)
1113     _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1114                                    _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1115   else if (strcmp (property_name, "sensitive") == 0)
1116     gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1117   else if (priv->use_action_appearance)
1118     {
1119       if (strcmp (property_name, "label") == 0)
1120         activatable_update_label (menu_item, action);
1121     }
1122 }
1123
1124 static void
1125 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1126                                       GtkAction      *action)
1127 {
1128   GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1129   GtkMenuItemPrivate *priv = menu_item->priv;
1130   GtkWidget *label;
1131
1132   if (!priv->use_action_appearance || !action)
1133     {
1134       label = gtk_bin_get_child (GTK_BIN (menu_item));
1135
1136       if (GTK_IS_ACCEL_LABEL (label))
1137         gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1138     }
1139
1140   if (!action)
1141     return;
1142
1143   _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1144                                  _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1145
1146   gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1147
1148   if (priv->use_action_appearance)
1149     {
1150       label = gtk_bin_get_child (GTK_BIN (menu_item));
1151
1152       /* make sure label is a label, deleting it otherwise */
1153       if (label && !GTK_IS_LABEL (label))
1154         {
1155           gtk_container_remove (GTK_CONTAINER (menu_item), label);
1156           label = NULL;
1157         }
1158       /* Make sure that menu_item has a label and that any
1159        * accelerators are set */
1160       gtk_menu_item_ensure_label (menu_item);
1161       gtk_menu_item_set_use_underline (menu_item, TRUE);
1162       /* Make label point to the menu_item's label */
1163       label = gtk_bin_get_child (GTK_BIN (menu_item));
1164
1165       if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1166         {
1167           gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1168           gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1169                                              gtk_action_get_accel_closure (action));
1170         }
1171
1172       activatable_update_label (menu_item, action);
1173     }
1174 }
1175
1176 static void
1177 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1178                                   GtkAction   *action)
1179 {
1180     GtkMenuItemPrivate *priv = menu_item->priv;
1181
1182     if (priv->action == action)
1183       return;
1184
1185     if (priv->action)
1186       {
1187         gtk_action_disconnect_accelerator (priv->action);
1188       }
1189
1190     if (action)
1191       {
1192         const gchar *accel_path;
1193
1194         accel_path = gtk_action_get_accel_path (action);
1195         if (accel_path)
1196           {
1197             gtk_action_connect_accelerator (action);
1198             gtk_menu_item_set_accel_path (menu_item, accel_path);
1199           }
1200       }
1201
1202     gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1203
1204     priv->action = action;
1205 }
1206
1207 static void
1208 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1209                                          gboolean     use_appearance)
1210 {
1211     GtkMenuItemPrivate *priv = menu_item->priv;
1212
1213     if (priv->use_action_appearance != use_appearance)
1214       {
1215         priv->use_action_appearance = use_appearance;
1216
1217         gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1218       }
1219 }
1220
1221
1222 /**
1223  * gtk_menu_item_set_submenu:
1224  * @menu_item: a #GtkMenuItem
1225  * @submenu: (allow-none): the submenu, or %NULL
1226  *
1227  * Sets or replaces the menu item's submenu, or removes it when a %NULL
1228  * submenu is passed.
1229  */
1230 void
1231 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1232                            GtkWidget   *submenu)
1233 {
1234   GtkMenuItemPrivate *priv = menu_item->priv;
1235
1236   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1237   g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1238
1239   if (priv->submenu != submenu)
1240     {
1241       if (priv->submenu)
1242         gtk_menu_detach (GTK_MENU (priv->submenu));
1243
1244       if (submenu)
1245         {
1246           priv->submenu = submenu;
1247           gtk_menu_attach_to_widget (GTK_MENU (submenu),
1248                                      GTK_WIDGET (menu_item),
1249                                      gtk_menu_item_detacher);
1250         }
1251
1252       if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1253         gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1254
1255       g_object_notify (G_OBJECT (menu_item), "submenu");
1256     }
1257 }
1258
1259 /**
1260  * gtk_menu_item_get_submenu:
1261  * @menu_item: a #GtkMenuItem
1262  *
1263  * Gets the submenu underneath this menu item, if any.
1264  * See gtk_menu_item_set_submenu().
1265  *
1266  * Return value: (transfer none): submenu for this menu item, or %NULL if none
1267  */
1268 GtkWidget *
1269 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1270 {
1271   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1272
1273   return menu_item->priv->submenu;
1274 }
1275
1276 void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1277                                    GtkSubmenuPlacement  placement);
1278
1279 void
1280 _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1281                              GtkSubmenuPlacement  placement)
1282 {
1283   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1284
1285   menu_item->priv->submenu_placement = placement;
1286 }
1287
1288 void
1289 gtk_menu_item_select (GtkMenuItem *menu_item)
1290 {
1291   GtkWidget *parent;
1292
1293   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1294
1295   g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1296
1297   /* Enable themeing of the parent menu item depending on whether
1298    * something is selected in its submenu
1299    */
1300   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1301   if (GTK_IS_MENU (parent))
1302     {
1303       GtkMenu *menu = GTK_MENU (parent);
1304
1305       if (menu->priv->parent_menu_item)
1306         gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1307     }
1308 }
1309
1310 /**
1311  * gtk_menu_item_deselect:
1312  * @menu_item: the menu item
1313  *
1314  * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1315  * exactly like #gtk_item_deselect.
1316  */
1317 void
1318 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1319 {
1320   GtkWidget *parent;
1321
1322   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1323
1324   g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1325
1326   /* Enable themeing of the parent menu item depending on whether
1327    * something is selected in its submenu
1328    */
1329   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1330   if (GTK_IS_MENU (parent))
1331     {
1332       GtkMenu *menu = GTK_MENU (parent);
1333
1334       if (menu->priv->parent_menu_item)
1335         gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1336     }
1337 }
1338
1339 /**
1340  * gtk_menu_item_activate:
1341  * @menu_item: the menu item
1342  *
1343  * Emits the #GtkMenuItem::activate signal on the given item
1344  */
1345 void
1346 gtk_menu_item_activate (GtkMenuItem *menu_item)
1347 {
1348   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1349
1350   g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1351 }
1352
1353 /**
1354  * gtk_menu_item_toggle_size_request:
1355  * @menu_item: the menu item
1356  * @requisition: the requisition to use as signal data.
1357  *
1358  * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1359  */
1360 void
1361 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1362                                    gint        *requisition)
1363 {
1364   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1365
1366   g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1367 }
1368
1369 /**
1370  * gtk_menu_item_toggle_size_allocate:
1371  * @menu_item: the menu item.
1372  * @allocation: the allocation to use as signal data.
1373  *
1374  * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1375  */
1376 void
1377 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1378                                     gint         allocation)
1379 {
1380   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1381
1382   g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1383 }
1384
1385 static void
1386 gtk_menu_item_size_allocate (GtkWidget     *widget,
1387                              GtkAllocation *allocation)
1388 {
1389   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1390   GtkMenuItemPrivate *priv = menu_item->priv;
1391   GtkBin *bin;
1392   GtkAllocation child_allocation;
1393   GtkTextDirection direction;
1394   GtkPackDirection pack_dir;
1395   GtkPackDirection child_pack_dir;
1396   GtkWidget *child;
1397   GtkWidget *parent;
1398
1399   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1400   g_return_if_fail (allocation != NULL);
1401
1402   bin = GTK_BIN (widget);
1403
1404   direction = gtk_widget_get_direction (widget);
1405
1406   parent = gtk_widget_get_parent (widget);
1407   if (GTK_IS_MENU_BAR (parent))
1408     {
1409       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1410       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1411     }
1412   else
1413     {
1414       pack_dir = GTK_PACK_DIRECTION_LTR;
1415       child_pack_dir = GTK_PACK_DIRECTION_LTR;
1416     }
1417
1418   gtk_widget_set_allocation (widget, allocation);
1419
1420   child = gtk_bin_get_child (bin);
1421   if (child)
1422     {
1423       GtkStyleContext *context;
1424       GtkStateFlags state;
1425       GtkBorder padding;
1426       guint horizontal_padding;
1427       guint border_width;
1428
1429       context = gtk_widget_get_style_context (widget);
1430       state = gtk_widget_get_state_flags (widget);
1431       gtk_style_context_get_padding (context, state, &padding);
1432
1433       gtk_widget_style_get (widget,
1434                             "horizontal-padding", &horizontal_padding,
1435                             NULL);
1436
1437       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1438       child_allocation.x = border_width + padding.left;
1439       child_allocation.y = border_width + padding.top;
1440
1441       child_allocation.width = allocation->width - (border_width * 2) -
1442         padding.left - padding.right;
1443       child_allocation.height = allocation->height - (border_width * 2) -
1444         padding.top - padding.bottom;
1445
1446       if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1447           (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1448         {
1449           child_allocation.x += horizontal_padding;
1450           child_allocation.width -= 2 * horizontal_padding;
1451         }
1452       else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1453                (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1454         {
1455           child_allocation.y += horizontal_padding;
1456           child_allocation.height -= 2 * horizontal_padding;
1457         }
1458
1459       if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1460           child_pack_dir == GTK_PACK_DIRECTION_RTL)
1461         {
1462           if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1463             child_allocation.x += priv->toggle_size;
1464           child_allocation.width -= priv->toggle_size;
1465         }
1466       else
1467         {
1468           if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1469             child_allocation.y += priv->toggle_size;
1470           child_allocation.height -= priv->toggle_size;
1471         }
1472
1473       child_allocation.x += allocation->x;
1474       child_allocation.y += allocation->y;
1475
1476       if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1477         {
1478           guint arrow_spacing;
1479           gint  arrow_size;
1480
1481           gtk_widget_style_get (widget,
1482                                 "arrow-spacing", &arrow_spacing,
1483                                 NULL);
1484
1485           get_arrow_size (widget, child, &arrow_size);
1486
1487           if (direction == GTK_TEXT_DIR_RTL)
1488             child_allocation.x += arrow_size + arrow_spacing;
1489           child_allocation.width -= arrow_size + arrow_spacing;
1490         }
1491       
1492       if (child_allocation.width < 1)
1493         child_allocation.width = 1;
1494
1495       gtk_widget_size_allocate (child, &child_allocation);
1496     }
1497
1498   if (gtk_widget_get_realized (widget))
1499     gdk_window_move_resize (priv->event_window,
1500                             allocation->x, allocation->y,
1501                             allocation->width, allocation->height);
1502
1503   if (priv->submenu)
1504     gtk_menu_reposition (GTK_MENU (priv->submenu));
1505 }
1506
1507 static void
1508 gtk_menu_item_realize (GtkWidget *widget)
1509 {
1510   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1511   GtkMenuItemPrivate *priv = menu_item->priv;
1512   GtkAllocation allocation;
1513   GdkWindow *window;
1514   GdkWindowAttr attributes;
1515   gint attributes_mask;
1516
1517   gtk_widget_set_realized (widget, TRUE);
1518
1519   window = gtk_widget_get_parent_window (widget);
1520   gtk_widget_set_window (widget, window);
1521   g_object_ref (window);
1522
1523   gtk_widget_get_allocation (widget, &allocation);
1524
1525   attributes.x = allocation.x;
1526   attributes.y = allocation.y;
1527   attributes.width = allocation.width;
1528   attributes.height = allocation.height;
1529   attributes.window_type = GDK_WINDOW_CHILD;
1530   attributes.wclass = GDK_INPUT_ONLY;
1531   attributes.event_mask = (gtk_widget_get_events (widget) |
1532                            GDK_BUTTON_PRESS_MASK |
1533                            GDK_BUTTON_RELEASE_MASK |
1534                            GDK_ENTER_NOTIFY_MASK |
1535                            GDK_LEAVE_NOTIFY_MASK |
1536                            GDK_POINTER_MOTION_MASK);
1537
1538   attributes_mask = GDK_WA_X | GDK_WA_Y;
1539
1540   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1541                                        &attributes, attributes_mask);
1542   gdk_window_set_user_data (priv->event_window, widget);
1543 }
1544
1545 static void
1546 gtk_menu_item_unrealize (GtkWidget *widget)
1547 {
1548   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1549   GtkMenuItemPrivate *priv = menu_item->priv;
1550
1551   gdk_window_set_user_data (priv->event_window, NULL);
1552   gdk_window_destroy (priv->event_window);
1553   priv->event_window = NULL;
1554
1555   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1556 }
1557
1558 static void
1559 gtk_menu_item_map (GtkWidget *widget)
1560 {
1561   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1562   GtkMenuItemPrivate *priv = menu_item->priv;
1563
1564   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1565
1566   gdk_window_show (priv->event_window);
1567 }
1568
1569 static void
1570 gtk_menu_item_unmap (GtkWidget *widget)
1571 {
1572   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1573   GtkMenuItemPrivate *priv = menu_item->priv;
1574
1575   gdk_window_hide (priv->event_window);
1576
1577   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1578 }
1579
1580 static gboolean
1581 gtk_menu_item_enter (GtkWidget        *widget,
1582                      GdkEventCrossing *event)
1583 {
1584   g_return_val_if_fail (event != NULL, FALSE);
1585
1586   return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1587 }
1588
1589 static gboolean
1590 gtk_menu_item_leave (GtkWidget        *widget,
1591                      GdkEventCrossing *event)
1592 {
1593   g_return_val_if_fail (event != NULL, FALSE);
1594
1595   return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1596 }
1597
1598 static gboolean
1599 gtk_menu_item_draw (GtkWidget *widget,
1600                     cairo_t   *cr)
1601 {
1602   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1603   GtkMenuItemPrivate *priv = menu_item->priv;
1604   GtkStateFlags state;
1605   GtkStyleContext *context;
1606   GtkBorder padding;
1607   GtkWidget *child, *parent;
1608   gint x, y, w, h, width, height;
1609   guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1610
1611   state = gtk_widget_get_state_flags (widget);
1612   context = gtk_widget_get_style_context (widget);
1613   width = gtk_widget_get_allocated_width (widget);
1614   height = gtk_widget_get_allocated_height (widget);
1615
1616   x = border_width;
1617   y = border_width;
1618   w = width - border_width * 2;
1619   h = height - border_width * 2;
1620
1621   child = gtk_bin_get_child (GTK_BIN (menu_item));
1622   parent = gtk_widget_get_parent (widget);
1623
1624   gtk_style_context_save (context);
1625   gtk_style_context_set_state (context, state);
1626
1627   gtk_style_context_get_padding (context, state, &padding);
1628
1629   if (GTK_IS_MENU_BAR (parent))
1630     gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1631
1632   if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1633     {
1634       gtk_render_background (context, cr, x, y, w, h);
1635       gtk_render_frame (context, cr, x, y, w, h);
1636     }
1637
1638   if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1639     {
1640       gint arrow_x, arrow_y;
1641       gint arrow_size;
1642       guint horizontal_padding;
1643       GtkTextDirection direction;
1644       gdouble angle;
1645
1646       direction = gtk_widget_get_direction (widget);
1647
1648       gtk_widget_style_get (widget,
1649                             "horizontal-padding", &horizontal_padding,
1650                             NULL);
1651
1652       get_arrow_size (widget, child, &arrow_size);
1653
1654       if (direction == GTK_TEXT_DIR_LTR)
1655         {
1656           arrow_x = x + w - horizontal_padding - arrow_size;
1657           angle = G_PI / 2;
1658         }
1659       else
1660         {
1661           arrow_x = x + horizontal_padding;
1662           angle = (3 * G_PI) / 2;
1663         }
1664
1665       arrow_y = y + (h - arrow_size) / 2;
1666
1667       gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1668     }
1669   else if (!child)
1670     {
1671       gboolean wide_separators;
1672       gint     separator_height;
1673       guint    horizontal_padding;
1674
1675       gtk_widget_style_get (widget,
1676                             "wide-separators",    &wide_separators,
1677                             "separator-height",   &separator_height,
1678                             "horizontal-padding", &horizontal_padding,
1679                             NULL);
1680       if (wide_separators)
1681         gtk_render_frame (context, cr,
1682                           x + horizontal_padding + padding.left,
1683                           y + padding.top,
1684                           w - (2 * horizontal_padding) - padding.left - padding.right,
1685                           separator_height);
1686       else
1687         gtk_render_line (context, cr,
1688                          x + horizontal_padding + padding.left,
1689                          y + padding.top,
1690                          x + w - horizontal_padding - padding.right - 1,
1691                          y + padding.top);
1692     }
1693
1694   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1695
1696   gtk_style_context_restore (context);
1697
1698   return FALSE;
1699 }
1700
1701 /**
1702  * gtk_menu_item_select:
1703  * @menu_item: the menu item
1704  *
1705  * Emits the #GtkMenuItem::select signal on the given item. Behaves
1706  * exactly like #gtk_item_select.
1707  */
1708 static void
1709 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1710 {
1711   GtkMenuItemPrivate *priv = menu_item->priv;
1712   gboolean touchscreen_mode;
1713
1714   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1715                 "gtk-touchscreen-mode", &touchscreen_mode,
1716                 NULL);
1717
1718   if (!touchscreen_mode && priv->submenu &&
1719       (!gtk_widget_get_mapped (priv->submenu) ||
1720        GTK_MENU (priv->submenu)->priv->tearoff_active))
1721     {
1722       _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1723     }
1724
1725   gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1726                               GTK_STATE_FLAG_PRELIGHT, FALSE);
1727   gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1728 }
1729
1730 static void
1731 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1732 {
1733   GtkMenuItemPrivate *priv = menu_item->priv;
1734
1735   if (priv->submenu)
1736     _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1737
1738   gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1739                                 GTK_STATE_FLAG_PRELIGHT);
1740   gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1741 }
1742
1743 static gboolean
1744 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1745                                  gboolean   group_cycling)
1746 {
1747   GtkWidget *parent;
1748
1749   parent = gtk_widget_get_parent (widget);
1750
1751   if (GTK_IS_MENU_SHELL (parent))
1752     _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1753
1754   if (group_cycling &&
1755       parent &&
1756       GTK_IS_MENU_SHELL (parent) &&
1757       GTK_MENU_SHELL (parent)->priv->active)
1758     {
1759       gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1760     }
1761   else
1762     g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1763
1764   return TRUE;
1765 }
1766
1767 static void
1768 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1769 {
1770   GtkMenuItemPrivate *priv = menu_item->priv;
1771
1772   if (priv->action)
1773     gtk_action_activate (priv->action);
1774 }
1775
1776
1777 static void
1778 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1779 {
1780   GtkMenuItemPrivate *priv = menu_item->priv;
1781   GtkWidget *parent;
1782   GtkWidget *widget;
1783
1784   widget = GTK_WIDGET (menu_item);
1785   parent = gtk_widget_get_parent (widget);
1786
1787   if (parent && GTK_IS_MENU_SHELL (parent))
1788     {
1789       GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1790
1791       if (priv->submenu == NULL)
1792         gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1793       else
1794         {
1795           gtk_menu_shell_select_item (menu_shell, widget);
1796           _gtk_menu_item_popup_submenu (widget, FALSE);
1797
1798           gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1799         }
1800     }
1801 }
1802
1803 static void
1804 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1805                                         gint        *requisition)
1806 {
1807   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1808
1809   *requisition = 0;
1810 }
1811
1812 static void
1813 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1814                                          gint         allocation)
1815 {
1816   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1817
1818   menu_item->priv->toggle_size = allocation;
1819 }
1820
1821 static void
1822 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1823                               const gchar *label)
1824 {
1825   GtkWidget *child;
1826
1827   gtk_menu_item_ensure_label (menu_item);
1828
1829   child = gtk_bin_get_child (GTK_BIN (menu_item));
1830   if (GTK_IS_LABEL (child))
1831     {
1832       gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1833
1834       g_object_notify (G_OBJECT (menu_item), "label");
1835     }
1836 }
1837
1838 static const gchar *
1839 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1840 {
1841   GtkWidget *child;
1842
1843   gtk_menu_item_ensure_label (menu_item);
1844
1845   child = gtk_bin_get_child (GTK_BIN (menu_item));
1846   if (GTK_IS_LABEL (child))
1847     return gtk_label_get_label (GTK_LABEL (child));
1848
1849   return NULL;
1850 }
1851
1852 static void
1853 free_timeval (GTimeVal *val)
1854 {
1855   g_slice_free (GTimeVal, val);
1856 }
1857
1858 static void
1859 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1860                                   gboolean   remember_exact_time)
1861 {
1862   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1863   GtkMenuItemPrivate *priv = menu_item->priv;
1864   GtkWidget *parent;
1865
1866   parent = gtk_widget_get_parent (widget);
1867
1868   if (gtk_widget_is_sensitive (priv->submenu) && parent)
1869     {
1870       gboolean take_focus;
1871       GtkMenuPositionFunc menu_position_func;
1872
1873       take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1874       gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1875
1876       if (remember_exact_time)
1877         {
1878           GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1879
1880           g_get_current_time (popup_time);
1881
1882           g_object_set_data_full (G_OBJECT (priv->submenu),
1883                                   "gtk-menu-exact-popup-time", popup_time,
1884                                   (GDestroyNotify) free_timeval);
1885         }
1886       else
1887         {
1888           g_object_set_data (G_OBJECT (priv->submenu),
1889                              "gtk-menu-exact-popup-time", NULL);
1890         }
1891
1892       /* gtk_menu_item_position_menu positions the submenu from the
1893        * menuitems position. If the menuitem doesn't have a window,
1894        * that doesn't work. In that case we use the default
1895        * positioning function instead which places the submenu at the
1896        * mouse cursor.
1897        */
1898       if (gtk_widget_get_window (widget))
1899         menu_position_func = gtk_menu_item_position_menu;
1900       else
1901         menu_position_func = NULL;
1902
1903       gtk_menu_popup (GTK_MENU (priv->submenu),
1904                       parent,
1905                       widget,
1906                       menu_position_func,
1907                       menu_item,
1908                       GTK_MENU_SHELL (parent)->priv->button,
1909                       0);
1910     }
1911
1912   /* Enable themeing of the parent menu item depending on whether
1913    * its submenu is shown or not.
1914    */
1915   gtk_widget_queue_draw (widget);
1916 }
1917
1918 static gint
1919 gtk_menu_item_popup_timeout (gpointer data)
1920 {
1921   GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1922   GtkMenuItemPrivate *priv = menu_item->priv;
1923   GtkWidget *parent;
1924
1925   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1926
1927   if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1928       (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1929     {
1930       gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1931       if (priv->timer_from_keypress && priv->submenu)
1932         GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1933     }
1934
1935   priv->timer = 0;
1936
1937   return FALSE;
1938 }
1939
1940 static gint
1941 get_popup_delay (GtkWidget *widget)
1942 {
1943   GtkWidget *parent;
1944
1945   parent = gtk_widget_get_parent (widget);
1946   if (GTK_IS_MENU_SHELL (parent))
1947     {
1948       return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1949     }
1950   else
1951     {
1952       gint popup_delay;
1953
1954       g_object_get (gtk_widget_get_settings (widget),
1955                     "gtk-menu-popup-delay", &popup_delay,
1956                     NULL);
1957
1958       return popup_delay;
1959     }
1960 }
1961
1962 void
1963 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1964                               gboolean   with_delay)
1965 {
1966   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1967   GtkMenuItemPrivate *priv = menu_item->priv;
1968
1969   if (priv->timer)
1970     {
1971       g_source_remove (priv->timer);
1972       priv->timer = 0;
1973       with_delay = FALSE;
1974     }
1975
1976   if (with_delay)
1977     {
1978       gint popup_delay = get_popup_delay (widget);
1979
1980       if (popup_delay > 0)
1981         {
1982           GdkEvent *event = gtk_get_current_event ();
1983
1984           priv->timer = gdk_threads_add_timeout (popup_delay,
1985                                                  gtk_menu_item_popup_timeout,
1986                                                  menu_item);
1987
1988           if (event &&
1989               event->type != GDK_BUTTON_PRESS &&
1990               event->type != GDK_ENTER_NOTIFY)
1991             priv->timer_from_keypress = TRUE;
1992           else
1993             priv->timer_from_keypress = FALSE;
1994
1995           if (event)
1996             gdk_event_free (event);
1997
1998           return;
1999         }
2000     }
2001
2002   gtk_menu_item_real_popup_submenu (widget, FALSE);
2003 }
2004
2005 void
2006 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
2007 {
2008   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2009   GtkMenuItemPrivate *priv = menu_item->priv;
2010
2011   if (priv->submenu)
2012     {
2013       g_object_set_data (G_OBJECT (priv->submenu),
2014                          "gtk-menu-exact-popup-time", NULL);
2015
2016       if (priv->timer)
2017         {
2018           g_source_remove (priv->timer);
2019           priv->timer = 0;
2020         }
2021       else
2022         gtk_menu_popdown (GTK_MENU (priv->submenu));
2023
2024       gtk_widget_queue_draw (widget);
2025     }
2026 }
2027
2028 static void
2029 get_offsets (GtkMenu *menu,
2030              gint    *horizontal_offset,
2031              gint    *vertical_offset)
2032 {
2033   gint vertical_padding;
2034   gint horizontal_padding;
2035   GtkStyleContext *context;
2036   GtkStateFlags state;
2037   GtkBorder padding;
2038
2039   gtk_widget_style_get (GTK_WIDGET (menu),
2040                         "horizontal-offset", horizontal_offset,
2041                         "vertical-offset", vertical_offset,
2042                         "horizontal-padding", &horizontal_padding,
2043                         "vertical-padding", &vertical_padding,
2044                         NULL);
2045
2046   context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2047   state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2048   gtk_style_context_get_padding (context, state, &padding);
2049
2050   *vertical_offset -= padding.top;
2051   *vertical_offset -= vertical_padding;
2052   *horizontal_offset += horizontal_padding;
2053 }
2054
2055 static void
2056 gtk_menu_item_position_menu (GtkMenu  *menu,
2057                              gint     *x,
2058                              gint     *y,
2059                              gboolean *push_in,
2060                              gpointer  user_data)
2061 {
2062   GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2063   GtkMenuItemPrivate *priv = menu_item->priv;
2064   GtkAllocation allocation;
2065   GtkWidget *widget;
2066   GtkMenuItem *parent_menu_item;
2067   GtkWidget *parent;
2068   GdkScreen *screen;
2069   gint twidth, theight;
2070   gint tx, ty;
2071   GtkTextDirection direction;
2072   GdkRectangle monitor;
2073   gint monitor_num;
2074   gint horizontal_offset;
2075   gint vertical_offset;
2076   gint available_left, available_right;
2077   GtkStyleContext *context;
2078   GtkStateFlags state;
2079   GtkBorder parent_padding;
2080
2081   g_return_if_fail (menu != NULL);
2082   g_return_if_fail (x != NULL);
2083   g_return_if_fail (y != NULL);
2084
2085   widget = GTK_WIDGET (user_data);
2086
2087   if (push_in)
2088     *push_in = FALSE;
2089
2090   direction = gtk_widget_get_direction (widget);
2091
2092   twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2093   theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2094
2095   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2096   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2097   if (monitor_num < 0)
2098     monitor_num = 0;
2099   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2100
2101   if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2102     {
2103       g_warning ("Menu not on screen");
2104       return;
2105     }
2106
2107   gtk_widget_get_allocation (widget, &allocation);
2108
2109   tx += allocation.x;
2110   ty += allocation.y;
2111
2112   get_offsets (menu, &horizontal_offset, &vertical_offset);
2113
2114   available_left = tx - monitor.x;
2115   available_right = monitor.x + monitor.width - (tx + allocation.width);
2116
2117   parent = gtk_widget_get_parent (widget);
2118   priv->from_menubar = GTK_IS_MENU_BAR (parent);
2119
2120   switch (priv->submenu_placement)
2121     {
2122     case GTK_TOP_BOTTOM:
2123       if (direction == GTK_TEXT_DIR_LTR)
2124         priv->submenu_direction = GTK_DIRECTION_RIGHT;
2125       else
2126         {
2127           priv->submenu_direction = GTK_DIRECTION_LEFT;
2128           tx += allocation.width - twidth;
2129         }
2130       if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2131         ty += allocation.height;
2132       else if ((ty - theight) >= monitor.y)
2133         ty -= theight;
2134       else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2135         ty += allocation.height;
2136       else
2137         ty -= theight;
2138       break;
2139
2140     case GTK_LEFT_RIGHT:
2141       if (GTK_IS_MENU (parent))
2142         parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2143       else
2144         parent_menu_item = NULL;
2145
2146       context = gtk_widget_get_style_context (parent);
2147       state = gtk_widget_get_state_flags (parent);
2148       gtk_style_context_get_padding (context, state, &parent_padding);
2149
2150       if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2151         {
2152           priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2153         }
2154       else
2155         {
2156           if (direction == GTK_TEXT_DIR_LTR)
2157             priv->submenu_direction = GTK_DIRECTION_RIGHT;
2158           else
2159             priv->submenu_direction = GTK_DIRECTION_LEFT;
2160         }
2161
2162       switch (priv->submenu_direction)
2163         {
2164         case GTK_DIRECTION_LEFT:
2165           if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2166               available_left >= available_right)
2167             tx -= twidth + parent_padding.left + horizontal_offset;
2168           else
2169             {
2170               priv->submenu_direction = GTK_DIRECTION_RIGHT;
2171               tx += allocation.width + parent_padding.right + horizontal_offset;
2172             }
2173           break;
2174
2175         case GTK_DIRECTION_RIGHT:
2176           if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2177               available_right >= available_left)
2178             tx += allocation.width + parent_padding.right + horizontal_offset;
2179           else
2180             {
2181               priv->submenu_direction = GTK_DIRECTION_LEFT;
2182               tx -= twidth + parent_padding.left + horizontal_offset;
2183             }
2184           break;
2185         }
2186
2187       ty += vertical_offset;
2188
2189       /* If the height of the menu doesn't fit we move it upward. */
2190       ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2191       break;
2192     }
2193
2194   /* If we have negative, tx, here it is because we can't get
2195    * the menu all the way on screen. Favor the left portion.
2196    */
2197   *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2198   *y = ty;
2199
2200   gtk_menu_set_monitor (menu, monitor_num);
2201
2202   if (!gtk_widget_get_visible (menu->priv->toplevel))
2203     {
2204       gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2205                                 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2206     }
2207 }
2208
2209 /**
2210  * gtk_menu_item_set_right_justified:
2211  * @menu_item: a #GtkMenuItem.
2212  * @right_justified: if %TRUE the menu item will appear at the
2213  *   far right if added to a menu bar
2214  *
2215  * Sets whether the menu item appears justified at the right
2216  * side of a menu bar. This was traditionally done for "Help"
2217  * menu items, but is now considered a bad idea. (If the widget
2218  * layout is reversed for a right-to-left language like Hebrew
2219  * or Arabic, right-justified-menu-items appear at the left.)
2220  *
2221  * Deprecated: 3.2: If you insist on using it, use
2222  *   gtk_widget_set_hexpand() and gtk_widget_set_halign().
2223  **/
2224 void
2225 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2226                                    gboolean     right_justified)
2227 {
2228   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2229
2230   gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2231 }
2232
2233 /**
2234  * gtk_menu_item_get_right_justified:
2235  * @menu_item: a #GtkMenuItem
2236  *
2237  * Gets whether the menu item appears justified at the right
2238  * side of the menu bar.
2239  *
2240  * Return value: %TRUE if the menu item will appear at the
2241  *   far right if added to a menu bar.
2242  *
2243  * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2244  **/
2245 gboolean
2246 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2247 {
2248   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2249
2250   return menu_item->priv->right_justify;
2251 }
2252
2253
2254 static void
2255 gtk_menu_item_show_all (GtkWidget *widget)
2256 {
2257   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2258   GtkMenuItemPrivate *priv = menu_item->priv;
2259
2260   /* show children including submenu */
2261   if (priv->submenu)
2262     gtk_widget_show_all (priv->submenu);
2263   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2264
2265   gtk_widget_show (widget);
2266 }
2267
2268 static gboolean
2269 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2270                                   guint      signal_id)
2271 {
2272   GtkWidget *parent;
2273
2274   parent = gtk_widget_get_parent (widget);
2275
2276   /* Chain to the parent GtkMenu for further checks */
2277   return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2278           parent && gtk_widget_can_activate_accel (parent, signal_id));
2279 }
2280
2281 static void
2282 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2283                                   gpointer   data)
2284 {
2285   const gchar **path_p = data;
2286
2287   if (!*path_p)
2288     {
2289       if (GTK_IS_LABEL (widget))
2290         {
2291           *path_p = gtk_label_get_text (GTK_LABEL (widget));
2292           if (*path_p && (*path_p)[0] == 0)
2293             *path_p = NULL;
2294         }
2295       else if (GTK_IS_CONTAINER (widget))
2296         gtk_container_foreach (GTK_CONTAINER (widget),
2297                                gtk_menu_item_accel_name_foreach,
2298                                data);
2299     }
2300 }
2301
2302 static void
2303 gtk_menu_item_parent_set (GtkWidget *widget,
2304                           GtkWidget *previous_parent)
2305 {
2306   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2307   GtkMenu *menu;
2308   GtkWidget *parent;
2309
2310   parent = gtk_widget_get_parent (widget);
2311   menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2312
2313   if (menu)
2314     _gtk_menu_item_refresh_accel_path (menu_item,
2315                                        menu->priv->accel_path,
2316                                        menu->priv->accel_group,
2317                                        TRUE);
2318
2319   if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2320     GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2321 }
2322
2323 void
2324 _gtk_menu_item_refresh_accel_path (GtkMenuItem   *menu_item,
2325                                    const gchar   *prefix,
2326                                    GtkAccelGroup *accel_group,
2327                                    gboolean       group_changed)
2328 {
2329   GtkMenuItemPrivate *priv = menu_item->priv;
2330   const gchar *path;
2331   GtkWidget *widget;
2332
2333   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2334   g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2335
2336   widget = GTK_WIDGET (menu_item);
2337
2338   if (!accel_group)
2339     {
2340       gtk_widget_set_accel_path (widget, NULL, NULL);
2341       return;
2342     }
2343
2344   path = _gtk_widget_get_accel_path (widget, NULL);
2345   if (!path)  /* no active accel_path yet */
2346     {
2347       path = priv->accel_path;
2348       if (!path && prefix)
2349         {
2350           const gchar *postfix = NULL;
2351           gchar *new_path;
2352
2353           /* try to construct one from label text */
2354           gtk_container_foreach (GTK_CONTAINER (menu_item),
2355                                  gtk_menu_item_accel_name_foreach,
2356                                  &postfix);
2357           if (postfix)
2358             {
2359               new_path = g_strconcat (prefix, "/", postfix, NULL);
2360               path = priv->accel_path = (char*)g_intern_string (new_path);
2361               g_free (new_path);
2362             }
2363         }
2364       if (path)
2365         gtk_widget_set_accel_path (widget, path, accel_group);
2366     }
2367   else if (group_changed)    /* reinstall accelerators */
2368     gtk_widget_set_accel_path (widget, path, accel_group);
2369 }
2370
2371 /**
2372  * gtk_menu_item_set_accel_path
2373  * @menu_item:  a valid #GtkMenuItem
2374  * @accel_path: (allow-none): accelerator path, corresponding to this menu
2375  *     item's functionality, or %NULL to unset the current path.
2376  *
2377  * Set the accelerator path on @menu_item, through which runtime
2378  * changes of the menu item's accelerator caused by the user can be
2379  * identified and saved to persistent storage (see gtk_accel_map_save()
2380  * on this). To set up a default accelerator for this menu item, call
2381  * gtk_accel_map_add_entry() with the same @accel_path. See also
2382  * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2383  * and gtk_menu_set_accel_path() for a more convenient variant of
2384  * this function.
2385  *
2386  * This function is basically a convenience wrapper that handles
2387  * calling gtk_widget_set_accel_path() with the appropriate accelerator
2388  * group for the menu item.
2389  *
2390  * Note that you do need to set an accelerator on the parent menu with
2391  * gtk_menu_set_accel_group() for this to work.
2392  *
2393  * Note that @accel_path string will be stored in a #GQuark.
2394  * Therefore, if you pass a static string, you can save some memory
2395  * by interning it first with g_intern_static_string().
2396  */
2397 void
2398 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2399                               const gchar *accel_path)
2400 {
2401   GtkMenuItemPrivate *priv = menu_item->priv;
2402   GtkWidget *parent;
2403   GtkWidget *widget;
2404
2405   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2406   g_return_if_fail (accel_path == NULL ||
2407                     (accel_path[0] == '<' && strchr (accel_path, '/')));
2408
2409   widget = GTK_WIDGET (menu_item);
2410
2411   /* store new path */
2412   priv->accel_path = (char*)g_intern_string (accel_path);
2413
2414   /* forget accelerators associated with old path */
2415   gtk_widget_set_accel_path (widget, NULL, NULL);
2416
2417   /* install accelerators associated with new path */
2418   parent = gtk_widget_get_parent (widget);
2419   if (GTK_IS_MENU (parent))
2420     {
2421       GtkMenu *menu = GTK_MENU (parent);
2422
2423       if (menu->priv->accel_group)
2424         _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2425                                            NULL,
2426                                            menu->priv->accel_group,
2427                                            FALSE);
2428     }
2429 }
2430
2431 /**
2432  * gtk_menu_item_get_accel_path
2433  * @menu_item:  a valid #GtkMenuItem
2434  *
2435  * Retrieve the accelerator path that was previously set on @menu_item.
2436  *
2437  * See gtk_menu_item_set_accel_path() for details.
2438  *
2439  * Returns: the accelerator path corresponding to this menu
2440  *     item's functionality, or %NULL if not set
2441  *
2442  * Since: 2.14
2443  */
2444 const gchar *
2445 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2446 {
2447   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2448
2449   return menu_item->priv->accel_path;
2450 }
2451
2452 static void
2453 gtk_menu_item_forall (GtkContainer *container,
2454                       gboolean      include_internals,
2455                       GtkCallback   callback,
2456                       gpointer      callback_data)
2457 {
2458   GtkWidget *child;
2459
2460   g_return_if_fail (GTK_IS_MENU_ITEM (container));
2461   g_return_if_fail (callback != NULL);
2462
2463   child = gtk_bin_get_child (GTK_BIN (container));
2464   if (child)
2465     callback (child, callback_data);
2466 }
2467
2468 gboolean
2469 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2470 {
2471   if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2472        G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2473       GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2474       !gtk_widget_is_sensitive (menu_item) ||
2475       !gtk_widget_get_visible (menu_item))
2476     return FALSE;
2477
2478   return TRUE;
2479 }
2480
2481 static void
2482 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2483 {
2484   GtkWidget *accel_label;
2485
2486   if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2487     {
2488       accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2489       gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2490
2491       gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2492       gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2493                                         GTK_WIDGET (menu_item));
2494       gtk_widget_show (accel_label);
2495     }
2496 }
2497
2498 /**
2499  * gtk_menu_item_set_label:
2500  * @menu_item: a #GtkMenuItem
2501  * @label: the text you want to set
2502  *
2503  * Sets @text on the @menu_item label
2504  *
2505  * Since: 2.16
2506  */
2507 void
2508 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2509                          const gchar *label)
2510 {
2511   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2512
2513   GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2514 }
2515
2516 /**
2517  * gtk_menu_item_get_label:
2518  * @menu_item: a #GtkMenuItem
2519  *
2520  * Sets @text on the @menu_item label
2521  *
2522  * Returns: The text in the @menu_item label. This is the internal
2523  *   string used by the label, and must not be modified.
2524  *
2525  * Since: 2.16
2526  */
2527 const gchar *
2528 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2529 {
2530   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2531
2532   return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2533 }
2534
2535 /**
2536  * gtk_menu_item_set_use_underline:
2537  * @menu_item: a #GtkMenuItem
2538  * @setting: %TRUE if underlines in the text indicate mnemonics
2539  *
2540  * If true, an underline in the text indicates the next character
2541  * should be used for the mnemonic accelerator key.
2542  *
2543  * Since: 2.16
2544  */
2545 void
2546 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2547                                  gboolean     setting)
2548 {
2549   GtkWidget *child;
2550
2551   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2552
2553   gtk_menu_item_ensure_label (menu_item);
2554
2555   child = gtk_bin_get_child (GTK_BIN (menu_item));
2556   if (GTK_IS_LABEL (child))
2557     {
2558       gtk_label_set_use_underline (GTK_LABEL (child), setting);
2559
2560       g_object_notify (G_OBJECT (menu_item), "use-underline");
2561     }
2562 }
2563
2564 /**
2565  * gtk_menu_item_get_use_underline:
2566  * @menu_item: a #GtkMenuItem
2567  *
2568  * Checks if an underline in the text indicates the next character
2569  * should be used for the mnemonic accelerator key.
2570  *
2571  * Return value: %TRUE if an embedded underline in the label
2572  *     indicates the mnemonic accelerator key.
2573  *
2574  * Since: 2.16
2575  */
2576 gboolean
2577 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2578 {
2579   GtkWidget *child;
2580
2581   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2582
2583   gtk_menu_item_ensure_label (menu_item);
2584
2585   child = gtk_bin_get_child (GTK_BIN (menu_item));
2586   if (GTK_IS_LABEL (child))
2587     return gtk_label_get_use_underline (GTK_LABEL (child));
2588
2589   return FALSE;
2590 }
2591
2592 /**
2593  * gtk_menu_item_set_reserve_indicator:
2594  * @menu_item: a #GtkMenuItem
2595  * @reserve: the new value
2596  *
2597  * Sets whether the @menu_item should reserve space for
2598  * the submenu indicator, regardless if it actually has
2599  * a submenu or not.
2600  *
2601  * There should be little need for applications to call
2602  * this functions.
2603  *
2604  * Since: 3.0
2605  */
2606 void
2607 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2608                                      gboolean     reserve)
2609 {
2610   GtkMenuItemPrivate *priv;
2611
2612   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2613
2614   priv = menu_item->priv;
2615
2616   if (priv->reserve_indicator != reserve)
2617     {
2618       priv->reserve_indicator = reserve;
2619       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2620     }
2621 }
2622
2623 /**
2624  * gtk_menu_item_get_reserve_indicator:
2625  * @menu_item: a #GtkMenuItem
2626  *
2627  * Returns whether the @menu_item reserves space for
2628  * the submenu indicator, regardless if it has a submenu
2629  * or not.
2630  *
2631  * Returns: %TRUE if @menu_item always reserves space for the
2632  *     submenu indicator
2633  *
2634  * Since: 3.0
2635  */
2636 gboolean
2637 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2638 {
2639   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2640
2641   return menu_item->priv->reserve_indicator;
2642 }