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