]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
c8c6c645577f38c9f74e24f74ff195476c58c42b
[~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   double value;
1995
1996   value = gtk_adjustment_get_value (adjustment);
1997   if (menu->priv->scroll_offset != value)
1998     gtk_menu_scroll_to (menu, value);
1999 }
2000
2001 static void
2002 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2003                             gint     width)
2004 {
2005   GtkMenuPrivate *priv = menu->priv;
2006   GdkGeometry geometry_hints;
2007
2008   if (!priv->tearoff_window)
2009     return;
2010
2011   if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2012     {
2013       GtkRequisition requisition;
2014
2015       gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2016                                      &requisition, NULL);
2017       width += requisition.width;
2018     }
2019
2020   geometry_hints.min_width = width;
2021   geometry_hints.max_width = width;
2022
2023   geometry_hints.min_height = 0;
2024   geometry_hints.max_height = priv->requested_height;
2025
2026   gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2027                                  NULL,
2028                                  &geometry_hints,
2029                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2030 }
2031
2032 static void
2033 gtk_menu_update_title (GtkMenu *menu)
2034 {
2035   GtkMenuPrivate *priv = menu->priv;
2036
2037   if (priv->tearoff_window)
2038     {
2039       const gchar *title;
2040       GtkWidget *attach_widget;
2041
2042       title = gtk_menu_get_title (menu);
2043       if (!title)
2044         {
2045           attach_widget = gtk_menu_get_attach_widget (menu);
2046           if (GTK_IS_MENU_ITEM (attach_widget))
2047             {
2048               GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2049               if (GTK_IS_LABEL (child))
2050                 title = gtk_label_get_text (GTK_LABEL (child));
2051             }
2052         }
2053
2054       if (title)
2055         gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2056     }
2057 }
2058
2059 static GtkWidget*
2060 gtk_menu_get_toplevel (GtkWidget *menu)
2061 {
2062   GtkWidget *attach, *toplevel;
2063
2064   attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2065
2066   if (GTK_IS_MENU_ITEM (attach))
2067     attach = gtk_widget_get_parent (attach);
2068
2069   if (GTK_IS_MENU (attach))
2070     return gtk_menu_get_toplevel (attach);
2071   else if (GTK_IS_WIDGET (attach))
2072     {
2073       toplevel = gtk_widget_get_toplevel (attach);
2074       if (gtk_widget_is_toplevel (toplevel))
2075         return toplevel;
2076     }
2077
2078   return NULL;
2079 }
2080
2081 static void
2082 tearoff_window_destroyed (GtkWidget *widget,
2083                           GtkMenu   *menu)
2084 {
2085   gtk_menu_set_tearoff_state (menu, FALSE);
2086 }
2087
2088 void
2089 gtk_menu_set_tearoff_state (GtkMenu  *menu,
2090                             gboolean  torn_off)
2091 {
2092   GtkMenuPrivate *priv = menu->priv;
2093   gint height;
2094
2095   g_return_if_fail (GTK_IS_MENU (menu));
2096
2097   if (priv->torn_off != torn_off)
2098     {
2099       priv->torn_off = torn_off;
2100       priv->tearoff_active = torn_off;
2101
2102       if (priv->torn_off)
2103         {
2104           if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2105             gtk_menu_popdown (menu);
2106
2107           if (!priv->tearoff_window)
2108             {
2109               GtkWidget *toplevel;
2110
2111               priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2112                                                    "type", GTK_WINDOW_TOPLEVEL,
2113                                                    "screen", gtk_widget_get_screen (priv->toplevel),
2114                                                    "app-paintable", TRUE,
2115                                                    NULL);
2116
2117               gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2118                                         GDK_WINDOW_TYPE_HINT_MENU);
2119               gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2120               g_signal_connect (priv->tearoff_window, "destroy",
2121                                 G_CALLBACK (tearoff_window_destroyed), menu);
2122               g_signal_connect (priv->tearoff_window, "event",
2123                                 G_CALLBACK (gtk_menu_window_event), menu);
2124
2125               gtk_menu_update_title (menu);
2126
2127               gtk_widget_realize (priv->tearoff_window);
2128
2129               toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2130               if (toplevel != NULL)
2131                 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2132                                               GTK_WINDOW (toplevel));
2133
2134               priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2135               gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2136                                  priv->tearoff_hbox);
2137
2138               height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2139               priv->tearoff_adjustment = gtk_adjustment_new (0,
2140                                                              0, priv->requested_height,
2141                                                              MENU_SCROLL_STEP2,
2142                                                              height/2,
2143                                                              height);
2144               g_object_connect (priv->tearoff_adjustment,
2145                                 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2146                                 NULL);
2147               priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2148
2149               gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2150                                 priv->tearoff_scrollbar,
2151                                 FALSE, FALSE, 0);
2152
2153               if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2154                 gtk_widget_show (priv->tearoff_scrollbar);
2155
2156               gtk_widget_show (priv->tearoff_hbox);
2157             }
2158
2159           gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2160
2161           /* Update menu->requisition */
2162           gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2163
2164           gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2165
2166           gtk_widget_realize (priv->tearoff_window);
2167           gtk_menu_position (menu, TRUE);
2168
2169           gtk_widget_show (GTK_WIDGET (menu));
2170           gtk_widget_show (priv->tearoff_window);
2171
2172           gtk_menu_scroll_to (menu, 0);
2173
2174         }
2175       else
2176         {
2177           gtk_widget_hide (GTK_WIDGET (menu));
2178           gtk_widget_hide (priv->tearoff_window);
2179           if (GTK_IS_CONTAINER (priv->toplevel))
2180             gtk_menu_reparent (menu, priv->toplevel, FALSE);
2181           gtk_widget_destroy (priv->tearoff_window);
2182
2183           priv->tearoff_window = NULL;
2184           priv->tearoff_hbox = NULL;
2185           priv->tearoff_scrollbar = NULL;
2186           priv->tearoff_adjustment = NULL;
2187         }
2188
2189       g_object_notify (G_OBJECT (menu), "tearoff-state");
2190     }
2191 }
2192
2193 /**
2194  * gtk_menu_get_tearoff_state:
2195  * @menu: a #GtkMenu
2196  *
2197  * Returns whether the menu is torn off.
2198  * See gtk_menu_set_tearoff_state().
2199  *
2200  * Return value: %TRUE if the menu is currently torn off.
2201  */
2202 gboolean
2203 gtk_menu_get_tearoff_state (GtkMenu *menu)
2204 {
2205   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2206
2207   return menu->priv->torn_off;
2208 }
2209
2210 /**
2211  * gtk_menu_set_title:
2212  * @menu: a #GtkMenu
2213  * @title: a string containing the title for the menu
2214  *
2215  * Sets the title string for the menu.
2216  *
2217  * The title is displayed when the menu is shown as a tearoff
2218  * menu. If @title is %NULL, the menu will see if it is attached
2219  * to a parent menu item, and if so it will try to use the same
2220  * text as that menu item's label.
2221  */
2222 void
2223 gtk_menu_set_title (GtkMenu     *menu,
2224                     const gchar *title)
2225 {
2226   GtkMenuPrivate *priv = menu->priv;
2227   char *old_title;
2228
2229   g_return_if_fail (GTK_IS_MENU (menu));
2230
2231   old_title = priv->title;
2232   priv->title = g_strdup (title);
2233   g_free (old_title);
2234
2235   gtk_menu_update_title (menu);
2236   g_object_notify (G_OBJECT (menu), "tearoff-title");
2237 }
2238
2239 /**
2240  * gtk_menu_get_title:
2241  * @menu: a #GtkMenu
2242  *
2243  * Returns the title of the menu. See gtk_menu_set_title().
2244  *
2245  * Return value: the title of the menu, or %NULL if the menu
2246  *     has no title set on it. This string is owned by GTK+
2247  *     and should not be modified or freed.
2248  **/
2249 G_CONST_RETURN gchar *
2250 gtk_menu_get_title (GtkMenu *menu)
2251 {
2252   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2253
2254   return menu->priv->title;
2255 }
2256
2257 void
2258 gtk_menu_reorder_child (GtkMenu   *menu,
2259                         GtkWidget *child,
2260                         gint       position)
2261 {
2262   GtkMenuShell *menu_shell;
2263
2264   g_return_if_fail (GTK_IS_MENU (menu));
2265   g_return_if_fail (GTK_IS_MENU_ITEM (child));
2266
2267   menu_shell = GTK_MENU_SHELL (menu);
2268
2269   if (g_list_find (menu_shell->priv->children, child))
2270     {
2271       menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2272       menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2273
2274       menu_queue_resize (menu);
2275     }
2276 }
2277
2278 static void
2279 gtk_menu_style_updated (GtkWidget *widget)
2280 {
2281   if (gtk_widget_get_realized (widget))
2282     {
2283       GtkMenu *menu = GTK_MENU (widget);
2284       GtkMenuPrivate *priv = menu->priv;
2285       GtkStyleContext *context;
2286
2287       context = gtk_widget_get_style_context (widget);
2288
2289       gtk_style_context_set_background (context, priv->bin_window);
2290       gtk_style_context_set_background (context, priv->view_window);
2291       gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2292     }
2293 }
2294
2295 static void
2296 get_arrows_border (GtkMenu   *menu,
2297                    GtkBorder *border)
2298 {
2299   GtkMenuPrivate *priv = menu->priv;
2300   guint scroll_arrow_height;
2301   GtkArrowPlacement arrow_placement;
2302
2303   gtk_widget_style_get (GTK_WIDGET (menu),
2304                         "scroll-arrow-vlength", &scroll_arrow_height,
2305                         "arrow_placement", &arrow_placement,
2306                         NULL);
2307
2308   switch (arrow_placement)
2309     {
2310     case GTK_ARROWS_BOTH:
2311       border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2312       border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2313       break;
2314
2315     case GTK_ARROWS_START:
2316       border->top = (priv->upper_arrow_visible ||
2317                      priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2318       border->bottom = 0;
2319       break;
2320
2321     case GTK_ARROWS_END:
2322       border->top = 0;
2323       border->bottom = (priv->upper_arrow_visible ||
2324                         priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2325       break;
2326     }
2327
2328   border->left = border->right = 0;
2329 }
2330
2331 static void
2332 get_menu_border (GtkWidget *widget,
2333                  GtkBorder *border)
2334 {
2335   GtkStyleContext *context;
2336   GtkStateFlags state;
2337   GtkBorder padding, border_width;
2338
2339   context = gtk_widget_get_style_context (widget);
2340   state = gtk_widget_get_state_flags (widget);
2341
2342   gtk_style_context_get_padding (context, state, &padding);
2343   gtk_style_context_get_border (context, state, &border_width);
2344
2345   border->left = border_width.left + padding.left;
2346   border->right = border_width.right + padding.right;
2347   border->top = border_width.top + padding.top;
2348   border->bottom = border_width.bottom + padding.bottom;
2349 }
2350
2351 static void
2352 gtk_menu_realize (GtkWidget *widget)
2353 {
2354   GtkMenu *menu = GTK_MENU (widget);
2355   GtkMenuPrivate *priv = menu->priv;
2356   GtkAllocation allocation;
2357   GtkStyleContext *context;
2358   GdkWindow *window;
2359   GdkWindowAttr attributes;
2360   gint attributes_mask;
2361   gint border_width;
2362   GtkWidget *child;
2363   GList *children;
2364   guint vertical_padding;
2365   guint horizontal_padding;
2366   GtkBorder arrow_border, border;
2367
2368   g_return_if_fail (GTK_IS_MENU (widget));
2369
2370   gtk_widget_set_realized (widget, TRUE);
2371
2372   gtk_widget_get_allocation (widget, &allocation);
2373
2374   attributes.window_type = GDK_WINDOW_CHILD;
2375   attributes.x = allocation.x;
2376   attributes.y = allocation.y;
2377   attributes.width = allocation.width;
2378   attributes.height = allocation.height;
2379   attributes.wclass = GDK_INPUT_OUTPUT;
2380   attributes.visual = gtk_widget_get_visual (widget);
2381   attributes.event_mask = gtk_widget_get_events (widget);
2382   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2383                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2384
2385   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2386
2387   window = gdk_window_new (gtk_widget_get_parent_window (widget),
2388                            &attributes, attributes_mask);
2389   gtk_widget_set_window (widget, window);
2390   gdk_window_set_user_data (window, widget);
2391
2392   get_menu_border (widget, &border);
2393   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2394   context = gtk_widget_get_style_context (widget);
2395
2396   gtk_widget_style_get (GTK_WIDGET (menu),
2397                         "vertical-padding", &vertical_padding,
2398                         "horizontal-padding", &horizontal_padding,
2399                         NULL);
2400
2401   gtk_widget_get_allocation (widget, &allocation);
2402
2403   attributes.x = border_width + border.left + horizontal_padding;
2404   attributes.y = border_width + border.top + vertical_padding;
2405   attributes.width = allocation.width -
2406     (2 * (border_width + horizontal_padding)) - border.left - border.right;
2407   attributes.height = allocation.height -
2408     (2 * (border_width + vertical_padding)) - border.top - border.bottom;
2409
2410   get_arrows_border (menu, &arrow_border);
2411   attributes.y += arrow_border.top;
2412   attributes.height -= arrow_border.top;
2413   attributes.height -= arrow_border.bottom;
2414
2415   attributes.width = MAX (1, attributes.width);
2416   attributes.height = MAX (1, attributes.height);
2417
2418   priv->view_window = gdk_window_new (window,
2419                                       &attributes, attributes_mask);
2420   gdk_window_set_user_data (priv->view_window, menu);
2421
2422   gtk_widget_get_allocation (widget, &allocation);
2423
2424   attributes.x = 0;
2425   attributes.y = 0;
2426   attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2427     border.left + border.right;
2428   attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2429     border.top + border.bottom;
2430
2431   attributes.width = MAX (1, attributes.width);
2432   attributes.height = MAX (1, attributes.height);
2433
2434   priv->bin_window = gdk_window_new (priv->view_window,
2435                                      &attributes, attributes_mask);
2436   gdk_window_set_user_data (priv->bin_window, menu);
2437
2438   children = GTK_MENU_SHELL (menu)->priv->children;
2439   while (children)
2440     {
2441       child = children->data;
2442       children = children->next;
2443
2444       gtk_widget_set_parent_window (child, priv->bin_window);
2445     }
2446
2447   gtk_style_context_set_background (context, priv->bin_window);
2448   gtk_style_context_set_background (context, priv->view_window);
2449   gtk_style_context_set_background (context, window);
2450
2451   if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2452     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2453                                   GTK_MENU_SHELL (widget)->priv->active_menu_item);
2454
2455   gdk_window_show (priv->bin_window);
2456   gdk_window_show (priv->view_window);
2457 }
2458
2459 static gboolean
2460 gtk_menu_focus (GtkWidget       *widget,
2461                 GtkDirectionType direction)
2462 {
2463   /* A menu or its menu items cannot have focus */
2464   return FALSE;
2465 }
2466
2467 /* See notes in gtk_menu_popup() for information
2468  * about the "grab transfer window"
2469  */
2470 static GdkWindow *
2471 menu_grab_transfer_window_get (GtkMenu *menu)
2472 {
2473   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2474   if (!window)
2475     {
2476       GdkWindowAttr attributes;
2477       gint attributes_mask;
2478
2479       attributes.x = -100;
2480       attributes.y = -100;
2481       attributes.width = 10;
2482       attributes.height = 10;
2483       attributes.window_type = GDK_WINDOW_TEMP;
2484       attributes.wclass = GDK_INPUT_ONLY;
2485       attributes.override_redirect = TRUE;
2486       attributes.event_mask = 0;
2487
2488       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2489
2490       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2491                                &attributes, attributes_mask);
2492       gdk_window_set_user_data (window, menu);
2493
2494       gdk_window_show (window);
2495
2496       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2497     }
2498
2499   return window;
2500 }
2501
2502 static void
2503 menu_grab_transfer_window_destroy (GtkMenu *menu)
2504 {
2505   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2506   if (window)
2507     {
2508       gdk_window_set_user_data (window, NULL);
2509       gdk_window_destroy (window);
2510       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2511     }
2512 }
2513
2514 static void
2515 gtk_menu_unrealize (GtkWidget *widget)
2516 {
2517   GtkMenu *menu = GTK_MENU (widget);
2518   GtkMenuPrivate *priv = menu->priv;
2519
2520   menu_grab_transfer_window_destroy (menu);
2521
2522   gdk_window_set_user_data (priv->view_window, NULL);
2523   gdk_window_destroy (priv->view_window);
2524   priv->view_window = NULL;
2525
2526   gdk_window_set_user_data (priv->bin_window, NULL);
2527   gdk_window_destroy (priv->bin_window);
2528   priv->bin_window = NULL;
2529
2530   GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2531 }
2532
2533 static gint
2534 calculate_line_heights (GtkMenu *menu,
2535                         gint     for_width,
2536                         guint  **ret_min_heights,
2537                         guint  **ret_nat_heights)
2538 {
2539   GtkStyleContext *context;
2540   GtkStateFlags   state;
2541   GtkBorder       padding;
2542   GtkMenuPrivate *priv;
2543   GtkMenuShell   *menu_shell;
2544   GtkWidget      *child, *widget;
2545   GList          *children;
2546   guint           horizontal_padding;
2547   guint           border_width;
2548   guint           n_columns;
2549   gint            n_heights;
2550   guint          *min_heights;
2551   guint          *nat_heights;
2552   gint            avail_width;
2553
2554   priv         = menu->priv;
2555   widget       = GTK_WIDGET (menu);
2556   menu_shell   = GTK_MENU_SHELL (widget);
2557
2558   min_heights  = g_new0 (guint, gtk_menu_get_n_rows (menu));
2559   nat_heights  = g_new0 (guint, gtk_menu_get_n_rows (menu));
2560   n_heights    = gtk_menu_get_n_rows (menu);
2561   n_columns    = gtk_menu_get_n_columns (menu);
2562   avail_width  = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2563
2564   gtk_widget_style_get (GTK_WIDGET (menu),
2565                         "horizontal-padding", &horizontal_padding,
2566                         NULL);
2567
2568   context = gtk_widget_get_style_context (widget);
2569   state = gtk_widget_get_state_flags (widget);
2570   gtk_style_context_get_padding (context, state, &padding);
2571
2572   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2573   avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2574
2575   for (children = menu_shell->priv->children; children; children = children->next)
2576     {
2577       gint part;
2578       gint toggle_size;
2579       gint l, r, t, b;
2580       gint child_min, child_nat;
2581
2582       child = children->data;
2583
2584       if (!gtk_widget_get_visible (child))
2585         continue;
2586
2587       get_effective_child_attach (child, &l, &r, &t, &b);
2588
2589       part = avail_width / (r - l);
2590
2591       gtk_widget_get_preferred_height_for_width (child, part,
2592                                                  &child_min, &child_nat);
2593
2594       gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2595
2596       part = MAX (child_min, toggle_size) / (b - t);
2597       min_heights[t] = MAX (min_heights[t], part);
2598
2599       part = MAX (child_nat, toggle_size) / (b - t);
2600       nat_heights[t] = MAX (nat_heights[t], part);
2601     }
2602
2603   if (ret_min_heights)
2604     *ret_min_heights = min_heights;
2605   else
2606     g_free (min_heights);
2607
2608   if (ret_nat_heights)
2609     *ret_nat_heights = nat_heights;
2610   else
2611     g_free (nat_heights);
2612
2613   return n_heights;
2614 }
2615
2616 static void
2617 gtk_menu_size_allocate (GtkWidget     *widget,
2618                         GtkAllocation *allocation)
2619 {
2620   GtkMenu *menu;
2621   GtkMenuPrivate *priv;
2622   GtkMenuShell *menu_shell;
2623   GtkWidget *child;
2624   GtkAllocation child_allocation;
2625   GList *children;
2626   gint x, y, i;
2627   gint width, height;
2628   guint border_width;
2629   guint vertical_padding;
2630   guint horizontal_padding;
2631   GtkBorder border;
2632
2633   g_return_if_fail (GTK_IS_MENU (widget));
2634   g_return_if_fail (allocation != NULL);
2635
2636   menu = GTK_MENU (widget);
2637   menu_shell = GTK_MENU_SHELL (widget);
2638   priv = menu->priv;
2639
2640   gtk_widget_set_allocation (widget, allocation);
2641
2642   gtk_widget_style_get (GTK_WIDGET (menu),
2643                         "vertical-padding", &vertical_padding,
2644                         "horizontal-padding", &horizontal_padding,
2645                         NULL);
2646
2647   get_menu_border (widget, &border);
2648   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2649
2650   g_free (priv->heights);
2651   priv->heights_length = calculate_line_heights (menu,
2652                                                  allocation->width,
2653                                                  &priv->heights,
2654                                                  NULL);
2655
2656   /* refresh our cached height request */
2657   priv->requested_height = (2 * (border_width + vertical_padding)) +
2658     border.top + border.bottom;
2659   for (i = 0; i < priv->heights_length; i++)
2660     priv->requested_height += priv->heights[i];
2661
2662   x = border_width + border.left + horizontal_padding;
2663   y = border_width + border.top + vertical_padding;
2664   width = allocation->width - (2 * (border_width + horizontal_padding)) -
2665     border.left - border.right;
2666   height = allocation->height - (2 * (border_width + vertical_padding)) -
2667     border.top - border.bottom;
2668
2669   if (menu_shell->priv->active)
2670     gtk_menu_scroll_to (menu, priv->scroll_offset);
2671
2672   if (!priv->tearoff_active)
2673     {
2674       GtkBorder arrow_border;
2675
2676       get_arrows_border (menu, &arrow_border);
2677       y += arrow_border.top;
2678       height -= arrow_border.top;
2679       height -= arrow_border.bottom;
2680     }
2681
2682   width = MAX (1, width);
2683   height = MAX (1, height);
2684
2685   if (gtk_widget_get_realized (widget))
2686     {
2687       gdk_window_move_resize (gtk_widget_get_window (widget),
2688                               allocation->x, allocation->y,
2689                               allocation->width, allocation->height);
2690
2691       gdk_window_move_resize (priv->view_window, x, y, width, height);
2692     }
2693
2694   if (menu_shell->priv->children)
2695     {
2696       gint base_width = width / gtk_menu_get_n_columns (menu);
2697
2698       children = menu_shell->priv->children;
2699       while (children)
2700         {
2701           child = children->data;
2702           children = children->next;
2703
2704           if (gtk_widget_get_visible (child))
2705             {
2706               gint i;
2707               gint l, r, t, b;
2708
2709               get_effective_child_attach (child, &l, &r, &t, &b);
2710
2711               if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2712                 {
2713                   guint tmp;
2714                   tmp = gtk_menu_get_n_columns (menu) - l;
2715                   l = gtk_menu_get_n_columns (menu) - r;
2716                   r = tmp;
2717                 }
2718
2719               child_allocation.width = (r - l) * base_width;
2720               child_allocation.height = 0;
2721               child_allocation.x = l * base_width;
2722               child_allocation.y = 0;
2723
2724               for (i = 0; i < b; i++)
2725                 {
2726                   if (i < t)
2727                     child_allocation.y += priv->heights[i];
2728                   else
2729                     child_allocation.height += priv->heights[i];
2730                 }
2731
2732               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2733                                                   priv->toggle_size);
2734
2735               gtk_widget_size_allocate (child, &child_allocation);
2736               gtk_widget_queue_draw (child);
2737             }
2738         }
2739
2740       /* Resize the item window */
2741       if (gtk_widget_get_realized (widget))
2742         {
2743           gint i;
2744           gint width, height;
2745
2746           height = 0;
2747           for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2748             height += priv->heights[i];
2749
2750           width = gtk_menu_get_n_columns (menu) * base_width;
2751           gdk_window_resize (priv->bin_window, width, height);
2752         }
2753
2754       if (priv->tearoff_active)
2755         {
2756           if (height >= priv->requested_height)
2757             {
2758               if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2759                 {
2760                   gtk_widget_hide (priv->tearoff_scrollbar);
2761                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2762
2763                   gtk_menu_scroll_to (menu, 0);
2764                 }
2765             }
2766           else
2767             {
2768               gtk_adjustment_configure (priv->tearoff_adjustment,
2769                                         gtk_adjustment_get_value (priv->tearoff_adjustment),
2770                                         0,
2771                                         priv->requested_height,
2772                                         gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2773                                         gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2774                                         allocation->height);
2775
2776               if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2777                 {
2778                   gtk_widget_show (priv->tearoff_scrollbar);
2779                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2780                 }
2781             }
2782         }
2783     }
2784 }
2785
2786 static void
2787 get_arrows_visible_area (GtkMenu      *menu,
2788                          GdkRectangle *border,
2789                          GdkRectangle *upper,
2790                          GdkRectangle *lower,
2791                          gint         *arrow_space)
2792 {
2793   GtkArrowPlacement arrow_placement;
2794   GtkWidget *widget = GTK_WIDGET (menu);
2795   guint border_width;
2796   guint vertical_padding;
2797   guint horizontal_padding;
2798   gint scroll_arrow_height;
2799   GtkBorder menu_border;
2800
2801   gtk_widget_style_get (widget,
2802                         "vertical-padding", &vertical_padding,
2803                         "horizontal-padding", &horizontal_padding,
2804                         "scroll-arrow-vlength", &scroll_arrow_height,
2805                         "arrow-placement", &arrow_placement,
2806                         NULL);
2807
2808   get_menu_border (widget, &menu_border);
2809   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2810   border->x = border_width + menu_border.left + horizontal_padding;
2811   border->y = border_width + menu_border.top + vertical_padding;
2812   border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2813   border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2814
2815   switch (arrow_placement)
2816     {
2817     case GTK_ARROWS_BOTH:
2818       upper->x = border->x;
2819       upper->y = border->y;
2820       upper->width = border->width - 2 * border->x;
2821       upper->height = scroll_arrow_height;
2822
2823       lower->x = border->x;
2824       lower->y = border->height - border->y - scroll_arrow_height;
2825       lower->width = border->width - 2 * border->x;
2826       lower->height = scroll_arrow_height;
2827       break;
2828
2829     case GTK_ARROWS_START:
2830       upper->x = border->x;
2831       upper->y = border->y;
2832       upper->width = (border->width - 2 * border->x) / 2;
2833       upper->height = scroll_arrow_height;
2834
2835       lower->x = border->x + upper->width;
2836       lower->y = border->y;
2837       lower->width = (border->width - 2 * border->x) / 2;
2838       lower->height = scroll_arrow_height;
2839       break;
2840
2841     case GTK_ARROWS_END:
2842       upper->x = border->x;
2843       upper->y = border->height - border->y - scroll_arrow_height;
2844       upper->width = (border->width - 2 * border->x) / 2;
2845       upper->height = scroll_arrow_height;
2846
2847       lower->x = border->x + upper->width;
2848       lower->y = border->height - border->y - scroll_arrow_height;
2849       lower->width = (border->width - 2 * border->x) / 2;
2850       lower->height = scroll_arrow_height;
2851       break;
2852
2853     default:
2854        g_assert_not_reached();
2855        upper->x = upper->y = upper->width = upper->height = 0;
2856        lower->x = lower->y = lower->width = lower->height = 0;
2857     }
2858
2859   *arrow_space = scroll_arrow_height - menu_border.top - menu_border.bottom;
2860 }
2861
2862 static gboolean
2863 gtk_menu_draw (GtkWidget *widget,
2864                cairo_t   *cr)
2865 {
2866   GtkMenu *menu;
2867   GtkMenuPrivate *priv;
2868   GtkStyleContext *context;
2869   GdkRectangle border;
2870   GdkRectangle upper;
2871   GdkRectangle lower;
2872   GdkWindow *window;
2873   gint arrow_space;
2874   GtkStateFlags state;
2875   GtkBorder menu_border;
2876
2877   menu = GTK_MENU (widget);
2878   priv = menu->priv;
2879   context = gtk_widget_get_style_context (widget);
2880   window = gtk_widget_get_window (widget);
2881   state = gtk_widget_get_state_flags (widget);
2882
2883   get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2884   get_menu_border (widget, &menu_border);
2885
2886   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2887     {
2888       gfloat arrow_scaling;
2889       gint arrow_size;
2890
2891       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2892       arrow_size = arrow_scaling * arrow_space;
2893
2894       gtk_render_background (context, cr, 0, 0,
2895                              gtk_widget_get_allocated_width (widget),
2896                              gtk_widget_get_allocated_height (widget));
2897       gtk_render_frame (context, cr, 0, 0,
2898                         gtk_widget_get_allocated_width (widget),
2899                         gtk_widget_get_allocated_height (widget));
2900
2901       gtk_style_context_save (context);
2902       gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2903
2904       if (priv->upper_arrow_visible && !priv->tearoff_active)
2905         {
2906           gtk_style_context_save (context);
2907           gtk_style_context_set_state (context, priv->upper_arrow_state);
2908
2909           gtk_render_background (context, cr,
2910                                  upper.x, upper.y,
2911                                  upper.width, upper.height);
2912           gtk_render_frame (context, cr,
2913                             upper.x, upper.y,
2914                             upper.width, upper.height);
2915
2916           gtk_render_arrow (context, cr, 0,
2917                            upper.x + (upper.width - arrow_size) / 2,
2918                            upper.y + menu_border.top + (arrow_space - arrow_size) / 2,
2919                            arrow_size);
2920
2921           gtk_style_context_restore (context);
2922         }
2923
2924       if (priv->lower_arrow_visible && !priv->tearoff_active)
2925         {
2926           gtk_style_context_save (context);
2927           gtk_style_context_set_state (context, priv->lower_arrow_state);
2928
2929           gtk_render_background (context, cr,
2930                                  lower.x, lower.y,
2931                                  lower.width, lower.height);
2932           gtk_render_frame (context, cr,
2933                             lower.x, lower.y,
2934                             lower.width, lower.height);
2935
2936           gtk_render_arrow (context, cr, G_PI,
2937                             lower.x + (lower.width - arrow_size) / 2,
2938                             lower.y + menu_border.top + (arrow_space - arrow_size) / 2,
2939                             arrow_size);
2940
2941           gtk_style_context_restore (context);
2942         }
2943
2944       gtk_style_context_restore (context);
2945     }
2946
2947   if (gtk_cairo_should_draw_window (cr, priv->bin_window))
2948     {
2949       gint y = -border.y + priv->scroll_offset;
2950
2951       cairo_save (cr);
2952       gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
2953
2954       if (!priv->tearoff_active)
2955         {
2956           GtkBorder arrow_border;
2957
2958           get_arrows_border (menu, &arrow_border);
2959           y -= arrow_border.top;
2960         }
2961
2962       gtk_render_background (context, cr,
2963                              - border.x, y,
2964                              border.width, border.height);
2965       gtk_render_frame (context, cr,
2966                         - border.x, y,
2967                         border.width, border.height);
2968
2969       cairo_restore (cr);
2970     }
2971
2972   GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2973
2974   return FALSE;
2975 }
2976
2977 static void
2978 gtk_menu_show (GtkWidget *widget)
2979 {
2980   GtkMenu *menu = GTK_MENU (widget);
2981
2982   _gtk_menu_refresh_accel_paths (menu, FALSE);
2983
2984   GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2985 }
2986
2987
2988 static void 
2989 gtk_menu_get_preferred_width (GtkWidget *widget,
2990                               gint      *minimum_size,
2991                               gint      *natural_size)
2992 {
2993   GtkMenu        *menu;
2994   GtkMenuShell   *menu_shell;
2995   GtkMenuPrivate *priv;
2996   GtkWidget      *child;
2997   GList          *children;
2998   guint           max_toggle_size;
2999   guint           max_accel_width;
3000   guint           horizontal_padding;
3001   guint           border_width;
3002   gint            child_min, child_nat;
3003   gint            min_width, nat_width;
3004   GtkBorder       border;
3005
3006   menu       = GTK_MENU (widget);
3007   menu_shell = GTK_MENU_SHELL (widget);
3008   priv       = menu->priv;
3009
3010   min_width = nat_width = 0;
3011
3012   max_toggle_size = 0;
3013   max_accel_width = 0;
3014
3015   children = menu_shell->priv->children;
3016   while (children)
3017     {
3018       gint part;
3019       gint toggle_size;
3020       gint l, r, t, b;
3021
3022       child = children->data;
3023       children = children->next;
3024
3025       if (! gtk_widget_get_visible (child))
3026         continue;
3027
3028       get_effective_child_attach (child, &l, &r, &t, &b);
3029
3030       /* It's important to size_request the child
3031        * before doing the toggle size request, in
3032        * case the toggle size request depends on the size
3033        * request of a child of the child (e.g. for ImageMenuItem)
3034        */
3035        gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3036
3037        gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3038        max_toggle_size = MAX (max_toggle_size, toggle_size);
3039        max_accel_width = MAX (max_accel_width,
3040                               GTK_MENU_ITEM (child)->priv->accelerator_width);
3041
3042        part = child_min / (r - l);
3043        min_width = MAX (min_width, part);
3044
3045        part = child_nat / (r - l);
3046        nat_width = MAX (nat_width, part);
3047     }
3048
3049   /* If the menu doesn't include any images or check items
3050    * reserve the space so that all menus are consistent.
3051    * We only do this for 'ordinary' menus, not for combobox
3052    * menus or multi-column menus
3053    */
3054   if (max_toggle_size == 0 &&
3055       gtk_menu_get_n_columns (menu) == 1 &&
3056       !priv->no_toggle_size)
3057     {
3058       GtkStyleContext *context;
3059       GtkWidgetPath *menu_path, *check_path;
3060       guint toggle_spacing;
3061       guint indicator_size;
3062
3063       context = gtk_widget_get_style_context (widget);
3064       menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3065
3066       /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3067       check_path = gtk_widget_path_copy (menu_path);
3068       gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3069
3070       gtk_style_context_set_path (context, check_path);
3071       gtk_widget_path_free (check_path);
3072
3073       gtk_style_context_get_style (context,
3074                                    "toggle-spacing", &toggle_spacing,
3075                                    "indicator-size", &indicator_size,
3076                                    NULL);
3077
3078       max_toggle_size = indicator_size + toggle_spacing;
3079
3080       /* Restore real widget path */
3081       gtk_style_context_set_path (context, menu_path);
3082       gtk_widget_path_free (menu_path);
3083     }
3084
3085   min_width += 2 * max_toggle_size + max_accel_width;
3086   min_width *= gtk_menu_get_n_columns (menu);
3087
3088   nat_width += 2 * max_toggle_size + max_accel_width;
3089   nat_width *= gtk_menu_get_n_columns (menu);
3090
3091   gtk_widget_style_get (GTK_WIDGET (menu),
3092                         "horizontal-padding", &horizontal_padding,
3093                         NULL);
3094
3095   get_menu_border (widget, &border);
3096   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3097   min_width   += (2 * (border_width + horizontal_padding)) +
3098     border.left + border.right;
3099   nat_width   += (2 * (border_width + horizontal_padding)) +
3100     border.top + border.bottom;
3101
3102   priv->toggle_size = max_toggle_size;
3103   priv->accel_size  = max_accel_width;
3104
3105   if (minimum_size)
3106     *minimum_size = min_width;
3107
3108   if (natural_size)
3109     *natural_size = nat_width;
3110
3111   /* Don't resize the tearoff if it is not active,
3112    * because it won't redraw (it is only a background pixmap).
3113    */
3114   if (priv->tearoff_active)
3115     gtk_menu_set_tearoff_hints (menu, min_width);
3116 }
3117
3118 static void
3119 gtk_menu_get_preferred_height (GtkWidget *widget,
3120                                gint      *minimum_size,
3121                                gint      *natural_size)
3122 {
3123   gint min_width;
3124
3125   /* Menus are height-for-width only, just return the height
3126    * for the minimum width
3127    */
3128   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3129   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3130 }
3131
3132 static void
3133 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3134                                          gint       for_size,
3135                                          gint      *minimum_size,
3136                                          gint      *natural_size)
3137 {
3138   GtkStyleContext *context;
3139   GtkStateFlags   state;
3140   GtkBorder       padding, border;
3141   GtkMenu        *menu = GTK_MENU (widget);
3142   GtkMenuPrivate *priv = menu->priv;
3143   guint          *min_heights, *nat_heights;
3144   guint           vertical_padding, border_width;
3145   gint            n_heights, i;
3146   gint            min_height, nat_height;
3147
3148   gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3149   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3150
3151   context = gtk_widget_get_style_context (widget);
3152   state = gtk_widget_get_state_flags (widget);
3153   gtk_style_context_get_padding (context, state, &padding);
3154   gtk_style_context_get_border (context, state, &border);
3155
3156   min_height = nat_height = (border_width + vertical_padding) * 2 +
3157           padding.left + padding.right + border.left + border.right;
3158
3159   n_heights =
3160     calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3161
3162   for (i = 0; i < n_heights; i++)
3163     {
3164       min_height += min_heights[i];
3165       nat_height += nat_heights[i];
3166     }
3167
3168   if (priv->have_position)
3169     {
3170       GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3171       GdkRectangle monitor;
3172
3173       gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3174
3175       if (priv->position_y + min_height > monitor.y + monitor.height)
3176         min_height = monitor.y + monitor.height - priv->position_y;
3177
3178       if (priv->position_y + nat_height > monitor.y + monitor.height)
3179         nat_height = monitor.y + monitor.height - priv->position_y;
3180
3181       if (priv->position_y < monitor.y)
3182         {
3183           min_height -= monitor.y - priv->position_y;
3184           nat_height -= monitor.y - priv->position_y;
3185         }
3186     }
3187
3188   if (minimum_size)
3189     *minimum_size = min_height;
3190
3191   if (natural_size)
3192     *natural_size = nat_height;
3193
3194   g_free (min_heights);
3195   g_free (nat_heights);
3196 }
3197
3198
3199
3200 static gboolean
3201 gtk_menu_button_scroll (GtkMenu        *menu,
3202                         GdkEventButton *event)
3203 {
3204   GtkMenuPrivate *priv = menu->priv;
3205
3206   if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3207     {
3208       gboolean touchscreen_mode;
3209
3210       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3211                     "gtk-touchscreen-mode", &touchscreen_mode,
3212                     NULL);
3213
3214       if (touchscreen_mode)
3215         gtk_menu_handle_scrolling (menu,
3216                                    event->x_root, event->y_root,
3217                                    event->type == GDK_BUTTON_PRESS,
3218                                    FALSE);
3219
3220       return TRUE;
3221     }
3222
3223   return FALSE;
3224 }
3225
3226 static gboolean
3227 pointer_in_menu_window (GtkWidget *widget,
3228                         gdouble    x_root,
3229                         gdouble    y_root)
3230 {
3231   GtkMenu *menu = GTK_MENU (widget);
3232   GtkMenuPrivate *priv = menu->priv;
3233   GtkAllocation allocation;
3234
3235   if (gtk_widget_get_mapped (priv->toplevel))
3236     {
3237       GtkMenuShell *menu_shell;
3238       gint          window_x, window_y;
3239
3240       gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3241                                &window_x, &window_y);
3242
3243       gtk_widget_get_allocation (widget, &allocation);
3244       if (x_root >= window_x && x_root < window_x + allocation.width &&
3245           y_root >= window_y && y_root < window_y + allocation.height)
3246         return TRUE;
3247
3248       menu_shell = GTK_MENU_SHELL (widget);
3249
3250       if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3251         return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3252                                        x_root, y_root);
3253     }
3254
3255   return FALSE;
3256 }
3257
3258 static gboolean
3259 gtk_menu_button_press (GtkWidget      *widget,
3260                        GdkEventButton *event)
3261 {
3262   if (event->type != GDK_BUTTON_PRESS)
3263     return FALSE;
3264
3265   /* Don't pass down to menu shell for presses over scroll arrows
3266    */
3267   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3268     return TRUE;
3269
3270   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3271    *  was clicked. The check for the event_widget being a GtkMenuShell
3272    *  works because we have the pointer grabbed on menu_shell->window
3273    *  with owner_events=TRUE, so all events that are either outside
3274    *  the menu or on its border are delivered relative to
3275    *  menu_shell->window.
3276    */
3277   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3278       pointer_in_menu_window (widget, event->x_root, event->y_root))
3279     return TRUE;
3280
3281   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3282 }
3283
3284 static gboolean
3285 gtk_menu_button_release (GtkWidget      *widget,
3286                          GdkEventButton *event)
3287 {
3288   GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3289
3290   if (priv->ignore_button_release)
3291     {
3292       priv->ignore_button_release = FALSE;
3293       return FALSE;
3294     }
3295
3296   if (event->type != GDK_BUTTON_RELEASE)
3297     return FALSE;
3298
3299   /* Don't pass down to menu shell for releases over scroll arrows
3300    */
3301   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3302     return TRUE;
3303
3304   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3305    *  was clicked (see comment in button_press()).
3306    */
3307   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3308       pointer_in_menu_window (widget, event->x_root, event->y_root))
3309     {
3310       /*  Ugly: make sure menu_shell->button gets reset to 0 when we
3311        *  bail out early here so it is in a consistent state for the
3312        *  next button_press/button_release in GtkMenuShell.
3313        *  See bug #449371.
3314        */
3315       if (GTK_MENU_SHELL (widget)->priv->active)
3316         GTK_MENU_SHELL (widget)->priv->button = 0;
3317
3318       return TRUE;
3319     }
3320
3321   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3322 }
3323
3324 static const gchar *
3325 get_accel_path (GtkWidget *menu_item,
3326                 gboolean  *locked)
3327 {
3328   const gchar *path;
3329   GtkWidget *label;
3330   GClosure *accel_closure;
3331   GtkAccelGroup *accel_group;
3332
3333   path = _gtk_widget_get_accel_path (menu_item, locked);
3334   if (!path)
3335     {
3336       path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3337
3338       if (locked)
3339         {
3340           *locked = TRUE;
3341
3342           label = gtk_bin_get_child (GTK_BIN (menu_item));
3343
3344           if (GTK_IS_ACCEL_LABEL (label))
3345             {
3346               g_object_get (label,
3347                             "accel-closure", &accel_closure,
3348                             NULL);
3349               if (accel_closure)
3350                 {
3351                   accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3352
3353                   *locked = gtk_accel_group_get_is_locked (accel_group);
3354                 }
3355             }
3356         }
3357     }
3358
3359   return path;
3360 }
3361
3362 static gboolean
3363 gtk_menu_key_press (GtkWidget   *widget,
3364                     GdkEventKey *event)
3365 {
3366   GtkMenuShell *menu_shell;
3367   GtkMenu *menu;
3368   gboolean delete = FALSE;
3369   gboolean can_change_accels;
3370   gchar *accel = NULL;
3371   guint accel_key, accel_mods;
3372   GdkModifierType consumed_modifiers;
3373   GdkDisplay *display;
3374
3375   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3376   g_return_val_if_fail (event != NULL, FALSE);
3377
3378   menu_shell = GTK_MENU_SHELL (widget);
3379   menu = GTK_MENU (widget);
3380
3381   gtk_menu_stop_navigating_submenu (menu);
3382
3383   if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3384     return TRUE;
3385
3386   display = gtk_widget_get_display (widget);
3387
3388   g_object_get (gtk_widget_get_settings (widget),
3389                 "gtk-menu-bar-accel", &accel,
3390                 "gtk-can-change-accels", &can_change_accels,
3391                 NULL);
3392
3393   if (accel && *accel)
3394     {
3395       guint keyval = 0;
3396       GdkModifierType mods = 0;
3397
3398       gtk_accelerator_parse (accel, &keyval, &mods);
3399
3400       if (keyval == 0)
3401         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3402
3403       /* FIXME this is wrong, needs to be in the global accel resolution
3404        * thing, to properly consider i18n etc., but that probably requires
3405        * AccelGroup changes etc.
3406        */
3407       if (event->keyval == keyval && (mods & event->state) == mods)
3408         {
3409           gtk_menu_shell_cancel (menu_shell);
3410           g_free (accel);
3411           return TRUE;
3412         }
3413     }
3414
3415   g_free (accel);
3416
3417   switch (event->keyval)
3418     {
3419     case GDK_KEY_Delete:
3420     case GDK_KEY_KP_Delete:
3421     case GDK_KEY_BackSpace:
3422       delete = TRUE;
3423       break;
3424     default:
3425       break;
3426     }
3427
3428   /* Figure out what modifiers went into determining the key symbol */
3429   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3430                                        event->hardware_keycode,
3431                                        event->state, event->group,
3432                                        NULL, NULL, NULL, &consumed_modifiers);
3433
3434   accel_key = gdk_keyval_to_lower (event->keyval);
3435   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3436
3437   /* If lowercasing affects the keysym, then we need to include SHIFT
3438    * in the modifiers, we re-uppercase when we match against the keyval,
3439    * but display and save in caseless form.
3440    */
3441   if (accel_key != event->keyval)
3442     accel_mods |= GDK_SHIFT_MASK;
3443
3444   /* Modify the accelerators */
3445   if (can_change_accels &&
3446       menu_shell->priv->active_menu_item &&
3447       gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3448       GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL &&  /* no submenus */
3449       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3450     {
3451       GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3452       gboolean locked, replace_accels = TRUE;
3453       const gchar *path;
3454
3455       path = get_accel_path (menu_item, &locked);
3456       if (!path || locked)
3457         {
3458           /* Can't change accelerators on menu_items without paths
3459            * (basically, those items are accelerator-locked).
3460            */
3461           gtk_widget_error_bell (widget);
3462         }
3463       else
3464         {
3465           gboolean changed;
3466
3467           /* For the keys that act to delete the current setting,
3468            * we delete the current setting if there is one, otherwise,
3469            * we set the key as the accelerator.
3470            */
3471           if (delete)
3472             {
3473               GtkAccelKey key;
3474
3475               if (gtk_accel_map_lookup_entry (path, &key) &&
3476                   (key.accel_key || key.accel_mods))
3477                 {
3478                   accel_key = 0;
3479                   accel_mods = 0;
3480                 }
3481             }
3482           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3483
3484           if (!changed)
3485             {
3486               /* We failed, probably because this key is in use
3487                * and locked already.
3488                */
3489               gtk_widget_error_bell (widget);
3490             }
3491         }
3492     }
3493
3494   return TRUE;
3495 }
3496
3497 static gboolean
3498 check_threshold (GtkWidget *widget,
3499                  gint       start_x,
3500                  gint       start_y,
3501                  gint       x,
3502                  gint       y)
3503 {
3504 #define THRESHOLD 8
3505
3506   return
3507     ABS (start_x - x) > THRESHOLD ||
3508     ABS (start_y - y) > THRESHOLD;
3509 }
3510
3511 static gboolean
3512 definitely_within_item (GtkWidget *widget,
3513                         gint       x,
3514                         gint       y)
3515 {
3516   GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3517   int w, h;
3518
3519   w = gdk_window_get_width (window);
3520   h = gdk_window_get_height (window);
3521
3522   return
3523     check_threshold (widget, 0, 0, x, y) &&
3524     check_threshold (widget, w - 1, 0, x, y) &&
3525     check_threshold (widget, w - 1, h - 1, x, y) &&
3526     check_threshold (widget, 0, h - 1, x, y);
3527 }
3528
3529 static gboolean
3530 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3531 {
3532   GtkMenuPrivate *priv = menu->priv;
3533
3534   return priv->navigation_height && priv->navigation_width;
3535 }
3536
3537 static gboolean
3538 gtk_menu_motion_notify (GtkWidget      *widget,
3539                         GdkEventMotion *event)
3540 {
3541   GtkWidget *menu_item;
3542   GtkMenu *menu;
3543   GtkMenuShell *menu_shell;
3544   GtkWidget *parent;
3545
3546   gboolean need_enter;
3547
3548   if (GTK_IS_MENU (widget))
3549     {
3550       GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3551
3552       if (priv->ignore_button_release)
3553         priv->ignore_button_release = FALSE;
3554
3555       gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3556                                  TRUE, TRUE);
3557     }
3558
3559   /* We received the event for one of two reasons:
3560    *
3561    * a) We are the active menu, and did gtk_grab_add()
3562    * b) The widget is a child of ours, and the event was propagated
3563    *
3564    * Since for computation of navigation regions, we want the menu which
3565    * is the parent of the menu item, for a), we need to find that menu,
3566    * which may be different from 'widget'.
3567    */
3568   menu_item = gtk_get_event_widget ((GdkEvent*) event);
3569   parent = gtk_widget_get_parent (menu_item);
3570   if (!GTK_IS_MENU_ITEM (menu_item) ||
3571       !GTK_IS_MENU (parent))
3572     return FALSE;
3573
3574   menu_shell = GTK_MENU_SHELL (parent);
3575   menu = GTK_MENU (menu_shell);
3576
3577   if (definitely_within_item (menu_item, event->x, event->y))
3578     menu_shell->priv->activate_time = 0;
3579
3580   need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3581
3582   /* Check to see if we are within an active submenu's navigation region
3583    */
3584   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3585     return TRUE;
3586
3587   /* Make sure we pop down if we enter a non-selectable menu item, so we
3588    * don't show a submenu when the cursor is outside the stay-up triangle.
3589    */
3590   if (!_gtk_menu_item_is_selectable (menu_item))
3591     {
3592       /* We really want to deselect, but this gives the menushell code
3593        * a chance to do some bookkeeping about the menuitem.
3594        */
3595       gtk_menu_shell_select_item (menu_shell, menu_item);
3596       return FALSE;
3597     }
3598
3599   if (need_enter)
3600     {
3601       /* The menu is now sensitive to enter events on its items, but
3602        * was previously sensitive.  So we fake an enter event.
3603        */
3604       menu_shell->priv->ignore_enter = FALSE;
3605
3606       if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3607           event->y >= 0 && event->y < gdk_window_get_height (event->window))
3608         {
3609           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3610           gboolean result;
3611
3612           send_event->crossing.window = g_object_ref (event->window);
3613           send_event->crossing.time = event->time;
3614           send_event->crossing.send_event = TRUE;
3615           send_event->crossing.x_root = event->x_root;
3616           send_event->crossing.y_root = event->y_root;
3617           send_event->crossing.x = event->x;
3618           send_event->crossing.y = event->y;
3619           send_event->crossing.state = event->state;
3620           gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3621
3622           /* We send the event to 'widget', the currently active menu,
3623            * instead of 'menu', the menu that the pointer is in. This
3624            * will ensure that the event will be ignored unless the
3625            * menuitem is a child of the active menu or some parent
3626            * menu of the active menu.
3627            */
3628           result = gtk_widget_event (widget, send_event);
3629           gdk_event_free (send_event);
3630
3631           return result;
3632         }
3633     }
3634
3635   return FALSE;
3636 }
3637
3638 static gboolean
3639 get_double_arrows (GtkMenu *menu)
3640 {
3641   GtkMenuPrivate   *priv = menu->priv;
3642   gboolean          double_arrows;
3643   GtkArrowPlacement arrow_placement;
3644
3645   gtk_widget_style_get (GTK_WIDGET (menu),
3646                         "double-arrows", &double_arrows,
3647                         "arrow-placement", &arrow_placement,
3648                         NULL);
3649
3650   if (arrow_placement != GTK_ARROWS_BOTH)
3651     return TRUE;
3652
3653   return double_arrows || (priv->initially_pushed_in &&
3654                            priv->scroll_offset != 0);
3655 }
3656
3657 static void
3658 gtk_menu_scroll_by (GtkMenu *menu,
3659                     gint     step)
3660 {
3661   GtkMenuPrivate *priv = menu->priv;
3662   GtkBorder arrow_border;
3663   GtkWidget *widget;
3664   gint offset;
3665   gint view_height;
3666   gboolean double_arrows;
3667
3668   widget = GTK_WIDGET (menu);
3669   offset = priv->scroll_offset + step;
3670
3671   get_arrows_border (menu, &arrow_border);
3672
3673   double_arrows = get_double_arrows (menu);
3674
3675   /* If we scroll upward and the non-visible top part
3676    * is smaller than the scroll arrow it would be
3677    * pretty stupid to show the arrow and taking more
3678    * screen space than just scrolling to the top.
3679    */
3680   if (!double_arrows)
3681     if ((step < 0) && (offset < arrow_border.top))
3682       offset = 0;
3683
3684   /* Don't scroll over the top if we weren't before: */
3685   if ((priv->scroll_offset >= 0) && (offset < 0))
3686     offset = 0;
3687
3688   view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3689
3690   if (priv->scroll_offset == 0 &&
3691       view_height >= priv->requested_height)
3692     return;
3693
3694   /* Don't scroll past the bottom if we weren't before: */
3695   if (priv->scroll_offset > 0)
3696     view_height -= arrow_border.top;
3697
3698   /* When both arrows are always shown,
3699    * reduce view height even more.
3700    */
3701   if (double_arrows)
3702     view_height -= arrow_border.bottom;
3703
3704   if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3705       (offset + view_height > priv->requested_height))
3706     offset = priv->requested_height - view_height;
3707
3708   if (offset != priv->scroll_offset)
3709     gtk_menu_scroll_to (menu, offset);
3710 }
3711
3712 static void
3713 gtk_menu_do_timeout_scroll (GtkMenu  *menu,
3714                             gboolean  touchscreen_mode)
3715 {
3716   GtkMenuPrivate *priv = menu->priv;
3717   gboolean upper_visible;
3718   gboolean lower_visible;
3719
3720   upper_visible = priv->upper_arrow_visible;
3721   lower_visible = priv->lower_arrow_visible;
3722
3723   gtk_menu_scroll_by (menu, priv->scroll_step);
3724
3725   if (touchscreen_mode &&
3726       (upper_visible != priv->upper_arrow_visible ||
3727        lower_visible != priv->lower_arrow_visible))
3728     {
3729       /* We are about to hide a scroll arrow while the mouse is pressed,
3730        * this would cause the uncovered menu item to be activated on button
3731        * release. Therefore we need to ignore button release here
3732        */
3733       GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3734       priv->ignore_button_release = TRUE;
3735     }
3736 }
3737
3738 static gboolean
3739 gtk_menu_scroll_timeout (gpointer data)
3740 {
3741   GtkMenu  *menu;
3742   gboolean  touchscreen_mode;
3743
3744   menu = GTK_MENU (data);
3745
3746   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3747                 "gtk-touchscreen-mode", &touchscreen_mode,
3748                 NULL);
3749
3750   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3751
3752   return TRUE;
3753 }
3754
3755 static gboolean
3756 gtk_menu_scroll_timeout_initial (gpointer data)
3757 {
3758   GtkMenu  *menu;
3759   guint     timeout;
3760   gboolean  touchscreen_mode;
3761
3762   menu = GTK_MENU (data);
3763
3764   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3765                 "gtk-timeout-repeat", &timeout,
3766                 "gtk-touchscreen-mode", &touchscreen_mode,
3767                 NULL);
3768
3769   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3770
3771   gtk_menu_remove_scroll_timeout (menu);
3772
3773   menu->priv->scroll_timeout =
3774     gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3775
3776   return FALSE;
3777 }
3778
3779 static void
3780 gtk_menu_start_scrolling (GtkMenu *menu)
3781 {
3782   guint    timeout;
3783   gboolean touchscreen_mode;
3784
3785   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3786                 "gtk-timeout-repeat", &timeout,
3787                 "gtk-touchscreen-mode", &touchscreen_mode,
3788                 NULL);
3789
3790   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3791
3792   menu->priv->scroll_timeout =
3793     gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3794 }
3795
3796 static gboolean
3797 gtk_menu_scroll (GtkWidget      *widget,
3798                  GdkEventScroll *event)
3799 {
3800   GtkMenu *menu = GTK_MENU (widget);
3801
3802   switch (event->direction)
3803     {
3804     case GDK_SCROLL_RIGHT:
3805     case GDK_SCROLL_DOWN:
3806       gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3807       break;
3808     case GDK_SCROLL_LEFT:
3809     case GDK_SCROLL_UP:
3810       gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3811       break;
3812     }
3813
3814   return TRUE;
3815 }
3816
3817 static void
3818 get_arrows_sensitive_area (GtkMenu      *menu,
3819                            GdkRectangle *upper,
3820                            GdkRectangle *lower)
3821 {
3822   GtkArrowPlacement arrow_placement;
3823   GtkWidget *widget = GTK_WIDGET (menu);
3824   GdkWindow *window;
3825   gint width, height;
3826   guint border;
3827   guint vertical_padding;
3828   gint win_x, win_y;
3829   gint scroll_arrow_height;
3830   GtkStyleContext *context;
3831   GtkStateFlags state;
3832   GtkBorder padding;
3833
3834   window = gtk_widget_get_window (widget);
3835   width = gdk_window_get_width (window);
3836   height = gdk_window_get_height (window);
3837
3838   gtk_widget_style_get (widget,
3839                         "vertical-padding", &vertical_padding,
3840                         "scroll-arrow-vlength", &scroll_arrow_height,
3841                         "arrow-placement", &arrow_placement,
3842                         NULL);
3843
3844   border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3845
3846   context = gtk_widget_get_style_context (widget);
3847   state = gtk_widget_get_state_flags (widget);
3848   gtk_style_context_get_padding (context, state, &padding);
3849
3850   gdk_window_get_position (window, &win_x, &win_y);
3851
3852   switch (arrow_placement)
3853     {
3854     case GTK_ARROWS_BOTH:
3855       if (upper)
3856         {
3857           upper->x = win_x;
3858           upper->y = win_y;
3859           upper->width = width;
3860           upper->height = scroll_arrow_height + border + padding.top;
3861         }
3862
3863       if (lower)
3864         {
3865           lower->x = win_x;
3866           lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3867           lower->width = width;
3868           lower->height = scroll_arrow_height + border + padding.bottom;
3869         }
3870       break;
3871
3872     case GTK_ARROWS_START:
3873       if (upper)
3874         {
3875           upper->x = win_x;
3876           upper->y = win_y;
3877           upper->width = width / 2;
3878           upper->height = scroll_arrow_height + border + padding.top;
3879         }
3880
3881       if (lower)
3882         {
3883           lower->x = win_x + width / 2;
3884           lower->y = win_y;
3885           lower->width = width / 2;
3886           lower->height = scroll_arrow_height + border + padding.bottom;
3887         }
3888       break;
3889
3890     case GTK_ARROWS_END:
3891       if (upper)
3892         {
3893           upper->x = win_x;
3894           upper->y = win_y + height - border - scroll_arrow_height;
3895           upper->width = width / 2;
3896           upper->height = scroll_arrow_height + border + padding.top;
3897         }
3898
3899       if (lower)
3900         {
3901           lower->x = win_x + width / 2;
3902           lower->y = win_y + height - border - scroll_arrow_height;
3903           lower->width = width / 2;
3904           lower->height = scroll_arrow_height + border + padding.bottom;
3905         }
3906       break;
3907     }
3908 }
3909
3910
3911 static void
3912 gtk_menu_handle_scrolling (GtkMenu *menu,
3913                            gint     x,
3914                            gint     y,
3915                            gboolean enter,
3916                            gboolean motion)
3917 {
3918   GtkMenuPrivate *priv = menu->priv;
3919   GtkMenuShell *menu_shell;
3920   GdkRectangle rect;
3921   gboolean in_arrow;
3922   gboolean scroll_fast = FALSE;
3923   gint top_x, top_y;
3924   gboolean touchscreen_mode;
3925
3926   menu_shell = GTK_MENU_SHELL (menu);
3927
3928   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3929                 "gtk-touchscreen-mode", &touchscreen_mode,
3930                 NULL);
3931
3932   gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3933                            &top_x, &top_y);
3934   x -= top_x;
3935   y -= top_y;
3936
3937   /*  upper arrow handling  */
3938
3939   get_arrows_sensitive_area (menu, &rect, NULL);
3940
3941   in_arrow = FALSE;
3942   if (priv->upper_arrow_visible && !priv->tearoff_active &&
3943       (x >= rect.x) && (x < rect.x + rect.width) &&
3944       (y >= rect.y) && (y < rect.y + rect.height))
3945     {
3946       in_arrow = TRUE;
3947     }
3948
3949   if (touchscreen_mode)
3950     priv->upper_arrow_prelight = in_arrow;
3951
3952   if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
3953     {
3954       gboolean arrow_pressed = FALSE;
3955
3956       if (priv->upper_arrow_visible && !priv->tearoff_active)
3957         {
3958           if (touchscreen_mode)
3959             {
3960               if (enter && priv->upper_arrow_prelight)
3961                 {
3962                   if (priv->scroll_timeout == 0)
3963                     {
3964                       /* Deselect the active item so that
3965                        * any submenus are popped down
3966                        */
3967                       gtk_menu_shell_deselect (menu_shell);
3968
3969                       gtk_menu_remove_scroll_timeout (menu);
3970                       priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3971
3972                       if (!motion)
3973                         {
3974                           /* Only do stuff on click. */
3975                           gtk_menu_start_scrolling (menu);
3976                           arrow_pressed = TRUE;
3977                         }
3978                     }
3979                   else
3980                     {
3981                       arrow_pressed = TRUE;
3982                     }
3983                 }
3984               else if (!enter)
3985                 {
3986                   gtk_menu_stop_scrolling (menu);
3987                 }
3988             }
3989           else /* !touchscreen_mode */
3990             {
3991               scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3992
3993               if (enter && in_arrow &&
3994                   (!priv->upper_arrow_prelight ||
3995                    priv->scroll_fast != scroll_fast))
3996                 {
3997                   priv->upper_arrow_prelight = TRUE;
3998                   priv->scroll_fast = scroll_fast;
3999
4000                   /* Deselect the active item so that
4001                    * any submenus are popped down
4002                    */
4003                   gtk_menu_shell_deselect (menu_shell);
4004
4005                   gtk_menu_remove_scroll_timeout (menu);
4006                   priv->scroll_step = scroll_fast
4007                                         ? -MENU_SCROLL_STEP2
4008                                         : -MENU_SCROLL_STEP1;
4009
4010                   priv->scroll_timeout =
4011                     gdk_threads_add_timeout (scroll_fast
4012                                                ? MENU_SCROLL_TIMEOUT2
4013                                                : MENU_SCROLL_TIMEOUT1,
4014                                              gtk_menu_scroll_timeout, menu);
4015                 }
4016               else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4017                 {
4018                   gtk_menu_stop_scrolling (menu);
4019                 }
4020             }
4021         }
4022
4023       /*  gtk_menu_start_scrolling() might have hit the top of the
4024        *  menu, so check if the button isn't insensitive before
4025        *  changing it to something else.
4026        */
4027       if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4028         {
4029           GtkStateFlags arrow_state = 0;
4030
4031           if (arrow_pressed)
4032             arrow_state |= GTK_STATE_FLAG_ACTIVE;
4033
4034           if (priv->upper_arrow_prelight)
4035             arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4036
4037           if (arrow_state != priv->upper_arrow_state)
4038             {
4039               priv->upper_arrow_state = arrow_state;
4040
4041               gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4042                                           &rect, FALSE);
4043             }
4044         }
4045     }
4046
4047   /*  lower arrow handling  */
4048
4049   get_arrows_sensitive_area (menu, NULL, &rect);
4050
4051   in_arrow = FALSE;
4052   if (priv->lower_arrow_visible && !priv->tearoff_active &&
4053       (x >= rect.x) && (x < rect.x + rect.width) &&
4054       (y >= rect.y) && (y < rect.y + rect.height))
4055     {
4056       in_arrow = TRUE;
4057     }
4058
4059   if (touchscreen_mode)
4060     priv->lower_arrow_prelight = in_arrow;
4061
4062   if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4063     {
4064       gboolean arrow_pressed = FALSE;
4065
4066       if (priv->lower_arrow_visible && !priv->tearoff_active)
4067         {
4068           if (touchscreen_mode)
4069             {
4070               if (enter && priv->lower_arrow_prelight)
4071                 {
4072                   if (priv->scroll_timeout == 0)
4073                     {
4074                       /* Deselect the active item so that
4075                        * any submenus are popped down
4076                        */
4077                       gtk_menu_shell_deselect (menu_shell);
4078
4079                       gtk_menu_remove_scroll_timeout (menu);
4080                       priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4081
4082                       if (!motion)
4083                         {
4084                           /* Only do stuff on click. */
4085                           gtk_menu_start_scrolling (menu);
4086                           arrow_pressed = TRUE;
4087                         }
4088                     }
4089                   else
4090                     {
4091                       arrow_pressed = TRUE;
4092                     }
4093                 }
4094               else if (!enter)
4095                 {
4096                   gtk_menu_stop_scrolling (menu);
4097                 }
4098             }
4099           else /* !touchscreen_mode */
4100             {
4101               scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4102
4103               if (enter && in_arrow &&
4104                   (!priv->lower_arrow_prelight ||
4105                    priv->scroll_fast != scroll_fast))
4106                 {
4107                   priv->lower_arrow_prelight = TRUE;
4108                   priv->scroll_fast = scroll_fast;
4109
4110                   /* Deselect the active item so that
4111                    * any submenus are popped down
4112                    */
4113                   gtk_menu_shell_deselect (menu_shell);
4114
4115                   gtk_menu_remove_scroll_timeout (menu);
4116                   priv->scroll_step = scroll_fast
4117                                         ? MENU_SCROLL_STEP2
4118                                         : MENU_SCROLL_STEP1;
4119
4120                   priv->scroll_timeout =
4121                     gdk_threads_add_timeout (scroll_fast
4122                                                ? MENU_SCROLL_TIMEOUT2
4123                                                : MENU_SCROLL_TIMEOUT1,
4124                                              gtk_menu_scroll_timeout, menu);
4125                 }
4126               else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4127                 {
4128                   gtk_menu_stop_scrolling (menu);
4129                 }
4130             }
4131         }
4132
4133       /*  gtk_menu_start_scrolling() might have hit the bottom of the
4134        *  menu, so check if the button isn't insensitive before
4135        *  changing it to something else.
4136        */
4137       if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4138         {
4139           GtkStateFlags arrow_state = 0;
4140
4141           if (arrow_pressed)
4142             arrow_state |= GTK_STATE_FLAG_ACTIVE;
4143
4144           if (priv->lower_arrow_prelight)
4145             arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4146
4147           if (arrow_state != priv->lower_arrow_state)
4148             {
4149               priv->lower_arrow_state = arrow_state;
4150
4151               gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4152                                           &rect, FALSE);
4153             }
4154         }
4155     }
4156 }
4157
4158 static gboolean
4159 gtk_menu_enter_notify (GtkWidget        *widget,
4160                        GdkEventCrossing *event)
4161 {
4162   GtkWidget *menu_item;
4163   GtkWidget *parent;
4164   gboolean   touchscreen_mode;
4165
4166   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4167       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4168       event->mode == GDK_CROSSING_STATE_CHANGED)
4169     return TRUE;
4170
4171   g_object_get (gtk_widget_get_settings (widget),
4172                 "gtk-touchscreen-mode", &touchscreen_mode,
4173                 NULL);
4174
4175   menu_item = gtk_get_event_widget ((GdkEvent*) event);
4176   if (GTK_IS_MENU (widget))
4177     {
4178       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4179
4180       if (!menu_shell->priv->ignore_enter)
4181         gtk_menu_handle_scrolling (GTK_MENU (widget),
4182                                    event->x_root, event->y_root, TRUE, TRUE);
4183     }
4184
4185   if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4186     {
4187       GtkWidget *menu = gtk_widget_get_parent (menu_item);
4188
4189       if (GTK_IS_MENU (menu))
4190         {
4191           GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4192           GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4193
4194           if (priv->seen_item_enter)
4195             {
4196               /* This is the second enter we see for an item
4197                * on this menu. This means a release should always
4198                * mean activate.
4199                */
4200               menu_shell->priv->activate_time = 0;
4201             }
4202           else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4203                     event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4204             {
4205               if (definitely_within_item (menu_item, event->x, event->y))
4206                 {
4207                   /* This is an actual user-enter (ie. not a pop-under)
4208                    * In this case, the user must either have entered
4209                    * sufficiently far enough into the item, or he must move
4210                    * far enough away from the enter point. (see
4211                    * gtk_menu_motion_notify())
4212                    */
4213                   menu_shell->priv->activate_time = 0;
4214                 }
4215             }
4216
4217           priv->seen_item_enter = TRUE;
4218         }
4219     }
4220
4221   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4222    * will not correspond to the event widget's parent.  Check to see
4223    * if we are in the parent's navigation region.
4224    */
4225   parent = gtk_widget_get_parent (menu_item);
4226   if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4227       gtk_menu_navigating_submenu (GTK_MENU (parent),
4228                                    event->x_root, event->y_root))
4229     return TRUE;
4230
4231   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4232 }
4233
4234 static gboolean
4235 gtk_menu_leave_notify (GtkWidget        *widget,
4236                        GdkEventCrossing *event)
4237 {
4238   GtkMenuShell *menu_shell;
4239   GtkMenu *menu;
4240   GtkMenuItem *menu_item;
4241   GtkWidget *event_widget;
4242
4243   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4244       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4245       event->mode == GDK_CROSSING_STATE_CHANGED)
4246     return TRUE;
4247
4248   menu = GTK_MENU (widget);
4249   menu_shell = GTK_MENU_SHELL (widget);
4250
4251   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4252     return TRUE;
4253
4254   gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4255
4256   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4257
4258   if (!GTK_IS_MENU_ITEM (event_widget))
4259     return TRUE;
4260
4261   menu_item = GTK_MENU_ITEM (event_widget);
4262
4263   /* Here we check to see if we're leaving an active menu item
4264    * with a submenu, in which case we enter submenu navigation mode.
4265    */
4266   if (menu_shell->priv->active_menu_item != NULL
4267       && menu_item->priv->submenu != NULL
4268       && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4269     {
4270       if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4271         {
4272           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4273           return TRUE;
4274         }
4275       else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4276         {
4277           /* We are leaving an active menu item with nonactive submenu.
4278            * Deselect it so we don't surprise the user with by popping
4279            * up a submenu _after_ he left the item.
4280            */
4281           gtk_menu_shell_deselect (menu_shell);
4282           return TRUE;
4283         }
4284     }
4285
4286   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4287 }
4288
4289 static void
4290 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4291 {
4292   GtkMenuPrivate *priv = menu->priv;
4293
4294   priv->navigation_x = 0;
4295   priv->navigation_y = 0;
4296   priv->navigation_width = 0;
4297   priv->navigation_height = 0;
4298
4299   if (priv->navigation_timeout)
4300     {
4301       g_source_remove (priv->navigation_timeout);
4302       priv->navigation_timeout = 0;
4303     }
4304 }
4305
4306 /* When the timeout is elapsed, the navigation region is destroyed
4307  * and the menuitem under the pointer (if any) is selected.
4308  */
4309 static gboolean
4310 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4311 {
4312   GtkMenuPopdownData *popdown_data = user_data;
4313   GtkMenu *menu = popdown_data->menu;
4314   GtkMenuPrivate *priv = menu->priv;
4315   GdkWindow *child_window;
4316
4317   gtk_menu_stop_navigating_submenu (menu);
4318
4319   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4320     {
4321       child_window = gdk_window_get_device_position (priv->bin_window,
4322                                                      popdown_data->device,
4323                                                      NULL, NULL, NULL);
4324
4325       if (child_window)
4326         {
4327           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4328
4329           send_event->crossing.window = g_object_ref (child_window);
4330           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4331           send_event->crossing.send_event = TRUE;
4332           gdk_event_set_device (send_event, popdown_data->device);
4333
4334           GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4335
4336           gdk_event_free (send_event);
4337         }
4338     }
4339
4340   return FALSE;
4341 }
4342
4343 static gboolean
4344 gtk_menu_navigating_submenu (GtkMenu *menu,
4345                              gint     event_x,
4346                              gint     event_y)
4347 {
4348   GtkMenuPrivate *priv = menu->priv;
4349   gint width, height;
4350
4351   if (!gtk_menu_has_navigation_triangle (menu))
4352     return FALSE;
4353
4354   width = priv->navigation_width;
4355   height = priv->navigation_height;
4356
4357   /* Check if x/y are in the triangle spanned by the navigation parameters */
4358
4359   /* 1) Move the coordinates so the triangle starts at 0,0 */
4360   event_x -= priv->navigation_x;
4361   event_y -= priv->navigation_y;
4362
4363   /* 2) Ensure both legs move along the positive axis */
4364   if (width < 0)
4365     {
4366       event_x = -event_x;
4367       width = -width;
4368     }
4369   if (height < 0)
4370     {
4371       event_y = -event_y;
4372       height = -height;
4373     }
4374
4375   /* 3) Check that the given coordinate is inside the triangle. The formula
4376    * is a transformed form of this formula: x/w + y/h <= 1
4377    */
4378   if (event_x >= 0 && event_y >= 0 &&
4379       event_x * height + event_y * width <= width * height)
4380     {
4381       return TRUE;
4382     }
4383   else
4384     {
4385       gtk_menu_stop_navigating_submenu (menu);
4386       return FALSE;
4387     }
4388 }
4389
4390 static void
4391 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
4392                                         GtkMenuItem      *menu_item,
4393                                         GdkEventCrossing *event)
4394 {
4395   GtkMenuPrivate *priv = menu->priv;
4396   gint submenu_left = 0;
4397   gint submenu_right = 0;
4398   gint submenu_top = 0;
4399   gint submenu_bottom = 0;
4400   gint width = 0;
4401   gint height = 0;
4402   GtkWidget *event_widget;
4403   GtkMenuPopdownData *popdown_data;
4404   GdkWindow *window;
4405
4406   g_return_if_fail (menu_item->priv->submenu != NULL);
4407   g_return_if_fail (event != NULL);
4408
4409   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4410
4411   window = gtk_widget_get_window (menu_item->priv->submenu);
4412   gdk_window_get_origin (window, &submenu_left, &submenu_top);
4413
4414   submenu_right = submenu_left + gdk_window_get_width (window);
4415   submenu_bottom = submenu_top + gdk_window_get_height (window);
4416
4417   width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4418   height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4419
4420   if (event->x >= 0 && event->x < width)
4421     {
4422       gint popdown_delay;
4423
4424       gtk_menu_stop_navigating_submenu (menu);
4425
4426       /* The navigation region is the triangle closest to the x/y
4427        * location of the rectangle. This is why the width or height
4428        * can be negative.
4429        */
4430       if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4431         {
4432           /* right */
4433           priv->navigation_x = submenu_left;
4434           priv->navigation_width = event->x_root - submenu_left;
4435         }
4436       else
4437         {
4438           /* left */
4439           priv->navigation_x = submenu_right;
4440           priv->navigation_width = event->x_root - submenu_right;
4441         }
4442
4443       if (event->y < 0)
4444         {
4445           /* top */
4446           priv->navigation_y = event->y_root;
4447           priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4448
4449           if (priv->navigation_height >= 0)
4450             return;
4451         }
4452       else
4453         {
4454           /* bottom */
4455           priv->navigation_y = event->y_root;
4456           priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4457
4458           if (priv->navigation_height <= 0)
4459             return;
4460         }
4461
4462       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4463                     "gtk-menu-popdown-delay", &popdown_delay,
4464                     NULL);
4465
4466       popdown_data = g_new (GtkMenuPopdownData, 1);
4467       popdown_data->menu = menu;
4468       popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4469
4470       priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4471                                                                popdown_delay,
4472                                                                gtk_menu_stop_navigating_submenu_cb,
4473                                                                popdown_data,
4474                                                                (GDestroyNotify) g_free);
4475     }
4476 }
4477
4478 static void
4479 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4480 {
4481   GtkWidget *parent;
4482
4483   g_return_if_fail (GTK_IS_MENU (menu_shell));
4484
4485   parent = menu_shell->priv->parent_menu_shell;
4486
4487   menu_shell->priv->activate_time = 0;
4488   gtk_menu_popdown (GTK_MENU (menu_shell));
4489
4490   if (parent)
4491     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4492 }
4493
4494 static void
4495 gtk_menu_position (GtkMenu  *menu,
4496                    gboolean  set_scroll_offset)
4497 {
4498   GtkMenuPrivate *priv = menu->priv;
4499   GtkWidget *widget;
4500   GtkRequisition requisition;
4501   gint x, y;
4502   gint scroll_offset;
4503   gint menu_height;
4504   GdkScreen *screen;
4505   GdkScreen *pointer_screen;
4506   GdkRectangle monitor;
4507   GdkDevice *pointer;
4508
4509   widget = GTK_WIDGET (menu);
4510
4511   screen = gtk_widget_get_screen (widget);
4512   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4513   gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4514
4515   /* Get the minimum height for minimum width to figure out
4516    * the right place to popup the menu.
4517    */
4518   gtk_widget_get_preferred_size (widget, &requisition, NULL);
4519
4520   if (pointer_screen != screen)
4521     {
4522       /* Pointer is on a different screen; roughly center the
4523        * menu on the screen. If someone was using multiscreen
4524        * + Xinerama together they'd probably want something
4525        * fancier; but that is likely to be vanishingly rare.
4526        */
4527       x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4528       y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4529     }
4530
4531   priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4532   priv->initially_pushed_in = FALSE;
4533
4534   /* Set the type hint here to allow custom position functions
4535    * to set a different hint
4536    */
4537   if (!gtk_widget_get_visible (priv->toplevel))
4538     gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4539
4540   if (priv->position_func)
4541     {
4542       (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4543                                priv->position_func_data);
4544
4545       if (priv->monitor_num < 0)
4546         priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4547
4548       gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4549     }
4550   else
4551     {
4552       gint space_left, space_right, space_above, space_below;
4553       gint needed_width;
4554       gint needed_height;
4555       GtkBorder border;
4556       gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4557
4558       get_menu_border (widget, &border);
4559
4560       /* The placement of popup menus horizontally works like this (with
4561        * RTL in parentheses)
4562        *
4563        * - If there is enough room to the right (left) of the mouse cursor,
4564        *   position the menu there.
4565        *
4566        * - Otherwise, if if there is enough room to the left (right) of the
4567        *   mouse cursor, position the menu there.
4568        *
4569        * - Otherwise if the menu is smaller than the monitor, position it
4570        *   on the side of the mouse cursor that has the most space available
4571        *
4572        * - Otherwise (if there is simply not enough room for the menu on the
4573        *   monitor), position it as far left (right) as possible.
4574        *
4575        * Positioning in the vertical direction is similar: first try below
4576        * mouse cursor, then above.
4577        */
4578       gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4579
4580       space_left = x - monitor.x;
4581       space_right = monitor.x + monitor.width - x - 1;
4582       space_above = y - monitor.y;
4583       space_below = monitor.y + monitor.height - y - 1;
4584
4585       /* Position horizontally. */
4586
4587       /* the amount of space we need to position the menu.
4588        * Note the menu is offset "thickness" pixels
4589        */
4590       needed_width = requisition.width - border.left;
4591
4592       if (needed_width <= space_left ||
4593           needed_width <= space_right)
4594         {
4595           if ((rtl  && needed_width <= space_left) ||
4596               (!rtl && needed_width >  space_right))
4597             {
4598               /* position left */
4599               x = x + border.left - requisition.width + 1;
4600             }
4601           else
4602             {
4603               /* position right */
4604               x = x - border.right;
4605             }
4606
4607           /* x is clamped on-screen further down */
4608         }
4609       else if (requisition.width <= monitor.width)
4610         {
4611           /* the menu is too big to fit on either side of the mouse
4612            * cursor, but smaller than the monitor. Position it on
4613            * the side that has the most space
4614            */
4615           if (space_left > space_right)
4616             {
4617               /* left justify */
4618               x = monitor.x;
4619             }
4620           else
4621             {
4622               /* right justify */
4623               x = monitor.x + monitor.width - requisition.width;
4624             }
4625         }
4626       else /* menu is simply too big for the monitor */
4627         {
4628           if (rtl)
4629             {
4630               /* right justify */
4631               x = monitor.x + monitor.width - requisition.width;
4632             }
4633           else
4634             {
4635               /* left justify */
4636               x = monitor.x;
4637             }
4638         }
4639
4640       /* Position vertically.
4641        * The algorithm is the same as above, but simpler
4642        * because we don't have to take RTL into account.
4643        */
4644       needed_height = requisition.height - border.top;
4645
4646       if (needed_height <= space_above ||
4647           needed_height <= space_below)
4648         {
4649           if (needed_height <= space_below)
4650             y = y - border.top;
4651           else
4652             y = y + border.bottom - requisition.height + 1;
4653
4654           y = CLAMP (y, monitor.y,
4655                      monitor.y + monitor.height - requisition.height);
4656         }
4657       else if (needed_height > space_below && needed_height > space_above)
4658         {
4659           if (space_below >= space_above)
4660             y = monitor.y + monitor.height - requisition.height;
4661           else
4662             y = monitor.y;
4663         }
4664       else
4665         {
4666           y = monitor.y;
4667         }
4668     }
4669
4670   scroll_offset = 0;
4671
4672   if (priv->initially_pushed_in)
4673     {
4674       menu_height = requisition.height;
4675
4676       if (y + menu_height > monitor.y + monitor.height)
4677         {
4678           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4679           y = (monitor.y + monitor.height) - menu_height;
4680         }
4681
4682       if (y < monitor.y)
4683         {
4684           scroll_offset += monitor.y - y;
4685           y = monitor.y;
4686         }
4687     }
4688
4689   /* FIXME: should this be done in the various position_funcs ? */
4690   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4691
4692   if (GTK_MENU_SHELL (menu)->priv->active)
4693     {
4694       priv->have_position = TRUE;
4695       priv->position_x = x;
4696       priv->position_y = y;
4697     }
4698
4699   if (y + requisition.height > monitor.y + monitor.height)
4700     requisition.height = (monitor.y + monitor.height) - y;
4701
4702   if (y < monitor.y)
4703     {
4704       scroll_offset += monitor.y - y;
4705       requisition.height -= monitor.y - y;
4706       y = monitor.y;
4707     }
4708
4709   if (scroll_offset > 0)
4710     {
4711       GtkBorder arrow_border;
4712
4713       get_arrows_border (menu, &arrow_border);
4714       scroll_offset += arrow_border.top;
4715     }
4716
4717   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4718                    x, y);
4719
4720   if (!GTK_MENU_SHELL (menu)->priv->active)
4721     {
4722       gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4723                          requisition.width, requisition.height);
4724     }
4725
4726   if (set_scroll_offset)
4727     priv->scroll_offset = scroll_offset;
4728 }
4729
4730 static void
4731 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4732 {
4733   GtkMenuPrivate *priv = menu->priv;
4734
4735   if (priv->scroll_timeout)
4736     {
4737       g_source_remove (priv->scroll_timeout);
4738       priv->scroll_timeout = 0;
4739     }
4740 }
4741
4742 static void
4743 gtk_menu_stop_scrolling (GtkMenu *menu)
4744 {
4745   GtkMenuPrivate *priv = menu->priv;
4746   gboolean touchscreen_mode;
4747
4748   gtk_menu_remove_scroll_timeout (menu);
4749
4750   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4751                 "gtk-touchscreen-mode", &touchscreen_mode,
4752                 NULL);
4753
4754   if (!touchscreen_mode)
4755     {
4756       priv->upper_arrow_prelight = FALSE;
4757       priv->lower_arrow_prelight = FALSE;
4758     }
4759 }
4760
4761 static void
4762 gtk_menu_scroll_to (GtkMenu *menu,
4763                     gint    offset)
4764 {
4765   GtkMenuPrivate *priv = menu->priv;
4766   GtkAllocation allocation;
4767   GtkBorder arrow_border, border;
4768   GtkWidget *widget;
4769   gint x, y;
4770   gint view_width, view_height;
4771   gint border_width;
4772   gint menu_height;
4773   guint vertical_padding;
4774   guint horizontal_padding;
4775   gboolean double_arrows;
4776
4777   widget = GTK_WIDGET (menu);
4778
4779   if (priv->tearoff_active && priv->tearoff_adjustment)
4780     gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4781
4782   /* Move/resize the viewport according to arrows: */
4783   gtk_widget_get_allocation (widget, &allocation);
4784   view_width = allocation.width;
4785   view_height = allocation.height;
4786
4787   gtk_widget_style_get (GTK_WIDGET (menu),
4788                         "vertical-padding", &vertical_padding,
4789                         "horizontal-padding", &horizontal_padding,
4790                         NULL);
4791
4792   get_menu_border (widget, &border);
4793   double_arrows = get_double_arrows (menu);
4794
4795   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4796
4797   view_width -= (2 * (border_width + horizontal_padding)) + border.left + border.right;
4798   view_height -= (2 * (border_width + vertical_padding)) + border.top + border.bottom;
4799   menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4800     border.top - border.bottom;
4801
4802   x = border_width + border.left + horizontal_padding;
4803   y = border_width + border.top + vertical_padding;
4804
4805   if (double_arrows && !priv->tearoff_active)
4806     {
4807       if (view_height < menu_height               ||
4808           (offset > 0 && priv->scroll_offset > 0) ||
4809           (offset < 0 && priv->scroll_offset < 0))
4810         {
4811           GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4812           GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4813
4814           if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4815             gtk_widget_queue_draw (GTK_WIDGET (menu));
4816
4817           priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4818
4819           get_arrows_border (menu, &arrow_border);
4820           y += arrow_border.top;
4821           view_height -= arrow_border.top;
4822           view_height -= arrow_border.bottom;
4823
4824           if (offset <= 0)
4825             priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4826           else
4827             {
4828               priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4829
4830               if (priv->upper_arrow_prelight)
4831                 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4832               else
4833                 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4834             }
4835
4836           if (offset >= menu_height - view_height)
4837             priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4838           else
4839             {
4840               priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4841
4842               if (priv->lower_arrow_prelight)
4843                 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4844               else
4845                 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4846             }
4847
4848           if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4849               (priv->lower_arrow_state != lower_arrow_previous_state))
4850             gtk_widget_queue_draw (GTK_WIDGET (menu));
4851
4852           if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4853               (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4854             {
4855               /* At the upper border, possibly remove timeout */
4856               if (priv->scroll_step < 0)
4857                 {
4858                   gtk_menu_stop_scrolling (menu);
4859                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4860                 }
4861             }
4862
4863           if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4864               (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4865             {
4866               /* At the lower border, possibly remove timeout */
4867               if (priv->scroll_step > 0)
4868                 {
4869                   gtk_menu_stop_scrolling (menu);
4870                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4871                 }
4872             }
4873         }
4874       else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4875         {
4876           offset = 0;
4877
4878           priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4879           priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4880
4881           gtk_menu_stop_scrolling (menu);
4882           gtk_widget_queue_draw (GTK_WIDGET (menu));
4883         }
4884     }
4885   else if (!priv->tearoff_active)
4886     {
4887       gboolean last_visible;
4888
4889       last_visible = priv->upper_arrow_visible;
4890       priv->upper_arrow_visible = offset > 0;
4891
4892       /* upper_arrow_visible may have changed, so requery the border */
4893       get_arrows_border (menu, &arrow_border);
4894       view_height -= arrow_border.top;
4895
4896       if ((last_visible != priv->upper_arrow_visible) &&
4897           !priv->upper_arrow_visible)
4898         {
4899           priv->upper_arrow_prelight = FALSE;
4900
4901           /* If we hide the upper arrow, possibly remove timeout */
4902           if (priv->scroll_step < 0)
4903             {
4904               gtk_menu_stop_scrolling (menu);
4905               gtk_widget_queue_draw (GTK_WIDGET (menu));
4906             }
4907         }
4908
4909       last_visible = priv->lower_arrow_visible;
4910       priv->lower_arrow_visible = offset < menu_height - view_height;
4911
4912       /* lower_arrow_visible may have changed, so requery the border */
4913       get_arrows_border (menu, &arrow_border);
4914       view_height -= arrow_border.bottom;
4915
4916       if ((last_visible != priv->lower_arrow_visible) &&
4917            !priv->lower_arrow_visible)
4918         {
4919           priv->lower_arrow_prelight = FALSE;
4920
4921           /* If we hide the lower arrow, possibly remove timeout */
4922           if (priv->scroll_step > 0)
4923             {
4924               gtk_menu_stop_scrolling (menu);
4925               gtk_widget_queue_draw (GTK_WIDGET (menu));
4926             }
4927         }
4928
4929       y += arrow_border.top;
4930     }
4931
4932   /* Scroll the menu: */
4933   if (gtk_widget_get_realized (widget))
4934     gdk_window_move (priv->bin_window, 0, -offset);
4935
4936   if (gtk_widget_get_realized (widget))
4937     gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
4938
4939   priv->scroll_offset = offset;
4940 }
4941
4942 static gboolean
4943 compute_child_offset (GtkMenu   *menu,
4944                       GtkWidget *menu_item,
4945                       gint      *offset,
4946                       gint      *height,
4947                       gboolean  *is_last_child)
4948 {
4949   GtkMenuPrivate *priv = menu->priv;
4950   gint item_top_attach;
4951   gint item_bottom_attach;
4952   gint child_offset = 0;
4953   gint i;
4954
4955   get_effective_child_attach (menu_item, NULL, NULL,
4956                               &item_top_attach, &item_bottom_attach);
4957
4958   /* there is a possibility that we get called before _size_request,
4959    * so check the height table for safety.
4960    */
4961   if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4962     return FALSE;
4963
4964   /* when we have a row with only invisible children, it's height will
4965    * be zero, so there's no need to check WIDGET_VISIBLE here
4966    */
4967   for (i = 0; i < item_top_attach; i++)
4968     child_offset += priv->heights[i];
4969
4970   if (is_last_child)
4971     *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4972   if (offset)
4973     *offset = child_offset;
4974   if (height)
4975     *height = priv->heights[item_top_attach];
4976
4977   return TRUE;
4978 }
4979
4980 static void
4981 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4982                               GtkWidget    *menu_item)
4983 {
4984   GtkMenu *menu = GTK_MENU (menu_shell);
4985   GtkMenuPrivate *priv = menu->priv;
4986   GtkWidget *widget = GTK_WIDGET (menu_shell);
4987   gint child_offset, child_height;
4988   gint width, height;
4989   gint y;
4990   gint arrow_height;
4991   gboolean last_child = 0;
4992
4993   /* We need to check if the selected item fully visible.
4994    * If not we need to scroll the menu so that it becomes fully
4995    * visible.
4996    */
4997
4998   if (compute_child_offset (menu, menu_item,
4999                             &child_offset, &child_height, &last_child))
5000     {
5001       guint vertical_padding;
5002       gboolean double_arrows;
5003       GtkStyleContext *context;
5004       GtkStateFlags state;
5005       GtkBorder padding;
5006
5007       y = priv->scroll_offset;
5008       width = gdk_window_get_width (gtk_widget_get_window (widget));
5009       height = gdk_window_get_height (gtk_widget_get_window (widget));
5010
5011       gtk_widget_style_get (widget,
5012                             "vertical-padding", &vertical_padding,
5013                             NULL);
5014
5015       double_arrows = get_double_arrows (menu);
5016
5017       context = gtk_widget_get_style_context (widget);
5018       state = gtk_widget_get_state_flags (widget);
5019       gtk_style_context_get_padding (context, state, &padding);
5020
5021       height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5022                 padding.top + padding.bottom +
5023                 2 * vertical_padding;
5024       if (child_offset < y)
5025         {
5026           /* Ignore the enter event we might get if the pointer
5027            * is on the menu
5028            */
5029           menu_shell->priv->ignore_enter = TRUE;
5030           gtk_menu_scroll_to (menu, child_offset);
5031         }
5032       else
5033         {
5034           GtkBorder arrow_border;
5035
5036           arrow_height = 0;
5037
5038           get_arrows_border (menu, &arrow_border);
5039           if (!priv->tearoff_active)
5040             arrow_height = arrow_border.top + arrow_border.bottom;
5041
5042           if (child_offset + child_height > y + height - arrow_height)
5043             {
5044               arrow_height = 0;
5045               if ((!last_child && !priv->tearoff_active) || double_arrows)
5046                 arrow_height += arrow_border.bottom;
5047
5048               y = child_offset + child_height - height + arrow_height;
5049               if (((y > 0) && !priv->tearoff_active) || double_arrows)
5050                 {
5051                   /* Need upper arrow */
5052                   arrow_height += arrow_border.top;
5053                   y = child_offset + child_height - height + arrow_height;
5054                 }
5055               /* Ignore the enter event we might get if the pointer
5056                * is on the menu
5057                */
5058               menu_shell->priv->ignore_enter = TRUE;
5059               gtk_menu_scroll_to (menu, y);
5060             }
5061         }
5062     }
5063 }
5064
5065 static void
5066 gtk_menu_select_item (GtkMenuShell *menu_shell,
5067                       GtkWidget    *menu_item)
5068 {
5069   GtkMenu *menu = GTK_MENU (menu_shell);
5070
5071   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5072     gtk_menu_scroll_item_visible (menu_shell, menu_item);
5073
5074   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5075 }
5076
5077
5078 /* Reparent the menu, taking care of the refcounting
5079  *
5080  * If unrealize is true we force a unrealize while reparenting the parent.
5081  * This can help eliminate flicker in some cases.
5082  *
5083  * What happens is that when the menu is unrealized and then re-realized,
5084  * the allocations are as follows:
5085  *
5086  *  parent - 1x1 at (0,0)
5087  *  child1 - 100x20 at (0,0)
5088  *  child2 - 100x20 at (0,20)
5089  *  child3 - 100x20 at (0,40)
5090  *
5091  * That is, the parent is small but the children are full sized. Then,
5092  * when the queued_resize gets processed, the parent gets resized to
5093  * full size.
5094  *
5095  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5096  * contains the following logic:
5097  *
5098  * - if a move or resize operation on a window would change the clip
5099  *   region on the children, then before the window is resized
5100  *   the background for children is temporarily set to None, the
5101  *   move/resize done, and the background for the children restored.
5102  *
5103  * So, at the point where the parent is resized to final size, the
5104  * background for the children is temporarily None, and thus they
5105  * are not cleared to the background color and the previous background
5106  * (the image of the menu) is left in place.
5107  */
5108 static void
5109 gtk_menu_reparent (GtkMenu   *menu,
5110                    GtkWidget *new_parent,
5111                    gboolean   unrealize)
5112 {
5113   GObject *object = G_OBJECT (menu);
5114   GtkWidget *widget = GTK_WIDGET (menu);
5115   gboolean was_floating = g_object_is_floating (object);
5116
5117   g_object_ref_sink (object);
5118
5119   if (unrealize)
5120     {
5121       g_object_ref (object);
5122       gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5123       gtk_container_add (GTK_CONTAINER (new_parent), widget);
5124       g_object_unref (object);
5125     }
5126   else
5127     gtk_widget_reparent (widget, new_parent);
5128
5129   if (was_floating)
5130     g_object_force_floating (object);
5131   else
5132     g_object_unref (object);
5133 }
5134
5135 static void
5136 gtk_menu_show_all (GtkWidget *widget)
5137 {
5138   /* Show children, but not self. */
5139   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5140 }
5141
5142 /**
5143  * gtk_menu_set_screen:
5144  * @menu: a #GtkMenu
5145  * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5146  *          determined by the widget the menu is attached to
5147  *
5148  * Sets the #GdkScreen on which the menu will be displayed.
5149  *
5150  * Since: 2.2
5151  */
5152 void
5153 gtk_menu_set_screen (GtkMenu   *menu,
5154                      GdkScreen *screen)
5155 {
5156   g_return_if_fail (GTK_IS_MENU (menu));
5157   g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5158
5159   g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5160
5161   if (screen)
5162     {
5163       menu_change_screen (menu, screen);
5164     }
5165   else
5166     {
5167       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5168       if (attach_widget)
5169         attach_widget_screen_changed (attach_widget, NULL, menu);
5170     }
5171 }
5172
5173 /**
5174  * gtk_menu_attach:
5175  * @menu: a #GtkMenu
5176  * @child: a #GtkMenuItem
5177  * @left_attach: The column number to attach the left side of the item to
5178  * @right_attach: The column number to attach the right side of the item to
5179  * @top_attach: The row number to attach the top of the item to
5180  * @bottom_attach: The row number to attach the bottom of the item to
5181  *
5182  * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5183  * an item will occupy is specified by @left_attach, @right_attach,
5184  * @top_attach and @bottom_attach. These each represent the leftmost,
5185  * rightmost, uppermost and lower column and row numbers of the table.
5186  * (Columns and rows are indexed from zero).
5187  *
5188  * Note that this function is not related to gtk_menu_detach().
5189  *
5190  * Since: 2.4
5191  */
5192 void
5193 gtk_menu_attach (GtkMenu   *menu,
5194                  GtkWidget *child,
5195                  guint      left_attach,
5196                  guint      right_attach,
5197                  guint      top_attach,
5198                  guint      bottom_attach)
5199 {
5200   GtkMenuShell *menu_shell;
5201   GtkWidget *parent;
5202
5203   g_return_if_fail (GTK_IS_MENU (menu));
5204   g_return_if_fail (GTK_IS_MENU_ITEM (child));
5205   parent = gtk_widget_get_parent (child);
5206   g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5207   g_return_if_fail (left_attach < right_attach);
5208   g_return_if_fail (top_attach < bottom_attach);
5209
5210   menu_shell = GTK_MENU_SHELL (menu);
5211
5212   if (!parent)
5213     {
5214       AttachInfo *ai = get_attach_info (child);
5215
5216       ai->left_attach = left_attach;
5217       ai->right_attach = right_attach;
5218       ai->top_attach = top_attach;
5219       ai->bottom_attach = bottom_attach;
5220
5221       menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5222
5223       gtk_widget_set_parent (child, GTK_WIDGET (menu));
5224
5225       menu_queue_resize (menu);
5226     }
5227   else
5228     {
5229       gtk_container_child_set (GTK_CONTAINER (parent), child,
5230                                "left-attach",   left_attach,
5231                                "right-attach",  right_attach,
5232                                "top-attach",    top_attach,
5233                                "bottom-attach", bottom_attach,
5234                                NULL);
5235     }
5236 }
5237
5238 static gint
5239 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5240 {
5241   gint popup_delay;
5242
5243   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5244                 "gtk-menu-popup-delay", &popup_delay,
5245                 NULL);
5246
5247   return popup_delay;
5248 }
5249
5250 static GtkWidget *
5251 find_child_containing (GtkMenuShell *menu_shell,
5252                        int           left,
5253                        int           right,
5254                        int           top,
5255                        int           bottom)
5256 {
5257   GList *list;
5258
5259   /* find a child which includes the area given by
5260    * left, right, top, bottom.
5261    */
5262   for (list = menu_shell->priv->children; list; list = list->next)
5263     {
5264       gint l, r, t, b;
5265
5266       if (!_gtk_menu_item_is_selectable (list->data))
5267         continue;
5268
5269       get_effective_child_attach (list->data, &l, &r, &t, &b);
5270
5271       if (l <= left && right <= r && t <= top && bottom <= b)
5272         return GTK_WIDGET (list->data);
5273     }
5274
5275   return NULL;
5276 }
5277
5278 static void
5279 gtk_menu_move_current (GtkMenuShell         *menu_shell,
5280                        GtkMenuDirectionType  direction)
5281 {
5282   GtkMenu *menu = GTK_MENU (menu_shell);
5283   gint i;
5284   gint l, r, t, b;
5285   GtkWidget *match = NULL;
5286
5287   if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5288     {
5289       switch (direction)
5290         {
5291         case GTK_MENU_DIR_CHILD:
5292           direction = GTK_MENU_DIR_PARENT;
5293           break;
5294         case GTK_MENU_DIR_PARENT:
5295           direction = GTK_MENU_DIR_CHILD;
5296           break;
5297         default: ;
5298         }
5299     }
5300
5301   /* use special table menu key bindings */
5302   if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5303     {
5304       get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5305
5306       if (direction == GTK_MENU_DIR_NEXT)
5307         {
5308           for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5309             {
5310               match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5311               if (match)
5312                 break;
5313             }
5314
5315           if (!match)
5316             {
5317               /* wrap around */
5318               for (i = 0; i < t; i++)
5319                 {
5320                   match = find_child_containing (menu_shell,
5321                                                  l, l + 1, i, i + 1);
5322                   if (match)
5323                     break;
5324                 }
5325             }
5326         }
5327       else if (direction == GTK_MENU_DIR_PREV)
5328         {
5329           for (i = t; i > 0; i--)
5330             {
5331               match = find_child_containing (menu_shell,
5332                                              l, l + 1, i - 1, i);
5333               if (match)
5334                 break;
5335             }
5336
5337           if (!match)
5338             {
5339               /* wrap around */
5340               for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5341                 {
5342                   match = find_child_containing (menu_shell,
5343                                                  l, l + 1, i - 1, i);
5344                   if (match)
5345                     break;
5346                 }
5347             }
5348         }
5349       else if (direction == GTK_MENU_DIR_PARENT)
5350         {
5351           /* we go one left if possible */
5352           if (l > 0)
5353             match = find_child_containing (menu_shell,
5354                                            l - 1, l, t, t + 1);
5355
5356           if (!match)
5357             {
5358               GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5359
5360               if (!parent
5361                   || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5362                 match = menu_shell->priv->active_menu_item;
5363             }
5364         }
5365       else if (direction == GTK_MENU_DIR_CHILD)
5366         {
5367           /* we go one right if possible */
5368           if (r < gtk_menu_get_n_columns (menu))
5369             match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5370
5371           if (!match)
5372             {
5373               GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5374
5375               if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5376                   (!parent ||
5377                    g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5378                 match = menu_shell->priv->active_menu_item;
5379             }
5380         }
5381
5382       if (match)
5383         {
5384           gtk_menu_shell_select_item (menu_shell, match);
5385           return;
5386         }
5387     }
5388
5389   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5390 }
5391
5392 static gint
5393 get_visible_size (GtkMenu *menu)
5394 {
5395   GtkMenuPrivate *priv = menu->priv;
5396   GtkAllocation allocation;
5397   GtkWidget *widget = GTK_WIDGET (menu);
5398   GtkContainer *container = GTK_CONTAINER (menu);
5399   GtkStyleContext *context;
5400   GtkStateFlags state;
5401   GtkBorder padding;
5402   gint menu_height;
5403
5404   gtk_widget_get_allocation (widget, &allocation);
5405
5406   context = gtk_widget_get_style_context (widget);
5407   state = gtk_widget_get_state_flags (widget);
5408   gtk_style_context_get_padding (context, state, &padding);
5409
5410   menu_height = (allocation.height -
5411                  (2 * gtk_container_get_border_width (container)) -
5412                  padding.top - padding.bottom);
5413
5414   if (!priv->tearoff_active)
5415     {
5416       GtkBorder arrow_border;
5417
5418       get_arrows_border (menu, &arrow_border);
5419       menu_height -= arrow_border.top;
5420       menu_height -= arrow_border.bottom;
5421     }
5422
5423   return menu_height;
5424 }
5425
5426 /* Find the sensitive on-screen child containing @y, or if none,
5427  * the nearest selectable onscreen child. (%NULL if none)
5428  */
5429 static GtkWidget *
5430 child_at (GtkMenu *menu,
5431           gint     y)
5432 {
5433   GtkMenuPrivate *priv = menu->priv;
5434   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5435   GtkWidget *child = NULL;
5436   gint child_offset = 0;
5437   GList *children;
5438   gint menu_height;
5439   gint lower, upper; /* Onscreen bounds */
5440
5441   menu_height = get_visible_size (menu);
5442   lower = priv->scroll_offset;
5443   upper = priv->scroll_offset + menu_height;
5444
5445   for (children = menu_shell->priv->children; children; children = children->next)
5446     {
5447       if (gtk_widget_get_visible (children->data))
5448         {
5449           GtkRequisition child_requisition;
5450
5451           gtk_widget_get_preferred_size (children->data,
5452                                          &child_requisition, NULL);
5453
5454           if (_gtk_menu_item_is_selectable (children->data) &&
5455               child_offset >= lower &&
5456               child_offset + child_requisition.height <= upper)
5457             {
5458               child = children->data;
5459
5460               if (child_offset + child_requisition.height > y &&
5461                   !GTK_IS_TEAROFF_MENU_ITEM (child))
5462                 return child;
5463             }
5464
5465           child_offset += child_requisition.height;
5466         }
5467     }
5468
5469   return child;
5470 }
5471
5472 static gint
5473 get_menu_height (GtkMenu *menu)
5474 {
5475   GtkMenuPrivate *priv = menu->priv;
5476   GtkAllocation allocation;
5477   GtkWidget *widget = GTK_WIDGET (menu);
5478   GtkStyleContext *context;
5479   GtkStateFlags state;
5480   GtkBorder padding, border;
5481   gint height;
5482
5483   gtk_widget_get_allocation (widget, &allocation);
5484
5485   context = gtk_widget_get_style_context (widget);
5486   state = gtk_widget_get_state_flags (widget);
5487   gtk_style_context_get_padding (context, state, &padding);
5488   gtk_style_context_get_border (context, state, &border);
5489
5490   height = allocation.height;
5491   height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5492     padding.top + padding.bottom + border.top + border.bottom;
5493
5494   if (!priv->tearoff_active)
5495     {
5496       GtkBorder arrow_border;
5497
5498       get_arrows_border (menu, &arrow_border);
5499       height -= arrow_border.top;
5500       height -= arrow_border.bottom;
5501     }
5502
5503   return height;
5504 }
5505
5506 static void
5507 gtk_menu_real_move_scroll (GtkMenu       *menu,
5508                            GtkScrollType  type)
5509 {
5510   GtkMenuPrivate *priv = menu->priv;
5511   gint page_size = get_visible_size (menu);
5512   gint end_position = get_menu_height (menu);
5513   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5514
5515   switch (type)
5516     {
5517     case GTK_SCROLL_PAGE_UP:
5518     case GTK_SCROLL_PAGE_DOWN:
5519       {
5520         gint old_offset;
5521         gint new_offset;
5522         gint child_offset = 0;
5523         gboolean old_upper_arrow_visible;
5524         gint step;
5525
5526         if (type == GTK_SCROLL_PAGE_UP)
5527           step = - page_size;
5528         else
5529           step = page_size;
5530
5531         if (menu_shell->priv->active_menu_item)
5532           {
5533             gint child_height;
5534
5535             compute_child_offset (menu, menu_shell->priv->active_menu_item,
5536                                   &child_offset, &child_height, NULL);
5537             child_offset += child_height / 2;
5538           }
5539
5540         menu_shell->priv->ignore_enter = TRUE;
5541         old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5542         old_offset = priv->scroll_offset;
5543
5544         new_offset = priv->scroll_offset + step;
5545         new_offset = CLAMP (new_offset, 0, end_position - page_size);
5546
5547         gtk_menu_scroll_to (menu, new_offset);
5548
5549         if (menu_shell->priv->active_menu_item)
5550           {
5551             GtkWidget *new_child;
5552             gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5553             GtkBorder arrow_border;
5554             get_arrows_border (menu, &arrow_border);
5555
5556             if (priv->scroll_offset != old_offset)
5557               step = priv->scroll_offset - old_offset;
5558
5559             step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5560
5561             new_child = child_at (menu, child_offset + step);
5562             if (new_child)
5563               gtk_menu_shell_select_item (menu_shell, new_child);
5564           }
5565       }
5566       break;
5567     case GTK_SCROLL_START:
5568       /* Ignore the enter event we might get if the pointer is on the menu */
5569       menu_shell->priv->ignore_enter = TRUE;
5570       gtk_menu_scroll_to (menu, 0);
5571       gtk_menu_shell_select_first (menu_shell, TRUE);
5572       break;
5573     case GTK_SCROLL_END:
5574       /* Ignore the enter event we might get if the pointer is on the menu */
5575       menu_shell->priv->ignore_enter = TRUE;
5576       gtk_menu_scroll_to (menu, end_position - page_size);
5577       _gtk_menu_shell_select_last (menu_shell, TRUE);
5578       break;
5579     default:
5580       break;
5581     }
5582 }
5583
5584
5585 /**
5586  * gtk_menu_set_monitor:
5587  * @menu: a #GtkMenu
5588  * @monitor_num: the number of the monitor on which the menu should
5589  *    be popped up
5590  *
5591  * Informs GTK+ on which monitor a menu should be popped up.
5592  * See gdk_screen_get_monitor_geometry().
5593  *
5594  * This function should be called from a #GtkMenuPositionFunc
5595  * if the menu should not appear on the same monitor as the pointer.
5596  * This information can't be reliably inferred from the coordinates
5597  * returned by a #GtkMenuPositionFunc, since, for very long menus,
5598  * these coordinates may extend beyond the monitor boundaries or even
5599  * the screen boundaries.
5600  *
5601  * Since: 2.4
5602  */
5603 void
5604 gtk_menu_set_monitor (GtkMenu *menu,
5605                       gint     monitor_num)
5606 {
5607   GtkMenuPrivate *priv = menu->priv;
5608
5609   g_return_if_fail (GTK_IS_MENU (menu));
5610
5611   priv->monitor_num = monitor_num;
5612 }
5613
5614 /**
5615  * gtk_menu_get_monitor:
5616  * @menu: a #GtkMenu
5617  *
5618  * Retrieves the number of the monitor on which to show the menu.
5619  *
5620  * Returns: the number of the monitor on which the menu should
5621  *    be popped up or -1, if no monitor has been set
5622  *
5623  * Since: 2.14
5624  */
5625 gint
5626 gtk_menu_get_monitor (GtkMenu *menu)
5627 {
5628   g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5629
5630   return menu->priv->monitor_num;
5631 }
5632
5633 /**
5634  * gtk_menu_get_for_attach_widget:
5635  * @widget: a #GtkWidget
5636  *
5637  * Returns a list of the menus which are attached to this widget.
5638  * This list is owned by GTK+ and must not be modified.
5639  *
5640  * Return value: (element-type GtkWidget) (transfer none): the list
5641  *     of menus attached to his widget.
5642  *
5643  * Since: 2.6
5644  */
5645 GList*
5646 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5647 {
5648   GList *list;
5649
5650   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5651
5652   list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5653
5654   return list;
5655 }
5656
5657 static void
5658 gtk_menu_grab_notify (GtkWidget *widget,
5659                       gboolean   was_grabbed)
5660 {
5661   GtkWidget *toplevel;
5662   GtkWindowGroup *group;
5663   GtkWidget *grab;
5664   GdkDevice *pointer;
5665
5666   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5667
5668   if (!pointer ||
5669       !gtk_widget_device_is_shadowed (widget, pointer))
5670     return;
5671
5672   toplevel = gtk_widget_get_toplevel (widget);
5673
5674   if (!GTK_IS_WINDOW (toplevel))
5675     return;
5676
5677   group = gtk_window_get_group (GTK_WINDOW (toplevel));
5678   grab = gtk_window_group_get_current_device_grab (group, pointer);
5679
5680   if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5681     gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5682 }
5683
5684 /**
5685  * gtk_menu_set_reserve_toggle_size:
5686  * @menu: a #GtkMenu
5687  * @reserve_toggle_size: whether to reserve size for toggles
5688  *
5689  * Sets whether the menu should reserve space for drawing toggles
5690  * or icons, regardless of their actual presence.
5691  *
5692  * Since: 2.18
5693  */
5694 void
5695 gtk_menu_set_reserve_toggle_size (GtkMenu  *menu,
5696                                   gboolean  reserve_toggle_size)
5697 {
5698   GtkMenuPrivate *priv = menu->priv;
5699   gboolean no_toggle_size;
5700
5701   g_return_if_fail (GTK_IS_MENU (menu));
5702
5703   no_toggle_size = !reserve_toggle_size;
5704
5705   if (priv->no_toggle_size != no_toggle_size)
5706     {
5707       priv->no_toggle_size = no_toggle_size;
5708
5709       g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5710     }
5711 }
5712
5713 /**
5714  * gtk_menu_get_reserve_toggle_size:
5715  * @menu: a #GtkMenu
5716  *
5717  * Returns whether the menu reserves space for toggles and
5718  * icons, regardless of their actual presence.
5719  *
5720  * Returns: Whether the menu reserves toggle space
5721  *
5722  * Since: 2.18
5723  */
5724 gboolean
5725 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5726 {
5727   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5728
5729   return !menu->priv->no_toggle_size;
5730 }