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