]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenuitem.c
Make GtkMenuItem's arrow size themeable. (#469239, Michael Natterer)
[~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 #define GTK_MENU_INTERNALS
28
29 #include <config.h>
30 #include <string.h>
31
32 #include "gtkaccellabel.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenu.h"
36 #include "gtkmenubar.h"
37 #include "gtkmenuitem.h"
38 #include "gtkseparatormenuitem.h"
39 #include "gtkprivate.h"
40 #include "gtkintl.h"
41 #include "gtkalias.h"
42
43 #define MENU_ITEM_CLASS(w)  GTK_MENU_ITEM_CLASS (GTK_OBJECT (w)->klass)
44
45 enum {
46   ACTIVATE,
47   ACTIVATE_ITEM,
48   TOGGLE_SIZE_REQUEST,
49   TOGGLE_SIZE_ALLOCATE,
50   LAST_SIGNAL
51 };
52
53 enum {
54   PROP_0,
55   PROP_SUBMENU
56 };
57
58
59 static void gtk_menu_item_set_property   (GObject          *object,
60                                           guint             prop_id,
61                                           const GValue     *value,
62                                           GParamSpec       *pspec);
63 static void gtk_menu_item_get_property   (GObject          *object,
64                                           guint             prop_id,
65                                           GValue           *value,
66                                           GParamSpec       *pspec);
67 static void gtk_menu_item_finalize       (GObject          *object);
68 static void gtk_menu_item_destroy        (GtkObject        *object);
69 static void gtk_menu_item_size_request   (GtkWidget        *widget,
70                                           GtkRequisition   *requisition);
71 static void gtk_menu_item_size_allocate  (GtkWidget        *widget,
72                                           GtkAllocation    *allocation);
73 static void gtk_menu_item_realize        (GtkWidget        *widget);
74 static void gtk_menu_item_unrealize      (GtkWidget        *widget);
75 static void gtk_menu_item_map            (GtkWidget        *widget);
76 static void gtk_menu_item_unmap          (GtkWidget        *widget);
77 static void gtk_menu_item_paint          (GtkWidget        *widget,
78                                           GdkRectangle     *area);
79 static gint gtk_menu_item_expose         (GtkWidget        *widget,
80                                           GdkEventExpose   *event);
81 static void gtk_menu_item_parent_set     (GtkWidget        *widget,
82                                           GtkWidget        *previous_parent);
83
84
85 static void gtk_real_menu_item_select               (GtkItem     *item);
86 static void gtk_real_menu_item_deselect             (GtkItem     *item);
87 static void gtk_real_menu_item_activate_item        (GtkMenuItem *item);
88 static void gtk_real_menu_item_toggle_size_request  (GtkMenuItem *menu_item,
89                                                      gint        *requisition);
90 static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
91                                                      gint         allocation);
92 static gboolean gtk_menu_item_mnemonic_activate     (GtkWidget   *widget,
93                                                      gboolean     group_cycling);
94
95 static gint gtk_menu_item_popup_timeout  (gpointer          data);
96 static void gtk_menu_item_position_menu  (GtkMenu          *menu,
97                                           gint             *x,
98                                           gint             *y,
99                                           gboolean         *push_in,
100                                           gpointer          user_data);
101 static void gtk_menu_item_show_all       (GtkWidget        *widget);
102 static void gtk_menu_item_hide_all       (GtkWidget        *widget);
103 static void gtk_menu_item_forall         (GtkContainer    *container,
104                                           gboolean         include_internals,
105                                           GtkCallback      callback,
106                                           gpointer         callback_data);
107 static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
108                                                   guint      signal_id);
109
110
111 static guint menu_item_signals[LAST_SIGNAL] = { 0 };
112
113 G_DEFINE_TYPE (GtkMenuItem, gtk_menu_item, GTK_TYPE_ITEM)
114
115 static void
116 gtk_menu_item_class_init (GtkMenuItemClass *klass)
117 {
118   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
120   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
121   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
122   GtkItemClass *item_class = GTK_ITEM_CLASS (klass);
123
124   gobject_class->set_property = gtk_menu_item_set_property;
125   gobject_class->get_property = gtk_menu_item_get_property;
126   gobject_class->finalize = gtk_menu_item_finalize;
127
128   object_class->destroy = gtk_menu_item_destroy;
129
130   widget_class->size_request = gtk_menu_item_size_request;
131   widget_class->size_allocate = gtk_menu_item_size_allocate;
132   widget_class->expose_event = gtk_menu_item_expose;
133   widget_class->realize = gtk_menu_item_realize;
134   widget_class->unrealize = gtk_menu_item_unrealize;
135   widget_class->map = gtk_menu_item_map;
136   widget_class->unmap = gtk_menu_item_unmap;
137   widget_class->show_all = gtk_menu_item_show_all;
138   widget_class->hide_all = gtk_menu_item_hide_all;
139   widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
140   widget_class->parent_set = gtk_menu_item_parent_set;
141   widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
142   
143   container_class->forall = gtk_menu_item_forall;
144
145   item_class->select = gtk_real_menu_item_select;
146   item_class->deselect = gtk_real_menu_item_deselect;
147
148   klass->activate = NULL;
149   klass->activate_item = gtk_real_menu_item_activate_item;
150   klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
151   klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
152
153   klass->hide_on_activate = TRUE;
154
155   menu_item_signals[ACTIVATE] =
156     g_signal_new (I_("activate"),
157                   G_OBJECT_CLASS_TYPE (gobject_class),
158                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
159                   G_STRUCT_OFFSET (GtkMenuItemClass, activate),
160                   NULL, NULL,
161                   _gtk_marshal_VOID__VOID,
162                   G_TYPE_NONE, 0);
163   widget_class->activate_signal = menu_item_signals[ACTIVATE];
164
165   menu_item_signals[ACTIVATE_ITEM] =
166     g_signal_new (I_("activate_item"),
167                   G_OBJECT_CLASS_TYPE (gobject_class),
168                   G_SIGNAL_RUN_FIRST,
169                   G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
170                   NULL, NULL,
171                   _gtk_marshal_VOID__VOID,
172                   G_TYPE_NONE, 0);
173
174   menu_item_signals[TOGGLE_SIZE_REQUEST] =
175     g_signal_new (I_("toggle_size_request"),
176                   G_OBJECT_CLASS_TYPE (gobject_class),
177                   G_SIGNAL_RUN_FIRST,
178                   G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
179                   NULL, NULL,
180                   _gtk_marshal_VOID__POINTER,
181                   G_TYPE_NONE, 1,
182                   G_TYPE_POINTER);
183
184   menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
185     g_signal_new (I_("toggle_size_allocate"),
186                   G_OBJECT_CLASS_TYPE (gobject_class),
187                   G_SIGNAL_RUN_FIRST,
188                   G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
189                   NULL, NULL,
190                   _gtk_marshal_NONE__INT,
191                   G_TYPE_NONE, 1,
192                   G_TYPE_INT);
193
194   /**
195    * GtkMenuItem:submenu:
196    *
197    * The submenu attached to the menu item, or NULL if it has none.
198    *
199    * Since: 2.12
200    **/
201   g_object_class_install_property (gobject_class,
202                                    PROP_SUBMENU,
203                                    g_param_spec_object ("submenu",
204                                                         P_("Submenu"),
205                                                         P_("The submenu attached to the menu item, or NULL if it has none"),
206                                                         GTK_TYPE_MENU,
207                                                         GTK_PARAM_READWRITE));
208
209   gtk_widget_class_install_style_property_parser (widget_class,
210                                                   g_param_spec_enum ("selected-shadow-type",
211                                                                      "Selected Shadow Type",
212                                                                      "Shadow type when item is selected",
213                                                                      GTK_TYPE_SHADOW_TYPE,
214                                                                      GTK_SHADOW_NONE,
215                                                                      GTK_PARAM_READABLE),
216                                                   gtk_rc_property_parse_enum);
217
218   gtk_widget_class_install_style_property (widget_class,
219                                            g_param_spec_int ("horizontal-padding",
220                                                              "Horizontal Padding",
221                                                              "Padding to left and right of the menu item",
222                                                              0,
223                                                              G_MAXINT,
224                                                              3,
225                                                              GTK_PARAM_READABLE));
226
227   gtk_widget_class_install_style_property (widget_class,
228                                            g_param_spec_int ("toggle-spacing",
229                                                              "Icon Spacing",
230                                                              "Space between icon and label",
231                                                              0,
232                                                              G_MAXINT,
233                                                              5,
234                                                              GTK_PARAM_READABLE));
235
236   gtk_widget_class_install_style_property (widget_class,
237                                            g_param_spec_int ("arrow-spacing",
238                                                              "Arrow Spacing",
239                                                              "Space between label and arrow",
240                                                              0,
241                                                              G_MAXINT,
242                                                              10,
243                                                              GTK_PARAM_READABLE));
244
245   gtk_widget_class_install_style_property (widget_class,
246                                            g_param_spec_float ("arrow-scaling",
247                                                                P_("Arrow Scaling"),
248                                                                P_("Amount of space used up by arrow, relative to the menu item's font size"),
249                                                                0.0, 2.0, 0.8,
250                                                                GTK_PARAM_READABLE));
251 }
252
253 static void
254 gtk_menu_item_init (GtkMenuItem *menu_item)
255 {
256   GTK_WIDGET_SET_FLAGS (menu_item, GTK_NO_WINDOW);
257   
258   menu_item->submenu = NULL;
259   menu_item->toggle_size = 0;
260   menu_item->accelerator_width = 0;
261   menu_item->show_submenu_indicator = FALSE;
262   if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
263     menu_item->submenu_direction = GTK_DIRECTION_LEFT;
264   else
265     menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
266   menu_item->submenu_placement = GTK_TOP_BOTTOM;
267   menu_item->right_justify = FALSE;
268
269   menu_item->timer = 0;
270 }
271
272 GtkWidget*
273 gtk_menu_item_new (void)
274 {
275   return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
276 }
277
278 GtkWidget*
279 gtk_menu_item_new_with_label (const gchar *label)
280 {
281   GtkWidget *menu_item;
282   GtkWidget *accel_label;
283
284   menu_item = gtk_menu_item_new ();
285   accel_label = gtk_accel_label_new (label);
286   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
287
288   gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
289   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), menu_item);
290   gtk_widget_show (accel_label);
291
292   return menu_item;
293 }
294
295
296 /**
297  * gtk_menu_item_new_with_mnemonic:
298  * @label: The text of the button, with an underscore in front of the
299  *         mnemonic character
300  * @returns: a new #GtkMenuItem
301  *
302  * Creates a new #GtkMenuItem containing a label. The label
303  * will be created using gtk_label_new_with_mnemonic(), so underscores
304  * in @label indicate the mnemonic for the menu item.
305  **/
306 GtkWidget*
307 gtk_menu_item_new_with_mnemonic (const gchar *label)
308 {
309   GtkWidget *menu_item;
310   GtkWidget *accel_label;
311
312   menu_item = gtk_menu_item_new ();
313   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
314   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
315   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
316
317   gtk_container_add (GTK_CONTAINER (menu_item), accel_label);
318   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), menu_item);
319   gtk_widget_show (accel_label);
320
321   return menu_item;
322 }
323
324 static void 
325 gtk_menu_item_set_property (GObject      *object,
326                             guint         prop_id,
327                             const GValue *value,
328                             GParamSpec   *pspec)
329 {
330   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
331   
332   switch (prop_id)
333     {
334     case PROP_SUBMENU:
335       gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
336       break;
337
338     default:
339       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340       break;
341     }
342 }
343
344 static void 
345 gtk_menu_item_get_property (GObject    *object,
346                             guint       prop_id,
347                             GValue     *value,
348                             GParamSpec *pspec)
349 {
350   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
351   
352   switch (prop_id)
353     {
354     case PROP_SUBMENU:
355       g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
356       break;
357
358     default:
359       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360       break;
361     }
362 }
363
364 static void
365 gtk_menu_item_finalize (GObject *object)
366 {
367   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
368
369   g_free (menu_item->accel_path);
370
371   G_OBJECT_CLASS (gtk_menu_item_parent_class)->finalize (object);
372 }
373
374 static void
375 gtk_menu_item_destroy (GtkObject *object)
376 {
377   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
378
379   if (menu_item->submenu)
380     gtk_widget_destroy (menu_item->submenu);
381
382   GTK_OBJECT_CLASS (gtk_menu_item_parent_class)->destroy (object);
383 }
384
385 static void
386 gtk_menu_item_detacher (GtkWidget *widget,
387                         GtkMenu   *menu)
388 {
389   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
390
391   g_return_if_fail (menu_item->submenu == (GtkWidget*) menu);
392
393   menu_item->submenu = NULL;
394 }
395
396 /**
397  * gtk_menu_item_set_submenu:
398  * @menu_item: a #GtkMenuItem
399  * @submenu: the submenu, or %NULL
400  *
401  * Sets or replaces the menu item's submenu, or removes it when a %NULL
402  * submenu is passed.
403  **/
404 void
405 gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
406                            GtkWidget   *submenu)
407 {
408   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
409   g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
410   
411   if (menu_item->submenu != submenu)
412     {
413       if (menu_item->submenu)
414         gtk_menu_detach (GTK_MENU (menu_item->submenu));
415
416       if (submenu)
417         {
418           menu_item->submenu = submenu;
419           gtk_menu_attach_to_widget (GTK_MENU (submenu),
420                                      GTK_WIDGET (menu_item),
421                                      gtk_menu_item_detacher);
422         }
423
424       if (GTK_WIDGET (menu_item)->parent)
425         gtk_widget_queue_resize (GTK_WIDGET (menu_item));
426
427       g_object_notify (G_OBJECT (menu_item), "submenu");
428     }
429 }
430
431 /**
432  * gtk_menu_item_get_submenu:
433  * @menu_item: a #GtkMenuItem
434  *
435  * Gets the submenu underneath this menu item, if any. See
436  * gtk_menu_item_set_submenu().
437  *
438  * Return value: submenu for this menu item, or %NULL if none.
439  **/
440 GtkWidget *
441 gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
442 {
443   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);
444
445   return menu_item->submenu;
446 }
447
448 /**
449  * gtk_menu_item_remove_submenu:
450  * @menu_item: a #GtkMenuItem
451  *
452  * Removes the widget's submenu.
453  *
454  * Deprecated: 2.12: gtk_menu_item_remove_submenu() is deprecated and
455  *                   should not be used in newly written code. Use
456  *                   gtk_menu_item_set_submenu() instead.
457  **/
458 void
459 gtk_menu_item_remove_submenu (GtkMenuItem *menu_item)
460 {
461   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
462
463   gtk_menu_item_set_submenu (menu_item, NULL);
464 }
465
466 void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
467                                    GtkSubmenuPlacement  placement);
468
469 void
470 _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
471                              GtkSubmenuPlacement  placement)
472 {
473   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
474
475   menu_item->submenu_placement = placement;
476 }
477
478 void
479 gtk_menu_item_select (GtkMenuItem *menu_item)
480 {
481   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
482
483   gtk_item_select (GTK_ITEM (menu_item));
484
485   /* Enable themeing of the parent menu item depending on whether
486    * something is selected in its submenu
487    */
488   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
489     {
490       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
491
492       if (menu->parent_menu_item)
493         gtk_widget_queue_draw (GTK_WIDGET (menu->parent_menu_item));
494     }
495 }
496
497 void
498 gtk_menu_item_deselect (GtkMenuItem *menu_item)
499 {
500   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
501
502   gtk_item_deselect (GTK_ITEM (menu_item));
503
504   /* Enable themeing of the parent menu item depending on whether
505    * something is selected in its submenu
506    */
507   if (GTK_IS_MENU (GTK_WIDGET (menu_item)->parent))
508     {
509       GtkMenu *menu = GTK_MENU (GTK_WIDGET (menu_item)->parent);
510
511       if (menu->parent_menu_item)
512         gtk_widget_queue_draw (GTK_WIDGET (menu->parent_menu_item));
513     }
514 }
515
516 void
517 gtk_menu_item_activate (GtkMenuItem *menu_item)
518 {
519   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
520   
521   g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
522 }
523
524 void
525 gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
526                                    gint        *requisition)
527 {
528   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
529
530   g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
531 }
532
533 void
534 gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
535                                     gint         allocation)
536 {
537   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
538
539   g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
540 }
541
542 static void
543 gtk_menu_item_accel_width_foreach (GtkWidget *widget,
544                                    gpointer data)
545 {
546   guint *width = data;
547
548   if (GTK_IS_ACCEL_LABEL (widget))
549     {
550       guint w;
551
552       w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
553       *width = MAX (*width, w);
554     }
555   else if (GTK_IS_CONTAINER (widget))
556     gtk_container_foreach (GTK_CONTAINER (widget),
557                            gtk_menu_item_accel_width_foreach,
558                            data);
559 }
560
561 static gint
562 get_minimum_width (GtkWidget *widget)
563 {
564   PangoContext *context;
565   PangoFontMetrics *metrics;
566   gint height;
567
568   context = gtk_widget_get_pango_context (widget);
569   metrics = pango_context_get_metrics (context,
570                                        widget->style->font_desc,
571                                        pango_context_get_language (context));
572
573   height = pango_font_metrics_get_ascent (metrics) +
574       pango_font_metrics_get_descent (metrics);
575   
576   pango_font_metrics_unref (metrics);
577
578   return PANGO_PIXELS (7 * height);
579 }
580
581 static void
582 gtk_menu_item_size_request (GtkWidget      *widget,
583                             GtkRequisition *requisition)
584 {
585   GtkMenuItem *menu_item;
586   GtkBin *bin;
587   guint accel_width;
588   guint horizontal_padding;
589   GtkPackDirection pack_dir;
590   GtkPackDirection child_pack_dir;
591
592   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
593   g_return_if_fail (requisition != NULL);
594
595   gtk_widget_style_get (widget,
596                         "horizontal-padding", &horizontal_padding,
597                         NULL);
598   
599   bin = GTK_BIN (widget);
600   menu_item = GTK_MENU_ITEM (widget);
601
602   if (GTK_IS_MENU_BAR (widget->parent))
603     {
604       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (widget->parent));
605       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
606     }
607   else
608     {
609       pack_dir = GTK_PACK_DIRECTION_LTR;
610       child_pack_dir = GTK_PACK_DIRECTION_LTR;
611     }
612
613   requisition->width = (GTK_CONTAINER (widget)->border_width +
614                         widget->style->xthickness) * 2;
615   requisition->height = (GTK_CONTAINER (widget)->border_width +
616                          widget->style->ythickness) * 2;
617
618   if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
619       (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
620     requisition->width += 2 * horizontal_padding;
621   else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
622       (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
623     requisition->height += 2 * horizontal_padding;
624
625   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
626     {
627       GtkRequisition child_requisition;
628       
629       gtk_widget_size_request (bin->child, &child_requisition);
630
631       requisition->width += child_requisition.width;
632       requisition->height += child_requisition.height;
633
634       if (menu_item->submenu && menu_item->show_submenu_indicator)
635         {
636           guint arrow_spacing;
637           
638           gtk_widget_style_get (widget,
639                                 "arrow-spacing", &arrow_spacing,
640                                 NULL);
641
642           requisition->width += child_requisition.height;
643           requisition->width += arrow_spacing;
644
645           requisition->width = MAX (requisition->width, get_minimum_width (widget));
646         }
647     }
648   else /* separator item */
649     {
650       gboolean wide_separators;
651       gint     separator_height;
652
653       gtk_widget_style_get (widget,
654                             "wide-separators",  &wide_separators,
655                             "separator-height", &separator_height,
656                             NULL);
657
658       if (wide_separators)
659         requisition->height += separator_height + widget->style->ythickness;
660       else
661         requisition->height += widget->style->ythickness * 2;
662     }
663
664   accel_width = 0;
665   gtk_container_foreach (GTK_CONTAINER (menu_item),
666                          gtk_menu_item_accel_width_foreach,
667                          &accel_width);
668   menu_item->accelerator_width = accel_width;
669 }
670
671 static void
672 gtk_menu_item_size_allocate (GtkWidget     *widget,
673                              GtkAllocation *allocation)
674 {
675   GtkMenuItem *menu_item;
676   GtkBin *bin;
677   GtkAllocation child_allocation;
678   GtkTextDirection direction;
679   GtkPackDirection pack_dir;
680   GtkPackDirection child_pack_dir;
681
682   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
683   g_return_if_fail (allocation != NULL);
684
685   menu_item = GTK_MENU_ITEM (widget);
686   bin = GTK_BIN (widget);
687   
688   direction = gtk_widget_get_direction (widget);
689
690   if (GTK_IS_MENU_BAR (widget->parent))
691     {
692       pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (widget->parent));
693       child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
694     }
695   else
696     {
697       pack_dir = GTK_PACK_DIRECTION_LTR;
698       child_pack_dir = GTK_PACK_DIRECTION_LTR;
699     }
700     
701   widget->allocation = *allocation;
702
703   if (bin->child)
704     {
705       GtkRequisition child_requisition;
706       guint horizontal_padding;
707
708       gtk_widget_style_get (widget,
709                             "horizontal-padding", &horizontal_padding,
710                             NULL);
711
712       child_allocation.x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
713       child_allocation.y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
714
715       if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
716           (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
717         child_allocation.x += horizontal_padding;
718       else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
719                (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
720         child_allocation.y += horizontal_padding;
721       
722       child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
723       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
724
725       if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
726           child_pack_dir == GTK_PACK_DIRECTION_RTL)
727         {
728           if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
729             child_allocation.x += GTK_MENU_ITEM (widget)->toggle_size;
730           child_allocation.width -= GTK_MENU_ITEM (widget)->toggle_size;
731         }
732       else
733         {
734           if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
735             child_allocation.y += GTK_MENU_ITEM (widget)->toggle_size;
736           child_allocation.height -= GTK_MENU_ITEM (widget)->toggle_size;
737         }
738
739       child_allocation.x += widget->allocation.x;
740       child_allocation.y += widget->allocation.y;
741
742       gtk_widget_get_child_requisition (bin->child, &child_requisition);
743       if (menu_item->submenu && menu_item->show_submenu_indicator) 
744         {
745           if (direction == GTK_TEXT_DIR_RTL)
746             child_allocation.x += child_requisition.height;
747           child_allocation.width -= child_requisition.height;
748         }
749       
750       if (child_allocation.width < 1)
751         child_allocation.width = 1;
752
753       gtk_widget_size_allocate (bin->child, &child_allocation);
754     }
755
756   if (GTK_WIDGET_REALIZED (widget))
757     gdk_window_move_resize (menu_item->event_window,
758                             allocation->x, allocation->y,
759                             allocation->width, allocation->height);
760
761   if (menu_item->submenu)
762     gtk_menu_reposition (GTK_MENU (menu_item->submenu));
763 }
764
765 static void
766 gtk_menu_item_realize (GtkWidget *widget)
767 {
768   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
769   GdkWindowAttr attributes;
770   gint attributes_mask;
771
772   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
773
774   widget->window = gtk_widget_get_parent_window (widget);
775   g_object_ref (widget->window);
776   
777   attributes.x = widget->allocation.x;
778   attributes.y = widget->allocation.y;
779   attributes.width = widget->allocation.width;
780   attributes.height = widget->allocation.height;
781   attributes.window_type = GDK_WINDOW_CHILD;
782   attributes.wclass = GDK_INPUT_ONLY;
783   attributes.event_mask = (gtk_widget_get_events (widget) |
784                            GDK_BUTTON_PRESS_MASK |
785                            GDK_BUTTON_RELEASE_MASK |
786                            GDK_ENTER_NOTIFY_MASK |
787                            GDK_LEAVE_NOTIFY_MASK |
788                            GDK_POINTER_MOTION_MASK);
789
790   attributes_mask = GDK_WA_X | GDK_WA_Y;
791   menu_item->event_window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
792   gdk_window_set_user_data (menu_item->event_window, widget);
793
794   widget->style = gtk_style_attach (widget->style, widget->window);
795 }
796
797 static void
798 gtk_menu_item_unrealize (GtkWidget *widget)
799 {
800   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
801
802   gdk_window_set_user_data (menu_item->event_window, NULL);
803   gdk_window_destroy (menu_item->event_window);
804   menu_item->event_window = NULL;
805   
806   if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize)
807     (* GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize) (widget);
808 }
809
810 static void
811 gtk_menu_item_map (GtkWidget *widget)
812 {
813   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
814   
815   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
816
817   gdk_window_show (menu_item->event_window);
818 }
819
820 static void
821 gtk_menu_item_unmap (GtkWidget *widget)
822 {
823   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
824     
825   gdk_window_hide (menu_item->event_window);
826
827   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
828 }
829
830 static void
831 gtk_menu_item_paint (GtkWidget    *widget,
832                      GdkRectangle *area)
833 {
834   GtkMenuItem *menu_item;
835   GtkStateType state_type;
836   GtkShadowType shadow_type, selected_shadow_type;
837   gint width, height;
838   gint x, y;
839   gint border_width = GTK_CONTAINER (widget)->border_width;
840
841   if (GTK_WIDGET_DRAWABLE (widget))
842     {
843       menu_item = GTK_MENU_ITEM (widget);
844
845       state_type = widget->state;
846       
847       x = widget->allocation.x + border_width;
848       y = widget->allocation.y + border_width;
849       width = widget->allocation.width - border_width * 2;
850       height = widget->allocation.height - border_width * 2;
851       
852       if ((state_type == GTK_STATE_PRELIGHT) &&
853           (GTK_BIN (menu_item)->child))
854         {
855           gtk_widget_style_get (widget,
856                                 "selected-shadow-type", &selected_shadow_type,
857                                 NULL);
858           gtk_paint_box (widget->style,
859                          widget->window,
860                          GTK_STATE_PRELIGHT,
861                          selected_shadow_type,
862                          area, widget, "menuitem",
863                          x, y, width, height);
864         }
865   
866       if (menu_item->submenu && menu_item->show_submenu_indicator)
867         {
868           gint arrow_x, arrow_y;
869           gint arrow_size;
870           gint arrow_extent;
871           guint horizontal_padding;
872           gfloat arrow_scaling;
873           GtkTextDirection direction;
874           GtkArrowType arrow_type;
875           PangoContext *context;
876           PangoFontMetrics *metrics;
877
878           direction = gtk_widget_get_direction (widget);
879       
880           gtk_widget_style_get (widget,
881                                 "horizontal-padding", &horizontal_padding,
882                                 "arrow-scaling", &arrow_scaling,
883                                 NULL);
884           
885           context = gtk_widget_get_pango_context (GTK_BIN (menu_item)->child);
886           metrics = pango_context_get_metrics (context, 
887                                                GTK_WIDGET (GTK_BIN (menu_item)->child)->style->font_desc,
888                                                pango_context_get_language (context));
889
890           arrow_size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
891                                       pango_font_metrics_get_descent (metrics)));
892
893           pango_font_metrics_unref (metrics);
894
895           arrow_extent = arrow_size * arrow_scaling;
896
897           g_printerr ("%s: scaling = %f   size = %d   extent = %d\n",
898                       G_STRFUNC, arrow_scaling, arrow_size, arrow_extent);
899
900           shadow_type = GTK_SHADOW_OUT;
901           if (state_type == GTK_STATE_PRELIGHT)
902             shadow_type = GTK_SHADOW_IN;
903
904           if (direction == GTK_TEXT_DIR_LTR)
905             {
906               arrow_x = x + width - horizontal_padding - arrow_extent;
907               arrow_type = GTK_ARROW_RIGHT;
908             }
909           else
910             {
911               arrow_x = x + horizontal_padding;
912               arrow_type = GTK_ARROW_LEFT;
913             }
914
915           arrow_y = y + (height - arrow_extent) / 2;
916
917           gtk_paint_arrow (widget->style, widget->window,
918                            state_type, shadow_type, 
919                            area, widget, "menuitem", 
920                            arrow_type, TRUE,
921                            arrow_x, arrow_y,
922                            arrow_extent, arrow_extent);
923         }
924       else if (!GTK_BIN (menu_item)->child)
925         {
926           gboolean wide_separators;
927           gint     separator_height;
928           guint    horizontal_padding;
929
930           gtk_widget_style_get (widget,
931                                 "wide-separators",    &wide_separators,
932                                 "separator-height",   &separator_height,
933                                 "horizontal-padding", &horizontal_padding,
934                                 NULL);
935
936           if (wide_separators)
937             gtk_paint_box (widget->style, widget->window,
938                            GTK_STATE_NORMAL, GTK_SHADOW_ETCHED_OUT,
939                            area, widget, "hseparator",
940                            widget->allocation.x + horizontal_padding + widget->style->xthickness,
941                            widget->allocation.y + (widget->allocation.height -
942                                                    separator_height -
943                                                    widget->style->ythickness) / 2,
944                            widget->allocation.width -
945                            2 * (horizontal_padding + widget->style->xthickness),
946                            separator_height);
947           else
948             gtk_paint_hline (widget->style, widget->window,
949                              GTK_STATE_NORMAL, area, widget, "menuitem",
950                              widget->allocation.x + horizontal_padding + widget->style->xthickness,
951                              widget->allocation.x + widget->allocation.width - horizontal_padding - widget->style->xthickness - 1,
952                              widget->allocation.y + (widget->allocation.height -
953                                                      widget->style->ythickness) / 2);
954         }
955     }
956 }
957
958 static gint
959 gtk_menu_item_expose (GtkWidget      *widget,
960                       GdkEventExpose *event)
961 {
962   g_return_val_if_fail (GTK_IS_MENU_ITEM (widget), FALSE);
963   g_return_val_if_fail (event != NULL, FALSE);
964
965   if (GTK_WIDGET_DRAWABLE (widget))
966     {
967       gtk_menu_item_paint (widget, &event->area);
968
969       (* GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->expose_event) (widget, event);
970     }
971
972   return FALSE;
973 }
974
975 static void
976 gtk_real_menu_item_select (GtkItem *item)
977 {
978   GtkMenuItem *menu_item;
979   gboolean touchscreen_mode;
980
981   g_return_if_fail (GTK_IS_MENU_ITEM (item));
982
983   menu_item = GTK_MENU_ITEM (item);
984
985   g_object_get (gtk_widget_get_settings (GTK_WIDGET (item)),
986                 "gtk-touchscreen-mode", &touchscreen_mode,
987                 NULL);
988
989   if (!touchscreen_mode &&
990       menu_item->submenu &&
991       (!GTK_WIDGET_MAPPED (menu_item->submenu) ||
992        GTK_MENU (menu_item->submenu)->tearoff_active))
993     {
994       _gtk_menu_item_popup_submenu (GTK_WIDGET (menu_item), TRUE);
995     }
996
997   gtk_widget_set_state (GTK_WIDGET (menu_item), GTK_STATE_PRELIGHT);
998   gtk_widget_queue_draw (GTK_WIDGET (menu_item));
999 }
1000
1001 static void
1002 gtk_real_menu_item_deselect (GtkItem *item)
1003 {
1004   GtkMenuItem *menu_item;
1005
1006   g_return_if_fail (GTK_IS_MENU_ITEM (item));
1007
1008   menu_item = GTK_MENU_ITEM (item);
1009
1010   if (menu_item->submenu)
1011     _gtk_menu_item_popdown_submenu (GTK_WIDGET (menu_item));
1012
1013   gtk_widget_set_state (GTK_WIDGET (menu_item), GTK_STATE_NORMAL);
1014   gtk_widget_queue_draw (GTK_WIDGET (menu_item));
1015 }
1016
1017 static gboolean
1018 gtk_menu_item_mnemonic_activate (GtkWidget *widget,
1019                                  gboolean   group_cycling)
1020 {
1021   if (group_cycling &&
1022       widget->parent &&
1023       GTK_IS_MENU_SHELL (widget->parent) &&
1024       GTK_MENU_SHELL (widget->parent)->active)
1025     {
1026       gtk_menu_shell_select_item (GTK_MENU_SHELL (widget->parent),
1027                                   widget);
1028     }
1029   else
1030     g_signal_emit (widget, menu_item_signals[ACTIVATE_ITEM], 0);
1031   
1032   return TRUE;
1033 }
1034
1035 static void
1036 gtk_real_menu_item_activate_item (GtkMenuItem *menu_item)
1037 {
1038   GtkWidget *widget;
1039
1040   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1041
1042   widget = GTK_WIDGET (menu_item);
1043   
1044   if (widget->parent &&
1045       GTK_IS_MENU_SHELL (widget->parent))
1046     {
1047       if (menu_item->submenu == NULL)
1048         gtk_menu_shell_activate_item (GTK_MENU_SHELL (widget->parent),
1049                                       widget, TRUE);
1050       else
1051         {
1052           GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget->parent);
1053
1054           _gtk_menu_shell_activate (menu_shell);
1055
1056           gtk_menu_shell_select_item (GTK_MENU_SHELL (widget->parent), widget);
1057           _gtk_menu_item_popup_submenu (widget, FALSE);
1058
1059           gtk_menu_shell_select_first (GTK_MENU_SHELL (menu_item->submenu), TRUE);
1060         }
1061     }
1062 }
1063
1064 static void
1065 gtk_real_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1066                                         gint        *requisition)
1067 {
1068   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1069
1070   *requisition = 0;
1071 }
1072
1073 static void
1074 gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1075                                          gint         allocation)
1076 {
1077   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1078
1079   menu_item->toggle_size = allocation;
1080 }
1081
1082 static void
1083 gtk_menu_item_real_popup_submenu (GtkWidget *widget,
1084                                   gboolean   remember_exact_time)
1085 {
1086   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1087
1088   if (GTK_WIDGET_IS_SENSITIVE (menu_item->submenu))
1089     {
1090       gboolean take_focus;
1091
1092       take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (widget->parent));
1093       gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (menu_item->submenu),
1094                                      take_focus);
1095
1096       if (remember_exact_time)
1097         {
1098           GTimeVal *popup_time = g_new0 (GTimeVal, 1);
1099
1100           g_get_current_time (popup_time);
1101
1102           g_object_set_data_full (G_OBJECT (menu_item->submenu),
1103                                   "gtk-menu-exact-popup-time", popup_time,
1104                                   (GDestroyNotify) g_free);
1105         }
1106       else
1107         {
1108           g_object_set_data (G_OBJECT (menu_item->submenu),
1109                              "gtk-menu-exact-popup-time", NULL);
1110         }
1111
1112       gtk_menu_popup (GTK_MENU (menu_item->submenu),
1113                       widget->parent,
1114                       widget,
1115                       gtk_menu_item_position_menu,
1116                       menu_item,
1117                       GTK_MENU_SHELL (widget->parent)->button,
1118                       0);
1119     }
1120
1121   /* Enable themeing of the parent menu item depending on whether
1122    * its submenu is shown or not.
1123    */
1124   gtk_widget_queue_draw (widget);
1125 }
1126
1127 static gint
1128 gtk_menu_item_popup_timeout (gpointer data)
1129 {
1130   GtkMenuItem *menu_item;
1131   GtkWidget *parent;
1132   
1133   menu_item = GTK_MENU_ITEM (data);
1134
1135   parent = GTK_WIDGET (menu_item)->parent;
1136
1137   if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->active) || 
1138       (GTK_IS_MENU (parent) && GTK_MENU (parent)->torn_off))
1139     {
1140       gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
1141       if (menu_item->timer_from_keypress && menu_item->submenu)
1142         GTK_MENU_SHELL (menu_item->submenu)->ignore_enter = TRUE;
1143     }
1144
1145   menu_item->timer = 0;
1146
1147   return FALSE;  
1148 }
1149
1150 static gint
1151 get_popup_delay (GtkWidget *widget)
1152 {
1153   if (GTK_IS_MENU_SHELL (widget->parent))
1154     {
1155       return _gtk_menu_shell_get_popup_delay (GTK_MENU_SHELL (widget->parent));
1156     }
1157   else
1158     {
1159       gint popup_delay;
1160
1161       g_object_get (gtk_widget_get_settings (widget),
1162                     "gtk-menu-popup-delay", &popup_delay,
1163                     NULL);
1164
1165       return popup_delay;
1166     }
1167 }
1168
1169 void
1170 _gtk_menu_item_popup_submenu (GtkWidget *widget,
1171                               gboolean   with_delay)
1172 {
1173   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1174
1175   if (menu_item->timer)
1176     {
1177       g_source_remove (menu_item->timer);
1178       menu_item->timer = 0;
1179       with_delay = FALSE;
1180     }
1181
1182   if (with_delay)
1183     {
1184       gint popup_delay = get_popup_delay (widget);
1185
1186       if (popup_delay > 0)
1187         {
1188           GdkEvent *event = gtk_get_current_event ();
1189
1190           menu_item->timer = gdk_threads_add_timeout (popup_delay,
1191                                                       gtk_menu_item_popup_timeout,
1192                                                       menu_item);
1193
1194           if (event &&
1195               event->type != GDK_BUTTON_PRESS &&
1196               event->type != GDK_ENTER_NOTIFY)
1197             menu_item->timer_from_keypress = TRUE;
1198           else
1199             menu_item->timer_from_keypress = FALSE;
1200
1201           if (event)
1202             gdk_event_free (event);
1203
1204           return;
1205         }
1206     }
1207
1208   gtk_menu_item_real_popup_submenu (widget, FALSE);
1209 }
1210
1211 void
1212 _gtk_menu_item_popdown_submenu (GtkWidget *widget)
1213 {
1214   GtkMenuItem *menu_item;
1215
1216   menu_item = GTK_MENU_ITEM (widget);
1217
1218   if (menu_item->submenu)
1219     {
1220       g_object_set_data (G_OBJECT (menu_item->submenu),
1221                          "gtk-menu-exact-popup-time", NULL);
1222
1223       if (menu_item->timer)
1224         {
1225           g_source_remove (menu_item->timer);
1226           menu_item->timer = 0;
1227         }
1228       else
1229         gtk_menu_popdown (GTK_MENU (menu_item->submenu));
1230
1231       gtk_widget_queue_draw (widget);
1232     }
1233 }
1234
1235 static void
1236 get_offsets (GtkMenu *menu,
1237              gint    *horizontal_offset,
1238              gint    *vertical_offset)
1239 {
1240   gint vertical_padding;
1241   gint horizontal_padding;
1242   
1243   gtk_widget_style_get (GTK_WIDGET (menu),
1244                         "horizontal-offset", horizontal_offset,
1245                         "vertical-offset", vertical_offset,
1246                         "horizontal-padding", &horizontal_padding,
1247                         "vertical-padding", &vertical_padding,
1248                         NULL);
1249
1250   *vertical_offset -= GTK_WIDGET (menu)->style->ythickness;
1251   *vertical_offset -= vertical_padding;
1252   *horizontal_offset += horizontal_padding;
1253 }
1254
1255 static void
1256 gtk_menu_item_position_menu (GtkMenu  *menu,
1257                              gint     *x,
1258                              gint     *y,
1259                              gboolean *push_in,
1260                              gpointer  user_data)
1261 {
1262   GtkMenuItem *menu_item;
1263   GtkWidget *widget;
1264   GtkMenuItem *parent_menu_item;
1265   GdkScreen *screen;
1266   gint twidth, theight;
1267   gint tx, ty;
1268   GtkTextDirection direction;
1269   GdkRectangle monitor;
1270   gint monitor_num;
1271   gint horizontal_offset;
1272   gint vertical_offset;
1273   gint parent_xthickness;
1274   gint available_left, available_right;
1275
1276   g_return_if_fail (menu != NULL);
1277   g_return_if_fail (x != NULL);
1278   g_return_if_fail (y != NULL);
1279
1280   menu_item = GTK_MENU_ITEM (user_data);
1281   widget = GTK_WIDGET (user_data);
1282
1283   if (push_in)
1284     *push_in = FALSE;
1285
1286   direction = gtk_widget_get_direction (widget);
1287
1288   twidth = GTK_WIDGET (menu)->requisition.width;
1289   theight = GTK_WIDGET (menu)->requisition.height;
1290
1291   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
1292   monitor_num = gdk_screen_get_monitor_at_window (screen, menu_item->event_window);
1293   if (monitor_num < 0)
1294     monitor_num = 0;
1295   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
1296
1297   if (!gdk_window_get_origin (widget->window, &tx, &ty))
1298     {
1299       g_warning ("Menu not on screen");
1300       return;
1301     }
1302
1303   tx += widget->allocation.x;
1304   ty += widget->allocation.y;
1305
1306   get_offsets (menu, &horizontal_offset, &vertical_offset);
1307
1308   available_left = tx - monitor.x;
1309   available_right = monitor.x + monitor.width - (tx + widget->allocation.width);
1310
1311   if (GTK_IS_MENU_BAR (widget->parent))
1312     {
1313       menu_item->from_menubar = TRUE;
1314     }
1315   else if (GTK_IS_MENU (widget->parent))
1316     {
1317       if (GTK_MENU (widget->parent)->parent_menu_item)
1318         menu_item->from_menubar = GTK_MENU_ITEM (GTK_MENU (widget->parent)->parent_menu_item)->from_menubar;
1319       else
1320         menu_item->from_menubar = FALSE;
1321     }
1322   else
1323     {
1324       menu_item->from_menubar = FALSE;
1325     }
1326   
1327   switch (menu_item->submenu_placement)
1328     {
1329     case GTK_TOP_BOTTOM:
1330       if (direction == GTK_TEXT_DIR_LTR)
1331         menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
1332       else 
1333         {
1334           menu_item->submenu_direction = GTK_DIRECTION_LEFT;
1335           tx += widget->allocation.width - twidth;
1336         }
1337       if ((ty + widget->allocation.height + theight) <= monitor.y + monitor.height)
1338         ty += widget->allocation.height;
1339       else if ((ty - theight) >= monitor.y)
1340         ty -= theight;
1341       else if (monitor.y + monitor.height - (ty + widget->allocation.height) > ty)
1342         ty += widget->allocation.height;
1343       else
1344         ty -= theight;
1345       break;
1346
1347     case GTK_LEFT_RIGHT:
1348       if (GTK_IS_MENU (widget->parent))
1349         parent_menu_item = GTK_MENU_ITEM (GTK_MENU (widget->parent)->parent_menu_item);
1350       else
1351         parent_menu_item = NULL;
1352       
1353       parent_xthickness = widget->parent->style->xthickness;
1354
1355       if (parent_menu_item && !GTK_MENU (widget->parent)->torn_off)
1356         {
1357           menu_item->submenu_direction = parent_menu_item->submenu_direction;
1358         }
1359       else
1360         {
1361           if (direction == GTK_TEXT_DIR_LTR)
1362             menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
1363           else
1364             menu_item->submenu_direction = GTK_DIRECTION_LEFT;
1365         }
1366
1367       switch (menu_item->submenu_direction)
1368         {
1369         case GTK_DIRECTION_LEFT:
1370           if (tx - twidth - parent_xthickness - horizontal_offset >= monitor.x ||
1371               available_left >= available_right)
1372             tx -= twidth + parent_xthickness + horizontal_offset;
1373           else
1374             {
1375               menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
1376               tx += widget->allocation.width + parent_xthickness + horizontal_offset;
1377             }
1378           break;
1379
1380         case GTK_DIRECTION_RIGHT:
1381           if (tx + widget->allocation.width + parent_xthickness + horizontal_offset + twidth <= monitor.x + monitor.width ||
1382               available_right >= available_left)
1383             tx += widget->allocation.width + parent_xthickness + horizontal_offset;
1384           else
1385             {
1386               menu_item->submenu_direction = GTK_DIRECTION_LEFT;
1387               tx -= twidth + parent_xthickness + horizontal_offset;
1388             }
1389           break;
1390         }
1391
1392       ty += vertical_offset;
1393       
1394       /* If the height of the menu doesn't fit we move it upward. */
1395       ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
1396       break;
1397     }
1398
1399   /* If we have negative, tx, here it is because we can't get
1400    * the menu all the way on screen. Favor the left portion.
1401    */
1402   *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
1403   *y = ty;
1404
1405   gtk_menu_set_monitor (menu, monitor_num);
1406
1407   if (!GTK_WIDGET_VISIBLE (menu->toplevel))
1408     {
1409       gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), menu_item->from_menubar?
1410                                 GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
1411     }
1412 }
1413
1414 /**
1415  * gtk_menu_item_set_right_justified:
1416  * @menu_item: a #GtkMenuItem.
1417  * @right_justified: if %TRUE the menu item will appear at the 
1418  *   far right if added to a menu bar.
1419  * 
1420  * Sets whether the menu item appears justified at the right
1421  * side of a menu bar. This was traditionally done for "Help" menu
1422  * items, but is now considered a bad idea. (If the widget
1423  * layout is reversed for a right-to-left language like Hebrew
1424  * or Arabic, right-justified-menu-items appear at the left.)
1425  **/
1426 void
1427 gtk_menu_item_set_right_justified (GtkMenuItem *menu_item,
1428                                    gboolean     right_justified)
1429 {
1430   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1431
1432   right_justified = right_justified != FALSE;
1433
1434   if (right_justified != menu_item->right_justify)
1435     {
1436       menu_item->right_justify = right_justified;
1437       gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1438     }
1439 }
1440
1441 /**
1442  * gtk_menu_item_get_right_justified:
1443  * @menu_item: a #GtkMenuItem
1444  * 
1445  * Gets whether the menu item appears justified at the right
1446  * side of the menu bar.
1447  * 
1448  * Return value: %TRUE if the menu item will appear at the
1449  *   far right if added to a menu bar.
1450  **/
1451 gboolean
1452 gtk_menu_item_get_right_justified (GtkMenuItem *menu_item)
1453 {
1454   g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), FALSE);
1455   
1456   return menu_item->right_justify;
1457 }
1458
1459
1460 static void
1461 gtk_menu_item_show_all (GtkWidget *widget)
1462 {
1463   GtkMenuItem *menu_item;
1464
1465   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1466
1467   menu_item = GTK_MENU_ITEM (widget);
1468
1469   /* show children including submenu */
1470   if (menu_item->submenu)
1471     gtk_widget_show_all (menu_item->submenu);
1472   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
1473
1474   gtk_widget_show (widget);
1475 }
1476
1477 static void
1478 gtk_menu_item_hide_all (GtkWidget *widget)
1479 {
1480   GtkMenuItem *menu_item;
1481
1482   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1483
1484   gtk_widget_hide (widget);
1485
1486   menu_item = GTK_MENU_ITEM (widget);
1487
1488   /* hide children including submenu */
1489   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
1490   if (menu_item->submenu)
1491     gtk_widget_hide_all (menu_item->submenu);
1492 }
1493
1494 static gboolean
1495 gtk_menu_item_can_activate_accel (GtkWidget *widget,
1496                                   guint      signal_id)
1497 {
1498   /* Chain to the parent GtkMenu for further checks */
1499   return (GTK_WIDGET_IS_SENSITIVE (widget) && GTK_WIDGET_VISIBLE (widget) &&
1500           widget->parent && gtk_widget_can_activate_accel (widget->parent, signal_id));
1501 }
1502
1503 static void
1504 gtk_menu_item_accel_name_foreach (GtkWidget *widget,
1505                                   gpointer data)
1506 {
1507   const gchar **path_p = data;
1508
1509   if (!*path_p)
1510     {
1511       if (GTK_IS_LABEL (widget))
1512         {
1513           *path_p = gtk_label_get_text (GTK_LABEL (widget));
1514           if (*path_p && (*path_p)[0] == 0)
1515             *path_p = NULL;
1516         }
1517       else if (GTK_IS_CONTAINER (widget))
1518         gtk_container_foreach (GTK_CONTAINER (widget),
1519                                gtk_menu_item_accel_name_foreach,
1520                                data);
1521     }
1522 }
1523
1524 static void
1525 gtk_menu_item_parent_set (GtkWidget *widget,
1526                           GtkWidget *previous_parent)
1527 {
1528   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1529   GtkMenu *menu = GTK_IS_MENU (widget->parent) ? GTK_MENU (widget->parent) : NULL;
1530
1531   if (menu)
1532     _gtk_menu_item_refresh_accel_path (menu_item,
1533                                        menu->accel_path,
1534                                        menu->accel_group,
1535                                        TRUE);
1536
1537   if (GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set)
1538     GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->parent_set (widget, previous_parent);
1539 }
1540
1541 void
1542 _gtk_menu_item_refresh_accel_path (GtkMenuItem   *menu_item,
1543                                    const gchar   *prefix,
1544                                    GtkAccelGroup *accel_group,
1545                                    gboolean       group_changed)
1546 {
1547   const gchar *path;
1548   GtkWidget *widget;
1549
1550   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1551   g_return_if_fail (!accel_group || GTK_IS_ACCEL_GROUP (accel_group));
1552
1553   widget = GTK_WIDGET (menu_item);
1554
1555   if (!accel_group)
1556     {
1557       gtk_widget_set_accel_path (widget, NULL, NULL);
1558       return;
1559     }
1560
1561   path = _gtk_widget_get_accel_path (widget, NULL);
1562   if (!path)                                    /* no active accel_path yet */
1563     {
1564       path = menu_item->accel_path;
1565       if (!path && prefix)
1566         {
1567           gchar *postfix = NULL;
1568
1569           /* try to construct one from label text */
1570           gtk_container_foreach (GTK_CONTAINER (menu_item),
1571                                  gtk_menu_item_accel_name_foreach,
1572                                  &postfix);
1573           menu_item->accel_path = postfix ? g_strconcat (prefix, "/", postfix, NULL) : NULL;
1574           path = menu_item->accel_path;
1575         }
1576       if (path)
1577         gtk_widget_set_accel_path (widget, path, accel_group);
1578     }
1579   else if (group_changed)                       /* reinstall accelerators */
1580     gtk_widget_set_accel_path (widget, path, accel_group);
1581 }
1582
1583 /**
1584  * gtk_menu_item_set_accel_path
1585  * @menu_item:  a valid #GtkMenuItem
1586  * @accel_path: accelerator path, corresponding to this menu item's
1587  *              functionality, or %NULL to unset the current path.
1588  *
1589  * Set the accelerator path on @menu_item, through which runtime changes of the
1590  * menu item's accelerator caused by the user can be identified and saved to
1591  * persistant storage (see gtk_accel_map_save() on this).
1592  * To setup a default accelerator for this menu item, call
1593  * gtk_accel_map_add_entry() with the same @accel_path.
1594  * See also gtk_accel_map_add_entry() on the specifics of accelerator paths,
1595  * and gtk_menu_set_accel_path() for a more convenient variant of this function.
1596  *
1597  * This function is basically a convenience wrapper that handles calling
1598  * gtk_widget_set_accel_path() with the appropriate accelerator group for
1599  * the menu item.
1600  *
1601  * Note that you do need to set an accelerator on the parent menu with
1602  * gtk_menu_set_accel_group() for this to work.
1603  */
1604 void
1605 gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
1606                               const gchar *accel_path)
1607 {
1608   GtkWidget *widget;
1609
1610   g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1611   g_return_if_fail (accel_path == NULL ||
1612                     (accel_path[0] == '<' && strchr (accel_path, '/')));
1613
1614   widget = GTK_WIDGET (menu_item);
1615
1616   /* store new path */
1617   g_free (menu_item->accel_path);
1618   menu_item->accel_path = g_strdup (accel_path);
1619
1620   /* forget accelerators associated with old path */
1621   gtk_widget_set_accel_path (widget, NULL, NULL);
1622
1623   /* install accelerators associated with new path */
1624   if (widget->parent && GTK_IS_MENU (widget->parent))
1625     {
1626       GtkMenu *menu = GTK_MENU (widget->parent);
1627
1628       if (menu->accel_group)
1629         _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1630                                            NULL,
1631                                            menu->accel_group,
1632                                            FALSE);
1633     }
1634 }
1635
1636 static void
1637 gtk_menu_item_forall (GtkContainer *container,
1638                       gboolean      include_internals,
1639                       GtkCallback   callback,
1640                       gpointer      callback_data)
1641 {
1642   GtkBin *bin;
1643
1644   g_return_if_fail (GTK_IS_MENU_ITEM (container));
1645   g_return_if_fail (callback != NULL);
1646
1647   bin = GTK_BIN (container);
1648
1649   if (bin->child)
1650     callback (bin->child, callback_data);
1651 }
1652
1653 gboolean
1654 _gtk_menu_item_is_selectable (GtkWidget *menu_item)
1655 {
1656   if ((!GTK_BIN (menu_item)->child &&
1657        G_OBJECT_TYPE (menu_item) == GTK_TYPE_MENU_ITEM) ||
1658       GTK_IS_SEPARATOR_MENU_ITEM (menu_item) ||
1659       !GTK_WIDGET_IS_SENSITIVE (menu_item) ||
1660       !GTK_WIDGET_VISIBLE (menu_item))
1661     return FALSE;
1662
1663   return TRUE;
1664 }
1665
1666 #define __GTK_MENU_ITEM_C__
1667 #include "gtkaliasdef.c"