gtkmenu.c 115 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Tim Janik's avatar
Tim Janik committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Manish Singh's avatar
Manish Singh committed
27 28
#define GTK_MENU_INTERNALS

29
#include <config.h>
30
#include <string.h> /* memset */
Elliot Lee's avatar
Elliot Lee committed
31
#include "gdk/gdkkeysyms.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "gtkaccellabel.h"
33
#include "gtkaccelmap.h"
34 35
#include "gtkbindings.h"
#include "gtklabel.h"
Elliot Lee's avatar
Elliot Lee committed
36
#include "gtkmain.h"
37
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
38 39
#include "gtkmenu.h"
#include "gtkmenuitem.h"
40
#include "gtktearoffmenuitem.h"
41
#include "gtkwindow.h"
42 43
#include "gtkhbox.h"
#include "gtkvscrollbar.h"
Havoc Pennington's avatar
Havoc Pennington committed
44
#include "gtksettings.h"
45
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
46 47


48
#define MENU_ITEM_CLASS(w)   GTK_MENU_ITEM_GET_CLASS (w)
Tim Janik's avatar
Tim Janik committed
49
#define	MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
Elliot Lee's avatar
Elliot Lee committed
50

51
#define DEFAULT_POPUP_DELAY    225
52 53
#define DEFAULT_POPDOWN_DELAY  1000

54 55 56
#define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
					 * extends below the submenu
					 */
57

58 59
#define MENU_SCROLL_STEP1 8
#define MENU_SCROLL_STEP2 15
60
#define MENU_SCROLL_ARROW_HEIGHT 16
61 62
#define MENU_SCROLL_FAST_ZONE 8
#define MENU_SCROLL_TIMEOUT1 50
63 64
#define MENU_SCROLL_TIMEOUT2 50

65 66
#define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"

Tim Janik's avatar
Tim Janik committed
67
typedef struct _GtkMenuAttachData	GtkMenuAttachData;
68
typedef struct _GtkMenuPrivate  	GtkMenuPrivate;
Tim Janik's avatar
Tim Janik committed
69 70 71 72 73 74 75

struct _GtkMenuAttachData
{
  GtkWidget *attach_widget;
  GtkMenuDetachFunc detacher;
};

76 77 78 79 80
struct _GtkMenuPrivate 
{
  gboolean have_position;
  gint x;
  gint y;
81 82 83 84 85 86 87

  /* info used for the table */
  guint rows;
  guint columns;

  guint *heights;
  gint heights_length;
88 89

  gint monitor_num;
90 91
};

92 93 94 95 96 97 98 99 100
typedef struct
{
  guint left_attach;
  guint right_attach;
  guint top_attach;
  guint bottom_attach;
}
AttachInfo;

101 102 103 104 105
enum {
  MOVE_SCROLL,
  LAST_SIGNAL
};

106 107 108 109
enum {
  PROP_0,
  PROP_TEAROFF_TITLE
};
Tim Janik's avatar
Tim Janik committed
110

111 112 113 114 115 116 117 118 119
enum
{
  CHILD_PROP_0,
  CHILD_PROP_LEFT_ATTACH,
  CHILD_PROP_RIGHT_ATTACH,
  CHILD_PROP_TOP_ATTACH,
  CHILD_PROP_BOTTOM_ATTACH
};

120 121
static void     gtk_menu_class_init        (GtkMenuClass     *klass);
static void     gtk_menu_init              (GtkMenu          *menu);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
static void     gtk_menu_set_property      (GObject          *object,
					    guint             prop_id,
					    const GValue     *value,
					    GParamSpec       *pspec);
static void     gtk_menu_get_property      (GObject          *object,
					    guint             prop_id,
					    GValue           *value,
					    GParamSpec       *pspec);
static void     gtk_menu_set_child_property(GtkContainer     *container,
                                            GtkWidget        *child,
                                            guint             property_id,
                                            const GValue     *value,
                                            GParamSpec       *pspec);
static void     gtk_menu_get_child_property(GtkContainer     *container,
                                            GtkWidget        *child,
                                            guint             property_id,
                                            GValue           *value,
                                            GParamSpec       *pspec);
140
static void     gtk_menu_destroy           (GtkObject        *object);
141
static void     gtk_menu_finalize          (GObject          *object);
142 143 144 145 146 147
static void     gtk_menu_realize           (GtkWidget        *widget);
static void     gtk_menu_unrealize         (GtkWidget        *widget);
static void     gtk_menu_size_request      (GtkWidget        *widget,
					    GtkRequisition   *requisition);
static void     gtk_menu_size_allocate     (GtkWidget        *widget,
					    GtkAllocation    *allocation);
148 149
static void     gtk_menu_paint             (GtkWidget        *widget,
					    GdkEventExpose   *expose);
150
static void     gtk_menu_show              (GtkWidget        *widget);
151 152 153 154
static gboolean gtk_menu_expose            (GtkWidget        *widget,
					    GdkEventExpose   *event);
static gboolean gtk_menu_key_press         (GtkWidget        *widget,
					    GdkEventKey      *event);
155 156 157 158
static gboolean gtk_menu_button_press      (GtkWidget        *widget,
					    GdkEventButton   *event);
static gboolean gtk_menu_button_release    (GtkWidget        *widget,
					    GdkEventButton   *event);
159 160 161 162 163 164 165 166
static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
					    GdkEventMotion   *event);
static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
					    GdkEventCrossing *event);
static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
					    GdkEventCrossing *event);
static void     gtk_menu_scroll_to         (GtkMenu          *menu,
					    gint              offset);
167 168 169 170 171

static void     gtk_menu_stop_scrolling        (GtkMenu  *menu);
static void     gtk_menu_remove_scroll_timeout (GtkMenu  *menu);
static gboolean gtk_menu_scroll_timeout        (gpointer  data);

172 173
static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
					      GtkWidget       *menu_item);
174 175
static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
					    GtkWidget        *menu_item);
176 177 178
static void     gtk_menu_do_insert         (GtkMenuShell     *menu_shell,
                                            GtkWidget        *child,
                                            gint              position);
179
static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
180 181 182 183 184 185 186 187
					    GtkWidget        *child,
					    gint              position);
static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
					    GtkMenu          *menu);
static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
					    gboolean         enter);
static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
					    gint             width);
188 189
static void     gtk_menu_style_set         (GtkWidget        *widget,
					    GtkStyle         *previous_style);
190 191
static gboolean gtk_menu_focus             (GtkWidget        *widget,
					    GtkDirectionType direction);
192
static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
193 194
static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
                                            GtkMenuDirectionType direction);
195 196
static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
					    GtkScrollType     type);
197 198 199 200 201 202 203 204 205 206

static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
							gint              event_x,
							gint              event_y);
static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
							GtkMenuItem      *menu_item,
							GdkEventCrossing *event);
 
Tim Janik's avatar
Tim Janik committed
207
static void gtk_menu_deactivate	    (GtkMenuShell      *menu_shell);
208 209
static void gtk_menu_show_all       (GtkWidget         *widget);
static void gtk_menu_hide_all       (GtkWidget         *widget);
210
static void gtk_menu_position       (GtkMenu           *menu);
Owen Taylor's avatar
Owen Taylor committed
211 212 213
static void gtk_menu_reparent       (GtkMenu           *menu, 
				     GtkWidget         *new_parent, 
				     gboolean           unrealize);
214 215
static void gtk_menu_remove         (GtkContainer      *menu,
				     GtkWidget         *widget);
Elliot Lee's avatar
Elliot Lee committed
216

217 218
static void gtk_menu_update_title   (GtkMenu           *menu);

219 220 221
static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);

222 223
static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
                                                  guint      signal_id);
224 225 226
static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
					   gboolean group_changed);

227
static GtkMenuShellClass *parent_class = NULL;
228
static const gchar	 *attach_data_key = "gtk-menu-attach-data";
Tim Janik's avatar
Tim Janik committed
229

230 231
static guint menu_signals[LAST_SIGNAL] = { 0 };

232 233 234 235 236 237
static void
gtk_menu_free_private (gpointer data)
{
  GtkMenuPrivate *priv = (GtkMenuPrivate *)data;

  g_free (priv->heights);
Matthias Clasen's avatar
Matthias Clasen committed
238 239

  g_free (priv);
240 241
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
GtkMenuPrivate *
gtk_menu_get_private (GtkMenu *menu)
{
  GtkMenuPrivate *private;
  static GQuark private_quark = 0;

  if (!private_quark)
    private_quark = g_quark_from_static_string ("gtk-menu-private");

  private = g_object_get_qdata (G_OBJECT (menu), private_quark);

  if (!private)
    {
      private = g_new0 (GtkMenuPrivate, 1);
      private->have_position = FALSE;
      
      g_object_set_qdata_full (G_OBJECT (menu), private_quark,
259
			       private, gtk_menu_free_private);
260 261 262 263 264
    }

  return private;
}

Manish Singh's avatar
Manish Singh committed
265
GType
266
gtk_menu_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
267
{
Manish Singh's avatar
Manish Singh committed
268
  static GType menu_type = 0;
Tim Janik's avatar
Tim Janik committed
269
  
Elliot Lee's avatar
Elliot Lee committed
270 271
  if (!menu_type)
    {
Manish Singh's avatar
Manish Singh committed
272
      static const GTypeInfo menu_info =
Elliot Lee's avatar
Elliot Lee committed
273 274
      {
	sizeof (GtkMenuClass),
Manish Singh's avatar
Manish Singh committed
275 276 277 278 279 280 281 282
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_menu_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkMenu),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_menu_init,
Elliot Lee's avatar
Elliot Lee committed
283
      };
Tim Janik's avatar
Tim Janik committed
284
      
Manish Singh's avatar
Manish Singh committed
285 286
      menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
					  &menu_info, 0);
Elliot Lee's avatar
Elliot Lee committed
287
    }
Tim Janik's avatar
Tim Janik committed
288
  
Elliot Lee's avatar
Elliot Lee committed
289 290 291 292 293 294
  return menu_type;
}

static void
gtk_menu_class_init (GtkMenuClass *class)
{
295 296 297 298 299
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
  GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
300
  GtkBindingSet *binding_set;
Tim Janik's avatar
Tim Janik committed
301
  
302
  parent_class = g_type_class_peek_parent (class);
Tim Janik's avatar
Tim Janik committed
303
  
304
  gobject_class->finalize = gtk_menu_finalize;
305 306 307
  gobject_class->set_property = gtk_menu_set_property;
  gobject_class->get_property = gtk_menu_get_property;

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  object_class->destroy = gtk_menu_destroy;
  
  widget_class->realize = gtk_menu_realize;
  widget_class->unrealize = gtk_menu_unrealize;
  widget_class->size_request = gtk_menu_size_request;
  widget_class->size_allocate = gtk_menu_size_allocate;
  widget_class->show = gtk_menu_show;
  widget_class->expose_event = gtk_menu_expose;
  widget_class->key_press_event = gtk_menu_key_press;
  widget_class->button_press_event = gtk_menu_button_press;
  widget_class->button_release_event = gtk_menu_button_release;
  widget_class->motion_notify_event = gtk_menu_motion_notify;
  widget_class->show_all = gtk_menu_show_all;
  widget_class->hide_all = gtk_menu_hide_all;
  widget_class->enter_notify_event = gtk_menu_enter_notify;
  widget_class->leave_notify_event = gtk_menu_leave_notify;
  widget_class->motion_notify_event = gtk_menu_motion_notify;
  widget_class->style_set = gtk_menu_style_set;
  widget_class->focus = gtk_menu_focus;
327
  widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
328 329 330 331 332 333 334 335 336 337 338 339

  container_class->remove = gtk_menu_remove;
  container_class->get_child_property = gtk_menu_get_child_property;
  container_class->set_child_property = gtk_menu_set_child_property;
  
  menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
  menu_shell_class->deactivate = gtk_menu_deactivate;
  menu_shell_class->select_item = gtk_menu_select_item;
  menu_shell_class->insert = gtk_menu_real_insert;
  menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
  menu_shell_class->move_current = gtk_menu_move_current;

340 341 342 343 344 345 346 347 348 349
  menu_signals[MOVE_SCROLL] =
    _gtk_binding_signal_new ("move_scroll",
			     G_OBJECT_CLASS_TYPE (object_class),
			     G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
			     G_CALLBACK (gtk_menu_real_move_scroll),
			     NULL, NULL,
			     _gtk_marshal_VOID__ENUM,
			     G_TYPE_NONE, 1,
			     GTK_TYPE_SCROLL_TYPE);
  
350 351 352
  g_object_class_install_property (gobject_class,
                                   PROP_TEAROFF_TITLE,
                                   g_param_spec_string ("tearoff-title",
353 354
                                                        P_("Tearoff Title"),
                                                        P_("A title that may be displayed by the window manager when this menu is torn-off"),
355 356
                                                        "",
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
Manish Singh's avatar
Manish Singh committed
357

358 359
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("vertical-padding",
360 361
							     P_("Vertical Padding"),
							     P_("Extra space at the top and bottom of the menu"),
362 363 364 365 366
							     0,
							     G_MAXINT,
							     1,
							     G_PARAM_READABLE));

367 368
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("vertical-offset",
369 370
							     P_("Vertical Offset"),
							     P_("When the menu is a submenu, position it this number of pixels offset vertically"),
371 372 373 374 375 376 377
							     G_MININT,
							     G_MAXINT,
							     0,
							     G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("horizontal-offset",
378 379
							     P_("Horizontal Offset"),
							     P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
380 381 382 383 384
							     G_MININT,
							     G_MAXINT,
							     -2,
							     G_PARAM_READABLE));

385

386 387 388
 gtk_container_class_install_child_property (container_class,
                                             CHILD_PROP_LEFT_ATTACH,
                                             g_param_spec_uint ("left_attach",
389 390
                                                               P_("Left Attach"),
                                                               P_("The column number to attach the left side of the child to"),
391 392 393 394 395 396
                                                               0, UINT_MAX, 0,
                                                               G_PARAM_READWRITE));

 gtk_container_class_install_child_property (container_class,
                                             CHILD_PROP_RIGHT_ATTACH,
                                             g_param_spec_uint ("right_attach",
397 398
                                                               P_("Right Attach"),
                                                               P_("The column number to attach the right side of the child to"),
399 400 401 402 403 404
                                                               0, UINT_MAX, 0,
                                                               G_PARAM_READWRITE));

 gtk_container_class_install_child_property (container_class,
                                             CHILD_PROP_TOP_ATTACH,
                                             g_param_spec_uint ("top_attach",
405 406
                                                               P_("Top Attach"),
                                                               P_("The row number to attach the top of the child to"),
407 408 409 410 411 412
                                                               0, UINT_MAX, 0,
                                                               G_PARAM_READWRITE));

 gtk_container_class_install_child_property (container_class,
                                             CHILD_PROP_BOTTOM_ATTACH,
                                             g_param_spec_uint ("bottom_attach",
413 414
                                                               P_("Bottom Attach"),
                                                               P_("The row number to attach the bottom of the child to"),
415 416
                                                               0, UINT_MAX, 0,
                                                               G_PARAM_READWRITE));
417 418 419 420 421 422 423

  binding_set = gtk_binding_set_by_class (class);
  gtk_binding_entry_add_signal (binding_set,
				GDK_Up, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PREV);
424 425 426 427 428
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Up, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PREV);
429 430 431 432 433
  gtk_binding_entry_add_signal (binding_set,
				GDK_Down, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_NEXT);
434 435 436 437 438
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Down, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_NEXT);
439 440 441 442 443
  gtk_binding_entry_add_signal (binding_set,
				GDK_Left, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PARENT);
444 445 446 447 448
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Left, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_PARENT);
449 450 451 452 453
  gtk_binding_entry_add_signal (binding_set,
				GDK_Right, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_CHILD);
454 455 456 457 458
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Right, 0,
				"move_current", 1,
				GTK_TYPE_MENU_DIRECTION_TYPE,
				GTK_MENU_DIR_CHILD);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  gtk_binding_entry_add_signal (binding_set,
				GDK_Home, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_START);
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Home, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_START);
  gtk_binding_entry_add_signal (binding_set,
				GDK_End, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_END);
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_End, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_END);
  gtk_binding_entry_add_signal (binding_set,
				GDK_Page_Up, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_PAGE_UP);
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Page_Up, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_PAGE_UP);
  gtk_binding_entry_add_signal (binding_set,
				GDK_Page_Down, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_PAGE_DOWN);
  gtk_binding_entry_add_signal (binding_set,
				GDK_KP_Page_Down, 0,
				"move_scroll", 1,
				GTK_TYPE_SCROLL_TYPE,
				GTK_SCROLL_PAGE_DOWN);
499 500

  gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
501 502
						       P_("Can change accelerators"),
						       P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
Owen Taylor's avatar
Owen Taylor committed
503
						       FALSE,
504
						       G_PARAM_READWRITE));
505 506

  gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
507 508
						   P_("Delay before submenus appear"),
						   P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
509 510 511 512 513 514
						   0,
						   G_MAXINT,
						   DEFAULT_POPUP_DELAY,
						   G_PARAM_READWRITE));

  gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
515 516
						   P_("Delay before hiding a submenu"),
						   P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
517 518 519 520 521
						   0,
						   G_MAXINT,
						   DEFAULT_POPDOWN_DELAY,
						   G_PARAM_READWRITE));
						   
Elliot Lee's avatar
Elliot Lee committed
522 523
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558

static void 
gtk_menu_set_property (GObject      *object,
		       guint         prop_id,
		       const GValue *value,
		       GParamSpec   *pspec)
{
  GtkMenu *menu;
  
  menu = GTK_MENU (object);
  
  switch (prop_id)
    {
    case PROP_TEAROFF_TITLE:
      gtk_menu_set_title (menu, g_value_get_string (value));
      break;	  
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void 
gtk_menu_get_property (GObject     *object,
		       guint        prop_id,
		       GValue      *value,
		       GParamSpec  *pspec)
{
  GtkMenu *menu;
  
  menu = GTK_MENU (object);
  
  switch (prop_id)
    {
    case PROP_TEAROFF_TITLE:
559
      g_value_set_string (value, gtk_menu_get_title (menu));
560 561 562 563 564 565 566
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
static AttachInfo *
get_attach_info (GObject *child)
{
  AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);

  if (!ai)
    {
      ai = g_new0 (AttachInfo, 1);
      g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
    }

  return ai;
}

static void
gtk_menu_set_child_property (GtkContainer *container,
                             GtkWidget    *child,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkMenu *menu = GTK_MENU (container);
  GtkMenuPrivate *priv;
  AttachInfo *ai = get_attach_info (G_OBJECT (child));

  priv = gtk_menu_get_private (menu);

  switch (property_id)
    {
      case CHILD_PROP_LEFT_ATTACH:
        ai->left_attach = g_value_get_uint (value);
        break;
      case CHILD_PROP_RIGHT_ATTACH:
        ai->right_attach = g_value_get_uint (value);
601
	priv->columns = MAX (priv->columns, ai->right_attach);
602 603 604 605 606 607
        break;
      case CHILD_PROP_TOP_ATTACH:
        ai->top_attach = g_value_get_uint (value);
        break;
      case CHILD_PROP_BOTTOM_ATTACH:
        ai->bottom_attach = g_value_get_uint (value);
608
	priv->rows = MAX (priv->rows, ai->bottom_attach);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
        break;
      default:
        GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
        return;
    }

  gtk_widget_queue_resize (GTK_WIDGET (menu));
}

static void
gtk_menu_get_child_property (GtkContainer *container,
                             GtkWidget    *child,
                             guint         property_id,
                             GValue       *value,
                             GParamSpec   *pspec)
{
  AttachInfo *ai = get_attach_info (G_OBJECT (child));

  switch (property_id)
    {
      case CHILD_PROP_LEFT_ATTACH:
        g_value_set_uint (value, ai->left_attach);
        break;
      case CHILD_PROP_RIGHT_ATTACH:
        g_value_set_uint (value, ai->right_attach);
        break;
      case CHILD_PROP_TOP_ATTACH:
        g_value_set_uint (value, ai->top_attach);
        break;
      case CHILD_PROP_BOTTOM_ATTACH:
        g_value_set_uint (value, ai->bottom_attach);
        break;

      default:
        GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
        return;
    }
}

static void
get_child_attach (GtkWidget *child,
                  gint      *l,
                  gint      *r,
                  gint      *t,
                  gint      *b)
{
  gtk_container_child_get (GTK_CONTAINER (child->parent), child,
                           "left_attach", l,
                           "right_attach", r,
                           "top_attach", t,
                           "bottom_attach", b,
                           NULL);
}

663
static gboolean
664 665 666 667 668 669
gtk_menu_window_event (GtkWidget *window,
		       GdkEvent  *event,
		       GtkWidget *menu)
{
  gboolean handled = FALSE;

Manish Singh's avatar
Manish Singh committed
670 671
  g_object_ref (window);
  g_object_ref (menu);
672 673 674 675 676

  switch (event->type)
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
677
      handled = gtk_widget_event (menu, event);
678 679 680 681 682
      break;
    default:
      break;
    }

Manish Singh's avatar
Manish Singh committed
683 684
  g_object_unref (window);
  g_object_unref (menu);
685 686 687 688

  return handled;
}

689 690 691 692 693 694 695 696 697
static void
gtk_menu_window_size_request (GtkWidget      *window,
			      GtkRequisition *requisition,
			      GtkMenu        *menu)
{
  GtkMenuPrivate *private = gtk_menu_get_private (menu);

  if (private->have_position)
    {
698
      GdkScreen *screen = gtk_widget_get_screen (window);
699 700
      GdkRectangle monitor;
      
701
      gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
702 703 704

      if (private->y + requisition->height > monitor.y + monitor.height)
	requisition->height = monitor.y + monitor.height - private->y;
705

706 707
      if (private->y < monitor.y)
	requisition->height -= monitor.y - private->y;
708 709 710
    }
}

Elliot Lee's avatar
Elliot Lee committed
711 712 713
static void
gtk_menu_init (GtkMenu *menu)
{
714 715
  GtkMenuPrivate *priv = gtk_menu_get_private (menu);

Elliot Lee's avatar
Elliot Lee committed
716 717
  menu->parent_menu_item = NULL;
  menu->old_active_menu_item = NULL;
Tim Janik's avatar
Tim Janik committed
718
  menu->accel_group = NULL;
Elliot Lee's avatar
Elliot Lee committed
719 720
  menu->position_func = NULL;
  menu->position_func_data = NULL;
721
  menu->toggle_size = 0;
Tim Janik's avatar
Tim Janik committed
722

Manish Singh's avatar
Manish Singh committed
723 724 725 726
  menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
						   "type", GTK_WINDOW_POPUP,
						   "child", menu,
						   NULL),
Tim Janik's avatar
Tim Janik committed
727
				     "signal::event", gtk_menu_window_event, menu,
728
				     "signal::size_request", gtk_menu_window_size_request, menu,
Tim Janik's avatar
Tim Janik committed
729 730
				     "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
				     NULL);
Manish Singh's avatar
Manish Singh committed
731
  gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
732
  gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
733

Owen Taylor's avatar
Owen Taylor committed
734 735 736 737
  /* Refloat the menu, so that reference counting for the menu isn't
   * affected by it being a child of the toplevel
   */
  GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
738
  menu->needs_destruction_ref_count = TRUE;
Owen Taylor's avatar
Owen Taylor committed
739

740 741 742 743 744 745 746 747
  menu->view_window = NULL;
  menu->bin_window = NULL;

  menu->scroll_offset = 0;
  menu->scroll_step  = 0;
  menu->timeout_id = 0;
  menu->scroll_fast = FALSE;
  
748
  menu->tearoff_window = NULL;
749
  menu->tearoff_hbox = NULL;
750
  menu->torn_off = FALSE;
751 752 753
  menu->tearoff_active = FALSE;
  menu->tearoff_adjustment = NULL;
  menu->tearoff_scrollbar = NULL;
754

755 756 757 758 759
  menu->upper_arrow_visible = FALSE;
  menu->lower_arrow_visible = FALSE;
  menu->upper_arrow_prelight = FALSE;
  menu->lower_arrow_prelight = FALSE;
  
Tim Janik's avatar
Tim Janik committed
760
  MENU_NEEDS_RESIZE (menu) = TRUE;
761 762

  priv->columns = 1;
763 764 765
}

static void
766
gtk_menu_destroy (GtkObject *object)
767
{
768
  GtkMenu *menu;
Tim Janik's avatar
Tim Janik committed
769
  GtkMenuAttachData *data;
770

771
  g_return_if_fail (GTK_IS_MENU (object));
772 773

  menu = GTK_MENU (object);
774 775

  gtk_menu_stop_scrolling (menu);
Tim Janik's avatar
Tim Janik committed
776
  
Manish Singh's avatar
Manish Singh committed
777
  data = g_object_get_data (G_OBJECT (object), attach_data_key);
Tim Janik's avatar
Tim Janik committed
778
  if (data)
779
    gtk_menu_detach (menu);
Tim Janik's avatar
Tim Janik committed
780
  
781 782
  gtk_menu_stop_navigating_submenu (menu);

783 784
  if (menu->old_active_menu_item)
    {
Manish Singh's avatar
Manish Singh committed
785
      g_object_unref (menu->old_active_menu_item);
786 787 788
      menu->old_active_menu_item = NULL;
    }

Owen Taylor's avatar
Owen Taylor committed
789
  /* Add back the reference count for being a child */
790 791 792
  if (menu->needs_destruction_ref_count)
    {
      menu->needs_destruction_ref_count = FALSE;
Manish Singh's avatar
Manish Singh committed
793
      g_object_ref (object);
794
    }
Tim Janik's avatar
Tim Janik committed
795
  
796 797 798 799 800 801
  if (menu->accel_group)
    {
      g_object_unref (menu->accel_group);
      menu->accel_group = NULL;
    }

802 803
  if (menu->toplevel)
    gtk_widget_destroy (menu->toplevel);
804 805 806
  if (menu->tearoff_window)
    gtk_widget_destroy (menu->tearoff_window);

807
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
Tim Janik's avatar
Tim Janik committed
808 809
}

810 811 812 813 814 815 816 817 818
static void
gtk_menu_finalize (GObject *object)
{
  GtkMenu *menu = GTK_MENU (object);

  g_free (menu->accel_path);
  
  G_OBJECT_CLASS (parent_class)->finalize (object);
}
Tim Janik's avatar
Tim Janik committed
819

820 821 822 823
static void
menu_change_screen (GtkMenu   *menu,
		    GdkScreen *new_screen)
{
824 825
  GtkMenuPrivate *private = gtk_menu_get_private (menu);

826 827 828 829 830 831 832
  if (menu->torn_off)
    {
      gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
      gtk_menu_position (menu);
    }

  gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
833
  private->monitor_num = -1;
834 835
}

836 837 838 839 840 841 842 843
static void
attach_widget_screen_changed (GtkWidget *attach_widget,
			      GdkScreen *previous_screen,
			      GtkMenu   *menu)
{
  if (gtk_widget_has_screen (attach_widget) &&
      !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
    {
844
      menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
845 846 847
    }
}

Tim Janik's avatar
Tim Janik committed
848
void
Tim Janik's avatar
Tim Janik committed
849 850 851
gtk_menu_attach_to_widget (GtkMenu	       *menu,
			   GtkWidget	       *attach_widget,
			   GtkMenuDetachFunc	detacher)
Tim Janik's avatar
Tim Janik committed
852 853
{
  GtkMenuAttachData *data;
Tim Janik's avatar
Tim Janik committed
854
  
Tim Janik's avatar
Tim Janik committed
855 856 857
  g_return_if_fail (GTK_IS_MENU (menu));
  g_return_if_fail (GTK_IS_WIDGET (attach_widget));
  g_return_if_fail (detacher != NULL);
Tim Janik's avatar
Tim Janik committed
858
  
Tim Janik's avatar
Tim Janik committed
859 860
  /* keep this function in sync with gtk_widget_set_parent()
   */
Tim Janik's avatar
Tim Janik committed
861
  
Manish Singh's avatar
Manish Singh committed
862
  data = g_object_get_data (G_OBJECT (menu), attach_data_key);
Tim Janik's avatar
Tim Janik committed
863 864 865
  if (data)
    {
      g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
Manish Singh's avatar
Manish Singh committed
866
		 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
867
     return;
Tim Janik's avatar
Tim Janik committed
868
    }
Tim Janik's avatar
Tim Janik committed
869
  
Manish Singh's avatar
Manish Singh committed
870
  g_object_ref (menu);
Tim Janik's avatar
Tim Janik committed
871
  gtk_object_sink (GTK_OBJECT (menu));
Tim Janik's avatar
Tim Janik committed
872
  
Tim Janik's avatar
Tim Janik committed
873 874
  data = g_new (GtkMenuAttachData, 1);
  data->attach_widget = attach_widget;
875 876 877 878 879
  
  g_signal_connect (attach_widget, "screen_changed",
		    G_CALLBACK (attach_widget_screen_changed), menu);
  attach_widget_screen_changed (attach_widget, NULL, menu);
  
Tim Janik's avatar
Tim Janik committed
880
  data->detacher = detacher;
Manish Singh's avatar
Manish Singh committed
881
  g_object_set_data (G_OBJECT (menu), attach_data_key, data);
Tim Janik's avatar
Tim Janik committed
882
  
Tim Janik's avatar
Tim Janik committed
883 884
  if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
    gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
Tim Janik's avatar
Tim Janik committed
885
  
Tim Janik's avatar
Tim Janik committed
886 887 888
  /* we don't need to set the style here, since
   * we are a toplevel widget.
   */
889 890 891

  /* Fallback title for menu comes from attach widget */
  gtk_menu_update_title (menu);
Tim Janik's avatar
Tim Janik committed
892 893
}

894
GtkWidget*
895
gtk_menu_get_attach_widget (GtkMenu *menu)
896 897
{
  GtkMenuAttachData *data;
Tim Janik's avatar
Tim Janik committed
898
  
899
  g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
Tim Janik's avatar
Tim Janik committed
900
  
Manish Singh's avatar
Manish Singh committed
901
  data = g_object_get_data (G_OBJECT (menu), attach_data_key);
902 903 904 905 906
  if (data)
    return data->attach_widget;
  return NULL;
}

Tim Janik's avatar
Tim Janik committed
907
void
908
gtk_menu_detach (GtkMenu *menu)
Tim Janik's avatar
Tim Janik committed
909 910
{
  GtkMenuAttachData *data;
Tim Janik's avatar
Tim Janik committed
911
  
Tim Janik's avatar
Tim Janik committed
912
  g_return_if_fail (GTK_IS_MENU (menu));
Tim Janik's avatar
Tim Janik committed
913
  
Tim Janik's avatar
Tim Janik committed
914 915
  /* keep this function in sync with gtk_widget_unparent()
   */
Manish Singh's avatar
Manish Singh committed
916
  data = g_object_get_data (G_OBJECT (menu), attach_data_key);
Tim Janik's avatar
Tim Janik committed
917 918 919 920 921
  if (!data)
    {
      g_warning ("gtk_menu_detach(): menu is not attached");
      return;
    }
Manish Singh's avatar
Manish Singh committed
922
  g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
Tim Janik's avatar
Tim Janik committed
923
  
924 925 926 927
  g_signal_handlers_disconnect_by_func (data->attach_widget,
					(gpointer) attach_widget_screen_changed,
					menu);

Tim Janik's avatar
Tim Janik committed
928
  data->detacher (data->attach_widget, menu);
Tim Janik's avatar
Tim Janik committed
929
  
Tim Janik's avatar
Tim Janik committed
930 931
  if (GTK_WIDGET_REALIZED (menu))
    gtk_widget_unrealize (GTK_WIDGET (menu));
Tim Janik's avatar
Tim Janik committed
932
  
Tim Janik's avatar
Tim Janik committed
933
  g_free (data);
Tim Janik's avatar
Tim Janik committed
934
  
935 936 937
  /* Fallback title for menu comes from attach widget */
  gtk_menu_update_title (menu);

Manish Singh's avatar
Manish Singh committed
938
  g_object_unref (menu);
Elliot Lee's avatar
Elliot Lee committed
939 940
}

941 942 943 944 945 946 947 948
static void
gtk_menu_do_remove (GtkMenuShell *menu_shell,
		    GtkWidget    *child)
{
  AttachInfo *ai;
  GList *children;
  GtkMenuPrivate *priv;
  gint delta;
949
  gboolean single_column;
950 951 952 953

  ai = get_attach_info (G_OBJECT (child));
  priv = gtk_menu_get_private (GTK_MENU (menu_shell));
  delta = ai->bottom_attach - ai->top_attach;
954
  single_column = priv->columns == 1;
955

956
  /* Recalculate these, assuming the child has already been removed. 
957
   * Note that an empty menu is assumed to have one column
958
   */
959
  priv->rows = 0;
960
  priv->columns = 1;
961 962 963 964 965 966

  for (children = menu_shell->children; children; children = children->next)
    {
      AttachInfo *child_ai = get_attach_info (children->data);
      
      /* Do not move items in table menus */
967
      if (single_column && child_ai->bottom_attach > ai->bottom_attach)
968 969 970 971 972 973 974 975 976 977 978 979
        gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
                                 "top_attach", child_ai->top_attach - delta,
                                 "bottom_attach", child_ai->bottom_attach - delta,
                                 NULL);
      else
	{
	  priv->columns = MAX (priv->columns, child_ai->right_attach);
	  priv->rows = MAX (priv->rows, child_ai->bottom_attach);
	}
    }
}

980
static void 
981 982
gtk_menu_remove (GtkContainer *container,
		 GtkWidget    *widget)
983 984
{
  GtkMenu *menu;
985

986 987 988 989 990 991 992 993 994
  g_return_if_fail (GTK_IS_MENU (container));
  g_return_if_fail (GTK_IS_MENU_ITEM (widget));

  menu = GTK_MENU (container);

  /* Clear out old_active_menu_item if it matches the item we are removing
   */
  if (menu->old_active_menu_item == widget)
    {
Manish Singh's avatar
Manish Singh committed
995
      g_object_unref (menu->old_active_menu_item);
996 997 998 999
      menu->old_active_menu_item = NULL;
    }

  GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1000
  gtk_menu_do_remove (GTK_MENU_SHELL (container), widget);
1001
  g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
1002 1003 1004
}


Elliot Lee's avatar
Elliot Lee committed
1005
GtkWidget*
1006
gtk_menu_new (void)
Elliot Lee's avatar
Elliot Lee committed
1007
{
Manish Singh's avatar
Manish Singh committed
1008
  return g_object_new (GTK_TYPE_MENU, NULL);
Elliot Lee's avatar
Elliot Lee committed
1009 1010
}

1011
static void
1012 1013 1014
gtk_menu_do_insert (GtkMenuShell *menu_shell,
                    GtkWidget    *child,
                    gint          position)
Elliot Lee's avatar
Elliot Lee committed
1015
{
1016 1017 1018 1019 1020
  GList *children;
  GtkMenuPrivate *priv;

  priv = gtk_menu_get_private (GTK_MENU (menu_shell));

1021
  if (position < 0 || position >= priv->rows)
1022 1023 1024
    {
      /* attach after the last row */
      gtk_menu_attach (GTK_MENU (menu_shell), child,
1025
                       0, priv->columns,
1026
                       priv->rows, priv->rows + 1);
1027 1028 1029 1030

      return;
    }

1031 1032 1033 1034
  /* We need to make space for this new item; move all items with
   * top >= position one down. 
   * Note that this does not prevent overlaps when the menu contains 
   * vertically spanning items.
1035 1036 1037
   */
  for (children = menu_shell->children; children; children = children->next)
    {
1038
      AttachInfo *child_ai = get_attach_info (children->data);
1039

1040
      if (child_ai->top_attach >= position)
1041
        gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1042 1043
                                 "top_attach", child_ai->top_attach + 1,
                                 "bottom_attach", child_ai->bottom_attach + 1,
1044 1045 1046 1047 1048
                                 NULL);
    }

  /* attach the new item */
  gtk_menu_attach (GTK_MENU (menu_shell), child,
1049
                   0, priv->columns,
1050
                   position, position + 1);
Elliot Lee's avatar
Elliot Lee committed
1051 1052
}

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
static void
gtk_menu_real_insert (GtkMenuShell     *menu_shell,
		      GtkWidget        *child,
		      gint              position)
{
  if (GTK_WIDGET_REALIZED (menu_shell))
    gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);

  GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);

  gtk_menu_do_insert (menu_shell, child, position);
}

1066 1067 1068 1069
static void
gtk_menu_tearoff_bg_copy (GtkMenu *menu)
{
  GtkWidget *widget;
1070
  gint width, height;
1071 1072 1073 1074 1075 1076 1077 1078

  widget = GTK_WIDGET (menu);

  if (menu->torn_off)
    {
      GdkPixmap *pixmap;
      GdkGC *gc;
      GdkGCValues gc_values;
1079 1080 1081

      menu->tearoff_active = FALSE;
      menu->saved_scroll_offset = menu->scroll_offset;
1082 1083 1084 1085 1086
      
      gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
      gc = gdk_gc_new_with_values (widget->window,
				   &gc_values, GDK_GC_SUBWINDOW);
      
Manish Singh's avatar
Manish Singh committed
1087
      gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1088 1089 1090 1091
      
      pixmap = gdk_pixmap_new (menu->tearoff_window->window,
			       width,
			       height,
1092 1093
			       -1);

Manish Singh's avatar
Manish Singh committed
1094 1095 1096 1097
      gdk_draw_drawable (pixmap, gc,
			 menu->tearoff_window->window,
			 0, 0, 0, 0, -1, -1);
      g_object_unref (gc);
1098

Manish Singh's avatar
Manish Singh committed
1099 1100 1101
      gtk_widget_set_size_request (menu->tearoff_window,
				   width,
				   height);
1102

1103
      gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
Manish Singh's avatar
Manish Singh committed
1104
      g_object_unref (pixmap);
1105 1106 1107
    }
}

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
static gboolean
popup_grab_on_window (GdkWindow *window,
		      guint32    activate_time)
{
  if ((gdk_pointer_grab (window, TRUE,
			 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
			 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
			 GDK_POINTER_MOTION_MASK,
			 NULL, NULL, activate_time) == 0))
    {
      if (gdk_keyboard_grab (window, TRUE,
			     activate_time) == 0)
	return TRUE;
      else
	{
1123 1124
	  gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
				      activate_time);
1125 1126 1127 1128 1129 1130 1131
	  return FALSE;
	}
    }

  return FALSE;
}

Soeren Sandmann's avatar
Soeren Sandmann committed
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
/**
 * gtk_menu_popup:
 * @menu: a #GtkMenu.
 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
 * @func: a user supplied function used to position the menu, or %NULL
 * @data: user supplied data to be passed to @func.
 * @button: the mouse button which was pressed to initiate the event.
 * @activate_time: the time at which the activation event occurred.
 *
 * Displays a menu and makes it available for selection.  Applications can use
 * this function to display context-sensitive menus, and will typically supply
 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
 * parameters. The default menu positioning function will position the menu
 * at the current mouse cursor position.
 *
 * The @button parameter should be the mouse button pressed to initiate
 * the menu popup. If the menu popup was initiated by something other than
 * a mouse button press, such as a mouse button release or a keypress,
 * @button should be 0.
 *
 * The @activate_time parameter should be the time stamp of the event that
 * initiated the popup. If such an event is not available, use
 * gtk_get_current_event_time() instead.
 *
 */
Elliot Lee's avatar
Elliot Lee committed
1158
void
Tim Janik's avatar
Tim Janik committed
1159 1160 1161
gtk_menu_popup (GtkMenu		    *menu,
		GtkWidget	    *parent_menu_shell,
		GtkWidget	    *parent_menu_item,
Elliot Lee's avatar
Elliot Lee committed
1162
		GtkMenuPositionFunc  func,
Tim Janik's avatar
Tim Janik committed
1163 1164 1165
		gpointer	     data,
		guint		     button,
		guint32		     activate_time)
Elliot Lee's avatar
Elliot Lee committed
1166
{
1167
  GtkWidget *widget;
Owen Taylor's avatar
Owen Taylor committed
1168 1169
  GtkWidget *xgrab_shell;
  GtkWidget *parent;
1170 1171
  GdkEvent *current_event;
  GtkMenuShell *menu_shell;
1172

Elliot Lee's avatar
Elliot Lee committed
1173
  g_return_if_fail (GTK_IS_MENU (menu));
Tim Janik's avatar
Tim Janik committed
1174
  
1175 1176 1177 1178
  widget = GTK_WIDGET (menu);
  menu_shell = GTK_MENU_SHELL (menu);
  
  menu_shell->parent_menu_shell = parent_menu_shell;
1179
  
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  /* Find the last viewable ancestor, and make an X grab on it
   */
  parent = GTK_WIDGET (menu);
  xgrab_shell = NULL;
  while (parent)
    {
      gboolean viewable = TRUE;
      GtkWidget *tmp = parent;
      
      while (tmp)
	{
	  if (!GTK_WIDGET_MAPPED (tmp))
	    {
	      viewable = FALSE;
	      break;
	    }
	  tmp = tmp->parent;
	}
      
      if (viewable)
	xgrab_shell = parent;
      
      parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
    }

  /* We want to receive events generated when we map the menu; unfortunately,
   * since there is probably already an implicit grab in place from the
   * button that the user used to pop up the menu, we won't receive then --
   * in particular, the EnterNotify when the menu pops up under the pointer.
   *
   * If we are grabbing on a parent menu shell, no problem; just grab on
   * that menu shell first before popping up the window with owner_events = TRUE.
   *
   * When grabbing on the menu itself, things get more convuluted - we
   * we do an explicit grab on a specially created window with
   * owner_events = TRUE, which we override further down with a grab
   * on the menu. (We can't grab on the menu until it is mapped; we
   * probably could just leave the grab on the other window, with a
   * little reorganization of the code in gtkmenu*).
   */
1220
  if (xgrab_shell && xgrab_shell != widget)
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    {
      if (popup_grab_on_window (xgrab_shell->window, activate_time))
	GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
    }
  else
    {
      GdkWindow *transfer_window;

      xgrab_shell = widget;
      transfer_window = menu_grab_transfer_window_get (menu);
      if (popup_grab_on_window (transfer_window, activate_time))
	GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
    }

  if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
    {
      /* We failed to make our pointer/keyboard grab. Rather than leaving the user
       * with a stuck up window, we just abort here. Presumably the user will
       * try again.
       */
      menu_shell->parent_menu_shell = NULL;
1242
      menu_grab_transfer_window_destroy (menu);
1243 1244 1245
      return;
    }

1246 1247 1248 1249 1250 1251 1252 1253
  menu_shell->active = TRUE;
  menu_shell->button = button;

  /* If we are popping up the menu from something other than, a button
   * press then, as a heuristic, we ignore enter events for the menu
   * until we get a MOTION_NOTIFY.  
   */

1254
  current_event = gtk_get_current_event ();
1255 1256 1257 1258 1259
  if (current_event)
    {
      if ((current_event->type != GDK_BUTTON_PRESS) &&
	  (current_event->type != GDK_ENTER_NOTIFY))
	menu_shell->ignore_enter = TRUE;
1260 1261

      gdk_event_free (current_event);
1262
    }
1263 1264
  else
    menu_shell->ignore_enter = TRUE;
1265 1266 1267

  if (menu->torn_off)
    {
1268
      gtk_menu_tearoff_bg_copy (menu);
1269

1270
      gtk_menu_reparent (menu, menu->toplevel, FALSE);
1271
    }
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
 
  if (parent_menu_shell) 
    {
      GtkWidget *toplevel;

      toplevel = gtk_widget_get_toplevel (parent_menu_shell);
      if (GTK_IS_WINDOW (toplevel))
	gtk_window_group_add_window (_gtk_window_get_group (GTK_WINDOW (toplevel)), 
				     GTK_WINDOW (menu->toplevel));
    }
Tim Janik's avatar
Tim Janik committed
1282
  
Elliot Lee's avatar
Elliot Lee committed
1283 1284 1285
  menu->parent_menu_item = parent_menu_item;
  menu->position_func = func;
  menu->position_func_data = data;
1286 1287
  menu_shell->activate_time = activate_time;

1288 1289 1290
  /* We need to show the menu here rather in the init function because
   * code expects to be able to tell if the menu is onscreen by
   * looking at the GTK_WIDGET_VISIBLE (menu)
1291
   */
Elliot Lee's avatar
Elliot Lee committed
1292
  gtk_widget_show (GTK_WIDGET (menu));
1293

1294 1295 1296 1297
  /* Position the menu, possibly changing the size request
   */
  gtk_menu_position (menu);

1298
  /* Compute the size of the toplevel and realize it so we
1299
   * can scroll correctly.
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
   */
  {
    GtkRequisition tmp_request;
    GtkAllocation tmp_allocation = { 0, };

    gtk_widget_size_request (menu->toplevel, &tmp_request);
    
    tmp_allocation.width = tmp_request.width;
    tmp_allocation.height = tmp_request.height;

    gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
    
    gtk_widget_realize (GTK_WIDGET (menu));
  }

Owen Taylor's avatar
Owen Taylor committed
1315 1316
  gtk_menu_scroll_to (menu, menu->scroll_offset);

1317 1318 1319 1320 1321
  /* Once everything is set up correctly, map the toplevel window on
     the screen.
   */
  gtk_widget_show (menu->toplevel);

1322 1323
  if (xgrab_shell == widget)
    popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
Elliot Lee's avatar
Elliot Lee committed
1324 1325 1326 1327 1328 1329
  gtk_grab_add (GTK_WIDGET (menu));
}

void
gtk_menu_popdown (GtkMenu *menu)
{
1330
  GtkMenuPrivate *private;
Elliot Lee's avatar
Elliot Lee committed
1331
  GtkMenuShell *menu_shell;
1332

Elliot Lee's avatar
Elliot Lee committed
1333
  g_return_if_fail (GTK_IS_MENU (menu));
Tim Janik's avatar
Tim Janik committed
1334
  
Elliot Lee's avatar
Elliot Lee committed
1335
  menu_shell = GTK_MENU_SHELL (menu);
1336
  private = gtk_menu_get_private (menu);
Tim Janik's avatar
Tim Janik committed
1337
  
Elliot Lee's avatar
Elliot Lee committed
1338 1339
  menu_shell->parent_menu_shell = NULL;
  menu_shell->active = FALSE;
1340
  menu_shell->ignore_enter = FALSE;
1341

1342 1343
  private->have_position = FALSE;

1344
  gtk_menu_stop_scrolling (menu);
Tim Janik's avatar
Tim Janik committed
1345
  
1346 1347
  gtk_menu_stop_navigating_submenu (menu);
  
Elliot Lee's avatar