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