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