gtkmenuitem.c 83.6 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

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

25
#include "config.h"
26

Elliot Lee's avatar
Elliot Lee committed
27
#include <string.h>
28

Tim Janik's avatar
Tim Janik committed
29
#include "gtkaccellabel.h"
30
#include "gtkbuiltiniconprivate.h"
31
#include "gtkcontainerprivate.h"
32
#include "gtkcsscustomgadgetprivate.h"
Elliot Lee's avatar
Elliot Lee committed
33
#include "gtkmain.h"
34
#include "gtkmarshalers.h"
35
#include "gtkmenuprivate.h"
36
#include "gtkmenushellprivate.h"
37
#include "gtkmenuitemprivate.h"
38
#include "gtkmenubar.h"
39
#include "gtkmenuprivate.h"
40
#include "gtkseparatormenuitem.h"
41
#include "gtkprivate.h"
42
#include "gtkbuildable.h"
43
#include "deprecated/gtkactivatable.h"
44
#include "gtkwidgetprivate.h"
45
#include "gtkintl.h"
46
#include "gtksettings.h"
47
#include "gtktypebuiltins.h"
48
#include "a11y/gtkmenuitemaccessible.h"
49
#include "deprecated/gtktearoffmenuitem.h"
50 51
#include "gtkstylecontextprivate.h"
#include "gtkcssstylepropertyprivate.h"
Elliot Lee's avatar
Elliot Lee committed
52

53 54
#define MENU_POPUP_DELAY     225

55 56 57 58
/**
 * SECTION:gtkmenuitem
 * @Short_description: The widget used for item in menus
 * @Title: GtkMenuItem
59
 * @See_also: #GtkBin, #GtkMenuShell
60 61
 *
 * The #GtkMenuItem widget and the derived widgets are the only valid
62
 * children for menus. Their function is to correctly handle highlighting,
63 64
 * alignment, events and submenus.
 *
65 66 67 68 69 70 71
 * As a GtkMenuItem derives from #GtkBin it can hold any valid child widget,
 * although only a few are really useful.
 *
 * By default, a GtkMenuItem sets a #GtkAccelLabel as its child.
 * GtkMenuItem has direct functions to set the label and its mnemonic.
 * For more advanced label settings, you can fetch the child widget from the GtkBin.
 *
72
 * An example for setting markup and accelerator on a MenuItem:
73
 * |[<!-- language="C" -->
74 75
 * GtkWidget *menu_item = gtk_menu_item_new_with_label ("Example Menu Item");
 *
76 77 78
 * GtkWidget *child = gtk_bin_get_child (GTK_BIN (menu_item));
 * gtk_label_set_markup (GTK_LABEL (child), "<i>new label</i> with <b>markup</b>");
 * gtk_accel_label_set_accel (GTK_ACCEL_LABEL (child), GDK_KEY_1, 0);
79
 * ]|
80
 *
81
 * # GtkMenuItem as GtkBuildable
82
 *
83 84 85
 * The GtkMenuItem implementation of the #GtkBuildable interface supports
 * adding a submenu by specifying “submenu” as the “type” attribute of
 * a <child> element.
86 87
 *
 * An example of UI definition fragment with submenus:
88
 * |[
89 90 91 92 93
 * <object class="GtkMenuItem">
 *   <child type="submenu">
 *     <object class="GtkMenu"/>
 *   </child>
 * </object>
94
 * ]|
95 96 97
 *
 * # CSS nodes
 *
98 99
 * |[<!-- language="plain" -->
 * menuitem
100
 * ├── <child>
101 102 103
 * ╰── [arrow.right]
 * ]|
 *
104 105 106
 * GtkMenuItem has a single CSS node with name menuitem. If the menuitem
 * has a submenu, it gets another CSS node with name arrow, which has
 * the .left or .right style class.
107 108 109
 */


Elliot Lee's avatar
Elliot Lee committed
110 111
enum {
  ACTIVATE,
112
  ACTIVATE_ITEM,
113 114
  TOGGLE_SIZE_REQUEST,
  TOGGLE_SIZE_ALLOCATE,
Matthias Clasen's avatar
Matthias Clasen committed
115 116
  SELECT,
  DESELECT,
Elliot Lee's avatar
Elliot Lee committed
117 118 119
  LAST_SIGNAL
};

120 121
enum {
  PROP_0,
Tim Janik's avatar
Tim Janik committed
122 123
  PROP_RIGHT_JUSTIFIED,
  PROP_SUBMENU,
124 125
  PROP_ACCEL_PATH,
  PROP_LABEL,
126 127
  PROP_USE_UNDERLINE,

128 129
  LAST_PROP,

130
  /* activatable properties */
131
  PROP_ACTIVATABLE_RELATED_ACTION = LAST_PROP,
132 133 134 135
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE,

  PROP_ACTION_NAME,
  PROP_ACTION_TARGET
136
};
Elliot Lee's avatar
Elliot Lee committed
137

138

139
static void gtk_menu_item_dispose        (GObject          *object);
140
static void gtk_menu_item_set_property   (GObject          *object,
141 142 143
                                          guint             prop_id,
                                          const GValue     *value,
                                          GParamSpec       *pspec);
144
static void gtk_menu_item_get_property   (GObject          *object,
145 146 147
                                          guint             prop_id,
                                          GValue           *value,
                                          GParamSpec       *pspec);
148
static void gtk_menu_item_destroy        (GtkWidget        *widget);
149 150 151 152
static void gtk_menu_item_realize        (GtkWidget        *widget);
static void gtk_menu_item_unrealize      (GtkWidget        *widget);
static void gtk_menu_item_map            (GtkWidget        *widget);
static void gtk_menu_item_unmap          (GtkWidget        *widget);
Matthias Clasen's avatar
Matthias Clasen committed
153 154 155 156
static gboolean gtk_menu_item_enter      (GtkWidget        *widget,
                                          GdkEventCrossing *event);
static gboolean gtk_menu_item_leave      (GtkWidget        *widget,
                                          GdkEventCrossing *event);
157
static void gtk_menu_item_parent_set     (GtkWidget        *widget,
158
                                          GtkWidget        *previous_parent);
159 160
static void gtk_menu_item_direction_changed (GtkWidget        *widget,
                                             GtkTextDirection  previous_dir);
161

162

Matthias Clasen's avatar
Matthias Clasen committed
163 164
static void gtk_real_menu_item_select               (GtkMenuItem *item);
static void gtk_real_menu_item_deselect             (GtkMenuItem *item);
165
static void gtk_real_menu_item_activate             (GtkMenuItem *item);
166 167
static void gtk_real_menu_item_activate_item        (GtkMenuItem *item);
static void gtk_real_menu_item_toggle_size_request  (GtkMenuItem *menu_item,
168
                                                     gint        *requisition);
169
static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
170
                                                     gint         allocation);
171
static gboolean gtk_menu_item_mnemonic_activate     (GtkWidget   *widget,
172
                                                     gboolean     group_cycling);
173

174
static void gtk_menu_item_ensure_label   (GtkMenuItem      *menu_item);
175
static gint gtk_menu_item_popup_timeout  (gpointer          data);
176
static void gtk_menu_item_show_all       (GtkWidget        *widget);
177

178
static void gtk_menu_item_forall         (GtkContainer    *container,
179 180 181
                                          gboolean         include_internals,
                                          GtkCallback      callback,
                                          gpointer         callback_data);
182

Owen Taylor's avatar
Owen Taylor committed
183
static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
184
                                                  guint      signal_id);
Elliot Lee's avatar
Elliot Lee committed
185

186
static void gtk_real_menu_item_set_label (GtkMenuItem     *menu_item,
187
                                          const gchar     *label);
188
static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
189

190 191
static void gtk_menu_item_buildable_interface_init (GtkBuildableIface   *iface);
static void gtk_menu_item_buildable_add_child      (GtkBuildable        *buildable,
192 193 194
                                                    GtkBuilder          *builder,
                                                    GObject             *child,
                                                    const gchar         *type);
195
static void gtk_menu_item_buildable_custom_finished(GtkBuildable        *buildable,
196 197 198 199
                                                    GtkBuilder          *builder,
                                                    GObject             *child,
                                                    const gchar         *tagname,
                                                    gpointer             user_data);
200

201
static void gtk_menu_item_actionable_interface_init  (GtkActionableInterface *iface);
202
static void gtk_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
203
static void gtk_menu_item_update                     (GtkActivatable       *activatable,
204 205
                                                      GtkAction            *action,
                                                      const gchar          *property_name);
206
static void gtk_menu_item_sync_action_properties     (GtkActivatable       *activatable,
207
                                                      GtkAction            *action);
208
static void gtk_menu_item_set_related_action         (GtkMenuItem          *menu_item, 
209
                                                      GtkAction            *action);
210
static void gtk_menu_item_set_use_action_appearance  (GtkMenuItem          *menu_item, 
211
                                                      gboolean              use_appearance);
212

213
static guint menu_item_signals[LAST_SIGNAL] = { 0 };
214
static GParamSpec *menu_item_props[LAST_PROP];
Elliot Lee's avatar
Elliot Lee committed
215

216 217
static GtkBuildableIface *parent_buildable_iface;

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
                         G_ADD_PRIVATE (GtkMenuItem)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_menu_item_buildable_interface_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
                                                gtk_menu_item_activatable_interface_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE,
                                                gtk_menu_item_actionable_interface_init))
G_GNUC_END_IGNORE_DEPRECATIONS;

static void
gtk_menu_item_set_action_name (GtkActionable *actionable,
                               const gchar   *action_name)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);

  if (!menu_item->priv->action_helper)
    menu_item->priv->action_helper = gtk_action_helper_new (actionable);

  gtk_action_helper_set_action_name (menu_item->priv->action_helper, action_name);
}

static void
gtk_menu_item_set_action_target_value (GtkActionable *actionable,
                                       GVariant      *action_target)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);

  if (!menu_item->priv->action_helper)
    menu_item->priv->action_helper = gtk_action_helper_new (actionable);

  gtk_action_helper_set_action_target_value (menu_item->priv->action_helper, action_target);
}

static const gchar *
gtk_menu_item_get_action_name (GtkActionable *actionable)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);

  return gtk_action_helper_get_action_name (menu_item->priv->action_helper);
}

static GVariant *
gtk_menu_item_get_action_target_value (GtkActionable *actionable)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);

  return gtk_action_helper_get_action_target_value (menu_item->priv->action_helper);
}

static void
gtk_menu_item_actionable_interface_init (GtkActionableInterface *iface)
{
  iface->set_action_name = gtk_menu_item_set_action_name;
  iface->get_action_name = gtk_menu_item_get_action_name;
  iface->set_action_target_value = gtk_menu_item_set_action_target_value;
  iface->get_action_target_value = gtk_menu_item_get_action_target_value;
}

static gboolean
gtk_menu_item_render (GtkCssGadget *gadget,
                      cairo_t      *cr,
                      int           x,
                      int           y,
                      int           width,
                      int           height,
                      gpointer      data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
290
  GtkWidget *parent;
291 292 293 294

  parent = gtk_widget_get_parent (widget);

  if (priv->submenu && !GTK_IS_MENU_BAR (parent))
295
    gtk_css_gadget_draw (priv->arrow_gadget, cr);
296

297 298
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  return FALSE;
}

static gboolean
gtk_menu_item_draw (GtkWidget *widget,
                    cairo_t   *cr)
{
  gtk_css_gadget_draw (GTK_MENU_ITEM (widget)->priv->gadget, cr);

  return FALSE;
}

static void
gtk_menu_item_allocate (GtkCssGadget        *gadget,
                        const GtkAllocation *allocation,
                        int                  baseline,
                        GtkAllocation       *out_clip,
                        gpointer             data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
  GtkAllocation child_allocation;
322
  GtkAllocation arrow_clip = { 0 };
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  GtkTextDirection direction;
  GtkPackDirection child_pack_dir;
  GtkWidget *child;
  GtkWidget *parent;

  g_return_if_fail (GTK_IS_MENU_ITEM (widget));
  g_return_if_fail (allocation != NULL);

  direction = gtk_widget_get_direction (widget);

  parent = gtk_widget_get_parent (widget);
  if (GTK_IS_MENU_BAR (parent))
    {
      child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
    }
  else
    {
      child_pack_dir = GTK_PACK_DIRECTION_LTR;
    }

  child = gtk_bin_get_child (GTK_BIN (widget));
  if (child)
    {
      child_allocation = *allocation;

      if (child_pack_dir == GTK_PACK_DIRECTION_LTR ||
          child_pack_dir == GTK_PACK_DIRECTION_RTL)
        {
          if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_RTL))
            child_allocation.x += priv->toggle_size;
          child_allocation.width -= priv->toggle_size;
        }
      else
        {
          if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
            child_allocation.y += priv->toggle_size;
          child_allocation.height -= priv->toggle_size;
        }

      if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
	{
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
          GtkAllocation arrow_alloc;

          gtk_css_gadget_get_preferred_size (priv->arrow_gadget,
                                             GTK_ORIENTATION_HORIZONTAL,
                                             -1,
                                             &arrow_alloc.width, NULL,
                                             NULL, NULL);
          gtk_css_gadget_get_preferred_size (priv->arrow_gadget,
                                             GTK_ORIENTATION_VERTICAL,
                                             -1,
                                             &arrow_alloc.height, NULL,
                                             NULL, NULL);

          if (direction == GTK_TEXT_DIR_LTR)
            {
              arrow_alloc.x = child_allocation.x +
                child_allocation.width - arrow_alloc.width;
            }
          else
            {
              arrow_alloc.x = 0;
              child_allocation.x += arrow_alloc.width;
            }
387

388 389 390
          child_allocation.width -= arrow_alloc.width;
          arrow_alloc.y = child_allocation.y +
            (child_allocation.height - arrow_alloc.height) / 2;
391

392 393 394 395
          gtk_css_gadget_allocate (priv->arrow_gadget,
                                   &arrow_alloc,
                                   baseline,
                                   &arrow_clip);
396 397
	}

398
      child_allocation.width = MAX (1, child_allocation.width);
399 400

      gtk_widget_size_allocate (child, &child_allocation);
401 402 403

      gtk_container_get_children_clip (GTK_CONTAINER (widget), out_clip);
      gdk_rectangle_union (out_clip, &arrow_clip, out_clip);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    }

  if (priv->submenu)
    gtk_menu_reposition (GTK_MENU (priv->submenu));
}

static void
gtk_menu_item_size_allocate (GtkWidget     *widget,
                             GtkAllocation *allocation)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
  GtkAllocation clip;
  
  gtk_widget_set_allocation (widget, allocation);

420 421 422 423 424
  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (priv->event_window,
                            allocation->x, allocation->y,
                            allocation->width, allocation->height);

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  gtk_css_gadget_allocate (priv->gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_menu_item_accel_width_foreach (GtkWidget *widget,
                                   gpointer   data)
{
  guint *width = data;

  if (GTK_IS_ACCEL_LABEL (widget))
    {
      guint w;

      w = gtk_accel_label_get_accel_width (GTK_ACCEL_LABEL (widget));
      *width = MAX (*width, w);
    }
  else if (GTK_IS_CONTAINER (widget))
    gtk_container_foreach (GTK_CONTAINER (widget),
                           gtk_menu_item_accel_width_foreach,
                           data);
}

static void
gtk_menu_item_real_get_width (GtkWidget *widget,
                              gint      *minimum_size,
                              gint      *natural_size)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
  GtkWidget *child;
  GtkWidget *parent;
  guint accel_width;
  gint  min_width, nat_width;

  min_width = nat_width = 0;

  parent = gtk_widget_get_parent (widget);
  child = gtk_bin_get_child (GTK_BIN (widget));

  if (child != NULL && gtk_widget_get_visible (child))
    {
      gint child_min, child_nat;

      gtk_widget_get_preferred_width (child, &child_min, &child_nat);

475
      if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
476
	{
477
          gint arrow_size;
478

479 480 481 482 483
          gtk_css_gadget_get_preferred_size (priv->arrow_gadget,
                                             GTK_ORIENTATION_HORIZONTAL,
                                             -1,
                                             &arrow_size, NULL,
                                             NULL, NULL);
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

          min_width += arrow_size;
          nat_width = min_width;
        }

      min_width += child_min;
      nat_width += child_nat;
    }

  accel_width = 0;
  gtk_container_foreach (GTK_CONTAINER (menu_item),
                         gtk_menu_item_accel_width_foreach,
                         &accel_width);
  priv->accelerator_width = accel_width;

  *minimum_size = min_width;
  *natural_size = nat_width;
}

static void
gtk_menu_item_real_get_height (GtkWidget *widget,
                               gint       for_size,
                               gint      *minimum_size,
                               gint      *natural_size)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
  GtkWidget *child;
  GtkWidget *parent;
  guint accel_width;
  gint min_height, nat_height;
  gint avail_size = 0;

  min_height = nat_height = 0;

  if (for_size != -1)
    avail_size = for_size;

  parent = gtk_widget_get_parent (widget);
  child = gtk_bin_get_child (GTK_BIN (widget));

  if (child != NULL && gtk_widget_get_visible (child))
    {
      gint child_min, child_nat;
528
      gint arrow_size = 0;
529 530

      if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
531 532 533 534 535
        gtk_css_gadget_get_preferred_size (priv->arrow_gadget,
                                           GTK_ORIENTATION_VERTICAL,
                                           -1,
                                           &arrow_size, NULL,
                                           NULL, NULL);
536 537 538

      if (for_size != -1)
        {
539
          avail_size -= arrow_size;
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
          gtk_widget_get_preferred_height_for_width (child,
                                                     avail_size,
                                                     &child_min,
                                                     &child_nat);
        }
      else
        {
          gtk_widget_get_preferred_height (child, &child_min, &child_nat);
        }

      min_height += child_min;
      nat_height += child_nat;

      min_height = MAX (min_height, arrow_size);
      nat_height = MAX (nat_height, arrow_size);
    }
556

557 558 559 560 561
  accel_width = 0;
  gtk_container_foreach (GTK_CONTAINER (menu_item),
                         gtk_menu_item_accel_width_foreach,
                         &accel_width);
  priv->accelerator_width = accel_width;
562

563 564
  *minimum_size = min_height;
  *natural_size = nat_height;
565 566 567
}

static void
568 569 570 571 572 573 574 575
gtk_menu_item_measure (GtkCssGadget   *gadget,
                       GtkOrientation  orientation,
                       int             size,
                       int            *minimum,
                       int            *natural,
                       int            *minimum_baseline,
                       int            *natural_baseline,
                       gpointer        data)
576
{
577
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
578

579 580 581 582
  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    gtk_menu_item_real_get_width (widget, minimum, natural);
  else
    gtk_menu_item_real_get_height (widget, size, minimum, natural);
583 584
}

585 586 587 588
static void
gtk_menu_item_get_preferred_width (GtkWidget *widget,
                                   gint      *minimum_size,
                                   gint      *natural_size)
589
{
590 591 592 593 594
  gtk_css_gadget_get_preferred_size (GTK_MENU_ITEM (widget)->priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum_size, natural_size,
                                     NULL, NULL);
595 596
}

597 598 599 600
static void
gtk_menu_item_get_preferred_height (GtkWidget *widget,
                                    gint      *minimum_size,
                                    gint      *natural_size)
601
{
602 603 604 605 606
  gtk_css_gadget_get_preferred_size (GTK_MENU_ITEM (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     minimum_size, natural_size,
                                     NULL, NULL);
607 608 609
}

static void
610 611 612 613
gtk_menu_item_get_preferred_height_for_width (GtkWidget *widget,
                                              gint       for_size,
                                              gint      *minimum_size,
                                              gint      *natural_size)
614
{
615 616 617 618 619
  gtk_css_gadget_get_preferred_size (GTK_MENU_ITEM (widget)->priv->gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     for_size,
                                     minimum_size, natural_size,
                                     NULL, NULL);
620
}
621

Elliot Lee's avatar
Elliot Lee committed
622 623 624
static void
gtk_menu_item_class_init (GtkMenuItemClass *klass)
{
625 626 627
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
628

629
  gobject_class->dispose = gtk_menu_item_dispose;
630 631
  gobject_class->set_property = gtk_menu_item_set_property;
  gobject_class->get_property = gtk_menu_item_get_property;
632

633
  widget_class->destroy = gtk_menu_item_destroy;
634
  widget_class->size_allocate = gtk_menu_item_size_allocate;
635
  widget_class->draw = gtk_menu_item_draw;
636 637 638 639
  widget_class->realize = gtk_menu_item_realize;
  widget_class->unrealize = gtk_menu_item_unrealize;
  widget_class->map = gtk_menu_item_map;
  widget_class->unmap = gtk_menu_item_unmap;
Matthias Clasen's avatar
Matthias Clasen committed
640 641
  widget_class->enter_notify_event = gtk_menu_item_enter;
  widget_class->leave_notify_event = gtk_menu_item_leave;
642
  widget_class->show_all = gtk_menu_item_show_all;
643
  widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
644
  widget_class->parent_set = gtk_menu_item_parent_set;
Owen Taylor's avatar
Owen Taylor committed
645
  widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
646 647 648
  widget_class->get_preferred_width = gtk_menu_item_get_preferred_width;
  widget_class->get_preferred_height = gtk_menu_item_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_menu_item_get_preferred_height_for_width;
649
  widget_class->direction_changed = gtk_menu_item_direction_changed;
650

651 652
  container_class->forall = gtk_menu_item_forall;

653 654 655
  klass->activate = gtk_real_menu_item_activate;
  klass->activate_item = gtk_real_menu_item_activate_item;
  klass->toggle_size_request = gtk_real_menu_item_toggle_size_request;
656
  klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
657 658 659 660
  klass->set_label = gtk_real_menu_item_set_label;
  klass->get_label = gtk_real_menu_item_get_label;
  klass->select = gtk_real_menu_item_select;
  klass->deselect = gtk_real_menu_item_deselect;
661 662 663

  klass->hide_on_activate = TRUE;

664 665 666 667 668 669
  /**
   * GtkMenuItem::activate:
   * @menuitem: the object which received the signal.
   *
   * Emitted when the item is activated.
   */
Elliot Lee's avatar
Elliot Lee committed
670
  menu_item_signals[ACTIVATE] =
671
    g_signal_new (I_("activate"),
672 673 674 675
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkMenuItemClass, activate),
                  NULL, NULL,
676
                  NULL,
677
                  G_TYPE_NONE, 0);
678
  widget_class->activate_signal = menu_item_signals[ACTIVATE];
Elliot Lee's avatar
Elliot Lee committed
679

680 681 682 683 684 685 686 687
  /**
   * GtkMenuItem::activate-item:
   * @menuitem: the object which received the signal.
   *
   * Emitted when the item is activated, but also if the menu item has a
   * submenu. For normal applications, the relevant signal is
   * #GtkMenuItem::activate.
   */
688
  menu_item_signals[ACTIVATE_ITEM] =
689
    g_signal_new (I_("activate-item"),
690 691 692 693
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
                  NULL, NULL,
694
                  NULL,
695
                  G_TYPE_NONE, 0);
696

697
  menu_item_signals[TOGGLE_SIZE_REQUEST] =
698
    g_signal_new (I_("toggle-size-request"),
699 700 701 702
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_request),
                  NULL, NULL,
703
                  NULL,
704 705
                  G_TYPE_NONE, 1,
                  G_TYPE_POINTER);
706 707

  menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
708
    g_signal_new (I_("toggle-size-allocate"),
709 710 711 712
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
                  NULL, NULL,
713
                  NULL,
714 715
                  G_TYPE_NONE, 1,
                  G_TYPE_INT);
716

Matthias Clasen's avatar
Matthias Clasen committed
717 718 719 720 721 722
  menu_item_signals[SELECT] =
    g_signal_new (I_("select"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuItemClass, select),
                  NULL, NULL,
723
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
724 725 726 727 728 729 730 731
                  G_TYPE_NONE, 0);

  menu_item_signals[DESELECT] =
    g_signal_new (I_("deselect"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuItemClass, deselect),
                  NULL, NULL,
732
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
733 734
                  G_TYPE_NONE, 0);

Tim Janik's avatar
Tim Janik committed
735 736 737
  /**
   * GtkMenuItem:right-justified:
   *
738 739
   * Sets whether the menu item appears justified
   * at the right side of a menu bar.
Tim Janik's avatar
Tim Janik committed
740
   *
741
   * Since: 2.14
742
   */
743 744 745 746 747 748
  menu_item_props[PROP_RIGHT_JUSTIFIED] =
      g_param_spec_boolean ("right-justified",
                            P_("Right Justified"),
                            P_("Sets whether the menu item appears justified at the right side of a menu bar"),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY|G_PARAM_DEPRECATED);
Tim Janik's avatar
Tim Janik committed
749

750 751 752
  /**
   * GtkMenuItem:submenu:
   *
753
   * The submenu attached to the menu item, or %NULL if it has none.
754 755
   *
   * Since: 2.12
756
   */
757 758 759 760 761 762
  menu_item_props[PROP_SUBMENU] =
      g_param_spec_object ("submenu",
                           P_("Submenu"),
                           P_("The submenu attached to the menu item, or NULL if it has none"),
                           GTK_TYPE_MENU,
                           GTK_PARAM_READWRITE);
763

Tim Janik's avatar
Tim Janik committed
764 765 766 767 768 769 770
  /**
   * GtkMenuItem:accel-path:
   *
   * Sets the accelerator path of the menu item, through which runtime
   * changes of the menu item's accelerator caused by the user can be
   * identified and saved to persistant storage.
   *
771
   * Since: 2.14
772
   */
773 774 775 776 777 778
  menu_item_props[PROP_ACCEL_PATH] =
      g_param_spec_string ("accel-path",
                           P_("Accel Path"),
                           P_("Sets the accelerator path of the menu item"),
                           NULL,
                           GTK_PARAM_READWRITE);
Tim Janik's avatar
Tim Janik committed
779

780 781 782 783 784 785
  /**
   * GtkMenuItem:label:
   *
   * The text for the child label.
   *
   * Since: 2.16
786
   */
787 788 789 790 791 792
  menu_item_props[PROP_LABEL] =
      g_param_spec_string ("label",
                           P_("Label"),
                           P_("The text for the child label"),
                           "",
                           GTK_PARAM_READWRITE);
793 794 795 796

  /**
   * GtkMenuItem:use-underline:
   *
797
   * %TRUE if underlines in the text indicate mnemonics.
798 799
   *
   * Since: 2.16
800
   */
801 802 803 804 805 806 807 808 809 810
  menu_item_props[PROP_USE_UNDERLINE] =
      g_param_spec_boolean ("use-underline",
                            P_("Use underline"),
                            P_("If set, an underline in the text indicates "
                               "the next character should be used for the "
                               "mnemonic accelerator key"),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (gobject_class, LAST_PROP, menu_item_props);
811

812 813 814
  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");

815 816 817
  g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
  g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");

818 819 820 821 822 823 824 825
  /**
   * GtkMenuItem:selected-shadow-type:
   *
   * The shadow type when the item is selected.
   *
   * Deprecated: 3.20: Use CSS to determine the shadow; the value of this
   *     style property is ignored.
   */
826 827 828 829 830 831
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_enum ("selected-shadow-type",
                                                              "Selected Shadow Type",
                                                              "Shadow type when item is selected",
                                                              GTK_TYPE_SHADOW_TYPE,
                                                              GTK_SHADOW_NONE,
832
                                                              GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
833

834 835 836 837 838 839 840 841 842
  /**
   * GtkMenuItem:horizontal-padding:
   *
   * Padding to left and right of the menu item.
   *
   * Deprecated: 3.8: use the standard padding CSS property (through objects
   *   like #GtkStyleContext and #GtkCssProvider); the value of this style
   *   property is ignored.
   */
843
  gtk_widget_class_install_style_property (widget_class,
844 845 846 847 848
                                           g_param_spec_int ("horizontal-padding",
                                                             "Horizontal Padding",
                                                             "Padding to left and right of the menu item",
                                                             0,
                                                             G_MAXINT,
849 850 851
                                                             0,
                                                             GTK_PARAM_READABLE |
                                                             G_PARAM_DEPRECATED));
852

853 854 855 856 857 858 859 860
  /**
   * GtkMenuItem:toggle-spacing:
   *
   * Spacing between menu icon and label.
   *
   * Deprecated: 3.20: use the standard margin CSS property on the check or
   *   radio nodes; the value of this style property is ignored.
   */
861
  gtk_widget_class_install_style_property (widget_class,
862 863 864 865 866 867
                                           g_param_spec_int ("toggle-spacing",
                                                             "Icon Spacing",
                                                             "Space between icon and label",
                                                             0,
                                                             G_MAXINT,
                                                             5,
868
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
869

870 871 872 873 874 875 876 877
  /**
   * GtkMenuItem:arrow-spacing:
   *
   * Spacing between menu item label and submenu arrow.
   *
   * Deprecated: 3.20: use the standard margin CSS property on the arrow node;
   *   the value of this style property is ignored.
   */
878
  gtk_widget_class_install_style_property (widget_class,
879 880 881 882 883 884
                                           g_param_spec_int ("arrow-spacing",
                                                             "Arrow Spacing",
                                                             "Space between label and arrow",
                                                             0,
                                                             G_MAXINT,
                                                             10,
885
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
886

887 888 889 890 891 892
  /**
   * GtkMenuItem:arrow-scaling:
   *
   * Amount of space used up by the arrow, relative to the menu item's font
   * size.
   *
893 894
   * Deprecated: 3.20: use the standard min-width/min-height CSS properties on
   *   the arrow node; the value of this style property is ignored.
895
   */
896 897 898 899 900
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_float ("arrow-scaling",
                                                               P_("Arrow Scaling"),
                                                               P_("Amount of space used up by arrow, relative to the menu item's font size"),
                                                               0.0, 2.0, 0.8,
901
                                                               GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
902 903 904 905 906 907 908

  /**
   * GtkMenuItem:width-chars:
   *
   * The minimum desired width of the menu item in characters.
   *
   * Since: 2.14
909 910 911
   *
   * Deprecated: 3.20: Use the standard CSS property min-width; the value of
   *     this style property is ignored.
912
   */
913 914 915 916 917
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("width-chars",
                                                             P_("Width in Characters"),
                                                             P_("The minimum desired width of the menu item in characters"),
                                                             0, G_MAXINT, 12,
918
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
919 920 921

  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
  gtk_widget_class_set_css_name (widget_class, "menuitem");
922 923

  gtk_container_class_handle_border_width (container_class);
Elliot Lee's avatar
Elliot Lee committed
924 925 926 927 928
}

static void
gtk_menu_item_init (GtkMenuItem *menu_item)
{
929
  GtkMenuItemPrivate *priv;
930
  GtkCssNode *widget_node;
931

932
  priv = gtk_menu_item_get_instance_private (menu_item);
933
  menu_item->priv = priv;
934

935
  gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
936

937 938 939
  priv->action = NULL;
  priv->use_action_appearance = TRUE;
  
940 941 942
  priv->submenu = NULL;
  priv->toggle_size = 0;
  priv->accelerator_width = 0;
943
  if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
944
    priv->submenu_direction = GTK_DIRECTION_LEFT;
945
  else
946 947 948 949 950 951
    priv->submenu_direction = GTK_DIRECTION_RIGHT;
  priv->submenu_placement = GTK_TOP_BOTTOM;
  priv->right_justify = FALSE;
  priv->use_action_appearance = TRUE;
  priv->timer = 0;
  priv->action = NULL;
952 953 954 955 956 957 958 959

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (menu_item));
  priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                     GTK_WIDGET (menu_item),
                                                     gtk_menu_item_measure,
                                                     gtk_menu_item_allocate,
                                                     gtk_menu_item_render,
                                                     NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
960 961
}

962 963 964 965 966 967
GtkCssGadget *
_gtk_menu_item_get_gadget (GtkMenuItem *menu_item)
{
  return menu_item->priv->gadget;
}

968 969 970 971 972 973 974
/**
 * gtk_menu_item_new:
 *
 * Creates a new #GtkMenuItem.
 *
 * Returns: the newly created #GtkMenuItem
 */
Elliot Lee's avatar
Elliot Lee committed
975
GtkWidget*
976
gtk_menu_item_new (void)
Elliot Lee's avatar
Elliot Lee committed
977
{
Manish Singh's avatar
Manish Singh committed
978
  return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
Elliot Lee's avatar
Elliot Lee committed
979 980
}

981 982 983 984 985 986 987 988
/**
 * gtk_menu_item_new_with_label:
 * @label: the text for the label
 *
 * Creates a new #GtkMenuItem whose child is a #GtkLabel.
 *
 * Returns: the newly created #GtkMenuItem
 */
Elliot Lee's avatar
Elliot Lee committed
989 990 991
GtkWidget*
gtk_menu_item_new_with_label (const gchar *label)
{
992 993 994
  return g_object_new (GTK_TYPE_MENU_ITEM,
                       "label", label,
                       NULL);
Elliot Lee's avatar
Elliot Lee committed
995 996
}

997 998 999 1000

/**
 * gtk_menu_item_new_with_mnemonic:
 * @label: The text of the button, with an underscore in front of the
1001
 *     mnemonic character
1002
 *
1003 1004 1005 1006 1007 1008 1009
 * Creates a new #GtkMenuItem containing a label.
 *
 * The label will be created using gtk_label_new_with_mnemonic(),
 * so underscores in @label indicate the mnemonic for the menu item.
 *
 * Returns: a new #GtkMenuItem
 */
1010 1011 1012
GtkWidget*
gtk_menu_item_new_with_mnemonic (const gchar *label)
{
1013 1014 1015 1016
  return g_object_new (GTK_TYPE_MENU_ITEM,
                       "use-underline", TRUE,
                       "label", label,
                       NULL);
1017 1018
}

1019 1020 1021 1022
static void
gtk_menu_item_dispose (GObject *object)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
1023
  GtkMenuItemPrivate *priv = menu_item->priv;
1024

1025 1026
  g_clear_object (&priv->action_helper);

1027 1028
  if (priv->action)
    {
1029
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1030 1031
      gtk_action_disconnect_accelerator (priv->action);
      gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
1032
      G_GNUC_END_IGNORE_DEPRECATIONS;
1033 1034
      priv->action = NULL;
    }
1035

1036
  g_clear_object (&priv->arrow_gadget);
1037 1038
  g_clear_object (&priv->gadget);

1039 1040 1041
  G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
}

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
static void
gtk_menu_item_do_set_right_justified (GtkMenuItem *menu_item,
                                      gboolean     right_justified)
{
  GtkMenuItemPrivate *priv = menu_item->priv;

  right_justified = right_justified != FALSE;

  if (priv->right_justify != right_justified)
    {
      priv->right_justify = right_justified;
      gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1054
      g_object_notify_by_pspec (G_OBJECT (menu_item), menu_item_props[PROP_RIGHT_JUSTIFIED]);
1055 1056 1057
    }
}

1058
static void
1059
gtk_menu_item_set_property (GObject      *object,
1060 1061 1062
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
Elliot Lee's avatar
Elliot Lee committed
1063
{
1064
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
1065

1066 1067
  switch (prop_id)
    {
Tim Janik's avatar
Tim Janik committed
1068
    case PROP_RIGHT_JUSTIFIED:
1069
      gtk_menu_item_do_set_right_justified (menu_item, g_value_get_boolean (value));
Tim Janik's avatar
Tim Janik committed
1070
      break;
1071 1072 1073
    case PROP_SUBMENU:
      gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
      break;
Tim Janik's avatar
Tim Janik committed
1074 1075 1076
    case PROP_ACCEL_PATH:
      gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
      break;
1077 1078 1079 1080 1081 1082
    case PROP_LABEL:
      gtk_menu_item_set_label (menu_item, g_value_get_string (value));
      break;
    case PROP_USE_UNDERLINE:
      gtk_menu_item_set_use_underline (menu_item, g_value_get_boolean (value));
      break;
1083 1084 1085 1086 1087 1088
    case PROP_ACTIVATABLE_RELATED_ACTION:
      gtk_menu_item_set_related_action (menu_item, g_value_get_object (value));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
      gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
      break;
1089 1090 1091 1092 1093 1094
    case PROP_ACTION_NAME:
      gtk_menu_item_set_action_name (GTK_ACTIONABLE (menu_item), g_value_get_string (value));
      break;
    case PROP_ACTION_TARGET:
      gtk_menu_item_set_action_target_value (GTK_ACTIONABLE (menu_item), g_value_get_variant (value));
      break;
1095 1096 1097 1098 1099
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
Elliot Lee's avatar
Elliot Lee committed
1100

1101
static void
1102
gtk_menu_item_get_property (GObject    *object,
1103 1104 1105
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
1106 1107
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
1108 1109
  GtkMenuItemPrivate *priv = menu_item->priv;

1110 1111
  switch (prop_id)
    {
Tim Janik's avatar
Tim Janik committed
1112
    case PROP_RIGHT_JUSTIFIED:
1113
      g_value_set_boolean (value, priv->right_justify);
Tim Janik's avatar
Tim Janik committed
1114
      break;
1115 1116 1117
    case PROP_SUBMENU:
      g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
      break;
Tim Janik's avatar
Tim Janik committed
1118 1119 1120
    case PROP_ACCEL_PATH:
      g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
      break;
1121 1122 1123 1124 1125 1126
    case PROP_LABEL:
      g_value_set_string (value, gtk_menu_item_get_label (menu_item));
      break;
    case PROP_USE_UNDERLINE:
      g_value_set_boolean (value, gtk_menu_item_get_use_underline (menu_item));
      break;
1127 1128 1129 1130 1131 1132
    case PROP_ACTIVATABLE_RELATED_ACTION:
      g_value_set_object (value, priv->action);
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
      g_value_set_boolean (value, priv->use_action_appearance);
      break;
1133 1134
    case PROP_ACTION_NAME:
      g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
1135 1136 1137 1138 1139 1140 1141 1142
      break;
    case PROP_ACTION_TARGET:
      g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
1143 1144
}

1145
static void
1146
gtk_menu_item_destroy (GtkWidget *widget)
1147
{
1148 1149
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
1150

1151 1152
  if (priv->submenu)
    gtk_widget_destroy (priv->submenu);
1153

1154 1155
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
}
1156

1157 1158 1159 1160 1161 1162
static void
gtk_menu_item_detacher (GtkWidget *widget,
                        GtkMenu   *menu)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
1163

1164
  g_return_if_fail (priv->submenu == (GtkWidget*) menu);
1165

1166
  priv->submenu = NULL;
1167
  g_clear_object (&priv->arrow_gadget);
1168 1169
}

1170 1171 1172 1173 1174
static void
gtk_menu_item_buildable_interface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
  iface->add_child = gtk_menu_item_buildable_add_child;
1175
  iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1176 1177
}

Matthias Clasen's avatar
Matthias Clasen committed
1178
static void
1179
gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
1180 1181 1182
                                   GtkBuilder   *builder,
                                   GObject      *child,
                                   const gchar  *type)
1183 1184
{
  if (type && strcmp (type, "submenu") == 0)
1185 1186
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
                                   GTK_WIDGET (child));
1187 1188 1189 1190
  else
    parent_buildable_iface->add_child (buildable, builder, child, type);
}

1191

1192 1193 1194 1195 1196 1197
static void
gtk_menu_item_buildable_custom_finished (GtkBuildable *buildable,
                                         GtkBuilder   *builder,
                                         GObject      *child,
                                         const gchar  *tagname,
                                         gpointer      user_data)
1198 1199 1200 1201 1202
{
  GtkWidget *toplevel;

  if (strcmp (tagname, "accelerator") == 0)
    {
1203
      GtkMenuShell *menu_shell;
1204 1205
      GtkWidget *attach;

1206
      menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1207
      if (menu_shell)
1208 1209 1210 1211 1212 1213 1214
        {
          while (GTK_IS_MENU (menu_shell) &&
                 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
            menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));

          toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
        }
1215
      else
1216 1217 1218
        {
          /* Fall back to something ... */
          toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1219

1220 1221 1222
          g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
                     gtk_buildable_get_name (buildable));
        }
1223 1224 1225 1226 1227 1228 1229 1230 1231

      /* Feed the correct toplevel to the GtkWidget accelerator parsing code */
      _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
    }
  else
    parent_buildable_iface->custom_finished (buildable, builder, child, tagname, user_data);
}


1232 1233 1234
static void
gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
{
1235 1236
  iface->update = gtk_menu_item_update;
  iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1237 1238
}

1239
static void
1240 1241
activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
{
Javier Jardón's avatar
Javier Jardón committed
1242 1243 1244
  GtkWidget *child;

  child = gtk_bin_get_child (GTK_BIN (menu_item));
1245

1246 1247 1248 1249
  if (GTK_IS_LABEL (child))
    {
      const gchar *label;

1250
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1251
      label = gtk_action_get_label (action);
1252
      G_GNUC_END_IGNORE_DEPRECATIONS;
1253
      gtk_menu_item_set_label (menu_item, label);
1254 1255 1256
    }
}

1257 1258 1259 1260 1261
/*
 * gtk_menu_is_empty:
 * @menu: (allow-none): a #GtkMenu or %NULL
 * 
 * Determines whether @menu is empty. A menu is considered empty if it
1262
 * the only visible children are tearoff menu items or “filler” menu 
1263 1264 1265 1266
 * items which were inserted to mark the menu as empty.
 * 
 * This function is used by #GtkAction.
 *
1267
 * Returns: whether @menu is empty.
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
 **/
static gboolean
gtk_menu_is_empty (GtkWidget *menu)
{
  GList *children, *cur;
  gboolean result = TRUE;

  g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);

  if (!menu)
    return FALSE;

  children = gtk_container_get_children (GTK_CONTAINER (menu));

  cur = children;
  while (cur) 
    {
      if (gtk_widget_get_visible (cur->data))
	{
1287 1288 1289

G_GNUC_BEGIN_IGNORE_DEPRECATIONS

1290 1291 1292 1293 1294 1295
	  if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
	      !g_object_get_data (cur->data, "gtk-empty-menu-item"))
            {
	      result = FALSE;
              break;
            }
1296 1297 1298

G_GNUC_END_IGNORE_DEPRECATIONS

1299 1300 1301 1302 1303 1304 1305 1306
	}
      cur = cur->next;
    }
  g_list_free (children);

  return result;
}

1307 1308

static void
1309
gtk_menu_item_update (GtkActivatable *activatable,
1310 1311
                      GtkAction      *action,
                      const gchar    *property_name)
1312 1313
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1314
  GtkMenuItemPrivate *priv = menu_item->priv;
1315 1316

  if (strcmp (property_name, "visible") == 0)
1317 1318 1319 1320 1321 1322
    {
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
                                     gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
1323
  else if (strcmp (property_name, "sensitive") == 0)
1324 1325 1326 1327 1328
    {
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
1329 1330 1331
  else if (priv->use_action_appearance)
    {
      if (strcmp (property_name, "label") == 0)
1332
        activatable_update_label (menu_item, action);
1333 1334 1335 1336
    }
}

static void
1337
gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
1338
                                      GtkAction      *action)
1339 1340
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
1341
  GtkMenuItemPrivate *priv = menu_item->priv;
Javier Jardón's avatar
Javier Jardón committed
1342
  GtkWidget *label;
1343

1344 1345
  if (!priv->use_action_appearance || !action)
    {
Javier Jardón's avatar
Javier Jardón committed
1346
      label = gtk_bin_get_child (GTK_BIN (menu_item));
1347 1348 1349 1350 1351

      if (GTK_IS_ACCEL_LABEL (label))
        gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), GTK_WIDGET (menu_item));
    }

1352 1353 1354
  if (!action)
    return;

1355
  _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1356
                                 gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1357

1358
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1359
  gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1360
  G_GNUC_END_IGNORE_DEPRECATIONS;
1361 1362 1363

  if (priv->use_action_appearance)
    {
Javier Jardón's avatar
Javier Jardón committed
1364
      label = gtk_bin_get_child (GTK_BIN (menu_item));
1365

1366
      /* make sure label is a label, deleting it otherwise */
1367
      if (label && !GTK_IS_LABEL (label))
1368 1369 1370 1371
        {
          gtk_container_remove (GTK_CONTAINER (menu_item), label);
          label = NULL;
        }
1372 1373
      /* Make sure that menu_item has a label and that any
       * accelerators are set */
1374 1375
      gtk_menu_item_ensure_label (menu_item);
      gtk_menu_item_set_use_underline (menu_item, TRUE);
1376 1377
      /* Make label point to the menu_item's label */
      label = gtk_bin_get_child (GTK_BIN (menu_item));
1378

1379
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1380
      if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1381 1382 1383 1384 1385
        {
          gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), NULL);
          gtk_accel_label_set_accel_closure (GTK_ACCEL_LABEL (label),
                                             gtk_action_get_accel_closure (action));
        }
1386
      G_GNUC_END_IGNORE_DEPRECATIONS;
1387

1388 1389 1390 1391 1392
      activatable_update_label (menu_item, action);
    }
}

static void
1393
gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1394
                                  GtkAction   *action)
1395
{
1396
    GtkMenuItemPrivate *priv = menu_item->priv;
1397 1398 1399 1400

    if (priv->action == action)
      return;

1401 1402
    G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

1403 1404
    if (priv->action)
      {
1405
        gtk_action_disconnect_accelerator (priv->action);
1406 1407 1408 1409
      }

    if (action)
      {
1410 1411 1412 1413 1414 1415 1416 1417
        const gchar *accel_path;

        accel_path = gtk_action_get_accel_path (action);
        if (accel_path)
          {
            gtk_action_connect_accelerator (action);
            gtk_menu_item_set_accel_path (menu_item, accel_path);
          }
1418 1419 1420 1421
      }

    gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);

1422 1423
    G_GNUC_END_IGNORE_DEPRECATIONS;

1424 1425 1426 1427
    priv->action = action;
}

static void