gtkimagemenuitem.c 32.8 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (C) 2001 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * 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"
28 29
#include "gtkimagemenuitem.h"
#include "gtkaccellabel.h"
30
#include "gtkintl.h"
31 32 33
#include "gtkstock.h"
#include "gtkiconfactory.h"
#include "gtkimage.h"
34
#include "gtkmenubar.h"
Matthias Clasen's avatar
Matthias Clasen committed
35 36
#include "gtkcontainer.h"
#include "gtkwindow.h"
37
#include "gtkactivatable.h"
38
#include "gtkprivate.h"
39

Matthias Clasen's avatar
Matthias Clasen committed
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

struct _GtkImageMenuItemPriv
{
  GtkWidget     *image;

  gchar         *label;
  guint          use_stock         : 1;
  guint          always_show_image : 1;
};

enum {
  PROP_0,
  PROP_IMAGE,
  PROP_USE_STOCK,
  PROP_ACCEL_GROUP,
  PROP_ALWAYS_SHOW_IMAGE
};

static GtkActivatableIface *parent_activatable_iface;

61
static void gtk_image_menu_item_destroy              (GtkObject        *object);
62 63 64 65
static void gtk_image_menu_item_size_request         (GtkWidget        *widget,
                                                      GtkRequisition   *requisition);
static void gtk_image_menu_item_size_allocate        (GtkWidget        *widget,
                                                      GtkAllocation    *allocation);
66
static void gtk_image_menu_item_map                  (GtkWidget        *widget);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static void gtk_image_menu_item_remove               (GtkContainer     *container,
                                                      GtkWidget        *child);
static void gtk_image_menu_item_toggle_size_request  (GtkMenuItem      *menu_item,
						      gint             *requisition);
static void gtk_image_menu_item_set_label            (GtkMenuItem      *menu_item,
						      const gchar      *label);
static G_CONST_RETURN gchar *gtk_image_menu_item_get_label (GtkMenuItem *menu_item);

static void gtk_image_menu_item_forall               (GtkContainer    *container,
						      gboolean	       include_internals,
						      GtkCallback      callback,
						      gpointer         callback_data);

static void gtk_image_menu_item_finalize             (GObject         *object);
static void gtk_image_menu_item_set_property         (GObject         *object,
						      guint            prop_id,
						      const GValue    *value,
						      GParamSpec      *pspec);
static void gtk_image_menu_item_get_property         (GObject         *object,
						      guint            prop_id,
						      GValue          *value,
						      GParamSpec      *pspec);
static void gtk_image_menu_item_screen_changed       (GtkWidget        *widget,
						      GdkScreen        *previous_screen);

static void gtk_image_menu_item_recalculate          (GtkImageMenuItem *image_menu_item);

94
static void gtk_image_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
95
static void gtk_image_menu_item_update                     (GtkActivatable       *activatable,
96 97
							    GtkAction            *action,
							    const gchar          *property_name);
98
static void gtk_image_menu_item_sync_action_properties     (GtkActivatable       *activatable,
99
							    GtkAction            *action);
100

101 102 103 104

G_DEFINE_TYPE_WITH_CODE (GtkImageMenuItem, gtk_image_menu_item, GTK_TYPE_MENU_ITEM,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
						gtk_image_menu_item_activatable_interface_init))
105

106

107 108 109
static void
gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
{
110 111 112 113 114 115 116 117
  GObjectClass *gobject_class = (GObjectClass*) klass;
  GtkObjectClass *object_class = (GtkObjectClass*) klass;
  GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
  GtkMenuItemClass *menu_item_class = (GtkMenuItemClass*) klass;
  GtkContainerClass *container_class = (GtkContainerClass*) klass;

  object_class->destroy = gtk_image_menu_item_destroy;

Matthias Clasen's avatar
Matthias Clasen committed
118
  widget_class->screen_changed = gtk_image_menu_item_screen_changed;
119 120
  widget_class->size_request = gtk_image_menu_item_size_request;
  widget_class->size_allocate = gtk_image_menu_item_size_allocate;
121
  widget_class->map = gtk_image_menu_item_map;
122 123 124 125 126

  container_class->forall = gtk_image_menu_item_forall;
  container_class->remove = gtk_image_menu_item_remove;
  
  menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
127 128
  menu_item_class->set_label           = gtk_image_menu_item_set_label;
  menu_item_class->get_label           = gtk_image_menu_item_get_label;
129

130
  gobject_class->finalize     = gtk_image_menu_item_finalize;
131 132 133 134 135 136
  gobject_class->set_property = gtk_image_menu_item_set_property;
  gobject_class->get_property = gtk_image_menu_item_get_property;
  
  g_object_class_install_property (gobject_class,
                                   PROP_IMAGE,
                                   g_param_spec_object ("image",
137 138
                                                        P_("Image widget"),
                                                        P_("Child widget to appear next to the menu text"),
139
                                                        GTK_TYPE_WIDGET,
140
                                                        GTK_PARAM_READWRITE));
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * GtkImageMenuItem:use-stock:
   *
   * If %TRUE, the label set in the menuitem is used as a
   * stock id to select the stock item for the item.
   *
   * Since: 2.16
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_USE_STOCK,
                                   g_param_spec_boolean ("use-stock",
							 P_("Use stock"),
							 P_("Whether to use the label text to create a stock menu item"),
							 FALSE,
							 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  /**
   * GtkImageMenuItem:always-show-image:
   *
   * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images 
   * setting and always show the image, if available.
   *
   * Use this property if the menuitem would be useless or hard to use
   * without the image. 
   *
   * Since: 2.16
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_ALWAYS_SHOW_IMAGE,
                                   g_param_spec_boolean ("always-show-image",
							 P_("Always show image"),
							 P_("Whether the image will always be shown"),
							 FALSE,
							 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));

176 177 178 179 180 181 182 183 184 185 186 187 188 189
  /**
   * GtkImageMenuItem:accel-group:
   *
   * The Accel Group to use for stock accelerator keys
   *
   * Since: 2.16
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_ACCEL_GROUP,
                                   g_param_spec_object ("accel-group",
							P_("Accel Group"),
							P_("The Accel Group to use for stock accelerator keys"),
							GTK_TYPE_ACCEL_GROUP,
							GTK_PARAM_WRITABLE));
Matthias Clasen's avatar
Matthias Clasen committed
190 191 192 193 194

  gtk_settings_install_property (g_param_spec_boolean ("gtk-menu-images",
						       P_("Show menu images"),
						       P_("Whether images should be shown in menus"),
						       TRUE,
195
						       GTK_PARAM_READWRITE));
196

197
  g_type_class_add_private (object_class, sizeof (GtkImageMenuItemPriv));
198 199 200 201 202
}

static void
gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
{
203
  GtkImageMenuItemPriv *priv;
204

205 206 207 208
  image_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (image_menu_item,
                                                       GTK_TYPE_IMAGE_MENU_ITEM,
                                                       GtkImageMenuItemPriv);
  priv = image_menu_item->priv;
209

210 211 212
  priv->image = NULL;
  priv->use_stock = FALSE;
  priv->label  = NULL;
213 214
}

215 216 217
static void 
gtk_image_menu_item_finalize (GObject *object)
{
218
  GtkImageMenuItemPriv *priv = GTK_IMAGE_MENU_ITEM (object)->priv;
219 220 221 222 223 224 225

  g_free (priv->label);
  priv->label  = NULL;

  G_OBJECT_CLASS (gtk_image_menu_item_parent_class)->finalize (object);
}

226
static void
227 228 229
gtk_image_menu_item_set_property (GObject         *object,
                                  guint            prop_id,
                                  const GValue    *value,
Tim Janik's avatar
Tim Janik committed
230
                                  GParamSpec      *pspec)
231
{
232 233 234 235 236
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_IMAGE:
237 238 239 240 241
      gtk_image_menu_item_set_image (image_menu_item, (GtkWidget *) g_value_get_object (value));
      break;
    case PROP_USE_STOCK:
      gtk_image_menu_item_set_use_stock (image_menu_item, g_value_get_boolean (value));
      break;
242 243 244
    case PROP_ALWAYS_SHOW_IMAGE:
      gtk_image_menu_item_set_always_show_image (image_menu_item, g_value_get_boolean (value));
      break;
245 246
    case PROP_ACCEL_GROUP:
      gtk_image_menu_item_set_accel_group (image_menu_item, g_value_get_object (value));
247 248 249 250 251 252
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
253

254 255 256 257
static void
gtk_image_menu_item_get_property (GObject         *object,
                                  guint            prop_id,
                                  GValue          *value,
Tim Janik's avatar
Tim Janik committed
258
                                  GParamSpec      *pspec)
259 260
{
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
261
  
262 263 264
  switch (prop_id)
    {
    case PROP_IMAGE:
265 266 267 268
      g_value_set_object (value, gtk_image_menu_item_get_image (image_menu_item));
      break;
    case PROP_USE_STOCK:
      g_value_set_boolean (value, gtk_image_menu_item_get_use_stock (image_menu_item));      
269
      break;
270 271 272
    case PROP_ALWAYS_SHOW_IMAGE:
      g_value_set_boolean (value, gtk_image_menu_item_get_always_show_image (image_menu_item));
      break;
273 274 275 276
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
277 278
}

Matthias Clasen's avatar
Matthias Clasen committed
279 280 281
static gboolean
show_image (GtkImageMenuItem *image_menu_item)
{
282
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
Matthias Clasen's avatar
Matthias Clasen committed
283 284 285
  GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
  gboolean show;

286 287 288 289
  if (priv->always_show_image)
    show = TRUE;
  else
    g_object_get (settings, "gtk-menu-images", &show, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
290 291 292

  return show;
}
293

294 295 296 297
static void
gtk_image_menu_item_map (GtkWidget *widget)
{
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
298
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
299 300 301

  GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->map (widget);

302 303
  if (priv->image)
    g_object_set (priv->image,
304 305 306 307
                  "visible", show_image (image_menu_item),
                  NULL);
}

308 309 310 311
static void
gtk_image_menu_item_destroy (GtkObject *object)
{
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
312
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
313

314
  if (priv->image)
315
    gtk_container_remove (GTK_CONTAINER (image_menu_item),
316
                          priv->image);
317 318 319 320

  GTK_OBJECT_CLASS (gtk_image_menu_item_parent_class)->destroy (object);
}

321 322 323 324
static void
gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
					 gint        *requisition)
{
325
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
326
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
327
  GtkPackDirection pack_dir;
328 329
  GtkWidget *parent;
  GtkWidget *widget = GTK_WIDGET (menu_item);
330

331 332 333 334
  parent = gtk_widget_get_parent (widget);

  if (GTK_IS_MENU_BAR (parent))
    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
335 336
  else
    pack_dir = GTK_PACK_DIRECTION_LTR;
337

338 339
  *requisition = 0;

340
  if (priv->image && gtk_widget_get_visible (priv->image))
341
    {
342
      GtkRequisition image_requisition;
343
      guint toggle_spacing;
344
      gtk_widget_get_child_requisition (priv->image,
345 346
                                        &image_requisition);

347 348 349 350 351
      gtk_widget_style_get (GTK_WIDGET (menu_item),
			    "toggle-spacing", &toggle_spacing,
			    NULL);
      
      if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
352
	{
353 354 355 356 357 358 359
	  if (image_requisition.width > 0)
	    *requisition = image_requisition.width + toggle_spacing;
	}
      else
	{
	  if (image_requisition.height > 0)
	    *requisition = image_requisition.height + toggle_spacing;
360
	}
361
    }
362 363
}

364 365 366
static void
gtk_image_menu_item_recalculate (GtkImageMenuItem *image_menu_item)
{
367
  GtkImageMenuItemPriv    *priv = image_menu_item->priv;
368 369 370 371 372 373 374
  GtkStockItem             stock_item;
  GtkWidget               *image;
  const gchar             *resolved_label = priv->label;

  if (priv->use_stock && priv->label)
    {

375
      if (!priv->image)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	{
	  image = gtk_image_new_from_stock (priv->label, GTK_ICON_SIZE_MENU);
	  gtk_image_menu_item_set_image (image_menu_item, image);
	}

      if (gtk_stock_lookup (priv->label, &stock_item))
	  resolved_label = stock_item.label;

	gtk_menu_item_set_use_underline (GTK_MENU_ITEM (image_menu_item), TRUE);
    }

  GTK_MENU_ITEM_CLASS
    (gtk_image_menu_item_parent_class)->set_label (GTK_MENU_ITEM (image_menu_item), resolved_label);

}

static void 
gtk_image_menu_item_set_label (GtkMenuItem      *menu_item,
			       const gchar      *label)
{
396
  GtkImageMenuItemPriv *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

  if (priv->label != label)
    {
      g_free (priv->label);
      priv->label = g_strdup (label);

      gtk_image_menu_item_recalculate (GTK_IMAGE_MENU_ITEM (menu_item));

      g_object_notify (G_OBJECT (menu_item), "label");

    }
}

static G_CONST_RETURN gchar *
gtk_image_menu_item_get_label (GtkMenuItem *menu_item)
{
413 414
  GtkImageMenuItemPriv *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;

415 416
  return priv->label;
}
417 418 419 420 421

static void
gtk_image_menu_item_size_request (GtkWidget      *widget,
                                  GtkRequisition *requisition)
{
422 423
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
424
  gint child_width = 0;
425
  gint child_height = 0;
426
  GtkPackDirection pack_dir;
427 428 429 430 431 432
  GtkWidget *parent;

  parent = gtk_widget_get_parent (widget);

  if (GTK_IS_MENU_BAR (parent))
    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
433 434 435
  else
    pack_dir = GTK_PACK_DIRECTION_LTR;

436
  if (priv->image && gtk_widget_get_visible (priv->image))
437 438
    {
      GtkRequisition child_requisition;
439 440

      gtk_widget_size_request (priv->image,
441 442
                               &child_requisition);

443
      child_width = child_requisition.width;
444 445
      child_height = child_requisition.height;
    }
446 447

  GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_request (widget, requisition);
448 449 450 451

  /* not done with height since that happens via the
   * toggle_size_request
   */
452 453 454 455 456
  if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
    requisition->height = MAX (requisition->height, child_height);
  else
    requisition->width = MAX (requisition->width, child_width);
    
457 458 459
  
  /* Note that GtkMenuShell always size requests before
   * toggle_size_request, so toggle_size_request will be able to use
460
   * priv->image->requisition
461 462 463 464 465 466 467
   */
}

static void
gtk_image_menu_item_size_allocate (GtkWidget     *widget,
                                   GtkAllocation *allocation)
{
468 469
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
470
  GtkAllocation widget_allocation;
471
  GtkPackDirection pack_dir;
472 473 474 475 476 477
  GtkWidget *parent;

  parent = gtk_widget_get_parent (widget);

  if (GTK_IS_MENU_BAR (parent))
    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
478 479
  else
    pack_dir = GTK_PACK_DIRECTION_LTR;
480 481

  GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_allocate (widget, allocation);
482

483
  if (priv->image && gtk_widget_get_visible (priv->image))
484
    {
485 486
      gint x, y, offset;
      GtkRequisition child_requisition;
487
      GtkAllocation child_allocation;
488 489 490
      guint horizontal_padding, toggle_spacing;

      gtk_widget_style_get (widget,
491 492
			    "horizontal-padding", &horizontal_padding,
			    "toggle-spacing", &toggle_spacing,
493
			    NULL);
494 495 496 497
      
      /* Man this is lame hardcoding action, but I can't
       * come up with a solution that's really better.
       */
498

499
      gtk_widget_get_child_requisition (priv->image,
500 501
                                        &child_requisition);

502 503
      gtk_widget_get_allocation (widget, &widget_allocation);

504 505
      if (pack_dir == GTK_PACK_DIRECTION_LTR ||
	  pack_dir == GTK_PACK_DIRECTION_RTL)
506
	{
507
	  offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
508 509
                   gtk_widget_get_style (widget)->xthickness;

510 511 512
	  if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
	      (pack_dir == GTK_PACK_DIRECTION_LTR))
	    x = offset + horizontal_padding +
513
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
514
	       toggle_spacing - child_requisition.width) / 2;
515
	  else
516
            x = widget_allocation.width - offset - horizontal_padding -
517 518 519
	      GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
	       toggle_spacing - child_requisition.width) / 2;
520 521

          y = (widget_allocation.height - child_requisition.height) / 2;
522 523 524
	}
      else
	{
525
	  offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
526 527
                   gtk_widget_get_style (widget)->ythickness;

528 529 530 531 532 533
	  if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
	      (pack_dir == GTK_PACK_DIRECTION_TTB))
	    y = offset + horizontal_padding +
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
	       toggle_spacing - child_requisition.height) / 2;
	  else
534
            y = widget_allocation.height - offset - horizontal_padding -
535 536
	      GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
537
	       toggle_spacing - child_requisition.height) / 2;
538

539
          x = (widget_allocation.width - child_requisition.width) / 2;
540 541
	}
      
542 543
      child_allocation.width = child_requisition.width;
      child_allocation.height = child_requisition.height;
544 545
      child_allocation.x = widget_allocation.x + MAX (x, 0);
      child_allocation.y = widget_allocation.y + MAX (y, 0);
546

547
      gtk_widget_size_allocate (priv->image, &child_allocation);
548 549 550 551 552 553 554 555 556
    }
}

static void
gtk_image_menu_item_forall (GtkContainer   *container,
                            gboolean	    include_internals,
                            GtkCallback     callback,
                            gpointer        callback_data)
{
557
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
558
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
559 560 561 562 563

  GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->forall (container,
                                                                  include_internals,
                                                                  callback,
                                                                  callback_data);
564

565 566
  if (include_internals && priv->image)
    (* callback) (priv->image, callback_data);
567 568
}

569 570 571 572 573

static void 
gtk_image_menu_item_activatable_interface_init (GtkActivatableIface  *iface)
{
  parent_activatable_iface = g_type_interface_peek_parent (iface);
574 575
  iface->update = gtk_image_menu_item_update;
  iface->sync_action_properties = gtk_image_menu_item_sync_action_properties;
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
}

static gboolean
activatable_update_stock_id (GtkImageMenuItem *image_menu_item, GtkAction *action)
{
  GtkWidget   *image;
  const gchar *stock_id  = gtk_action_get_stock_id (action);

  image = gtk_image_menu_item_get_image (image_menu_item);
	  
  if (GTK_IS_IMAGE (image) &&
      stock_id && gtk_icon_factory_lookup_default (stock_id))
    {
      gtk_image_set_from_stock (GTK_IMAGE (image), stock_id, GTK_ICON_SIZE_MENU);
      return TRUE;
    }

  return FALSE;
}

static gboolean
activatable_update_gicon (GtkImageMenuItem *image_menu_item, GtkAction *action)
{
  GtkWidget   *image;
  GIcon       *icon = gtk_action_get_gicon (action);
  const gchar *stock_id = gtk_action_get_stock_id (action);

  image = gtk_image_menu_item_get_image (image_menu_item);

  if (icon && GTK_IS_IMAGE (image) &&
      !(stock_id && gtk_icon_factory_lookup_default (stock_id)))
    {
      gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_MENU);
      return TRUE;
    }

  return FALSE;
}

static void
activatable_update_icon_name (GtkImageMenuItem *image_menu_item, GtkAction *action)
{
  GtkWidget   *image;
  const gchar *icon_name = gtk_action_get_icon_name (action);

  image = gtk_image_menu_item_get_image (image_menu_item);
	  
623
  if (GTK_IS_IMAGE (image) && 
624 625 626 627 628 629 630
      (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
       gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
    {
      gtk_image_set_from_icon_name (GTK_IMAGE (image), icon_name, GTK_ICON_SIZE_MENU);
    }
}

631 632 633 634
static void
gtk_image_menu_item_update (GtkActivatable *activatable,
			    GtkAction      *action,
			    const gchar    *property_name)
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
{
  GtkImageMenuItem *image_menu_item;
  gboolean   use_appearance;

  image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);

  parent_activatable_iface->update (activatable, action, property_name);

  use_appearance = gtk_activatable_get_use_action_appearance (activatable);
  if (!use_appearance)
    return;

  if (strcmp (property_name, "stock-id") == 0)
    activatable_update_stock_id (image_menu_item, action);
  else if (strcmp (property_name, "gicon") == 0)
    activatable_update_gicon (image_menu_item, action);
  else if (strcmp (property_name, "icon-name") == 0)
    activatable_update_icon_name (image_menu_item, action);
}

static void 
656 657
gtk_image_menu_item_sync_action_properties (GtkActivatable *activatable,
			                    GtkAction      *action)
658 659 660 661 662 663 664
{
  GtkImageMenuItem *image_menu_item;
  GtkWidget *image;
  gboolean   use_appearance;

  image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);

665
  parent_activatable_iface->sync_action_properties (activatable, action);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

  if (!action)
    return;

  use_appearance = gtk_activatable_get_use_action_appearance (activatable);
  if (!use_appearance)
    return;

  image = gtk_image_menu_item_get_image (image_menu_item);
  if (image && !GTK_IS_IMAGE (image))
    {
      gtk_image_menu_item_set_image (image_menu_item, NULL);
      image = NULL;
    }
  
  if (!image)
    {
      image = gtk_image_new ();
      gtk_widget_show (image);
      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (activatable),
				     image);
    }
  
  if (!activatable_update_stock_id (image_menu_item, action) &&
      !activatable_update_gicon (image_menu_item, action))
    activatable_update_icon_name (image_menu_item, action);

693 694
  gtk_image_menu_item_set_always_show_image (image_menu_item,
                                             gtk_action_get_always_show_image (action));
695 696 697
}


Matthias Clasen's avatar
Matthias Clasen committed
698 699 700 701 702 703
/**
 * gtk_image_menu_item_new:
 * @returns: a new #GtkImageMenuItem.
 *
 * Creates a new #GtkImageMenuItem with an empty label.
 **/
704
GtkWidget*
705 706 707 708 709
gtk_image_menu_item_new (void)
{
  return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
710 711 712 713 714 715 716
/**
 * gtk_image_menu_item_new_with_label:
 * @label: the text of the menu item.
 * @returns: a new #GtkImageMenuItem.
 *
 * Creates a new #GtkImageMenuItem containing a label. 
 **/
717 718
GtkWidget*
gtk_image_menu_item_new_with_label (const gchar *label)
719
{
720 721 722
  return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, 
		       "label", label,
		       NULL);
723 724
}

725 726 727

/**
 * gtk_image_menu_item_new_with_mnemonic:
Matthias Clasen's avatar
Matthias Clasen committed
728
 * @label: the text of the menu item, with an underscore in front of the
729 730 731 732 733 734 735 736 737 738
 *         mnemonic character
 * @returns: a new #GtkImageMenuItem
 *
 * Creates a new #GtkImageMenuItem 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_image_menu_item_new_with_mnemonic (const gchar *label)
{
739 740 741 742
  return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, 
		       "use-underline", TRUE,
		       "label", label,
		       NULL);
743 744
}

Matthias Clasen's avatar
Matthias Clasen committed
745 746 747
/**
 * gtk_image_menu_item_new_from_stock:
 * @stock_id: the name of the stock item.
748 749
 * @accel_group: (allow-none): the #GtkAccelGroup to add the menu items 
 *   accelerator to, or %NULL.
Matthias Clasen's avatar
Matthias Clasen committed
750 751 752 753 754
 * @returns: a new #GtkImageMenuItem.
 *
 * Creates a new #GtkImageMenuItem containing the image and text from a 
 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK 
 * and #GTK_STOCK_APPLY.
755
 *
Matthias Clasen's avatar
Matthias Clasen committed
756 757 758 759 760
 * If you want this menu item to have changeable accelerators, then pass in
 * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
 * appropriate path for the menu item, use gtk_stock_lookup() to look up the
 * standard accelerator for the stock item, and if one is found, call
 * gtk_accel_map_add_entry() to register it.
Matthias Clasen's avatar
Matthias Clasen committed
761
 **/
762 763 764 765
GtkWidget*
gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
				    GtkAccelGroup    *accel_group)
{
766 767 768 769 770 771
  return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, 
		       "label", stock_id,
		       "use-stock", TRUE,
		       "accel-group", accel_group,
		       NULL);
}
772

773 774 775 776 777 778 779 780 781 782 783 784 785 786
/**
 * gtk_image_menu_item_set_use_stock:
 * @image_menu_item: a #GtkImageMenuItem
 * @use_stock: %TRUE if the menuitem should use a stock item
 *
 * If %TRUE, the label set in the menuitem is used as a
 * stock id to select the stock item for the item.
 *
 * Since: 2.16
 */
void
gtk_image_menu_item_set_use_stock (GtkImageMenuItem *image_menu_item,
				   gboolean          use_stock)
{
787
  GtkImageMenuItemPriv *priv;
788 789

  g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
790

791
  priv = image_menu_item->priv;
792

793
  if (priv->use_stock != use_stock)
794
    {
795
      priv->use_stock = use_stock;
796

797 798 799 800 801 802 803 804 805 806 807 808 809 810
      gtk_image_menu_item_recalculate (image_menu_item);

      g_object_notify (G_OBJECT (image_menu_item), "use-stock");
    }
}

/**
 * gtk_image_menu_item_get_use_stock:
 * @image_menu_item: a #GtkImageMenuItem
 *
 * Checks whether the label set in the menuitem is used as a
 * stock id to select the stock item for the item.
 *
 * Returns: %TRUE if the label set in the menuitem is used as a
Matthias Clasen's avatar
Matthias Clasen committed
811
 *     stock id to select the stock item for the item
812 813 814 815 816 817 818 819
 *
 * Since: 2.16
 */
gboolean
gtk_image_menu_item_get_use_stock (GtkImageMenuItem *image_menu_item)
{
  g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);

820
  return image_menu_item->priv->use_stock;
821 822
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
/**
 * gtk_image_menu_item_set_always_show_image:
 * @image_menu_item: a #GtkImageMenuItem
 * @always_show: %TRUE if the menuitem should always show the image
 *
 * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images 
 * setting and always show the image, if available.
 *
 * Use this property if the menuitem would be useless or hard to use
 * without the image. 
 * 
 * Since: 2.16
 */
void
gtk_image_menu_item_set_always_show_image (GtkImageMenuItem *image_menu_item,
                                           gboolean          always_show)
{
840
  GtkImageMenuItemPriv *priv;
841 842 843

  g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));

844
  priv = image_menu_item->priv;
845 846 847 848 849

  if (priv->always_show_image != always_show)
    {
      priv->always_show_image = always_show;

850
      if (priv->image)
851 852
        {
          if (show_image (image_menu_item))
853
            gtk_widget_show (priv->image);
854
          else
855
            gtk_widget_hide (priv->image);
856 857 858 859 860 861 862 863 864 865
        }

      g_object_notify (G_OBJECT (image_menu_item), "always-show-image");
    }
}

/**
 * gtk_image_menu_item_get_always_show_image:
 * @image_menu_item: a #GtkImageMenuItem
 *
Matthias Clasen's avatar
Matthias Clasen committed
866
 * Returns whether the menu item will ignore the #GtkSettings:gtk-menu-images
867 868 869 870 871 872 873 874 875 876 877
 * setting and always show the image, if available.
 * 
 * Returns: %TRUE if the menu item will always show the image
 *
 * Since: 2.16
 */
gboolean
gtk_image_menu_item_get_always_show_image (GtkImageMenuItem *image_menu_item)
{
  g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);

878
  return image_menu_item->priv->always_show_image;
879 880
}

881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

/**
 * gtk_image_menu_item_set_accel_group:
 * @image_menu_item: a #GtkImageMenuItem
 * @accel_group: the #GtkAccelGroup
 *
 * Specifies an @accel_group to add the menu items accelerator to
 * (this only applies to stock items so a stock item must already
 * be set, make sure to call gtk_image_menu_item_set_use_stock()
 * and gtk_menu_item_set_label() with a valid stock item first).
 *
 * If you want this menu item to have changeable accelerators then
 * you shouldnt need this (see gtk_image_menu_item_new_from_stock()).
 *
 * Since: 2.16
 */
void
gtk_image_menu_item_set_accel_group (GtkImageMenuItem *image_menu_item, 
				     GtkAccelGroup    *accel_group)
{
901
  GtkImageMenuItemPriv    *priv;
902 903 904 905 906 907 908 909 910
  GtkStockItem             stock_item;

  /* Silent return for the constructor */
  if (!accel_group) 
    return;
  
  g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
  g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));

911
  priv = image_menu_item->priv;
912 913 914 915 916

  if (priv->use_stock && priv->label && gtk_stock_lookup (priv->label, &stock_item))
    if (stock_item.keyval)
      {
	gtk_widget_add_accelerator (GTK_WIDGET (image_menu_item),
917 918 919 920 921
				    "activate",
				    accel_group,
				    stock_item.keyval,
				    stock_item.modifier,
				    GTK_ACCEL_VISIBLE);
922 923 924
	
	g_object_notify (G_OBJECT (image_menu_item), "accel-group");
      }
925 926
}

Matthias Clasen's avatar
Matthias Clasen committed
927 928 929
/** 
 * gtk_image_menu_item_set_image:
 * @image_menu_item: a #GtkImageMenuItem.
930 931
 * @image: (allow-none): a widget to set as the image for the menu item.
 *
Matthias Clasen's avatar
Matthias Clasen committed
932
 * Sets the image of @image_menu_item to the given widget.
Matthias Clasen's avatar
Matthias Clasen committed
933 934
 * Note that it depends on the show-menu-images setting whether
 * the image will be displayed or not.
Matthias Clasen's avatar
Matthias Clasen committed
935
 **/ 
936
void
937 938
gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
                               GtkWidget        *image)
939
{
940 941
  GtkImageMenuItemPriv *priv;

942
  g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
943

944 945 946
  priv = image_menu_item->priv;

  if (image == priv->image)
947 948
    return;

949
  if (priv->image)
950
    gtk_container_remove (GTK_CONTAINER (image_menu_item),
951
                          priv->image);
952

953
  priv->image = image;
954 955 956 957 958

  if (image == NULL)
    return;

  gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
959
  g_object_set (image,
Matthias Clasen's avatar
Matthias Clasen committed
960
		"visible", show_image (image_menu_item),
961
		"no-show-all", TRUE,
Matthias Clasen's avatar
Matthias Clasen committed
962
		NULL);
963

964
  g_object_notify (G_OBJECT (image_menu_item), "image");
965 966
}

Matthias Clasen's avatar
Matthias Clasen committed
967 968 969 970 971 972 973 974
/**
 * gtk_image_menu_item_get_image:
 * @image_menu_item: a #GtkImageMenuItem.
 * @returns: the widget set as image of @image_menu_item.
 *
 * Gets the widget that is currently set as the image of @image_menu_item.
 * See gtk_image_menu_item_set_image().
 **/
975 976 977 978 979
GtkWidget*
gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
{
  g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);

980
  return image_menu_item->priv->image;
981 982
}

983
static void
984 985 986
gtk_image_menu_item_remove (GtkContainer *container,
                            GtkWidget    *child)
{
987 988
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
  GtkImageMenuItemPriv *priv = image_menu_item->priv;
989

990
  if (child == priv->image)
991 992 993
    {
      gboolean widget_was_visible;
      
994
      widget_was_visible = gtk_widget_get_visible (child);
995 996
      
      gtk_widget_unparent (child);
997 998
      priv->image = NULL;

999 1000
      if (widget_was_visible &&
          gtk_widget_get_visible (GTK_WIDGET (container)))
1001
        gtk_widget_queue_resize (GTK_WIDGET (container));
1002 1003

      g_object_notify (G_OBJECT (image_menu_item), "image");
1004 1005 1006
    }
  else
    {
1007
      GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->remove (container, child);
1008 1009 1010
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
1011 1012 1013
static void 
show_image_change_notify (GtkImageMenuItem *image_menu_item)
{
1014 1015 1016
  GtkImageMenuItemPriv *priv = image_menu_item->priv;

  if (priv->image)
Matthias Clasen's avatar
Matthias Clasen committed
1017 1018
    {
      if (show_image (image_menu_item))
1019
	gtk_widget_show (priv->image);
Matthias Clasen's avatar
Matthias Clasen committed
1020
      else
1021
	gtk_widget_hide (priv->image);
Matthias Clasen's avatar
Matthias Clasen committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    }
}

static void
traverse_container (GtkWidget *widget,
		    gpointer   data)
{
  if (GTK_IS_IMAGE_MENU_ITEM (widget))
    show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
  else if (GTK_IS_CONTAINER (widget))
    gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
}

static void
gtk_image_menu_item_setting_changed (GtkSettings *settings)
{
1038
  GList *list, *l;
Matthias Clasen's avatar
Matthias Clasen committed
1039 1040 1041

  list = gtk_window_list_toplevels ();

1042 1043 1044
  for (l = list; l; l = l->next)
    gtk_container_forall (GTK_CONTAINER (l->data), 
			  traverse_container, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1045 1046

  g_list_free (list);  
Matthias Clasen's avatar
Matthias Clasen committed
1047 1048 1049 1050 1051 1052 1053
}

static void
gtk_image_menu_item_screen_changed (GtkWidget *widget,
				    GdkScreen *previous_screen)
{
  GtkSettings *settings;
Matthias Clasen's avatar
Matthias Clasen committed
1054
  guint show_image_connection;
Matthias Clasen's avatar
Matthias Clasen committed
1055

Matthias Clasen's avatar
Matthias Clasen committed
1056
  if (!gtk_widget_has_screen (widget))
Matthias Clasen's avatar
Matthias Clasen committed
1057
    return;
1058

Matthias Clasen's avatar
Matthias Clasen committed
1059 1060 1061
  settings = gtk_widget_get_settings (widget);
  
  show_image_connection = 
1062 1063
    GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
					 "gtk-image-menu-item-connection"));
Matthias Clasen's avatar
Matthias Clasen committed
1064 1065 1066
  
  if (show_image_connection)
    return;
Matthias Clasen's avatar
Matthias Clasen committed
1067

Matthias Clasen's avatar
Matthias Clasen committed
1068 1069
  show_image_connection =
    g_signal_connect (settings, "notify::gtk-menu-images",
1070
		      G_CALLBACK (gtk_image_menu_item_setting_changed), NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1071
  g_object_set_data (G_OBJECT (settings), 
Matthias Clasen's avatar
Matthias Clasen committed
1072
		     I_("gtk-image-menu-item-connection"),
1073
		     GUINT_TO_POINTER (show_image_connection));
Matthias Clasen's avatar
Matthias Clasen committed
1074 1075 1076

  show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
}