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

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

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

Tim Janik's avatar
Tim Janik committed
30
#include "gtkaccellabel.h"
Elliot Lee's avatar
Elliot Lee committed
31
#include "gtkmain.h"
32
#include "gtkmarshalers.h"
Elliot Lee's avatar
Elliot Lee committed
33
#include "gtkmenu.h"
34
#include "gtkmenubar.h"
35
#include "gtkmenuprivate.h"
36
#include "gtkseparatormenuitem.h"
37
#include "gtkprivate.h"
38
#include "gtkbuildable.h"
39
#include "gtkactivatable.h"
40
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
41

42 43 44 45 46 47

typedef struct {
  GtkAction *action;
  gboolean   use_action_appearance;
} GtkMenuItemPrivate;

Elliot Lee's avatar
Elliot Lee committed
48 49
enum {
  ACTIVATE,
50
  ACTIVATE_ITEM,
51 52
  TOGGLE_SIZE_REQUEST,
  TOGGLE_SIZE_ALLOCATE,
Matthias Clasen's avatar
Matthias Clasen committed
53 54
  SELECT,
  DESELECT,
Elliot Lee's avatar
Elliot Lee committed
55 56 57
  LAST_SIGNAL
};

58 59
enum {
  PROP_0,
Tim Janik's avatar
Tim Janik committed
60 61
  PROP_RIGHT_JUSTIFIED,
  PROP_SUBMENU,
62 63
  PROP_ACCEL_PATH,
  PROP_LABEL,
64 65 66 67 68
  PROP_USE_UNDERLINE,

  /* activatable properties */
  PROP_ACTIVATABLE_RELATED_ACTION,
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
69
};
Elliot Lee's avatar
Elliot Lee committed
70

71

72
static void gtk_menu_item_dispose        (GObject          *object);
73 74 75 76 77 78 79 80 81
static void gtk_menu_item_set_property   (GObject          *object,
					  guint             prop_id,
					  const GValue     *value,
					  GParamSpec       *pspec);
static void gtk_menu_item_get_property   (GObject          *object,
					  guint             prop_id,
					  GValue           *value,
					  GParamSpec       *pspec);
static void gtk_menu_item_destroy        (GtkObject        *object);
Elliot Lee's avatar
Elliot Lee committed
82 83
static void gtk_menu_item_size_allocate  (GtkWidget        *widget,
					  GtkAllocation    *allocation);
84 85 86 87
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
88 89 90 91
static gboolean gtk_menu_item_enter      (GtkWidget        *widget,
                                          GdkEventCrossing *event);
static gboolean gtk_menu_item_leave      (GtkWidget        *widget,
                                          GdkEventCrossing *event);
92 93
static gboolean gtk_menu_item_draw       (GtkWidget        *widget,
                                          cairo_t          *cr);
94 95
static void gtk_menu_item_parent_set     (GtkWidget        *widget,
					  GtkWidget        *previous_parent);
96

97

Matthias Clasen's avatar
Matthias Clasen committed
98 99
static void gtk_real_menu_item_select               (GtkMenuItem *item);
static void gtk_real_menu_item_deselect             (GtkMenuItem *item);
100
static void gtk_real_menu_item_activate             (GtkMenuItem *item);
101 102
static void gtk_real_menu_item_activate_item        (GtkMenuItem *item);
static void gtk_real_menu_item_toggle_size_request  (GtkMenuItem *menu_item,
103
						     gint        *requisition);
104
static void gtk_real_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
105
						     gint         allocation);
106
static gboolean gtk_menu_item_mnemonic_activate     (GtkWidget   *widget,
107
						     gboolean     group_cycling);
108

109
static void gtk_menu_item_ensure_label   (GtkMenuItem      *menu_item);
110
static gint gtk_menu_item_popup_timeout  (gpointer          data);
Elliot Lee's avatar
Elliot Lee committed
111 112 113
static void gtk_menu_item_position_menu  (GtkMenu          *menu,
					  gint             *x,
					  gint             *y,
114
					  gboolean         *push_in,
Elliot Lee's avatar
Elliot Lee committed
115
					  gpointer          user_data);
116 117
static void gtk_menu_item_show_all       (GtkWidget        *widget);
static void gtk_menu_item_hide_all       (GtkWidget        *widget);
118 119 120 121
static void gtk_menu_item_forall         (GtkContainer    *container,
					  gboolean         include_internals,
					  GtkCallback      callback,
					  gpointer         callback_data);
Owen Taylor's avatar
Owen Taylor committed
122 123
static gboolean gtk_menu_item_can_activate_accel (GtkWidget *widget,
						  guint      signal_id);
Elliot Lee's avatar
Elliot Lee committed
124

125 126 127 128
static void gtk_real_menu_item_set_label (GtkMenuItem     *menu_item,
					  const gchar     *label);
static G_CONST_RETURN gchar * gtk_real_menu_item_get_label (GtkMenuItem *menu_item);

129 130 131 132 133 134 135 136 137 138
static void gtk_menu_item_get_preferred_width            (GtkWidget           *widget,
                                                          gint                *minimum_size,
                                                          gint                *natural_size);
static void gtk_menu_item_get_preferred_height           (GtkWidget           *widget,
                                                          gint                *minimum_size,
                                                          gint                *natural_size);
static void gtk_menu_item_get_preferred_height_for_width (GtkWidget           *widget,
                                                          gint                 for_size,
                                                          gint                *minimum_size,
                                                          gint                *natural_size);
139

140 141 142 143 144
static void gtk_menu_item_buildable_interface_init (GtkBuildableIface   *iface);
static void gtk_menu_item_buildable_add_child      (GtkBuildable        *buildable,
						    GtkBuilder          *builder,
						    GObject             *child,
						    const gchar         *type);
145 146 147 148 149
static void gtk_menu_item_buildable_custom_finished(GtkBuildable        *buildable,
						    GtkBuilder          *builder,
						    GObject             *child,
						    const gchar         *tagname,
						    gpointer             user_data);
150

151
static void gtk_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
152
static void gtk_menu_item_update                     (GtkActivatable       *activatable,
153 154
						      GtkAction            *action,
						      const gchar          *property_name);
155
static void gtk_menu_item_sync_action_properties     (GtkActivatable       *activatable,
156 157 158 159 160 161 162
						      GtkAction            *action);
static void gtk_menu_item_set_related_action         (GtkMenuItem          *menu_item, 
						      GtkAction            *action);
static void gtk_menu_item_set_use_action_appearance  (GtkMenuItem          *menu_item, 
						      gboolean              use_appearance);


163
static guint menu_item_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
164

165 166
static GtkBuildableIface *parent_buildable_iface;

Matthias Clasen's avatar
Matthias Clasen committed
167
G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
168
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
169 170
						gtk_menu_item_buildable_interface_init)
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
171
						gtk_menu_item_activatable_interface_init))
172 173 174

#define GET_PRIVATE(object)  \
  (G_TYPE_INSTANCE_GET_PRIVATE ((object), GTK_TYPE_MENU_ITEM, GtkMenuItemPrivate))
Elliot Lee's avatar
Elliot Lee committed
175 176 177 178

static void
gtk_menu_item_class_init (GtkMenuItemClass *klass)
{
179 180 181 182
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
183

184
  gobject_class->dispose      = gtk_menu_item_dispose;
185 186
  gobject_class->set_property = gtk_menu_item_set_property;
  gobject_class->get_property = gtk_menu_item_get_property;
187 188 189 190

  object_class->destroy = gtk_menu_item_destroy;

  widget_class->size_allocate = gtk_menu_item_size_allocate;
191
  widget_class->draw = gtk_menu_item_draw;
192 193 194 195
  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
196 197
  widget_class->enter_notify_event = gtk_menu_item_enter;
  widget_class->leave_notify_event = gtk_menu_item_leave;
198 199
  widget_class->show_all = gtk_menu_item_show_all;
  widget_class->hide_all = gtk_menu_item_hide_all;
200
  widget_class->mnemonic_activate = gtk_menu_item_mnemonic_activate;
201
  widget_class->parent_set = gtk_menu_item_parent_set;
Owen Taylor's avatar
Owen Taylor committed
202
  widget_class->can_activate_accel = gtk_menu_item_can_activate_accel;
203 204 205
  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;
206
  
207 208
  container_class->forall = gtk_menu_item_forall;

209 210 211
  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;
212
  klass->toggle_size_allocate = gtk_real_menu_item_toggle_size_allocate;
213 214
  klass->set_label            = gtk_real_menu_item_set_label;
  klass->get_label            = gtk_real_menu_item_get_label;
Matthias Clasen's avatar
Matthias Clasen committed
215 216
  klass->select               = gtk_real_menu_item_select;
  klass->deselect             = gtk_real_menu_item_deselect;
217 218 219

  klass->hide_on_activate = TRUE;

Elliot Lee's avatar
Elliot Lee committed
220
  menu_item_signals[ACTIVATE] =
221
    g_signal_new (I_("activate"),
Manish Singh's avatar
Manish Singh committed
222 223 224 225 226 227
		  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);
228
  widget_class->activate_signal = menu_item_signals[ACTIVATE];
Elliot Lee's avatar
Elliot Lee committed
229

230
  menu_item_signals[ACTIVATE_ITEM] =
231
    g_signal_new (I_("activate-item"),
Manish Singh's avatar
Manish Singh committed
232 233 234 235 236 237
		  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);
238

239
  menu_item_signals[TOGGLE_SIZE_REQUEST] =
240
    g_signal_new (I_("toggle-size-request"),
Manish Singh's avatar
Manish Singh committed
241 242 243 244 245 246 247
		  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);
248 249

  menu_item_signals[TOGGLE_SIZE_ALLOCATE] =
250
    g_signal_new (I_("toggle-size-allocate"),
Manish Singh's avatar
Manish Singh committed
251 252
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_FIRST,
253
 		  G_STRUCT_OFFSET (GtkMenuItemClass, toggle_size_allocate),
Manish Singh's avatar
Manish Singh committed
254
		  NULL, NULL,
255
		  _gtk_marshal_VOID__INT,
Manish Singh's avatar
Manish Singh committed
256 257
		  G_TYPE_NONE, 1,
		  G_TYPE_INT);
258

Matthias Clasen's avatar
Matthias Clasen committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  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
277 278 279 280 281
  /**
   * GtkMenuItem:right-justified:
   *
   * Sets whether the menu item appears justified at the right side of a menu bar.
   *
282
   * Since: 2.14
Tim Janik's avatar
Tim Janik committed
283 284 285 286 287 288 289 290 291
   **/
  g_object_class_install_property (gobject_class,
                                   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));

292 293 294 295 296 297 298 299 300 301 302 303 304 305
  /**
   * GtkMenuItem:submenu:
   *
   * The submenu attached to the menu item, or NULL if it has none.
   *
   * Since: 2.12
   **/
  g_object_class_install_property (gobject_class,
                                   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));
306
  
307

Tim Janik's avatar
Tim Janik committed
308 309 310 311 312 313 314
  /**
   * 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.
   *
315
   * Since: 2.14
Tim Janik's avatar
Tim Janik committed
316 317 318 319 320 321 322 323 324
   **/
  g_object_class_install_property (gobject_class,
                                   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));

325 326 327 328 329 330 331 332 333 334 335 336
  /**
   * GtkMenuItem:label:
   *
   * The text for the child label.
   *
   * Since: 2.16
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
							P_("Label"),
							P_("The text for the child label"),
337
							"",
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
							GTK_PARAM_READWRITE));

  /**
   * GtkMenuItem:use-underline:
   *
   * %TRUE if underlines in the text indicate mnemonics  
   *
   * Since: 2.16
   **/
  g_object_class_install_property (gobject_class,
                                   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));

357 358 359
  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");

360
  gtk_widget_class_install_style_property_parser (widget_class,
361
						  g_param_spec_enum ("selected-shadow-type",
362 363 364
								     "Selected Shadow Type",
								     "Shadow type when item is selected",
								     GTK_TYPE_SHADOW_TYPE,
365
								     GTK_SHADOW_NONE,
366
								     GTK_PARAM_READABLE),
367
						  gtk_rc_property_parse_enum);
368 369

  gtk_widget_class_install_style_property (widget_class,
370
					   g_param_spec_int ("horizontal-padding",
371 372 373 374 375
							     "Horizontal Padding",
							     "Padding to left and right of the menu item",
							     0,
							     G_MAXINT,
							     3,
376
							     GTK_PARAM_READABLE));
377 378

  gtk_widget_class_install_style_property (widget_class,
379
					   g_param_spec_int ("toggle-spacing",
380 381 382 383 384
							     "Icon Spacing",
							     "Space between icon and label",
							     0,
							     G_MAXINT,
							     5,
385
							     GTK_PARAM_READABLE));
386 387

  gtk_widget_class_install_style_property (widget_class,
388
					   g_param_spec_int ("arrow-spacing",
389 390 391 392 393
							     "Arrow Spacing",
							     "Space between label and arrow",
							     0,
							     G_MAXINT,
							     10,
394
							     GTK_PARAM_READABLE));
395 396 397 398 399 400 401

  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,
                                                               GTK_PARAM_READABLE));
402 403 404 405 406 407 408 409 410 411 412 413 414 415

  /**
   * GtkMenuItem:width-chars:
   *
   * The minimum desired width of the menu item in characters.
   *
   * Since: 2.14
   **/
  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,
                                                             GTK_PARAM_READABLE));
416 417

  g_type_class_add_private (object_class, sizeof (GtkMenuItemPrivate));
Elliot Lee's avatar
Elliot Lee committed
418 419 420 421 422
}

static void
gtk_menu_item_init (GtkMenuItem *menu_item)
{
423 424
  GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);

425
  gtk_widget_set_has_window (GTK_WIDGET (menu_item), FALSE);
426 427 428

  priv->action = NULL;
  priv->use_action_appearance = TRUE;
429
  
Elliot Lee's avatar
Elliot Lee committed
430 431
  menu_item->submenu = NULL;
  menu_item->toggle_size = 0;
432
  menu_item->accelerator_width = 0;
Elliot Lee's avatar
Elliot Lee committed
433
  menu_item->show_submenu_indicator = FALSE;
434 435 436 437
  if (gtk_widget_get_direction (GTK_WIDGET (menu_item)) == GTK_TEXT_DIR_RTL)
    menu_item->submenu_direction = GTK_DIRECTION_LEFT;
  else
    menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
Elliot Lee's avatar
Elliot Lee committed
438 439 440 441 442 443 444
  menu_item->submenu_placement = GTK_TOP_BOTTOM;
  menu_item->right_justify = FALSE;

  menu_item->timer = 0;
}

GtkWidget*
445
gtk_menu_item_new (void)
Elliot Lee's avatar
Elliot Lee committed
446
{
Manish Singh's avatar
Manish Singh committed
447
  return g_object_new (GTK_TYPE_MENU_ITEM, NULL);
Elliot Lee's avatar
Elliot Lee committed
448 449 450 451 452
}

GtkWidget*
gtk_menu_item_new_with_label (const gchar *label)
{
453 454 455
  return g_object_new (GTK_TYPE_MENU_ITEM, 
		       "label", label,
		       NULL);
Elliot Lee's avatar
Elliot Lee committed
456 457
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471

/**
 * gtk_menu_item_new_with_mnemonic:
 * @label: The text of the button, with an underscore in front of the
 *         mnemonic character
 * @returns: a new #GtkMenuItem
 *
 * 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.
 **/
GtkWidget*
gtk_menu_item_new_with_mnemonic (const gchar *label)
{
472 473 474 475
  return g_object_new (GTK_TYPE_MENU_ITEM, 
		       "use-underline", TRUE,
		       "label", label,
		       NULL);
476 477
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
static void
gtk_menu_item_dispose (GObject *object)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
  GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);

  if (priv->action)
    {
      gtk_action_disconnect_accelerator (priv->action);
      gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), NULL);
      
      priv->action = NULL;
    }
  G_OBJECT_CLASS (gtk_menu_item_parent_class)->dispose (object);
}

494 495 496 497 498
static void 
gtk_menu_item_set_property (GObject      *object,
			    guint         prop_id,
			    const GValue *value,
			    GParamSpec   *pspec)
Elliot Lee's avatar
Elliot Lee committed
499
{
500 501 502 503
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
  
  switch (prop_id)
    {
Tim Janik's avatar
Tim Janik committed
504 505 506
    case PROP_RIGHT_JUSTIFIED:
      gtk_menu_item_set_right_justified (menu_item, g_value_get_boolean (value));
      break;
507 508 509
    case PROP_SUBMENU:
      gtk_menu_item_set_submenu (menu_item, g_value_get_object (value));
      break;
Tim Janik's avatar
Tim Janik committed
510 511 512
    case PROP_ACCEL_PATH:
      gtk_menu_item_set_accel_path (menu_item, g_value_get_string (value));
      break;
513 514 515 516 517 518
    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;
519 520 521 522 523 524
    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;
525 526 527 528 529
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
Elliot Lee's avatar
Elliot Lee committed
530

531 532 533 534 535 536 537
static void 
gtk_menu_item_get_property (GObject    *object,
			    guint       prop_id,
			    GValue     *value,
			    GParamSpec *pspec)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
538
  GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);
539 540 541
  
  switch (prop_id)
    {
Tim Janik's avatar
Tim Janik committed
542 543 544
    case PROP_RIGHT_JUSTIFIED:
      g_value_set_boolean (value, gtk_menu_item_get_right_justified (menu_item));
      break;
545 546 547
    case PROP_SUBMENU:
      g_value_set_object (value, gtk_menu_item_get_submenu (menu_item));
      break;
Tim Janik's avatar
Tim Janik committed
548 549 550
    case PROP_ACCEL_PATH:
      g_value_set_string (value, gtk_menu_item_get_accel_path (menu_item));
      break;
551 552 553 554 555 556
    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;
557 558 559 560 561 562
    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;
563 564 565 566
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
567 568
}

569
static void
570
gtk_menu_item_destroy (GtkObject *object)
571
{
572 573 574 575 576 577 578
  GtkMenuItem *menu_item = GTK_MENU_ITEM (object);

  if (menu_item->submenu)
    gtk_widget_destroy (menu_item->submenu);

  GTK_OBJECT_CLASS (gtk_menu_item_parent_class)->destroy (object);
}
579

580 581 582 583 584
static void
gtk_menu_item_detacher (GtkWidget *widget,
			GtkMenu   *menu)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
585 586 587 588 589 590

  g_return_if_fail (menu_item->submenu == (GtkWidget*) menu);

  menu_item->submenu = NULL;
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static void
get_arrow_size (GtkWidget *widget,
		GtkWidget *child,
		gint      *size)
{
  PangoContext     *context;
  PangoFontMetrics *metrics;
  gfloat            arrow_scaling;
	  
  g_assert (size);

  gtk_widget_style_get (widget,
			"arrow-scaling", &arrow_scaling,
			NULL);
 	  
  context = gtk_widget_get_pango_context (child);
  metrics = pango_context_get_metrics (context, 
608
                                       gtk_widget_get_style (child)->font_desc,
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
				       pango_context_get_language (context));
  
  *size = (PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) +
			 pango_font_metrics_get_descent (metrics)));
  
  pango_font_metrics_unref (metrics);
  
  *size = *size * arrow_scaling;
}


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 gint
get_minimum_width (GtkWidget *widget)
{
  PangoContext *context;
  PangoFontMetrics *metrics;
  gint width;
  gint width_chars;

  context = gtk_widget_get_pango_context (widget);
  metrics = pango_context_get_metrics (context,
649
                                       gtk_widget_get_style (widget)->font_desc,
650 651 652 653 654 655 656 657 658 659 660 661
				       pango_context_get_language (context));

  width = pango_font_metrics_get_approximate_char_width (metrics);

  pango_font_metrics_unref (metrics);

  gtk_widget_style_get (widget, "width-chars", &width_chars, NULL);

  return PANGO_PIXELS (width_chars * width);
}

static void 
662 663 664
gtk_menu_item_get_preferred_width (GtkWidget *request,
                                   gint      *minimum_size,
                                   gint      *natural_size)
665 666 667 668
{
  GtkMenuItem *menu_item;
  GtkBin *bin;
  GtkWidget *child, *widget = GTK_WIDGET (request);
669
  GtkWidget *parent;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
  guint accel_width;
  guint horizontal_padding;
  guint border_width;
  GtkPackDirection pack_dir;
  GtkPackDirection child_pack_dir;
  gint  min_width, nat_width;

  min_width = nat_width = 0;

  gtk_widget_style_get (widget,
 			"horizontal-padding", &horizontal_padding,
			NULL);
  
  bin = GTK_BIN (widget);
  menu_item = GTK_MENU_ITEM (widget);
685
  parent = gtk_widget_get_parent (widget);
686

687
  if (GTK_IS_MENU_BAR (parent))
688
    {
689 690
      pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
      child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
691 692 693 694 695 696 697 698 699
    }
  else
    {
      pack_dir = GTK_PACK_DIRECTION_LTR;
      child_pack_dir = GTK_PACK_DIRECTION_LTR;
    }

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

700
  min_width = (border_width + gtk_widget_get_style (widget)->xthickness) * 2;
701 702 703 704 705 706 707 708 709 710 711 712 713

  if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
      (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
    min_width += 2 * horizontal_padding;

  nat_width = min_width;

  child = gtk_bin_get_child (bin);

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

714
      gtk_widget_get_preferred_width (child, &child_min, &child_nat);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

      if (menu_item->submenu && menu_item->show_submenu_indicator)
	{
	  guint arrow_spacing;
	  gint  arrow_size;
	  
	  gtk_widget_style_get (widget,
				"arrow-spacing", &arrow_spacing,
				NULL);

	  get_arrow_size (widget, child, &arrow_size);

	  min_width += arrow_size;
	  min_width += arrow_spacing;

	  min_width = MAX (min_width, get_minimum_width (widget));

	  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);
  menu_item->accelerator_width = accel_width;

  if (minimum_size)
    *minimum_size = min_width;

  if (natural_size)
    *natural_size = nat_width;
}

static void 
756 757 758
gtk_menu_item_get_preferred_height (GtkWidget *request,
                                    gint      *minimum_size,
                                    gint      *natural_size)
759 760 761
{
  GtkMenuItem *menu_item;
  GtkBin *bin;
762
  GtkStyle *style;
763
  GtkWidget *child, *widget = GTK_WIDGET (request);
764
  GtkWidget *parent;
765 766 767 768 769 770 771 772 773
  guint accel_width;
  guint horizontal_padding;
  guint border_width;
  GtkPackDirection pack_dir;
  GtkPackDirection child_pack_dir;
  gint  min_height, nat_height;

  min_height = nat_height = 0;

774 775
  style = gtk_widget_get_style (widget);

776 777 778 779 780 781
  gtk_widget_style_get (widget,
 			"horizontal-padding", &horizontal_padding,
			NULL);
  
  bin = GTK_BIN (widget);
  menu_item = GTK_MENU_ITEM (widget);
782
  parent = gtk_widget_get_parent (widget);
783

784
  if (GTK_IS_MENU_BAR (parent))
785
    {
786 787
      pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
      child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
788 789 790 791 792 793 794 795
    }
  else
    {
      pack_dir = GTK_PACK_DIRECTION_LTR;
      child_pack_dir = GTK_PACK_DIRECTION_LTR;
    }

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
796
  min_height = (border_width + style->ythickness) * 2;
797 798 799 800 801 802 803 804 805 806 807 808 809

  if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
      (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
    min_height += 2 * horizontal_padding;

  nat_height = min_height;

  child = gtk_bin_get_child (bin);

  if (child != NULL && gtk_widget_get_visible (child))
    {
      gint child_min, child_nat;
      
810
      gtk_widget_get_preferred_height (child, &child_min, &child_nat);
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

      min_height += child_min;
      nat_height += child_nat;

      if (menu_item->submenu && menu_item->show_submenu_indicator)
	{
	  gint  arrow_size;

	  get_arrow_size (widget, child, &arrow_size);

	  min_height = MAX (min_height, arrow_size);
	  nat_height = MAX (nat_height, arrow_size);
	}
    }
  else /* separator item */
    {
      gboolean wide_separators;
      gint     separator_height;

      gtk_widget_style_get (widget,
                            "wide-separators",  &wide_separators,
                            "separator-height", &separator_height,
                            NULL);

      if (wide_separators)
836
        min_height += separator_height + style->ythickness;
837
      else
838
        min_height += style->ythickness * 2;
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856

      nat_height = min_height;
    }

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

  if (minimum_size)
    *minimum_size = min_height;

  if (natural_size)
    *natural_size = nat_height;
}

static void
857 858 859 860
gtk_menu_item_get_preferred_height_for_width (GtkWidget *request,
                                              gint       for_size,
                                              gint      *minimum_size,
                                              gint      *natural_size)
861 862 863
{
  GtkMenuItem *menu_item;
  GtkBin *bin;
864
  GtkStyle *style;
865
  GtkWidget *child, *widget = GTK_WIDGET (request);
866
  GtkWidget *parent;
867 868 869 870 871 872 873 874 875
  guint horizontal_padding;
  guint border_width;
  GtkPackDirection pack_dir;
  GtkPackDirection child_pack_dir;
  gint  min_height, nat_height;
  gint  avail_size;

  min_height = nat_height = 0;

876 877
  style = gtk_widget_get_style (widget);

878 879 880 881 882 883
  gtk_widget_style_get (widget,
 			"horizontal-padding", &horizontal_padding,
			NULL);
  
  bin = GTK_BIN (widget);
  menu_item = GTK_MENU_ITEM (widget);
884
  parent = gtk_widget_get_parent (widget);
885

886
  if (GTK_IS_MENU_BAR (parent))
887
    {
888 889
      pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
      child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
890 891 892 893 894 895 896 897
    }
  else
    {
      pack_dir = GTK_PACK_DIRECTION_LTR;
      child_pack_dir = GTK_PACK_DIRECTION_LTR;
    }

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
898
  min_height   = (border_width + style->ythickness) * 2;
899 900

  avail_size   = for_size;
901
  avail_size  -= (border_width + style->xthickness) * 2;
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934

  if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
      (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
    min_height += 2 * horizontal_padding;

  if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
      (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
    avail_size -= 2 * horizontal_padding;

  nat_height = min_height;

  child = gtk_bin_get_child (bin);

  if (child != NULL && gtk_widget_get_visible (child))
    {
      gint child_min, child_nat;
      gint arrow_size = 0;
      
      if (menu_item->submenu && menu_item->show_submenu_indicator)
	{

	  guint arrow_spacing;
	  
	  gtk_widget_style_get (widget,
				"arrow-spacing", &arrow_spacing,
				NULL);

	  get_arrow_size (widget, child, &arrow_size);

	  avail_size -= arrow_size;
	  avail_size -= arrow_spacing;
	}

935 936 937 938
      gtk_widget_get_preferred_height_for_width (child,
                                                 avail_size,
                                                 &child_min,
                                                 &child_nat);
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

      min_height += child_min;
      nat_height += child_nat;

      if (menu_item->submenu && menu_item->show_submenu_indicator)
	{
	  min_height = MAX (min_height, arrow_size);
	  nat_height = MAX (nat_height, arrow_size);
	}
    }
  else /* separator item */
    {
      gboolean wide_separators;
      gint     separator_height;

      gtk_widget_style_get (widget,
                            "wide-separators",  &wide_separators,
                            "separator-height", &separator_height,
                            NULL);

      if (wide_separators)
960
        min_height += separator_height + style->ythickness;
961
      else
962
        min_height += style->ythickness * 2;
963 964 965 966 967 968 969 970 971 972 973

      nat_height = min_height;
    }

  if (minimum_size)
    *minimum_size = min_height;

  if (natural_size)
    *natural_size = nat_height;
}

974 975 976 977 978
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;
979
  iface->custom_finished = gtk_menu_item_buildable_custom_finished;
980 981
}

Matthias Clasen's avatar
Matthias Clasen committed
982
static void
983 984 985 986 987 988 989 990 991 992 993 994
gtk_menu_item_buildable_add_child (GtkBuildable *buildable,
				   GtkBuilder   *builder,
				   GObject      *child,
				   const gchar  *type)
{
  if (type && strcmp (type, "submenu") == 0)
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (buildable),
				   GTK_WIDGET (child));
  else
    parent_buildable_iface->add_child (buildable, builder, child, type);
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

static void 
gtk_menu_item_buildable_custom_finished (GtkBuildable        *buildable,
					 GtkBuilder          *builder,
					 GObject             *child,
					 const gchar         *tagname,
					 gpointer             user_data)
{
  GtkWidget *toplevel;

  if (strcmp (tagname, "accelerator") == 0)
    {
1007
      GtkMenuShell *menu_shell;
1008 1009
      GtkWidget *attach;

1010
      menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (GTK_WIDGET (buildable)));
1011 1012 1013 1014
      if (menu_shell)
	{
	  while (GTK_IS_MENU (menu_shell) &&
		 (attach = gtk_menu_get_attach_widget (GTK_MENU (menu_shell))) != NULL)
1015
	    menu_shell = GTK_MENU_SHELL (gtk_widget_get_parent (attach));
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	  
	  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menu_shell));
	}
      else
	{
	  /* Fall back to something ... */
	  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (buildable));

	  g_warning ("found a GtkMenuItem '%s' without a parent GtkMenuShell, assigned accelerators wont work.",
		     gtk_buildable_get_name (buildable));
	}

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


1036 1037 1038
static void
gtk_menu_item_activatable_interface_init (GtkActivatableIface *iface)
{
1039 1040
  iface->update = gtk_menu_item_update;
  iface->sync_action_properties = gtk_menu_item_sync_action_properties;
1041 1042
}

1043
static void
1044 1045
activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
{
Javier Jardon's avatar
Javier Jardon committed
1046 1047 1048
  GtkWidget *child;

  child = gtk_bin_get_child (GTK_BIN (menu_item));
1049

1050 1051 1052 1053 1054
  if (GTK_IS_LABEL (child))
    {
      const gchar *label;

      label = gtk_action_get_label (action);
1055
      gtk_menu_item_set_label (menu_item, label);
1056 1057 1058 1059 1060 1061
    }
}

gboolean _gtk_menu_is_empty (GtkWidget *menu);

static void
1062 1063 1064
gtk_menu_item_update (GtkActivatable *activatable,
		      GtkAction      *action,
		      const gchar    *property_name)
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
  GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);

  if (strcmp (property_name, "visible") == 0)
    _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item), 
				   _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
  else if (strcmp (property_name, "sensitive") == 0)
    gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
  else if (priv->use_action_appearance)
    {
      if (strcmp (property_name, "label") == 0)
	activatable_update_label (menu_item, action);
    }
}

static void
1082 1083
gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
				      GtkAction      *action)
1084 1085 1086
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (activatable);
  GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);
Javier Jardon's avatar
Javier Jardon committed
1087
  GtkWidget *label;
1088

1089 1090
  if (!priv->use_action_appearance || !action)
    {
Javier Jardon's avatar
Javier Jardon committed
1091
      label = gtk_bin_get_child (GTK_BIN (menu_item));
1092 1093 1094 1095 1096

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

1097 1098 1099
  if (!action)
    return;

1100
  _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
1101 1102 1103 1104 1105 1106
				 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));

  gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));

  if (priv->use_action_appearance)
    {
Javier Jardon's avatar
Javier Jardon committed
1107
      label = gtk_bin_get_child (GTK_BIN (menu_item));
1108

1109 1110 1111 1112 1113 1114
      /* make sure label is a label */
      if (label && !GTK_IS_LABEL (label))
	{
	  gtk_container_remove (GTK_CONTAINER (menu_item), label);
	  label = NULL;
	}
1115

1116 1117 1118
      gtk_menu_item_ensure_label (menu_item);
      gtk_menu_item_set_use_underline (menu_item, TRUE);

1119
      if (GTK_IS_ACCEL_LABEL (label) && gtk_action_get_accel_path (action))
1120 1121 1122 1123 1124
        {
          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));
        }
1125

1126 1127 1128 1129 1130
      activatable_update_label (menu_item, action);
    }
}

static void
1131
gtk_menu_item_set_related_action (GtkMenuItem *menu_item,
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
				  GtkAction   *action)
{
    GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);

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

    if (priv->action)
      {
	gtk_action_disconnect_accelerator (priv->action);
      }

    if (action)
      {
	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);
	  }
      }

    gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (menu_item), action);

    priv->action = action;
}

static void
1162
gtk_menu_item_set_use_action_appearance (GtkMenuItem *menu_item,
1163 1164 1165 1166 1167 1168 1169
					 gboolean     use_appearance)
{
    GtkMenuItemPrivate *priv = GET_PRIVATE (menu_item);

    if (priv->use_action_appearance != use_appearance)
      {
	priv->use_action_appearance = use_appearance;
1170 1171

	gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (menu_item), priv->action);
1172 1173 1174 1175
      }
}


1176 1177 1178
/**
 * gtk_menu_item_set_submenu:
 * @menu_item: a #GtkMenuItem
1179
 * @submenu: (allow-none): the submenu, or %NULL
1180 1181 1182 1183
 *
 * Sets or replaces the menu item's submenu, or removes it when a %NULL
 * submenu is passed.
 **/
Elliot Lee's avatar
Elliot Lee committed
1184 1185 1186 1187 1188
void
gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
			   GtkWidget   *submenu)
{
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1189
  g_return_if_fail (submenu == NULL || GTK_IS_MENU (submenu));
1190
  
1191
  if (menu_item->submenu != submenu)
Elliot Lee's avatar
Elliot Lee committed
1192
    {
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
      if (menu_item->submenu)
	gtk_menu_detach (GTK_MENU (menu_item->submenu));

      if (submenu)
	{
	  menu_item->submenu = submenu;
	  gtk_menu_attach_to_widget (GTK_MENU (submenu),
				     GTK_WIDGET (menu_item),
				     gtk_menu_item_detacher);
	}

1204
      if (gtk_widget_get_parent (GTK_WIDGET (menu_item)))
1205
	gtk_widget_queue_resize (GTK_WIDGET (menu_item));
1206 1207

      g_object_notify (G_OBJECT (menu_item), "submenu");
Elliot Lee's avatar
Elliot Lee committed
1208 1209 1210
    }
}

1211 1212 1213 1214
/**
 * gtk_menu_item_get_submenu:
 * @menu_item: a #GtkMenuItem
 *
1215 1216
 * Gets the submenu underneath this menu item, if any.
 * See gtk_menu_item_set_submenu().
1217
 *
1218
 * Return value: (transfer none): submenu for this menu item, or %NULL if none.
1219 1220 1221 1222 1223 1224 1225 1226 1227
 **/
GtkWidget *
gtk_menu_item_get_submenu (GtkMenuItem *menu_item)
{
  g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL);

  return menu_item->submenu;
}

1228 1229 1230
void _gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
				   GtkSubmenuPlacement  placement);

Elliot Lee's avatar
Elliot Lee committed
1231
void
Owen Taylor's avatar
Owen Taylor committed
1232
_gtk_menu_item_set_placement (GtkMenuItem         *menu_item,
Elliot Lee's avatar
Elliot Lee committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
			     GtkSubmenuPlacement  placement)
{
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

  menu_item->submenu_placement = placement;
}

void
gtk_menu_item_select (GtkMenuItem *menu_item)
{
1243 1244
  GtkWidget *parent;

1245
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1246

Matthias Clasen's avatar
Matthias Clasen committed
1247
  g_signal_emit (menu_item, menu_item_signals[SELECT], 0);
1248 1249 1250 1251

  /* Enable themeing of the parent menu item depending on whether
   * something is selected in its submenu
   */
1252 1253
  parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
  if (GTK_IS_MENU (parent))
1254
    {
1255
      GtkMenu *menu = GTK_MENU (parent);
1256 1257 1258 1259

      if (menu->parent_menu_item)
        gtk_widget_queue_draw (GTK_WIDGET (menu->parent_menu_item));
    }
Elliot Lee's avatar
Elliot Lee committed
1260 1261 1262 1263 1264
}

void
gtk_menu_item_deselect (GtkMenuItem *menu_item)
{
1265 1266
  GtkWidget *parent;

1267
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1268

Matthias Clasen's avatar
Matthias Clasen committed
1269
  g_signal_emit (menu_item, menu_item_signals[DESELECT], 0);
1270 1271 1272 1273

  /* Enable themeing of the parent menu item depending on whether
   * something is selected in its submenu
   */
1274 1275
  parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
  if (GTK_IS_MENU (parent))
1276
    {
1277
      GtkMenu *menu = GTK_MENU (parent);
1278 1279 1280 1281

      if (menu->parent_menu_item)
        gtk_widget_queue_draw (GTK_WIDGET (menu->parent_menu_item));
    }
Elliot Lee's avatar
Elliot Lee committed
1282 1283 1284 1285 1286
}

void
gtk_menu_item_activate (GtkMenuItem *menu_item)
{
1287
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));
1288

Manish Singh's avatar
Manish Singh committed
1289
  g_signal_emit (menu_item, menu_item_signals[ACTIVATE], 0);
Elliot Lee's avatar
Elliot Lee committed
1290 1291
}

1292 1293
void
gtk_menu_item_toggle_size_request (GtkMenuItem *menu_item,
1294
				   gint        *requisition)
1295 1296 1297
{
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

Manish Singh's avatar
Manish Singh committed
1298
  g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_REQUEST], 0, requisition);
1299 1300 1301 1302
}

void
gtk_menu_item_toggle_size_allocate (GtkMenuItem *menu_item,
1303
				    gint         allocation)
1304 1305 1306
{
  g_return_if_fail (GTK_IS_MENU_ITEM (menu_item));

Manish Singh's avatar
Manish Singh committed
1307
  g_signal_emit (menu_item, menu_item_signals[TOGGLE_SIZE_ALLOCATE], 0, allocation);
1308 1309
}

Elliot Lee's avatar
Elliot Lee committed
1310 1311 1312 1313
static void
gtk_menu_item_size_allocate (GtkWidget     *widget,
			     GtkAllocation *allocation)
{
Tim Janik's avatar
Tim Janik committed
1314
  GtkMenuItem *menu_item;
Elliot Lee's avatar
Elliot Lee committed
1315 1316
  GtkBin *bin;
  GtkAllocation child_allocation;
1317
  GtkTextDirection direction;
1318 1319
  GtkPackDirection pack_dir;
  GtkPackDirection child_pack_dir;
Javier Jardon's avatar
Javier Jardon committed
1320
  GtkWidget *child;
1321
  GtkWidget *parent;
Elliot Lee's avatar
Elliot Lee committed
1322 1323 1324 1325

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

Tim Janik's avatar
Tim Janik committed
1326
  menu_item = GTK_MENU_ITEM (widget);
Elliot Lee's avatar
Elliot Lee committed
1327
  bin = GTK_BIN (widget);
Tim Janik's avatar
Tim Janik committed
1328
  
1329 1330
  direction = gtk_widget_get_direction (widget);

1331 1332
  parent = gtk_widget_get_parent (widget);
  if (GTK_IS_MENU_BAR (parent))
1333
    {
1334 1335
      pack_dir = gtk_menu_bar_get_pack_direction (GTK_MENU_BAR (parent));
      child_pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
1336 1337 1338 1339 1340 1341
    }
  else
    {
      pack_dir = GTK_PACK_DIRECTION_LTR;
      child_pack_dir = GTK_PACK_DIRECTION_LTR;
    }
1342 1343

  gtk_widget_set_allocation (widget, allocation);
Elliot Lee's avatar
Elliot Lee committed
1344

Javier Jardon's avatar
Javier Jardon committed
1345 1346
  child = gtk_bin_get_child (bin);
  if (child)
Elliot Lee's avatar
Elliot Lee committed
1347
    {
1348
      GtkRequisition child_requisition;
1349
      GtkStyle *style;
1350
      guint horizontal_padding;
1351
      guint border_width;
1352

1353
      style = gtk_widget_get_style (widget);
1354
      gtk_widget_style_get (widget,
1355
			    "horizontal-padding", &horizontal_padding,
1356
			    NULL);
1357

1358
      border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1359 1360
      child_allocation.x = border_width + style->xthickness;
      child_allocation.y = border_width + style->ythickness;
1361 1362 1363 1364 1365 1366 1367

      if ((pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL) &&
	  (child_pack_dir == GTK_PACK_DIRECTION_LTR || child_pack_dir == GTK_PACK_DIRECTION_RTL))
	child_allocation.x += horizontal_padding;
      else if ((pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT) &&
	       (child_pack_dir == GTK_PACK_DIRECTION_TTB || child_pack_dir == GTK_PACK_DIRECTION_BTT))
	child_allocation.y += horizontal_padding;
1368

1369 1370
      child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
      child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

      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 += GTK_MENU_ITEM (widget)->toggle_size;
	  child_allocation.width -= GTK_MENU_ITEM (widget)->toggle_size;
	}
      else
	{
	  if ((direction == GTK_TEXT_DIR_LTR) == (child_pack_dir != GTK_PACK_DIRECTION_BTT))
	    child_allocation.y += GTK_MENU_ITEM (widget)->toggle_size;
	  child_allocation.height -= GTK_MENU_ITEM (widget)->toggle_size;
	}

1386 1387
      child_allocation.x += allocation->x;
      child_allocation.y += allocation->y;
1388

1389
      gtk_widget_get_preferred_size (child, &child_requisition, NULL);
1390 1391 1392 1393 1394 1395 1396
      if (menu_item->submenu && menu_item->show_submenu_indicator) 
	{
	  if (direction == GTK_TEXT_DIR_RTL)
	    child_allocation.x += child_requisition.height;
	  child_allocation.width -= child_requisition.height;
	}
      
1397 1398 1399
      if (child_allocation.width < 1)
	child_allocation.width = 1;

Javier Jardon's avatar
Javier Jardon committed
1400
      gtk_widget_size_allocate (child, &child_allocation);
Elliot Lee's avatar
Elliot Lee committed
1401 1402
    }

1403
  if (gtk_widget_get_realized (widget))
1404
    gdk_window_move_resize (menu_item->event_window,
Tim Janik's avatar
Tim Janik committed
1405 1406
                            allocation->x, allocation->y,
                            allocation->width, allocation->height);
Elliot Lee's avatar
Elliot Lee committed
1407

Tim Janik's avatar
Tim Janik committed
1408 1409
  if (menu_item->submenu)
    gtk_menu_reposition (GTK_MENU (menu_item->submenu));
Elliot Lee's avatar
Elliot Lee committed
1410 1411
}

1412 1413 1414
static void
gtk_menu_item_realize (GtkWidget *widget)
{
1415
  GtkAllocation allocation;
1416
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
1417
  GdkWindow *window;
1418 1419 1420
  GdkWindowAttr attributes;
  gint attributes_mask;

1421
  gtk_widget_set_realized (widget, TRUE);
1422

1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
  window = gtk_widget_get_parent_window (widget);
  gtk_widget_set_window (widget, window);
  g_object_ref (window);

  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.event_mask = (gtk_widget_get_events (widget) |
			   GDK_BUTTON_PRESS_MASK |
			   GDK_BUTTON_RELEASE_MASK |
			   GDK_ENTER_NOTIFY_MASK |
			   GDK_LEAVE_NOTIFY_MASK |
			   GDK_POINTER_MOTION_MASK);

  attributes_mask = GDK_WA_X | GDK_WA_Y;
1443 1444 1445

  menu_item->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
                                            &attributes, attributes_mask);
1446 1447
  gdk_window_set_user_data (menu_item->event_window, widget);

1448
  gtk_widget_style_attach (widget);
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
}

static void
gtk_menu_item_unrealize (GtkWidget *widget)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);

  gdk_window_set_user_data (menu_item->event_window, NULL);
  gdk_window_destroy (menu_item->event_window);
  menu_item->event_window = NULL;
1459

1460
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unrealize (widget);
1461 1462 1463 1464 1465 1466 1467
}

static void
gtk_menu_item_map (GtkWidget *widget)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
  
Matthias Clasen's avatar
Matthias Clasen committed
1468
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->map (widget);
1469 1470

  gdk_window_show (menu_item->event_window);
1471 1472 1473 1474 1475 1476 1477 1478 1479
}

static void
gtk_menu_item_unmap (GtkWidget *widget)
{
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
    
  gdk_window_hide (menu_item->event_window);

Matthias Clasen's avatar
Matthias Clasen committed
1480
  GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->unmap (widget);
1481 1482
}

Matthias Clasen's avatar
Matthias Clasen committed
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
static gboolean
gtk_menu_item_enter (GtkWidget        *widget,
                     GdkEventCrossing *event)
{
  g_return_val_if_fail (event != NULL, FALSE);

  return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent *) event);
}

static gboolean
gtk_menu_item_leave (GtkWidget        *widget,
                     GdkEventCrossing *event)
{
  g_return_val_if_fail (event != NULL, FALSE);

  return gtk_widget_event (gtk_widget_get_parent (widget), (GdkEvent*) event);
}

1501
static gboolean
1502 1503
gtk_menu_item_draw (GtkWidget *widget,
                    cairo_t   *cr)
Elliot Lee's avatar
Elliot Lee committed
1504
{
1505
  GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
Elliot Lee's avatar
Elliot Lee committed
1506
  GtkStateType state_type;
1507
  GtkShadowType shadow_type, selected_shadow_type;
1508
  GtkStyle *style;
Javier Jardon's avatar
Javier Jardon committed
1509
  GtkWidget *child;
1510
  GdkWindow *window;
1511
  gint x, y, w, h, width, height;
1512
  guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
Elliot Lee's avatar
Elliot Lee committed
1513

1514 1515 1516 1517 1518
  state_type = gtk_widget_get_state (widget);
  style = gtk_widget_get_style (widget);
  window = gtk_widget_get_window (widget);
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
Elliot Lee's avatar
Elliot Lee committed
1519

1520 1521 1522 1523
  x = border_width;
  y = border_width;
  w = width - border_width * 2;
  h = height - border_width * 2;
Javier Jardon's avatar