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