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