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