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