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