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