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