gtkmenuitem.c 84.5 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 76
 * 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);
77
 * ]|
78
 *
79
 * # GtkMenuItem as GtkBuildable
80
 *
81 82 83
 * The GtkMenuItem implementation of the #GtkBuildable interface supports
 * adding a submenu by specifying “submenu” as the “type” attribute of
 * a <child> element.
84 85
 *
 * An example of UI definition fragment with submenus:
86
 * |[
87 88 89 90 91
 * <object class="GtkMenuItem">
 *   <child type="submenu">
 *     <object class="GtkMenu"/>
 *   </child>
 * </object>
92
 * ]|
93 94 95
 *
 * # CSS nodes
 *
96 97
 * |[<!-- language="plain" -->
 * menuitem
98
 * ├── <child>
99 100 101
 * ╰── [arrow.right]
 * ]|
 *
102 103 104
 * 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.
105 106 107
 */


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

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

126 127
  LAST_PROP,

128
  /* activatable properties */
129
  PROP_ACTIVATABLE_RELATED_ACTION = LAST_PROP,
130 131 132 133
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE,

  PROP_ACTION_NAME,
  PROP_ACTION_TARGET
134
};
Elliot Lee's avatar
Elliot Lee committed
135

136

137
static void gtk_menu_item_dispose        (GObject          *object);
138
static void gtk_menu_item_set_property   (GObject          *object,
139 140 141
                                          guint             prop_id,
                                          const GValue     *value,
                                          GParamSpec       *pspec);
142
static void gtk_menu_item_get_property   (GObject          *object,
143 144 145
                                          guint             prop_id,
                                          GValue           *value,
                                          GParamSpec       *pspec);
146
static void gtk_menu_item_destroy        (GtkWidget        *widget);
147 148 149 150
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
151 152 153 154
static gboolean gtk_menu_item_enter      (GtkWidget        *widget,
                                          GdkEventCrossing *event);
static gboolean gtk_menu_item_leave      (GtkWidget        *widget,
                                          GdkEventCrossing *event);
155
static void gtk_menu_item_parent_set     (GtkWidget        *widget,
156
                                          GtkWidget        *previous_parent);
157 158
static void gtk_menu_item_direction_changed (GtkWidget        *widget,
                                             GtkTextDirection  previous_dir);
159

160

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

172
static void gtk_menu_item_ensure_label   (GtkMenuItem      *menu_item);
173
static gint gtk_menu_item_popup_timeout  (gpointer          data);
Elliot Lee's avatar
Elliot Lee committed
174
static void gtk_menu_item_position_menu  (GtkMenu          *menu,
175 176 177 178
                                          gint             *x,
                                          gint             *y,
                                          gboolean         *push_in,
                                          gpointer          user_data);
179
static void gtk_menu_item_show_all       (GtkWidget        *widget);
180

181
static void gtk_menu_item_forall         (GtkContainer    *container,
182 183 184
                                          gboolean         include_internals,
                                          GtkCallback      callback,
                                          gpointer         callback_data);
185

Owen Taylor's avatar
Owen Taylor committed
186
static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
187
                                                  guint      signal_id);
Elliot Lee's avatar
Elliot Lee committed
188

189
static void gtk_real_menu_item_set_label (GtkMenuItem     *menu_item,
190
                                          const gchar     *label);
191
static const gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);
192

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

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

216
static guint menu_item_signals[LAST_SIGNAL] = { 0 };
217
static GParamSpec *menu_item_props[LAST_PROP];
Elliot Lee's avatar
Elliot Lee committed
218

219 220
static GtkBuildableIface *parent_buildable_iface;

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 290 291 292
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;
293
  GtkWidget *parent;
294 295 296 297

  parent = gtk_widget_get_parent (widget);

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

300 301
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  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;
325
  GtkAllocation arrow_clip = { 0 };
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 364 365 366
  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)
	{
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
          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;
            }
390

391 392 393
          child_allocation.width -= arrow_alloc.width;
          arrow_alloc.y = child_allocation.y +
            (child_allocation.height - arrow_alloc.height) / 2;
394

395 396 397 398
          gtk_css_gadget_allocate (priv->arrow_gadget,
                                   &arrow_alloc,
                                   baseline,
                                   &arrow_clip);
399 400
	}

401
      child_allocation.width = MAX (1, child_allocation.width);
402 403

      gtk_widget_size_allocate (child, &child_allocation);
404 405 406

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

  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);

423 424 425 426 427
  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (priv->event_window,
                            allocation->x, allocation->y,
                            allocation->width, allocation->height);

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 475 476 477
  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);

478
      if ((priv->submenu && !GTK_IS_MENU_BAR (parent)) || priv->reserve_indicator)
479
	{
480
          gint arrow_size;
481

482 483 484 485 486
          gtk_css_gadget_get_preferred_size (priv->arrow_gadget,
                                             GTK_ORIENTATION_HORIZONTAL,
                                             -1,
                                             &arrow_size, NULL,
                                             NULL, NULL);
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 528 529 530

          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;
531
      gint arrow_size = 0;
532 533

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

      if (for_size != -1)
        {
542
          avail_size -= arrow_size;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
          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);
    }
559

560 561 562 563 564
  accel_width = 0;
  gtk_container_foreach (GTK_CONTAINER (menu_item),
                         gtk_menu_item_accel_width_foreach,
                         &accel_width);
  priv->accelerator_width = accel_width;
565

566 567
  *minimum_size = min_height;
  *natural_size = nat_height;
568 569 570
}

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

582 583 584 585
  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);
586 587
}

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

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

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

Elliot Lee's avatar
Elliot Lee committed
625 626 627
static void
gtk_menu_item_class_init (GtkMenuItemClass *klass)
{
628 629 630
  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
631

632
  gobject_class->dispose = gtk_menu_item_dispose;
633 634
  gobject_class->set_property = gtk_menu_item_set_property;
  gobject_class->get_property = gtk_menu_item_get_property;
635

636
  widget_class->destroy = gtk_menu_item_destroy;
637
  widget_class->size_allocate = gtk_menu_item_size_allocate;
638
  widget_class->draw = gtk_menu_item_draw;
639 640 641 642
  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
643 644
  widget_class->enter_notify_event = gtk_menu_item_enter;
  widget_class->leave_notify_event = gtk_menu_item_leave;
645
  widget_class->show_all = gtk_menu_item_show_all;
646
  widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
647
  widget_class->parent_set = gtk_menu_item_parent_set;
Owen Taylor's avatar
Owen Taylor committed
648
  widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
649 650 651
  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;
652
  widget_class->direction_changed = gtk_menu_item_direction_changed;
653

654 655
  container_class->forall = gtk_menu_item_forall;

656 657 658
  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;
659
  klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
660 661 662 663
  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;
664 665 666

  klass->hide_on_activate = TRUE;

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

683 684 685 686 687 688 689 690
  /**
   * 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.
   */
691
  menu_item_signals[ACTIVATE_ITEM] =
692
    g_signal_new (I_("activate-item"),
693 694 695 696 697 698
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GtkMenuItemClass, activate_item),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
699

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

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

Matthias Clasen's avatar
Matthias Clasen committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
  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,
                  _gtk_marshal_VOID__VOID,
                  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,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

Tim Janik's avatar
Tim Janik committed
738 739 740
  /**
   * GtkMenuItem:right-justified:
   *
741 742
   * Sets whether the menu item appears justified
   * at the right side of a menu bar.
Tim Janik's avatar
Tim Janik committed
743
   *
744
   * Since: 2.14
745
   */
746 747 748 749 750 751
  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
752

753 754 755
  /**
   * GtkMenuItem:submenu:
   *
756
   * The submenu attached to the menu item, or %NULL if it has none.
757 758
   *
   * Since: 2.12
759
   */
760 761 762 763 764 765
  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);
766

Tim Janik's avatar
Tim Janik committed
767 768 769 770 771 772 773
  /**
   * 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.
   *
774
   * Since: 2.14
775
   */
776 777 778 779 780 781
  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
782

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

  /**
   * GtkMenuItem:use-underline:
   *
800
   * %TRUE if underlines in the text indicate mnemonics.
801 802
   *
   * Since: 2.16
803
   */
804 805 806 807 808 809 810 811 812 813
  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);
814

815 816 817
  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");

818 819 820
  g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
  g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");

821 822 823 824 825 826 827 828
  /**
   * 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.
   */
829 830 831 832 833 834
  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,
835
                                                              GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
836

837 838 839 840 841 842 843 844 845
  /**
   * 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.
   */
846
  gtk_widget_class_install_style_property (widget_class,
847 848 849 850 851
                                           g_param_spec_int ("horizontal-padding",
                                                             "Horizontal Padding",
                                                             "Padding to left and right of the menu item",
                                                             0,
                                                             G_MAXINT,
852 853 854
                                                             0,
                                                             GTK_PARAM_READABLE |
                                                             G_PARAM_DEPRECATED));
855

856 857 858 859 860 861 862 863
  /**
   * 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.
   */
864
  gtk_widget_class_install_style_property (widget_class,
865 866 867 868 869 870
                                           g_param_spec_int ("toggle-spacing",
                                                             "Icon Spacing",
                                                             "Space between icon and label",
                                                             0,
                                                             G_MAXINT,
                                                             5,
871
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
872

873 874 875 876 877 878 879 880
  /**
   * 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.
   */
881
  gtk_widget_class_install_style_property (widget_class,
882 883 884 885 886 887
                                           g_param_spec_int ("arrow-spacing",
                                                             "Arrow Spacing",
                                                             "Space between label and arrow",
                                                             0,
                                                             G_MAXINT,
                                                             10,
888
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
889

890 891 892 893 894 895
  /**
   * GtkMenuItem:arrow-scaling:
   *
   * Amount of space used up by the arrow, relative to the menu item's font
   * size.
   *
896 897
   * Deprecated: 3.20: use the standard min-width/min-height CSS properties on
   *   the arrow node; the value of this style property is ignored.
898
   */
899 900 901 902 903
  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,
904
                                                               GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
905 906 907 908 909 910 911

  /**
   * GtkMenuItem:width-chars:
   *
   * The minimum desired width of the menu item in characters.
   *
   * Since: 2.14
912 913 914
   *
   * Deprecated: 3.20: Use the standard CSS property min-width; the value of
   *     this style property is ignored.
915
   */
916 917 918 919 920
  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,
921
                                                             GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
922 923 924

  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ITEM_ACCESSIBLE);
  gtk_widget_class_set_css_name (widget_class, "menuitem");
925 926

  gtk_container_class_handle_border_width (container_class);
Elliot Lee's avatar
Elliot Lee committed
927 928 929 930 931
}

static void
gtk_menu_item_init (GtkMenuItem *menu_item)
{
932
  GtkMenuItemPrivate *priv;
933
  GtkCssNode *widget_node;
934

935
  priv = gtk_menu_item_get_instance_private (menu_item);
936
  menu_item->priv = priv;
937

938
  gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
939

940 941 942
  priv->action = NULL;
  priv->use_action_appearance = TRUE;
  
943 944 945
  priv->submenu = NULL;
  priv->toggle_size = 0;
  priv->accelerator_width = 0;
946
  if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
947
    priv->submenu_direction = GTK_DIRECTION_LEFT;
948
  else
949 950 951 952 953 954
    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;
955 956 957 958 959 960 961 962

  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
963 964
}

965 966 967 968 969 970
GtkCssGadget *
_gtk_menu_item_get_gadget (GtkMenuItem *menu_item)
{
  return menu_item->priv->gadget;
}

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

984 985 986 987 988 989 990 991
/**
 * 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
992 993 994
GtkWidget*
gtk_menu_item_new_with_label (const gchar *label)
{
995 996 997
  return g_object_new (GTK_TYPE_MENU_ITEM,
                       "label", label,
                       NULL);
Elliot Lee's avatar
Elliot Lee committed
998 999
}

1000 1001 1002 1003

/**
 * gtk_menu_item_new_with_mnemonic:
 * @label: The text of the button, with an underscore in front of the
1004
 *     mnemonic character
1005
 *
1006 1007 1008 1009 1010 1011 1012
 * 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
 */
1013 1014 1015
GtkWidget*
gtk_menu_item_new_with_mnemonic (const gchar *label)
{
1016 1017 1018 1019
  return g_object_new (GTK_TYPE_MENU_ITEM,
                       "use-underline", TRUE,
                       "label", label,
                       NULL);
1020 1021
}

1022 1023 1024 1025
static void
gtk_menu_item_dispose (GObject *object)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
1026
  GtkMenuItemPrivate *priv = menu_item->priv;
1027

1028 1029
  g_clear_object (&priv->action_helper);

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

1039
  g_clear_object (&priv->arrow_gadget);
1040 1041
  g_clear_object (&priv->gadget);

1042 1043 1044
  G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
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));
1057
      g_object_notify_by_pspec (G_OBJECT (menu_item), menu_item_props[PROP_RIGHT_JUSTIFIED]);
1058 1059 1060
    }
}

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

1069 1070
  switch (prop_id)
    {
Tim Janik's avatar
Tim Janik committed
1071
    case PROP_RIGHT_JUSTIFIED:
1072
      gtk_menu_item_do_set_right_justified (menu_item, g_value_get_boolean (value));
Tim Janik's avatar
Tim Janik committed
1073
      break;
1074 1075 1076
    case PROP_SUBMENU:
      gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
      break;
Tim Janik's avatar
Tim Janik committed
1077 1078 1079
    case PROP_ACCEL_PATH:
      gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
      break;
1080 1081 1082 1083 1084 1085
    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;
1086 1087 1088 1089 1090 1091
    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;
1092 1093 1094 1095 1096 1097
    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;
1098 1099 1100 1101 1102
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
Elliot Lee's avatar
Elliot Lee committed
1103

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

1113 1114
  switch (prop_id)
    {
Tim Janik's avatar
Tim Janik committed
1115
    case PROP_RIGHT_JUSTIFIED:
1116
      g_value_set_boolean (value, priv->right_justify);
Tim Janik's avatar
Tim Janik committed
1117
      break;
1118 1119 1120
    case PROP_SUBMENU:
      g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
      break;
Tim Janik's avatar
Tim Janik committed
1121 1122 1123
    case PROP_ACCEL_PATH:
      g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
      break;
1124 1125 1126 1127 1128 1129
    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;
1130 1131 1132 1133 1134 1135
    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;
1136 1137
    case PROP_ACTION_NAME:
      g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
1138 1139 1140 1141 1142 1143 1144 1145
      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;
    }
1146 1147
}

1148
static void
1149
gtk_menu_item_destroy (GtkWidget *widget)
1150
{
1151 1152
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  GtkMenuItemPrivate *priv = menu_item->priv;
1153

1154 1155
  if (priv->submenu)
    gtk_widget_destroy (priv->submenu);
1156

1157 1158
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->destroy (widget);
}
1159

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

1167
  g_return_if_fail (priv->submenu == (GtkWidget*) menu);
1168

1169
  priv->submenu = NULL;
1170
  g_clear_object (&priv->arrow_gadget);
1171 1172
}

1173 1174 1175 1176 1177
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;
1178
  iface->custom_finished = gtk_menu_item_buildable_custom_finished;
1179 1180
}

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

1194

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

  if (strcmp (tagname, "accelerator") == 0)
    {
1206
      GtkMenuShell *menu_shell;
1207 1208
      GtkWidget *attach;

1209
      menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1210
      if (menu_shell)
1211 1212 1213 1214 1215 1216 1217
        {
          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));
        }
1218
      else
1219 1220 1221
        {
          /* Fall back to something ... */
          toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));
1222

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

      /* 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);
}


1235 1236 1237
static void
gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
{
1238 1239
  iface->update = gtk_menu_item_update;
  iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1240 1241
}

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

  child = gtk_bin_get_child (GTK_BIN (menu_item));
1248

1249 1250 1251 1252
  if (GTK_IS_LABEL (child))
    {
      const gchar *label;

1253
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1254
      label = gtk_action_get_label (action);
1255
      G_GNUC_END_IGNORE_DEPRECATIONS;
1256
      gtk_menu_item_set_label (menu_item, label);
1257 1258 1259
    }
}

1260 1261 1262 1263 1264
/*
 * gtk_menu_is_empty:
 * @menu: (allow-none): a #GtkMenu or %NULL
 * 
 * Determines whether @menu is empty. A menu is considered empty if it
1265
 * the only visible children are tearoff menu items or “filler” menu 
1266 1267 1268 1269
 * items which were inserted to mark the menu as empty.
 * 
 * This function is used by #GtkAction.
 *
1270
 * Returns: whether @menu is empty.
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
 **/
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))
	{
1290 1291 1292

G_GNUC_BEGIN_IGNORE_DEPRECATIONS

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

G_GNUC_END_IGNORE_DEPRECATIONS

1302 1303 1304 1305 1306 1307 1308 1309
	}
      cur = cur->next;
    }
  g_list_free (children);

  return result;
}

1310 1311

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

  if (strcmp (property_name, "visible") == 0)
1320 1321 1322 1323 1324 1325
    {
      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;
    }
1326
  else if (strcmp (property_name, "sensitive") == 0)
1327 1328 1329 1330 1331
    {
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
1332 1333 1334
  else if (priv->use_action_appearance)
    {
      if (strcmp (property_name, "label") == 0)
1335
        activatable_update_label (menu_item, action);
1336 1337 1338 1339
    }
}

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

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

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

1355 1356 1357
  if (!action)
    return;

1358
  _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1359
                                 gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
1360

1361
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1362
  gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
1363
  G_GNUC_END_IGNORE_DEPRECATIONS;
1364 1365 1366

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

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

1382
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1383
      if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1384 1385 1386 1387 1388
        {
          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));
        }
1389
      G_GNUC_END_IGNORE_DEPRECATIONS;
1390

1391 1392 1393 1394 1395
      activatable_update_label (menu_item, action);
    }
}

static void
1396
gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1397
                                  GtkAction   *action)
1398
{
1399
    GtkMenuItemPrivate *priv = menu_item->priv;
1400 1401 1402 1403

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

1404 1405
    G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

1406 1407
    if (priv->action)
      {
1408
        gtk_action_disconnect_accelerator (priv->action);
1409 1410 1411 1412
      }

    if (action)
      {
1413 1414 1415 1416 1417 1418 1419 1420
        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);
          }
1421 1422 1423 1424
      }

    gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);

1425 1426
    G_GNUC_END_IGNORE_DEPRECATIONS;

1427 1428 1429 1430
    priv->action = action;
}

static void
1431
gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1432
                                         gboolean     use_appearance)
1433
{
1434
    GtkMenuItemPrivate *priv = menu_item->priv;
1435 1436 1437

    if (priv->use_action_appearance != use_appearance)
      {
1438
        priv->use_action_appearance = use_appearance;
1439

1440
        G_GNUC_BEGIN_IGNORE_DEPRECATIONS;