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