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