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