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