gtkmenushell.c 61.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * 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 11
 * 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
 * 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/. 
 */

27
#include "config.h"
28

29
#include "gtkbindings.h"
30
#include "gtkkeyhash.h"
31
#include "gtklabel.h"
32
#include "gtkmainprivate.h"
33
#include "gtkmarshalers.h"
34
#include "gtkmenubar.h"
35
#include "gtkmenuitemprivate.h"
36
#include "gtkmenushellprivate.h"
37
#include "gtkmenuprivate.h"
38
#include "gtkmnemonichash.h"
39
#include "gtktearoffmenuitem.h"
40
#include "gtkwindow.h"
41 42
#include "gtkprivate.h"
#include "gtkintl.h"
43
#include "gtktypebuiltins.h"
Elliot Lee's avatar
Elliot Lee committed
44 45 46

#define MENU_SHELL_TIMEOUT   500

47 48 49 50 51
#define PACK_DIRECTION(m)                                 \
   (GTK_IS_MENU_BAR (m)                                   \
     ? gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (m)) \
     : GTK_PACK_DIRECTION_LTR)

Elliot Lee's avatar
Elliot Lee committed
52 53
enum {
  DEACTIVATE,
Tim Janik's avatar
Tim Janik committed
54
  SELECTION_DONE,
55 56 57
  MOVE_CURRENT,
  ACTIVATE_CURRENT,
  CANCEL,
58
  CYCLE_FOCUS,
59
  MOVE_SELECTED,
Elliot Lee's avatar
Elliot Lee committed
60 61 62
  LAST_SIGNAL
};

63 64 65 66 67
enum {
  PROP_0,
  PROP_TAKE_FOCUS
};

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/* Terminology:
 * 
 * A menu item can be "selected", this means that it is displayed
 * in the prelight state, and if it has a submenu, that submenu
 * will be popped up. 
 * 
 * A menu is "active" when it is visible onscreen and the user
 * is selecting from it. A menubar is not active until the user
 * clicks on one of its menuitems. When a menu is active,
 * passing the mouse over a submenu will pop it up.
 *
 * menu_shell->active_menu_item, is however, not an "active"
 * menu item (there is no such thing) but rather, the selected
 * menu item in that MenuShell, if there is one.
 *
 * There is also is a concept of the current menu and a current
 * menu item. The current menu item is the selected menu item
Matthias Clasen's avatar
Matthias Clasen committed
85
 * that is furthest down in the hierarchy. (Every active menu_shell
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
 * does not necessarily contain a selected menu item, but if
 * it does, then menu_shell->parent_menu_shell must also contain
 * a selected menu item. The current menu is the menu that 
 * contains the current menu_item. It will always have a GTK
 * grab and receive all key presses.
 *
 *
 * Action signals:
 *
 *  ::move_current (GtkMenuDirection *dir)
 *     Moves the current menu item in direction 'dir':
 *
 *       GTK_MENU_DIR_PARENT: To the parent menu shell
 *       GTK_MENU_DIR_CHILD: To the child menu shell (if this item has
 *          a submenu.
 *       GTK_MENU_DIR_NEXT/PREV: To the next or previous item
 *          in this menu.
 * 
 *     As a a bit of a hack to get movement between menus and
 *     menubars working, if submenu_placement is different for
 *     the menu and its MenuShell then the following apply:
 * 
 *       - For 'parent' the current menu is not just moved to
 *         the parent, but moved to the previous entry in the parent
 *       - For 'child', if there is no child, then current is
 *         moved to the next item in the parent.
 *
113 114 115 116
 *    Note that the above explanation of ::move_current was written
 *    before menus and menubars had support for RTL flipping and
 *    different packing directions, and therefore only applies for
 *    when text direction and packing direction are both left-to-right.
117 118 119 120 121 122 123 124 125
 * 
 *  ::activate_current (GBoolean *force_hide)
 *     Activate the current item. If 'force_hide' is true, hide
 *     the current menu item always. Otherwise, only hide
 *     it if menu_item->klass->hide_on_activate is true.
 *
 *  ::cancel ()
 *     Cancels the current selection
 */
Elliot Lee's avatar
Elliot Lee committed
126

127

128 129 130 131 132 133 134 135
static void gtk_menu_shell_set_property      (GObject           *object,
                                              guint              prop_id,
                                              const GValue      *value,
                                              GParamSpec        *pspec);
static void gtk_menu_shell_get_property      (GObject           *object,
                                              guint              prop_id,
                                              GValue            *value,
                                              GParamSpec        *pspec);
Elliot Lee's avatar
Elliot Lee committed
136
static void gtk_menu_shell_realize           (GtkWidget         *widget);
137
static void gtk_menu_shell_finalize          (GObject           *object);
138
static void gtk_menu_shell_dispose           (GObject           *object);
Elliot Lee's avatar
Elliot Lee committed
139
static gint gtk_menu_shell_button_press      (GtkWidget         *widget,
140
                                              GdkEventButton    *event);
Elliot Lee's avatar
Elliot Lee committed
141
static gint gtk_menu_shell_button_release    (GtkWidget         *widget,
142 143 144
                                              GdkEventButton    *event);
static gint gtk_menu_shell_key_press         (GtkWidget         *widget,
                                              GdkEventKey       *event);
Elliot Lee's avatar
Elliot Lee committed
145
static gint gtk_menu_shell_enter_notify      (GtkWidget         *widget,
146
                                              GdkEventCrossing  *event);
Elliot Lee's avatar
Elliot Lee committed
147
static gint gtk_menu_shell_leave_notify      (GtkWidget         *widget,
148
                                              GdkEventCrossing  *event);
149
static void gtk_menu_shell_screen_changed    (GtkWidget         *widget,
150
                                              GdkScreen         *previous_screen);
151
static gboolean gtk_menu_shell_grab_broken       (GtkWidget         *widget,
152
                                              GdkEventGrabBroken *event);
Elliot Lee's avatar
Elliot Lee committed
153
static void gtk_menu_shell_add               (GtkContainer      *container,
154
                                              GtkWidget         *widget);
Elliot Lee's avatar
Elliot Lee committed
155
static void gtk_menu_shell_remove            (GtkContainer      *container,
156
                                              GtkWidget         *widget);
157
static void gtk_menu_shell_forall            (GtkContainer      *container,
158 159 160
                                              gboolean           include_internals,
                                              GtkCallback        callback,
                                              gpointer           callback_data);
161
static void gtk_menu_shell_real_insert       (GtkMenuShell *menu_shell,
162 163
                                              GtkWidget    *child,
                                              gint          position);
Elliot Lee's avatar
Elliot Lee committed
164 165
static void gtk_real_menu_shell_deactivate   (GtkMenuShell      *menu_shell);
static gint gtk_menu_shell_is_item           (GtkMenuShell      *menu_shell,
166
                                              GtkWidget         *child);
Owen Taylor's avatar
Owen Taylor committed
167
static GtkWidget *gtk_menu_shell_get_item    (GtkMenuShell      *menu_shell,
168
                                              GdkEvent          *event);
Manish Singh's avatar
Manish Singh committed
169
static GType    gtk_menu_shell_child_type  (GtkContainer      *container);
170
static void gtk_menu_shell_real_select_item  (GtkMenuShell      *menu_shell,
171
                                              GtkWidget         *menu_item);
172
static gboolean gtk_menu_shell_select_submenu_first (GtkMenuShell   *menu_shell); 
Elliot Lee's avatar
Elliot Lee committed
173

174
static void gtk_real_menu_shell_move_current (GtkMenuShell      *menu_shell,
175
                                              GtkMenuDirectionType direction);
176
static void gtk_real_menu_shell_activate_current (GtkMenuShell      *menu_shell,
177
                                                  gboolean           force_hide);
178
static void gtk_real_menu_shell_cancel           (GtkMenuShell      *menu_shell);
179
static void gtk_real_menu_shell_cycle_focus      (GtkMenuShell      *menu_shell,
180
                                                  GtkDirectionType   dir);
Elliot Lee's avatar
Elliot Lee committed
181

182 183
static void     gtk_menu_shell_reset_key_hash    (GtkMenuShell *menu_shell);
static gboolean gtk_menu_shell_activate_mnemonic (GtkMenuShell *menu_shell,
184
                                                  GdkEventKey  *event);
185
static gboolean gtk_menu_shell_real_move_selected (GtkMenuShell  *menu_shell, 
186
                                                   gint           distance);
187

188
static guint menu_shell_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
189

190
G_DEFINE_ABSTRACT_TYPE (GtkMenuShell, gtk_menu_shell, GTK_TYPE_CONTAINER)
Elliot Lee's avatar
Elliot Lee committed
191 192 193 194

static void
gtk_menu_shell_class_init (GtkMenuShellClass *klass)
{
Manish Singh's avatar
Manish Singh committed
195
  GObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
196 197 198
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

199 200
  GtkBindingSet *binding_set;

Manish Singh's avatar
Manish Singh committed
201
  object_class = (GObjectClass*) klass;
Elliot Lee's avatar
Elliot Lee committed
202 203 204
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;

205 206
  object_class->set_property = gtk_menu_shell_set_property;
  object_class->get_property = gtk_menu_shell_get_property;
207
  object_class->finalize = gtk_menu_shell_finalize;
208
  object_class->dispose = gtk_menu_shell_dispose;
209

210 211 212
  widget_class->realize = gtk_menu_shell_realize;
  widget_class->button_press_event = gtk_menu_shell_button_press;
  widget_class->button_release_event = gtk_menu_shell_button_release;
213
  widget_class->grab_broken_event = gtk_menu_shell_grab_broken;
214 215 216
  widget_class->key_press_event = gtk_menu_shell_key_press;
  widget_class->enter_notify_event = gtk_menu_shell_enter_notify;
  widget_class->leave_notify_event = gtk_menu_shell_leave_notify;
217
  widget_class->screen_changed = gtk_menu_shell_screen_changed;
218 219 220 221 222 223 224 225 226 227 228 229 230 231

  container_class->add = gtk_menu_shell_add;
  container_class->remove = gtk_menu_shell_remove;
  container_class->forall = gtk_menu_shell_forall;
  container_class->child_type = gtk_menu_shell_child_type;

  klass->submenu_placement = GTK_TOP_BOTTOM;
  klass->deactivate = gtk_real_menu_shell_deactivate;
  klass->selection_done = NULL;
  klass->move_current = gtk_real_menu_shell_move_current;
  klass->activate_current = gtk_real_menu_shell_activate_current;
  klass->cancel = gtk_real_menu_shell_cancel;
  klass->select_item = gtk_menu_shell_real_select_item;
  klass->insert = gtk_menu_shell_real_insert;
232
  klass->move_selected = gtk_menu_shell_real_move_selected;
233

Elliot Lee's avatar
Elliot Lee committed
234
  menu_shell_signals[DEACTIVATE] =
235
    g_signal_new (I_("deactivate"),
236 237 238 239 240 241
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, deactivate),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
242

Tim Janik's avatar
Tim Janik committed
243
  menu_shell_signals[SELECTION_DONE] =
244
    g_signal_new (I_("selection-done"),
245 246 247 248 249 250
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, selection_done),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
251

252
  menu_shell_signals[MOVE_CURRENT] =
253
    g_signal_new (I_("move-current"),
254 255 256 257 258 259 260
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuShellClass, move_current),
                  NULL, NULL,
                  _gtk_marshal_VOID__ENUM,
                  G_TYPE_NONE, 1,
                  GTK_TYPE_MENU_DIRECTION_TYPE);
261

262
  menu_shell_signals[ACTIVATE_CURRENT] =
263
    g_signal_new (I_("activate-current"),
264 265 266 267 268 269 270
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuShellClass, activate_current),
                  NULL, NULL,
                  _gtk_marshal_VOID__BOOLEAN,
                  G_TYPE_NONE, 1,
                  G_TYPE_BOOLEAN);
271

272
  menu_shell_signals[CANCEL] =
273
    g_signal_new (I_("cancel"),
274 275 276 277 278 279
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuShellClass, cancel),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
280

281
  menu_shell_signals[CYCLE_FOCUS] =
282
    g_signal_new_class_handler (I_("cycle-focus"),
283 284 285 286 287 288 289 290
                                G_OBJECT_CLASS_TYPE (object_class),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_real_menu_shell_cycle_focus),
                                NULL, NULL,
                                _gtk_marshal_VOID__ENUM,
                                G_TYPE_NONE, 1,
                                GTK_TYPE_DIRECTION_TYPE);

Matthias Clasen's avatar
Matthias Clasen committed
291 292 293 294 295 296
  /**
   * GtkMenuShell::move-selected:
   * @menu_shell: the object on which the signal is emitted
   * @distance: +1 to move to the next item, -1 to move to the previous
   *
   * The ::move-selected signal is emitted to move the selection to
297 298
   * another item.
   *
Matthias Clasen's avatar
Matthias Clasen committed
299 300 301
   * Returns: %TRUE to stop the signal emission, %FALSE to continue
   *
   * Since: 2.12
302
   */
303
  menu_shell_signals[MOVE_SELECTED] =
304
    g_signal_new (I_("move-selected"),
305 306 307 308 309 310 311
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkMenuShellClass, move_selected),
                  _gtk_boolean_handled_accumulator, NULL,
                  _gtk_marshal_BOOLEAN__INT,
                  G_TYPE_BOOLEAN, 1,
                  G_TYPE_INT);
312 313 314

  binding_set = gtk_binding_set_by_class (klass);
  gtk_binding_entry_add_signal (binding_set,
315 316
                                GDK_KEY_Escape, 0,
                                "cancel", 0);
317
  gtk_binding_entry_add_signal (binding_set,
318 319 320 321
                                GDK_KEY_Return, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
322
  gtk_binding_entry_add_signal (binding_set,
323 324 325 326
                                GDK_KEY_ISO_Enter, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
327
  gtk_binding_entry_add_signal (binding_set,
328 329 330 331
                                GDK_KEY_KP_Enter, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                TRUE);
332
  gtk_binding_entry_add_signal (binding_set,
333 334 335 336
                                GDK_KEY_space, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                FALSE);
337
  gtk_binding_entry_add_signal (binding_set,
338 339 340 341
                                GDK_KEY_KP_Space, 0,
                                "activate-current", 1,
                                G_TYPE_BOOLEAN,
                                FALSE);
342
  gtk_binding_entry_add_signal (binding_set,
343 344
                                GDK_KEY_F10, 0,
                                "cycle-focus", 1,
345 346
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
  gtk_binding_entry_add_signal (binding_set,
347 348
                                GDK_KEY_F10, GDK_SHIFT_MASK,
                                "cycle-focus", 1,
349
                                GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
350

351 352 353 354 355 356 357 358 359 360 361 362
  /**
   * GtkMenuShell:take-focus:
   *
   * A boolean that determines whether the menu and its submenus grab the
   * keyboard focus. See gtk_menu_shell_set_take_focus() and
   * gtk_menu_shell_get_take_focus().
   *
   * Since: 2.8
   **/
  g_object_class_install_property (object_class,
                                   PROP_TAKE_FOCUS,
                                   g_param_spec_boolean ("take-focus",
363 364 365 366
                                                         P_("Take Focus"),
                                                         P_("A boolean that determines whether the menu grabs the keyboard focus"),
                                                         TRUE,
                                                         GTK_PARAM_READWRITE));
367

368
  g_type_class_add_private (object_class, sizeof (GtkMenuShellPrivate));
Elliot Lee's avatar
Elliot Lee committed
369 370
}

Manish Singh's avatar
Manish Singh committed
371
static GType
372
gtk_menu_shell_child_type (GtkContainer *container)
373 374 375 376
{
  return GTK_TYPE_MENU_ITEM;
}

Elliot Lee's avatar
Elliot Lee committed
377 378 379
static void
gtk_menu_shell_init (GtkMenuShell *menu_shell)
{
380 381 382 383 384 385
  GtkMenuShellPrivate *priv;

  priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_shell,
                                      GTK_TYPE_MENU_SHELL,
                                      GtkMenuShellPrivate);
  menu_shell->priv = priv;
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  priv->take_focus = TRUE;
}

static void
gtk_menu_shell_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);

  switch (prop_id)
    {
    case PROP_TAKE_FOCUS:
      gtk_menu_shell_set_take_focus (menu_shell, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
409 410 411 412
gtk_menu_shell_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
413 414 415 416 417 418 419 420 421 422 423 424
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);

  switch (prop_id)
    {
    case PROP_TAKE_FOCUS:
      g_value_set_boolean (value, gtk_menu_shell_get_take_focus (menu_shell));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
425 426
}

427 428 429 430
static void
gtk_menu_shell_finalize (GObject *object)
{
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (object);
431
  GtkMenuShellPrivate *priv = menu_shell->priv;
432 433 434 435 436 437

  if (priv->mnemonic_hash)
    _gtk_mnemonic_hash_free (priv->mnemonic_hash);
  if (priv->key_hash)
    _gtk_key_hash_free (priv->key_hash);

Matthias Clasen's avatar
Matthias Clasen committed
438
  G_OBJECT_CLASS (gtk_menu_shell_parent_class)->finalize (object);
439 440 441
}


442 443
static void
gtk_menu_shell_dispose (GObject *object)
444
{
445
  gtk_menu_shell_deactivate (GTK_MENU_SHELL (object));
446 447 448 449

  G_OBJECT_CLASS (gtk_menu_shell_parent_class)->dispose (object);
}

Elliot Lee's avatar
Elliot Lee committed
450 451
void
gtk_menu_shell_append (GtkMenuShell *menu_shell,
452
                       GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
453 454 455 456 457 458
{
  gtk_menu_shell_insert (menu_shell, child, -1);
}

void
gtk_menu_shell_prepend (GtkMenuShell *menu_shell,
459
                        GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
460 461 462 463 464 465
{
  gtk_menu_shell_insert (menu_shell, child, 0);
}

void
gtk_menu_shell_insert (GtkMenuShell *menu_shell,
466 467
                       GtkWidget    *child,
                       gint          position)
Elliot Lee's avatar
Elliot Lee committed
468
{
469 470
  GtkMenuShellClass *class;

Elliot Lee's avatar
Elliot Lee committed
471 472 473
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (child));

474 475 476 477 478 479 480 481
  class = GTK_MENU_SHELL_GET_CLASS (menu_shell);

  if (class->insert)
    class->insert (menu_shell, child, position);
}

static void
gtk_menu_shell_real_insert (GtkMenuShell *menu_shell,
482 483
                            GtkWidget    *child,
                            gint          position)
484
{
485 486 487
  GtkMenuShellPrivate *priv = menu_shell->priv;

  priv->children = g_list_insert (priv->children, child, position);
488

Elliot Lee's avatar
Elliot Lee committed
489 490 491 492 493 494 495 496
  gtk_widget_set_parent (child, GTK_WIDGET (menu_shell));
}

void
gtk_menu_shell_deactivate (GtkMenuShell *menu_shell)
{
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));

Manish Singh's avatar
Manish Singh committed
497
  g_signal_emit (menu_shell, menu_shell_signals[DEACTIVATE], 0);
Elliot Lee's avatar
Elliot Lee committed
498 499 500 501 502
}

static void
gtk_menu_shell_realize (GtkWidget *widget)
{
503 504
  GtkAllocation allocation;
  GdkWindow *window;
Elliot Lee's avatar
Elliot Lee committed
505 506 507
  GdkWindowAttr attributes;
  gint attributes_mask;

508
  gtk_widget_set_realized (widget, TRUE);
Elliot Lee's avatar
Elliot Lee committed
509

510 511 512 513 514 515
  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
Elliot Lee's avatar
Elliot Lee committed
516 517 518 519 520
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK |
521 522 523 524 525
                            GDK_BUTTON_PRESS_MASK |
                            GDK_BUTTON_RELEASE_MASK |
                            GDK_KEY_PRESS_MASK |
                            GDK_ENTER_NOTIFY_MASK |
                            GDK_LEAVE_NOTIFY_MASK);
Elliot Lee's avatar
Elliot Lee committed
526

527
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
Elliot Lee's avatar
Elliot Lee committed
528

529 530 531 532 533 534 535
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
  gdk_window_set_user_data (window, widget);

  gtk_widget_style_attach (widget);
  gtk_style_set_background (gtk_widget_get_style (widget), window, GTK_STATE_NORMAL);
Elliot Lee's avatar
Elliot Lee committed
536 537
}

538 539 540
void
_gtk_menu_shell_activate (GtkMenuShell *menu_shell)
{
541 542 543
  GtkMenuShellPrivate *priv = menu_shell->priv;

  if (!priv->active)
544
    {
545 546 547 548 549 550 551
      GdkDevice *device;

      device = gtk_get_current_event_device ();

      _gtk_menu_shell_set_grab_device (menu_shell, device);
      gtk_device_grab_add (GTK_WIDGET (menu_shell), device, TRUE);

552 553
      priv->have_grab = TRUE;
      priv->active = TRUE;
554 555 556
    }
}

Elliot Lee's avatar
Elliot Lee committed
557 558
static gint
gtk_menu_shell_button_press (GtkWidget      *widget,
559
                             GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
560 561
{
  GtkMenuShell *menu_shell;
562
  GtkMenuShellPrivate *priv;
Elliot Lee's avatar
Elliot Lee committed
563
  GtkWidget *menu_item;
564
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
565 566 567 568 569

  if (event->type != GDK_BUTTON_PRESS)
    return FALSE;

  menu_shell = GTK_MENU_SHELL (widget);
570
  priv = menu_shell->priv;
Elliot Lee's avatar
Elliot Lee committed
571

572 573
  if (priv->parent_menu_shell)
    return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
574 575 576

  menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent *)event);

577
  if (menu_item && _gtk_menu_item_is_selectable (menu_item))
Elliot Lee's avatar
Elliot Lee committed
578
    {
579 580
      parent = gtk_widget_get_parent (menu_item);

581
      if (menu_item != GTK_MENU_SHELL (parent)->priv->active_menu_item)
582
        {
583 584 585 586 587 588 589 590 591
          /*  select the menu item *before* activating the shell, so submenus
           *  which might be open are closed the friendly way. If we activate
           *  (and thus grab) this menu shell first, we might get grab_broken
           *  events which will close the entire menu hierarchy. Selecting the
           *  menu item also fixes up the state as if enter_notify() would
           *  have run before (which normally selects the item).
           */
          if (GTK_MENU_SHELL_GET_CLASS (parent)->submenu_placement != GTK_TOP_BOTTOM)
            gtk_menu_shell_select_item (GTK_MENU_SHELL (parent), menu_item);
592
        }
Elliot Lee's avatar
Elliot Lee committed
593
    }
594

595
  if (!priv->active || !priv->button)
Elliot Lee's avatar
Elliot Lee committed
596
    {
597
      gboolean initially_active = priv->active;
598

599
      priv->button = event->button;
600

601
      if (menu_item)
602
        {
603
          if (_gtk_menu_item_is_selectable (menu_item) &&
604
              gtk_widget_get_parent (menu_item) == widget &&
605
              menu_item != priv->active_menu_item)
606
            {
607
              _gtk_menu_shell_activate (menu_shell);
608
              priv->button = event->button;
609 610 611

              if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement == GTK_TOP_BOTTOM)
                {
612
                  priv->activate_time = event->time;
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
                  gtk_menu_shell_select_item (menu_shell, menu_item);
                }
            }
        }
      else
        {
          if (!initially_active)
            {
              gboolean window_drag = FALSE;

              gtk_widget_style_get (widget,
                                    "window-dragging", &window_drag,
                                    NULL);

              if (window_drag)
                {
                  gtk_menu_shell_deactivate (menu_shell);
                  gtk_window_begin_move_drag (GTK_WINDOW (gtk_widget_get_toplevel (widget)),
                                              event->button,
                                              event->x_root,
                                              event->y_root,
                                              event->time);
                }
636 637
            }
        }
Elliot Lee's avatar
Elliot Lee committed
638 639 640 641 642
    }
  else
    {
      widget = gtk_get_event_widget ((GdkEvent*) event);
      if (widget == GTK_WIDGET (menu_shell))
643 644 645 646
        {
          gtk_menu_shell_deactivate (menu_shell);
          g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
        }
Elliot Lee's avatar
Elliot Lee committed
647 648
    }

649 650 651 652
  if (menu_item &&
      _gtk_menu_item_is_selectable (menu_item) &&
      GTK_MENU_ITEM (menu_item)->priv->submenu != NULL &&
      !gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
653 654 655 656 657
    {
      _gtk_menu_item_popup_submenu (menu_item, FALSE);
      priv->activated_submenu = TRUE;
    }

Elliot Lee's avatar
Elliot Lee committed
658 659 660
  return TRUE;
}

661 662
static gboolean
gtk_menu_shell_grab_broken (GtkWidget          *widget,
663
                            GdkEventGrabBroken *event)
664
{
665
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
666
  GtkMenuShellPrivate *priv = menu_shell->priv;
667

668
  if (priv->have_xgrab && event->grab_window == NULL)
669
    {
670
      /* Unset the active menu item so gtk_menu_popdown() doesn't see it. */
671
      gtk_menu_shell_deselect (menu_shell);
672 673 674 675 676 677 678
      gtk_menu_shell_deactivate (menu_shell);
      g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
    }

  return TRUE;
}

Elliot Lee's avatar
Elliot Lee committed
679 680
static gint
gtk_menu_shell_button_release (GtkWidget      *widget,
681
                               GdkEventButton *event)
Elliot Lee's avatar
Elliot Lee committed
682
{
683
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
684
  GtkMenuShellPrivate *priv = menu_shell->priv;
Elliot Lee's avatar
Elliot Lee committed
685

686
  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
687
    {
688 689 690
      GtkWidget *menu_item;
      gboolean   deactivate = TRUE;

691 692 693 694 695 696
      if (priv->button && (event->button != priv->button))
        {
          priv->button = 0;
          if (priv->parent_menu_shell)
            return gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
697

698
      priv->button = 0;
Owen Taylor's avatar
Owen Taylor committed
699
      menu_item = gtk_menu_shell_get_item (menu_shell, (GdkEvent*) event);
700

701
      if ((event->time - priv->activate_time) > MENU_SHELL_TIMEOUT)
702
        {
703
          if (menu_item && (priv->active_menu_item == menu_item) &&
704 705
              _gtk_menu_item_is_selectable (menu_item))
            {
706
              GtkWidget *submenu = GTK_MENU_ITEM (menu_item)->priv->submenu;
707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
              if (submenu == NULL)
                {
                  gtk_menu_shell_activate_item (menu_shell, menu_item, TRUE);
                  deactivate = FALSE;
                }
              else if (GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM ||
                       priv->activated_submenu)
                {
                  gint popdown_delay;
                  GTimeVal *popup_time;
                  gint64 usec_since_popup = 0;

                  g_object_get (gtk_widget_get_settings (widget),
                                "gtk-menu-popdown-delay", &popdown_delay,
                                NULL);

                  popup_time = g_object_get_data (G_OBJECT (submenu),
                                                  "gtk-menu-exact-popup-time");

                  if (popup_time)
                    {
                      GTimeVal current_time;

                      g_get_current_time (&current_time);

                      usec_since_popup = ((gint64) current_time.tv_sec * 1000 * 1000 +
                                          (gint64) current_time.tv_usec -
                                          (gint64) popup_time->tv_sec * 1000 * 1000 -
                                          (gint64) popup_time->tv_usec);

                      g_object_set_data (G_OBJECT (submenu),
                                         "gtk-menu-exact-popup-time", NULL);
                    }

742 743 744 745
                  /* Only close the submenu on click if we opened the
                   * menu explicitely (usec_since_popup == 0) or
                   * enough time has passed since it was opened by
                   * GtkMenuItem's timeout (usec_since_popup > delay).
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
                   */
                  if (!priv->activated_submenu &&
                      (usec_since_popup == 0 ||
                       usec_since_popup > popdown_delay * 1000))
                    {
                      _gtk_menu_item_popdown_submenu (menu_item);
                    }
                  else
                    {
                      gtk_menu_item_select (GTK_MENU_ITEM (menu_item));
                    }

                  deactivate = FALSE;
                }
            }
          else if (menu_item &&
                   !_gtk_menu_item_is_selectable (menu_item) &&
                   GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
            {
              deactivate = FALSE;
            }
767
          else if (priv->parent_menu_shell)
768
            {
769 770
              priv->active = TRUE;
              gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
771 772 773
              deactivate = FALSE;
            }

774 775 776
          /* If we ended up on an item with a submenu, leave the menu up. */
          if (menu_item &&
              (priv->active_menu_item == menu_item) &&
777 778 779 780 781
              GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement != GTK_TOP_BOTTOM)
            {
              deactivate = FALSE;
            }
        }
782
      else /* a very fast press-release */
783 784
        {
          /* We only ever want to prevent deactivation on the first
Owen Taylor's avatar
Owen Taylor committed
785
           * press/release. Setting the time to zero is a bit of a
786 787 788 789 790
           * hack, since we could be being triggered in the first
           * few fractions of a second after a server time wraparound.
           * the chances of that happening are ~1/10^6, without
           * serious harm if we lose.
           */
791
          priv->activate_time = 0;
792 793 794
          deactivate = FALSE;
        }

Elliot Lee's avatar
Elliot Lee committed
795
      if (deactivate)
796 797 798 799 800 801
        {
          gtk_menu_shell_deactivate (menu_shell);
          g_signal_emit (menu_shell, menu_shell_signals[SELECTION_DONE], 0);
        }

      priv->activated_submenu = FALSE;
Elliot Lee's avatar
Elliot Lee committed
802 803 804 805 806
    }

  return TRUE;
}

807 808 809 810
void
_gtk_menu_shell_set_keyboard_mode (GtkMenuShell *menu_shell,
                                   gboolean      keyboard_mode)
{
811
  menu_shell->priv->keyboard_mode = keyboard_mode;
812 813 814 815 816
}

gboolean
_gtk_menu_shell_get_keyboard_mode (GtkMenuShell *menu_shell)
{
817
  return menu_shell->priv->keyboard_mode;
818 819 820 821 822 823 824 825 826 827 828
}

void
_gtk_menu_shell_update_mnemonics (GtkMenuShell *menu_shell)
{
  GtkMenuShell *target;
  gboolean auto_mnemonics;
  gboolean found;
  gboolean mnemonics_visible;

  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
829 830
                "gtk-auto-mnemonics", &auto_mnemonics,
                NULL);
831 832 833 834 835 836 837 838

  if (!auto_mnemonics)
    return;

  target = menu_shell;
  found = FALSE;
  while (target)
    {
839
      GtkMenuShellPrivate *priv = target->priv;
840 841
      GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (target));

842 843 844 845 846 847
      /* The idea with keyboard mode is that once you start using
       * the keyboard to navigate the menus, we show mnemonics
       * until the menu navigation is over. To that end, we spread
       * the keyboard mode upwards in the menu hierarchy here.
       * Also see gtk_menu_popup, where we inherit it downwards.
       */
848 849
      if (menu_shell->priv->keyboard_mode)
        target->priv->keyboard_mode = TRUE;
850 851 852 853 854 855 856 857

      /* While navigating menus, the first parent menu with an active
       * item is the one where mnemonics are effective, as can be seen
       * in gtk_menu_shell_key_press below.
       * We also show mnemonics in context menus. The grab condition is
       * necessary to ensure we remove underlines from menu bars when
       * dismissing menus.
       */
858 859
      mnemonics_visible = target->priv->keyboard_mode &&
                          (((target->priv->active_menu_item || priv->in_unselectable_item) && !found) ||
860
                           (target == menu_shell &&
861
                            !target->priv->parent_menu_shell &&
862
                            gtk_widget_has_grab (GTK_WIDGET (target))));
863 864 865 866 867

      /* While menus are up, only show underlines inside the menubar,
       * not in the entire window.
       */
      if (GTK_IS_MENU_BAR (target))
868
        {
869
          gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), FALSE);
870 871 872
          _gtk_label_mnemonics_visible_apply_recursively (GTK_WIDGET (target),
                                                          mnemonics_visible);
        }
873
      else
874
        gtk_window_set_mnemonics_visible (GTK_WINDOW (toplevel), mnemonics_visible);
875

876
      if (target->priv->active_menu_item || priv->in_unselectable_item)
877 878
        found = TRUE;

879
      target = GTK_MENU_SHELL (target->priv->parent_menu_shell);
880 881 882
    }
}

883
static gint
884
gtk_menu_shell_key_press (GtkWidget   *widget,
885
                          GdkEventKey *event)
886
{
887
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
888
  GtkMenuShellPrivate *priv = menu_shell->priv;
889
  gboolean enable_mnemonics;
890

891
  priv->keyboard_mode = TRUE;
892

893 894 895
  if (!(priv->active_menu_item || priv->in_unselectable_item) &&
      priv->parent_menu_shell)
    return gtk_widget_event (priv->parent_menu_shell, (GdkEvent *)event);
896

897
  if (gtk_bindings_activate_event (G_OBJECT (widget), event))
898 899
    return TRUE;

900
  g_object_get (gtk_widget_get_settings (widget),
901 902
                "gtk-enable-mnemonics", &enable_mnemonics,
                NULL);
903 904 905 906 907

  if (enable_mnemonics)
    return gtk_menu_shell_activate_mnemonic (menu_shell, event);

  return FALSE;
908 909
}

Elliot Lee's avatar
Elliot Lee committed
910 911
static gint
gtk_menu_shell_enter_notify (GtkWidget        *widget,
912
                             GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
913
{
914
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
915
  GtkMenuShellPrivate *priv = menu_shell->priv;
916

917 918 919 920 921
  if (event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_GTK_UNGRAB ||
      event->mode == GDK_CROSSING_STATE_CHANGED)
    return TRUE;

922
  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
923
    {
924
      GtkWidget *menu_item;
925
      GtkWidget *parent;
926

Elliot Lee's avatar
Elliot Lee committed
927 928
      menu_item = gtk_get_event_widget ((GdkEvent*) event);

929 930 931 932 933 934 935 936 937
      if (!menu_item)
        return TRUE;

      if (GTK_IS_MENU_ITEM (menu_item) &&
          !_gtk_menu_item_is_selectable (menu_item))
        {
          priv->in_unselectable_item = TRUE;
          return TRUE;
        }
938

939 940
      parent = gtk_widget_get_parent (menu_item);
      if (parent == widget &&
941 942 943 944
          GTK_IS_MENU_ITEM (menu_item))
        {
          if (priv->ignore_enter)
            return TRUE;
945

946
          if (event->detail != GDK_NOTIFY_INFERIOR)
947
            {
948
              if (gtk_widget_get_state (menu_item) != GTK_STATE_PRELIGHT)
949
                gtk_menu_shell_select_item (menu_shell, menu_item);
950 951 952 953 954 955 956 957 958

              /* If any mouse button is down, and there is a submenu
               * that is not yet visible, activate it. It's sufficient
               * to check for any button's mask (not only the one
               * matching menu_shell->button), because there is no
               * situation a mouse button could be pressed while
               * entering a menu item where we wouldn't want to show
               * its submenu.
               */
959
              if ((event->state & (GDK_BUTTON1_MASK|GDK_BUTTON2_MASK|GDK_BUTTON3_MASK)) &&
960
                  GTK_MENU_ITEM (menu_item)->priv->submenu != NULL)
961
                {
962
                  GTK_MENU_SHELL (parent)->priv->activated_submenu = TRUE;
963

964
                  if (!gtk_widget_get_visible (GTK_MENU_ITEM (menu_item)->priv->submenu))
965 966
                    {
                      gboolean touchscreen_mode;
967

968 969 970 971 972 973 974
                      g_object_get (gtk_widget_get_settings (widget),
                                    "gtk-touchscreen-mode", &touchscreen_mode,
                                    NULL);

                      if (touchscreen_mode)
                        _gtk_menu_item_popup_submenu (menu_item, TRUE);
                    }
975
                }
976 977 978 979 980 981
            }
        }
      else if (priv->parent_menu_shell)
        {
          gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
Elliot Lee's avatar
Elliot Lee committed
982 983 984 985 986 987 988
    }

  return TRUE;
}

static gint
gtk_menu_shell_leave_notify (GtkWidget        *widget,
989
                             GdkEventCrossing *event)
Elliot Lee's avatar
Elliot Lee committed
990
{
991 992 993 994 995
  if (event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_GTK_GRAB ||
      event->mode == GDK_CROSSING_STATE_CHANGED)
    return TRUE;

996
  if (gtk_widget_get_visible (widget))
Elliot Lee's avatar
Elliot Lee committed
997
    {
998
      GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
999
      GtkMenuShellPrivate *priv = menu_shell->priv;
1000 1001
      GtkWidget *event_widget = gtk_get_event_widget ((GdkEvent*) event);
      GtkMenuItem *menu_item;
Elliot Lee's avatar
Elliot Lee committed
1002

1003
      if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1004
        return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1005 1006 1007

      menu_item = GTK_MENU_ITEM (event_widget);

1008
      if (!_gtk_menu_item_is_selectable (event_widget))
1009 1010 1011 1012
        {
          priv->in_unselectable_item = TRUE;
          return TRUE;
        }
Tim Janik's avatar
Tim Janik committed
1013

1014
      if ((priv->active_menu_item == event_widget) &&
1015
          (menu_item->priv->submenu == NULL))
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
        {
          if ((event->detail != GDK_NOTIFY_INFERIOR) &&
              (gtk_widget_get_state (GTK_WIDGET (menu_item)) != GTK_STATE_NORMAL))
            {
              gtk_menu_shell_deselect (menu_shell);
            }
        }
      else if (priv->parent_menu_shell)
        {
          gtk_widget_event (priv->parent_menu_shell, (GdkEvent*) event);
        }
Elliot Lee's avatar
Elliot Lee committed
1027 1028 1029 1030 1031
    }

  return TRUE;
}

1032 1033
static void
gtk_menu_shell_screen_changed (GtkWidget *widget,
1034
                               GdkScreen *previous_screen)
1035 1036 1037 1038
{
  gtk_menu_shell_reset_key_hash (GTK_MENU_SHELL (widget));
}

Elliot Lee's avatar
Elliot Lee committed
1039 1040
static void
gtk_menu_shell_add (GtkContainer *container,
1041
                    GtkWidget    *widget)
Elliot Lee's avatar
Elliot Lee committed
1042 1043 1044 1045 1046 1047
{
  gtk_menu_shell_append (GTK_MENU_SHELL (container), widget);
}

static void
gtk_menu_shell_remove (GtkContainer *container,
1048
                       GtkWidget    *widget)
Elliot Lee's avatar
Elliot Lee committed
1049
{
1050
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
1051
  GtkMenuShellPrivate *priv = menu_shell->priv;
1052
  gint was_visible;
1053

1054
  was_visible = gtk_widget_get_visible (widget);
1055 1056 1057
  priv->children = g_list_remove (priv->children, widget);

  if (widget == priv->active_menu_item)
1058
    {
1059 1060
      g_signal_emit_by_name (priv->active_menu_item, "deselect");
      priv->active_menu_item = NULL;
1061 1062
    }

1063
  gtk_widget_unparent (widget);
1064 1065

  /* Queue resize regardless of gtk_widget_get_visible (container),
1066 1067 1068
   * since that's what is needed by toplevels.
   */
  if (was_visible)
Elliot Lee's avatar
Elliot Lee committed
1069 1070 1071 1072
    gtk_widget_queue_resize (GTK_WIDGET (container));
}

static void
1073
gtk_menu_shell_forall (GtkContainer *container,
1074 1075 1076
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
Elliot Lee's avatar
Elliot Lee committed
1077
{
1078
  GtkMenuShell *menu_shell = GTK_MENU_SHELL (container);
Elliot Lee's avatar
Elliot Lee committed
1079 1080 1081
  GtkWidget *child;
  GList *children;

1082
  children = menu_shell->priv->children;
Elliot Lee's avatar
Elliot Lee committed
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
  while (children)
    {
      child = children->data;
      children = children->next;

      (* callback) (child, callback_data);
    }
}


static void
gtk_real_menu_shell_deactivate (GtkMenuShell *menu_shell)
{
1096 1097 1098
  GtkMenuShellPrivate *priv = menu_shell->priv;

  if (priv->active)
Elliot Lee's avatar
Elliot Lee committed
1099
    {
1100

1101 1102 1103
      priv->button = 0;
      priv->active = FALSE;
      priv->activate_time = 0;
Elliot Lee's avatar
Elliot Lee committed
1104

1105 1106 1107 1108 1109
      if (priv->active_menu_item)
        {
          gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
          priv->active_menu_item = NULL;
        }
Elliot Lee's avatar
Elliot Lee committed
1110

1111 1112 1113
      if (priv->have_grab)
        {
          priv->have_grab = FALSE;
1114
          gtk_device_grab_remove (GTK_WIDGET (menu_shell), priv->grab_pointer);
1115 1116 1117
        }
      if (priv->have_xgrab)
        {
1118 1119
          GdkDevice *keyboard;

1120
          gdk_device_ungrab (priv->grab_pointer, GDK_CURRENT_TIME);
1121 1122 1123 1124
          keyboard = gdk_device_get_associated_device (priv->grab_pointer);

          if (keyboard)
            gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1125

1126 1127
          priv->have_xgrab = FALSE;
        }
1128

1129
      priv->keyboard_mode = FALSE;
1130
      _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1131 1132

      _gtk_menu_shell_update_mnemonics (menu_shell);
Elliot Lee's avatar
Elliot Lee committed
1133 1134 1135 1136 1137
    }
}

static gint
gtk_menu_shell_is_item (GtkMenuShell *menu_shell,
1138
                        GtkWidget    *child)
Elliot Lee's avatar
Elliot Lee committed
1139
{
1140
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
1141 1142 1143 1144

  g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), FALSE);
  g_return_val_if_fail (child != NULL, FALSE);

1145
  parent = gtk_widget_get_parent (child);
1146
  while (GTK_IS_MENU_SHELL (parent))
Elliot Lee's avatar
Elliot Lee committed
1147
    {
1148
      if (parent == (GtkWidget*) menu_shell)
1149 1150
        return TRUE;
      parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
Elliot Lee's avatar
Elliot Lee committed
1151 1152 1153 1154
    }

  return FALSE;
}
Owen Taylor's avatar
Owen Taylor committed
1155

1156
static GtkWidget*
Owen Taylor's avatar
Owen Taylor committed
1157
gtk_menu_shell_get_item (GtkMenuShell *menu_shell,
1158
                         GdkEvent     *event)
Owen Taylor's avatar
Owen Taylor committed
1159 1160 1161 1162
{
  GtkWidget *menu_item;

  menu_item = gtk_get_event_widget ((GdkEvent*) event);
1163

Owen Taylor's avatar
Owen Taylor committed
1164
  while (menu_item && !GTK_IS_MENU_ITEM (menu_item))
1165
    menu_item = gtk_widget_get_parent (menu_item);
Owen Taylor's avatar
Owen Taylor committed
1166 1167 1168 1169 1170 1171 1172

  if (menu_item && gtk_menu_shell_is_item (menu_shell, menu_item))
    return menu_item;
  else
    return NULL;
}

1173 1174 1175
/* Handlers for action signals */

void
1176
gtk_menu_shell_select_item (GtkMenuShell *menu_shell,
1177
                            GtkWidget    *menu_item)
1178
{
1179
  GtkMenuShellPrivate *priv = menu_shell->priv;
1180 1181
  GtkMenuShellClass *class;

1182 1183 1184
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

1185 1186
  class = GTK_MENU_SHELL_GET_CLASS (menu_shell);

1187
  if (class->select_item &&
1188 1189
      !(priv->active &&
        priv->active_menu_item == menu_item))
1190 1191 1192
    class->select_item (menu_shell, menu_item);
}

1193
void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
1194
                                   GtkSubmenuPlacement  placement);
1195 1196 1197

static void
gtk_menu_shell_real_select_item (GtkMenuShell *menu_shell,
1198
                                 GtkWidget    *menu_item)
1199
{
1200
  GtkMenuShellPrivate *priv = menu_shell->priv;
1201 1202
  GtkPackDirection pack_dir = PACK_DIRECTION (menu_shell);

1203
  if (priv->active_menu_item)
1204
    {
1205 1206
      gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
      priv->active_menu_item = NULL;
1207
    }
1208

1209
  if (!_gtk_menu_item_is_selectable (menu_item))
1210 1211 1212 1213 1214
    {
      priv->in_unselectable_item = TRUE;
      _gtk_menu_shell_update_mnemonics (menu_shell);
      return;
    }
1215

1216
  priv->active_menu_item = menu_item;
1217
  if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
1218 1219
    _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
                                  GTK_LEFT_RIGHT);
1220
  else
1221 1222 1223
    _gtk_menu_item_set_placement (GTK_MENU_ITEM (priv->active_menu_item),
                                  GTK_MENU_SHELL_GET_CLASS (menu_shell)->submenu_placement);
  gtk_menu_item_select (GTK_MENU_ITEM (priv->active_menu_item));
1224

1225 1226
  _gtk_menu_shell_update_mnemonics (menu_shell);

1227 1228 1229
  /* This allows the bizarre radio buttons-with-submenus-display-history
   * behavior
   */
1230
  if (GTK_MENU_ITEM (priv->active_menu_item)->priv->submenu)
1231
    gtk_widget_activate (priv->active_menu_item);
1232 1233
}

1234 1235
void
gtk_menu_shell_deselect (GtkMenuShell *menu_shell)
1236
{
1237 1238
  GtkMenuShellPrivate *priv = menu_shell->priv;

1239 1240
  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));

1241
  if (priv->active_menu_item)
1242
    {
1243 1244
      gtk_menu_item_deselect (GTK_MENU_ITEM (priv->active_menu_item));
      priv->active_menu_item = NULL;
1245
      _gtk_menu_shell_update_mnemonics (menu_shell);
1246
    }
1247 1248 1249
}

void
1250 1251 1252
gtk_menu_shell_activate_item (GtkMenuShell *menu_shell,
                              GtkWidget    *menu_item,
                              gboolean      force_deactivate)
1253
{
1254
  GSList *slist, *shells = NULL;
1255 1256 1257 1258 1259 1260
  gboolean deactivate = force_deactivate;

  g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell));
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

  if (!deactivate)
1261
    deactivate = GTK_MENU_ITEM_GET_CLASS (menu_item)->hide_on_activate;
1262

Manish Singh's avatar
Manish Singh committed
1263
  g_object_ref (menu_shell);
1264
  g_object_ref (menu_item);
1265 1266 1267

  if (deactivate)
    {
1268 1269 1270
      GtkMenuShell *parent_menu_shell = menu_shell;

      do
1271 1272 1273 1274 1275
        {
          g_object_ref (parent_menu_shell);
          shells = g_slist_prepend (shells, parent_menu_shell);
          parent_menu_shell = (GtkMenuShell*) parent_menu_shell->priv->parent_menu_shell;
        }
1276 1277 1278
      while (parent_menu_shell);
      shells = g_slist_reverse (shells);

1279
      gtk_menu_shell_deactivate (menu_shell);
1280 1281

      /* Flush the x-queue, so any grabs are removed and
1282 1283
       * the menu is actually taken down
       */
1284
      gdk_display_sync (gtk_widget_get_display (menu_item));
1285
    }
1286

1287 1288
  gtk_widget_activate (menu_item);

1289 1290
  for (slist = shells; slist; slist = slist->next)
    {
Manish Singh's avatar
Manish Singh committed
1291 1292
      g_signal_emit (slist->data, menu_shell_signals[SELECTION_DONE], 0);
      g_object_unref (slist->data);
1293 1294 1295
    }
  g_slist_free (shells);

Manish Singh's avatar
Manish Singh committed
1296
  g_object_unref (menu_shell);
1297
  g_object_unref (menu_item);
1298 1299 1300
}

/* Distance should be +/- 1 */
1301
static gboolean
Matthias Clasen's avatar