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