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