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