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