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