]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
Changing to 200 was a mistake. Change back to 225.
[~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
29 #include <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
34 #include "gtklabel.h"
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37 #include "gtkmenu.h"
38 #include "gtkmenuitem.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
41 #include "gtkhbox.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkintl.h"
45
46
47 #define MENU_ITEM_CLASS(w)   GTK_MENU_ITEM_GET_CLASS (w)
48 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
49
50 #define DEFAULT_POPUP_DELAY    225
51 #define DEFAULT_POPDOWN_DELAY  1000
52
53 #define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
54                                          * extends below the submenu
55                                          */
56
57 #define MENU_SCROLL_STEP1 8
58 #define MENU_SCROLL_STEP2 15
59 #define MENU_SCROLL_ARROW_HEIGHT 16
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 50
63
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
65
66 typedef struct _GtkMenuAttachData       GtkMenuAttachData;
67 typedef struct _GtkMenuPrivate          GtkMenuPrivate;
68
69 struct _GtkMenuAttachData
70 {
71   GtkWidget *attach_widget;
72   GtkMenuDetachFunc detacher;
73 };
74
75 struct _GtkMenuPrivate 
76 {
77   gboolean have_position;
78   gint x;
79   gint y;
80
81   /* info used for the table */
82   guint rows;
83   guint columns;
84
85   guint *heights;
86   gint heights_length;
87
88   gint monitor_num;
89 };
90
91 typedef struct
92 {
93   guint left_attach;
94   guint right_attach;
95   guint top_attach;
96   guint bottom_attach;
97 }
98 AttachInfo;
99
100 enum {
101   MOVE_SCROLL,
102   LAST_SIGNAL
103 };
104
105 enum {
106   PROP_0,
107   PROP_TEAROFF_TITLE
108 };
109
110 enum
111 {
112   CHILD_PROP_0,
113   CHILD_PROP_LEFT_ATTACH,
114   CHILD_PROP_RIGHT_ATTACH,
115   CHILD_PROP_TOP_ATTACH,
116   CHILD_PROP_BOTTOM_ATTACH
117 };
118
119 static void     gtk_menu_class_init        (GtkMenuClass     *klass);
120 static void     gtk_menu_init              (GtkMenu          *menu);
121 static void     gtk_menu_set_property      (GObject          *object,
122                                             guint             prop_id,
123                                             const GValue     *value,
124                                             GParamSpec       *pspec);
125 static void     gtk_menu_get_property      (GObject          *object,
126                                             guint             prop_id,
127                                             GValue           *value,
128                                             GParamSpec       *pspec);
129 static void     gtk_menu_set_child_property(GtkContainer     *container,
130                                             GtkWidget        *child,
131                                             guint             property_id,
132                                             const GValue     *value,
133                                             GParamSpec       *pspec);
134 static void     gtk_menu_get_child_property(GtkContainer     *container,
135                                             GtkWidget        *child,
136                                             guint             property_id,
137                                             GValue           *value,
138                                             GParamSpec       *pspec);
139 static void     gtk_menu_destroy           (GtkObject        *object);
140 static void     gtk_menu_finalize          (GObject          *object);
141 static void     gtk_menu_realize           (GtkWidget        *widget);
142 static void     gtk_menu_unrealize         (GtkWidget        *widget);
143 static void     gtk_menu_size_request      (GtkWidget        *widget,
144                                             GtkRequisition   *requisition);
145 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
146                                             GtkAllocation    *allocation);
147 static void     gtk_menu_paint             (GtkWidget        *widget,
148                                             GdkEventExpose   *expose);
149 static void     gtk_menu_show              (GtkWidget        *widget);
150 static gboolean gtk_menu_expose            (GtkWidget        *widget,
151                                             GdkEventExpose   *event);
152 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
153                                             GdkEventKey      *event);
154 static gboolean gtk_menu_button_press      (GtkWidget        *widget,
155                                             GdkEventButton   *event);
156 static gboolean gtk_menu_button_release    (GtkWidget        *widget,
157                                             GdkEventButton   *event);
158 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
159                                             GdkEventMotion   *event);
160 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
161                                             GdkEventCrossing *event);
162 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
163                                             GdkEventCrossing *event);
164 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
165                                             gint              offset);
166
167 static void     gtk_menu_stop_scrolling        (GtkMenu  *menu);
168 static void     gtk_menu_remove_scroll_timeout (GtkMenu  *menu);
169 static gboolean gtk_menu_scroll_timeout        (gpointer  data);
170
171 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
172                                               GtkWidget       *menu_item);
173 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
174                                             GtkWidget        *menu_item);
175 static void     gtk_menu_do_insert         (GtkMenuShell     *menu_shell,
176                                             GtkWidget        *child,
177                                             gint              position);
178 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
179                                             GtkWidget        *child,
180                                             gint              position);
181 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
182                                             GtkMenu          *menu);
183 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
184                                             gboolean         enter);
185 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
186                                             gint             width);
187 static void     gtk_menu_style_set         (GtkWidget        *widget,
188                                             GtkStyle         *previous_style);
189 static gboolean gtk_menu_focus             (GtkWidget        *widget,
190                                             GtkDirectionType direction);
191 static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
192 static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
193                                             GtkMenuDirectionType direction);
194 static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
195                                             GtkScrollType     type);
196
197 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
198 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
199 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
200                                                         gint              event_x,
201                                                         gint              event_y);
202 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
203                                                         GtkMenuItem      *menu_item,
204                                                         GdkEventCrossing *event);
205  
206 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
207 static void gtk_menu_show_all       (GtkWidget         *widget);
208 static void gtk_menu_hide_all       (GtkWidget         *widget);
209 static void gtk_menu_position       (GtkMenu           *menu);
210 static void gtk_menu_reparent       (GtkMenu           *menu, 
211                                      GtkWidget         *new_parent, 
212                                      gboolean           unrealize);
213 static void gtk_menu_remove         (GtkContainer      *menu,
214                                      GtkWidget         *widget);
215
216 static void gtk_menu_update_title   (GtkMenu           *menu);
217
218 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
219 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
220
221 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
222                                                   guint      signal_id);
223 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
224                                            gboolean group_changed);
225
226 static GtkMenuShellClass *parent_class = NULL;
227 static const gchar       *attach_data_key = "gtk-menu-attach-data";
228
229 static guint menu_signals[LAST_SIGNAL] = { 0 };
230
231 static void
232 gtk_menu_free_private (gpointer data)
233 {
234   GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
235
236   g_free (priv->heights);
237
238   g_free (priv);
239 }
240
241 GtkMenuPrivate *
242 gtk_menu_get_private (GtkMenu *menu)
243 {
244   GtkMenuPrivate *private;
245   static GQuark private_quark = 0;
246
247   if (!private_quark)
248     private_quark = g_quark_from_static_string ("gtk-menu-private");
249
250   private = g_object_get_qdata (G_OBJECT (menu), private_quark);
251
252   if (!private)
253     {
254       private = g_new0 (GtkMenuPrivate, 1);
255       private->have_position = FALSE;
256       
257       g_object_set_qdata_full (G_OBJECT (menu), private_quark,
258                                private, gtk_menu_free_private);
259     }
260
261   return private;
262 }
263
264 GType
265 gtk_menu_get_type (void)
266 {
267   static GType menu_type = 0;
268   
269   if (!menu_type)
270     {
271       static const GTypeInfo menu_info =
272       {
273         sizeof (GtkMenuClass),
274         NULL,           /* base_init */
275         NULL,           /* base_finalize */
276         (GClassInitFunc) gtk_menu_class_init,
277         NULL,           /* class_finalize */
278         NULL,           /* class_data */
279         sizeof (GtkMenu),
280         0,              /* n_preallocs */
281         (GInstanceInitFunc) gtk_menu_init,
282       };
283       
284       menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
285                                           &menu_info, 0);
286     }
287   
288   return menu_type;
289 }
290
291 static void
292 gtk_menu_class_init (GtkMenuClass *class)
293 {
294   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
295   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
296   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
297   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
298   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
299   GtkBindingSet *binding_set;
300   
301   parent_class = g_type_class_peek_parent (class);
302   
303   gobject_class->finalize = gtk_menu_finalize;
304   gobject_class->set_property = gtk_menu_set_property;
305   gobject_class->get_property = gtk_menu_get_property;
306
307   object_class->destroy = gtk_menu_destroy;
308   
309   widget_class->realize = gtk_menu_realize;
310   widget_class->unrealize = gtk_menu_unrealize;
311   widget_class->size_request = gtk_menu_size_request;
312   widget_class->size_allocate = gtk_menu_size_allocate;
313   widget_class->show = gtk_menu_show;
314   widget_class->expose_event = gtk_menu_expose;
315   widget_class->key_press_event = gtk_menu_key_press;
316   widget_class->button_press_event = gtk_menu_button_press;
317   widget_class->button_release_event = gtk_menu_button_release;
318   widget_class->motion_notify_event = gtk_menu_motion_notify;
319   widget_class->show_all = gtk_menu_show_all;
320   widget_class->hide_all = gtk_menu_hide_all;
321   widget_class->enter_notify_event = gtk_menu_enter_notify;
322   widget_class->leave_notify_event = gtk_menu_leave_notify;
323   widget_class->motion_notify_event = gtk_menu_motion_notify;
324   widget_class->style_set = gtk_menu_style_set;
325   widget_class->focus = gtk_menu_focus;
326   widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
327
328   container_class->remove = gtk_menu_remove;
329   container_class->get_child_property = gtk_menu_get_child_property;
330   container_class->set_child_property = gtk_menu_set_child_property;
331   
332   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
333   menu_shell_class->deactivate = gtk_menu_deactivate;
334   menu_shell_class->select_item = gtk_menu_select_item;
335   menu_shell_class->insert = gtk_menu_real_insert;
336   menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
337   menu_shell_class->move_current = gtk_menu_move_current;
338
339   menu_signals[MOVE_SCROLL] =
340     _gtk_binding_signal_new ("move_scroll",
341                              G_OBJECT_CLASS_TYPE (object_class),
342                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
343                              G_CALLBACK (gtk_menu_real_move_scroll),
344                              NULL, NULL,
345                              _gtk_marshal_VOID__ENUM,
346                              G_TYPE_NONE, 1,
347                              GTK_TYPE_SCROLL_TYPE);
348   
349   g_object_class_install_property (gobject_class,
350                                    PROP_TEAROFF_TITLE,
351                                    g_param_spec_string ("tearoff-title",
352                                                         P_("Tearoff Title"),
353                                                         P_("A title that may be displayed by the window manager when this menu is torn-off"),
354                                                         "",
355                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
356
357   gtk_widget_class_install_style_property (widget_class,
358                                            g_param_spec_int ("vertical-padding",
359                                                              P_("Vertical Padding"),
360                                                              P_("Extra space at the top and bottom of the menu"),
361                                                              0,
362                                                              G_MAXINT,
363                                                              1,
364                                                              G_PARAM_READABLE));
365
366   gtk_widget_class_install_style_property (widget_class,
367                                            g_param_spec_int ("vertical-offset",
368                                                              P_("Vertical Offset"),
369                                                              P_("When the menu is a submenu, position it this number of pixels offset vertically"),
370                                                              G_MININT,
371                                                              G_MAXINT,
372                                                              0,
373                                                              G_PARAM_READABLE));
374
375   gtk_widget_class_install_style_property (widget_class,
376                                            g_param_spec_int ("horizontal-offset",
377                                                              P_("Horizontal Offset"),
378                                                              P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
379                                                              G_MININT,
380                                                              G_MAXINT,
381                                                              -2,
382                                                              G_PARAM_READABLE));
383
384
385  gtk_container_class_install_child_property (container_class,
386                                              CHILD_PROP_LEFT_ATTACH,
387                                              g_param_spec_uint ("left_attach",
388                                                                P_("Left Attach"),
389                                                                P_("The column number to attach the left side of the child to"),
390                                                                0, UINT_MAX, 0,
391                                                                G_PARAM_READWRITE));
392
393  gtk_container_class_install_child_property (container_class,
394                                              CHILD_PROP_RIGHT_ATTACH,
395                                              g_param_spec_uint ("right_attach",
396                                                                P_("Right Attach"),
397                                                                P_("The column number to attach the right side of the child to"),
398                                                                0, UINT_MAX, 0,
399                                                                G_PARAM_READWRITE));
400
401  gtk_container_class_install_child_property (container_class,
402                                              CHILD_PROP_TOP_ATTACH,
403                                              g_param_spec_uint ("top_attach",
404                                                                P_("Top Attach"),
405                                                                P_("The row number to attach the top of the child to"),
406                                                                0, UINT_MAX, 0,
407                                                                G_PARAM_READWRITE));
408
409  gtk_container_class_install_child_property (container_class,
410                                              CHILD_PROP_BOTTOM_ATTACH,
411                                              g_param_spec_uint ("bottom_attach",
412                                                                P_("Bottom Attach"),
413                                                                P_("The row number to attach the bottom of the child to"),
414                                                                0, UINT_MAX, 0,
415                                                                G_PARAM_READWRITE));
416
417   binding_set = gtk_binding_set_by_class (class);
418   gtk_binding_entry_add_signal (binding_set,
419                                 GDK_Up, 0,
420                                 "move_current", 1,
421                                 GTK_TYPE_MENU_DIRECTION_TYPE,
422                                 GTK_MENU_DIR_PREV);
423   gtk_binding_entry_add_signal (binding_set,
424                                 GDK_KP_Up, 0,
425                                 "move_current", 1,
426                                 GTK_TYPE_MENU_DIRECTION_TYPE,
427                                 GTK_MENU_DIR_PREV);
428   gtk_binding_entry_add_signal (binding_set,
429                                 GDK_Down, 0,
430                                 "move_current", 1,
431                                 GTK_TYPE_MENU_DIRECTION_TYPE,
432                                 GTK_MENU_DIR_NEXT);
433   gtk_binding_entry_add_signal (binding_set,
434                                 GDK_KP_Down, 0,
435                                 "move_current", 1,
436                                 GTK_TYPE_MENU_DIRECTION_TYPE,
437                                 GTK_MENU_DIR_NEXT);
438   gtk_binding_entry_add_signal (binding_set,
439                                 GDK_Left, 0,
440                                 "move_current", 1,
441                                 GTK_TYPE_MENU_DIRECTION_TYPE,
442                                 GTK_MENU_DIR_PARENT);
443   gtk_binding_entry_add_signal (binding_set,
444                                 GDK_KP_Left, 0,
445                                 "move_current", 1,
446                                 GTK_TYPE_MENU_DIRECTION_TYPE,
447                                 GTK_MENU_DIR_PARENT);
448   gtk_binding_entry_add_signal (binding_set,
449                                 GDK_Right, 0,
450                                 "move_current", 1,
451                                 GTK_TYPE_MENU_DIRECTION_TYPE,
452                                 GTK_MENU_DIR_CHILD);
453   gtk_binding_entry_add_signal (binding_set,
454                                 GDK_KP_Right, 0,
455                                 "move_current", 1,
456                                 GTK_TYPE_MENU_DIRECTION_TYPE,
457                                 GTK_MENU_DIR_CHILD);
458   gtk_binding_entry_add_signal (binding_set,
459                                 GDK_Home, 0,
460                                 "move_scroll", 1,
461                                 GTK_TYPE_SCROLL_TYPE,
462                                 GTK_SCROLL_START);
463   gtk_binding_entry_add_signal (binding_set,
464                                 GDK_KP_Home, 0,
465                                 "move_scroll", 1,
466                                 GTK_TYPE_SCROLL_TYPE,
467                                 GTK_SCROLL_START);
468   gtk_binding_entry_add_signal (binding_set,
469                                 GDK_End, 0,
470                                 "move_scroll", 1,
471                                 GTK_TYPE_SCROLL_TYPE,
472                                 GTK_SCROLL_END);
473   gtk_binding_entry_add_signal (binding_set,
474                                 GDK_KP_End, 0,
475                                 "move_scroll", 1,
476                                 GTK_TYPE_SCROLL_TYPE,
477                                 GTK_SCROLL_END);
478   gtk_binding_entry_add_signal (binding_set,
479                                 GDK_Page_Up, 0,
480                                 "move_scroll", 1,
481                                 GTK_TYPE_SCROLL_TYPE,
482                                 GTK_SCROLL_PAGE_UP);
483   gtk_binding_entry_add_signal (binding_set,
484                                 GDK_KP_Page_Up, 0,
485                                 "move_scroll", 1,
486                                 GTK_TYPE_SCROLL_TYPE,
487                                 GTK_SCROLL_PAGE_UP);
488   gtk_binding_entry_add_signal (binding_set,
489                                 GDK_Page_Down, 0,
490                                 "move_scroll", 1,
491                                 GTK_TYPE_SCROLL_TYPE,
492                                 GTK_SCROLL_PAGE_DOWN);
493   gtk_binding_entry_add_signal (binding_set,
494                                 GDK_KP_Page_Down, 0,
495                                 "move_scroll", 1,
496                                 GTK_TYPE_SCROLL_TYPE,
497                                 GTK_SCROLL_PAGE_DOWN);
498
499   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
500                                                        P_("Can change accelerators"),
501                                                        P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
502                                                        FALSE,
503                                                        G_PARAM_READWRITE));
504
505   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
506                                                    P_("Delay before submenus appear"),
507                                                    P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
508                                                    0,
509                                                    G_MAXINT,
510                                                    DEFAULT_POPUP_DELAY,
511                                                    G_PARAM_READWRITE));
512
513   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
514                                                    P_("Delay before hiding a submenu"),
515                                                    P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
516                                                    0,
517                                                    G_MAXINT,
518                                                    DEFAULT_POPDOWN_DELAY,
519                                                    G_PARAM_READWRITE));
520                                                    
521 }
522
523
524 static void 
525 gtk_menu_set_property (GObject      *object,
526                        guint         prop_id,
527                        const GValue *value,
528                        GParamSpec   *pspec)
529 {
530   GtkMenu *menu;
531   
532   menu = GTK_MENU (object);
533   
534   switch (prop_id)
535     {
536     case PROP_TEAROFF_TITLE:
537       gtk_menu_set_title (menu, g_value_get_string (value));
538       break;      
539     default:
540       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541       break;
542     }
543 }
544
545 static void 
546 gtk_menu_get_property (GObject     *object,
547                        guint        prop_id,
548                        GValue      *value,
549                        GParamSpec  *pspec)
550 {
551   GtkMenu *menu;
552   
553   menu = GTK_MENU (object);
554   
555   switch (prop_id)
556     {
557     case PROP_TEAROFF_TITLE:
558       g_value_set_string (value, gtk_menu_get_title (menu));
559       break;
560     default:
561       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
562       break;
563     }
564 }
565
566 static AttachInfo *
567 get_attach_info (GObject *child)
568 {
569   AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
570
571   if (!ai)
572     {
573       ai = g_new0 (AttachInfo, 1);
574       g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
575     }
576
577   return ai;
578 }
579
580 static void
581 gtk_menu_set_child_property (GtkContainer *container,
582                              GtkWidget    *child,
583                              guint         property_id,
584                              const GValue *value,
585                              GParamSpec   *pspec)
586 {
587   GtkMenu *menu = GTK_MENU (container);
588   GtkMenuPrivate *priv;
589   AttachInfo *ai = get_attach_info (G_OBJECT (child));
590
591   priv = gtk_menu_get_private (menu);
592
593   switch (property_id)
594     {
595       case CHILD_PROP_LEFT_ATTACH:
596         ai->left_attach = g_value_get_uint (value);
597         break;
598       case CHILD_PROP_RIGHT_ATTACH:
599         ai->right_attach = g_value_get_uint (value);
600         priv->columns = MAX (priv->columns, ai->right_attach);
601         break;
602       case CHILD_PROP_TOP_ATTACH:
603         ai->top_attach = g_value_get_uint (value);
604         break;
605       case CHILD_PROP_BOTTOM_ATTACH:
606         ai->bottom_attach = g_value_get_uint (value);
607         priv->rows = MAX (priv->rows, ai->bottom_attach);
608         break;
609       default:
610         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
611         return;
612     }
613
614   gtk_widget_queue_resize (GTK_WIDGET (menu));
615 }
616
617 static void
618 gtk_menu_get_child_property (GtkContainer *container,
619                              GtkWidget    *child,
620                              guint         property_id,
621                              GValue       *value,
622                              GParamSpec   *pspec)
623 {
624   AttachInfo *ai = get_attach_info (G_OBJECT (child));
625
626   switch (property_id)
627     {
628       case CHILD_PROP_LEFT_ATTACH:
629         g_value_set_uint (value, ai->left_attach);
630         break;
631       case CHILD_PROP_RIGHT_ATTACH:
632         g_value_set_uint (value, ai->right_attach);
633         break;
634       case CHILD_PROP_TOP_ATTACH:
635         g_value_set_uint (value, ai->top_attach);
636         break;
637       case CHILD_PROP_BOTTOM_ATTACH:
638         g_value_set_uint (value, ai->bottom_attach);
639         break;
640
641       default:
642         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
643         return;
644     }
645 }
646
647 static void
648 get_child_attach (GtkWidget *child,
649                   gint      *l,
650                   gint      *r,
651                   gint      *t,
652                   gint      *b)
653 {
654   gtk_container_child_get (GTK_CONTAINER (child->parent), child,
655                            "left_attach", l,
656                            "right_attach", r,
657                            "top_attach", t,
658                            "bottom_attach", b,
659                            NULL);
660 }
661
662 static gboolean
663 gtk_menu_window_event (GtkWidget *window,
664                        GdkEvent  *event,
665                        GtkWidget *menu)
666 {
667   gboolean handled = FALSE;
668
669   g_object_ref (window);
670   g_object_ref (menu);
671
672   switch (event->type)
673     {
674     case GDK_KEY_PRESS:
675     case GDK_KEY_RELEASE:
676       handled = gtk_widget_event (menu, event);
677       break;
678     default:
679       break;
680     }
681
682   g_object_unref (window);
683   g_object_unref (menu);
684
685   return handled;
686 }
687
688 static void
689 gtk_menu_window_size_request (GtkWidget      *window,
690                               GtkRequisition *requisition,
691                               GtkMenu        *menu)
692 {
693   GtkMenuPrivate *private = gtk_menu_get_private (menu);
694
695   if (private->have_position)
696     {
697       GdkScreen *screen = gtk_widget_get_screen (window);
698       GdkRectangle monitor;
699       gint monitor_num;
700       
701       monitor_num = gdk_screen_get_monitor_at_point (screen,
702                                                      private->x, private->y);
703       gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
704
705       if (private->y + requisition->height > monitor.y + monitor.height)
706         requisition->height = monitor.y + monitor.height - private->y;
707
708       if (private->y < monitor.y)
709         requisition->height -= monitor.y - private->y;
710     }
711 }
712
713 static void
714 gtk_menu_init (GtkMenu *menu)
715 {
716   menu->parent_menu_item = NULL;
717   menu->old_active_menu_item = NULL;
718   menu->accel_group = NULL;
719   menu->position_func = NULL;
720   menu->position_func_data = NULL;
721   menu->toggle_size = 0;
722
723   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
724                                                    "type", GTK_WINDOW_POPUP,
725                                                    "child", menu,
726                                                    NULL),
727                                      "signal::event", gtk_menu_window_event, menu,
728                                      "signal::size_request", gtk_menu_window_size_request, menu,
729                                      "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
730                                      NULL);
731   gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
732   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
733
734   /* Refloat the menu, so that reference counting for the menu isn't
735    * affected by it being a child of the toplevel
736    */
737   GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
738   menu->needs_destruction_ref_count = TRUE;
739
740   menu->view_window = NULL;
741   menu->bin_window = NULL;
742
743   menu->scroll_offset = 0;
744   menu->scroll_step  = 0;
745   menu->timeout_id = 0;
746   menu->scroll_fast = FALSE;
747   
748   menu->tearoff_window = NULL;
749   menu->tearoff_hbox = NULL;
750   menu->torn_off = FALSE;
751   menu->tearoff_active = FALSE;
752   menu->tearoff_adjustment = NULL;
753   menu->tearoff_scrollbar = NULL;
754
755   menu->upper_arrow_visible = FALSE;
756   menu->lower_arrow_visible = FALSE;
757   menu->upper_arrow_prelight = FALSE;
758   menu->lower_arrow_prelight = FALSE;
759   
760   MENU_NEEDS_RESIZE (menu) = TRUE;
761 }
762
763 static void
764 gtk_menu_destroy (GtkObject *object)
765 {
766   GtkMenu *menu;
767   GtkMenuAttachData *data;
768
769   g_return_if_fail (GTK_IS_MENU (object));
770
771   menu = GTK_MENU (object);
772
773   gtk_menu_stop_scrolling (menu);
774   
775   data = g_object_get_data (G_OBJECT (object), attach_data_key);
776   if (data)
777     gtk_menu_detach (menu);
778   
779   gtk_menu_stop_navigating_submenu (menu);
780
781   if (menu->old_active_menu_item)
782     {
783       g_object_unref (menu->old_active_menu_item);
784       menu->old_active_menu_item = NULL;
785     }
786
787   /* Add back the reference count for being a child */
788   if (menu->needs_destruction_ref_count)
789     {
790       menu->needs_destruction_ref_count = FALSE;
791       g_object_ref (object);
792     }
793   
794   if (menu->accel_group)
795     {
796       g_object_unref (menu->accel_group);
797       menu->accel_group = NULL;
798     }
799
800   if (menu->toplevel)
801     gtk_widget_destroy (menu->toplevel);
802   if (menu->tearoff_window)
803     gtk_widget_destroy (menu->tearoff_window);
804
805   GTK_OBJECT_CLASS (parent_class)->destroy (object);
806 }
807
808 static void
809 gtk_menu_finalize (GObject *object)
810 {
811   GtkMenu *menu = GTK_MENU (object);
812
813   g_free (menu->accel_path);
814   
815   G_OBJECT_CLASS (parent_class)->finalize (object);
816 }
817
818 static void
819 menu_change_screen (GtkMenu   *menu,
820                     GdkScreen *new_screen)
821 {
822   if (menu->torn_off)
823     {
824       gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
825       gtk_menu_position (menu);
826     }
827
828   gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
829 }
830
831 static void
832 attach_widget_screen_changed (GtkWidget *attach_widget,
833                               GdkScreen *previous_screen,
834                               GtkMenu   *menu)
835 {
836   if (gtk_widget_has_screen (attach_widget) &&
837       !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
838     {
839       menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
840     }
841 }
842
843 void
844 gtk_menu_attach_to_widget (GtkMenu             *menu,
845                            GtkWidget           *attach_widget,
846                            GtkMenuDetachFunc    detacher)
847 {
848   GtkMenuAttachData *data;
849   
850   g_return_if_fail (GTK_IS_MENU (menu));
851   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
852   g_return_if_fail (detacher != NULL);
853   
854   /* keep this function in sync with gtk_widget_set_parent()
855    */
856   
857   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
858   if (data)
859     {
860       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
861                  g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
862      return;
863     }
864   
865   g_object_ref (menu);
866   gtk_object_sink (GTK_OBJECT (menu));
867   
868   data = g_new (GtkMenuAttachData, 1);
869   data->attach_widget = attach_widget;
870   
871   g_signal_connect (attach_widget, "screen_changed",
872                     G_CALLBACK (attach_widget_screen_changed), menu);
873   attach_widget_screen_changed (attach_widget, NULL, menu);
874   
875   data->detacher = detacher;
876   g_object_set_data (G_OBJECT (menu), attach_data_key, data);
877   
878   if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
879     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
880   
881   /* we don't need to set the style here, since
882    * we are a toplevel widget.
883    */
884
885   /* Fallback title for menu comes from attach widget */
886   gtk_menu_update_title (menu);
887 }
888
889 GtkWidget*
890 gtk_menu_get_attach_widget (GtkMenu *menu)
891 {
892   GtkMenuAttachData *data;
893   
894   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
895   
896   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
897   if (data)
898     return data->attach_widget;
899   return NULL;
900 }
901
902 void
903 gtk_menu_detach (GtkMenu *menu)
904 {
905   GtkMenuAttachData *data;
906   
907   g_return_if_fail (GTK_IS_MENU (menu));
908   
909   /* keep this function in sync with gtk_widget_unparent()
910    */
911   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
912   if (!data)
913     {
914       g_warning ("gtk_menu_detach(): menu is not attached");
915       return;
916     }
917   g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
918   
919   g_signal_handlers_disconnect_by_func (data->attach_widget,
920                                         (gpointer) attach_widget_screen_changed,
921                                         menu);
922
923   data->detacher (data->attach_widget, menu);
924   
925   if (GTK_WIDGET_REALIZED (menu))
926     gtk_widget_unrealize (GTK_WIDGET (menu));
927   
928   g_free (data);
929   
930   /* Fallback title for menu comes from attach widget */
931   gtk_menu_update_title (menu);
932
933   g_object_unref (menu);
934 }
935
936 static void
937 gtk_menu_do_remove (GtkMenuShell *menu_shell,
938                     GtkWidget    *child)
939 {
940   AttachInfo *ai;
941   GList *children;
942   GtkMenuPrivate *priv;
943   gint delta;
944   gboolean single_column;
945
946   ai = get_attach_info (G_OBJECT (child));
947   priv = gtk_menu_get_private (GTK_MENU (menu_shell));
948   delta = ai->bottom_attach - ai->top_attach;
949   single_column = priv->columns == 1;
950
951   /* recalculate these, assuming the child has already been removed */
952   priv->rows = 0;
953   priv->columns = 0;
954
955   for (children = menu_shell->children; children; children = children->next)
956     {
957       AttachInfo *child_ai = get_attach_info (children->data);
958       
959       /* Do not move items in table menus */
960       if (single_column && child_ai->bottom_attach > ai->bottom_attach)
961         gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
962                                  "top_attach", child_ai->top_attach - delta,
963                                  "bottom_attach", child_ai->bottom_attach - delta,
964                                  NULL);
965       else
966         {
967           priv->columns = MAX (priv->columns, child_ai->right_attach);
968           priv->rows = MAX (priv->rows, child_ai->bottom_attach);
969         }
970     }
971 }
972
973 static void 
974 gtk_menu_remove (GtkContainer *container,
975                  GtkWidget    *widget)
976 {
977   GtkMenu *menu;
978
979   g_return_if_fail (GTK_IS_MENU (container));
980   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
981
982   menu = GTK_MENU (container);
983
984   /* Clear out old_active_menu_item if it matches the item we are removing
985    */
986   if (menu->old_active_menu_item == widget)
987     {
988       g_object_unref (menu->old_active_menu_item);
989       menu->old_active_menu_item = NULL;
990     }
991
992   GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
993   gtk_menu_do_remove (GTK_MENU_SHELL (container), widget);
994   g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
995 }
996
997
998 GtkWidget*
999 gtk_menu_new (void)
1000 {
1001   return g_object_new (GTK_TYPE_MENU, NULL);
1002 }
1003
1004 static void
1005 gtk_menu_do_insert (GtkMenuShell *menu_shell,
1006                     GtkWidget    *child,
1007                     gint          position)
1008 {
1009   GList *children;
1010   GtkMenuPrivate *priv;
1011
1012   priv = gtk_menu_get_private (GTK_MENU (menu_shell));
1013
1014   if (position < 0 || position >= priv->rows)
1015     {
1016       /* attach after the last row */
1017       gtk_menu_attach (GTK_MENU (menu_shell), child,
1018                        0, priv->columns ? priv->columns : 1,
1019                        priv->rows, priv->rows + 1);
1020
1021       return;
1022     }
1023
1024   /* We need to make space for this new item; move all items with
1025    * top >= position one down. 
1026    * Note that this does not prevent overlaps when the menu contains 
1027    * vertically spanning items.
1028    */
1029   for (children = menu_shell->children; children; children = children->next)
1030     {
1031       AttachInfo *child_ai = get_attach_info (children->data);
1032
1033       if (child_ai->top_attach >= position)
1034         gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1035                                  "top_attach", child_ai->top_attach + 1,
1036                                  "bottom_attach", child_ai->bottom_attach + 1,
1037                                  NULL);
1038     }
1039
1040   /* attach the new item */
1041   gtk_menu_attach (GTK_MENU (menu_shell), child,
1042                    0, priv->columns ? priv->columns : 1,
1043                    position, position + 1);
1044 }
1045
1046 static void
1047 gtk_menu_real_insert (GtkMenuShell     *menu_shell,
1048                       GtkWidget        *child,
1049                       gint              position)
1050 {
1051   if (GTK_WIDGET_REALIZED (menu_shell))
1052     gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
1053
1054   GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1055
1056   gtk_menu_do_insert (menu_shell, child, position);
1057 }
1058
1059 static void
1060 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1061 {
1062   GtkWidget *widget;
1063   gint width, height;
1064
1065   widget = GTK_WIDGET (menu);
1066
1067   if (menu->torn_off)
1068     {
1069       GdkPixmap *pixmap;
1070       GdkGC *gc;
1071       GdkGCValues gc_values;
1072
1073       menu->tearoff_active = FALSE;
1074       menu->saved_scroll_offset = menu->scroll_offset;
1075       
1076       gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1077       gc = gdk_gc_new_with_values (widget->window,
1078                                    &gc_values, GDK_GC_SUBWINDOW);
1079       
1080       gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1081       
1082       pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1083                                width,
1084                                height,
1085                                -1);
1086
1087       gdk_draw_drawable (pixmap, gc,
1088                          menu->tearoff_window->window,
1089                          0, 0, 0, 0, -1, -1);
1090       g_object_unref (gc);
1091
1092       gtk_widget_set_size_request (menu->tearoff_window,
1093                                    width,
1094                                    height);
1095
1096       gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1097       g_object_unref (pixmap);
1098     }
1099 }
1100
1101 static gboolean
1102 popup_grab_on_window (GdkWindow *window,
1103                       guint32    activate_time)
1104 {
1105   if ((gdk_pointer_grab (window, TRUE,
1106                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1107                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1108                          GDK_POINTER_MOTION_MASK,
1109                          NULL, NULL, activate_time) == 0))
1110     {
1111       if (gdk_keyboard_grab (window, TRUE,
1112                              activate_time) == 0)
1113         return TRUE;
1114       else
1115         {
1116           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1117                                       activate_time);
1118           return FALSE;
1119         }
1120     }
1121
1122   return FALSE;
1123 }
1124
1125 /**
1126  * gtk_menu_popup:
1127  * @menu: a #GtkMenu.
1128  * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1129  * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1130  * @func: a user supplied function used to position the menu, or %NULL
1131  * @data: user supplied data to be passed to @func.
1132  * @button: the mouse button which was pressed to initiate the event.
1133  * @activate_time: the time at which the activation event occurred.
1134  *
1135  * Displays a menu and makes it available for selection.  Applications can use
1136  * this function to display context-sensitive menus, and will typically supply
1137  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
1138  * parameters. The default menu positioning function will position the menu
1139  * at the current mouse cursor position.
1140  *
1141  * The @button parameter should be the mouse button pressed to initiate
1142  * the menu popup. If the menu popup was initiated by something other than
1143  * a mouse button press, such as a mouse button release or a keypress,
1144  * @button should be 0.
1145  *
1146  * The @activate_time parameter should be the time stamp of the event that
1147  * initiated the popup. If such an event is not available, use
1148  * gtk_get_current_event_time() instead.
1149  *
1150  */
1151 void
1152 gtk_menu_popup (GtkMenu             *menu,
1153                 GtkWidget           *parent_menu_shell,
1154                 GtkWidget           *parent_menu_item,
1155                 GtkMenuPositionFunc  func,
1156                 gpointer             data,
1157                 guint                button,
1158                 guint32              activate_time)
1159 {
1160   GtkWidget *widget;
1161   GtkWidget *xgrab_shell;
1162   GtkWidget *parent;
1163   GdkEvent *current_event;
1164   GtkMenuShell *menu_shell;
1165
1166   g_return_if_fail (GTK_IS_MENU (menu));
1167   
1168   widget = GTK_WIDGET (menu);
1169   menu_shell = GTK_MENU_SHELL (menu);
1170   
1171   menu_shell->parent_menu_shell = parent_menu_shell;
1172   
1173   /* Find the last viewable ancestor, and make an X grab on it
1174    */
1175   parent = GTK_WIDGET (menu);
1176   xgrab_shell = NULL;
1177   while (parent)
1178     {
1179       gboolean viewable = TRUE;
1180       GtkWidget *tmp = parent;
1181       
1182       while (tmp)
1183         {
1184           if (!GTK_WIDGET_MAPPED (tmp))
1185             {
1186               viewable = FALSE;
1187               break;
1188             }
1189           tmp = tmp->parent;
1190         }
1191       
1192       if (viewable)
1193         xgrab_shell = parent;
1194       
1195       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1196     }
1197
1198   /* We want to receive events generated when we map the menu; unfortunately,
1199    * since there is probably already an implicit grab in place from the
1200    * button that the user used to pop up the menu, we won't receive then --
1201    * in particular, the EnterNotify when the menu pops up under the pointer.
1202    *
1203    * If we are grabbing on a parent menu shell, no problem; just grab on
1204    * that menu shell first before popping up the window with owner_events = TRUE.
1205    *
1206    * When grabbing on the menu itself, things get more convuluted - we
1207    * we do an explicit grab on a specially created window with
1208    * owner_events = TRUE, which we override further down with a grab
1209    * on the menu. (We can't grab on the menu until it is mapped; we
1210    * probably could just leave the grab on the other window, with a
1211    * little reorganization of the code in gtkmenu*).
1212    */
1213   if (xgrab_shell && xgrab_shell != widget)
1214     {
1215       if (popup_grab_on_window (xgrab_shell->window, activate_time))
1216         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1217     }
1218   else
1219     {
1220       GdkWindow *transfer_window;
1221
1222       xgrab_shell = widget;
1223       transfer_window = menu_grab_transfer_window_get (menu);
1224       if (popup_grab_on_window (transfer_window, activate_time))
1225         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1226     }
1227
1228   if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1229     {
1230       /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1231        * with a stuck up window, we just abort here. Presumably the user will
1232        * try again.
1233        */
1234       menu_shell->parent_menu_shell = NULL;
1235       menu_grab_transfer_window_destroy (menu);
1236       return;
1237     }
1238
1239   menu_shell->active = TRUE;
1240   menu_shell->button = button;
1241
1242   /* If we are popping up the menu from something other than, a button
1243    * press then, as a heuristic, we ignore enter events for the menu
1244    * until we get a MOTION_NOTIFY.  
1245    */
1246
1247   current_event = gtk_get_current_event ();
1248   if (current_event)
1249     {
1250       if ((current_event->type != GDK_BUTTON_PRESS) &&
1251           (current_event->type != GDK_ENTER_NOTIFY))
1252         menu_shell->ignore_enter = TRUE;
1253
1254       gdk_event_free (current_event);
1255     }
1256   else
1257     menu_shell->ignore_enter = TRUE;
1258
1259   if (menu->torn_off)
1260     {
1261       gtk_menu_tearoff_bg_copy (menu);
1262
1263       gtk_menu_reparent (menu, menu->toplevel, FALSE);
1264     }
1265   
1266   menu->parent_menu_item = parent_menu_item;
1267   menu->position_func = func;
1268   menu->position_func_data = data;
1269   menu_shell->activate_time = activate_time;
1270
1271   /* We need to show the menu here rather in the init function because
1272    * code expects to be able to tell if the menu is onscreen by
1273    * looking at the GTK_WIDGET_VISIBLE (menu)
1274    */
1275   gtk_widget_show (GTK_WIDGET (menu));
1276
1277   /* Position the menu, possibly changing the size request
1278    */
1279   gtk_menu_position (menu);
1280
1281   /* Compute the size of the toplevel and realize it so we
1282    * can scroll correctly.
1283    */
1284   {
1285     GtkRequisition tmp_request;
1286     GtkAllocation tmp_allocation = { 0, };
1287
1288     gtk_widget_size_request (menu->toplevel, &tmp_request);
1289     
1290     tmp_allocation.width = tmp_request.width;
1291     tmp_allocation.height = tmp_request.height;
1292
1293     gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1294     
1295     gtk_widget_realize (GTK_WIDGET (menu));
1296   }
1297
1298   gtk_menu_scroll_to (menu, menu->scroll_offset);
1299
1300   /* Once everything is set up correctly, map the toplevel window on
1301      the screen.
1302    */
1303   gtk_widget_show (menu->toplevel);
1304
1305   if (xgrab_shell == widget)
1306     popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1307   gtk_grab_add (GTK_WIDGET (menu));
1308 }
1309
1310 void
1311 gtk_menu_popdown (GtkMenu *menu)
1312 {
1313   GtkMenuPrivate *private;
1314   GtkMenuShell *menu_shell;
1315
1316   g_return_if_fail (GTK_IS_MENU (menu));
1317   
1318   menu_shell = GTK_MENU_SHELL (menu);
1319   private = gtk_menu_get_private (menu);
1320   
1321   menu_shell->parent_menu_shell = NULL;
1322   menu_shell->active = FALSE;
1323   menu_shell->ignore_enter = FALSE;
1324
1325   private->have_position = FALSE;
1326
1327   gtk_menu_stop_scrolling (menu);
1328   
1329   gtk_menu_stop_navigating_submenu (menu);
1330   
1331   if (menu_shell->active_menu_item)
1332     {
1333       if (menu->old_active_menu_item)
1334         g_object_unref (menu->old_active_menu_item);
1335       menu->old_active_menu_item = menu_shell->active_menu_item;
1336       g_object_ref (menu->old_active_menu_item);
1337     }
1338
1339   gtk_menu_shell_deselect (menu_shell);
1340   
1341   /* The X Grab, if present, will automatically be removed when we hide
1342    * the window */
1343   gtk_widget_hide (menu->toplevel);
1344
1345   if (menu->torn_off)
1346     {
1347       gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1348       
1349       if (GTK_BIN (menu->toplevel)->child) 
1350         {
1351           gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1352         } 
1353       else
1354         {
1355           /* We popped up the menu from the tearoff, so we need to 
1356            * release the grab - we aren't actually hiding the menu.
1357            */
1358           if (menu_shell->have_xgrab)
1359             {
1360               GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1361               
1362               gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1363               gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1364             }
1365         }
1366
1367       /* gtk_menu_popdown is called each time a menu item is selected from
1368        * a torn off menu. Only scroll back to the saved position if the
1369        * non-tearoff menu was popped down.
1370        */
1371       if (!menu->tearoff_active)
1372         gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1373       menu->tearoff_active = TRUE;
1374     }
1375   else
1376     gtk_widget_hide (GTK_WIDGET (menu));
1377
1378   menu_shell->have_xgrab = FALSE;
1379   gtk_grab_remove (GTK_WIDGET (menu));
1380
1381   menu_grab_transfer_window_destroy (menu);
1382 }
1383
1384 GtkWidget*
1385 gtk_menu_get_active (GtkMenu *menu)
1386 {
1387   GtkWidget *child;
1388   GList *children;
1389   
1390   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1391   
1392   if (!menu->old_active_menu_item)
1393     {
1394       child = NULL;
1395       children = GTK_MENU_SHELL (menu)->children;
1396       
1397       while (children)
1398         {
1399           child = children->data;
1400           children = children->next;
1401           
1402           if (GTK_BIN (child)->child)
1403             break;
1404           child = NULL;
1405         }
1406       
1407       menu->old_active_menu_item = child;
1408       if (menu->old_active_menu_item)
1409         g_object_ref (menu->old_active_menu_item);
1410     }
1411   
1412   return menu->old_active_menu_item;
1413 }
1414
1415 void
1416 gtk_menu_set_active (GtkMenu *menu,
1417                      guint    index)
1418 {
1419   GtkWidget *child;
1420   GList *tmp_list;
1421   
1422   g_return_if_fail (GTK_IS_MENU (menu));
1423   
1424   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1425   if (tmp_list)
1426     {
1427       child = tmp_list->data;
1428       if (GTK_BIN (child)->child)
1429         {
1430           if (menu->old_active_menu_item)
1431             g_object_unref (menu->old_active_menu_item);
1432           menu->old_active_menu_item = child;
1433           g_object_ref (menu->old_active_menu_item);
1434         }
1435     }
1436 }
1437
1438 void
1439 gtk_menu_set_accel_group (GtkMenu       *menu,
1440                           GtkAccelGroup *accel_group)
1441 {
1442   g_return_if_fail (GTK_IS_MENU (menu));
1443   
1444   if (menu->accel_group != accel_group)
1445     {
1446       if (menu->accel_group)
1447         g_object_unref (menu->accel_group);
1448       menu->accel_group = accel_group;
1449       if (menu->accel_group)
1450         g_object_ref (menu->accel_group);
1451       _gtk_menu_refresh_accel_paths (menu, TRUE);
1452     }
1453 }
1454
1455 GtkAccelGroup*
1456 gtk_menu_get_accel_group (GtkMenu *menu)
1457 {
1458   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1459
1460   return menu->accel_group;
1461 }
1462
1463 static gboolean
1464 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1465                                   guint      signal_id)
1466 {
1467   /* Menu items chain here to figure whether they can activate their
1468    * accelerators.  Unlike ordinary widgets, menus allow accel
1469    * activation even if invisible since that's the usual case for
1470    * submenus/popup-menus. however, the state of the attach widget
1471    * affects the "activeness" of the menu.
1472    */
1473   GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1474
1475   if (awidget)
1476     return gtk_widget_can_activate_accel (awidget, signal_id);
1477   else
1478     return GTK_WIDGET_IS_SENSITIVE (widget);
1479 }
1480
1481 /**
1482  * gtk_menu_set_accel_path
1483  * @menu:       a valid #GtkMenu
1484  * @accel_path: a valid accelerator path
1485  *
1486  * Sets an accelerator path for this menu from which accelerator paths
1487  * for its immediate children, its menu items, can be constructed.
1488  * The main purpose of this function is to spare the programmer the
1489  * inconvenience of having to call gtk_menu_item_set_accel_path() on
1490  * each menu item that should support runtime user changable accelerators.
1491  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1492  * each menu item of this menu, that contains a label describing its purpose,
1493  * automatically gets an accel path assigned. For example, a menu containing
1494  * menu items "New" and "Exit", will, after 
1495  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
1496  * has been called, assign its items the accel paths:
1497  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
1498  * Assigning accel paths to menu items then enables the user to change
1499  * their accelerators at runtime. More details about accelerator paths
1500  * and their default setups can be found at gtk_accel_map_add_entry().
1501  */
1502 void
1503 gtk_menu_set_accel_path (GtkMenu     *menu,
1504                          const gchar *accel_path)
1505 {
1506   g_return_if_fail (GTK_IS_MENU (menu));
1507   if (accel_path)
1508     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1509
1510   g_free (menu->accel_path);
1511   menu->accel_path = g_strdup (accel_path);
1512   if (menu->accel_path)
1513     _gtk_menu_refresh_accel_paths (menu, FALSE);
1514 }
1515
1516 typedef struct {
1517   GtkMenu *menu;
1518   gboolean group_changed;
1519 } AccelPropagation;
1520
1521 static void
1522 refresh_accel_paths_foreach (GtkWidget *widget,
1523                              gpointer   data)
1524 {
1525   AccelPropagation *prop = data;
1526
1527   if (GTK_IS_MENU_ITEM (widget))        /* should always be true */
1528     _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1529                                        prop->menu->accel_path,
1530                                        prop->menu->accel_group,
1531                                        prop->group_changed);
1532 }
1533
1534 static void
1535 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1536                                gboolean group_changed)
1537 {
1538   g_return_if_fail (GTK_IS_MENU (menu));
1539       
1540   if (menu->accel_path && menu->accel_group)
1541     {
1542       AccelPropagation prop;
1543
1544       prop.menu = menu;
1545       prop.group_changed = group_changed;
1546       gtk_container_foreach (GTK_CONTAINER (menu),
1547                              refresh_accel_paths_foreach,
1548                              &prop);
1549     }
1550 }
1551
1552 void
1553 gtk_menu_reposition (GtkMenu *menu)
1554 {
1555   g_return_if_fail (GTK_IS_MENU (menu));
1556
1557   if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1558     gtk_menu_position (menu);
1559 }
1560
1561 static void
1562 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1563                             GtkMenu       *menu)
1564 {
1565   g_return_if_fail (GTK_IS_MENU (menu));
1566
1567   if (adjustment->value != menu->scroll_offset)
1568     gtk_menu_scroll_to (menu, adjustment->value);
1569 }
1570
1571 static void
1572 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1573                             gint     width)
1574 {
1575   GdkGeometry geometry_hints;
1576   
1577   if (!menu->tearoff_window)
1578     return;
1579
1580   if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1581     {
1582       gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1583       width += menu->tearoff_scrollbar->requisition.width;
1584     }
1585
1586   geometry_hints.min_width = width;
1587   geometry_hints.max_width = width;
1588     
1589   geometry_hints.min_height = 0;
1590   geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1591
1592   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1593                                  NULL,
1594                                  &geometry_hints,
1595                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1596 }
1597
1598 static void
1599 gtk_menu_update_title (GtkMenu *menu)
1600 {
1601   if (menu->tearoff_window)
1602     {
1603       const gchar *title;
1604       GtkWidget *attach_widget;
1605
1606       title = gtk_menu_get_title (menu);
1607       if (!title)
1608         {
1609           attach_widget = gtk_menu_get_attach_widget (menu);
1610           if (GTK_IS_MENU_ITEM (attach_widget))
1611             {
1612               GtkWidget *child = GTK_BIN (attach_widget)->child;
1613               if (GTK_IS_LABEL (child))
1614                 title = gtk_label_get_text (GTK_LABEL (child));
1615             }
1616         }
1617       
1618       if (title)
1619         gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1620     }
1621 }
1622
1623 static GtkWidget*
1624 gtk_menu_get_toplevel (GtkWidget *menu)
1625 {
1626   GtkWidget *attach, *toplevel;
1627
1628   attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1629
1630   if (GTK_IS_MENU_ITEM (attach))
1631     attach = attach->parent;
1632
1633   if (GTK_IS_MENU (attach))
1634     return gtk_menu_get_toplevel (attach->parent);
1635   else if (GTK_IS_WIDGET (attach))
1636     {
1637       toplevel = gtk_widget_get_toplevel (attach->parent);
1638       if (GTK_WIDGET_TOPLEVEL (toplevel)) 
1639         return toplevel;
1640     }
1641
1642   return NULL;
1643 }
1644
1645 void       
1646 gtk_menu_set_tearoff_state (GtkMenu  *menu,
1647                             gboolean  torn_off)
1648 {
1649   gint width, height;
1650   
1651   g_return_if_fail (GTK_IS_MENU (menu));
1652
1653   if (menu->torn_off != torn_off)
1654     {
1655       menu->torn_off = torn_off;
1656       menu->tearoff_active = torn_off;
1657       
1658       if (menu->torn_off)
1659         {
1660           if (GTK_WIDGET_VISIBLE (menu))
1661             gtk_menu_popdown (menu);
1662
1663           if (!menu->tearoff_window)
1664             {
1665               GtkWidget *toplevel;
1666
1667               menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1668                                                      "type", GTK_WINDOW_TOPLEVEL,
1669                                                      "screen", gtk_widget_get_screen (menu->toplevel),
1670                                                      "app_paintable", TRUE,
1671                                                      NULL);
1672
1673               
1674               gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1675                                         GDK_WINDOW_TYPE_HINT_MENU);
1676               gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1677               g_signal_connect (menu->tearoff_window, "destroy",
1678                                 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1679               g_signal_connect (menu->tearoff_window, "event",
1680                                 G_CALLBACK (gtk_menu_window_event), menu);
1681
1682               gtk_menu_update_title (menu);
1683
1684               gtk_widget_realize (menu->tearoff_window);
1685
1686               toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1687               if (toplevel != NULL)
1688                 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1689                                               GTK_WINDOW (toplevel));
1690               
1691               menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1692               gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1693
1694               gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1695               menu->tearoff_adjustment =
1696                 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1697                                                     0,
1698                                                     GTK_WIDGET (menu)->requisition.height,
1699                                                     MENU_SCROLL_STEP2,
1700                                                     height/2,
1701                                                     height));
1702               g_object_connect (menu->tearoff_adjustment,
1703                                 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1704                                 NULL);
1705               menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1706
1707               gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1708                                 menu->tearoff_scrollbar,
1709                                 FALSE, FALSE, 0);
1710               
1711               if (menu->tearoff_adjustment->upper > height)
1712                 gtk_widget_show (menu->tearoff_scrollbar);
1713               
1714               gtk_widget_show (menu->tearoff_hbox);
1715             }
1716           
1717           gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1718
1719           gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1720
1721           /* Update menu->requisition
1722            */
1723           gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1724   
1725           gtk_menu_set_tearoff_hints (menu, width);
1726             
1727           gtk_widget_realize (menu->tearoff_window);
1728           gtk_menu_position (menu);
1729           
1730           gtk_widget_show (GTK_WIDGET (menu));
1731           gtk_widget_show (menu->tearoff_window);
1732
1733           gtk_menu_scroll_to (menu, 0);
1734
1735         }
1736       else
1737         {
1738           gtk_widget_hide (GTK_WIDGET (menu));
1739           gtk_widget_hide (menu->tearoff_window);
1740           gtk_menu_reparent (menu, menu->toplevel, FALSE);
1741           gtk_widget_destroy (menu->tearoff_window);
1742           
1743           menu->tearoff_window = NULL;
1744           menu->tearoff_hbox = NULL;
1745           menu->tearoff_scrollbar = NULL;
1746           menu->tearoff_adjustment = NULL;
1747         }
1748     }
1749 }
1750
1751 /**
1752  * gtk_menu_get_tearoff_state:
1753  * @menu: a #GtkMenu
1754  *
1755  * Returns whether the menu is torn off. See
1756  * gtk_menu_set_tearoff_state ().
1757  *
1758  * Return value: %TRUE if the menu is currently torn off.
1759  **/
1760 gboolean
1761 gtk_menu_get_tearoff_state (GtkMenu *menu)
1762 {
1763   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1764
1765   return menu->torn_off;
1766 }
1767
1768 /**
1769  * gtk_menu_set_title:
1770  * @menu: a #GtkMenu
1771  * @title: a string containing the title for the menu.
1772  * 
1773  * Sets the title string for the menu.  The title is displayed when the menu
1774  * is shown as a tearoff menu.
1775  **/
1776 void       
1777 gtk_menu_set_title (GtkMenu     *menu,
1778                     const gchar *title)
1779 {
1780   g_return_if_fail (GTK_IS_MENU (menu));
1781
1782   if (title)
1783     g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1784                             g_strdup (title), (GtkDestroyNotify) g_free);
1785   else
1786     g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1787     
1788   gtk_menu_update_title (menu);
1789   g_object_notify (G_OBJECT (menu), "tearoff_title");
1790 }
1791
1792 /**
1793  * gtk_menu_get_title:
1794  * @menu: a #GtkMenu
1795  *
1796  * Returns the title of the menu. See gtk_menu_set_title().
1797  *
1798  * Return value: the title of the menu, or %NULL if the menu has no
1799  * title set on it. This string is owned by the widget and should
1800  * not be modified or freed.
1801  **/
1802 G_CONST_RETURN gchar *
1803 gtk_menu_get_title (GtkMenu *menu)
1804 {
1805   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1806
1807   return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1808 }
1809
1810 void
1811 gtk_menu_reorder_child (GtkMenu   *menu,
1812                         GtkWidget *child,
1813                         gint       position)
1814 {
1815   GtkMenuShell *menu_shell;
1816
1817   g_return_if_fail (GTK_IS_MENU (menu));
1818   g_return_if_fail (GTK_IS_MENU_ITEM (child));
1819
1820   menu_shell = GTK_MENU_SHELL (menu);
1821
1822   if (g_list_find (menu_shell->children, child))
1823     {   
1824       menu_shell->children = g_list_remove (menu_shell->children, child);
1825       menu_shell->children = g_list_insert (menu_shell->children, child, position);
1826       gtk_menu_do_insert (menu_shell, child, position);
1827
1828       if (GTK_WIDGET_VISIBLE (menu_shell))
1829         gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1830     }   
1831 }
1832
1833 static void
1834 gtk_menu_style_set (GtkWidget *widget,
1835                     GtkStyle  *previous_style)
1836 {
1837   if (GTK_WIDGET_REALIZED (widget))
1838     {
1839       GtkMenu *menu = GTK_MENU (widget);
1840       
1841       gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1842       gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1843       gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1844     }
1845 }
1846
1847 static void
1848 gtk_menu_realize (GtkWidget *widget)
1849 {
1850   GdkWindowAttr attributes;
1851   gint attributes_mask;
1852   gint border_width;
1853   GtkMenu *menu;
1854   GtkWidget *child;
1855   GList *children;
1856   guint vertical_padding;
1857   
1858   g_return_if_fail (GTK_IS_MENU (widget));
1859
1860   menu = GTK_MENU (widget);
1861   
1862   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1863   
1864   attributes.window_type = GDK_WINDOW_CHILD;
1865   attributes.x = widget->allocation.x;
1866   attributes.y = widget->allocation.y;
1867   attributes.width = widget->allocation.width;
1868   attributes.height = widget->allocation.height;
1869   attributes.wclass = GDK_INPUT_OUTPUT;
1870   attributes.visual = gtk_widget_get_visual (widget);
1871   attributes.colormap = gtk_widget_get_colormap (widget);
1872   
1873   attributes.event_mask = gtk_widget_get_events (widget);
1874
1875   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1876                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1877   
1878   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1879   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1880   gdk_window_set_user_data (widget->window, widget);
1881   
1882   border_width = GTK_CONTAINER (widget)->border_width;
1883
1884   gtk_widget_style_get (GTK_WIDGET (menu),
1885                         "vertical-padding", &vertical_padding,
1886                         NULL);
1887   
1888   attributes.x = border_width + widget->style->xthickness;
1889   attributes.y = border_width + widget->style->ythickness + vertical_padding;
1890   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1891   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1892
1893   if (menu->upper_arrow_visible)
1894     {
1895       attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1896       attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1897     }
1898   if (menu->lower_arrow_visible)
1899     attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1900
1901   menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1902   gdk_window_set_user_data (menu->view_window, menu);
1903
1904   attributes.x = 0;
1905   attributes.y = 0;
1906   attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1907   
1908   menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1909   gdk_window_set_user_data (menu->bin_window, menu);
1910
1911   children = GTK_MENU_SHELL (menu)->children;
1912   while (children)
1913     {
1914       child = children->data;
1915       children = children->next;
1916           
1917       gtk_widget_set_parent_window (child, menu->bin_window);
1918     }
1919   
1920   widget->style = gtk_style_attach (widget->style, widget->window);
1921   gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1922   gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1923   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1924
1925   if (GTK_MENU_SHELL (widget)->active_menu_item)
1926     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1927                                   GTK_MENU_SHELL (widget)->active_menu_item);
1928
1929   gdk_window_show (menu->bin_window);
1930   gdk_window_show (menu->view_window);
1931 }
1932
1933 static gboolean 
1934 gtk_menu_focus (GtkWidget       *widget,
1935                 GtkDirectionType direction)
1936 {
1937   /*
1938    * A menu or its menu items cannot have focus
1939    */
1940   return FALSE;
1941 }
1942
1943 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1944  */
1945 static GdkWindow *
1946 menu_grab_transfer_window_get (GtkMenu *menu)
1947 {
1948   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1949   if (!window)
1950     {
1951       GdkWindowAttr attributes;
1952       gint attributes_mask;
1953       
1954       attributes.x = -100;
1955       attributes.y = -100;
1956       attributes.width = 10;
1957       attributes.height = 10;
1958       attributes.window_type = GDK_WINDOW_TEMP;
1959       attributes.wclass = GDK_INPUT_ONLY;
1960       attributes.override_redirect = TRUE;
1961       attributes.event_mask = 0;
1962
1963       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1964       
1965       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1966                                &attributes, attributes_mask);
1967       gdk_window_set_user_data (window, menu);
1968
1969       gdk_window_show (window);
1970
1971       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1972     }
1973
1974   return window;
1975 }
1976
1977 static void
1978 menu_grab_transfer_window_destroy (GtkMenu *menu)
1979 {
1980   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1981   if (window)
1982     {
1983       gdk_window_set_user_data (window, NULL);
1984       gdk_window_destroy (window);
1985       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1986     }
1987 }
1988
1989 static void
1990 gtk_menu_unrealize (GtkWidget *widget)
1991 {
1992   GtkMenu *menu;
1993
1994   g_return_if_fail (GTK_IS_MENU (widget));
1995
1996   menu = GTK_MENU (widget);
1997
1998   menu_grab_transfer_window_destroy (menu);
1999
2000   gdk_window_set_user_data (menu->view_window, NULL);
2001   gdk_window_destroy (menu->view_window);
2002   menu->view_window = NULL;
2003
2004   gdk_window_set_user_data (menu->bin_window, NULL);
2005   gdk_window_destroy (menu->bin_window);
2006   menu->bin_window = NULL;
2007
2008   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2009 }
2010
2011 static void
2012 gtk_menu_size_request (GtkWidget      *widget,
2013                        GtkRequisition *requisition)
2014 {
2015   gint i;
2016   GtkMenu *menu;
2017   GtkMenuShell *menu_shell;
2018   GtkWidget *child;
2019   GList *children;
2020   guint max_toggle_size;
2021   guint max_accel_width;
2022   guint vertical_padding;
2023   GtkRequisition child_requisition;
2024   GtkMenuPrivate *priv;
2025   
2026   g_return_if_fail (GTK_IS_MENU (widget));
2027   g_return_if_fail (requisition != NULL);
2028   
2029   menu = GTK_MENU (widget);
2030   menu_shell = GTK_MENU_SHELL (widget);
2031   priv = gtk_menu_get_private (menu);
2032   
2033   requisition->width = 0;
2034   requisition->height = 0;
2035   
2036   max_toggle_size = 0;
2037   max_accel_width = 0;
2038   
2039   g_free (priv->heights);
2040   priv->heights = g_new0 (guint, priv->rows);
2041   priv->heights_length = priv->rows;
2042
2043   children = menu_shell->children;
2044   while (children)
2045     {
2046       gint part;
2047       gint toggle_size;
2048       guint l, r, t, b;
2049
2050       child = children->data;
2051       children = children->next;
2052       
2053       if (! GTK_WIDGET_VISIBLE (child))
2054         continue;
2055
2056       get_child_attach (child, &l, &r, &t, &b);
2057
2058       /* It's important to size_request the child
2059        * before doing the toggle size request, in
2060        * case the toggle size request depends on the size
2061        * request of a child of the child (e.g. for ImageMenuItem)
2062        */
2063
2064        GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2065        gtk_widget_size_request (child, &child_requisition);
2066
2067        gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2068        max_toggle_size = MAX (max_toggle_size, toggle_size);
2069        max_accel_width = MAX (max_accel_width,
2070                               GTK_MENU_ITEM (child)->accelerator_width);
2071
2072        part = child_requisition.width / (r - l);
2073        requisition->width = MAX (requisition->width, part);
2074
2075        part = child_requisition.height / (b - t);
2076        priv->heights[t] = MAX (priv->heights[t], part);
2077     }
2078
2079   for (i = 0; i < priv->rows; i++)
2080     requisition->height += priv->heights[i];
2081
2082   requisition->width += max_toggle_size + max_accel_width;
2083   requisition->width *= priv->columns;
2084   requisition->width += (GTK_CONTAINER (menu)->border_width +
2085                          widget->style->xthickness) * 2;
2086
2087   gtk_widget_style_get (GTK_WIDGET (menu),
2088                         "vertical-padding", &vertical_padding,
2089                         NULL);
2090   requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2091                           widget->style->ythickness) * 2;
2092   
2093   menu->toggle_size = max_toggle_size;
2094
2095   /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2096    */
2097   if (menu->tearoff_active)
2098     gtk_menu_set_tearoff_hints (menu, requisition->width);
2099 }
2100
2101 static void
2102 gtk_menu_size_allocate (GtkWidget     *widget,
2103                         GtkAllocation *allocation)
2104 {
2105   GtkMenu *menu;
2106   GtkMenuShell *menu_shell;
2107   GtkWidget *child;
2108   GtkAllocation child_allocation;
2109   GtkRequisition child_requisition;
2110   GtkMenuPrivate *priv;
2111   GList *children;
2112   gint x, y;
2113   gint width, height;
2114   guint vertical_padding;
2115
2116   g_return_if_fail (GTK_IS_MENU (widget));
2117   g_return_if_fail (allocation != NULL);
2118   
2119   menu = GTK_MENU (widget);
2120   menu_shell = GTK_MENU_SHELL (widget);
2121   priv = gtk_menu_get_private (menu);
2122
2123   widget->allocation = *allocation;
2124   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2125
2126   gtk_widget_style_get (GTK_WIDGET (menu),
2127                         "vertical-padding", &vertical_padding,
2128                         NULL);
2129   
2130   x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2131   y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2132
2133   width = MAX (1, allocation->width - x * 2);
2134   height = MAX (1, allocation->height - y * 2);
2135
2136   child_requisition.width -= x * 2;
2137   child_requisition.height -= y * 2;
2138
2139   if (menu_shell->active)
2140     gtk_menu_scroll_to (menu, menu->scroll_offset);
2141   
2142   if (menu->upper_arrow_visible && !menu->tearoff_active)
2143     {
2144       y += MENU_SCROLL_ARROW_HEIGHT;
2145       height -= MENU_SCROLL_ARROW_HEIGHT;
2146     }
2147   
2148   if (menu->lower_arrow_visible && !menu->tearoff_active)
2149     height -= MENU_SCROLL_ARROW_HEIGHT;
2150   
2151   if (GTK_WIDGET_REALIZED (widget))
2152     {
2153       gdk_window_move_resize (widget->window,
2154                               allocation->x, allocation->y,
2155                               allocation->width, allocation->height);
2156
2157       gdk_window_move_resize (menu->view_window,
2158                               x,
2159                               y,
2160                               width,
2161                               height);
2162     }
2163
2164   if (menu_shell->children)
2165     {
2166       gint base_width = width / priv->columns;
2167
2168       children = menu_shell->children;
2169       while (children)
2170         {
2171           child = children->data;
2172           children = children->next;
2173
2174           if (GTK_WIDGET_VISIBLE (child))
2175             {
2176               gint i;
2177               guint l, r, t, b;
2178
2179               get_child_attach (child, &l, &r, &t, &b);
2180
2181               if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2182                 {
2183                   guint tmp;
2184                   tmp = priv->columns - l;
2185                   l = priv->columns - r;
2186                   r = tmp;
2187                 }
2188
2189               child_allocation.width = (r - l) * base_width;
2190               child_allocation.height = 0;
2191               child_allocation.x = l * base_width;
2192               child_allocation.y = 0;
2193
2194               for (i = 0; i < b; i++)
2195                 {
2196                   if (i < t)
2197                     child_allocation.y += priv->heights[i];
2198                   else
2199                     child_allocation.height += priv->heights[i];
2200                 }
2201
2202               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2203                                                   menu->toggle_size);
2204
2205               gtk_widget_size_allocate (child, &child_allocation);
2206               gtk_widget_queue_draw (child);
2207             }
2208         }
2209       
2210       /* Resize the item window */
2211       if (GTK_WIDGET_REALIZED (widget))
2212         {
2213           gint i;
2214           gint width, height;
2215
2216           height = 0;
2217           for (i = 0; i < priv->rows; i++)
2218             height += priv->heights[i];
2219
2220           width = priv->columns * base_width;
2221           gdk_window_resize (menu->bin_window, width, height);
2222         }
2223
2224       if (menu->tearoff_active)
2225         {
2226           if (allocation->height >= widget->requisition.height)
2227             {
2228               if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2229                 {
2230                   gtk_widget_hide (menu->tearoff_scrollbar);
2231                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2232
2233                   gtk_menu_scroll_to (menu, 0);
2234                 }
2235             }
2236           else
2237             {
2238               menu->tearoff_adjustment->upper = widget->requisition.height;
2239               menu->tearoff_adjustment->page_size = allocation->height;
2240               
2241               if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2242                   menu->tearoff_adjustment->upper)
2243                 {
2244                   gint value;
2245                   value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2246                   if (value < 0)
2247                     value = 0;
2248                   gtk_menu_scroll_to (menu, value);
2249                 }
2250               
2251               gtk_adjustment_changed (menu->tearoff_adjustment);
2252               
2253               if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2254                 {
2255                   gtk_widget_show (menu->tearoff_scrollbar);
2256                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2257                 }
2258             }
2259         }
2260     }
2261 }
2262
2263 static void
2264 gtk_menu_paint (GtkWidget      *widget,
2265                 GdkEventExpose *event)
2266 {
2267   GtkMenu *menu;
2268   gint width, height;
2269   gint border_x, border_y;
2270   guint vertical_padding;
2271   
2272   g_return_if_fail (GTK_IS_MENU (widget));
2273
2274   menu = GTK_MENU (widget);
2275
2276   gtk_widget_style_get (GTK_WIDGET (menu),
2277                         "vertical-padding", &vertical_padding,
2278                         NULL);
2279   
2280   border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2281   border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2282   gdk_drawable_get_size (widget->window, &width, &height);
2283
2284   if (event->window == widget->window)
2285     {
2286       gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2287       gint arrow_size = 0.7 * arrow_space;
2288         
2289       gtk_paint_box (widget->style,
2290                      widget->window,
2291                      GTK_STATE_NORMAL,
2292                      GTK_SHADOW_OUT,
2293                      NULL, widget, "menu",
2294                      0, 0, -1, -1);
2295       if (menu->upper_arrow_visible && !menu->tearoff_active)
2296         {
2297           gtk_paint_box (widget->style,
2298                          widget->window,
2299                          menu->upper_arrow_prelight ?
2300                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2301                          GTK_SHADOW_OUT,
2302                          NULL, widget, "menu",
2303                          border_x,
2304                          border_y,
2305                          width - 2 * border_x,
2306                          MENU_SCROLL_ARROW_HEIGHT);
2307           
2308           gtk_paint_arrow (widget->style,
2309                            widget->window,
2310                            menu->upper_arrow_prelight ?
2311                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2312                            GTK_SHADOW_OUT,
2313                            NULL, widget, "menu_scroll_arrow_up",
2314                            GTK_ARROW_UP,
2315                            TRUE,
2316                            (width - arrow_size ) / 2,
2317                            border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2318                            arrow_size, arrow_size);
2319         }
2320   
2321       if (menu->lower_arrow_visible && !menu->tearoff_active)
2322         {
2323           gtk_paint_box (widget->style,
2324                          widget->window,
2325                          menu->lower_arrow_prelight ?
2326                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2327                          GTK_SHADOW_OUT,
2328                          NULL, widget, "menu",
2329                          border_x,
2330                          height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2331                          width - 2*border_x,
2332                          MENU_SCROLL_ARROW_HEIGHT);
2333           
2334           gtk_paint_arrow (widget->style,
2335                            widget->window,
2336                            menu->lower_arrow_prelight ?
2337                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2338                            GTK_SHADOW_OUT,
2339                            NULL, widget, "menu_scroll_arrow_down",
2340                            GTK_ARROW_DOWN,
2341                            TRUE,
2342                            (width - arrow_size) / 2,
2343                            height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2344                               widget->style->ythickness + (arrow_space - arrow_size)/2,
2345                            arrow_size, arrow_size);
2346         }
2347     }
2348 }
2349
2350 static gboolean
2351 gtk_menu_expose (GtkWidget      *widget,
2352                  GdkEventExpose *event)
2353 {
2354   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2355   g_return_val_if_fail (event != NULL, FALSE);
2356
2357   if (GTK_WIDGET_DRAWABLE (widget))
2358     {
2359       gtk_menu_paint (widget, event);
2360       
2361       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2362     }
2363   
2364   return FALSE;
2365 }
2366
2367 static void
2368 gtk_menu_show (GtkWidget *widget)
2369 {
2370   GtkMenu *menu = GTK_MENU (widget);
2371
2372   _gtk_menu_refresh_accel_paths (menu, FALSE);
2373
2374   GTK_WIDGET_CLASS (parent_class)->show (widget);
2375 }
2376
2377 static gboolean
2378 gtk_menu_button_press (GtkWidget      *widget,
2379                          GdkEventButton *event)
2380 {
2381   /* Don't pop down the menu for releases over scroll arrows
2382    */
2383   if (GTK_IS_MENU (widget))
2384     {
2385       GtkMenu *menu = GTK_MENU (widget);
2386
2387       if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
2388         return TRUE;
2389     }
2390
2391   return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2392 }
2393
2394 static gboolean
2395 gtk_menu_button_release (GtkWidget      *widget,
2396                          GdkEventButton *event)
2397 {
2398   /* Don't pop down the menu for releases over scroll arrows
2399    */
2400   if (GTK_IS_MENU (widget))
2401     {
2402       GtkMenu *menu = GTK_MENU (widget);
2403
2404       if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
2405         return TRUE;
2406     }
2407
2408   return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2409 }
2410
2411 static const gchar *
2412 get_accel_path (GtkWidget *menu_item,
2413                 gboolean  *locked)
2414 {
2415   const gchar *path;
2416   GtkWidget *label;
2417   GClosure *accel_closure;
2418   GtkAccelGroup *accel_group;    
2419
2420   path = _gtk_widget_get_accel_path (menu_item, locked);
2421   if (!path)
2422     {
2423       path = GTK_MENU_ITEM (menu_item)->accel_path;
2424       
2425       if (locked)
2426         {
2427           *locked = TRUE;
2428
2429           label = GTK_BIN (menu_item)->child;
2430           
2431           if (GTK_IS_ACCEL_LABEL (label))
2432             {
2433               g_object_get (label, 
2434                             "accel_closure", &accel_closure, 
2435                             NULL);
2436               accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2437               
2438               *locked = accel_group->lock_count > 0;
2439             }
2440         }
2441     }
2442
2443   return path;
2444 }
2445
2446 static gboolean
2447 gtk_menu_key_press (GtkWidget   *widget,
2448                     GdkEventKey *event)
2449 {
2450   GtkMenuShell *menu_shell;
2451   GtkMenu *menu;
2452   gboolean delete = FALSE;
2453   gboolean can_change_accels;
2454   gchar *accel = NULL;
2455   guint accel_key, accel_mods;
2456   GdkModifierType consumed_modifiers;
2457   GdkDisplay *display;
2458   
2459   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2460   g_return_val_if_fail (event != NULL, FALSE);
2461       
2462   menu_shell = GTK_MENU_SHELL (widget);
2463   menu = GTK_MENU (widget);
2464   
2465   gtk_menu_stop_navigating_submenu (menu);
2466
2467   if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2468     return TRUE;
2469
2470   display = gtk_widget_get_display (widget);
2471     
2472   g_object_get (gtk_widget_get_settings (widget),
2473                 "gtk-menu-bar-accel", &accel,
2474                 "gtk-can-change-accels", &can_change_accels,
2475                 NULL);
2476
2477   if (accel)
2478     {
2479       guint keyval = 0;
2480       GdkModifierType mods = 0;
2481       gboolean handled = FALSE;
2482       
2483       gtk_accelerator_parse (accel, &keyval, &mods);
2484
2485       if (keyval == 0)
2486         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2487
2488       /* FIXME this is wrong, needs to be in the global accel resolution
2489        * thing, to properly consider i18n etc., but that probably requires
2490        * AccelGroup changes etc.
2491        */
2492       if (event->keyval == keyval &&
2493           (mods & event->state) == mods)
2494         gtk_menu_shell_cancel (menu_shell);
2495
2496       g_free (accel);
2497
2498       if (handled)
2499         return TRUE;
2500     }
2501   
2502   switch (event->keyval)
2503     {
2504     case GDK_Delete:
2505     case GDK_KP_Delete:
2506     case GDK_BackSpace:
2507       delete = TRUE;
2508       break;
2509     default:
2510       break;
2511     }
2512
2513   /* Figure out what modifiers went into determining the key symbol */
2514   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2515                                        event->hardware_keycode, event->state, event->group,
2516                                        NULL, NULL, NULL, &consumed_modifiers);
2517
2518   accel_key = gdk_keyval_to_lower (event->keyval);
2519   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2520
2521   /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2522    * We re-upper case when we match against the keyval, but display and save in caseless form.
2523    */
2524   if (accel_key != event->keyval)
2525     accel_mods |= GDK_SHIFT_MASK;
2526   
2527   /* Modify the accelerators */
2528   if (can_change_accels &&
2529       menu_shell->active_menu_item &&
2530       GTK_BIN (menu_shell->active_menu_item)->child &&                  /* no separators */
2531       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&  /* no submenus */
2532       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2533     {
2534       GtkWidget *menu_item = menu_shell->active_menu_item;
2535       gboolean locked, replace_accels = TRUE;
2536       const gchar *path;
2537
2538       path = get_accel_path (menu_item, &locked);
2539       if (!path || locked)
2540         {
2541           /* can't change accelerators on menu_items without paths
2542            * (basically, those items are accelerator-locked).
2543            */
2544           /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2545           gdk_display_beep (display);
2546         }
2547       else
2548         {
2549           gboolean changed;
2550
2551           /* For the keys that act to delete the current setting, we delete
2552            * the current setting if there is one, otherwise, we set the
2553            * key as the accelerator.
2554            */
2555           if (delete)
2556             {
2557               GtkAccelKey key;
2558               
2559               if (gtk_accel_map_lookup_entry (path, &key) &&
2560                   (key.accel_key || key.accel_mods))
2561                 {
2562                   accel_key = 0;
2563                   accel_mods = 0;
2564                 }
2565             }
2566           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2567
2568           if (!changed)
2569             {
2570               /* we failed, probably because this key is in use and
2571                * locked already
2572                */
2573               /* g_print("failed to change\n"); */
2574               gdk_display_beep (display);
2575             }
2576         }
2577     }
2578   
2579   return TRUE;
2580 }
2581
2582 static gboolean
2583 gtk_menu_motion_notify  (GtkWidget         *widget,
2584                          GdkEventMotion    *event)
2585 {
2586   GtkWidget *menu_item;
2587   GtkMenu *menu;
2588   GtkMenuShell *menu_shell;
2589
2590   gboolean need_enter;
2591
2592   if (GTK_IS_MENU (widget))
2593     gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2594
2595   /* We received the event for one of two reasons:
2596    *
2597    * a) We are the active menu, and did gtk_grab_add()
2598    * b) The widget is a child of ours, and the event was propagated
2599    *
2600    * Since for computation of navigation regions, we want the menu which
2601    * is the parent of the menu item, for a), we need to find that menu,
2602    * which may be different from 'widget'.
2603    */
2604   menu_item = gtk_get_event_widget ((GdkEvent*) event);
2605   if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2606       !_gtk_menu_item_is_selectable (menu_item) ||
2607       !GTK_IS_MENU (menu_item->parent))
2608     return FALSE;
2609
2610   menu_shell = GTK_MENU_SHELL (menu_item->parent);
2611   menu = GTK_MENU (menu_shell);
2612   
2613   need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2614
2615   /* Check to see if we are within an active submenu's navigation region
2616    */
2617   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2618     return TRUE; 
2619
2620   if (need_enter)
2621     {
2622       /* The menu is now sensitive to enter events on its items, but
2623        * was previously sensitive.  So we fake an enter event.
2624        */
2625       gint width, height;
2626       
2627       menu_shell->ignore_enter = FALSE; 
2628       
2629       gdk_drawable_get_size (event->window, &width, &height);
2630       if (event->x >= 0 && event->x < width &&
2631           event->y >= 0 && event->y < height)
2632         {
2633           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2634           gboolean result;
2635
2636           send_event->crossing.window = g_object_ref (event->window);
2637           send_event->crossing.time = event->time;
2638           send_event->crossing.send_event = TRUE;
2639           send_event->crossing.x_root = event->x_root;
2640           send_event->crossing.y_root = event->y_root;
2641           send_event->crossing.x = event->x;
2642           send_event->crossing.y = event->y;
2643
2644           /* We send the event to 'widget', the currently active menu,
2645            * instead of 'menu', the menu that the pointer is in. This
2646            * will ensure that the event will be ignored unless the
2647            * menuitem is a child of the active menu or some parent
2648            * menu of the active menu.
2649            */
2650           result = gtk_widget_event (widget, send_event);
2651           gdk_event_free (send_event);
2652
2653           return result;
2654         }
2655     }
2656
2657   return FALSE;
2658 }
2659
2660 static gboolean
2661 gtk_menu_scroll_timeout (gpointer  data)
2662 {
2663   GtkMenu *menu;
2664   GtkWidget *widget;
2665   gint offset;
2666   gint view_width, view_height;
2667
2668   GDK_THREADS_ENTER ();
2669
2670   menu = GTK_MENU (data);
2671   widget = GTK_WIDGET (menu);
2672
2673   offset = menu->scroll_offset + menu->scroll_step;
2674
2675   /* If we scroll upward and the non-visible top part
2676    * is smaller than the scroll arrow it would be
2677    * pretty stupid to show the arrow and taking more
2678    * screen space than just scrolling to the top.
2679    */
2680   if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2681     offset = 0;
2682
2683   /* Don't scroll over the top if we weren't before: */
2684   if ((menu->scroll_offset >= 0) && (offset < 0))
2685     offset = 0;
2686
2687   gdk_drawable_get_size (widget->window, &view_width, &view_height);
2688
2689   /* Don't scroll past the bottom if we weren't before: */
2690   if (menu->scroll_offset > 0)
2691     view_height -= MENU_SCROLL_ARROW_HEIGHT;
2692   
2693   if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2694       (offset + view_height > widget->requisition.height))
2695     offset = widget->requisition.height - view_height;
2696
2697   gtk_menu_scroll_to (menu, offset);
2698
2699   GDK_THREADS_LEAVE ();
2700
2701   return TRUE;
2702 }
2703
2704 static void
2705 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2706 {
2707   GtkMenuShell *menu_shell;
2708   gint width, height;
2709   gint x, y;
2710   gint border;
2711   GdkRectangle rect;
2712   gboolean in_arrow;
2713   gboolean scroll_fast = FALSE;
2714   guint vertical_padding;
2715
2716   menu_shell = GTK_MENU_SHELL (menu);
2717
2718   gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2719   gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2720
2721   gtk_widget_style_get (GTK_WIDGET (menu),
2722                         "vertical-padding", &vertical_padding,
2723                         NULL);
2724   
2725   border = GTK_CONTAINER (menu)->border_width +
2726     GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2727
2728   if (menu->upper_arrow_visible && !menu->tearoff_active)
2729     {
2730       rect.x = 0;
2731       rect.y = 0;
2732       rect.width = width;
2733       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2734       
2735       in_arrow = FALSE;
2736       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2737           (y >= rect.y) && (y < rect.y + rect.height))
2738         {
2739           in_arrow = TRUE;
2740           scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2741         }
2742         
2743       if (enter && in_arrow &&
2744           (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2745         {
2746           menu->upper_arrow_prelight = TRUE;
2747           menu->scroll_fast = scroll_fast;
2748           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2749           
2750           /* Deselect the active item so that any submenus are poped down */
2751           gtk_menu_shell_deselect (menu_shell);
2752
2753           gtk_menu_remove_scroll_timeout (menu);
2754           menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2755           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2756                                             gtk_menu_scroll_timeout,
2757                                             menu);
2758         }
2759       else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2760         {
2761           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2762           
2763           gtk_menu_stop_scrolling (menu);
2764         }
2765     }
2766   
2767   if (menu->lower_arrow_visible && !menu->tearoff_active)
2768     {
2769       rect.x = 0;
2770       rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2771       rect.width = width;
2772       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2773
2774       in_arrow = FALSE;
2775       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2776           (y >= rect.y) && (y < rect.y + rect.height))
2777         {
2778           in_arrow = TRUE;
2779           scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2780         }
2781
2782       if (enter && in_arrow &&
2783           (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2784         {
2785           menu->lower_arrow_prelight = TRUE;
2786           menu->scroll_fast = scroll_fast;
2787           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2788
2789           /* Deselect the active item so that any submenus are poped down */
2790           gtk_menu_shell_deselect (menu_shell);
2791
2792           gtk_menu_remove_scroll_timeout (menu);
2793           menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2794           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2795                                             gtk_menu_scroll_timeout,
2796                                             menu);
2797         }
2798       else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2799         {
2800           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2801           
2802           gtk_menu_stop_scrolling (menu);
2803         }
2804     }
2805 }
2806
2807 static gboolean
2808 gtk_menu_enter_notify (GtkWidget        *widget,
2809                        GdkEventCrossing *event)
2810 {
2811   GtkWidget *menu_item;
2812
2813   if (widget && GTK_IS_MENU (widget))
2814     {
2815       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2816
2817       if (!menu_shell->ignore_enter)
2818         gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2819     }
2820   
2821   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2822    * will not correspond to the event widget's parent.  Check to see
2823    * if we are in the parent's navigation region.
2824    */
2825   menu_item = gtk_get_event_widget ((GdkEvent*) event);
2826   if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2827       gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2828     return TRUE;
2829
2830   return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event); 
2831 }
2832
2833 static gboolean
2834 gtk_menu_leave_notify (GtkWidget        *widget,
2835                        GdkEventCrossing *event)
2836 {
2837   GtkMenuShell *menu_shell;
2838   GtkMenu *menu;
2839   GtkMenuItem *menu_item;
2840   GtkWidget *event_widget;
2841
2842   menu = GTK_MENU (widget);
2843   menu_shell = GTK_MENU_SHELL (widget); 
2844   
2845   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2846     return TRUE; 
2847
2848   gtk_menu_handle_scrolling (menu, FALSE);
2849   
2850   event_widget = gtk_get_event_widget ((GdkEvent*) event);
2851   
2852   if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2853     return TRUE;
2854   
2855   menu_item = GTK_MENU_ITEM (event_widget); 
2856   
2857   /* Here we check to see if we're leaving an active menu item with a submenu, 
2858    * in which case we enter submenu navigation mode. 
2859    */
2860   if (menu_shell->active_menu_item != NULL
2861       && menu_item->submenu != NULL
2862       && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2863     {
2864       if (GTK_MENU_SHELL (menu_item->submenu)->active)
2865         {
2866           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2867           return TRUE;
2868         }
2869     }
2870   
2871   return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); 
2872 }
2873
2874 static void 
2875 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2876 {
2877   if (menu->navigation_region) 
2878     {
2879       gdk_region_destroy (menu->navigation_region);
2880       menu->navigation_region = NULL;
2881     }
2882   
2883   if (menu->navigation_timeout)
2884     {
2885       g_source_remove (menu->navigation_timeout);
2886       menu->navigation_timeout = 0;
2887     }
2888 }
2889
2890 /* When the timeout is elapsed, the navigation region is destroyed
2891  * and the menuitem under the pointer (if any) is selected.
2892  */
2893 static gboolean
2894 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2895 {
2896   GtkMenu *menu = user_data;
2897   GdkWindow *child_window;
2898
2899   GDK_THREADS_ENTER ();
2900
2901   gtk_menu_stop_navigating_submenu (menu);
2902   
2903   if (GTK_WIDGET_REALIZED (menu))
2904     {
2905       child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2906
2907       if (child_window)
2908         {
2909           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2910
2911           send_event->crossing.window = g_object_ref (child_window);
2912           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2913           send_event->crossing.send_event = TRUE;
2914
2915           GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2916
2917           gdk_event_free (send_event);
2918         }
2919     }
2920
2921   GDK_THREADS_LEAVE ();
2922
2923   return FALSE; 
2924 }
2925
2926 static gboolean
2927 gtk_menu_navigating_submenu (GtkMenu *menu,
2928                              gint     event_x,
2929                              gint     event_y)
2930 {
2931   if (menu->navigation_region)
2932     {
2933       if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2934         return TRUE;
2935       else
2936         {
2937           gtk_menu_stop_navigating_submenu (menu);
2938           return FALSE;
2939         }
2940     }
2941   return FALSE;
2942 }
2943
2944 #undef DRAW_STAY_UP_TRIANGLE
2945
2946 #ifdef DRAW_STAY_UP_TRIANGLE
2947
2948 static void
2949 draw_stay_up_triangle (GdkWindow *window,
2950                        GdkRegion *region)
2951 {
2952   /* Draw ugly color all over the stay-up triangle */
2953   GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2954   GdkGCValues gc_values;
2955   GdkGC *ugly_gc;
2956   GdkRectangle clipbox;
2957
2958   gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2959   ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2960   gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2961   gdk_gc_set_clip_region (ugly_gc, region);
2962
2963   gdk_region_get_clipbox (region, &clipbox);
2964   
2965   gdk_draw_rectangle (window,
2966                      ugly_gc,
2967                      TRUE,
2968                      clipbox.x, clipbox.y,
2969                      clipbox.width, clipbox.height);
2970   
2971   g_object_unref (ugly_gc);
2972 }
2973 #endif
2974
2975 static GdkRegion *
2976 flip_region (GdkRegion *region,
2977              gboolean   flip_x,
2978              gboolean   flip_y)
2979 {
2980   gint n_rectangles;
2981   GdkRectangle *rectangles;
2982   GdkRectangle clipbox;
2983   GdkRegion *new_region;
2984   gint i;
2985
2986   new_region = gdk_region_new ();
2987   
2988   gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2989   gdk_region_get_clipbox (region, &clipbox);
2990
2991   for (i = 0; i < n_rectangles; ++i)
2992     {
2993       GdkRectangle rect = rectangles[i];
2994
2995       if (flip_y)
2996         rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2997
2998       if (flip_x)
2999         rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3000
3001       gdk_region_union_with_rect (new_region, &rect);
3002     }
3003
3004   g_free (rectangles);
3005
3006   return new_region;
3007 }
3008
3009 static void
3010 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
3011                                         GtkMenuItem      *menu_item,
3012                                         GdkEventCrossing *event)
3013 {
3014   gint submenu_left = 0;
3015   gint submenu_right = 0;
3016   gint submenu_top = 0;
3017   gint submenu_bottom = 0;
3018   gint width = 0;
3019   gint height = 0;
3020   GdkPoint point[3];
3021   GtkWidget *event_widget;
3022
3023   g_return_if_fail (menu_item->submenu != NULL);
3024   g_return_if_fail (event != NULL);
3025   
3026   event_widget = gtk_get_event_widget ((GdkEvent*) event);
3027   
3028   gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3029   gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3030   
3031   submenu_right = submenu_left + width;
3032   submenu_bottom = submenu_top + height;
3033   
3034   gdk_drawable_get_size (event_widget->window, &width, &height);
3035   
3036   if (event->x >= 0 && event->x < width)
3037     {
3038       gint popdown_delay;
3039       gboolean flip_y = FALSE;
3040       gboolean flip_x = FALSE;
3041       
3042       gtk_menu_stop_navigating_submenu (menu);
3043
3044       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3045         {
3046           /* right */
3047           point[0].x = event->x_root;
3048           point[1].x = submenu_left;
3049         }
3050       else
3051         {
3052           /* left */
3053           point[0].x = event->x_root + 1;
3054           point[1].x = 2 * (event->x_root + 1) - submenu_right;
3055
3056           flip_x = TRUE;
3057         }
3058
3059       if (event->y < 0)
3060         {
3061           /* top */
3062           point[0].y = event->y_root + 1;
3063           point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3064
3065           if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3066             return;
3067
3068           flip_y = TRUE;
3069         }
3070       else
3071         {
3072           /* bottom */
3073           point[0].y = event->y_root;
3074           point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3075
3076           if (point[0].y >= submenu_bottom)
3077             return;
3078         }
3079
3080       point[2].x = point[1].x;
3081       point[2].y = point[0].y;
3082
3083       menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3084
3085       if (flip_x || flip_y)
3086         {
3087           GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3088           gdk_region_destroy (menu->navigation_region);
3089           menu->navigation_region = new_region;
3090         }
3091
3092       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3093                     "gtk-menu-popdown-delay", &popdown_delay,
3094                     NULL);
3095
3096       menu->navigation_timeout = g_timeout_add (popdown_delay,
3097                                                 gtk_menu_stop_navigating_submenu_cb, menu);
3098
3099 #ifdef DRAW_STAY_UP_TRIANGLE
3100       draw_stay_up_triangle (gdk_get_default_root_window(),
3101                              menu->navigation_region);
3102 #endif
3103     }
3104 }
3105
3106 static void
3107 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3108 {
3109   GtkWidget *parent;
3110   
3111   g_return_if_fail (GTK_IS_MENU (menu_shell));
3112   
3113   parent = menu_shell->parent_menu_shell;
3114   
3115   menu_shell->activate_time = 0;
3116   gtk_menu_popdown (GTK_MENU (menu_shell));
3117   
3118   if (parent)
3119     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3120 }
3121
3122 static void
3123 gtk_menu_position (GtkMenu *menu)
3124 {
3125   GtkWidget *widget;
3126   GtkRequisition requisition;
3127   GtkMenuPrivate *private;
3128   gint x, y;
3129   gint scroll_offset;
3130   gint menu_height;
3131   gboolean push_in;
3132   GdkScreen *screen;
3133   GdkScreen *pointer_screen;
3134   GdkRectangle monitor;
3135
3136   g_return_if_fail (GTK_IS_MENU (menu));
3137
3138   widget = GTK_WIDGET (menu);
3139
3140   screen = gtk_widget_get_screen (widget);
3141   gdk_display_get_pointer (gdk_screen_get_display (screen),
3142                            &pointer_screen, &x, &y, NULL);
3143
3144   /* We need the requisition to figure out the right place to
3145    * popup the menu. In fact, we always need to ask here, since
3146    * if a size_request was queued while we weren't popped up,
3147    * the requisition won't have been recomputed yet.
3148    */
3149   gtk_widget_size_request (widget, &requisition);
3150
3151   if (pointer_screen != screen)
3152     {
3153       /* Pointer is on a different screen; roughly center the
3154        * menu on the screen. If someone was using multiscreen
3155        * + Xinerama together they'd probably want something
3156        * fancier; but that is likely to be vanishingly rare.
3157        */
3158       x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3159       y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3160     }
3161
3162   private = gtk_menu_get_private (menu);
3163   private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3164
3165   push_in = FALSE;
3166   
3167   if (menu->position_func)
3168     {
3169       (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3170       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3171     }
3172   else
3173     {
3174       gint space_left, space_right, space_above, space_below;
3175       gint needed_width;
3176       gint needed_height;
3177       gint xthickness = widget->style->xthickness;
3178       gint ythickness = widget->style->ythickness;
3179       gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3180
3181       /* The placement of popup menus horizontally works like this (with
3182        * RTL in parentheses)
3183        *
3184        * - If there is enough room to the right (left) of the mouse cursor,
3185        *   position the menu there.
3186        * 
3187        * - Otherwise, if if there is enough room to the left (right) of the 
3188        *   mouse cursor, position the menu there.
3189        * 
3190        * - Otherwise if the menu is smaller than the monitor, position it
3191        *   on the side of the mouse cursor that has the most space available
3192        *
3193        * - Otherwise (if there is simply not enough room for the menu on the
3194        *   monitor), position it as far left (right) as possible.
3195        *
3196        * Positioning in the vertical direction is similar: first try below
3197        * mouse cursor, then above.
3198        */
3199       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3200
3201       space_left = x - monitor.x;
3202       space_right = monitor.x + monitor.width - x - 1;
3203       space_above = y - monitor.y;
3204       space_below = monitor.y + monitor.height - y - 1;
3205
3206       /* position horizontally */
3207
3208       /* the amount of space we need to position the menu. Note the
3209        * menu is offset "xthickness" pixels 
3210        */
3211       needed_width = requisition.width - xthickness;
3212
3213       if (needed_width <= space_left ||
3214           needed_width <= space_right)
3215         {
3216           if ((rtl  && needed_width <= space_left) ||
3217               (!rtl && needed_width >  space_right))
3218             {
3219               /* position left */
3220               x = x + xthickness - requisition.width + 1;
3221             }
3222           else
3223             {
3224               /* position right */
3225               x = x - xthickness;
3226             }
3227
3228           /* x is clamped on-screen further down */
3229         }
3230       else if (requisition.width <= monitor.width)
3231         {
3232           /* the menu is too big to fit on either side of the mouse
3233            * cursor, but smaller than the monitor. Position it on
3234            * the side that has the most space
3235            */
3236           if (space_left > space_right)
3237             {
3238               /* left justify */
3239               x = monitor.x;
3240             }
3241           else
3242             {
3243               /* right justify */
3244               x = monitor.x + monitor.width - requisition.width;
3245             }
3246         }
3247       else /* menu is simply too big for the monitor */
3248         {
3249           if (rtl)
3250             {
3251               /* right justify */
3252               x = monitor.x + monitor.width - requisition.width;
3253             }
3254           else
3255             {
3256               /* left justify */
3257               x = monitor.x;
3258             }
3259         }
3260
3261       /* Position vertically. The algorithm is the same as above, but
3262        * simpler because we don't have to take RTL into account.
3263        */
3264       needed_height = requisition.height - ythickness;
3265
3266       if (needed_height <= space_above ||
3267           needed_height <= space_below)
3268         {
3269           if (needed_height <= space_below)
3270             y = y - ythickness;
3271           else
3272             y = y + ythickness - requisition.height + 1;
3273           
3274           y = CLAMP (y, monitor.y,
3275                      monitor.y + monitor.height - requisition.height);
3276         }
3277       else if (needed_height > space_below && needed_height > space_above)
3278         {
3279           if (space_below >= space_above)
3280             y = monitor.y + monitor.height - requisition.height;
3281           else
3282             y = monitor.y;
3283         }
3284       else
3285         {
3286           y = monitor.y;
3287         }
3288     }
3289
3290   scroll_offset = 0;
3291
3292   if (push_in)
3293     {
3294       menu_height = GTK_WIDGET (menu)->requisition.height;
3295
3296       if (y + menu_height > monitor.y + monitor.height)
3297         {
3298           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3299           y = (monitor.y + monitor.height) - menu_height;
3300         }
3301   
3302       if (y < monitor.y)
3303         {
3304           scroll_offset += monitor.y - y;
3305           y = monitor.y;
3306         }
3307     }
3308
3309   /* FIXME: should this be done in the various position_funcs ? */
3310   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3311  
3312   if (GTK_MENU_SHELL (menu)->active)
3313     {
3314       private->have_position = TRUE;
3315       private->x = x;
3316       private->y = y;
3317     }
3318   
3319   if (y + requisition.height > monitor.y + monitor.height)
3320     requisition.height = (monitor.y + monitor.height) - y;
3321   
3322   if (y < monitor.y)
3323     {
3324       scroll_offset += monitor.y - y;
3325       requisition.height -= monitor.y - y;
3326       y = monitor.y;
3327     }
3328
3329   if (scroll_offset > 0)
3330     scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3331   
3332   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
3333                    x, y);
3334
3335   if (!GTK_MENU_SHELL (menu)->active)
3336     {
3337       gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3338                          requisition.width, requisition.height);
3339     }
3340   
3341   menu->scroll_offset = scroll_offset;
3342 }
3343
3344 static void
3345 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3346 {
3347   if (menu->timeout_id)
3348     {
3349       g_source_remove (menu->timeout_id);
3350       menu->timeout_id = 0;
3351     }
3352 }
3353
3354 static void
3355 gtk_menu_stop_scrolling (GtkMenu *menu)
3356 {
3357   gtk_menu_remove_scroll_timeout (menu);
3358
3359   menu->upper_arrow_prelight = FALSE;
3360   menu->lower_arrow_prelight = FALSE;
3361 }
3362
3363 static void
3364 gtk_menu_scroll_to (GtkMenu *menu,
3365                     gint    offset)
3366 {
3367   GtkWidget *widget;
3368   gint x, y;
3369   gint view_width, view_height;
3370   gint border_width;
3371   gboolean last_visible;
3372   gint menu_height;
3373   guint vertical_padding;
3374
3375   widget = GTK_WIDGET (menu);
3376
3377   if (menu->tearoff_active &&
3378       menu->tearoff_adjustment &&
3379       (menu->tearoff_adjustment->value != offset))
3380     {
3381       menu->tearoff_adjustment->value =
3382         CLAMP (offset,
3383                0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3384       gtk_adjustment_value_changed (menu->tearoff_adjustment);
3385     }
3386   
3387   /* Move/resize the viewport according to arrows: */
3388   view_width = widget->allocation.width;
3389   view_height = widget->allocation.height;
3390
3391   gtk_widget_style_get (GTK_WIDGET (menu),
3392                         "vertical-padding", &vertical_padding,
3393                         NULL);
3394   
3395   border_width = GTK_CONTAINER (menu)->border_width;
3396   view_width -= (border_width + widget->style->xthickness) * 2;
3397   view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3398   menu_height = widget->requisition.height -
3399       (border_width + widget->style->ythickness + vertical_padding) * 2;
3400
3401   x = border_width + widget->style->xthickness;
3402   y = border_width + widget->style->ythickness + vertical_padding;
3403
3404   if (!menu->tearoff_active)
3405     {
3406       last_visible = menu->upper_arrow_visible;
3407       menu->upper_arrow_visible = offset > 0;
3408       
3409       if (menu->upper_arrow_visible)
3410         view_height -= MENU_SCROLL_ARROW_HEIGHT;
3411       
3412       if ( (last_visible != menu->upper_arrow_visible) &&
3413            !menu->upper_arrow_visible)
3414         {
3415           menu->upper_arrow_prelight = FALSE;
3416           
3417           /* If we hid the upper arrow, possibly remove timeout */
3418           if (menu->scroll_step < 0)
3419             {
3420               gtk_menu_stop_scrolling (menu);
3421               gtk_widget_queue_draw (GTK_WIDGET (menu));
3422             }
3423         }
3424
3425       last_visible = menu->lower_arrow_visible;
3426       menu->lower_arrow_visible = offset < menu_height - view_height;
3427       
3428       if (menu->lower_arrow_visible)
3429         view_height -= MENU_SCROLL_ARROW_HEIGHT;
3430       
3431       if ( (last_visible != menu->lower_arrow_visible) &&
3432            !menu->lower_arrow_visible)
3433         {
3434           menu->lower_arrow_prelight = FALSE;
3435           
3436           /* If we hid the lower arrow, possibly remove timeout */
3437           if (menu->scroll_step > 0)
3438             {
3439               gtk_menu_stop_scrolling (menu);
3440               gtk_widget_queue_draw (GTK_WIDGET (menu));
3441             }
3442         }
3443       
3444       if (menu->upper_arrow_visible)
3445         y += MENU_SCROLL_ARROW_HEIGHT;
3446     }
3447
3448   /* Scroll the menu: */
3449   if (GTK_WIDGET_REALIZED (menu))
3450     gdk_window_move (menu->bin_window, 0, -offset);
3451
3452   if (GTK_WIDGET_REALIZED (menu))
3453     gdk_window_move_resize (menu->view_window,
3454                             x,
3455                             y,
3456                             view_width,
3457                             view_height);
3458
3459   menu->scroll_offset = offset;
3460 }
3461
3462 static gboolean
3463 compute_child_offset (GtkMenu   *menu,
3464                       GtkWidget *menu_item,
3465                       gint      *offset,
3466                       gint      *height,
3467                       gboolean  *is_last_child)
3468 {
3469   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3470   guint item_top_attach;
3471   guint item_bottom_attach;
3472   gint child_offset = 0;
3473   gint i;
3474
3475   gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3476                            "top_attach", &item_top_attach,
3477                            "bottom_attach", &item_bottom_attach,
3478                            NULL);
3479
3480   /* there is a possibility that we get called before _size_request, so
3481    * check the height table for safety.
3482    */
3483   if (!priv->heights || priv->heights_length < priv->rows)
3484     return FALSE;
3485
3486   /* when we have a row with only invisible children, it's height will
3487    * be zero, so there's no need to check WIDGET_VISIBLE here
3488    */
3489   for (i = 0; i < item_top_attach; i++)
3490     child_offset += priv->heights[i];
3491
3492   if (is_last_child)
3493     *is_last_child = (item_bottom_attach == priv->rows);
3494   if (offset)
3495     *offset = child_offset;
3496   if (height)
3497     *height = priv->heights[item_top_attach];
3498
3499   return TRUE;
3500 }
3501
3502 static void
3503 gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
3504                               GtkWidget       *menu_item)
3505 {
3506   GtkMenu *menu;
3507   gint child_offset, child_height;
3508   gint width, height;
3509   gint y;
3510   gint arrow_height;
3511   gboolean last_child = 0;
3512   
3513   menu = GTK_MENU (menu_shell);
3514
3515   /* We need to check if the selected item fully visible.
3516    * If not we need to scroll the menu so that it becomes fully
3517    * visible.
3518    */
3519
3520   if (compute_child_offset (menu, menu_item,
3521                             &child_offset, &child_height, &last_child))
3522     {
3523       guint vertical_padding;
3524       
3525       y = menu->scroll_offset;
3526       gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3527
3528       gtk_widget_style_get (GTK_WIDGET (menu),
3529                             "vertical-padding", &vertical_padding,
3530                             NULL);
3531                             
3532       height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3533       
3534       if (child_offset < y)
3535         {
3536           /* Ignore the enter event we might get if the pointer is on the menu
3537            */
3538           menu_shell->ignore_enter = TRUE;
3539           gtk_menu_scroll_to (menu, child_offset);
3540         }
3541       else
3542         {
3543           arrow_height = 0;
3544           if (menu->upper_arrow_visible && !menu->tearoff_active)
3545             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3546           if (menu->lower_arrow_visible && !menu->tearoff_active)
3547             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3548           
3549           if (child_offset + child_height > y + height - arrow_height)
3550             {
3551               arrow_height = 0;
3552               if (!last_child && !menu->tearoff_active)
3553                 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3554               
3555               y = child_offset + child_height - height + arrow_height;
3556               if ((y > 0) && !menu->tearoff_active)
3557                 {
3558                   /* Need upper arrow */
3559                   arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3560                   y = child_offset + child_height - height + arrow_height;
3561                 }
3562               /* Ignore the enter event we might get if the pointer is on the menu
3563                */
3564               menu_shell->ignore_enter = TRUE;
3565               gtk_menu_scroll_to (menu, y);
3566             }
3567         }    
3568       
3569     }
3570 }
3571
3572 static void
3573 gtk_menu_select_item (GtkMenuShell  *menu_shell,
3574                       GtkWidget     *menu_item)
3575 {
3576   GtkMenu *menu = GTK_MENU (menu_shell);
3577
3578   if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3579     gtk_menu_scroll_item_visible (menu_shell, menu_item);
3580
3581   GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3582 }
3583
3584
3585 /* Reparent the menu, taking care of the refcounting
3586  *
3587  * If unrealize is true we force a unrealize while reparenting the parent.
3588  * This can help eliminate flicker in some cases.
3589  *
3590  * What happens is that when the menu is unrealized and then re-realized,
3591  * the allocations are as follows:
3592  *
3593  *  parent - 1x1 at (0,0) 
3594  *  child1 - 100x20 at (0,0)
3595  *  child2 - 100x20 at (0,20)
3596  *  child3 - 100x20 at (0,40)
3597  *
3598  * That is, the parent is small but the children are full sized. Then,
3599  * when the queued_resize gets processed, the parent gets resized to
3600  * full size. 
3601  *
3602  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3603  * contains the following logic:
3604  * 
3605  * - if a move or resize operation on a window would change the clip 
3606  *   region on the children, then before the window is resized
3607  *   the background for children is temporarily set to None, the
3608  *   move/resize done, and the background for the children restored.
3609  *
3610  * So, at the point where the parent is resized to final size, the
3611  * background for the children is temporarily None, and thus they
3612  * are not cleared to the background color and the previous background
3613  * (the image of the menu) is left in place.
3614  */
3615 static void 
3616 gtk_menu_reparent (GtkMenu      *menu, 
3617                    GtkWidget    *new_parent, 
3618                    gboolean      unrealize)
3619 {
3620   GtkObject *object = GTK_OBJECT (menu);
3621   GtkWidget *widget = GTK_WIDGET (menu);
3622   gboolean was_floating = GTK_OBJECT_FLOATING (object);
3623
3624   g_object_ref (object);
3625   gtk_object_sink (object);
3626
3627   if (unrealize)
3628     {
3629       g_object_ref (object);
3630       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3631       gtk_container_add (GTK_CONTAINER (new_parent), widget);
3632       g_object_unref (object);
3633     }
3634   else
3635     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3636   
3637   if (was_floating)
3638     GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3639   else
3640     g_object_unref (object);
3641 }
3642
3643 static void
3644 gtk_menu_show_all (GtkWidget *widget)
3645 {
3646   /* Show children, but not self. */
3647   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3648 }
3649
3650
3651 static void
3652 gtk_menu_hide_all (GtkWidget *widget)
3653 {
3654   /* Hide children, but not self. */
3655   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3656 }
3657
3658 /**
3659  * gtk_menu_set_screen:
3660  * @menu: a #GtkMenu.
3661  * @screen: a #GdkScreen, or %NULL if the screen should be
3662  *          determined by the widget the menu is attached to.
3663  *
3664  * Sets the #GdkScreen on which the menu will be displayed.
3665  * 
3666  * Since: 2.2
3667  **/
3668 void
3669 gtk_menu_set_screen (GtkMenu   *menu, 
3670                      GdkScreen *screen)
3671 {
3672   g_return_if_fail (GTK_IS_MENU (menu));
3673   g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3674
3675   g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3676
3677   if (screen)
3678     {
3679       menu_change_screen (menu, screen);
3680     }
3681   else
3682     {
3683       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3684       if (attach_widget)
3685         attach_widget_screen_changed (attach_widget, NULL, menu);
3686     }
3687 }
3688
3689 /**
3690  * gtk_menu_attach:
3691  * @menu: a #GtkMenu.
3692  * @child: a #GtkMenuItem.
3693  * @left_attach: The column number to attach the left side of the item to.
3694  * @right_attach: The column number to attach the right side of the item to.
3695  * @top_attach: The row number to attach the top of the item to.
3696  * @bottom_attach: The row number to attach the bottom of the item to.
3697  *
3698  * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3699  * an item will occupy is specified by @left_attach, @right_attach,
3700  * @top_attach and @bottom_attach. These each represent the leftmost,
3701  * rightmost, uppermost and lower column and row numbers of the table.
3702  * (Columns and rows are indexed from zero).
3703  *
3704  * Note that this function is not related to gtk_menu_detach().
3705  *
3706  * Since: 2.4
3707  **/
3708 void
3709 gtk_menu_attach (GtkMenu   *menu,
3710                  GtkWidget *child,
3711                  guint      left_attach,
3712                  guint      right_attach,
3713                  guint      top_attach,
3714                  guint      bottom_attach)
3715 {
3716   g_return_if_fail (GTK_IS_MENU (menu));
3717   g_return_if_fail (GTK_IS_MENU_ITEM (child));
3718   g_return_if_fail (child->parent == NULL || 
3719                     child->parent == GTK_WIDGET (menu));
3720   g_return_if_fail (left_attach < right_attach);
3721   g_return_if_fail (top_attach < bottom_attach);
3722
3723   if (!child->parent)
3724     {
3725       GTK_MENU_SHELL (menu)->children = 
3726         g_list_append (GTK_MENU_SHELL (menu)->children, child);
3727
3728       gtk_widget_set_parent (child, GTK_WIDGET (menu));
3729     }
3730
3731   gtk_container_child_set (GTK_CONTAINER (menu), child,
3732                            "left_attach", left_attach,
3733                            "right_attach", right_attach,
3734                            "top_attach", top_attach,
3735                            "bottom_attach", bottom_attach,
3736                            NULL);
3737 }
3738
3739 static gint
3740 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3741 {
3742   gint popup_delay;
3743
3744   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3745                 "gtk-menu-popup-delay", &popup_delay,
3746                 NULL);
3747
3748   return popup_delay;
3749 }
3750
3751 static GtkWidget *
3752 find_child_containing (GtkMenuShell *menu_shell,
3753                        int           left,
3754                        int           right,
3755                        int           top,
3756                        int           bottom)
3757 {
3758   GList *list;
3759
3760   /* find a child which includes the area given by
3761    * left, right, top, bottom.
3762    */
3763
3764   for (list = menu_shell->children; list; list = list->next)
3765     {
3766       guint l, r, t, b;
3767
3768       if (!_gtk_menu_item_is_selectable (list->data))
3769         continue;
3770
3771       get_child_attach (list->data, &l, &r, &t, &b);
3772
3773       if (l <= left && right <= r
3774           && t <= top && bottom <= b)
3775         return GTK_WIDGET (list->data);
3776     }
3777
3778   return NULL;
3779 }
3780
3781 static void
3782 gtk_menu_move_current (GtkMenuShell *menu_shell,
3783                        GtkMenuDirectionType direction)
3784 {
3785   GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3786
3787   /* use special table menu key bindings */
3788   if (menu_shell->active_menu_item && priv->columns > 1)
3789     {
3790       int i;
3791       guint l, r, t, b;
3792       gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3793       GtkWidget *match = NULL;
3794
3795       get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3796
3797       if (direction == GTK_MENU_DIR_NEXT)
3798         {
3799           for (i = b; i < priv->rows; i++)
3800             {
3801               match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3802               if (match)
3803                 break;
3804             }
3805
3806           if (!match)
3807             {
3808               /* wrap around */
3809               for (i = 0; i < t; i++)
3810                 {
3811                   match = find_child_containing (menu_shell,
3812                                                  l, l + 1, i, i + 1);
3813                   if (match)
3814                     break;
3815                 }
3816             }
3817         }
3818       else if (direction == GTK_MENU_DIR_PREV)
3819         {
3820           for (i = t; i > 0; i--)
3821             {
3822               match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3823               if (match)
3824                 break;
3825             }
3826
3827           if (!match)
3828             {
3829               /* wrap around */
3830               for (i = priv->rows; i > b; i--)
3831                 {
3832                   match = find_child_containing (menu_shell,
3833                                                  l, l + 1, i - 1, i);
3834                   if (match)
3835                     break;
3836                 }
3837             }
3838         }
3839       else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3840                || (rtl && direction == GTK_MENU_DIR_CHILD))
3841         {
3842           /* we go one left if possible */
3843           if (l > 0)
3844             match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3845
3846           if (!match)
3847             {
3848               GtkWidget *parent = menu_shell->parent_menu_shell;
3849
3850               if (!parent
3851                   || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3852                 match = menu_shell->active_menu_item;
3853             }
3854         }
3855       else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3856                || (rtl && direction == GTK_MENU_DIR_PARENT))
3857         {
3858           /* we go one right if possible */
3859           if (r < priv->columns)
3860             match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3861
3862           if (!match)
3863             {
3864               GtkWidget *parent = menu_shell->parent_menu_shell;
3865
3866               if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3867                   (!parent ||
3868                    g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3869                 match = menu_shell->active_menu_item;
3870             }
3871         }
3872
3873       if (match)
3874         {
3875           gtk_menu_shell_select_item (menu_shell, match);
3876           return;
3877         }
3878     }
3879
3880   GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3881 }
3882
3883 static gint
3884 get_visible_size (GtkMenu *menu)
3885 {
3886   GtkWidget *widget = GTK_WIDGET (menu);
3887   GtkContainer *container = GTK_CONTAINER (menu);
3888   
3889   gint menu_height = (widget->allocation.height
3890                       - 2 * (container->border_width
3891                              + widget->style->ythickness));
3892   
3893   if (menu->upper_arrow_visible && !menu->tearoff_active)
3894     menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3895   if (menu->lower_arrow_visible && !menu->tearoff_active)
3896     menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3897
3898   return menu_height;
3899 }
3900
3901 /* Find the sensitive on-screen child containing @y, or if none,
3902  * the nearest selectable onscreen child. (%NULL if none)
3903  */
3904 static GtkWidget *
3905 child_at (GtkMenu *menu,
3906           gint     y)
3907 {
3908   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3909   GtkWidget *child = NULL;
3910   gint child_offset = 0;
3911   GList *children;
3912   gint menu_height;
3913   gint lower, upper;            /* Onscreen bounds */
3914
3915   menu_height = get_visible_size (menu);
3916   lower = menu->scroll_offset;
3917   upper = menu->scroll_offset + menu_height;
3918   
3919   for (children = menu_shell->children; children; children = children->next)
3920     {
3921       if (GTK_WIDGET_VISIBLE (children->data))
3922         {
3923           GtkRequisition child_requisition;
3924
3925           gtk_widget_size_request (children->data, &child_requisition);
3926
3927           if (_gtk_menu_item_is_selectable (children->data) &&
3928               child_offset >= lower &&
3929               child_offset + child_requisition.height <= upper)
3930             {
3931               child = children->data;
3932               
3933               if (child_offset + child_requisition.height > y &&
3934                   !GTK_IS_TEAROFF_MENU_ITEM (child))
3935                 return child;
3936             }
3937       
3938           child_offset += child_requisition.height;
3939         }
3940     }
3941
3942   return child;
3943 }
3944
3945 static gint
3946 get_menu_height (GtkMenu *menu)
3947 {
3948   gint height;
3949   GtkWidget *widget = GTK_WIDGET (menu);
3950
3951   height = widget->requisition.height;
3952   height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3953
3954   if (menu->upper_arrow_visible && !menu->tearoff_active)
3955     height -= MENU_SCROLL_ARROW_HEIGHT;
3956
3957   if (menu->lower_arrow_visible && !menu->tearoff_active)
3958     height -= MENU_SCROLL_ARROW_HEIGHT;
3959
3960   return height;
3961 }
3962
3963 static void
3964 gtk_menu_real_move_scroll (GtkMenu       *menu,
3965                            GtkScrollType  type)
3966 {
3967   gint page_size = get_visible_size (menu);
3968   gint end_position = get_menu_height (menu);
3969   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3970   
3971   switch (type)
3972     {
3973     case GTK_SCROLL_PAGE_UP:
3974     case GTK_SCROLL_PAGE_DOWN:
3975       {
3976         gint old_offset;
3977         gint new_offset;
3978         gint child_offset = 0;
3979         gboolean old_upper_arrow_visible;
3980         gint step;
3981
3982         if (type == GTK_SCROLL_PAGE_UP)
3983           step = - page_size;
3984         else
3985           step = page_size;
3986
3987         if (menu_shell->active_menu_item)
3988           {
3989             gint child_height;
3990             
3991             compute_child_offset (menu, menu_shell->active_menu_item,
3992                                   &child_offset, &child_height, NULL);
3993             child_offset += child_height / 2;
3994           }
3995
3996         menu_shell->ignore_enter = TRUE;
3997         old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3998         old_offset = menu->scroll_offset;
3999
4000         new_offset = menu->scroll_offset + step;
4001         new_offset = CLAMP (new_offset, 0, end_position - page_size);
4002
4003         gtk_menu_scroll_to (menu, new_offset);
4004         
4005         if (menu_shell->active_menu_item)
4006           {
4007             GtkWidget *new_child;
4008             gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4009
4010             if (menu->scroll_offset != old_offset)
4011               step = menu->scroll_offset - old_offset;
4012
4013             step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4014
4015             new_child = child_at (menu, child_offset + step);
4016             if (new_child)
4017               gtk_menu_shell_select_item (menu_shell, new_child);
4018           }
4019       }
4020       break;
4021     case GTK_SCROLL_START:
4022       /* Ignore the enter event we might get if the pointer is on the menu
4023        */
4024       menu_shell->ignore_enter = TRUE;
4025       gtk_menu_scroll_to (menu, 0);
4026       gtk_menu_shell_select_first (menu_shell, TRUE);
4027       break;
4028     case GTK_SCROLL_END:
4029       /* Ignore the enter event we might get if the pointer is on the menu
4030        */
4031       menu_shell->ignore_enter = TRUE;
4032       gtk_menu_scroll_to (menu, end_position - page_size);
4033       _gtk_menu_shell_select_last (menu_shell, TRUE);
4034       break;
4035     default:
4036       break;
4037     }
4038 }
4039
4040
4041 /**
4042  * gtk_menu_set_monitor:
4043  * @menu: a #GtkMenu
4044  * @monitor_num: the number of the monitor on which the menu should
4045  *    be popped up
4046  * 
4047  * Informs GTK+ on which monitor a menu should be popped up. 
4048  * See gdk_screen_get_monitor_geometry().
4049  *
4050  * This function should be called from a #GtkMenuPositionFunc if the
4051  * menu should not appear on the same monitor as the pointer. This 
4052  * information can't be reliably inferred from the coordinates returned
4053  * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates 
4054  * may extend beyond the monitor boundaries or even the screen boundaries. 
4055  *
4056  * Since: 2.4
4057  **/
4058 void gtk_menu_set_monitor (GtkMenu *menu,
4059                            gint     monitor_num)
4060 {
4061   GtkMenuPrivate *priv;
4062   g_return_if_fail (GTK_IS_MENU (menu));
4063
4064   priv = gtk_menu_get_private (menu);
4065   
4066   priv->monitor_num = monitor_num;
4067 }