]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
Merge branch 'windows_list'
[~andy/gtk] / gtk / gtkmenu.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 #include "config.h"
29 #include <string.h>
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
34 #include "gtkcheckmenuitem.h"
35 #include  <gobject/gvaluecollector.h>
36 #include "gtkmain.h"
37 #include "gtkmarshalers.h"
38 #include "gtkmenu.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
41 #include "gtkhbox.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
45 #include "gtkintl.h"
46
47 #define DEFAULT_POPUP_DELAY     225
48 #define DEFAULT_POPDOWN_DELAY  1000
49
50 #define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
51                                          * extends below the submenu
52                                          */
53
54 #define MENU_SCROLL_STEP1      8
55 #define MENU_SCROLL_STEP2     15
56 #define MENU_SCROLL_FAST_ZONE  8
57 #define MENU_SCROLL_TIMEOUT1  50
58 #define MENU_SCROLL_TIMEOUT2  20
59
60 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
61 #define ATTACHED_MENUS "gtk-attached-menus"
62
63 typedef struct _GtkMenuAttachData       GtkMenuAttachData;
64 typedef struct _GtkMenuPrivate          GtkMenuPrivate;
65 typedef struct _GtkMenuPopdownData      GtkMenuPopdownData;
66
67 struct _GtkMenuAttachData
68 {
69   GtkWidget *attach_widget;
70   GtkMenuDetachFunc detacher;
71 };
72
73 struct _GtkMenuPrivate 
74 {
75   gint x;
76   gint y;
77   gboolean initially_pushed_in;
78
79   /* info used for the table */
80   guint *heights;
81   gint heights_length;
82
83   gint monitor_num;
84
85   /* Cached layout information */
86   gint n_rows;
87   gint n_columns;
88
89   gchar *title;
90
91   /* Arrow states */
92   GtkStateType lower_arrow_state;
93   GtkStateType upper_arrow_state;
94
95   /* navigation region */
96   int navigation_x;
97   int navigation_y;
98   int navigation_width;
99   int navigation_height;
100
101   guint have_layout           : 1;
102   guint seen_item_enter       : 1;
103   guint have_position         : 1;
104   guint ignore_button_release : 1;
105   guint no_toggle_size        : 1;
106 };
107
108 struct _GtkMenuPopdownData
109 {
110   GtkMenu *menu;
111   GdkDevice *device;
112 };
113
114 typedef struct
115 {
116   gint left_attach;
117   gint right_attach;
118   gint top_attach;
119   gint bottom_attach;
120   gint effective_left_attach;
121   gint effective_right_attach;
122   gint effective_top_attach;
123   gint effective_bottom_attach;
124 } AttachInfo;
125
126 enum {
127   MOVE_SCROLL,
128   LAST_SIGNAL
129 };
130
131 enum {
132   PROP_0,
133   PROP_ACTIVE,
134   PROP_ACCEL_GROUP,
135   PROP_ACCEL_PATH,
136   PROP_ATTACH_WIDGET,
137   PROP_TEAROFF_STATE,
138   PROP_TEAROFF_TITLE,
139   PROP_MONITOR,
140   PROP_RESERVE_TOGGLE_SIZE
141 };
142
143 enum {
144   CHILD_PROP_0,
145   CHILD_PROP_LEFT_ATTACH,
146   CHILD_PROP_RIGHT_ATTACH,
147   CHILD_PROP_TOP_ATTACH,
148   CHILD_PROP_BOTTOM_ATTACH
149 };
150
151 static void     gtk_menu_set_property      (GObject          *object,
152                                             guint             prop_id,
153                                             const GValue     *value,
154                                             GParamSpec       *pspec);
155 static void     gtk_menu_get_property      (GObject          *object,
156                                             guint             prop_id,
157                                             GValue           *value,
158                                             GParamSpec       *pspec);
159 static void     gtk_menu_set_child_property(GtkContainer     *container,
160                                             GtkWidget        *child,
161                                             guint             property_id,
162                                             const GValue     *value,
163                                             GParamSpec       *pspec);
164 static void     gtk_menu_get_child_property(GtkContainer     *container,
165                                             GtkWidget        *child,
166                                             guint             property_id,
167                                             GValue           *value,
168                                             GParamSpec       *pspec);
169 static void     gtk_menu_destroy           (GtkObject        *object);
170 static void     gtk_menu_realize           (GtkWidget        *widget);
171 static void     gtk_menu_unrealize         (GtkWidget        *widget);
172 static void     gtk_menu_size_request      (GtkWidget        *widget,
173                                             GtkRequisition   *requisition);
174 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
175                                             GtkAllocation    *allocation);
176 static void     gtk_menu_paint             (GtkWidget        *widget,
177                                             GdkEventExpose   *expose);
178 static void     gtk_menu_show              (GtkWidget        *widget);
179 static gboolean gtk_menu_expose            (GtkWidget        *widget,
180                                             GdkEventExpose   *event);
181 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
182                                             GdkEventKey      *event);
183 static gboolean gtk_menu_scroll            (GtkWidget        *widget,
184                                             GdkEventScroll   *event);
185 static gboolean gtk_menu_button_press      (GtkWidget        *widget,
186                                             GdkEventButton   *event);
187 static gboolean gtk_menu_button_release    (GtkWidget        *widget,
188                                             GdkEventButton   *event);
189 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
190                                             GdkEventMotion   *event);
191 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
192                                             GdkEventCrossing *event);
193 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
194                                             GdkEventCrossing *event);
195 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
196                                             gint              offset);
197 static void     gtk_menu_grab_notify       (GtkWidget        *widget,
198                                             gboolean          was_grabbed);
199
200 static void     gtk_menu_stop_scrolling         (GtkMenu  *menu);
201 static void     gtk_menu_remove_scroll_timeout  (GtkMenu  *menu);
202 static gboolean gtk_menu_scroll_timeout         (gpointer  data);
203 static gboolean gtk_menu_scroll_timeout_initial (gpointer  data);
204 static void     gtk_menu_start_scrolling        (GtkMenu  *menu);
205
206 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
207                                               GtkWidget       *menu_item);
208 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
209                                             GtkWidget        *menu_item);
210 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
211                                             GtkWidget        *child,
212                                             gint              position);
213 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
214                                             GtkMenu          *menu);
215 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
216                                             gint              event_x,
217                                             gint              event_y,
218                                             gboolean          enter,
219                                             gboolean          motion);
220 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
221                                             gint             width);
222 static void     gtk_menu_style_set         (GtkWidget        *widget,
223                                             GtkStyle         *previous_style);
224 static gboolean gtk_menu_focus             (GtkWidget        *widget,
225                                             GtkDirectionType direction);
226 static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
227 static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
228                                             GtkMenuDirectionType direction);
229 static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
230                                             GtkScrollType     type);
231
232 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
233 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
234 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
235                                                         gint              event_x,
236                                                         gint              event_y);
237 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
238                                                         GtkMenuItem      *menu_item,
239                                                         GdkEventCrossing *event);
240  
241 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
242 static void gtk_menu_show_all       (GtkWidget         *widget);
243 static void gtk_menu_hide_all       (GtkWidget         *widget);
244 static void gtk_menu_position       (GtkMenu           *menu);
245 static void gtk_menu_reparent       (GtkMenu           *menu, 
246                                      GtkWidget         *new_parent, 
247                                      gboolean           unrealize);
248 static void gtk_menu_remove         (GtkContainer      *menu,
249                                      GtkWidget         *widget);
250
251 static void gtk_menu_update_title   (GtkMenu           *menu);
252
253 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
254 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
255
256 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
257                                                   guint      signal_id);
258 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
259                                            gboolean group_changed);
260
261 static const gchar attach_data_key[] = "gtk-menu-attach-data";
262
263 static guint menu_signals[LAST_SIGNAL] = { 0 };
264
265 static GtkMenuPrivate *
266 gtk_menu_get_private (GtkMenu *menu)
267 {
268   return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
269 }
270
271 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
272
273 static void
274 menu_queue_resize (GtkMenu *menu)
275 {
276   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
277
278   priv->have_layout = FALSE;
279   gtk_widget_queue_resize (GTK_WIDGET (menu));
280 }
281
282 static void
283 attach_info_free (AttachInfo *info)
284 {
285   g_slice_free (AttachInfo, info);
286 }
287
288 static AttachInfo *
289 get_attach_info (GtkWidget *child)
290 {
291   GObject *object = G_OBJECT (child);
292   AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
293
294   if (!ai)
295     {
296       ai = g_slice_new0 (AttachInfo);
297       g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
298                               (GDestroyNotify) attach_info_free);
299     }
300
301   return ai;
302 }
303
304 static gboolean
305 is_grid_attached (AttachInfo *ai)
306 {
307   return (ai->left_attach >= 0 &&
308           ai->right_attach >= 0 &&
309           ai->top_attach >= 0 &&
310           ai->bottom_attach >= 0);
311 }
312
313 static void
314 menu_ensure_layout (GtkMenu *menu)
315 {
316   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
317
318   if (!priv->have_layout)
319     {
320       GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
321       GList *l;
322       gchar *row_occupied;
323       gint current_row;
324       gint max_right_attach;      
325       gint max_bottom_attach;
326
327       /* Find extents of gridded portion
328        */
329       max_right_attach = 1;
330       max_bottom_attach = 0;
331
332       for (l = menu_shell->children; l; l = l->next)
333         {
334           GtkWidget *child = l->data;
335           AttachInfo *ai = get_attach_info (child);
336
337           if (is_grid_attached (ai))
338             {
339               max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
340               max_right_attach = MAX (max_right_attach, ai->right_attach);
341             }
342         }
343          
344       /* Find empty rows
345        */
346       row_occupied = g_malloc0 (max_bottom_attach);
347
348       for (l = menu_shell->children; l; l = l->next)
349         {
350           GtkWidget *child = l->data;
351           AttachInfo *ai = get_attach_info (child);
352
353           if (is_grid_attached (ai))
354             {
355               gint i;
356
357               for (i = ai->top_attach; i < ai->bottom_attach; i++)
358                 row_occupied[i] = TRUE;
359             }
360         }
361
362       /* Lay non-grid-items out in those rows
363        */
364       current_row = 0;
365       for (l = menu_shell->children; l; l = l->next)
366         {
367           GtkWidget *child = l->data;
368           AttachInfo *ai = get_attach_info (child);
369
370           if (!is_grid_attached (ai))
371             {
372               while (current_row < max_bottom_attach && row_occupied[current_row])
373                 current_row++;
374                 
375               ai->effective_left_attach = 0;
376               ai->effective_right_attach = max_right_attach;
377               ai->effective_top_attach = current_row;
378               ai->effective_bottom_attach = current_row + 1;
379
380               current_row++;
381             }
382           else
383             {
384               ai->effective_left_attach = ai->left_attach;
385               ai->effective_right_attach = ai->right_attach;
386               ai->effective_top_attach = ai->top_attach;
387               ai->effective_bottom_attach = ai->bottom_attach;
388             }
389         }
390
391       g_free (row_occupied);
392
393       priv->n_rows = MAX (current_row, max_bottom_attach);
394       priv->n_columns = max_right_attach;
395       priv->have_layout = TRUE;
396     }
397 }
398
399
400 static gint
401 gtk_menu_get_n_columns (GtkMenu *menu)
402 {
403   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
404
405   menu_ensure_layout (menu);
406
407   return priv->n_columns;
408 }
409
410 static gint
411 gtk_menu_get_n_rows (GtkMenu *menu)
412 {
413   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
414
415   menu_ensure_layout (menu);
416
417   return priv->n_rows;
418 }
419
420 static void
421 get_effective_child_attach (GtkWidget *child,
422                             int       *l,
423                             int       *r,
424                             int       *t,
425                             int       *b)
426 {
427   GtkMenu *menu = GTK_MENU (child->parent);
428   AttachInfo *ai;
429   
430   menu_ensure_layout (menu);
431
432   ai = get_attach_info (child);
433
434   if (l)
435     *l = ai->effective_left_attach;
436   if (r)
437     *r = ai->effective_right_attach;
438   if (t)
439     *t = ai->effective_top_attach;
440   if (b)
441     *b = ai->effective_bottom_attach;
442
443 }
444
445 static void
446 gtk_menu_class_init (GtkMenuClass *class)
447 {
448   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
449   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
450   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
451   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
452   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
453   GtkBindingSet *binding_set;
454   
455   gobject_class->set_property = gtk_menu_set_property;
456   gobject_class->get_property = gtk_menu_get_property;
457
458   object_class->destroy = gtk_menu_destroy;
459   
460   widget_class->realize = gtk_menu_realize;
461   widget_class->unrealize = gtk_menu_unrealize;
462   widget_class->size_request = gtk_menu_size_request;
463   widget_class->size_allocate = gtk_menu_size_allocate;
464   widget_class->show = gtk_menu_show;
465   widget_class->expose_event = gtk_menu_expose;
466   widget_class->scroll_event = gtk_menu_scroll;
467   widget_class->key_press_event = gtk_menu_key_press;
468   widget_class->button_press_event = gtk_menu_button_press;
469   widget_class->button_release_event = gtk_menu_button_release;
470   widget_class->motion_notify_event = gtk_menu_motion_notify;
471   widget_class->show_all = gtk_menu_show_all;
472   widget_class->hide_all = gtk_menu_hide_all;
473   widget_class->enter_notify_event = gtk_menu_enter_notify;
474   widget_class->leave_notify_event = gtk_menu_leave_notify;
475   widget_class->style_set = gtk_menu_style_set;
476   widget_class->focus = gtk_menu_focus;
477   widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
478   widget_class->grab_notify = gtk_menu_grab_notify;
479
480   container_class->remove = gtk_menu_remove;
481   container_class->get_child_property = gtk_menu_get_child_property;
482   container_class->set_child_property = gtk_menu_set_child_property;
483   
484   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
485   menu_shell_class->deactivate = gtk_menu_deactivate;
486   menu_shell_class->select_item = gtk_menu_select_item;
487   menu_shell_class->insert = gtk_menu_real_insert;
488   menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
489   menu_shell_class->move_current = gtk_menu_move_current;
490
491   menu_signals[MOVE_SCROLL] =
492     g_signal_new_class_handler (I_("move-scroll"),
493                                 G_OBJECT_CLASS_TYPE (object_class),
494                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
495                                 G_CALLBACK (gtk_menu_real_move_scroll),
496                                 NULL, NULL,
497                                 _gtk_marshal_VOID__ENUM,
498                                 G_TYPE_NONE, 1,
499                                 GTK_TYPE_SCROLL_TYPE);
500
501   /**
502    * GtkMenu:active:
503    *
504    * The index of the currently selected menu item, or -1 if no
505    * menu item is selected.
506    *
507    * Since: 2.14
508    **/
509   g_object_class_install_property (gobject_class,
510                                    PROP_ACTIVE,
511                                    g_param_spec_int ("active",
512                                                      P_("Active"),
513                                                      P_("The currently selected menu item"),
514                                                      -1, G_MAXINT, -1,
515                                                      GTK_PARAM_READWRITE));
516
517   /**
518    * GtkMenu:accel-group:
519    *
520    * The accel group holding accelerators for the menu.
521    *
522    * Since: 2.14
523    **/
524   g_object_class_install_property (gobject_class,
525                                    PROP_ACCEL_GROUP,
526                                    g_param_spec_object ("accel-group",
527                                                         P_("Accel Group"),
528                                                         P_("The accel group holding accelerators for the menu"),
529                                                         GTK_TYPE_ACCEL_GROUP,
530                                                         GTK_PARAM_READWRITE));
531
532   /**
533    * GtkMenu:accel-path:
534    *
535    * An accel path used to conveniently construct accel paths of child items.
536    *
537    * Since: 2.14
538    **/
539   g_object_class_install_property (gobject_class,
540                                    PROP_ACCEL_PATH,
541                                    g_param_spec_string ("accel-path",
542                                                         P_("Accel Path"),
543                                                         P_("An accel path used to conveniently construct accel paths of child items"),
544                                                         NULL,
545                                                         GTK_PARAM_READWRITE));
546
547   /**
548    * GtkMenu:attach-widget:
549    *
550    * The widget the menu is attached to. Setting this property attaches
551    * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
552    * use gtk_menu_attach_to_widget() directly.
553    *
554    * Since: 2.14
555    **/
556   g_object_class_install_property (gobject_class,
557                                    PROP_ATTACH_WIDGET,
558                                    g_param_spec_object ("attach-widget",
559                                                         P_("Attach Widget"),
560                                                         P_("The widget the menu is attached to"),
561                                                         GTK_TYPE_WIDGET,
562                                                         GTK_PARAM_READWRITE));
563
564   g_object_class_install_property (gobject_class,
565                                    PROP_TEAROFF_TITLE,
566                                    g_param_spec_string ("tearoff-title",
567                                                         P_("Tearoff Title"),
568                                                         P_("A title that may be displayed by the window manager when this menu is torn-off"),
569                                                         NULL,
570                                                         GTK_PARAM_READWRITE));
571
572   /**
573    * GtkMenu:tearoff-state:
574    *
575    * A boolean that indicates whether the menu is torn-off.
576    *
577    * Since: 2.6
578    **/
579   g_object_class_install_property (gobject_class,
580                                    PROP_TEAROFF_STATE,
581                                    g_param_spec_boolean ("tearoff-state",
582                                                          P_("Tearoff State"),
583                                                          P_("A boolean that indicates whether the menu is torn-off"),
584                                                          FALSE,
585                                                          GTK_PARAM_READWRITE));
586
587   /**
588    * GtkMenu:monitor:
589    *
590    * The monitor the menu will be popped up on.
591    *
592    * Since: 2.14
593    **/
594   g_object_class_install_property (gobject_class,
595                                    PROP_MONITOR,
596                                    g_param_spec_int ("monitor",
597                                                      P_("Monitor"),
598                                                      P_("The monitor the menu will be popped up on"),
599                                                      -1, G_MAXINT, -1,
600                                                      GTK_PARAM_READWRITE));
601
602   gtk_widget_class_install_style_property (widget_class,
603                                            g_param_spec_int ("vertical-padding",
604                                                              P_("Vertical Padding"),
605                                                              P_("Extra space at the top and bottom of the menu"),
606                                                              0,
607                                                              G_MAXINT,
608                                                              1,
609                                                              GTK_PARAM_READABLE));
610
611   /**
612    * GtkMenu:reserve-toggle-size:
613    *
614    * A boolean that indicates whether the menu reserves space for
615    * toggles and icons, regardless of their actual presence.
616    *
617    * This property should only be changed from its default value
618    * for special-purposes such as tabular menus. Regular menus that
619    * are connected to a menu bar or context menus should reserve
620    * toggle space for consistency.
621    *
622    * Since: 2.18
623    */
624   g_object_class_install_property (gobject_class,
625                                    PROP_RESERVE_TOGGLE_SIZE,
626                                    g_param_spec_boolean ("reserve-toggle-size",
627                                                          P_("Reserve Toggle Size"),
628                                                          P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
629                                                          TRUE,
630                                                          GTK_PARAM_READWRITE));
631
632   gtk_widget_class_install_style_property (widget_class,
633                                            g_param_spec_int ("horizontal-padding",
634                                                              P_("Horizontal Padding"),
635                                                              P_("Extra space at the left and right edges of the menu"),
636                                                              0,
637                                                              G_MAXINT,
638                                                              0,
639                                                              GTK_PARAM_READABLE));
640
641   gtk_widget_class_install_style_property (widget_class,
642                                            g_param_spec_int ("vertical-offset",
643                                                              P_("Vertical Offset"),
644                                                              P_("When the menu is a submenu, position it this number of pixels offset vertically"),
645                                                              G_MININT,
646                                                              G_MAXINT,
647                                                              0,
648                                                              GTK_PARAM_READABLE));
649
650   gtk_widget_class_install_style_property (widget_class,
651                                            g_param_spec_int ("horizontal-offset",
652                                                              P_("Horizontal Offset"),
653                                                              P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
654                                                              G_MININT,
655                                                              G_MAXINT,
656                                                              -2,
657                                                              GTK_PARAM_READABLE));
658
659   gtk_widget_class_install_style_property (widget_class,
660                                            g_param_spec_boolean ("double-arrows",
661                                                                  P_("Double Arrows"),
662                                                                  P_("When scrolling, always show both arrows."),
663                                                                  TRUE,
664                                                                  GTK_PARAM_READABLE));
665
666   /**
667    * GtkMenu:arrow-placement:
668    *
669    * Indicates where scroll arrows should be placed.
670    *
671    * Since: 2.16
672    **/
673   gtk_widget_class_install_style_property (widget_class,
674                                            g_param_spec_enum ("arrow-placement",
675                                                               P_("Arrow Placement"),
676                                                               P_("Indicates where scroll arrows should be placed"),
677                                                               GTK_TYPE_ARROW_PLACEMENT,
678                                                               GTK_ARROWS_BOTH,
679                                                               GTK_PARAM_READABLE));
680
681  gtk_container_class_install_child_property (container_class,
682                                              CHILD_PROP_LEFT_ATTACH,
683                                               g_param_spec_int ("left-attach",
684                                                                P_("Left Attach"),
685                                                                P_("The column number to attach the left side of the child to"),
686                                                                 -1, INT_MAX, -1,
687                                                                GTK_PARAM_READWRITE));
688
689  gtk_container_class_install_child_property (container_class,
690                                              CHILD_PROP_RIGHT_ATTACH,
691                                               g_param_spec_int ("right-attach",
692                                                                P_("Right Attach"),
693                                                                P_("The column number to attach the right side of the child to"),
694                                                                 -1, INT_MAX, -1,
695                                                                GTK_PARAM_READWRITE));
696
697  gtk_container_class_install_child_property (container_class,
698                                              CHILD_PROP_TOP_ATTACH,
699                                               g_param_spec_int ("top-attach",
700                                                                P_("Top Attach"),
701                                                                P_("The row number to attach the top of the child to"),
702                                                                 -1, INT_MAX, -1,
703                                                                GTK_PARAM_READWRITE));
704
705  gtk_container_class_install_child_property (container_class,
706                                              CHILD_PROP_BOTTOM_ATTACH,
707                                               g_param_spec_int ("bottom-attach",
708                                                                P_("Bottom Attach"),
709                                                                P_("The row number to attach the bottom of the child to"),
710                                                                 -1, INT_MAX, -1,
711                                                                GTK_PARAM_READWRITE));
712
713  /**
714   * GtkMenu::arrow-scaling
715   *
716   * Arbitrary constant to scale down the size of the scroll arrow.
717   *
718   * Since: 2.16
719   */
720   gtk_widget_class_install_style_property (widget_class,
721                                            g_param_spec_float ("arrow-scaling",
722                                                                P_("Arrow Scaling"),
723                                                                P_("Arbitrary constant to scale down the size of the scroll arrow"),
724                                                                0.0, 1.0, 0.7,
725                                                                GTK_PARAM_READABLE));
726
727   binding_set = gtk_binding_set_by_class (class);
728   gtk_binding_entry_add_signal (binding_set,
729                                 GDK_Up, 0,
730                                 I_("move-current"), 1,
731                                 GTK_TYPE_MENU_DIRECTION_TYPE,
732                                 GTK_MENU_DIR_PREV);
733   gtk_binding_entry_add_signal (binding_set,
734                                 GDK_KP_Up, 0,
735                                 "move-current", 1,
736                                 GTK_TYPE_MENU_DIRECTION_TYPE,
737                                 GTK_MENU_DIR_PREV);
738   gtk_binding_entry_add_signal (binding_set,
739                                 GDK_Down, 0,
740                                 "move-current", 1,
741                                 GTK_TYPE_MENU_DIRECTION_TYPE,
742                                 GTK_MENU_DIR_NEXT);
743   gtk_binding_entry_add_signal (binding_set,
744                                 GDK_KP_Down, 0,
745                                 "move-current", 1,
746                                 GTK_TYPE_MENU_DIRECTION_TYPE,
747                                 GTK_MENU_DIR_NEXT);
748   gtk_binding_entry_add_signal (binding_set,
749                                 GDK_Left, 0,
750                                 "move-current", 1,
751                                 GTK_TYPE_MENU_DIRECTION_TYPE,
752                                 GTK_MENU_DIR_PARENT);
753   gtk_binding_entry_add_signal (binding_set,
754                                 GDK_KP_Left, 0,
755                                 "move-current", 1,
756                                 GTK_TYPE_MENU_DIRECTION_TYPE,
757                                 GTK_MENU_DIR_PARENT);
758   gtk_binding_entry_add_signal (binding_set,
759                                 GDK_Right, 0,
760                                 "move-current", 1,
761                                 GTK_TYPE_MENU_DIRECTION_TYPE,
762                                 GTK_MENU_DIR_CHILD);
763   gtk_binding_entry_add_signal (binding_set,
764                                 GDK_KP_Right, 0,
765                                 "move-current", 1,
766                                 GTK_TYPE_MENU_DIRECTION_TYPE,
767                                 GTK_MENU_DIR_CHILD);
768   gtk_binding_entry_add_signal (binding_set,
769                                 GDK_Home, 0,
770                                 "move-scroll", 1,
771                                 GTK_TYPE_SCROLL_TYPE,
772                                 GTK_SCROLL_START);
773   gtk_binding_entry_add_signal (binding_set,
774                                 GDK_KP_Home, 0,
775                                 "move-scroll", 1,
776                                 GTK_TYPE_SCROLL_TYPE,
777                                 GTK_SCROLL_START);
778   gtk_binding_entry_add_signal (binding_set,
779                                 GDK_End, 0,
780                                 "move-scroll", 1,
781                                 GTK_TYPE_SCROLL_TYPE,
782                                 GTK_SCROLL_END);
783   gtk_binding_entry_add_signal (binding_set,
784                                 GDK_KP_End, 0,
785                                 "move-scroll", 1,
786                                 GTK_TYPE_SCROLL_TYPE,
787                                 GTK_SCROLL_END);
788   gtk_binding_entry_add_signal (binding_set,
789                                 GDK_Page_Up, 0,
790                                 "move-scroll", 1,
791                                 GTK_TYPE_SCROLL_TYPE,
792                                 GTK_SCROLL_PAGE_UP);
793   gtk_binding_entry_add_signal (binding_set,
794                                 GDK_KP_Page_Up, 0,
795                                 "move-scroll", 1,
796                                 GTK_TYPE_SCROLL_TYPE,
797                                 GTK_SCROLL_PAGE_UP);
798   gtk_binding_entry_add_signal (binding_set,
799                                 GDK_Page_Down, 0,
800                                 "move-scroll", 1,
801                                 GTK_TYPE_SCROLL_TYPE,
802                                 GTK_SCROLL_PAGE_DOWN);
803   gtk_binding_entry_add_signal (binding_set,
804                                 GDK_KP_Page_Down, 0,
805                                 "move-scroll", 1,
806                                 GTK_TYPE_SCROLL_TYPE,
807                                 GTK_SCROLL_PAGE_DOWN);
808
809   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
810                                                        P_("Can change accelerators"),
811                                                        P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
812                                                        FALSE,
813                                                        GTK_PARAM_READWRITE));
814
815   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
816                                                    P_("Delay before submenus appear"),
817                                                    P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
818                                                    0,
819                                                    G_MAXINT,
820                                                    DEFAULT_POPUP_DELAY,
821                                                    GTK_PARAM_READWRITE));
822
823   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
824                                                    P_("Delay before hiding a submenu"),
825                                                    P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
826                                                    0,
827                                                    G_MAXINT,
828                                                    DEFAULT_POPDOWN_DELAY,
829                                                    GTK_PARAM_READWRITE));
830
831   g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
832 }
833
834
835 static void
836 gtk_menu_set_property (GObject      *object,
837                        guint         prop_id,
838                        const GValue *value,
839                        GParamSpec   *pspec)
840 {
841   GtkMenu *menu = GTK_MENU (object);
842
843   switch (prop_id)
844     {
845     case PROP_ACTIVE:
846       gtk_menu_set_active (menu, g_value_get_int (value));
847       break;
848     case PROP_ACCEL_GROUP:
849       gtk_menu_set_accel_group (menu, g_value_get_object (value));
850       break;
851     case PROP_ACCEL_PATH:
852       gtk_menu_set_accel_path (menu, g_value_get_string (value));
853       break;
854     case PROP_ATTACH_WIDGET:
855       {
856         GtkWidget *widget;
857
858         widget = gtk_menu_get_attach_widget (menu);
859         if (widget)
860           gtk_menu_detach (menu);
861
862         widget = (GtkWidget*) g_value_get_object (value); 
863         if (widget)
864           gtk_menu_attach_to_widget (menu, widget, NULL);
865       }
866       break;
867     case PROP_TEAROFF_STATE:
868       gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
869       break;
870     case PROP_TEAROFF_TITLE:
871       gtk_menu_set_title (menu, g_value_get_string (value));
872       break;
873     case PROP_MONITOR:
874       gtk_menu_set_monitor (menu, g_value_get_int (value));
875       break;
876     case PROP_RESERVE_TOGGLE_SIZE:
877       gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
878       break;
879     default:
880       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
881       break;
882     }
883 }
884
885 static void
886 gtk_menu_get_property (GObject     *object,
887                        guint        prop_id,
888                        GValue      *value,
889                        GParamSpec  *pspec)
890 {
891   GtkMenu *menu = GTK_MENU (object);
892
893   switch (prop_id)
894     {
895     case PROP_ACTIVE:
896       g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
897       break;
898     case PROP_ACCEL_GROUP:
899       g_value_set_object (value, gtk_menu_get_accel_group (menu));
900       break;
901     case PROP_ACCEL_PATH:
902       g_value_set_string (value, gtk_menu_get_accel_path (menu));
903       break;
904     case PROP_ATTACH_WIDGET:
905       g_value_set_object (value, gtk_menu_get_attach_widget (menu));
906       break;
907     case PROP_TEAROFF_STATE:
908       g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
909       break;
910     case PROP_TEAROFF_TITLE:
911       g_value_set_string (value, gtk_menu_get_title (menu));
912       break;
913     case PROP_MONITOR:
914       g_value_set_int (value, gtk_menu_get_monitor (menu));
915       break;
916     case PROP_RESERVE_TOGGLE_SIZE:
917       g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
918       break;
919     default:
920       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
921       break;
922     }
923 }
924
925 static void
926 gtk_menu_set_child_property (GtkContainer *container,
927                              GtkWidget    *child,
928                              guint         property_id,
929                              const GValue *value,
930                              GParamSpec   *pspec)
931 {
932   GtkMenu *menu = GTK_MENU (container);
933   AttachInfo *ai = get_attach_info (child);
934
935   switch (property_id)
936     {
937     case CHILD_PROP_LEFT_ATTACH:
938       ai->left_attach = g_value_get_int (value);
939       break;
940     case CHILD_PROP_RIGHT_ATTACH:
941       ai->right_attach = g_value_get_int (value);
942       break;
943     case CHILD_PROP_TOP_ATTACH:
944       ai->top_attach = g_value_get_int (value); 
945       break;
946     case CHILD_PROP_BOTTOM_ATTACH:
947       ai->bottom_attach = g_value_get_int (value);
948       break;
949
950     default:
951       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
952       return;
953     }
954
955   menu_queue_resize (menu);
956 }
957
958 static void
959 gtk_menu_get_child_property (GtkContainer *container,
960                              GtkWidget    *child,
961                              guint         property_id,
962                              GValue       *value,
963                              GParamSpec   *pspec)
964 {
965   AttachInfo *ai = get_attach_info (child);
966
967   switch (property_id)
968     {
969     case CHILD_PROP_LEFT_ATTACH:
970       g_value_set_int (value, ai->left_attach);
971       break;
972     case CHILD_PROP_RIGHT_ATTACH:
973       g_value_set_int (value, ai->right_attach);
974       break;
975     case CHILD_PROP_TOP_ATTACH:
976       g_value_set_int (value, ai->top_attach);
977       break;
978     case CHILD_PROP_BOTTOM_ATTACH:
979       g_value_set_int (value, ai->bottom_attach);
980       break;
981       
982     default:
983       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
984       return;
985     }
986 }
987
988 static gboolean
989 gtk_menu_window_event (GtkWidget *window,
990                        GdkEvent  *event,
991                        GtkWidget *menu)
992 {
993   gboolean handled = FALSE;
994
995   g_object_ref (window);
996   g_object_ref (menu);
997
998   switch (event->type)
999     {
1000     case GDK_KEY_PRESS:
1001     case GDK_KEY_RELEASE:
1002       handled = gtk_widget_event (menu, event);
1003       break;
1004     default:
1005       break;
1006     }
1007
1008   g_object_unref (window);
1009   g_object_unref (menu);
1010
1011   return handled;
1012 }
1013
1014 static void
1015 gtk_menu_window_size_request (GtkWidget      *window,
1016                               GtkRequisition *requisition,
1017                               GtkMenu        *menu)
1018 {
1019   GtkMenuPrivate *private = gtk_menu_get_private (menu);
1020
1021   if (private->have_position)
1022     {
1023       GdkScreen *screen = gtk_widget_get_screen (window);
1024       GdkRectangle monitor;
1025       
1026       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
1027
1028       if (private->y + requisition->height > monitor.y + monitor.height)
1029         requisition->height = monitor.y + monitor.height - private->y;
1030
1031       if (private->y < monitor.y)
1032         requisition->height -= monitor.y - private->y;
1033     }
1034 }
1035
1036 static void
1037 gtk_menu_init (GtkMenu *menu)
1038 {
1039   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1040
1041   menu->parent_menu_item = NULL;
1042   menu->old_active_menu_item = NULL;
1043   menu->accel_group = NULL;
1044   menu->position_func = NULL;
1045   menu->position_func_data = NULL;
1046   menu->toggle_size = 0;
1047
1048   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1049                                                    "type", GTK_WINDOW_POPUP,
1050                                                    "child", menu,
1051                                                    NULL),
1052                                      "signal::event", gtk_menu_window_event, menu,
1053                                      "signal::size-request", gtk_menu_window_size_request, menu,
1054                                      "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1055                                      NULL);
1056   gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1057   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1058
1059   /* Refloat the menu, so that reference counting for the menu isn't
1060    * affected by it being a child of the toplevel
1061    */
1062   g_object_force_floating (G_OBJECT (menu));
1063   menu->needs_destruction_ref_count = TRUE;
1064
1065   menu->view_window = NULL;
1066   menu->bin_window = NULL;
1067
1068   menu->scroll_offset = 0;
1069   menu->scroll_step  = 0;
1070   menu->timeout_id = 0;
1071   menu->scroll_fast = FALSE;
1072   
1073   menu->tearoff_window = NULL;
1074   menu->tearoff_hbox = NULL;
1075   menu->torn_off = FALSE;
1076   menu->tearoff_active = FALSE;
1077   menu->tearoff_adjustment = NULL;
1078   menu->tearoff_scrollbar = NULL;
1079
1080   menu->upper_arrow_visible = FALSE;
1081   menu->lower_arrow_visible = FALSE;
1082   menu->upper_arrow_prelight = FALSE;
1083   menu->lower_arrow_prelight = FALSE;
1084
1085   priv->upper_arrow_state = GTK_STATE_NORMAL;
1086   priv->lower_arrow_state = GTK_STATE_NORMAL;
1087
1088   priv->have_layout = FALSE;
1089   priv->monitor_num = -1;
1090 }
1091
1092 static void
1093 gtk_menu_destroy (GtkObject *object)
1094 {
1095   GtkMenu *menu = GTK_MENU (object);
1096   GtkMenuAttachData *data;
1097   GtkMenuPrivate *priv; 
1098
1099   gtk_menu_remove_scroll_timeout (menu);
1100   
1101   data = g_object_get_data (G_OBJECT (object), attach_data_key);
1102   if (data)
1103     gtk_menu_detach (menu);
1104   
1105   gtk_menu_stop_navigating_submenu (menu);
1106
1107   if (menu->old_active_menu_item)
1108     {
1109       g_object_unref (menu->old_active_menu_item);
1110       menu->old_active_menu_item = NULL;
1111     }
1112
1113   /* Add back the reference count for being a child */
1114   if (menu->needs_destruction_ref_count)
1115     {
1116       menu->needs_destruction_ref_count = FALSE;
1117       g_object_ref (object);
1118     }
1119   
1120   if (menu->accel_group)
1121     {
1122       g_object_unref (menu->accel_group);
1123       menu->accel_group = NULL;
1124     }
1125
1126   if (menu->toplevel)
1127     gtk_widget_destroy (menu->toplevel);
1128
1129   if (menu->tearoff_window)
1130     gtk_widget_destroy (menu->tearoff_window);
1131
1132   priv = gtk_menu_get_private (menu);
1133
1134   if (priv->heights)
1135     {
1136       g_free (priv->heights);
1137       priv->heights = NULL;
1138     }
1139
1140   if (priv->title)
1141     {
1142       g_free (priv->title);
1143       priv->title = NULL;
1144     }
1145
1146   GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1147 }
1148
1149 static void
1150 menu_change_screen (GtkMenu   *menu,
1151                     GdkScreen *new_screen)
1152 {
1153   GtkMenuPrivate *private = gtk_menu_get_private (menu);
1154
1155   if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1156     {
1157       if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1158         return;
1159     }
1160
1161   if (menu->torn_off)
1162     {
1163       gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1164       gtk_menu_position (menu);
1165     }
1166
1167   gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1168   private->monitor_num = -1;
1169 }
1170
1171 static void
1172 attach_widget_screen_changed (GtkWidget *attach_widget,
1173                               GdkScreen *previous_screen,
1174                               GtkMenu   *menu)
1175 {
1176   if (gtk_widget_has_screen (attach_widget) &&
1177       !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1178     {
1179       menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1180     }
1181 }
1182
1183 void
1184 gtk_menu_attach_to_widget (GtkMenu             *menu,
1185                            GtkWidget           *attach_widget,
1186                            GtkMenuDetachFunc    detacher)
1187 {
1188   GtkMenuAttachData *data;
1189   GList *list;
1190   
1191   g_return_if_fail (GTK_IS_MENU (menu));
1192   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1193   
1194   /* keep this function in sync with gtk_widget_set_parent()
1195    */
1196   
1197   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1198   if (data)
1199     {
1200       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1201                  g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1202      return;
1203     }
1204   
1205   g_object_ref_sink (menu);
1206   
1207   data = g_slice_new (GtkMenuAttachData);
1208   data->attach_widget = attach_widget;
1209   
1210   g_signal_connect (attach_widget, "screen-changed",
1211                     G_CALLBACK (attach_widget_screen_changed), menu);
1212   attach_widget_screen_changed (attach_widget, NULL, menu);
1213   
1214   data->detacher = detacher;
1215   g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1216   list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1217   if (!g_list_find (list, menu))
1218     {
1219       list = g_list_prepend (list, menu);
1220     }
1221   g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1222                           (GDestroyNotify) g_list_free);
1223
1224   if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1225     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1226   
1227   /* we don't need to set the style here, since
1228    * we are a toplevel widget.
1229    */
1230
1231   /* Fallback title for menu comes from attach widget */
1232   gtk_menu_update_title (menu);
1233
1234   g_object_notify (G_OBJECT (menu), "attach-widget");
1235 }
1236
1237 GtkWidget*
1238 gtk_menu_get_attach_widget (GtkMenu *menu)
1239 {
1240   GtkMenuAttachData *data;
1241   
1242   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1243   
1244   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1245   if (data)
1246     return data->attach_widget;
1247   return NULL;
1248 }
1249
1250 void
1251 gtk_menu_detach (GtkMenu *menu)
1252 {
1253   GtkMenuAttachData *data;
1254   GList *list;
1255   
1256   g_return_if_fail (GTK_IS_MENU (menu));
1257   
1258   /* keep this function in sync with gtk_widget_unparent()
1259    */
1260   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1261   if (!data)
1262     {
1263       g_warning ("gtk_menu_detach(): menu is not attached");
1264       return;
1265     }
1266   g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1267   
1268   g_signal_handlers_disconnect_by_func (data->attach_widget,
1269                                         (gpointer) attach_widget_screen_changed,
1270                                         menu);
1271
1272   if (data->detacher)
1273     data->detacher (data->attach_widget, menu);
1274   list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1275   list = g_list_remove (list, menu);
1276   if (list)
1277     g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1278                             (GDestroyNotify) g_list_free);
1279   else
1280     g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1281   
1282   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1283     gtk_widget_unrealize (GTK_WIDGET (menu));
1284   
1285   g_slice_free (GtkMenuAttachData, data);
1286   
1287   /* Fallback title for menu comes from attach widget */
1288   gtk_menu_update_title (menu);
1289
1290   g_object_unref (menu);
1291 }
1292
1293 static void
1294 gtk_menu_remove (GtkContainer *container,
1295                  GtkWidget    *widget)
1296 {
1297   GtkMenu *menu = GTK_MENU (container);
1298
1299   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1300
1301   /* Clear out old_active_menu_item if it matches the item we are removing
1302    */
1303   if (menu->old_active_menu_item == widget)
1304     {
1305       g_object_unref (menu->old_active_menu_item);
1306       menu->old_active_menu_item = NULL;
1307     }
1308
1309   GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1310   g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1311
1312   menu_queue_resize (menu);
1313 }
1314
1315 GtkWidget*
1316 gtk_menu_new (void)
1317 {
1318   return g_object_new (GTK_TYPE_MENU, NULL);
1319 }
1320
1321 static void
1322 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1323                       GtkWidget    *child,
1324                       gint          position)
1325 {
1326   GtkMenu *menu = GTK_MENU (menu_shell);
1327   AttachInfo *ai = get_attach_info (child);
1328
1329   ai->left_attach = -1;
1330   ai->right_attach = -1;
1331   ai->top_attach = -1;
1332   ai->bottom_attach = -1;
1333
1334   if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1335     gtk_widget_set_parent_window (child, menu->bin_window);
1336
1337   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1338
1339   menu_queue_resize (menu);
1340 }
1341
1342 static void
1343 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1344 {
1345   GtkWidget *widget;
1346   gint width, height;
1347
1348   widget = GTK_WIDGET (menu);
1349
1350   if (menu->torn_off)
1351     {
1352       GdkPixmap *pixmap;
1353       GdkGC *gc;
1354       GdkGCValues gc_values;
1355
1356       menu->tearoff_active = FALSE;
1357       menu->saved_scroll_offset = menu->scroll_offset;
1358       
1359       gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1360       gc = gdk_gc_new_with_values (widget->window,
1361                                    &gc_values, GDK_GC_SUBWINDOW);
1362       
1363       gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1364       
1365       pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1366                                width,
1367                                height,
1368                                -1);
1369
1370       gdk_draw_drawable (pixmap, gc,
1371                          menu->tearoff_window->window,
1372                          0, 0, 0, 0, -1, -1);
1373       g_object_unref (gc);
1374
1375       gtk_widget_set_size_request (menu->tearoff_window,
1376                                    width,
1377                                    height);
1378
1379       gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1380       g_object_unref (pixmap);
1381     }
1382 }
1383
1384 static gboolean
1385 popup_grab_on_window (GdkWindow *window,
1386                       GdkDevice *keyboard,
1387                       GdkDevice *pointer,
1388                       guint32    activate_time)
1389 {
1390   if (keyboard &&
1391       gdk_device_grab (keyboard, window,
1392                        GDK_OWNERSHIP_WINDOW, TRUE,
1393                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1394                        NULL, activate_time) != GDK_GRAB_SUCCESS)
1395     return FALSE;
1396
1397   if (pointer &&
1398       gdk_device_grab (pointer, window,
1399                        GDK_OWNERSHIP_WINDOW, TRUE,
1400                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1401                        GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1402                        GDK_POINTER_MOTION_MASK,
1403                        NULL, activate_time) != GDK_GRAB_SUCCESS)
1404     {
1405       if (keyboard)
1406         gdk_device_ungrab (keyboard, activate_time);
1407
1408       return FALSE;
1409     }
1410
1411   return TRUE;
1412 }
1413
1414 /**
1415  * gtk_menu_popup_for_device:
1416  * @menu: a #GtkMenu.
1417  * @device: (allow-none): a #GdkDevice
1418  * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1419  * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1420  * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1421  * @data: (allow-none): user supplied data to be passed to @func.
1422  * @button: the mouse button which was pressed to initiate the event.
1423  * @activate_time: the time at which the activation event occurred.
1424  *
1425  * Displays a menu and makes it available for selection.  Applications can use
1426  * this function to display context-sensitive menus, and will typically supply
1427  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1428  * parameters. The default menu positioning function will position the menu
1429  * at the current position of @device (or its corresponding pointer).
1430  *
1431  * The @button parameter should be the mouse button pressed to initiate
1432  * the menu popup. If the menu popup was initiated by something other than
1433  * a mouse button press, such as a mouse button release or a keypress,
1434  * @button should be 0.
1435  *
1436  * The @activate_time parameter is used to conflict-resolve initiation of
1437  * concurrent requests for mouse/keyboard grab requests. To function
1438  * properly, this needs to be the time stamp of the user event (such as
1439  * a mouse click or key press) that caused the initiation of the popup.
1440  * Only if no such event is available, gtk_get_current_event_time() can
1441  * be used instead.
1442  *
1443  * Since: 3.0
1444  */
1445 void
1446 gtk_menu_popup_for_device (GtkMenu             *menu,
1447                            GdkDevice           *device,
1448                            GtkWidget           *parent_menu_shell,
1449                            GtkWidget           *parent_menu_item,
1450                            GtkMenuPositionFunc  func,
1451                            gpointer             data,
1452                            guint                button,
1453                            guint32              activate_time)
1454 {
1455   GtkWidget *widget;
1456   GtkWidget *xgrab_shell;
1457   GtkWidget *parent;
1458   GdkEvent *current_event;
1459   GtkMenuShell *menu_shell;
1460   gboolean grab_keyboard;
1461   GtkMenuPrivate *priv;
1462   GtkWidget *parent_toplevel;
1463   GdkDevice *keyboard, *pointer;
1464
1465   g_return_if_fail (GTK_IS_MENU (menu));
1466   g_return_if_fail (GDK_IS_DEVICE (device));
1467
1468   widget = GTK_WIDGET (menu);
1469   menu_shell = GTK_MENU_SHELL (menu);
1470   priv = gtk_menu_get_private (menu);
1471
1472   if (device->source == GDK_SOURCE_KEYBOARD)
1473     {
1474       keyboard = device;
1475       pointer = gdk_device_get_associated_device (device);
1476     }
1477   else
1478     {
1479       pointer = device;
1480       keyboard = gdk_device_get_associated_device (device);
1481     }
1482
1483   menu_shell->parent_menu_shell = parent_menu_shell;
1484
1485   priv->seen_item_enter = FALSE;
1486   
1487   /* Find the last viewable ancestor, and make an X grab on it
1488    */
1489   parent = GTK_WIDGET (menu);
1490   xgrab_shell = NULL;
1491   while (parent)
1492     {
1493       gboolean viewable = TRUE;
1494       GtkWidget *tmp = parent;
1495       
1496       while (tmp)
1497         {
1498           if (!gtk_widget_get_mapped (tmp))
1499             {
1500               viewable = FALSE;
1501               break;
1502             }
1503           tmp = tmp->parent;
1504         }
1505       
1506       if (viewable)
1507         xgrab_shell = parent;
1508       
1509       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1510     }
1511
1512   /* We want to receive events generated when we map the menu; unfortunately,
1513    * since there is probably already an implicit grab in place from the
1514    * button that the user used to pop up the menu, we won't receive then --
1515    * in particular, the EnterNotify when the menu pops up under the pointer.
1516    *
1517    * If we are grabbing on a parent menu shell, no problem; just grab on
1518    * that menu shell first before popping up the window with owner_events = TRUE.
1519    *
1520    * When grabbing on the menu itself, things get more convuluted - we
1521    * we do an explicit grab on a specially created window with
1522    * owner_events = TRUE, which we override further down with a grab
1523    * on the menu. (We can't grab on the menu until it is mapped; we
1524    * probably could just leave the grab on the other window, with a
1525    * little reorganization of the code in gtkmenu*).
1526    */
1527   grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1528   gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1529
1530   if (!grab_keyboard)
1531     keyboard = NULL;
1532
1533   if (xgrab_shell && xgrab_shell != widget)
1534     {
1535       if (popup_grab_on_window (xgrab_shell->window, keyboard, pointer, activate_time))
1536         {
1537           _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1538           GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1539         }
1540     }
1541   else
1542     {
1543       GdkWindow *transfer_window;
1544
1545       xgrab_shell = widget;
1546       transfer_window = menu_grab_transfer_window_get (menu);
1547       if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1548         {
1549           _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1550           GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1551         }
1552     }
1553
1554   if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1555     {
1556       /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1557        * with a stuck up window, we just abort here. Presumably the user will
1558        * try again.
1559        */
1560       menu_shell->parent_menu_shell = NULL;
1561       menu_grab_transfer_window_destroy (menu);
1562       return;
1563     }
1564
1565   _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1566   menu_shell->active = TRUE;
1567   menu_shell->button = button;
1568
1569   /* If we are popping up the menu from something other than, a button
1570    * press then, as a heuristic, we ignore enter events for the menu
1571    * until we get a MOTION_NOTIFY.  
1572    */
1573
1574   current_event = gtk_get_current_event ();
1575   if (current_event)
1576     {
1577       if ((current_event->type != GDK_BUTTON_PRESS) &&
1578           (current_event->type != GDK_ENTER_NOTIFY))
1579         menu_shell->ignore_enter = TRUE;
1580
1581       gdk_event_free (current_event);
1582     }
1583   else
1584     menu_shell->ignore_enter = TRUE;
1585
1586   if (menu->torn_off)
1587     {
1588       gtk_menu_tearoff_bg_copy (menu);
1589
1590       gtk_menu_reparent (menu, menu->toplevel, FALSE);
1591     }
1592
1593   parent_toplevel = NULL;
1594   if (parent_menu_shell) 
1595     parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1596   else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1597     {
1598       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1599       if (attach_widget)
1600         parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1601     }
1602
1603   /* Set transient for to get the right window group and parent relationship */
1604   if (GTK_IS_WINDOW (parent_toplevel))
1605     gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1606                                   GTK_WINDOW (parent_toplevel));
1607   
1608   menu->parent_menu_item = parent_menu_item;
1609   menu->position_func = func;
1610   menu->position_func_data = data;
1611   menu_shell->activate_time = activate_time;
1612
1613   /* We need to show the menu here rather in the init function because
1614    * code expects to be able to tell if the menu is onscreen by
1615    * looking at the gtk_widget_get_visible (menu)
1616    */
1617   gtk_widget_show (GTK_WIDGET (menu));
1618
1619   /* Position the menu, possibly changing the size request
1620    */
1621   gtk_menu_position (menu);
1622
1623   /* Compute the size of the toplevel and realize it so we
1624    * can scroll correctly.
1625    */
1626   {
1627     GtkRequisition tmp_request;
1628     GtkAllocation tmp_allocation = { 0, };
1629
1630     gtk_widget_size_request (menu->toplevel, &tmp_request);
1631     
1632     tmp_allocation.width = tmp_request.width;
1633     tmp_allocation.height = tmp_request.height;
1634
1635     gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1636     
1637     gtk_widget_realize (GTK_WIDGET (menu));
1638   }
1639
1640   gtk_menu_scroll_to (menu, menu->scroll_offset);
1641
1642   /* if no item is selected, select the first one */
1643   if (!menu_shell->active_menu_item)
1644     {
1645       gboolean touchscreen_mode;
1646
1647       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1648                     "gtk-touchscreen-mode", &touchscreen_mode,
1649                     NULL);
1650
1651       if (touchscreen_mode)
1652         gtk_menu_shell_select_first (menu_shell, TRUE);
1653     }
1654
1655   /* Once everything is set up correctly, map the toplevel window on
1656      the screen.
1657    */
1658   gtk_widget_show (menu->toplevel);
1659
1660   if (xgrab_shell == widget)
1661     popup_grab_on_window (widget->window, keyboard, pointer, activate_time); /* Should always succeed */
1662
1663   gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1664
1665   if (parent_menu_shell)
1666     {
1667       gboolean keyboard_mode;
1668
1669       keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1670       _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1671     }
1672   else if (menu_shell->button == 0) /* a keynav-activated context menu */
1673     _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1674
1675   _gtk_menu_shell_update_mnemonics (menu_shell);
1676 }
1677
1678 /**
1679  * gtk_menu_popup:
1680  * @menu: a #GtkMenu.
1681  * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1682  * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1683  * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1684  * @data: (allow-none): user supplied data to be passed to @func.
1685  * @button: the mouse button which was pressed to initiate the event.
1686  * @activate_time: the time at which the activation event occurred.
1687  *
1688  * Displays a menu and makes it available for selection.  Applications can use
1689  * this function to display context-sensitive menus, and will typically supply
1690  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
1691  * parameters. The default menu positioning function will position the menu
1692  * at the current mouse cursor position.
1693  *
1694  * The @button parameter should be the mouse button pressed to initiate
1695  * the menu popup. If the menu popup was initiated by something other than
1696  * a mouse button press, such as a mouse button release or a keypress,
1697  * @button should be 0.
1698  *
1699  * The @activate_time parameter is used to conflict-resolve initiation of
1700  * concurrent requests for mouse/keyboard grab requests. To function
1701  * properly, this needs to be the time stamp of the user event (such as
1702  * a mouse click or key press) that caused the initiation of the popup.
1703  * Only if no such event is available, gtk_get_current_event_time() can
1704  * be used instead.
1705  */
1706 void
1707 gtk_menu_popup (GtkMenu             *menu,
1708                 GtkWidget           *parent_menu_shell,
1709                 GtkWidget           *parent_menu_item,
1710                 GtkMenuPositionFunc  func,
1711                 gpointer             data,
1712                 guint                button,
1713                 guint32              activate_time)
1714 {
1715   GdkDevice *device;
1716
1717   g_return_if_fail (GTK_IS_MENU (menu));
1718
1719   device = gtk_get_current_event_device ();
1720
1721   if (!device)
1722     {
1723       GdkDisplay *display;
1724       GdkDeviceManager *device_manager;
1725       GList *devices;
1726
1727       display = gtk_widget_get_display (GTK_WIDGET (menu));
1728       device_manager = gdk_display_get_device_manager (display);
1729       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1730
1731       device = devices->data;
1732
1733       g_list_free (devices);
1734     }
1735
1736   gtk_menu_popup_for_device (menu, device,
1737                              parent_menu_shell,
1738                              parent_menu_item,
1739                              func, data,
1740                              button, activate_time);
1741 }
1742
1743 void
1744 gtk_menu_popdown (GtkMenu *menu)
1745 {
1746   GtkMenuPrivate *private;
1747   GtkMenuShell *menu_shell;
1748   GdkDevice *pointer;
1749
1750   g_return_if_fail (GTK_IS_MENU (menu));
1751   
1752   menu_shell = GTK_MENU_SHELL (menu);
1753   private = gtk_menu_get_private (menu);
1754
1755   menu_shell->parent_menu_shell = NULL;
1756   menu_shell->active = FALSE;
1757   menu_shell->ignore_enter = FALSE;
1758
1759   private->have_position = FALSE;
1760
1761   gtk_menu_stop_scrolling (menu);
1762   
1763   gtk_menu_stop_navigating_submenu (menu);
1764   
1765   if (menu_shell->active_menu_item)
1766     {
1767       if (menu->old_active_menu_item)
1768         g_object_unref (menu->old_active_menu_item);
1769       menu->old_active_menu_item = menu_shell->active_menu_item;
1770       g_object_ref (menu->old_active_menu_item);
1771     }
1772
1773   gtk_menu_shell_deselect (menu_shell);
1774   
1775   /* The X Grab, if present, will automatically be removed when we hide
1776    * the window */
1777   gtk_widget_hide (menu->toplevel);
1778   gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1779
1780   pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1781
1782   if (menu->torn_off)
1783     {
1784       gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1785       
1786       if (gtk_bin_get_child (GTK_BIN (menu->toplevel))) 
1787         {
1788           gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1789         } 
1790       else
1791         {
1792           /* We popped up the menu from the tearoff, so we need to
1793            * release the grab - we aren't actually hiding the menu.
1794            */
1795           if (menu_shell->have_xgrab && pointer)
1796             {
1797               GdkDevice *keyboard;
1798
1799               gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1800               keyboard = gdk_device_get_associated_device (pointer);
1801
1802               if (keyboard)
1803                 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1804             }
1805         }
1806
1807       /* gtk_menu_popdown is called each time a menu item is selected from
1808        * a torn off menu. Only scroll back to the saved position if the
1809        * non-tearoff menu was popped down.
1810        */
1811       if (!menu->tearoff_active)
1812         gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1813       menu->tearoff_active = TRUE;
1814     }
1815   else
1816     gtk_widget_hide (GTK_WIDGET (menu));
1817
1818   menu_shell->have_xgrab = FALSE;
1819
1820   if (pointer)
1821     gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1822
1823   _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1824
1825   menu_grab_transfer_window_destroy (menu);
1826 }
1827
1828 GtkWidget*
1829 gtk_menu_get_active (GtkMenu *menu)
1830 {
1831   GtkWidget *child;
1832   GList *children;
1833   
1834   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1835   
1836   if (!menu->old_active_menu_item)
1837     {
1838       child = NULL;
1839       children = GTK_MENU_SHELL (menu)->children;
1840       
1841       while (children)
1842         {
1843           child = children->data;
1844           children = children->next;
1845           
1846           if (gtk_bin_get_child (GTK_BIN (child)))
1847             break;
1848           child = NULL;
1849         }
1850       
1851       menu->old_active_menu_item = child;
1852       if (menu->old_active_menu_item)
1853         g_object_ref (menu->old_active_menu_item);
1854     }
1855   
1856   return menu->old_active_menu_item;
1857 }
1858
1859 void
1860 gtk_menu_set_active (GtkMenu *menu,
1861                      guint    index)
1862 {
1863   GtkWidget *child;
1864   GList *tmp_list;
1865   
1866   g_return_if_fail (GTK_IS_MENU (menu));
1867   
1868   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1869   if (tmp_list)
1870     {
1871       child = tmp_list->data;
1872       if (gtk_bin_get_child (GTK_BIN (child)))
1873         {
1874           if (menu->old_active_menu_item)
1875             g_object_unref (menu->old_active_menu_item);
1876           menu->old_active_menu_item = child;
1877           g_object_ref (menu->old_active_menu_item);
1878         }
1879     }
1880 }
1881
1882
1883 /**
1884  * gtk_menu_set_accel_group:
1885  * @accel_group: (allow-none):
1886  */
1887 void
1888 gtk_menu_set_accel_group (GtkMenu       *menu,
1889                           GtkAccelGroup *accel_group)
1890 {
1891   g_return_if_fail (GTK_IS_MENU (menu));
1892   
1893   if (menu->accel_group != accel_group)
1894     {
1895       if (menu->accel_group)
1896         g_object_unref (menu->accel_group);
1897       menu->accel_group = accel_group;
1898       if (menu->accel_group)
1899         g_object_ref (menu->accel_group);
1900       _gtk_menu_refresh_accel_paths (menu, TRUE);
1901     }
1902 }
1903
1904 GtkAccelGroup*
1905 gtk_menu_get_accel_group (GtkMenu *menu)
1906 {
1907   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1908
1909   return menu->accel_group;
1910 }
1911
1912 static gboolean
1913 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1914                                   guint      signal_id)
1915 {
1916   /* Menu items chain here to figure whether they can activate their
1917    * accelerators.  Unlike ordinary widgets, menus allow accel
1918    * activation even if invisible since that's the usual case for
1919    * submenus/popup-menus. however, the state of the attach widget
1920    * affects the "activeness" of the menu.
1921    */
1922   GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1923
1924   if (awidget)
1925     return gtk_widget_can_activate_accel (awidget, signal_id);
1926   else
1927     return gtk_widget_is_sensitive (widget);
1928 }
1929
1930 /**
1931  * gtk_menu_set_accel_path
1932  * @menu:       a valid #GtkMenu
1933  * @accel_path: (allow-none): a valid accelerator path
1934  *
1935  * Sets an accelerator path for this menu from which accelerator paths
1936  * for its immediate children, its menu items, can be constructed.
1937  * The main purpose of this function is to spare the programmer the
1938  * inconvenience of having to call gtk_menu_item_set_accel_path() on
1939  * each menu item that should support runtime user changable accelerators.
1940  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1941  * each menu item of this menu, that contains a label describing its purpose,
1942  * automatically gets an accel path assigned. For example, a menu containing
1943  * menu items "New" and "Exit", will, after 
1944  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
1945  * has been called, assign its items the accel paths:
1946  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
1947  * Assigning accel paths to menu items then enables the user to change
1948  * their accelerators at runtime. More details about accelerator paths
1949  * and their default setups can be found at gtk_accel_map_add_entry().
1950  * 
1951  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1952  * pass a static string, you can save some memory by interning it first with 
1953  * g_intern_static_string().
1954  */
1955 void
1956 gtk_menu_set_accel_path (GtkMenu     *menu,
1957                          const gchar *accel_path)
1958 {
1959   g_return_if_fail (GTK_IS_MENU (menu));
1960   if (accel_path)
1961     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1962
1963   /* FIXME: accel_path should be defined as const gchar* */
1964   menu->accel_path = (gchar*)g_intern_string (accel_path);
1965   if (menu->accel_path)
1966     _gtk_menu_refresh_accel_paths (menu, FALSE);
1967 }
1968
1969 /**
1970  * gtk_menu_get_accel_path
1971  * @menu: a valid #GtkMenu
1972  *
1973  * Retrieves the accelerator path set on the menu.
1974  *
1975  * Returns: the accelerator path set on the menu.
1976  *
1977  * Since: 2.14
1978  */
1979 const gchar*
1980 gtk_menu_get_accel_path (GtkMenu *menu)
1981 {
1982   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1983
1984   return menu->accel_path;
1985 }
1986
1987 typedef struct {
1988   GtkMenu *menu;
1989   gboolean group_changed;
1990 } AccelPropagation;
1991
1992 static void
1993 refresh_accel_paths_foreach (GtkWidget *widget,
1994                              gpointer   data)
1995 {
1996   AccelPropagation *prop = data;
1997
1998   if (GTK_IS_MENU_ITEM (widget))        /* should always be true */
1999     _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2000                                        prop->menu->accel_path,
2001                                        prop->menu->accel_group,
2002                                        prop->group_changed);
2003 }
2004
2005 static void
2006 _gtk_menu_refresh_accel_paths (GtkMenu  *menu,
2007                                gboolean  group_changed)
2008 {
2009   g_return_if_fail (GTK_IS_MENU (menu));
2010
2011   if (menu->accel_path && menu->accel_group)
2012     {
2013       AccelPropagation prop;
2014
2015       prop.menu = menu;
2016       prop.group_changed = group_changed;
2017       gtk_container_foreach (GTK_CONTAINER (menu),
2018                              refresh_accel_paths_foreach,
2019                              &prop);
2020     }
2021 }
2022
2023 void
2024 gtk_menu_reposition (GtkMenu *menu)
2025 {
2026   g_return_if_fail (GTK_IS_MENU (menu));
2027
2028   if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2029     gtk_menu_position (menu);
2030 }
2031
2032 static void
2033 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2034                             GtkMenu       *menu)
2035 {
2036   g_return_if_fail (GTK_IS_MENU (menu));
2037
2038   if (adjustment->value != menu->scroll_offset)
2039     gtk_menu_scroll_to (menu, adjustment->value);
2040 }
2041
2042 static void
2043 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2044                             gint     width)
2045 {
2046   GdkGeometry geometry_hints;
2047   
2048   if (!menu->tearoff_window)
2049     return;
2050
2051   if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2052     {
2053       gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
2054       width += menu->tearoff_scrollbar->requisition.width;
2055     }
2056
2057   geometry_hints.min_width = width;
2058   geometry_hints.max_width = width;
2059     
2060   geometry_hints.min_height = 0;
2061   geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
2062
2063   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2064                                  NULL,
2065                                  &geometry_hints,
2066                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2067 }
2068
2069 static void
2070 gtk_menu_update_title (GtkMenu *menu)
2071 {
2072   if (menu->tearoff_window)
2073     {
2074       const gchar *title;
2075       GtkWidget *attach_widget;
2076
2077       title = gtk_menu_get_title (menu);
2078       if (!title)
2079         {
2080           attach_widget = gtk_menu_get_attach_widget (menu);
2081           if (GTK_IS_MENU_ITEM (attach_widget))
2082             {
2083               GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2084               if (GTK_IS_LABEL (child))
2085                 title = gtk_label_get_text (GTK_LABEL (child));
2086             }
2087         }
2088       
2089       if (title)
2090         gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2091     }
2092 }
2093
2094 static GtkWidget*
2095 gtk_menu_get_toplevel (GtkWidget *menu)
2096 {
2097   GtkWidget *attach, *toplevel;
2098
2099   attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2100
2101   if (GTK_IS_MENU_ITEM (attach))
2102     attach = attach->parent;
2103
2104   if (GTK_IS_MENU (attach))
2105     return gtk_menu_get_toplevel (attach);
2106   else if (GTK_IS_WIDGET (attach))
2107     {
2108       toplevel = gtk_widget_get_toplevel (attach);
2109       if (gtk_widget_is_toplevel (toplevel)) 
2110         return toplevel;
2111     }
2112
2113   return NULL;
2114 }
2115
2116 static void
2117 tearoff_window_destroyed (GtkWidget *widget,
2118                           GtkMenu   *menu)
2119 {
2120   gtk_menu_set_tearoff_state (menu, FALSE);
2121 }
2122
2123 void       
2124 gtk_menu_set_tearoff_state (GtkMenu  *menu,
2125                             gboolean  torn_off)
2126 {
2127   gint width, height;
2128   
2129   g_return_if_fail (GTK_IS_MENU (menu));
2130
2131   if (menu->torn_off != torn_off)
2132     {
2133       menu->torn_off = torn_off;
2134       menu->tearoff_active = torn_off;
2135       
2136       if (menu->torn_off)
2137         {
2138           if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2139             gtk_menu_popdown (menu);
2140
2141           if (!menu->tearoff_window)
2142             {
2143               GtkWidget *toplevel;
2144
2145               menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2146                                                      "type", GTK_WINDOW_TOPLEVEL,
2147                                                      "screen", gtk_widget_get_screen (menu->toplevel),
2148                                                      "app-paintable", TRUE,
2149                                                      NULL);
2150
2151               gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2152                                         GDK_WINDOW_TYPE_HINT_MENU);
2153               gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2154               g_signal_connect (menu->tearoff_window, "destroy",
2155                                 G_CALLBACK (tearoff_window_destroyed), menu);
2156               g_signal_connect (menu->tearoff_window, "event",
2157                                 G_CALLBACK (gtk_menu_window_event), menu);
2158
2159               gtk_menu_update_title (menu);
2160
2161               gtk_widget_realize (menu->tearoff_window);
2162
2163               toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2164               if (toplevel != NULL)
2165                 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2166                                               GTK_WINDOW (toplevel));
2167               
2168               menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2169               gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2170
2171               gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2172               menu->tearoff_adjustment =
2173                 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2174                                                     0,
2175                                                     GTK_WIDGET (menu)->requisition.height,
2176                                                     MENU_SCROLL_STEP2,
2177                                                     height/2,
2178                                                     height));
2179               g_object_connect (menu->tearoff_adjustment,
2180                                 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2181                                 NULL);
2182               menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2183
2184               gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2185                                 menu->tearoff_scrollbar,
2186                                 FALSE, FALSE, 0);
2187               
2188               if (menu->tearoff_adjustment->upper > height)
2189                 gtk_widget_show (menu->tearoff_scrollbar);
2190               
2191               gtk_widget_show (menu->tearoff_hbox);
2192             }
2193           
2194           gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2195
2196           gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2197
2198           /* Update menu->requisition
2199            */
2200           gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2201   
2202           gtk_menu_set_tearoff_hints (menu, width);
2203             
2204           gtk_widget_realize (menu->tearoff_window);
2205           gtk_menu_position (menu);
2206           
2207           gtk_widget_show (GTK_WIDGET (menu));
2208           gtk_widget_show (menu->tearoff_window);
2209
2210           gtk_menu_scroll_to (menu, 0);
2211
2212         }
2213       else
2214         {
2215           gtk_widget_hide (GTK_WIDGET (menu));
2216           gtk_widget_hide (menu->tearoff_window);
2217           if (GTK_IS_CONTAINER (menu->toplevel))
2218             gtk_menu_reparent (menu, menu->toplevel, FALSE);
2219           gtk_widget_destroy (menu->tearoff_window);
2220           
2221           menu->tearoff_window = NULL;
2222           menu->tearoff_hbox = NULL;
2223           menu->tearoff_scrollbar = NULL;
2224           menu->tearoff_adjustment = NULL;
2225         }
2226
2227       g_object_notify (G_OBJECT (menu), "tearoff-state");
2228     }
2229 }
2230
2231 /**
2232  * gtk_menu_get_tearoff_state:
2233  * @menu: a #GtkMenu
2234  *
2235  * Returns whether the menu is torn off. See
2236  * gtk_menu_set_tearoff_state ().
2237  *
2238  * Return value: %TRUE if the menu is currently torn off.
2239  **/
2240 gboolean
2241 gtk_menu_get_tearoff_state (GtkMenu *menu)
2242 {
2243   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2244
2245   return menu->torn_off;
2246 }
2247
2248 /**
2249  * gtk_menu_set_title:
2250  * @menu: a #GtkMenu
2251  * @title: a string containing the title for the menu.
2252  * 
2253  * Sets the title string for the menu.  The title is displayed when the menu
2254  * is shown as a tearoff menu.  If @title is %NULL, the menu will see if it is
2255  * attached to a parent menu item, and if so it will try to use the same text as
2256  * that menu item's label.
2257  **/
2258 void
2259 gtk_menu_set_title (GtkMenu     *menu,
2260                     const gchar *title)
2261 {
2262   GtkMenuPrivate *priv;
2263   char *old_title;
2264
2265   g_return_if_fail (GTK_IS_MENU (menu));
2266
2267   priv = gtk_menu_get_private (menu);
2268
2269   old_title = priv->title;
2270   priv->title = g_strdup (title);
2271   g_free (old_title);
2272        
2273   gtk_menu_update_title (menu);
2274   g_object_notify (G_OBJECT (menu), "tearoff-title");
2275 }
2276
2277 /**
2278  * gtk_menu_get_title:
2279  * @menu: a #GtkMenu
2280  *
2281  * Returns the title of the menu. See gtk_menu_set_title().
2282  *
2283  * Return value: the title of the menu, or %NULL if the menu has no
2284  * title set on it. This string is owned by the widget and should
2285  * not be modified or freed.
2286  **/
2287 G_CONST_RETURN gchar *
2288 gtk_menu_get_title (GtkMenu *menu)
2289 {
2290   GtkMenuPrivate *priv;
2291
2292   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2293
2294   priv = gtk_menu_get_private (menu);
2295
2296   return priv->title;
2297 }
2298
2299 void
2300 gtk_menu_reorder_child (GtkMenu   *menu,
2301                         GtkWidget *child,
2302                         gint       position)
2303 {
2304   GtkMenuShell *menu_shell;
2305
2306   g_return_if_fail (GTK_IS_MENU (menu));
2307   g_return_if_fail (GTK_IS_MENU_ITEM (child));
2308
2309   menu_shell = GTK_MENU_SHELL (menu);
2310
2311   if (g_list_find (menu_shell->children, child))
2312     {   
2313       menu_shell->children = g_list_remove (menu_shell->children, child);
2314       menu_shell->children = g_list_insert (menu_shell->children, child, position);
2315
2316       menu_queue_resize (menu);
2317     }   
2318 }
2319
2320 static void
2321 gtk_menu_style_set (GtkWidget *widget,
2322                     GtkStyle  *previous_style)
2323 {
2324   if (gtk_widget_get_realized (widget))
2325     {
2326       GtkMenu *menu = GTK_MENU (widget);
2327       
2328       gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2329       gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2330       gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2331     }
2332 }
2333
2334 static void
2335 get_arrows_border (GtkMenu   *menu,
2336                    GtkBorder *border)
2337 {
2338   guint scroll_arrow_height;
2339   GtkArrowPlacement arrow_placement;
2340
2341   gtk_widget_style_get (GTK_WIDGET (menu),
2342                         "scroll-arrow-vlength", &scroll_arrow_height,
2343                         "arrow_placement", &arrow_placement,
2344                         NULL);
2345
2346   switch (arrow_placement)
2347     {
2348     case GTK_ARROWS_BOTH:
2349       border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2350       border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2351       break;
2352
2353     case GTK_ARROWS_START:
2354       border->top = (menu->upper_arrow_visible ||
2355                      menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2356       border->bottom = 0;
2357       break;
2358
2359     case GTK_ARROWS_END:
2360       border->top = 0;
2361       border->bottom = (menu->upper_arrow_visible ||
2362                         menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2363       break;
2364     }
2365
2366   border->left = border->right = 0;
2367 }
2368
2369 static void
2370 gtk_menu_realize (GtkWidget *widget)
2371 {
2372   GdkWindowAttr attributes;
2373   gint attributes_mask;
2374   gint border_width;
2375   GtkMenu *menu;
2376   GtkWidget *child;
2377   GList *children;
2378   guint vertical_padding;
2379   guint horizontal_padding;
2380   GtkBorder arrow_border;
2381
2382   g_return_if_fail (GTK_IS_MENU (widget));
2383
2384   menu = GTK_MENU (widget);
2385   
2386   gtk_widget_set_realized (widget, TRUE);
2387   
2388   attributes.window_type = GDK_WINDOW_CHILD;
2389   attributes.x = widget->allocation.x;
2390   attributes.y = widget->allocation.y;
2391   attributes.width = widget->allocation.width;
2392   attributes.height = widget->allocation.height;
2393   attributes.wclass = GDK_INPUT_OUTPUT;
2394   attributes.visual = gtk_widget_get_visual (widget);
2395   attributes.colormap = gtk_widget_get_colormap (widget);
2396   
2397   attributes.event_mask = gtk_widget_get_events (widget);
2398
2399   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2400                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2401   
2402   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2403   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2404   gdk_window_set_user_data (widget->window, widget);
2405   
2406   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2407
2408   gtk_widget_style_get (GTK_WIDGET (menu),
2409                         "vertical-padding", &vertical_padding,
2410                         "horizontal-padding", &horizontal_padding,
2411                         NULL);
2412
2413   attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2414   attributes.y = border_width + widget->style->ythickness + vertical_padding;
2415   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2416   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2417
2418   get_arrows_border (menu, &arrow_border);
2419   attributes.y += arrow_border.top;
2420   attributes.height -= arrow_border.top;
2421   attributes.height -= arrow_border.bottom;
2422
2423   menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2424   gdk_window_set_user_data (menu->view_window, menu);
2425
2426   attributes.x = 0;
2427   attributes.y = 0;
2428   attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2429   attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2430   
2431   menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2432   gdk_window_set_user_data (menu->bin_window, menu);
2433
2434   children = GTK_MENU_SHELL (menu)->children;
2435   while (children)
2436     {
2437       child = children->data;
2438       children = children->next;
2439           
2440       gtk_widget_set_parent_window (child, menu->bin_window);
2441     }
2442   
2443   widget->style = gtk_style_attach (widget->style, widget->window);
2444   gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2445   gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2446   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2447
2448   if (GTK_MENU_SHELL (widget)->active_menu_item)
2449     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2450                                   GTK_MENU_SHELL (widget)->active_menu_item);
2451
2452   gdk_window_show (menu->bin_window);
2453   gdk_window_show (menu->view_window);
2454 }
2455
2456 static gboolean 
2457 gtk_menu_focus (GtkWidget       *widget,
2458                 GtkDirectionType direction)
2459 {
2460   /*
2461    * A menu or its menu items cannot have focus
2462    */
2463   return FALSE;
2464 }
2465
2466 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2467  */
2468 static GdkWindow *
2469 menu_grab_transfer_window_get (GtkMenu *menu)
2470 {
2471   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2472   if (!window)
2473     {
2474       GdkWindowAttr attributes;
2475       gint attributes_mask;
2476       
2477       attributes.x = -100;
2478       attributes.y = -100;
2479       attributes.width = 10;
2480       attributes.height = 10;
2481       attributes.window_type = GDK_WINDOW_TEMP;
2482       attributes.wclass = GDK_INPUT_ONLY;
2483       attributes.override_redirect = TRUE;
2484       attributes.event_mask = 0;
2485
2486       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2487       
2488       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2489                                &attributes, attributes_mask);
2490       gdk_window_set_user_data (window, menu);
2491
2492       gdk_window_show (window);
2493
2494       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2495     }
2496
2497   return window;
2498 }
2499
2500 static void
2501 menu_grab_transfer_window_destroy (GtkMenu *menu)
2502 {
2503   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2504   if (window)
2505     {
2506       gdk_window_set_user_data (window, NULL);
2507       gdk_window_destroy (window);
2508       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2509     }
2510 }
2511
2512 static void
2513 gtk_menu_unrealize (GtkWidget *widget)
2514 {
2515   GtkMenu *menu = GTK_MENU (widget);
2516
2517   menu_grab_transfer_window_destroy (menu);
2518
2519   gdk_window_set_user_data (menu->view_window, NULL);
2520   gdk_window_destroy (menu->view_window);
2521   menu->view_window = NULL;
2522
2523   gdk_window_set_user_data (menu->bin_window, NULL);
2524   gdk_window_destroy (menu->bin_window);
2525   menu->bin_window = NULL;
2526
2527   GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2528 }
2529
2530 static void
2531 gtk_menu_size_request (GtkWidget      *widget,
2532                        GtkRequisition *requisition)
2533 {
2534   gint i;
2535   GtkMenu *menu;
2536   GtkMenuShell *menu_shell;
2537   GtkWidget *child;
2538   GList *children;
2539   guint max_toggle_size;
2540   guint max_accel_width;
2541   guint vertical_padding;
2542   guint horizontal_padding;
2543   guint border_width;
2544   GtkRequisition child_requisition;
2545   GtkMenuPrivate *priv;
2546   
2547   g_return_if_fail (GTK_IS_MENU (widget));
2548   g_return_if_fail (requisition != NULL);
2549   
2550   menu = GTK_MENU (widget);
2551   menu_shell = GTK_MENU_SHELL (widget);
2552   priv = gtk_menu_get_private (menu);
2553   
2554   requisition->width = 0;
2555   requisition->height = 0;
2556   
2557   max_toggle_size = 0;
2558   max_accel_width = 0;
2559   
2560   g_free (priv->heights);
2561   priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2562   priv->heights_length = gtk_menu_get_n_rows (menu);
2563
2564   children = menu_shell->children;
2565   while (children)
2566     {
2567       gint part;
2568       gint toggle_size;
2569       gint l, r, t, b;
2570
2571       child = children->data;
2572       children = children->next;
2573       
2574       if (! gtk_widget_get_visible (child))
2575         continue;
2576
2577       get_effective_child_attach (child, &l, &r, &t, &b);
2578
2579       /* It's important to size_request the child
2580        * before doing the toggle size request, in
2581        * case the toggle size request depends on the size
2582        * request of a child of the child (e.g. for ImageMenuItem)
2583        */
2584
2585        GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2586        gtk_widget_size_request (child, &child_requisition);
2587
2588        gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2589        max_toggle_size = MAX (max_toggle_size, toggle_size);
2590        max_accel_width = MAX (max_accel_width,
2591                               GTK_MENU_ITEM (child)->accelerator_width);
2592
2593        part = child_requisition.width / (r - l);
2594        requisition->width = MAX (requisition->width, part);
2595
2596        part = MAX (child_requisition.height, toggle_size) / (b - t);
2597        priv->heights[t] = MAX (priv->heights[t], part);
2598     }
2599
2600   /* If the menu doesn't include any images or check items
2601    * reserve the space so that all menus are consistent.
2602    * We only do this for 'ordinary' menus, not for combobox
2603    * menus or multi-column menus
2604    */
2605   if (max_toggle_size == 0 && 
2606       gtk_menu_get_n_columns (menu) == 1 &&
2607       !priv->no_toggle_size)
2608     {
2609       guint toggle_spacing;
2610       guint indicator_size;
2611
2612       gtk_style_get (widget->style,
2613                      GTK_TYPE_CHECK_MENU_ITEM,
2614                      "toggle-spacing", &toggle_spacing,
2615                      "indicator-size", &indicator_size,
2616                      NULL);
2617
2618       max_toggle_size = indicator_size + toggle_spacing;
2619     }
2620
2621   for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2622     requisition->height += priv->heights[i];
2623
2624   requisition->width += 2 * max_toggle_size + max_accel_width;
2625   requisition->width *= gtk_menu_get_n_columns (menu);
2626
2627   gtk_widget_style_get (GTK_WIDGET (menu),
2628                         "vertical-padding", &vertical_padding,
2629                         "horizontal-padding", &horizontal_padding,
2630                         NULL);
2631
2632   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2633   requisition->width += (border_width + horizontal_padding + widget->style->xthickness) * 2;
2634   requisition->height += (border_width + vertical_padding + widget->style->ythickness) * 2;
2635   
2636   menu->toggle_size = max_toggle_size;
2637
2638   /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2639    */
2640   if (menu->tearoff_active)
2641     gtk_menu_set_tearoff_hints (menu, requisition->width);
2642 }
2643
2644 static void
2645 gtk_menu_size_allocate (GtkWidget     *widget,
2646                         GtkAllocation *allocation)
2647 {
2648   GtkMenu *menu;
2649   GtkMenuShell *menu_shell;
2650   GtkWidget *child;
2651   GtkAllocation child_allocation;
2652   GtkRequisition child_requisition;
2653   GtkMenuPrivate *priv;
2654   GList *children;
2655   gint x, y;
2656   gint width, height;
2657   guint border_width;
2658   guint vertical_padding;
2659   guint horizontal_padding;
2660   
2661   g_return_if_fail (GTK_IS_MENU (widget));
2662   g_return_if_fail (allocation != NULL);
2663   
2664   menu = GTK_MENU (widget);
2665   menu_shell = GTK_MENU_SHELL (widget);
2666   priv = gtk_menu_get_private (menu);
2667
2668   widget->allocation = *allocation;
2669   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2670
2671   gtk_widget_style_get (GTK_WIDGET (menu),
2672                         "vertical-padding", &vertical_padding,
2673                         "horizontal-padding", &horizontal_padding,
2674                         NULL);
2675
2676   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2677   x = border_width + widget->style->xthickness + horizontal_padding;
2678   y = border_width + widget->style->ythickness + vertical_padding;
2679
2680   width = MAX (1, allocation->width - x * 2);
2681   height = MAX (1, allocation->height - y * 2);
2682
2683   child_requisition.width -= x * 2;
2684   child_requisition.height -= y * 2;
2685
2686   if (menu_shell->active)
2687     gtk_menu_scroll_to (menu, menu->scroll_offset);
2688
2689   if (!menu->tearoff_active)
2690     {
2691       GtkBorder arrow_border;
2692
2693       get_arrows_border (menu, &arrow_border);
2694       y += arrow_border.top;
2695       height -= arrow_border.top;
2696       height -= arrow_border.bottom;
2697     }
2698
2699   if (gtk_widget_get_realized (widget))
2700     {
2701       gdk_window_move_resize (widget->window,
2702                               allocation->x, allocation->y,
2703                               allocation->width, allocation->height);
2704
2705       gdk_window_move_resize (menu->view_window,
2706                               x,
2707                               y,
2708                               width,
2709                               height);
2710     }
2711
2712   if (menu_shell->children)
2713     {
2714       gint base_width = width / gtk_menu_get_n_columns (menu);
2715
2716       children = menu_shell->children;
2717       while (children)
2718         {
2719           child = children->data;
2720           children = children->next;
2721
2722           if (gtk_widget_get_visible (child))
2723             {
2724               gint i;
2725               gint l, r, t, b;
2726
2727               get_effective_child_attach (child, &l, &r, &t, &b);
2728
2729               if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2730                 {
2731                   guint tmp;
2732                   tmp = gtk_menu_get_n_columns (menu) - l;
2733                   l = gtk_menu_get_n_columns (menu) - r;
2734                   r = tmp;
2735                 }
2736
2737               child_allocation.width = (r - l) * base_width;
2738               child_allocation.height = 0;
2739               child_allocation.x = l * base_width;
2740               child_allocation.y = 0;
2741
2742               for (i = 0; i < b; i++)
2743                 {
2744                   if (i < t)
2745                     child_allocation.y += priv->heights[i];
2746                   else
2747                     child_allocation.height += priv->heights[i];
2748                 }
2749
2750               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2751                                                   menu->toggle_size);
2752
2753               gtk_widget_size_allocate (child, &child_allocation);
2754               gtk_widget_queue_draw (child);
2755             }
2756         }
2757       
2758       /* Resize the item window */
2759       if (gtk_widget_get_realized (widget))
2760         {
2761           gint i;
2762           gint width, height;
2763
2764           height = 0;
2765           for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2766             height += priv->heights[i];
2767
2768           width = gtk_menu_get_n_columns (menu) * base_width;
2769           gdk_window_resize (menu->bin_window, width, height);
2770         }
2771
2772       if (menu->tearoff_active)
2773         {
2774           if (allocation->height >= widget->requisition.height)
2775             {
2776               if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2777                 {
2778                   gtk_widget_hide (menu->tearoff_scrollbar);
2779                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2780
2781                   gtk_menu_scroll_to (menu, 0);
2782                 }
2783             }
2784           else
2785             {
2786               menu->tearoff_adjustment->upper = widget->requisition.height;
2787               menu->tearoff_adjustment->page_size = allocation->height;
2788               
2789               if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2790                   menu->tearoff_adjustment->upper)
2791                 {
2792                   gint value;
2793                   value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2794                   if (value < 0)
2795                     value = 0;
2796                   gtk_menu_scroll_to (menu, value);
2797                 }
2798               
2799               gtk_adjustment_changed (menu->tearoff_adjustment);
2800               
2801               if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2802                 {
2803                   gtk_widget_show (menu->tearoff_scrollbar);
2804                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2805                 }
2806             }
2807         }
2808     }
2809 }
2810
2811 static void
2812 get_arrows_visible_area (GtkMenu      *menu,
2813                          GdkRectangle *border,
2814                          GdkRectangle *upper,
2815                          GdkRectangle *lower,
2816                          gint         *arrow_space)
2817 {
2818   GtkWidget *widget = GTK_WIDGET (menu);
2819   guint border_width;
2820   guint vertical_padding;
2821   guint horizontal_padding;
2822   gint scroll_arrow_height;
2823   GtkArrowPlacement arrow_placement;
2824
2825   gtk_widget_style_get (widget,
2826                         "vertical-padding", &vertical_padding,
2827                         "horizontal-padding", &horizontal_padding,
2828                         "scroll-arrow-vlength", &scroll_arrow_height,
2829                         "arrow-placement", &arrow_placement,
2830                         NULL);
2831
2832   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2833   border->x = border_width + widget->style->xthickness + horizontal_padding;
2834   border->y = border_width + widget->style->ythickness + vertical_padding;
2835   gdk_drawable_get_size (widget->window, &border->width, &border->height);
2836
2837   switch (arrow_placement)
2838     {
2839     case GTK_ARROWS_BOTH:
2840       upper->x = border->x;
2841       upper->y = border->y;
2842       upper->width = border->width - 2 * border->x;
2843       upper->height = scroll_arrow_height;
2844
2845       lower->x = border->x;
2846       lower->y = border->height - border->y - scroll_arrow_height;
2847       lower->width = border->width - 2 * border->x;
2848       lower->height = scroll_arrow_height;
2849       break;
2850
2851     case GTK_ARROWS_START:
2852       upper->x = border->x;
2853       upper->y = border->y;
2854       upper->width = (border->width - 2 * border->x) / 2;
2855       upper->height = scroll_arrow_height;
2856
2857       lower->x = border->x + upper->width;
2858       lower->y = border->y;
2859       lower->width = (border->width - 2 * border->x) / 2;
2860       lower->height = scroll_arrow_height;
2861       break;
2862
2863     case GTK_ARROWS_END:
2864       upper->x = border->x;
2865       upper->y = border->height - border->y - scroll_arrow_height;
2866       upper->width = (border->width - 2 * border->x) / 2;
2867       upper->height = scroll_arrow_height;
2868
2869       lower->x = border->x + upper->width;
2870       lower->y = border->height - border->y - scroll_arrow_height;
2871       lower->width = (border->width - 2 * border->x) / 2;
2872       lower->height = scroll_arrow_height;
2873       break;
2874
2875     default:
2876        g_assert_not_reached();
2877        upper->x = upper->y = upper->width = upper->height = 0;
2878        lower->x = lower->y = lower->width = lower->height = 0;
2879     }
2880
2881   *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2882 }
2883
2884 static void
2885 gtk_menu_paint (GtkWidget      *widget,
2886                 GdkEventExpose *event)
2887 {
2888   GtkMenu *menu;
2889   GtkMenuPrivate *priv;
2890   GdkRectangle border;
2891   GdkRectangle upper;
2892   GdkRectangle lower;
2893   gint arrow_space;
2894   
2895   g_return_if_fail (GTK_IS_MENU (widget));
2896
2897   menu = GTK_MENU (widget);
2898   priv = gtk_menu_get_private (menu);
2899
2900   get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2901
2902   if (event->window == widget->window)
2903     {
2904       gfloat arrow_scaling;
2905       gint arrow_size;
2906
2907       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2908       arrow_size = arrow_scaling * arrow_space;
2909
2910       gtk_paint_box (widget->style,
2911                      widget->window,
2912                      GTK_STATE_NORMAL,
2913                      GTK_SHADOW_OUT,
2914                      &event->area, widget, "menu",
2915                      0, 0, -1, -1);
2916
2917       if (menu->upper_arrow_visible && !menu->tearoff_active)
2918         {
2919           gtk_paint_box (widget->style,
2920                          widget->window,
2921                          priv->upper_arrow_state,
2922                          GTK_SHADOW_OUT,
2923                          &event->area, widget, "menu_scroll_arrow_up",
2924                          upper.x,
2925                          upper.y,
2926                          upper.width,
2927                          upper.height);
2928
2929           gtk_paint_arrow (widget->style,
2930                            widget->window,
2931                            priv->upper_arrow_state,
2932                            GTK_SHADOW_OUT,
2933                            &event->area, widget, "menu_scroll_arrow_up",
2934                            GTK_ARROW_UP,
2935                            TRUE,
2936                            upper.x + (upper.width - arrow_size) / 2,
2937                            upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2938                            arrow_size, arrow_size);
2939         }
2940
2941       if (menu->lower_arrow_visible && !menu->tearoff_active)
2942         {
2943           gtk_paint_box (widget->style,
2944                          widget->window,
2945                          priv->lower_arrow_state,
2946                          GTK_SHADOW_OUT,
2947                          &event->area, widget, "menu_scroll_arrow_down",
2948                          lower.x,
2949                          lower.y,
2950                          lower.width,
2951                          lower.height);
2952
2953           gtk_paint_arrow (widget->style,
2954                            widget->window,
2955                            priv->lower_arrow_state,
2956                            GTK_SHADOW_OUT,
2957                            &event->area, widget, "menu_scroll_arrow_down",
2958                            GTK_ARROW_DOWN,
2959                            TRUE,
2960                            lower.x + (lower.width - arrow_size) / 2,
2961                            lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2962                            arrow_size, arrow_size);
2963         }
2964     }
2965   else if (event->window == menu->bin_window)
2966     {
2967       gint y = -border.y + menu->scroll_offset;
2968
2969       if (!menu->tearoff_active)
2970         {
2971           GtkBorder arrow_border;
2972
2973           get_arrows_border (menu, &arrow_border);
2974           y -= arrow_border.top;
2975         }
2976
2977       gtk_paint_box (widget->style,
2978                      menu->bin_window,
2979                      GTK_STATE_NORMAL,
2980                      GTK_SHADOW_OUT,
2981                      &event->area, widget, "menu",
2982                      - border.x, y,
2983                      border.width, border.height);
2984     }
2985 }
2986
2987 static gboolean
2988 gtk_menu_expose (GtkWidget      *widget,
2989                  GdkEventExpose *event)
2990 {
2991   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2992   g_return_val_if_fail (event != NULL, FALSE);
2993
2994   if (gtk_widget_is_drawable (widget))
2995     {
2996       gtk_menu_paint (widget, event);
2997
2998       GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
2999     }
3000   
3001   return FALSE;
3002 }
3003
3004 static void
3005 gtk_menu_show (GtkWidget *widget)
3006 {
3007   GtkMenu *menu = GTK_MENU (widget);
3008
3009   _gtk_menu_refresh_accel_paths (menu, FALSE);
3010
3011   GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3012 }
3013
3014 static gboolean
3015 gtk_menu_button_scroll (GtkMenu        *menu,
3016                         GdkEventButton *event)
3017 {
3018   if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3019     {
3020       gboolean touchscreen_mode;
3021
3022       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3023                     "gtk-touchscreen-mode", &touchscreen_mode,
3024                     NULL);
3025
3026       if (touchscreen_mode)
3027         gtk_menu_handle_scrolling (menu,
3028                                    event->x_root, event->y_root,
3029                                    event->type == GDK_BUTTON_PRESS,
3030                                    FALSE);
3031
3032       return TRUE;
3033     }
3034
3035   return FALSE;
3036 }
3037
3038 static gboolean
3039 pointer_in_menu_window (GtkWidget *widget,
3040                         gdouble    x_root,
3041                         gdouble    y_root)
3042 {
3043   GtkMenu *menu = GTK_MENU (widget);
3044
3045   if (gtk_widget_get_mapped (menu->toplevel))
3046     {
3047       GtkMenuShell *menu_shell;
3048       gint          window_x, window_y;
3049
3050       gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
3051
3052       if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
3053           y_root >= window_y && y_root < window_y + widget->allocation.height)
3054         return TRUE;
3055
3056       menu_shell = GTK_MENU_SHELL (widget);
3057
3058       if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3059         return pointer_in_menu_window (menu_shell->parent_menu_shell,
3060                                        x_root, y_root);
3061     }
3062
3063   return FALSE;
3064 }
3065
3066 static gboolean
3067 gtk_menu_button_press (GtkWidget      *widget,
3068                        GdkEventButton *event)
3069 {
3070   if (event->type != GDK_BUTTON_PRESS)
3071     return FALSE;
3072
3073   /* Don't pass down to menu shell for presses over scroll arrows
3074    */
3075   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3076     return TRUE;
3077
3078   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3079    *  was clicked. The check for the event_widget being a GtkMenuShell
3080    *  works because we have the pointer grabbed on menu_shell->window
3081    *  with owner_events=TRUE, so all events that are either outside
3082    *  the menu or on its border are delivered relative to
3083    *  menu_shell->window.
3084    */
3085   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3086       pointer_in_menu_window (widget, event->x_root, event->y_root))
3087     return TRUE;
3088
3089   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3090 }
3091
3092 static gboolean
3093 gtk_menu_button_release (GtkWidget      *widget,
3094                          GdkEventButton *event)
3095 {
3096   GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3097
3098   if (priv->ignore_button_release)
3099     {
3100       priv->ignore_button_release = FALSE;
3101       return FALSE;
3102     }
3103
3104   if (event->type != GDK_BUTTON_RELEASE)
3105     return FALSE;
3106
3107   /* Don't pass down to menu shell for releases over scroll arrows
3108    */
3109   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3110     return TRUE;
3111
3112   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3113    *  was clicked (see comment in button_press()).
3114    */
3115   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3116       pointer_in_menu_window (widget, event->x_root, event->y_root))
3117     {
3118       /*  Ugly: make sure menu_shell->button gets reset to 0 when we
3119        *  bail out early here so it is in a consistent state for the
3120        *  next button_press/button_release in GtkMenuShell.
3121        *  See bug #449371.
3122        */
3123       if (GTK_MENU_SHELL (widget)->active)
3124         GTK_MENU_SHELL (widget)->button = 0;
3125
3126       return TRUE;
3127     }
3128
3129   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3130 }
3131
3132 static const gchar *
3133 get_accel_path (GtkWidget *menu_item,
3134                 gboolean  *locked)
3135 {
3136   const gchar *path;
3137   GtkWidget *label;
3138   GClosure *accel_closure;
3139   GtkAccelGroup *accel_group;    
3140
3141   path = _gtk_widget_get_accel_path (menu_item, locked);
3142   if (!path)
3143     {
3144       path = GTK_MENU_ITEM (menu_item)->accel_path;
3145       
3146       if (locked)
3147         {
3148           *locked = TRUE;
3149
3150           label = gtk_bin_get_child (GTK_BIN (menu_item));
3151           
3152           if (GTK_IS_ACCEL_LABEL (label))
3153             {
3154               g_object_get (label, 
3155                             "accel-closure", &accel_closure, 
3156                             NULL);
3157               if (accel_closure)
3158                 {
3159                   accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3160                   
3161                   *locked = gtk_accel_group_get_is_locked (accel_group);
3162                 }
3163             }
3164         }
3165     }
3166
3167   return path;
3168 }
3169
3170 static gboolean
3171 gtk_menu_key_press (GtkWidget   *widget,
3172                     GdkEventKey *event)
3173 {
3174   GtkMenuShell *menu_shell;
3175   GtkMenu *menu;
3176   gboolean delete = FALSE;
3177   gboolean can_change_accels;
3178   gchar *accel = NULL;
3179   guint accel_key, accel_mods;
3180   GdkModifierType consumed_modifiers;
3181   GdkDisplay *display;
3182   
3183   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3184   g_return_val_if_fail (event != NULL, FALSE);
3185       
3186   menu_shell = GTK_MENU_SHELL (widget);
3187   menu = GTK_MENU (widget);
3188   
3189   gtk_menu_stop_navigating_submenu (menu);
3190
3191   if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3192     return TRUE;
3193
3194   display = gtk_widget_get_display (widget);
3195     
3196   g_object_get (gtk_widget_get_settings (widget),
3197                 "gtk-menu-bar-accel", &accel,
3198                 "gtk-can-change-accels", &can_change_accels,
3199                 NULL);
3200
3201   if (accel && *accel)
3202     {
3203       guint keyval = 0;
3204       GdkModifierType mods = 0;
3205       
3206       gtk_accelerator_parse (accel, &keyval, &mods);
3207
3208       if (keyval == 0)
3209         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3210
3211       /* FIXME this is wrong, needs to be in the global accel resolution
3212        * thing, to properly consider i18n etc., but that probably requires
3213        * AccelGroup changes etc.
3214        */
3215       if (event->keyval == keyval && (mods & event->state) == mods)
3216         {
3217           gtk_menu_shell_cancel (menu_shell);
3218           g_free (accel);
3219           return TRUE;
3220         }
3221     }
3222
3223   g_free (accel);
3224   
3225   switch (event->keyval)
3226     {
3227     case GDK_Delete:
3228     case GDK_KP_Delete:
3229     case GDK_BackSpace:
3230       delete = TRUE;
3231       break;
3232     default:
3233       break;
3234     }
3235
3236   /* Figure out what modifiers went into determining the key symbol */
3237   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3238                                        event->hardware_keycode, event->state, event->group,
3239                                        NULL, NULL, NULL, &consumed_modifiers);
3240
3241   accel_key = gdk_keyval_to_lower (event->keyval);
3242   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3243
3244   /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3245    * We re-upper case when we match against the keyval, but display and save in caseless form.
3246    */
3247   if (accel_key != event->keyval)
3248     accel_mods |= GDK_SHIFT_MASK;
3249   
3250   /* Modify the accelerators */
3251   if (can_change_accels &&
3252       menu_shell->active_menu_item &&
3253       gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3254       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&  /* no submenus */
3255       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3256     {
3257       GtkWidget *menu_item = menu_shell->active_menu_item;
3258       gboolean locked, replace_accels = TRUE;
3259       const gchar *path;
3260
3261       path = get_accel_path (menu_item, &locked);
3262       if (!path || locked)
3263         {
3264           /* can't change accelerators on menu_items without paths
3265            * (basically, those items are accelerator-locked).
3266            */
3267           /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3268           gtk_widget_error_bell (widget);
3269         }
3270       else
3271         {
3272           gboolean changed;
3273
3274           /* For the keys that act to delete the current setting, we delete
3275            * the current setting if there is one, otherwise, we set the
3276            * key as the accelerator.
3277            */
3278           if (delete)
3279             {
3280               GtkAccelKey key;
3281               
3282               if (gtk_accel_map_lookup_entry (path, &key) &&
3283                   (key.accel_key || key.accel_mods))
3284                 {
3285                   accel_key = 0;
3286                   accel_mods = 0;
3287                 }
3288             }
3289           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3290
3291           if (!changed)
3292             {
3293               /* we failed, probably because this key is in use and
3294                * locked already
3295                */
3296               /* g_print("failed to change\n"); */
3297               gtk_widget_error_bell (widget);
3298             }
3299         }
3300     }
3301   
3302   return TRUE;
3303 }
3304
3305 static gboolean
3306 check_threshold (GtkWidget *widget,
3307                  gint       start_x,
3308                  gint       start_y,
3309                  gint       x,
3310                  gint       y)
3311 {
3312 #define THRESHOLD 8
3313   
3314   return
3315     ABS (start_x - x) > THRESHOLD  ||
3316     ABS (start_y - y) > THRESHOLD;
3317 }
3318
3319 static gboolean
3320 definitely_within_item (GtkWidget *widget,
3321                         gint       x,
3322                         gint       y)
3323 {
3324   GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3325   int w, h;
3326
3327   gdk_drawable_get_size (window, &w, &h);
3328   
3329   return
3330     check_threshold (widget, 0, 0, x, y) &&
3331     check_threshold (widget, w - 1, 0, x, y) &&
3332     check_threshold (widget, w - 1, h - 1, x, y) &&
3333     check_threshold (widget, 0, h - 1, x, y);
3334 }
3335
3336 static gboolean
3337 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3338 {
3339   GtkMenuPrivate *priv;
3340
3341   priv = gtk_menu_get_private (menu);
3342
3343   return priv->navigation_height && priv->navigation_width;
3344 }
3345
3346 static gboolean
3347 gtk_menu_motion_notify (GtkWidget      *widget,
3348                         GdkEventMotion *event)
3349 {
3350   GtkWidget *menu_item;
3351   GtkMenu *menu;
3352   GtkMenuShell *menu_shell;
3353
3354   gboolean need_enter;
3355
3356   if (GTK_IS_MENU (widget))
3357     {
3358       GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3359
3360       if (priv->ignore_button_release)
3361         priv->ignore_button_release = FALSE;
3362
3363       gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3364                                  TRUE, TRUE);
3365     }
3366
3367   /* We received the event for one of two reasons:
3368    *
3369    * a) We are the active menu, and did gtk_grab_add()
3370    * b) The widget is a child of ours, and the event was propagated
3371    *
3372    * Since for computation of navigation regions, we want the menu which
3373    * is the parent of the menu item, for a), we need to find that menu,
3374    * which may be different from 'widget'.
3375    */
3376   menu_item = gtk_get_event_widget ((GdkEvent*) event);
3377   if (!GTK_IS_MENU_ITEM (menu_item) ||
3378       !GTK_IS_MENU (menu_item->parent))
3379     return FALSE;
3380
3381   menu_shell = GTK_MENU_SHELL (menu_item->parent);
3382   menu = GTK_MENU (menu_shell);
3383
3384   if (definitely_within_item (menu_item, event->x, event->y))
3385     menu_shell->activate_time = 0;
3386
3387   need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3388
3389   /* Check to see if we are within an active submenu's navigation region
3390    */
3391   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3392     return TRUE; 
3393
3394   /* Make sure we pop down if we enter a non-selectable menu item, so we
3395    * don't show a submenu when the cursor is outside the stay-up triangle.
3396    */
3397   if (!_gtk_menu_item_is_selectable (menu_item))
3398     {
3399       /* We really want to deselect, but this gives the menushell code
3400        * a chance to do some bookkeeping about the menuitem.
3401        */
3402       gtk_menu_shell_select_item (menu_shell, menu_item);
3403       return FALSE;
3404     }
3405
3406   if (need_enter)
3407     {
3408       /* The menu is now sensitive to enter events on its items, but
3409        * was previously sensitive.  So we fake an enter event.
3410        */
3411       gint width, height;
3412       
3413       menu_shell->ignore_enter = FALSE; 
3414       
3415       gdk_drawable_get_size (event->window, &width, &height);
3416       if (event->x >= 0 && event->x < width &&
3417           event->y >= 0 && event->y < height)
3418         {
3419           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3420           gboolean result;
3421
3422           send_event->crossing.window = g_object_ref (event->window);
3423           send_event->crossing.time = event->time;
3424           send_event->crossing.send_event = TRUE;
3425           send_event->crossing.x_root = event->x_root;
3426           send_event->crossing.y_root = event->y_root;
3427           send_event->crossing.x = event->x;
3428           send_event->crossing.y = event->y;
3429           send_event->crossing.state = event->state;
3430           gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3431
3432           /* We send the event to 'widget', the currently active menu,
3433            * instead of 'menu', the menu that the pointer is in. This
3434            * will ensure that the event will be ignored unless the
3435            * menuitem is a child of the active menu or some parent
3436            * menu of the active menu.
3437            */
3438           result = gtk_widget_event (widget, send_event);
3439           gdk_event_free (send_event);
3440
3441           return result;
3442         }
3443     }
3444
3445   return FALSE;
3446 }
3447
3448 static gboolean
3449 get_double_arrows (GtkMenu *menu)
3450 {
3451   GtkMenuPrivate   *priv = gtk_menu_get_private (menu);
3452   gboolean          double_arrows;
3453   GtkArrowPlacement arrow_placement;
3454
3455   gtk_widget_style_get (GTK_WIDGET (menu),
3456                         "double-arrows", &double_arrows,
3457                         "arrow-placement", &arrow_placement,
3458                         NULL);
3459
3460   if (arrow_placement != GTK_ARROWS_BOTH)
3461     return TRUE;
3462
3463   return double_arrows || (priv->initially_pushed_in &&
3464                            menu->scroll_offset != 0);
3465 }
3466
3467 static void
3468 gtk_menu_scroll_by (GtkMenu *menu, 
3469                     gint     step)
3470 {
3471   GtkWidget *widget;
3472   gint offset;
3473   gint view_width, view_height;
3474   gboolean double_arrows;
3475   GtkBorder arrow_border;
3476   
3477   widget = GTK_WIDGET (menu);
3478   offset = menu->scroll_offset + step;
3479
3480   get_arrows_border (menu, &arrow_border);
3481
3482   double_arrows = get_double_arrows (menu);
3483
3484   /* If we scroll upward and the non-visible top part
3485    * is smaller than the scroll arrow it would be
3486    * pretty stupid to show the arrow and taking more
3487    * screen space than just scrolling to the top.
3488    */
3489   if (!double_arrows)
3490     if ((step < 0) && (offset < arrow_border.top))
3491       offset = 0;
3492
3493   /* Don't scroll over the top if we weren't before: */
3494   if ((menu->scroll_offset >= 0) && (offset < 0))
3495     offset = 0;
3496
3497   gdk_drawable_get_size (widget->window, &view_width, &view_height);
3498
3499   if (menu->scroll_offset == 0 &&
3500       view_height >= widget->requisition.height)
3501     return;
3502
3503   /* Don't scroll past the bottom if we weren't before: */
3504   if (menu->scroll_offset > 0)
3505     view_height -= arrow_border.top;
3506
3507   /* When both arrows are always shown, reduce
3508    * view height even more.
3509    */
3510   if (double_arrows)
3511     view_height -= arrow_border.bottom;
3512
3513   if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3514       (offset + view_height > widget->requisition.height))
3515     offset = widget->requisition.height - view_height;
3516
3517   if (offset != menu->scroll_offset)
3518     gtk_menu_scroll_to (menu, offset);
3519 }
3520
3521 static void
3522 gtk_menu_do_timeout_scroll (GtkMenu  *menu,
3523                             gboolean  touchscreen_mode)
3524 {
3525   gboolean upper_visible;
3526   gboolean lower_visible;
3527
3528   upper_visible = menu->upper_arrow_visible;
3529   lower_visible = menu->lower_arrow_visible;
3530
3531   gtk_menu_scroll_by (menu, menu->scroll_step);
3532
3533   if (touchscreen_mode &&
3534       (upper_visible != menu->upper_arrow_visible ||
3535        lower_visible != menu->lower_arrow_visible))
3536     {
3537       /* We are about to hide a scroll arrow while the mouse is pressed,
3538        * this would cause the uncovered menu item to be activated on button
3539        * release. Therefore we need to ignore button release here
3540        */
3541       GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3542       gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3543     }
3544 }
3545
3546 static gboolean
3547 gtk_menu_scroll_timeout (gpointer data)
3548 {
3549   GtkMenu  *menu;
3550   gboolean  touchscreen_mode;
3551
3552   menu = GTK_MENU (data);
3553
3554   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3555                 "gtk-touchscreen-mode", &touchscreen_mode,
3556                 NULL);
3557
3558   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3559
3560   return TRUE;
3561 }
3562
3563 static gboolean
3564 gtk_menu_scroll_timeout_initial (gpointer data)
3565 {
3566   GtkMenu  *menu;
3567   guint     timeout;
3568   gboolean  touchscreen_mode;
3569
3570   menu = GTK_MENU (data);
3571
3572   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3573                 "gtk-timeout-repeat", &timeout,
3574                 "gtk-touchscreen-mode", &touchscreen_mode,
3575                 NULL);
3576
3577   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3578
3579   gtk_menu_remove_scroll_timeout (menu);
3580
3581   menu->timeout_id = gdk_threads_add_timeout (timeout,
3582                                               gtk_menu_scroll_timeout,
3583                                               menu);
3584
3585   return FALSE;
3586 }
3587
3588 static void
3589 gtk_menu_start_scrolling (GtkMenu *menu)
3590 {
3591   guint    timeout;
3592   gboolean touchscreen_mode;
3593
3594   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3595                 "gtk-timeout-repeat", &timeout,
3596                 "gtk-touchscreen-mode", &touchscreen_mode,
3597                 NULL);
3598
3599   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3600
3601   menu->timeout_id = gdk_threads_add_timeout (timeout,
3602                                               gtk_menu_scroll_timeout_initial,
3603                                               menu);
3604 }
3605
3606 static gboolean
3607 gtk_menu_scroll (GtkWidget      *widget,
3608                  GdkEventScroll *event)
3609 {
3610   GtkMenu *menu = GTK_MENU (widget);
3611
3612   switch (event->direction)
3613     {
3614     case GDK_SCROLL_RIGHT:
3615     case GDK_SCROLL_DOWN:
3616       gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3617       break;
3618     case GDK_SCROLL_LEFT:
3619     case GDK_SCROLL_UP:
3620       gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3621       break;
3622     }
3623
3624   return TRUE;
3625 }
3626
3627 static void
3628 get_arrows_sensitive_area (GtkMenu      *menu,
3629                            GdkRectangle *upper,
3630                            GdkRectangle *lower)
3631 {
3632   gint width, height;
3633   guint border;
3634   guint vertical_padding;
3635   gint win_x, win_y;
3636   gint scroll_arrow_height;
3637   GtkArrowPlacement arrow_placement;
3638
3639   gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3640
3641   gtk_widget_style_get (GTK_WIDGET (menu),
3642                         "vertical-padding", &vertical_padding,
3643                         "scroll-arrow-vlength", &scroll_arrow_height,
3644                         "arrow-placement", &arrow_placement,
3645                         NULL);
3646
3647   border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3648     GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3649
3650   gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3651
3652   switch (arrow_placement)
3653     {
3654     case GTK_ARROWS_BOTH:
3655       if (upper)
3656         {
3657           upper->x = win_x;
3658           upper->y = win_y;
3659           upper->width = width;
3660           upper->height = scroll_arrow_height + border;
3661         }
3662
3663       if (lower)
3664         {
3665           lower->x = win_x;
3666           lower->y = win_y + height - border - scroll_arrow_height;
3667           lower->width = width;
3668           lower->height = scroll_arrow_height + border;
3669         }
3670       break;
3671
3672     case GTK_ARROWS_START:
3673       if (upper)
3674         {
3675           upper->x = win_x;
3676           upper->y = win_y;
3677           upper->width = width / 2;
3678           upper->height = scroll_arrow_height + border;
3679         }
3680
3681       if (lower)
3682         {
3683           lower->x = win_x + width / 2;
3684           lower->y = win_y;
3685           lower->width = width / 2;
3686           lower->height = scroll_arrow_height + border;
3687         }
3688       break;
3689
3690     case GTK_ARROWS_END:
3691       if (upper)
3692         {
3693           upper->x = win_x;
3694           upper->y = win_y + height - border - scroll_arrow_height;
3695           upper->width = width / 2;
3696           upper->height = scroll_arrow_height + border;
3697         }
3698
3699       if (lower)
3700         {
3701           lower->x = win_x + width / 2;
3702           lower->y = win_y + height - border - scroll_arrow_height;
3703           lower->width = width / 2;
3704           lower->height = scroll_arrow_height + border;
3705         }
3706       break;
3707     }
3708 }
3709
3710
3711 static void
3712 gtk_menu_handle_scrolling (GtkMenu *menu,
3713                            gint     x,
3714                            gint     y,
3715                            gboolean enter,
3716                            gboolean motion)
3717 {
3718   GtkMenuShell *menu_shell;
3719   GtkMenuPrivate *priv;
3720   GdkRectangle rect;
3721   gboolean in_arrow;
3722   gboolean scroll_fast = FALSE;
3723   gint top_x, top_y;
3724   gboolean touchscreen_mode;
3725
3726   priv = gtk_menu_get_private (menu);
3727
3728   menu_shell = GTK_MENU_SHELL (menu);
3729
3730   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3731                 "gtk-touchscreen-mode", &touchscreen_mode,
3732                 NULL);
3733
3734   gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3735   x -= top_x;
3736   y -= top_y;
3737
3738   /*  upper arrow handling  */
3739
3740   get_arrows_sensitive_area (menu, &rect, NULL);
3741
3742   in_arrow = FALSE;
3743   if (menu->upper_arrow_visible && !menu->tearoff_active &&
3744       (x >= rect.x) && (x < rect.x + rect.width) &&
3745       (y >= rect.y) && (y < rect.y + rect.height))
3746     {
3747       in_arrow = TRUE;
3748     }
3749
3750   if (touchscreen_mode)
3751     menu->upper_arrow_prelight = in_arrow;
3752
3753   if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3754     {
3755       gboolean arrow_pressed = FALSE;
3756
3757       if (menu->upper_arrow_visible && !menu->tearoff_active)
3758         {
3759           if (touchscreen_mode)
3760             {
3761               if (enter && menu->upper_arrow_prelight)
3762                 {
3763                   if (menu->timeout_id == 0)
3764                     {
3765                       /* Deselect the active item so that
3766                        * any submenus are popped down
3767                        */
3768                       gtk_menu_shell_deselect (menu_shell);
3769
3770                       gtk_menu_remove_scroll_timeout (menu);
3771                       menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3772
3773                       if (!motion)
3774                         {
3775                           /* Only do stuff on click. */
3776                           gtk_menu_start_scrolling (menu);
3777                           arrow_pressed = TRUE;
3778                         }
3779                     }
3780                   else
3781                     {
3782                       arrow_pressed = TRUE;
3783                     }
3784                 }
3785               else if (!enter)
3786                 {
3787                   gtk_menu_stop_scrolling (menu);
3788                 }
3789             }
3790           else /* !touchscreen_mode */
3791             {
3792               scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3793
3794               if (enter && in_arrow &&
3795                   (!menu->upper_arrow_prelight ||
3796                    menu->scroll_fast != scroll_fast))
3797                 {
3798                   menu->upper_arrow_prelight = TRUE;
3799                   menu->scroll_fast = scroll_fast;
3800
3801                   /* Deselect the active item so that
3802                    * any submenus are popped down
3803                    */
3804                   gtk_menu_shell_deselect (menu_shell);
3805
3806                   gtk_menu_remove_scroll_timeout (menu);
3807                   menu->scroll_step = scroll_fast ?
3808                     -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3809
3810                   menu->timeout_id =
3811                     gdk_threads_add_timeout (scroll_fast ?
3812                                              MENU_SCROLL_TIMEOUT2 :
3813                                              MENU_SCROLL_TIMEOUT1,
3814                                              gtk_menu_scroll_timeout, menu);
3815                 }
3816               else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3817                 {
3818                   gtk_menu_stop_scrolling (menu);
3819                 }
3820             }
3821         }
3822
3823       /*  gtk_menu_start_scrolling() might have hit the top of the
3824        *  menu, so check if the button isn't insensitive before
3825        *  changing it to something else.
3826        */
3827       if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3828         {
3829           GtkStateType arrow_state = GTK_STATE_NORMAL;
3830
3831           if (arrow_pressed)
3832             arrow_state = GTK_STATE_ACTIVE;
3833           else if (menu->upper_arrow_prelight)
3834             arrow_state = GTK_STATE_PRELIGHT;
3835
3836           if (arrow_state != priv->upper_arrow_state)
3837             {
3838               priv->upper_arrow_state = arrow_state;
3839
3840               gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3841                                           &rect, FALSE);
3842             }
3843         }
3844     }
3845
3846   /*  lower arrow handling  */
3847
3848   get_arrows_sensitive_area (menu, NULL, &rect);
3849
3850   in_arrow = FALSE;
3851   if (menu->lower_arrow_visible && !menu->tearoff_active &&
3852       (x >= rect.x) && (x < rect.x + rect.width) &&
3853       (y >= rect.y) && (y < rect.y + rect.height))
3854     {
3855       in_arrow = TRUE;
3856     }
3857
3858   if (touchscreen_mode)
3859     menu->lower_arrow_prelight = in_arrow;
3860
3861   if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3862     {
3863       gboolean arrow_pressed = FALSE;
3864
3865       if (menu->lower_arrow_visible && !menu->tearoff_active)
3866         {
3867           if (touchscreen_mode)
3868             {
3869               if (enter && menu->lower_arrow_prelight)
3870                 {
3871                   if (menu->timeout_id == 0)
3872                     {
3873                       /* Deselect the active item so that
3874                        * any submenus are popped down
3875                        */
3876                       gtk_menu_shell_deselect (menu_shell);
3877
3878                       gtk_menu_remove_scroll_timeout (menu);
3879                       menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3880
3881                       if (!motion)
3882                         {
3883                           /* Only do stuff on click. */
3884                           gtk_menu_start_scrolling (menu);
3885                           arrow_pressed = TRUE;
3886                         }
3887                     }
3888                   else
3889                     {
3890                       arrow_pressed = TRUE;
3891                     }
3892                 }
3893               else if (!enter)
3894                 {
3895                   gtk_menu_stop_scrolling (menu);
3896                 }
3897             }
3898           else /* !touchscreen_mode */
3899             {
3900               scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3901
3902               if (enter && in_arrow &&
3903                   (!menu->lower_arrow_prelight ||
3904                    menu->scroll_fast != scroll_fast))
3905                 {
3906                   menu->lower_arrow_prelight = TRUE;
3907                   menu->scroll_fast = scroll_fast;
3908
3909                   /* Deselect the active item so that
3910                    * any submenus are popped down
3911                    */
3912                   gtk_menu_shell_deselect (menu_shell);
3913
3914                   gtk_menu_remove_scroll_timeout (menu);
3915                   menu->scroll_step = scroll_fast ?
3916                     MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3917
3918                   menu->timeout_id =
3919                     gdk_threads_add_timeout (scroll_fast ?
3920                                              MENU_SCROLL_TIMEOUT2 :
3921                                              MENU_SCROLL_TIMEOUT1,
3922                                              gtk_menu_scroll_timeout, menu);
3923                 }
3924               else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3925                 {
3926                   gtk_menu_stop_scrolling (menu);
3927                 }
3928             }
3929         }
3930
3931       /*  gtk_menu_start_scrolling() might have hit the bottom of the
3932        *  menu, so check if the button isn't insensitive before
3933        *  changing it to something else.
3934        */
3935       if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3936         {
3937           GtkStateType arrow_state = GTK_STATE_NORMAL;
3938
3939           if (arrow_pressed)
3940             arrow_state = GTK_STATE_ACTIVE;
3941           else if (menu->lower_arrow_prelight)
3942             arrow_state = GTK_STATE_PRELIGHT;
3943
3944           if (arrow_state != priv->lower_arrow_state)
3945             {
3946               priv->lower_arrow_state = arrow_state;
3947
3948               gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3949                                           &rect, FALSE);
3950             }
3951         }
3952     }
3953 }
3954
3955 static gboolean
3956 gtk_menu_enter_notify (GtkWidget        *widget,
3957                        GdkEventCrossing *event)
3958 {
3959   GtkWidget *menu_item;
3960   gboolean   touchscreen_mode;
3961
3962   if (event->mode == GDK_CROSSING_GTK_GRAB ||
3963       event->mode == GDK_CROSSING_GTK_UNGRAB ||
3964       event->mode == GDK_CROSSING_STATE_CHANGED)
3965     return TRUE;
3966
3967   g_object_get (gtk_widget_get_settings (widget),
3968                 "gtk-touchscreen-mode", &touchscreen_mode,
3969                 NULL);
3970
3971   menu_item = gtk_get_event_widget ((GdkEvent*) event);
3972   if (GTK_IS_MENU (widget))
3973     {
3974       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3975
3976       if (!menu_shell->ignore_enter)
3977         gtk_menu_handle_scrolling (GTK_MENU (widget),
3978                                    event->x_root, event->y_root, TRUE, TRUE);
3979     }
3980
3981   if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3982     {
3983       GtkWidget *menu = menu_item->parent;
3984       
3985       if (GTK_IS_MENU (menu))
3986         {
3987           GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3988           GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3989
3990           if (priv->seen_item_enter)
3991             {
3992               /* This is the second enter we see for an item
3993                * on this menu. This means a release should always
3994                * mean activate.
3995                */
3996               menu_shell->activate_time = 0;
3997             }
3998           else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3999                     event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4000             {
4001               if (definitely_within_item (menu_item, event->x, event->y))
4002                 {
4003                   /* This is an actual user-enter (ie. not a pop-under)
4004                    * In this case, the user must either have entered
4005                    * sufficiently far enough into the item, or he must move
4006                    * far enough away from the enter point. (see
4007                    * gtk_menu_motion_notify())
4008                    */
4009                   menu_shell->activate_time = 0;
4010                 }
4011             }
4012             
4013           priv->seen_item_enter = TRUE;
4014         }
4015     }
4016   
4017   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4018    * will not correspond to the event widget's parent.  Check to see
4019    * if we are in the parent's navigation region.
4020    */
4021   if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
4022       gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
4023                                    event->x_root, event->y_root))
4024     return TRUE;
4025
4026   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event); 
4027 }
4028
4029 static gboolean
4030 gtk_menu_leave_notify (GtkWidget        *widget,
4031                        GdkEventCrossing *event)
4032 {
4033   GtkMenuShell *menu_shell;
4034   GtkMenu *menu;
4035   GtkMenuItem *menu_item;
4036   GtkWidget *event_widget;
4037
4038   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4039       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4040       event->mode == GDK_CROSSING_STATE_CHANGED)
4041     return TRUE;
4042
4043   menu = GTK_MENU (widget);
4044   menu_shell = GTK_MENU_SHELL (widget); 
4045   
4046   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4047     return TRUE; 
4048
4049   gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4050
4051   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4052   
4053   if (!GTK_IS_MENU_ITEM (event_widget))
4054     return TRUE;
4055   
4056   menu_item = GTK_MENU_ITEM (event_widget); 
4057
4058   /* Here we check to see if we're leaving an active menu item with a submenu, 
4059    * in which case we enter submenu navigation mode. 
4060    */
4061   if (menu_shell->active_menu_item != NULL
4062       && menu_item->submenu != NULL
4063       && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4064     {
4065       if (GTK_MENU_SHELL (menu_item->submenu)->active)
4066         {
4067           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4068           return TRUE;
4069         }
4070       else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4071         {
4072           /* We are leaving an active menu item with nonactive submenu.
4073            * Deselect it so we don't surprise the user with by popping
4074            * up a submenu _after_ he left the item.
4075            */
4076           gtk_menu_shell_deselect (menu_shell);
4077           return TRUE;
4078         }
4079     }
4080   
4081   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event); 
4082 }
4083
4084 static void 
4085 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4086 {
4087   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4088
4089   priv->navigation_x = 0;
4090   priv->navigation_y = 0;
4091   priv->navigation_width = 0;
4092   priv->navigation_height = 0;
4093
4094   if (menu->navigation_timeout)
4095     {
4096       g_source_remove (menu->navigation_timeout);
4097       menu->navigation_timeout = 0;
4098     }
4099 }
4100
4101 /* When the timeout is elapsed, the navigation region is destroyed
4102  * and the menuitem under the pointer (if any) is selected.
4103  */
4104 static gboolean
4105 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4106 {
4107   GtkMenuPopdownData *popdown_data = user_data;
4108   GtkMenu *menu = popdown_data->menu;
4109   GdkWindow *child_window;
4110
4111   gtk_menu_stop_navigating_submenu (menu);
4112   
4113   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4114     {
4115       child_window = gdk_window_get_device_position (menu->bin_window,
4116                                                      popdown_data->device,
4117                                                      NULL, NULL, NULL);
4118
4119       if (child_window)
4120         {
4121           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4122
4123           send_event->crossing.window = g_object_ref (child_window);
4124           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4125           send_event->crossing.send_event = TRUE;
4126           gdk_event_set_device (send_event, popdown_data->device);
4127
4128           GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4129
4130           gdk_event_free (send_event);
4131         }
4132     }
4133
4134   return FALSE; 
4135 }
4136
4137 static gboolean
4138 gtk_menu_navigating_submenu (GtkMenu *menu,
4139                              gint     event_x,
4140                              gint     event_y)
4141 {
4142   GtkMenuPrivate *priv;
4143   int width, height;
4144
4145   if (!gtk_menu_has_navigation_triangle (menu))
4146     return FALSE;
4147
4148   priv = gtk_menu_get_private (menu);
4149   width = priv->navigation_width;
4150   height = priv->navigation_height;
4151
4152   /* check if x/y are in the triangle spanned by the navigation parameters */
4153
4154   /* 1) Move the coordinates so the triangle starts at 0,0 */
4155   event_x -= priv->navigation_x;
4156   event_y -= priv->navigation_y;
4157
4158   /* 2) Ensure both legs move along the positive axis */
4159   if (width < 0)
4160     {
4161       event_x = -event_x;
4162       width = -width;
4163     }
4164   if (height < 0)
4165     {
4166       event_y = -event_y;
4167       height = -height;
4168     }
4169
4170   /* 3) Check that the given coordinate is inside the triangle. The formula
4171    * is a transformed form of this formula: x/w + y/h <= 1
4172    */
4173   if (event_x >= 0 && event_y >= 0 &&
4174       event_x * height + event_y * width <= width * height)
4175     {
4176       return TRUE;
4177     }
4178   else
4179     {
4180       gtk_menu_stop_navigating_submenu (menu);
4181       return FALSE;
4182     }
4183 }
4184
4185 static void
4186 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
4187                                         GtkMenuItem      *menu_item,
4188                                         GdkEventCrossing *event)
4189 {
4190   gint submenu_left = 0;
4191   gint submenu_right = 0;
4192   gint submenu_top = 0;
4193   gint submenu_bottom = 0;
4194   gint width = 0;
4195   gint height = 0;
4196   GtkWidget *event_widget;
4197   GtkMenuPopdownData *popdown_data;
4198   GtkMenuPrivate *priv;
4199
4200   g_return_if_fail (menu_item->submenu != NULL);
4201   g_return_if_fail (event != NULL);
4202   
4203   priv = gtk_menu_get_private (menu);
4204
4205   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4206   
4207   gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
4208   gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
4209   
4210   submenu_right = submenu_left + width;
4211   submenu_bottom = submenu_top + height;
4212   
4213   gdk_drawable_get_size (event_widget->window, &width, &height);
4214   
4215   if (event->x >= 0 && event->x < width)
4216     {
4217       gint popdown_delay;
4218       
4219       gtk_menu_stop_navigating_submenu (menu);
4220
4221       /* The navigation region is the triangle closest to the x/y
4222        * location of the rectangle. This is why the width or height
4223        * can be negative.
4224        */
4225
4226       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4227         {
4228           /* right */
4229           priv->navigation_x = submenu_left;
4230           priv->navigation_width = event->x_root - submenu_left;
4231         }
4232       else
4233         {
4234           /* left */
4235           priv->navigation_x = submenu_right;
4236           priv->navigation_width = event->x_root - submenu_right;
4237         }
4238
4239       if (event->y < 0)
4240         {
4241           /* top */
4242           priv->navigation_y = event->y_root;
4243           priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4244
4245           if (priv->navigation_height >= 0)
4246             return;
4247         }
4248       else
4249         {
4250           /* bottom */
4251           priv->navigation_y = event->y_root;
4252           priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4253
4254           if (priv->navigation_height <= 0)
4255             return;
4256         }
4257
4258       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4259                     "gtk-menu-popdown-delay", &popdown_delay,
4260                     NULL);
4261
4262       popdown_data = g_new (GtkMenuPopdownData, 1);
4263       popdown_data->menu = menu;
4264       popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4265
4266       menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4267                                                                popdown_delay,
4268                                                                gtk_menu_stop_navigating_submenu_cb,
4269                                                                popdown_data,
4270                                                                (GDestroyNotify) g_free);
4271     }
4272 }
4273
4274 static void
4275 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4276 {
4277   GtkWidget *parent;
4278   
4279   g_return_if_fail (GTK_IS_MENU (menu_shell));
4280   
4281   parent = menu_shell->parent_menu_shell;
4282   
4283   menu_shell->activate_time = 0;
4284   gtk_menu_popdown (GTK_MENU (menu_shell));
4285   
4286   if (parent)
4287     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4288 }
4289
4290 static void
4291 gtk_menu_position (GtkMenu *menu)
4292 {
4293   GtkWidget *widget;
4294   GtkRequisition requisition;
4295   GtkMenuPrivate *private;
4296   gint x, y;
4297   gint scroll_offset;
4298   gint menu_height;
4299   GdkScreen *screen;
4300   GdkScreen *pointer_screen;
4301   GdkRectangle monitor;
4302   GdkDevice *pointer;
4303
4304   g_return_if_fail (GTK_IS_MENU (menu));
4305
4306   widget = GTK_WIDGET (menu);
4307
4308   screen = gtk_widget_get_screen (widget);
4309   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4310   gdk_display_get_device_state (gdk_screen_get_display (screen),
4311                                 pointer, &pointer_screen, &x, &y, NULL);
4312
4313   /* We need the requisition to figure out the right place to
4314    * popup the menu. In fact, we always need to ask here, since
4315    * if a size_request was queued while we weren't popped up,
4316    * the requisition won't have been recomputed yet.
4317    */
4318   gtk_widget_size_request (widget, &requisition);
4319
4320   if (pointer_screen != screen)
4321     {
4322       /* Pointer is on a different screen; roughly center the
4323        * menu on the screen. If someone was using multiscreen
4324        * + Xinerama together they'd probably want something
4325        * fancier; but that is likely to be vanishingly rare.
4326        */
4327       x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4328       y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4329     }
4330
4331   private = gtk_menu_get_private (menu);
4332   private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4333
4334   private->initially_pushed_in = FALSE;
4335
4336   /* Set the type hint here to allow custom position functions to set a different hint */
4337   if (!gtk_widget_get_visible (menu->toplevel))
4338     gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4339   
4340   if (menu->position_func)
4341     {
4342       (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4343                                menu->position_func_data);
4344
4345       if (private->monitor_num < 0) 
4346         private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4347
4348       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4349     }
4350   else
4351     {
4352       gint space_left, space_right, space_above, space_below;
4353       gint needed_width;
4354       gint needed_height;
4355       gint xthickness = widget->style->xthickness;
4356       gint ythickness = widget->style->ythickness;
4357       gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4358
4359       /* The placement of popup menus horizontally works like this (with
4360        * RTL in parentheses)
4361        *
4362        * - If there is enough room to the right (left) of the mouse cursor,
4363        *   position the menu there.
4364        * 
4365        * - Otherwise, if if there is enough room to the left (right) of the 
4366        *   mouse cursor, position the menu there.
4367        * 
4368        * - Otherwise if the menu is smaller than the monitor, position it
4369        *   on the side of the mouse cursor that has the most space available
4370        *
4371        * - Otherwise (if there is simply not enough room for the menu on the
4372        *   monitor), position it as far left (right) as possible.
4373        *
4374        * Positioning in the vertical direction is similar: first try below
4375        * mouse cursor, then above.
4376        */
4377       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4378
4379       space_left = x - monitor.x;
4380       space_right = monitor.x + monitor.width - x - 1;
4381       space_above = y - monitor.y;
4382       space_below = monitor.y + monitor.height - y - 1;
4383
4384       /* position horizontally */
4385
4386       /* the amount of space we need to position the menu. Note the
4387        * menu is offset "xthickness" pixels 
4388        */
4389       needed_width = requisition.width - xthickness;
4390
4391       if (needed_width <= space_left ||
4392           needed_width <= space_right)
4393         {
4394           if ((rtl  && needed_width <= space_left) ||
4395               (!rtl && needed_width >  space_right))
4396             {
4397               /* position left */
4398               x = x + xthickness - requisition.width + 1;
4399             }
4400           else
4401             {
4402               /* position right */
4403               x = x - xthickness;
4404             }
4405
4406           /* x is clamped on-screen further down */
4407         }
4408       else if (requisition.width <= monitor.width)
4409         {
4410           /* the menu is too big to fit on either side of the mouse
4411            * cursor, but smaller than the monitor. Position it on
4412            * the side that has the most space
4413            */
4414           if (space_left > space_right)
4415             {
4416               /* left justify */
4417               x = monitor.x;
4418             }
4419           else
4420             {
4421               /* right justify */
4422               x = monitor.x + monitor.width - requisition.width;
4423             }
4424         }
4425       else /* menu is simply too big for the monitor */
4426         {
4427           if (rtl)
4428             {
4429               /* right justify */
4430               x = monitor.x + monitor.width - requisition.width;
4431             }
4432           else
4433             {
4434               /* left justify */
4435               x = monitor.x;
4436             }
4437         }
4438
4439       /* Position vertically. The algorithm is the same as above, but
4440        * simpler because we don't have to take RTL into account.
4441        */
4442       needed_height = requisition.height - ythickness;
4443
4444       if (needed_height <= space_above ||
4445           needed_height <= space_below)
4446         {
4447           if (needed_height <= space_below)
4448             y = y - ythickness;
4449           else
4450             y = y + ythickness - requisition.height + 1;
4451           
4452           y = CLAMP (y, monitor.y,
4453                      monitor.y + monitor.height - requisition.height);
4454         }
4455       else if (needed_height > space_below && needed_height > space_above)
4456         {
4457           if (space_below >= space_above)
4458             y = monitor.y + monitor.height - requisition.height;
4459           else
4460             y = monitor.y;
4461         }
4462       else
4463         {
4464           y = monitor.y;
4465         }
4466     }
4467
4468   scroll_offset = 0;
4469
4470   if (private->initially_pushed_in)
4471     {
4472       menu_height = GTK_WIDGET (menu)->requisition.height;
4473
4474       if (y + menu_height > monitor.y + monitor.height)
4475         {
4476           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4477           y = (monitor.y + monitor.height) - menu_height;
4478         }
4479   
4480       if (y < monitor.y)
4481         {
4482           scroll_offset += monitor.y - y;
4483           y = monitor.y;
4484         }
4485     }
4486
4487   /* FIXME: should this be done in the various position_funcs ? */
4488   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4489  
4490   if (GTK_MENU_SHELL (menu)->active)
4491     {
4492       private->have_position = TRUE;
4493       private->x = x;
4494       private->y = y;
4495     }
4496   
4497   if (y + requisition.height > monitor.y + monitor.height)
4498     requisition.height = (monitor.y + monitor.height) - y;
4499   
4500   if (y < monitor.y)
4501     {
4502       scroll_offset += monitor.y - y;
4503       requisition.height -= monitor.y - y;
4504       y = monitor.y;
4505     }
4506
4507   if (scroll_offset > 0)
4508     {
4509       GtkBorder arrow_border;
4510
4511       get_arrows_border (menu, &arrow_border);
4512       scroll_offset += arrow_border.top;
4513     }
4514   
4515   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
4516                    x, y);
4517
4518   if (!GTK_MENU_SHELL (menu)->active)
4519     {
4520       gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4521                          requisition.width, requisition.height);
4522     }
4523   
4524   menu->scroll_offset = scroll_offset;
4525 }
4526
4527 static void
4528 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4529 {
4530   if (menu->timeout_id)
4531     {
4532       g_source_remove (menu->timeout_id);
4533       menu->timeout_id = 0;
4534     }
4535 }
4536
4537 static void
4538 gtk_menu_stop_scrolling (GtkMenu *menu)
4539 {
4540   gboolean touchscreen_mode;
4541
4542   gtk_menu_remove_scroll_timeout (menu);
4543
4544   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4545                 "gtk-touchscreen-mode", &touchscreen_mode,
4546                 NULL);
4547
4548   if (!touchscreen_mode)
4549     {
4550       menu->upper_arrow_prelight = FALSE;
4551       menu->lower_arrow_prelight = FALSE;
4552     }
4553 }
4554
4555 static void
4556 gtk_menu_scroll_to (GtkMenu *menu,
4557                     gint    offset)
4558 {
4559   GtkWidget *widget;
4560   gint x, y;
4561   gint view_width, view_height;
4562   gint border_width;
4563   gint menu_height;
4564   guint vertical_padding;
4565   guint horizontal_padding;
4566   gboolean double_arrows;
4567   GtkBorder arrow_border;
4568   
4569   widget = GTK_WIDGET (menu);
4570
4571   if (menu->tearoff_active &&
4572       menu->tearoff_adjustment &&
4573       (menu->tearoff_adjustment->value != offset))
4574     {
4575       menu->tearoff_adjustment->value =
4576         CLAMP (offset,
4577                0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4578       gtk_adjustment_value_changed (menu->tearoff_adjustment);
4579     }
4580   
4581   /* Move/resize the viewport according to arrows: */
4582   view_width = widget->allocation.width;
4583   view_height = widget->allocation.height;
4584
4585   gtk_widget_style_get (GTK_WIDGET (menu),
4586                         "vertical-padding", &vertical_padding,
4587                         "horizontal-padding", &horizontal_padding,
4588                         NULL);
4589
4590   double_arrows = get_double_arrows (menu);
4591
4592   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4593   view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4594   view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4595   menu_height = widget->requisition.height -
4596     (border_width + widget->style->ythickness + vertical_padding) * 2;
4597
4598   x = border_width + widget->style->xthickness + horizontal_padding;
4599   y = border_width + widget->style->ythickness + vertical_padding;
4600
4601   if (double_arrows && !menu->tearoff_active)
4602     {
4603       if (view_height < menu_height               ||
4604           (offset > 0 && menu->scroll_offset > 0) ||
4605           (offset < 0 && menu->scroll_offset < 0))
4606         {
4607           GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4608           GtkStateType    upper_arrow_previous_state = priv->upper_arrow_state;
4609           GtkStateType    lower_arrow_previous_state = priv->lower_arrow_state;
4610
4611           if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4612             gtk_widget_queue_draw (GTK_WIDGET (menu));
4613
4614           menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4615
4616           get_arrows_border (menu, &arrow_border);
4617           y += arrow_border.top;
4618           view_height -= arrow_border.top;
4619           view_height -= arrow_border.bottom;
4620
4621           if (offset <= 0)
4622             priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4623           else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4624             priv->upper_arrow_state = menu->upper_arrow_prelight ?
4625               GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4626
4627           if (offset >= menu_height - view_height)
4628             priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4629           else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4630             priv->lower_arrow_state = menu->lower_arrow_prelight ?
4631               GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4632
4633           if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4634               (priv->lower_arrow_state != lower_arrow_previous_state))
4635             gtk_widget_queue_draw (GTK_WIDGET (menu));
4636
4637           if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4638               priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4639             {
4640               /* At the upper border, possibly remove timeout */
4641               if (menu->scroll_step < 0)
4642                 {
4643                   gtk_menu_stop_scrolling (menu);
4644                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4645                 }
4646             }
4647
4648           if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4649               priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4650             {
4651               /* At the lower border, possibly remove timeout */
4652               if (menu->scroll_step > 0)
4653                 {
4654                   gtk_menu_stop_scrolling (menu);
4655                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4656                 }
4657             }
4658         }
4659       else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4660         {
4661           offset = 0;
4662
4663           menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4664           menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4665
4666           gtk_menu_stop_scrolling (menu);
4667           gtk_widget_queue_draw (GTK_WIDGET (menu));
4668         }
4669     }
4670   else if (!menu->tearoff_active)
4671     {
4672       gboolean last_visible;
4673
4674       last_visible = menu->upper_arrow_visible;
4675       menu->upper_arrow_visible = offset > 0;
4676       
4677       /* upper_arrow_visible may have changed, so requery the border */
4678       get_arrows_border (menu, &arrow_border);
4679       view_height -= arrow_border.top;
4680       
4681       if ((last_visible != menu->upper_arrow_visible) &&
4682           !menu->upper_arrow_visible)
4683         {
4684           menu->upper_arrow_prelight = FALSE;
4685
4686           /* If we hid the upper arrow, possibly remove timeout */
4687           if (menu->scroll_step < 0)
4688             {
4689               gtk_menu_stop_scrolling (menu);
4690               gtk_widget_queue_draw (GTK_WIDGET (menu));
4691             }
4692         }
4693
4694       last_visible = menu->lower_arrow_visible;
4695       menu->lower_arrow_visible = offset < menu_height - view_height;
4696       
4697       /* lower_arrow_visible may have changed, so requery the border */
4698       get_arrows_border (menu, &arrow_border);
4699       view_height -= arrow_border.bottom;
4700       
4701       if ((last_visible != menu->lower_arrow_visible) &&
4702            !menu->lower_arrow_visible)
4703         {
4704           menu->lower_arrow_prelight = FALSE;
4705
4706           /* If we hid the lower arrow, possibly remove timeout */
4707           if (menu->scroll_step > 0)
4708             {
4709               gtk_menu_stop_scrolling (menu);
4710               gtk_widget_queue_draw (GTK_WIDGET (menu));
4711             }
4712         }
4713       
4714       y += arrow_border.top;
4715     }
4716
4717   /* Scroll the menu: */
4718   if (gtk_widget_get_realized (widget))
4719     gdk_window_move (menu->bin_window, 0, -offset);
4720
4721   if (gtk_widget_get_realized (widget))
4722     gdk_window_move_resize (menu->view_window,
4723                             x,
4724                             y,
4725                             view_width,
4726                             view_height);
4727
4728   menu->scroll_offset = offset;
4729 }
4730
4731 static gboolean
4732 compute_child_offset (GtkMenu   *menu,
4733                       GtkWidget *menu_item,
4734                       gint      *offset,
4735                       gint      *height,
4736                       gboolean  *is_last_child)
4737 {
4738   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4739   gint item_top_attach;
4740   gint item_bottom_attach;
4741   gint child_offset = 0;
4742   gint i;
4743
4744   get_effective_child_attach (menu_item, NULL, NULL,
4745                               &item_top_attach, &item_bottom_attach);
4746
4747   /* there is a possibility that we get called before _size_request, so
4748    * check the height table for safety.
4749    */
4750   if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4751     return FALSE;
4752
4753   /* when we have a row with only invisible children, it's height will
4754    * be zero, so there's no need to check WIDGET_VISIBLE here
4755    */
4756   for (i = 0; i < item_top_attach; i++)
4757     child_offset += priv->heights[i];
4758
4759   if (is_last_child)
4760     *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4761   if (offset)
4762     *offset = child_offset;
4763   if (height)
4764     *height = priv->heights[item_top_attach];
4765
4766   return TRUE;
4767 }
4768
4769 static void
4770 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4771                               GtkWidget    *menu_item)
4772 {
4773   GtkMenu *menu;
4774   gint child_offset, child_height;
4775   gint width, height;
4776   gint y;
4777   gint arrow_height;
4778   gboolean last_child = 0;
4779   
4780   menu = GTK_MENU (menu_shell);
4781
4782   /* We need to check if the selected item fully visible.
4783    * If not we need to scroll the menu so that it becomes fully
4784    * visible.
4785    */
4786
4787   if (compute_child_offset (menu, menu_item,
4788                             &child_offset, &child_height, &last_child))
4789     {
4790       guint vertical_padding;
4791       gboolean double_arrows;
4792       
4793       y = menu->scroll_offset;
4794       gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4795
4796       gtk_widget_style_get (GTK_WIDGET (menu),
4797                             "vertical-padding", &vertical_padding,
4798                             NULL);
4799
4800       double_arrows = get_double_arrows (menu);
4801
4802       height -= 2*gtk_container_get_border_width (GTK_CONTAINER (menu)) + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4803       
4804       if (child_offset < y)
4805         {
4806           /* Ignore the enter event we might get if the pointer is on the menu
4807            */
4808           menu_shell->ignore_enter = TRUE;
4809           gtk_menu_scroll_to (menu, child_offset);
4810         }
4811       else
4812         {
4813           GtkBorder arrow_border;
4814
4815           arrow_height = 0;
4816
4817           get_arrows_border (menu, &arrow_border);
4818           if (!menu->tearoff_active)
4819             arrow_height = arrow_border.top + arrow_border.bottom;
4820           
4821           if (child_offset + child_height > y + height - arrow_height)
4822             {
4823               arrow_height = 0;
4824               if ((!last_child && !menu->tearoff_active) || double_arrows)
4825                 arrow_height += arrow_border.bottom;
4826
4827               y = child_offset + child_height - height + arrow_height;
4828               if (((y > 0) && !menu->tearoff_active) || double_arrows)
4829                 {
4830                   /* Need upper arrow */
4831                   arrow_height += arrow_border.top;
4832                   y = child_offset + child_height - height + arrow_height;
4833                 }
4834               /* Ignore the enter event we might get if the pointer is on the menu
4835                */
4836               menu_shell->ignore_enter = TRUE;
4837               gtk_menu_scroll_to (menu, y);
4838             }
4839         }    
4840       
4841     }
4842 }
4843
4844 static void
4845 gtk_menu_select_item (GtkMenuShell *menu_shell,
4846                       GtkWidget    *menu_item)
4847 {
4848   GtkMenu *menu = GTK_MENU (menu_shell);
4849
4850   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4851     gtk_menu_scroll_item_visible (menu_shell, menu_item);
4852
4853   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4854 }
4855
4856
4857 /* Reparent the menu, taking care of the refcounting
4858  *
4859  * If unrealize is true we force a unrealize while reparenting the parent.
4860  * This can help eliminate flicker in some cases.
4861  *
4862  * What happens is that when the menu is unrealized and then re-realized,
4863  * the allocations are as follows:
4864  *
4865  *  parent - 1x1 at (0,0) 
4866  *  child1 - 100x20 at (0,0)
4867  *  child2 - 100x20 at (0,20)
4868  *  child3 - 100x20 at (0,40)
4869  *
4870  * That is, the parent is small but the children are full sized. Then,
4871  * when the queued_resize gets processed, the parent gets resized to
4872  * full size. 
4873  *
4874  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4875  * contains the following logic:
4876  * 
4877  * - if a move or resize operation on a window would change the clip 
4878  *   region on the children, then before the window is resized
4879  *   the background for children is temporarily set to None, the
4880  *   move/resize done, and the background for the children restored.
4881  *
4882  * So, at the point where the parent is resized to final size, the
4883  * background for the children is temporarily None, and thus they
4884  * are not cleared to the background color and the previous background
4885  * (the image of the menu) is left in place.
4886  */
4887 static void 
4888 gtk_menu_reparent (GtkMenu   *menu,
4889                    GtkWidget *new_parent,
4890                    gboolean   unrealize)
4891 {
4892   GtkObject *object = GTK_OBJECT (menu);
4893   GtkWidget *widget = GTK_WIDGET (menu);
4894   gboolean was_floating = g_object_is_floating (object);
4895
4896   g_object_ref_sink (object);
4897
4898   if (unrealize)
4899     {
4900       g_object_ref (object);
4901       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4902       gtk_container_add (GTK_CONTAINER (new_parent), widget);
4903       g_object_unref (object);
4904     }
4905   else
4906     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4907   
4908   if (was_floating)
4909     g_object_force_floating (G_OBJECT (object));
4910   else
4911     g_object_unref (object);
4912 }
4913
4914 static void
4915 gtk_menu_show_all (GtkWidget *widget)
4916 {
4917   /* Show children, but not self. */
4918   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4919 }
4920
4921
4922 static void
4923 gtk_menu_hide_all (GtkWidget *widget)
4924 {
4925   /* Hide children, but not self. */
4926   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4927 }
4928
4929 /**
4930  * gtk_menu_set_screen:
4931  * @menu: a #GtkMenu.
4932  * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
4933  *          determined by the widget the menu is attached to.
4934  *
4935  * Sets the #GdkScreen on which the menu will be displayed.
4936  *
4937  * Since: 2.2
4938  **/
4939 void
4940 gtk_menu_set_screen (GtkMenu   *menu, 
4941                      GdkScreen *screen)
4942 {
4943   g_return_if_fail (GTK_IS_MENU (menu));
4944   g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4945
4946   g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4947
4948   if (screen)
4949     {
4950       menu_change_screen (menu, screen);
4951     }
4952   else
4953     {
4954       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4955       if (attach_widget)
4956         attach_widget_screen_changed (attach_widget, NULL, menu);
4957     }
4958 }
4959
4960 /**
4961  * gtk_menu_attach:
4962  * @menu: a #GtkMenu.
4963  * @child: a #GtkMenuItem.
4964  * @left_attach: The column number to attach the left side of the item to.
4965  * @right_attach: The column number to attach the right side of the item to.
4966  * @top_attach: The row number to attach the top of the item to.
4967  * @bottom_attach: The row number to attach the bottom of the item to.
4968  *
4969  * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4970  * an item will occupy is specified by @left_attach, @right_attach,
4971  * @top_attach and @bottom_attach. These each represent the leftmost,
4972  * rightmost, uppermost and lower column and row numbers of the table.
4973  * (Columns and rows are indexed from zero).
4974  *
4975  * Note that this function is not related to gtk_menu_detach().
4976  *
4977  * Since: 2.4
4978  **/
4979 void
4980 gtk_menu_attach (GtkMenu   *menu,
4981                  GtkWidget *child,
4982                  guint      left_attach,
4983                  guint      right_attach,
4984                  guint      top_attach,
4985                  guint      bottom_attach)
4986 {
4987   GtkMenuShell *menu_shell;
4988   
4989   g_return_if_fail (GTK_IS_MENU (menu));
4990   g_return_if_fail (GTK_IS_MENU_ITEM (child));
4991   g_return_if_fail (child->parent == NULL || 
4992                     child->parent == GTK_WIDGET (menu));
4993   g_return_if_fail (left_attach < right_attach);
4994   g_return_if_fail (top_attach < bottom_attach);
4995
4996   menu_shell = GTK_MENU_SHELL (menu);
4997   
4998   if (!child->parent)
4999     {
5000       AttachInfo *ai = get_attach_info (child);
5001       
5002       ai->left_attach = left_attach;
5003       ai->right_attach = right_attach;
5004       ai->top_attach = top_attach;
5005       ai->bottom_attach = bottom_attach;
5006       
5007       menu_shell->children = g_list_append (menu_shell->children, child);
5008
5009       gtk_widget_set_parent (child, GTK_WIDGET (menu));
5010
5011       menu_queue_resize (menu);
5012     }
5013   else
5014     {
5015       gtk_container_child_set (GTK_CONTAINER (child->parent), child,
5016                                "left-attach",   left_attach,
5017                                "right-attach",  right_attach,
5018                                "top-attach",    top_attach,
5019                                "bottom-attach", bottom_attach,
5020                                NULL);
5021     }
5022 }
5023
5024 static gint
5025 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5026 {
5027   gint popup_delay;
5028
5029   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5030                 "gtk-menu-popup-delay", &popup_delay,
5031                 NULL);
5032
5033   return popup_delay;
5034 }
5035
5036 static GtkWidget *
5037 find_child_containing (GtkMenuShell *menu_shell,
5038                        int           left,
5039                        int           right,
5040                        int           top,
5041                        int           bottom)
5042 {
5043   GList *list;
5044
5045   /* find a child which includes the area given by
5046    * left, right, top, bottom.
5047    */
5048
5049   for (list = menu_shell->children; list; list = list->next)
5050     {
5051       gint l, r, t, b;
5052
5053       if (!_gtk_menu_item_is_selectable (list->data))
5054         continue;
5055
5056       get_effective_child_attach (list->data, &l, &r, &t, &b);
5057
5058       if (l <= left && right <= r
5059           && t <= top && bottom <= b)
5060         return GTK_WIDGET (list->data);
5061     }
5062
5063   return NULL;
5064 }
5065
5066 static void
5067 gtk_menu_move_current (GtkMenuShell         *menu_shell,
5068                        GtkMenuDirectionType  direction)
5069 {
5070   GtkMenu *menu = GTK_MENU (menu_shell);
5071   gint i;
5072   gint l, r, t, b;
5073   GtkWidget *match = NULL;
5074
5075   if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5076     {
5077       switch (direction)
5078         {
5079         case GTK_MENU_DIR_CHILD:
5080           direction = GTK_MENU_DIR_PARENT;
5081           break;
5082         case GTK_MENU_DIR_PARENT:
5083           direction = GTK_MENU_DIR_CHILD;
5084           break;
5085         default: ;
5086         }
5087     }
5088
5089   /* use special table menu key bindings */
5090   if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5091     {
5092       get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5093
5094       if (direction == GTK_MENU_DIR_NEXT)
5095         {
5096           for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5097             {
5098               match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5099               if (match)
5100                 break;
5101             }
5102
5103           if (!match)
5104             {
5105               /* wrap around */
5106               for (i = 0; i < t; i++)
5107                 {
5108                   match = find_child_containing (menu_shell,
5109                                                  l, l + 1, i, i + 1);
5110                   if (match)
5111                     break;
5112                 }
5113             }
5114         }
5115       else if (direction == GTK_MENU_DIR_PREV)
5116         {
5117           for (i = t; i > 0; i--)
5118             {
5119               match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5120               if (match)
5121                 break;
5122             }
5123
5124           if (!match)
5125             {
5126               /* wrap around */
5127               for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5128                 {
5129                   match = find_child_containing (menu_shell,
5130                                                  l, l + 1, i - 1, i);
5131                   if (match)
5132                     break;
5133                 }
5134             }
5135         }
5136       else if (direction == GTK_MENU_DIR_PARENT)
5137         {
5138           /* we go one left if possible */
5139           if (l > 0)
5140             match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5141
5142           if (!match)
5143             {
5144               GtkWidget *parent = menu_shell->parent_menu_shell;
5145
5146               if (!parent
5147                   || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5148                 match = menu_shell->active_menu_item;
5149             }
5150         }
5151       else if (direction == GTK_MENU_DIR_CHILD)
5152         {
5153           /* we go one right if possible */
5154           if (r < gtk_menu_get_n_columns (menu))
5155             match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5156
5157           if (!match)
5158             {
5159               GtkWidget *parent = menu_shell->parent_menu_shell;
5160
5161               if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5162                   (!parent ||
5163                    g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5164                 match = menu_shell->active_menu_item;
5165             }
5166         }
5167
5168       if (match)
5169         {
5170           gtk_menu_shell_select_item (menu_shell, match);
5171           return;
5172         }
5173     }
5174
5175   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5176 }
5177
5178 static gint
5179 get_visible_size (GtkMenu *menu)
5180 {
5181   GtkWidget *widget = GTK_WIDGET (menu);
5182   GtkContainer *container = GTK_CONTAINER (menu);
5183   
5184   gint menu_height = (widget->allocation.height
5185                       - 2 * (gtk_container_get_border_width (container)
5186                              + widget->style->ythickness));
5187
5188   if (!menu->tearoff_active)
5189     {
5190       GtkBorder arrow_border;
5191
5192       get_arrows_border (menu, &arrow_border);
5193       menu_height -= arrow_border.top;
5194       menu_height -= arrow_border.bottom;
5195     }
5196   
5197   return menu_height;
5198 }
5199
5200 /* Find the sensitive on-screen child containing @y, or if none,
5201  * the nearest selectable onscreen child. (%NULL if none)
5202  */
5203 static GtkWidget *
5204 child_at (GtkMenu *menu,
5205           gint     y)
5206 {
5207   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5208   GtkWidget *child = NULL;
5209   gint child_offset = 0;
5210   GList *children;
5211   gint menu_height;
5212   gint lower, upper;            /* Onscreen bounds */
5213
5214   menu_height = get_visible_size (menu);
5215   lower = menu->scroll_offset;
5216   upper = menu->scroll_offset + menu_height;
5217   
5218   for (children = menu_shell->children; children; children = children->next)
5219     {
5220       if (gtk_widget_get_visible (children->data))
5221         {
5222           GtkRequisition child_requisition;
5223
5224           gtk_widget_size_request (children->data, &child_requisition);
5225
5226           if (_gtk_menu_item_is_selectable (children->data) &&
5227               child_offset >= lower &&
5228               child_offset + child_requisition.height <= upper)
5229             {
5230               child = children->data;
5231               
5232               if (child_offset + child_requisition.height > y &&
5233                   !GTK_IS_TEAROFF_MENU_ITEM (child))
5234                 return child;
5235             }
5236       
5237           child_offset += child_requisition.height;
5238         }
5239     }
5240
5241   return child;
5242 }
5243
5244 static gint
5245 get_menu_height (GtkMenu *menu)
5246 {
5247   gint height;
5248   GtkWidget *widget = GTK_WIDGET (menu);
5249
5250   height = widget->requisition.height;
5251   height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + widget->style->ythickness) * 2;
5252
5253   if (!menu->tearoff_active)
5254     {
5255       GtkBorder arrow_border;
5256
5257       get_arrows_border (menu, &arrow_border);
5258       height -= arrow_border.top;
5259       height -= arrow_border.bottom;
5260     }
5261
5262   return height;
5263 }
5264
5265 static void
5266 gtk_menu_real_move_scroll (GtkMenu       *menu,
5267                            GtkScrollType  type)
5268 {
5269   gint page_size = get_visible_size (menu);
5270   gint end_position = get_menu_height (menu);
5271   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5272   
5273   switch (type)
5274     {
5275     case GTK_SCROLL_PAGE_UP:
5276     case GTK_SCROLL_PAGE_DOWN:
5277       {
5278         gint old_offset;
5279         gint new_offset;
5280         gint child_offset = 0;
5281         gboolean old_upper_arrow_visible;
5282         gint step;
5283
5284         if (type == GTK_SCROLL_PAGE_UP)
5285           step = - page_size;
5286         else
5287           step = page_size;
5288
5289         if (menu_shell->active_menu_item)
5290           {
5291             gint child_height;
5292             
5293             compute_child_offset (menu, menu_shell->active_menu_item,
5294                                   &child_offset, &child_height, NULL);
5295             child_offset += child_height / 2;
5296           }
5297
5298         menu_shell->ignore_enter = TRUE;
5299         old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5300         old_offset = menu->scroll_offset;
5301
5302         new_offset = menu->scroll_offset + step;
5303         new_offset = CLAMP (new_offset, 0, end_position - page_size);
5304
5305         gtk_menu_scroll_to (menu, new_offset);
5306         
5307         if (menu_shell->active_menu_item)
5308           {
5309             GtkWidget *new_child;
5310             gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5311             GtkBorder arrow_border;
5312
5313             get_arrows_border (menu, &arrow_border);
5314
5315             if (menu->scroll_offset != old_offset)
5316               step = menu->scroll_offset - old_offset;
5317
5318             step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5319
5320             new_child = child_at (menu, child_offset + step);
5321             if (new_child)
5322               gtk_menu_shell_select_item (menu_shell, new_child);
5323           }
5324       }
5325       break;
5326     case GTK_SCROLL_START:
5327       /* Ignore the enter event we might get if the pointer is on the menu
5328        */
5329       menu_shell->ignore_enter = TRUE;
5330       gtk_menu_scroll_to (menu, 0);
5331       gtk_menu_shell_select_first (menu_shell, TRUE);
5332       break;
5333     case GTK_SCROLL_END:
5334       /* Ignore the enter event we might get if the pointer is on the menu
5335        */
5336       menu_shell->ignore_enter = TRUE;
5337       gtk_menu_scroll_to (menu, end_position - page_size);
5338       _gtk_menu_shell_select_last (menu_shell, TRUE);
5339       break;
5340     default:
5341       break;
5342     }
5343 }
5344
5345
5346 /**
5347  * gtk_menu_set_monitor:
5348  * @menu: a #GtkMenu
5349  * @monitor_num: the number of the monitor on which the menu should
5350  *    be popped up
5351  * 
5352  * Informs GTK+ on which monitor a menu should be popped up. 
5353  * See gdk_screen_get_monitor_geometry().
5354  *
5355  * This function should be called from a #GtkMenuPositionFunc if the
5356  * menu should not appear on the same monitor as the pointer. This 
5357  * information can't be reliably inferred from the coordinates returned
5358  * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates 
5359  * may extend beyond the monitor boundaries or even the screen boundaries. 
5360  *
5361  * Since: 2.4
5362  **/
5363 void
5364 gtk_menu_set_monitor (GtkMenu *menu,
5365                       gint     monitor_num)
5366 {
5367   GtkMenuPrivate *priv;
5368   g_return_if_fail (GTK_IS_MENU (menu));
5369
5370   priv = gtk_menu_get_private (menu);
5371   
5372   priv->monitor_num = monitor_num;
5373 }
5374
5375 /**
5376  * gtk_menu_get_monitor:
5377  * @menu: a #GtkMenu
5378  *
5379  * Retrieves the number of the monitor on which to show the menu.
5380  *
5381  * Returns: the number of the monitor on which the menu should
5382  *    be popped up or -1, if no monitor has been set
5383  *
5384  * Since: 2.14
5385  **/
5386 gint
5387 gtk_menu_get_monitor (GtkMenu *menu)
5388 {
5389   GtkMenuPrivate *priv;
5390   g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5391
5392   priv = gtk_menu_get_private (menu);
5393   
5394   return priv->monitor_num;
5395 }
5396
5397 /**
5398  * gtk_menu_get_for_attach_widget:
5399  * @widget: a #GtkWidget
5400  *
5401  * Returns a list of the menus which are attached to this widget.
5402  * This list is owned by GTK+ and must not be modified.
5403  *
5404  * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5405  *
5406  * Since: 2.6
5407  **/
5408 GList*
5409 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5410 {
5411   GList *list;
5412   
5413   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5414   
5415   list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5416   return list;
5417 }
5418
5419 static void
5420 gtk_menu_grab_notify (GtkWidget *widget,
5421                       gboolean   was_grabbed)
5422 {
5423   GtkWidget *toplevel;
5424   GtkWindowGroup *group;
5425   GtkWidget *grab;
5426   GdkDevice *pointer;
5427
5428   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5429
5430   if (!pointer ||
5431       !gtk_widget_device_is_shadowed (widget, pointer))
5432     return;
5433
5434   toplevel = gtk_widget_get_toplevel (widget);
5435
5436   if (!GTK_IS_WINDOW (toplevel))
5437     return;
5438
5439   group = gtk_window_get_group (GTK_WINDOW (toplevel));
5440   grab = gtk_window_group_get_current_device_grab (group, pointer);
5441
5442   if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5443     gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5444 }
5445
5446 /**
5447  * gtk_menu_set_reserve_toggle_size:
5448  * @menu: a #GtkMenu
5449  * @reserve_toggle_size: whether to reserve size for toggles
5450  *
5451  * Sets whether the menu should reserve space for drawing toggles 
5452  * or icons, regardless of their actual presence.
5453  *
5454  * Since: 2.18
5455  */
5456 void
5457 gtk_menu_set_reserve_toggle_size (GtkMenu  *menu,
5458                                   gboolean  reserve_toggle_size)
5459 {
5460   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5461   gboolean no_toggle_size;
5462   
5463   no_toggle_size = !reserve_toggle_size;
5464
5465   if (priv->no_toggle_size != no_toggle_size)
5466     {
5467       priv->no_toggle_size = no_toggle_size;
5468
5469       g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5470     }
5471 }
5472
5473 /**
5474  * gtk_menu_get_reserve_toggle_size:
5475  * @menu: a #GtkMenu
5476  *
5477  * Returns whether the menu reserves space for toggles and
5478  * icons, regardless of their actual presence.
5479  *
5480  * Returns: Whether the menu reserves toggle space
5481  *
5482  * Since: 2.18
5483  */
5484 gboolean
5485 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5486 {
5487   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5488
5489   return !priv->no_toggle_size;
5490 }