]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenuitem.c
ed3a60dcc4636ea047c55068f26d01ffd1e31a4f
[~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                 gint      *spacing)
679 {
680   GtkStyleContext  *style_context;
681   GtkStateFlags     state;
682   PangoContext     *context;
683   PangoFontMetrics *metrics;
684   gfloat            arrow_scaling;
685   gint              arrow_spacing;
686
687   g_assert (size);
688
689   gtk_widget_style_get (widget,
690                         "arrow-scaling", &arrow_scaling,
691                         "arrow-spacing", &arrow_spacing,
692                         NULL);
693
694   if (spacing != NULL)
695     *spacing = arrow_spacing;
696
697   context = gtk_widget_get_pango_context (child);
698   style_context = gtk_widget_get_style_context (child);
699   state = gtk_widget_get_state_flags (child);
700
701   metrics = pango_context_get_metrics (context,
702                                        gtk_style_context_get_font (style_context, state),
703                                        pango_context_get_language (context));
704
705   *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
706                          pango_font_metrics_get_descent (metrics)));
707
708   pango_font_metrics_unref (metrics);
709
710   *size = *size * arrow_scaling;
711 }
712
713
714 static void
715 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
716                                    gpointer   data)
717 {
718   guint *width = data;
719
720   if (GTK_IS_ACCEL_LABEL (widget))
721     {
722       guint w;
723
724       w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
725       *width = MAX (*width, w);
726     }
727   else if (GTK_IS_CONTAINER (widget))
728     gtk_container_foreach (GTK_CONTAINER (widget),
729                            gtk_menu_item_accel_width_foreach,
730                            data);
731 }
732
733 static gint
734 get_minimum_width (GtkWidget *widget)
735 {
736   GtkStyleContext *style_context;
737   GtkStateFlags state;
738   PangoContext *context;
739   PangoFontMetrics *metrics;
740   gint width;
741   gint width_chars;
742
743   context = gtk_widget_get_pango_context (widget);
744   style_context = gtk_widget_get_style_context (widget);
745   state = gtk_widget_get_state_flags (widget);
746
747   metrics = pango_context_get_metrics (context,
748                                        gtk_style_context_get_font (style_context, state),
749                                        pango_context_get_language (context));
750
751   width = pango_font_metrics_get_approximate_char_width (metrics);
752
753   pango_font_metrics_unref (metrics);
754
755   gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);
756
757   return PANGO_PIXELS (width_chars * width);
758 }
759
760 static void
761 gtk_menu_item_get_preferred_width (GtkWidget *widget,
762                                    gint      *minimum_size,
763                                    gint      *natural_size)
764 {
765   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
766   GtkMenuItemPrivate *priv = menu_item->priv;
767   GtkBin *bin;
768   GtkWidget *child;
769   GtkWidget *parent;
770   guint accel_width;
771   guint horizontal_padding;
772   guint border_width;
773   GtkPackDirection pack_dir;
774   GtkPackDirection child_pack_dir;
775   gint  min_width, nat_width;
776   GtkStyleContext *context;
777   GtkStateFlags state;
778   GtkBorder padding;
779
780   min_width = nat_width = 0;
781
782   gtk_widget_style_get (widget,
783                         "horizontal-padding", &horizontal_padding,
784                         NULL);
785
786   bin = GTK_BIN (widget);
787   parent = gtk_widget_get_parent (widget);
788
789   if (GTK_IS_MENU_BAR (parent))
790     {
791       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
792       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
793     }
794   else
795     {
796       pack_dir = GTK_PACK_DIRECTION_LTR;
797       child_pack_dir = GTK_PACK_DIRECTION_LTR;
798     }
799
800   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
801
802   context = gtk_widget_get_style_context (widget);
803   state = gtk_widget_get_state_flags (widget);
804   gtk_style_context_get_padding (context, state, &padding);
805
806   min_width = (border_width * 2) + padding.left + padding.right;
807
808   if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
809       (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
810     min_width += 2 * horizontal_padding;
811
812   nat_width = min_width;
813           
814   child = gtk_bin_get_child (bin);
815
816   if (child != NULL && gtk_widget_get_visible (child))
817     {
818       GtkMenuItemPrivate *priv = menu_item->priv;
819       gint child_min, child_nat;
820
821       gtk_widget_get_preferred_width (child, &child_min, &child_nat);
822
823       if ((menu_item->priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
824         {
825           gint arrow_spacing, arrow_size;
826
827           get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
828
829           min_width += arrow_size;
830           min_width += arrow_spacing;
831
832           min_width = MAX (min_width, get_minimum_width (widget));
833           nat_width = min_width;
834         }
835
836       min_width += child_min;
837       nat_width += child_nat;
838     }
839
840   accel_width = 0;
841   gtk_container_foreach (GTK_CONTAINER (menu_item),
842                          gtk_menu_item_accel_width_foreach,
843                          &accel_width);
844   priv->accelerator_width = accel_width;
845
846   if (minimum_size)
847     *minimum_size = min_width;
848
849   if (natural_size)
850     *natural_size = nat_width;
851 }
852
853 static void
854 gtk_menu_item_real_get_height (GtkWidget *widget,
855                                gint       for_size,
856                                gint      *minimum_size,
857                                gint      *natural_size)
858 {
859   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
860   GtkMenuItemPrivate *priv = menu_item->priv;
861   GtkBin *bin;
862   GtkStyleContext *context;
863   GtkStateFlags state;
864   GtkBorder padding;
865   GtkWidget *child;
866   GtkWidget *parent;
867   guint accel_width;
868   guint horizontal_padding;
869   guint border_width;
870   GtkPackDirection pack_dir;
871   GtkPackDirection child_pack_dir;
872   gint min_height, nat_height;
873   gint avail_size = 0;
874
875   min_height = nat_height = 0;
876
877   context = gtk_widget_get_style_context (widget);
878   state = gtk_widget_get_state_flags (widget);
879   gtk_style_context_get_padding (context, state, &padding);
880
881   gtk_widget_style_get (widget,
882                         "horizontal-padding", &horizontal_padding,
883                         NULL);
884
885   bin = GTK_BIN (widget);
886   parent = gtk_widget_get_parent (widget);
887
888   if (GTK_IS_MENU_BAR (parent))
889     {
890       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
891       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
892     }
893   else
894     {
895       pack_dir = GTK_PACK_DIRECTION_LTR;
896       child_pack_dir = GTK_PACK_DIRECTION_LTR;
897     }
898
899   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
900   min_height   = (border_width * 2) + padding.top + padding.bottom;
901
902   if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
903       (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
904     min_height += 2 * horizontal_padding;
905
906   if (for_size != -1)
907     {
908       avail_size = for_size;
909       avail_size -= (border_width * 2) + padding.left + padding.right;
910
911       if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
912           (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
913         avail_size -= 2 * horizontal_padding;
914     }
915
916   nat_height = min_height;
917
918   child = gtk_bin_get_child (bin);
919
920   if (child != NULL && gtk_widget_get_visible (child))
921     {
922       gint child_min, child_nat;
923       gint arrow_size = 0, arrow_spacing = 0;
924
925       if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
926         get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
927
928       if (for_size != -1)
929         {
930           avail_size -= (arrow_size + arrow_spacing);
931           gtk_widget_get_preferred_height_for_width (child,
932                                                      avail_size,
933                                                      &child_min,
934                                                      &child_nat);
935         }
936       else
937         {
938           gtk_widget_get_preferred_height (child, &child_min, &child_nat);
939         }
940
941       min_height += child_min;
942       nat_height += child_nat;
943
944       min_height = MAX (min_height, arrow_size);
945       nat_height = MAX (nat_height, arrow_size);
946     }
947   else /* separator item */
948     {
949       gboolean wide_separators;
950       gint     separator_height;
951
952       gtk_widget_style_get (widget,
953                             "wide-separators",  &wide_separators,
954                             "separator-height", &separator_height,
955                             NULL);
956
957       if (wide_separators)
958         {
959           min_height += separator_height;
960           nat_height += separator_height;
961         }
962       else
963         {
964           /* force odd, so that we can have the same space above and
965            * below the line.
966            */
967           if (min_height % 2 == 0)
968             min_height += 1;
969           if (nat_height % 2 == 0)
970             nat_height += 1;
971         }
972     }
973
974   accel_width = 0;
975   gtk_container_foreach (GTK_CONTAINER (menu_item),
976                          gtk_menu_item_accel_width_foreach,
977                          &accel_width);
978   priv->accelerator_width = accel_width;
979
980   if (minimum_size)
981     *minimum_size = min_height;
982
983   if (natural_size)
984     *natural_size = nat_height;
985 }
986
987 static void
988 gtk_menu_item_get_preferred_height (GtkWidget *widget,
989                                     gint      *minimum_size,
990                                     gint      *natural_size)
991 {
992   gtk_menu_item_real_get_height (widget, -1, minimum_size, natural_size);
993 }
994
995 static void
996 gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
997                                               gint       for_size,
998                                               gint      *minimum_size,
999                                               gint      *natural_size)
1000 {
1001   gtk_menu_item_real_get_height (widget, for_size, minimum_size, natural_size);
1002 }
1003
1004 static void
1005 gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
1006 {
1007   parent_buildable_iface = g_type_interface_peek_parent (iface);
1008   iface->add_child = gtk_menu_item_buildable_add_child;
1009   iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1010 }
1011
1012 static void
1013 gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1014                                    GtkBuilder   *builder,
1015                                    GObject      *child,
1016                                    const gchar  *type)
1017 {
1018   if (type && strcmp (type, "submenu") == 0)
1019         gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
1020                                    GTK_WIDGET (child));
1021   else
1022     parent_buildable_iface->add_child (buildable, builder, child, type);
1023 }
1024
1025
1026 static void
1027 gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
1028                                          GtkBuilder   *builder,
1029                                          GObject      *child,
1030                                          const gchar  *tagname,
1031                                          gpointer      user_data)
1032 {
1033   GtkWidget *toplevel;
1034
1035   if (strcmp (tagname, "accelerator") == 0)
1036     {
1037       GtkMenuShell *menu_shell;
1038       GtkWidget *attach;
1039
1040       menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1041       if (menu_shell)
1042         {
1043           while (GTK_IS_MENU (menu_shell) &&
1044                  (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1045             menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1046
1047           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
1048         }
1049       else
1050         {
1051           /* Fall back to something ... */
1052           toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1053
1054           g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
1055                      gtk_buildable_get_name (buildable));
1056         }
1057
1058       /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
1059       _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
1060     }
1061   else
1062     parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
1063 }
1064
1065
1066 static void
1067 gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
1068 {
1069   iface->update = gtk_menu_item_update;
1070   iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1071 }
1072
1073 static void
1074 activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
1075 {
1076   GtkWidget *child;
1077
1078   child = gtk_bin_get_child (GTK_BIN (menu_item));
1079
1080   if (GTK_IS_LABEL (child))
1081     {
1082       const gchar *label;
1083
1084       label = gtk_action_get_label (action);
1085       gtk_menu_item_set_label (menu_item, label);
1086     }
1087 }
1088
1089 gboolean _gtk_menu_is_empty (GtkWidget *menu);
1090
1091 static void
1092 gtk_menu_item_update (GtkActivatable *activatable,
1093                       GtkAction      *action,
1094                       const gchar    *property_name)
1095 {
1096   GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1097   GtkMenuItemPrivate *priv = menu_item->priv;
1098
1099   if (strcmp (property_name, "visible") == 0)
1100     _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1101                                    _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1102   else if (strcmp (property_name, "sensitive") == 0)
1103     gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1104   else if (priv->use_action_appearance)
1105     {
1106       if (strcmp (property_name, "label") == 0)
1107         activatable_update_label (menu_item, action);
1108     }
1109 }
1110
1111 static void
1112 gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1113                                       GtkAction      *action)
1114 {
1115   GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1116   GtkMenuItemPrivate *priv = menu_item->priv;
1117   GtkWidget *label;
1118
1119   if (!priv->use_action_appearance || !action)
1120     {
1121       label = gtk_bin_get_child (GTK_BIN (menu_item));
1122
1123       if (GTK_IS_ACCEL_LABEL (label))
1124         gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
1125     }
1126
1127   if (!action)
1128     return;
1129
1130   _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1131                                  _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1132
1133   gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1134
1135   if (priv->use_action_appearance)
1136     {
1137       label = gtk_bin_get_child (GTK_BIN (menu_item));
1138
1139       /* make sure label is a label, deleting it otherwise */
1140       if (label && !GTK_IS_LABEL (label))
1141         {
1142           gtk_container_remove (GTK_CONTAINER (menu_item), label);
1143           label = NULL;
1144         }
1145       /* Make sure that menu_item has a label and that any
1146        * accelerators are set */
1147       gtk_menu_item_ensure_label (menu_item);
1148       gtk_menu_item_set_use_underline (menu_item, TRUE);
1149       /* Make label point to the menu_item's label */
1150       label = gtk_bin_get_child (GTK_BIN (menu_item));
1151
1152       if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1153         {
1154           gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
1155           gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
1156                                              gtk_action_get_accel_closure (action));
1157         }
1158
1159       activatable_update_label (menu_item, action);
1160     }
1161 }
1162
1163 static void
1164 gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1165                                   GtkAction   *action)
1166 {
1167     GtkMenuItemPrivate *priv = menu_item->priv;
1168
1169     if (priv->action == action)
1170       return;
1171
1172     if (priv->action)
1173       {
1174         gtk_action_disconnect_accelerator (priv->action);
1175       }
1176
1177     if (action)
1178       {
1179         const gchar *accel_path;
1180
1181         accel_path = gtk_action_get_accel_path (action);
1182         if (accel_path)
1183           {
1184             gtk_action_connect_accelerator (action);
1185             gtk_menu_item_set_accel_path (menu_item, accel_path);
1186           }
1187       }
1188
1189     gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);
1190
1191     priv->action = action;
1192 }
1193
1194 static void
1195 gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1196                                          gboolean     use_appearance)
1197 {
1198     GtkMenuItemPrivate *priv = menu_item->priv;
1199
1200     if (priv->use_action_appearance != use_appearance)
1201       {
1202         priv->use_action_appearance = use_appearance;
1203
1204         gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1205       }
1206 }
1207
1208
1209 /**
1210  * gtk_menu_item_set_submenu:
1211  * @menu_item: a #GtkMenuItem
1212  * @submenu: (allow-none): the submenu, or %NULL
1213  *
1214  * Sets or replaces the menu item's submenu, or removes it when a %NULL
1215  * submenu is passed.
1216  */
1217 void
1218 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
1219                            GtkWidget   *submenu)
1220 {
1221   GtkMenuItemPrivate *priv = menu_item->priv;
1222
1223   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1224   g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1225
1226   if (priv->submenu != submenu)
1227     {
1228       if (priv->submenu)
1229         gtk_menu_detach (GTK_MENU (priv->submenu));
1230
1231       if (submenu)
1232         {
1233           priv->submenu = submenu;
1234           gtk_menu_attach_to_widget (GTK_MENU (submenu),
1235                                      GTK_WIDGET (menu_item),
1236                                      gtk_menu_item_detacher);
1237         }
1238
1239       if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1240         gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1241
1242       g_object_notify (G_OBJECT (menu_item), "submenu");
1243     }
1244 }
1245
1246 /**
1247  * gtk_menu_item_get_submenu:
1248  * @menu_item: a #GtkMenuItem
1249  *
1250  * Gets the submenu underneath this menu item, if any.
1251  * See gtk_menu_item_set_submenu().
1252  *
1253  * Return value: (transfer none): submenu for this menu item, or %NULL if none
1254  */
1255 GtkWidget *
1256 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
1257 {
1258   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
1259
1260   return menu_item->priv->submenu;
1261 }
1262
1263 void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1264                                    GtkSubmenuPlacement  placement);
1265
1266 void
1267 _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1268                              GtkSubmenuPlacement  placement)
1269 {
1270   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1271
1272   menu_item->priv->submenu_placement = placement;
1273 }
1274
1275 void
1276 gtk_menu_item_select (GtkMenuItem *menu_item)
1277 {
1278   GtkWidget *parent;
1279
1280   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1281
1282   g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1283
1284   /* Enable themeing of the parent menu item depending on whether
1285    * something is selected in its submenu
1286    */
1287   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1288   if (GTK_IS_MENU (parent))
1289     {
1290       GtkMenu *menu = GTK_MENU (parent);
1291
1292       if (menu->priv->parent_menu_item)
1293         gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1294     }
1295 }
1296
1297 /**
1298  * gtk_menu_item_deselect:
1299  * @menu_item: the menu item
1300  *
1301  * Emits the #GtkMenuItem::deselect signal on the given item. Behaves
1302  * exactly like #gtk_item_deselect.
1303  */
1304 void
1305 gtk_menu_item_deselect (GtkMenuItem *menu_item)
1306 {
1307   GtkWidget *parent;
1308
1309   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1310
1311   g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1312
1313   /* Enable themeing of the parent menu item depending on whether
1314    * something is selected in its submenu
1315    */
1316   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1317   if (GTK_IS_MENU (parent))
1318     {
1319       GtkMenu *menu = GTK_MENU (parent);
1320
1321       if (menu->priv->parent_menu_item)
1322         gtk_widget_queue_draw (GTK_WIDGET (menu->priv->parent_menu_item));
1323     }
1324 }
1325
1326 /**
1327  * gtk_menu_item_activate:
1328  * @menu_item: the menu item
1329  *
1330  * Emits the #GtkMenuItem::activate signal on the given item
1331  */
1332 void
1333 gtk_menu_item_activate (GtkMenuItem *menu_item)
1334 {
1335   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1336
1337   g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
1338 }
1339
1340 /**
1341  * gtk_menu_item_toggle_size_request:
1342  * @menu_item: the menu item
1343  * @requisition: the requisition to use as signal data.
1344  *
1345  * Emits the #GtkMenuItem::toggle-size-request signal on the given item.
1346  */
1347 void
1348 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1349                                    gint        *requisition)
1350 {
1351   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1352
1353   g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1354 }
1355
1356 /**
1357  * gtk_menu_item_toggle_size_allocate:
1358  * @menu_item: the menu item.
1359  * @allocation: the allocation to use as signal data.
1360  *
1361  * Emits the #GtkMenuItem::toggle-size-allocate signal on the given item.
1362  */
1363 void
1364 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1365                                     gint         allocation)
1366 {
1367   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1368
1369   g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1370 }
1371
1372 static void
1373 gtk_menu_item_size_allocate (GtkWidget     *widget,
1374                              GtkAllocation *allocation)
1375 {
1376   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1377   GtkMenuItemPrivate *priv = menu_item->priv;
1378   GtkBin *bin;
1379   GtkAllocation child_allocation;
1380   GtkTextDirection direction;
1381   GtkPackDirection pack_dir;
1382   GtkPackDirection child_pack_dir;
1383   GtkWidget *child;
1384   GtkWidget *parent;
1385
1386   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1387   g_return_if_fail (allocation != NULL);
1388
1389   bin = GTK_BIN (widget);
1390
1391   direction = gtk_widget_get_direction (widget);
1392
1393   parent = gtk_widget_get_parent (widget);
1394   if (GTK_IS_MENU_BAR (parent))
1395     {
1396       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
1397       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1398     }
1399   else
1400     {
1401       pack_dir = GTK_PACK_DIRECTION_LTR;
1402       child_pack_dir = GTK_PACK_DIRECTION_LTR;
1403     }
1404
1405   gtk_widget_set_allocation (widget, allocation);
1406
1407   child = gtk_bin_get_child (bin);
1408   if (child)
1409     {
1410       GtkStyleContext *context;
1411       GtkStateFlags state;
1412       GtkBorder padding;
1413       guint horizontal_padding;
1414       guint border_width;
1415
1416       context = gtk_widget_get_style_context (widget);
1417       state = gtk_widget_get_state_flags (widget);
1418       gtk_style_context_get_padding (context, state, &padding);
1419
1420       gtk_widget_style_get (widget,
1421                             "horizontal-padding", &horizontal_padding,
1422                             NULL);
1423
1424       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1425       child_allocation.x = border_width + padding.left;
1426       child_allocation.y = border_width + padding.top;
1427
1428       child_allocation.width = allocation->width - (border_width * 2) -
1429         padding.left - padding.right;
1430       child_allocation.height = allocation->height - (border_width * 2) -
1431         padding.top - padding.bottom;
1432
1433       if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
1434           (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
1435         {
1436           child_allocation.x += horizontal_padding;
1437           child_allocation.width -= 2 * horizontal_padding;
1438         }
1439       else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
1440                (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
1441         {
1442           child_allocation.y += horizontal_padding;
1443           child_allocation.height -= 2 * horizontal_padding;
1444         }
1445
1446       if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
1447           child_pack_dir == GTK_PACK_DIRECTION_RTL)
1448         {
1449           if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
1450             child_allocation.x += priv->toggle_size;
1451           child_allocation.width -= priv->toggle_size;
1452         }
1453       else
1454         {
1455           if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
1456             child_allocation.y += priv->toggle_size;
1457           child_allocation.height -= priv->toggle_size;
1458         }
1459
1460       child_allocation.x += allocation->x;
1461       child_allocation.y += allocation->y;
1462
1463       if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
1464         {
1465           gint arrow_spacing, arrow_size;
1466
1467           get_arrow_size (widget, child, &arrow_size, &arrow_spacing);
1468
1469           if (direction == GTK_TEXT_DIR_RTL)
1470             child_allocation.x += arrow_size + arrow_spacing;
1471           child_allocation.width -= arrow_size + arrow_spacing;
1472         }
1473       
1474       if (child_allocation.width < 1)
1475         child_allocation.width = 1;
1476
1477       gtk_widget_size_allocate (child, &child_allocation);
1478     }
1479
1480   if (gtk_widget_get_realized (widget))
1481     gdk_window_move_resize (priv->event_window,
1482                             allocation->x, allocation->y,
1483                             allocation->width, allocation->height);
1484
1485   if (priv->submenu)
1486     gtk_menu_reposition (GTK_MENU (priv->submenu));
1487 }
1488
1489 static void
1490 gtk_menu_item_realize (GtkWidget *widget)
1491 {
1492   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1493   GtkMenuItemPrivate *priv = menu_item->priv;
1494   GtkAllocation allocation;
1495   GdkWindow *window;
1496   GdkWindowAttr attributes;
1497   gint attributes_mask;
1498
1499   gtk_widget_set_realized (widget, TRUE);
1500
1501   window = gtk_widget_get_parent_window (widget);
1502   gtk_widget_set_window (widget, window);
1503   g_object_ref (window);
1504
1505   gtk_widget_get_allocation (widget, &allocation);
1506
1507   attributes.x = allocation.x;
1508   attributes.y = allocation.y;
1509   attributes.width = allocation.width;
1510   attributes.height = allocation.height;
1511   attributes.window_type = GDK_WINDOW_CHILD;
1512   attributes.wclass = GDK_INPUT_ONLY;
1513   attributes.event_mask = (gtk_widget_get_events (widget) |
1514                            GDK_BUTTON_PRESS_MASK |
1515                            GDK_BUTTON_RELEASE_MASK |
1516                            GDK_ENTER_NOTIFY_MASK |
1517                            GDK_LEAVE_NOTIFY_MASK |
1518                            GDK_POINTER_MOTION_MASK);
1519
1520   attributes_mask = GDK_WA_X | GDK_WA_Y;
1521
1522   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1523                                        &attributes, attributes_mask);
1524   gdk_window_set_user_data (priv->event_window, widget);
1525 }
1526
1527 static void
1528 gtk_menu_item_unrealize (GtkWidget *widget)
1529 {
1530   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1531   GtkMenuItemPrivate *priv = menu_item->priv;
1532
1533   gdk_window_set_user_data (priv->event_window, NULL);
1534   gdk_window_destroy (priv->event_window);
1535   priv->event_window = NULL;
1536
1537   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1538 }
1539
1540 static void
1541 gtk_menu_item_map (GtkWidget *widget)
1542 {
1543   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1544   GtkMenuItemPrivate *priv = menu_item->priv;
1545
1546   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1547
1548   gdk_window_show (priv->event_window);
1549 }
1550
1551 static void
1552 gtk_menu_item_unmap (GtkWidget *widget)
1553 {
1554   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1555   GtkMenuItemPrivate *priv = menu_item->priv;
1556
1557   gdk_window_hide (priv->event_window);
1558
1559   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1560 }
1561
1562 static gboolean
1563 gtk_menu_item_enter (GtkWidget        *widget,
1564                      GdkEventCrossing *event)
1565 {
1566   g_return_val_if_fail (event != NULL, FALSE);
1567
1568   return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
1569 }
1570
1571 static gboolean
1572 gtk_menu_item_leave (GtkWidget        *widget,
1573                      GdkEventCrossing *event)
1574 {
1575   g_return_val_if_fail (event != NULL, FALSE);
1576
1577   return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
1578 }
1579
1580 static gboolean
1581 gtk_menu_item_draw (GtkWidget *widget,
1582                     cairo_t   *cr)
1583 {
1584   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1585   GtkMenuItemPrivate *priv = menu_item->priv;
1586   GtkStateFlags state;
1587   GtkStyleContext *context;
1588   GtkBorder padding;
1589   GtkWidget *child, *parent;
1590   gint x, y, w, h, width, height;
1591   guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1592
1593   state = gtk_widget_get_state_flags (widget);
1594   context = gtk_widget_get_style_context (widget);
1595   width = gtk_widget_get_allocated_width (widget);
1596   height = gtk_widget_get_allocated_height (widget);
1597
1598   x = border_width;
1599   y = border_width;
1600   w = width - border_width * 2;
1601   h = height - border_width * 2;
1602
1603   child = gtk_bin_get_child (GTK_BIN (menu_item));
1604   parent = gtk_widget_get_parent (widget);
1605
1606   gtk_style_context_save (context);
1607
1608   gtk_style_context_get_padding (context, state, &padding);
1609
1610   if (GTK_IS_MENU_BAR (parent))
1611     gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
1612
1613   if (child && (state & GTK_STATE_FLAG_PRELIGHT))
1614     {
1615       gtk_render_background (context, cr, x, y, w, h);
1616       gtk_render_frame (context, cr, x, y, w, h);
1617     }
1618
1619   if (priv->submenu && !GTK_IS_MENU_BAR (parent))
1620     {
1621       gint arrow_x, arrow_y;
1622       gint arrow_size;
1623       guint horizontal_padding;
1624       GtkTextDirection direction;
1625       gdouble angle;
1626
1627       direction = gtk_widget_get_direction (widget);
1628
1629       gtk_widget_style_get (widget,
1630                             "horizontal-padding", &horizontal_padding,
1631                             NULL);
1632
1633       get_arrow_size (widget, child, &arrow_size, NULL);
1634
1635       if (direction == GTK_TEXT_DIR_LTR)
1636         {
1637           arrow_x = x + w - horizontal_padding - arrow_size;
1638           angle = G_PI / 2;
1639         }
1640       else
1641         {
1642           arrow_x = x + horizontal_padding;
1643           angle = (3 * G_PI) / 2;
1644         }
1645
1646       arrow_y = y + (h - arrow_size) / 2;
1647
1648       gtk_render_arrow (context, cr, angle, arrow_x, arrow_y, arrow_size);
1649     }
1650   else if (!child)
1651     {
1652       gboolean wide_separators;
1653       gint     separator_height;
1654       guint    horizontal_padding;
1655
1656       gtk_widget_style_get (widget,
1657                             "wide-separators",    &wide_separators,
1658                             "separator-height",   &separator_height,
1659                             "horizontal-padding", &horizontal_padding,
1660                             NULL);
1661       if (wide_separators)
1662         gtk_render_frame (context, cr,
1663                           x + horizontal_padding + padding.left,
1664                           y + padding.top,
1665                           w - (2 * horizontal_padding) - padding.left - padding.right,
1666                           separator_height);
1667       else
1668         gtk_render_line (context, cr,
1669                          x + horizontal_padding + padding.left,
1670                          y + padding.top,
1671                          x + w - horizontal_padding - padding.right - 1,
1672                          y + padding.top);
1673     }
1674
1675   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
1676
1677   gtk_style_context_restore (context);
1678
1679   return FALSE;
1680 }
1681
1682 /**
1683  * gtk_menu_item_select:
1684  * @menu_item: the menu item
1685  *
1686  * Emits the #GtkMenuItem::select signal on the given item. Behaves
1687  * exactly like #gtk_item_select.
1688  */
1689 static void
1690 gtk_real_menu_item_select (GtkMenuItem *menu_item)
1691 {
1692   GtkMenuItemPrivate *priv = menu_item->priv;
1693   gboolean touchscreen_mode;
1694
1695   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_item)),
1696                 "gtk-touchscreen-mode", &touchscreen_mode,
1697                 NULL);
1698
1699   if (!touchscreen_mode && priv->submenu &&
1700       (!gtk_widget_get_mapped (priv->submenu) ||
1701        GTK_MENU (priv->submenu)->priv->tearoff_active))
1702     {
1703       _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1704     }
1705
1706   gtk_widget_set_state_flags (GTK_WIDGET (menu_item),
1707                               GTK_STATE_FLAG_PRELIGHT, FALSE);
1708   gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1709 }
1710
1711 static void
1712 gtk_real_menu_item_deselect (GtkMenuItem *menu_item)
1713 {
1714   GtkMenuItemPrivate *priv = menu_item->priv;
1715
1716   if (priv->submenu)
1717     _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1718
1719   gtk_widget_unset_state_flags (GTK_WIDGET (menu_item),
1720                                 GTK_STATE_FLAG_PRELIGHT);
1721   gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1722 }
1723
1724 static gboolean
1725 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1726                                  gboolean   group_cycling)
1727 {
1728   GtkWidget *parent;
1729
1730   parent = gtk_widget_get_parent (widget);
1731
1732   if (GTK_IS_MENU_SHELL (parent))
1733     _gtk_menu_shell_set_keyboard_mode (GTK_MENU_SHELL (parent), TRUE);
1734
1735   if (group_cycling &&
1736       parent &&
1737       GTK_IS_MENU_SHELL (parent) &&
1738       GTK_MENU_SHELL (parent)->priv->active)
1739     {
1740       gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), widget);
1741     }
1742   else
1743     g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1744
1745   return TRUE;
1746 }
1747
1748 static void
1749 gtk_real_menu_item_activate (GtkMenuItem *menu_item)
1750 {
1751   GtkMenuItemPrivate *priv = menu_item->priv;
1752
1753   if (priv->action)
1754     gtk_action_activate (priv->action);
1755 }
1756
1757
1758 static void
1759 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1760 {
1761   GtkMenuItemPrivate *priv = menu_item->priv;
1762   GtkWidget *parent;
1763   GtkWidget *widget;
1764
1765   widget = GTK_WIDGET (menu_item);
1766   parent = gtk_widget_get_parent (widget);
1767
1768   if (parent && GTK_IS_MENU_SHELL (parent))
1769     {
1770       GtkMenuShell *menu_shell = GTK_MENU_SHELL (parent);
1771
1772       if (priv->submenu == NULL)
1773         gtk_menu_shell_activate_item (menu_shell, widget, TRUE);
1774       else
1775         {
1776           gtk_menu_shell_select_item (menu_shell, widget);
1777           _gtk_menu_item_popup_submenu (widget, FALSE);
1778
1779           gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->submenu), TRUE);
1780         }
1781     }
1782 }
1783
1784 static void
1785 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1786                                         gint        *requisition)
1787 {
1788   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1789
1790   *requisition = 0;
1791 }
1792
1793 static void
1794 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1795                                          gint         allocation)
1796 {
1797   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1798
1799   menu_item->priv->toggle_size = allocation;
1800 }
1801
1802 static void
1803 gtk_real_menu_item_set_label (GtkMenuItem *menu_item,
1804                               const gchar *label)
1805 {
1806   GtkWidget *child;
1807
1808   gtk_menu_item_ensure_label (menu_item);
1809
1810   child = gtk_bin_get_child (GTK_BIN (menu_item));
1811   if (GTK_IS_LABEL (child))
1812     {
1813       gtk_label_set_label (GTK_LABEL (child), label ? label : "");
1814
1815       g_object_notify (G_OBJECT (menu_item), "label");
1816     }
1817 }
1818
1819 static const gchar *
1820 gtk_real_menu_item_get_label (GtkMenuItem *menu_item)
1821 {
1822   GtkWidget *child;
1823
1824   gtk_menu_item_ensure_label (menu_item);
1825
1826   child = gtk_bin_get_child (GTK_BIN (menu_item));
1827   if (GTK_IS_LABEL (child))
1828     return gtk_label_get_label (GTK_LABEL (child));
1829
1830   return NULL;
1831 }
1832
1833 static void
1834 free_timeval (GTimeVal *val)
1835 {
1836   g_slice_free (GTimeVal, val);
1837 }
1838
1839 static void
1840 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1841                                   gboolean   remember_exact_time)
1842 {
1843   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1844   GtkMenuItemPrivate *priv = menu_item->priv;
1845   GtkWidget *parent;
1846
1847   parent = gtk_widget_get_parent (widget);
1848
1849   if (gtk_widget_is_sensitive (priv->submenu) && parent)
1850     {
1851       gboolean take_focus;
1852       GtkMenuPositionFunc menu_position_func;
1853
1854       take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
1855       gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
1856
1857       if (remember_exact_time)
1858         {
1859           GTimeVal *popup_time = g_slice_new0 (GTimeVal);
1860
1861           g_get_current_time (popup_time);
1862
1863           g_object_set_data_full (G_OBJECT (priv->submenu),
1864                                   "gtk-menu-exact-popup-time", popup_time,
1865                                   (GDestroyNotify) free_timeval);
1866         }
1867       else
1868         {
1869           g_object_set_data (G_OBJECT (priv->submenu),
1870                              "gtk-menu-exact-popup-time", NULL);
1871         }
1872
1873       /* gtk_menu_item_position_menu positions the submenu from the
1874        * menuitems position. If the menuitem doesn't have a window,
1875        * that doesn't work. In that case we use the default
1876        * positioning function instead which places the submenu at the
1877        * mouse cursor.
1878        */
1879       if (gtk_widget_get_window (widget))
1880         menu_position_func = gtk_menu_item_position_menu;
1881       else
1882         menu_position_func = NULL;
1883
1884       gtk_menu_popup (GTK_MENU (priv->submenu),
1885                       parent,
1886                       widget,
1887                       menu_position_func,
1888                       menu_item,
1889                       GTK_MENU_SHELL (parent)->priv->button,
1890                       0);
1891     }
1892
1893   /* Enable themeing of the parent menu item depending on whether
1894    * its submenu is shown or not.
1895    */
1896   gtk_widget_queue_draw (widget);
1897 }
1898
1899 static gint
1900 gtk_menu_item_popup_timeout (gpointer data)
1901 {
1902   GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
1903   GtkMenuItemPrivate *priv = menu_item->priv;
1904   GtkWidget *parent;
1905
1906   parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
1907
1908   if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
1909       (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
1910     {
1911       gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1912       if (priv->timer_from_keypress && priv->submenu)
1913         GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
1914     }
1915
1916   priv->timer = 0;
1917
1918   return FALSE;
1919 }
1920
1921 static gint
1922 get_popup_delay (GtkWidget *widget)
1923 {
1924   GtkWidget *parent;
1925
1926   parent = gtk_widget_get_parent (widget);
1927   if (GTK_IS_MENU_SHELL (parent))
1928     {
1929       return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (parent));
1930     }
1931   else
1932     {
1933       gint popup_delay;
1934
1935       g_object_get (gtk_widget_get_settings (widget),
1936                     "gtk-menu-popup-delay", &popup_delay,
1937                     NULL);
1938
1939       return popup_delay;
1940     }
1941 }
1942
1943 void
1944 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1945                               gboolean   with_delay)
1946 {
1947   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1948   GtkMenuItemPrivate *priv = menu_item->priv;
1949
1950   if (priv->timer)
1951     {
1952       g_source_remove (priv->timer);
1953       priv->timer = 0;
1954       with_delay = FALSE;
1955     }
1956
1957   if (with_delay)
1958     {
1959       gint popup_delay = get_popup_delay (widget);
1960
1961       if (popup_delay > 0)
1962         {
1963           GdkEvent *event = gtk_get_current_event ();
1964
1965           priv->timer = gdk_threads_add_timeout (popup_delay,
1966                                                  gtk_menu_item_popup_timeout,
1967                                                  menu_item);
1968
1969           if (event &&
1970               event->type != GDK_BUTTON_PRESS &&
1971               event->type != GDK_ENTER_NOTIFY)
1972             priv->timer_from_keypress = TRUE;
1973           else
1974             priv->timer_from_keypress = FALSE;
1975
1976           if (event)
1977             gdk_event_free (event);
1978
1979           return;
1980         }
1981     }
1982
1983   gtk_menu_item_real_popup_submenu (widget, FALSE);
1984 }
1985
1986 void
1987 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1988 {
1989   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1990   GtkMenuItemPrivate *priv = menu_item->priv;
1991
1992   if (priv->submenu)
1993     {
1994       g_object_set_data (G_OBJECT (priv->submenu),
1995                          "gtk-menu-exact-popup-time", NULL);
1996
1997       if (priv->timer)
1998         {
1999           g_source_remove (priv->timer);
2000           priv->timer = 0;
2001         }
2002       else
2003         gtk_menu_popdown (GTK_MENU (priv->submenu));
2004
2005       gtk_widget_queue_draw (widget);
2006     }
2007 }
2008
2009 static void
2010 get_offsets (GtkMenu *menu,
2011              gint    *horizontal_offset,
2012              gint    *vertical_offset)
2013 {
2014   gint vertical_padding;
2015   gint horizontal_padding;
2016   GtkStyleContext *context;
2017   GtkStateFlags state;
2018   GtkBorder padding;
2019
2020   gtk_widget_style_get (GTK_WIDGET (menu),
2021                         "horizontal-offset", horizontal_offset,
2022                         "vertical-offset", vertical_offset,
2023                         "horizontal-padding", &horizontal_padding,
2024                         "vertical-padding", &vertical_padding,
2025                         NULL);
2026
2027   context = gtk_widget_get_style_context (GTK_WIDGET (menu));
2028   state = gtk_widget_get_state_flags (GTK_WIDGET (menu));
2029   gtk_style_context_get_padding (context, state, &padding);
2030
2031   *vertical_offset -= padding.top;
2032   *vertical_offset -= vertical_padding;
2033   *horizontal_offset += horizontal_padding;
2034 }
2035
2036 static void
2037 gtk_menu_item_position_menu (GtkMenu  *menu,
2038                              gint     *x,
2039                              gint     *y,
2040                              gboolean *push_in,
2041                              gpointer  user_data)
2042 {
2043   GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
2044   GtkMenuItemPrivate *priv = menu_item->priv;
2045   GtkAllocation allocation;
2046   GtkWidget *widget;
2047   GtkMenuItem *parent_menu_item;
2048   GtkWidget *parent;
2049   GdkScreen *screen;
2050   gint twidth, theight;
2051   gint tx, ty;
2052   GtkTextDirection direction;
2053   GdkRectangle monitor;
2054   gint monitor_num;
2055   gint horizontal_offset;
2056   gint vertical_offset;
2057   gint available_left, available_right;
2058   GtkStyleContext *context;
2059   GtkStateFlags state;
2060   GtkBorder parent_padding;
2061
2062   g_return_if_fail (menu != NULL);
2063   g_return_if_fail (x != NULL);
2064   g_return_if_fail (y != NULL);
2065
2066   widget = GTK_WIDGET (user_data);
2067
2068   if (push_in)
2069     *push_in = FALSE;
2070
2071   direction = gtk_widget_get_direction (widget);
2072
2073   twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
2074   theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
2075
2076   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2077   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->event_window);
2078   if (monitor_num < 0)
2079     monitor_num = 0;
2080   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2081
2082   if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
2083     {
2084       g_warning ("Menu not on screen");
2085       return;
2086     }
2087
2088   gtk_widget_get_allocation (widget, &allocation);
2089
2090   tx += allocation.x;
2091   ty += allocation.y;
2092
2093   get_offsets (menu, &horizontal_offset, &vertical_offset);
2094
2095   available_left = tx - monitor.x;
2096   available_right = monitor.x + monitor.width - (tx + allocation.width);
2097
2098   parent = gtk_widget_get_parent (widget);
2099   priv->from_menubar = GTK_IS_MENU_BAR (parent);
2100
2101   switch (priv->submenu_placement)
2102     {
2103     case GTK_TOP_BOTTOM:
2104       if (direction == GTK_TEXT_DIR_LTR)
2105         priv->submenu_direction = GTK_DIRECTION_RIGHT;
2106       else
2107         {
2108           priv->submenu_direction = GTK_DIRECTION_LEFT;
2109           tx += allocation.width - twidth;
2110         }
2111       if ((ty + allocation.height + theight) <= monitor.y + monitor.height)
2112         ty += allocation.height;
2113       else if ((ty - theight) >= monitor.y)
2114         ty -= theight;
2115       else if (monitor.y + monitor.height - (ty + allocation.height) > ty)
2116         ty += allocation.height;
2117       else
2118         ty -= theight;
2119       break;
2120
2121     case GTK_LEFT_RIGHT:
2122       if (GTK_IS_MENU (parent))
2123         parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
2124       else
2125         parent_menu_item = NULL;
2126
2127       context = gtk_widget_get_style_context (parent);
2128       state = gtk_widget_get_state_flags (parent);
2129       gtk_style_context_get_padding (context, state, &parent_padding);
2130
2131       if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
2132         {
2133           priv->submenu_direction = parent_menu_item->priv->submenu_direction;
2134         }
2135       else
2136         {
2137           if (direction == GTK_TEXT_DIR_LTR)
2138             priv->submenu_direction = GTK_DIRECTION_RIGHT;
2139           else
2140             priv->submenu_direction = GTK_DIRECTION_LEFT;
2141         }
2142
2143       switch (priv->submenu_direction)
2144         {
2145         case GTK_DIRECTION_LEFT:
2146           if (tx - twidth - parent_padding.left - horizontal_offset >= monitor.x ||
2147               available_left >= available_right)
2148             tx -= twidth + parent_padding.left + horizontal_offset;
2149           else
2150             {
2151               priv->submenu_direction = GTK_DIRECTION_RIGHT;
2152               tx += allocation.width + parent_padding.right + horizontal_offset;
2153             }
2154           break;
2155
2156         case GTK_DIRECTION_RIGHT:
2157           if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= monitor.x + monitor.width ||
2158               available_right >= available_left)
2159             tx += allocation.width + parent_padding.right + horizontal_offset;
2160           else
2161             {
2162               priv->submenu_direction = GTK_DIRECTION_LEFT;
2163               tx -= twidth + parent_padding.left + horizontal_offset;
2164             }
2165           break;
2166         }
2167
2168       ty += vertical_offset;
2169
2170       /* If the height of the menu doesn't fit we move it upward. */
2171       ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
2172       break;
2173     }
2174
2175   /* If we have negative, tx, here it is because we can't get
2176    * the menu all the way on screen. Favor the left portion.
2177    */
2178   *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
2179   *y = ty;
2180
2181   gtk_menu_set_monitor (menu, monitor_num);
2182
2183   if (!gtk_widget_get_visible (menu->priv->toplevel))
2184     {
2185       gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
2186                                 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
2187     }
2188 }
2189
2190 /**
2191  * gtk_menu_item_set_right_justified:
2192  * @menu_item: a #GtkMenuItem.
2193  * @right_justified: if %TRUE the menu item will appear at the
2194  *   far right if added to a menu bar
2195  *
2196  * Sets whether the menu item appears justified at the right
2197  * side of a menu bar. This was traditionally done for "Help"
2198  * menu items, but is now considered a bad idea. (If the widget
2199  * layout is reversed for a right-to-left language like Hebrew
2200  * or Arabic, right-justified-menu-items appear at the left.)
2201  *
2202  * Deprecated: 3.2: If you insist on using it, use
2203  *   gtk_widget_set_hexpand() and gtk_widget_set_halign().
2204  **/
2205 void
2206 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
2207                                    gboolean     right_justified)
2208 {
2209   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2210
2211   gtk_menu_item_do_set_right_justified (menu_item, right_justified);
2212 }
2213
2214 /**
2215  * gtk_menu_item_get_right_justified:
2216  * @menu_item: a #GtkMenuItem
2217  *
2218  * Gets whether the menu item appears justified at the right
2219  * side of the menu bar.
2220  *
2221  * Return value: %TRUE if the menu item will appear at the
2222  *   far right if added to a menu bar.
2223  *
2224  * Deprecated: 3.2: See gtk_menu_item_set_right_justified()
2225  **/
2226 gboolean
2227 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
2228 {
2229   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2230
2231   return menu_item->priv->right_justify;
2232 }
2233
2234
2235 static void
2236 gtk_menu_item_show_all (GtkWidget *widget)
2237 {
2238   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2239   GtkMenuItemPrivate *priv = menu_item->priv;
2240
2241   /* show children including submenu */
2242   if (priv->submenu)
2243     gtk_widget_show_all (priv->submenu);
2244   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2245
2246   gtk_widget_show (widget);
2247 }
2248
2249 static gboolean
2250 gtk_menu_item_can_activate_accel (GtkWidget *widget,
2251                                   guint      signal_id)
2252 {
2253   GtkWidget *parent;
2254
2255   parent = gtk_widget_get_parent (widget);
2256
2257   /* Chain to the parent GtkMenu for further checks */
2258   return (gtk_widget_is_sensitive (widget) && gtk_widget_get_visible (widget) &&
2259           parent && gtk_widget_can_activate_accel (parent, signal_id));
2260 }
2261
2262 static void
2263 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
2264                                   gpointer   data)
2265 {
2266   const gchar **path_p = data;
2267
2268   if (!*path_p)
2269     {
2270       if (GTK_IS_LABEL (widget))
2271         {
2272           *path_p = gtk_label_get_text (GTK_LABEL (widget));
2273           if (*path_p && (*path_p)[0] == 0)
2274             *path_p = NULL;
2275         }
2276       else if (GTK_IS_CONTAINER (widget))
2277         gtk_container_foreach (GTK_CONTAINER (widget),
2278                                gtk_menu_item_accel_name_foreach,
2279                                data);
2280     }
2281 }
2282
2283 static void
2284 gtk_menu_item_parent_set (GtkWidget *widget,
2285                           GtkWidget *previous_parent)
2286 {
2287   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
2288   GtkMenu *menu;
2289   GtkWidget *parent;
2290
2291   parent = gtk_widget_get_parent (widget);
2292   menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
2293
2294   if (menu)
2295     _gtk_menu_item_refresh_accel_path (menu_item,
2296                                        menu->priv->accel_path,
2297                                        menu->priv->accel_group,
2298                                        TRUE);
2299
2300   if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
2301     GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
2302 }
2303
2304 void
2305 _gtk_menu_item_refresh_accel_path (GtkMenuItem   *menu_item,
2306                                    const gchar   *prefix,
2307                                    GtkAccelGroup *accel_group,
2308                                    gboolean       group_changed)
2309 {
2310   GtkMenuItemPrivate *priv = menu_item->priv;
2311   const gchar *path;
2312   GtkWidget *widget;
2313
2314   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2315   g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
2316
2317   widget = GTK_WIDGET (menu_item);
2318
2319   if (!accel_group)
2320     {
2321       gtk_widget_set_accel_path (widget, NULL, NULL);
2322       return;
2323     }
2324
2325   path = _gtk_widget_get_accel_path (widget, NULL);
2326   if (!path)  /* no active accel_path yet */
2327     {
2328       path = priv->accel_path;
2329       if (!path && prefix)
2330         {
2331           const gchar *postfix = NULL;
2332           gchar *new_path;
2333
2334           /* try to construct one from label text */
2335           gtk_container_foreach (GTK_CONTAINER (menu_item),
2336                                  gtk_menu_item_accel_name_foreach,
2337                                  &postfix);
2338           if (postfix)
2339             {
2340               new_path = g_strconcat (prefix, "/", postfix, NULL);
2341               path = priv->accel_path = (char*)g_intern_string (new_path);
2342               g_free (new_path);
2343             }
2344         }
2345       if (path)
2346         gtk_widget_set_accel_path (widget, path, accel_group);
2347     }
2348   else if (group_changed)    /* reinstall accelerators */
2349     gtk_widget_set_accel_path (widget, path, accel_group);
2350 }
2351
2352 /**
2353  * gtk_menu_item_set_accel_path
2354  * @menu_item:  a valid #GtkMenuItem
2355  * @accel_path: (allow-none): accelerator path, corresponding to this menu
2356  *     item's functionality, or %NULL to unset the current path.
2357  *
2358  * Set the accelerator path on @menu_item, through which runtime
2359  * changes of the menu item's accelerator caused by the user can be
2360  * identified and saved to persistent storage (see gtk_accel_map_save()
2361  * on this). To set up a default accelerator for this menu item, call
2362  * gtk_accel_map_add_entry() with the same @accel_path. See also
2363  * gtk_accel_map_add_entry() on the specifics of accelerator paths,
2364  * and gtk_menu_set_accel_path() for a more convenient variant of
2365  * this function.
2366  *
2367  * This function is basically a convenience wrapper that handles
2368  * calling gtk_widget_set_accel_path() with the appropriate accelerator
2369  * group for the menu item.
2370  *
2371  * Note that you do need to set an accelerator on the parent menu with
2372  * gtk_menu_set_accel_group() for this to work.
2373  *
2374  * Note that @accel_path string will be stored in a #GQuark.
2375  * Therefore, if you pass a static string, you can save some memory
2376  * by interning it first with g_intern_static_string().
2377  */
2378 void
2379 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
2380                               const gchar *accel_path)
2381 {
2382   GtkMenuItemPrivate *priv = menu_item->priv;
2383   GtkWidget *parent;
2384   GtkWidget *widget;
2385
2386   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2387   g_return_if_fail (accel_path == NULL ||
2388                     (accel_path[0] == '<' && strchr (accel_path, '/')));
2389
2390   widget = GTK_WIDGET (menu_item);
2391
2392   /* store new path */
2393   priv->accel_path = (char*)g_intern_string (accel_path);
2394
2395   /* forget accelerators associated with old path */
2396   gtk_widget_set_accel_path (widget, NULL, NULL);
2397
2398   /* install accelerators associated with new path */
2399   parent = gtk_widget_get_parent (widget);
2400   if (GTK_IS_MENU (parent))
2401     {
2402       GtkMenu *menu = GTK_MENU (parent);
2403
2404       if (menu->priv->accel_group)
2405         _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2406                                            NULL,
2407                                            menu->priv->accel_group,
2408                                            FALSE);
2409     }
2410 }
2411
2412 /**
2413  * gtk_menu_item_get_accel_path
2414  * @menu_item:  a valid #GtkMenuItem
2415  *
2416  * Retrieve the accelerator path that was previously set on @menu_item.
2417  *
2418  * See gtk_menu_item_set_accel_path() for details.
2419  *
2420  * Returns: the accelerator path corresponding to this menu
2421  *     item's functionality, or %NULL if not set
2422  *
2423  * Since: 2.14
2424  */
2425 const gchar *
2426 gtk_menu_item_get_accel_path (GtkMenuItem *menu_item)
2427 {
2428   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2429
2430   return menu_item->priv->accel_path;
2431 }
2432
2433 static void
2434 gtk_menu_item_forall (GtkContainer *container,
2435                       gboolean      include_internals,
2436                       GtkCallback   callback,
2437                       gpointer      callback_data)
2438 {
2439   GtkWidget *child;
2440
2441   g_return_if_fail (GTK_IS_MENU_ITEM (container));
2442   g_return_if_fail (callback != NULL);
2443
2444   child = gtk_bin_get_child (GTK_BIN (container));
2445   if (child)
2446     callback (child, callback_data);
2447 }
2448
2449 gboolean
2450 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
2451 {
2452   if ((!gtk_bin_get_child (GTK_BIN (menu_item)) &&
2453        G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
2454       GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
2455       !gtk_widget_is_sensitive (menu_item) ||
2456       !gtk_widget_get_visible (menu_item))
2457     return FALSE;
2458
2459   return TRUE;
2460 }
2461
2462 static void
2463 gtk_menu_item_ensure_label (GtkMenuItem *menu_item)
2464 {
2465   GtkWidget *accel_label;
2466
2467   if (!gtk_bin_get_child (GTK_BIN (menu_item)))
2468     {
2469       accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
2470       gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
2471
2472       gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
2473       gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
2474                                         GTK_WIDGET (menu_item));
2475       gtk_widget_show (accel_label);
2476     }
2477 }
2478
2479 /**
2480  * gtk_menu_item_set_label:
2481  * @menu_item: a #GtkMenuItem
2482  * @label: the text you want to set
2483  *
2484  * Sets @text on the @menu_item label
2485  *
2486  * Since: 2.16
2487  */
2488 void
2489 gtk_menu_item_set_label (GtkMenuItem *menu_item,
2490                          const gchar *label)
2491 {
2492   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2493
2494   GTK_MENU_ITEM_GET_CLASS (menu_item)->set_label (menu_item, label);
2495 }
2496
2497 /**
2498  * gtk_menu_item_get_label:
2499  * @menu_item: a #GtkMenuItem
2500  *
2501  * Sets @text on the @menu_item label
2502  *
2503  * Returns: The text in the @menu_item label. This is the internal
2504  *   string used by the label, and must not be modified.
2505  *
2506  * Since: 2.16
2507  */
2508 const gchar *
2509 gtk_menu_item_get_label (GtkMenuItem *menu_item)
2510 {
2511   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
2512
2513   return GTK_MENU_ITEM_GET_CLASS (menu_item)->get_label (menu_item);
2514 }
2515
2516 /**
2517  * gtk_menu_item_set_use_underline:
2518  * @menu_item: a #GtkMenuItem
2519  * @setting: %TRUE if underlines in the text indicate mnemonics
2520  *
2521  * If true, an underline in the text indicates the next character
2522  * should be used for the mnemonic accelerator key.
2523  *
2524  * Since: 2.16
2525  */
2526 void
2527 gtk_menu_item_set_use_underline (GtkMenuItem *menu_item,
2528                                  gboolean     setting)
2529 {
2530   GtkWidget *child;
2531
2532   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2533
2534   gtk_menu_item_ensure_label (menu_item);
2535
2536   child = gtk_bin_get_child (GTK_BIN (menu_item));
2537   if (GTK_IS_LABEL (child))
2538     {
2539       gtk_label_set_use_underline (GTK_LABEL (child), setting);
2540
2541       g_object_notify (G_OBJECT (menu_item), "use-underline");
2542     }
2543 }
2544
2545 /**
2546  * gtk_menu_item_get_use_underline:
2547  * @menu_item: a #GtkMenuItem
2548  *
2549  * Checks if an underline in the text indicates the next character
2550  * should be used for the mnemonic accelerator key.
2551  *
2552  * Return value: %TRUE if an embedded underline in the label
2553  *     indicates the mnemonic accelerator key.
2554  *
2555  * Since: 2.16
2556  */
2557 gboolean
2558 gtk_menu_item_get_use_underline (GtkMenuItem *menu_item)
2559 {
2560   GtkWidget *child;
2561
2562   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2563
2564   gtk_menu_item_ensure_label (menu_item);
2565
2566   child = gtk_bin_get_child (GTK_BIN (menu_item));
2567   if (GTK_IS_LABEL (child))
2568     return gtk_label_get_use_underline (GTK_LABEL (child));
2569
2570   return FALSE;
2571 }
2572
2573 /**
2574  * gtk_menu_item_set_reserve_indicator:
2575  * @menu_item: a #GtkMenuItem
2576  * @reserve: the new value
2577  *
2578  * Sets whether the @menu_item should reserve space for
2579  * the submenu indicator, regardless if it actually has
2580  * a submenu or not.
2581  *
2582  * There should be little need for applications to call
2583  * this functions.
2584  *
2585  * Since: 3.0
2586  */
2587 void
2588 gtk_menu_item_set_reserve_indicator (GtkMenuItem *menu_item,
2589                                      gboolean     reserve)
2590 {
2591   GtkMenuItemPrivate *priv;
2592
2593   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
2594
2595   priv = menu_item->priv;
2596
2597   if (priv->reserve_indicator != reserve)
2598     {
2599       priv->reserve_indicator = reserve;
2600       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
2601     }
2602 }
2603
2604 /**
2605  * gtk_menu_item_get_reserve_indicator:
2606  * @menu_item: a #GtkMenuItem
2607  *
2608  * Returns whether the @menu_item reserves space for
2609  * the submenu indicator, regardless if it has a submenu
2610  * or not.
2611  *
2612  * Returns: %TRUE if @menu_item always reserves space for the
2613  *     submenu indicator
2614  *
2615  * Since: 3.0
2616  */
2617 gboolean
2618 gtk_menu_item_get_reserve_indicator (GtkMenuItem *menu_item)
2619 {
2620   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
2621
2622   return menu_item->priv->reserve_indicator;
2623 }