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