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