gtkimagemenuitem.c 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* GTK - The GIMP Toolkit
 * 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
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

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"
35 36
#include "gtkcontainer.h"
#include "gtkwindow.h"
37
#include "gtkprivate.h"
38
#include "gtkalias.h"
Matthias Clasen's avatar
Matthias Clasen committed
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static void gtk_image_menu_item_class_init           (GtkImageMenuItemClass *klass);
static void gtk_image_menu_item_init                 (GtkImageMenuItem      *image_menu_item);
static void gtk_image_menu_item_size_request         (GtkWidget        *widget,
                                                      GtkRequisition   *requisition);
static void gtk_image_menu_item_size_allocate        (GtkWidget        *widget,
                                                      GtkAllocation    *allocation);
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_forall     (GtkContainer   *container,
                                            gboolean	    include_internals,
                                            GtkCallback     callback,
                                            gpointer        callback_data);

56 57 58
static void gtk_image_menu_item_set_property (GObject         *object,
                                              guint            prop_id,
                                              const GValue    *value,
Tim Janik's avatar
Tim Janik committed
59
                                              GParamSpec      *pspec);
60 61 62
static void gtk_image_menu_item_get_property (GObject         *object,
                                              guint            prop_id,
                                              GValue          *value,
Tim Janik's avatar
Tim Janik committed
63
                                              GParamSpec      *pspec);
Matthias Clasen's avatar
Matthias Clasen committed
64 65
static void gtk_image_menu_item_screen_changed (GtkWidget        *widget,
						GdkScreen        *previous_screen);
66 67 68 69 70 71 72


enum {
  PROP_ZERO,
  PROP_IMAGE
};

73 74
static GtkMenuItemClass *parent_class = NULL;

Manish Singh's avatar
Manish Singh committed
75
GType
76 77
gtk_image_menu_item_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
78
  static GType image_menu_item_type = 0;
79 80 81

  if (!image_menu_item_type)
    {
Manish Singh's avatar
Manish Singh committed
82
      static const GTypeInfo image_menu_item_info =
83 84
      {
        sizeof (GtkImageMenuItemClass),
Manish Singh's avatar
Manish Singh committed
85 86 87 88 89 90 91 92
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_image_menu_item_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkImageMenuItem),
	0,		/* n_preallocs */
        (GInstanceInitFunc) gtk_image_menu_item_init,
93 94
      };

Manish Singh's avatar
Manish Singh committed
95
      image_menu_item_type =
96
	g_type_register_static (GTK_TYPE_MENU_ITEM, I_("GtkImageMenuItem"),
Manish Singh's avatar
Manish Singh committed
97
				&image_menu_item_info, 0);
98 99 100 101 102 103 104 105
    }

  return image_menu_item_type;
}

static void
gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
{
106
  GObjectClass *gobject_class;
107 108 109
  GtkWidgetClass *widget_class;
  GtkMenuItemClass *menu_item_class;
  GtkContainerClass *container_class;
110 111

  gobject_class = (GObjectClass*) klass;
112 113 114 115
  widget_class = (GtkWidgetClass*) klass;
  menu_item_class = (GtkMenuItemClass*) klass;
  container_class = (GtkContainerClass*) klass;
  
Manish Singh's avatar
Manish Singh committed
116
  parent_class = g_type_class_peek_parent (klass);
117
  
Matthias Clasen's avatar
Matthias Clasen committed
118
  widget_class->screen_changed = gtk_image_menu_item_screen_changed;
119 120 121 122 123 124 125
  widget_class->size_request = gtk_image_menu_item_size_request;
  widget_class->size_allocate = gtk_image_menu_item_size_allocate;

  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;
126 127 128 129 130 131 132

  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",
133 134
                                                        P_("Image widget"),
                                                        P_("Child widget to appear next to the menu text"),
135
                                                        GTK_TYPE_WIDGET,
136
                                                        GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
137 138 139 140 141

  gtk_settings_install_property (g_param_spec_boolean ("gtk-menu-images",
						       P_("Show menu images"),
						       P_("Whether images should be shown in menus"),
						       TRUE,
142
						       GTK_PARAM_READWRITE));
143
  
144 145 146 147 148 149 150 151 152
}

static void
gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
{
  image_menu_item->image = NULL;
}

static void
153 154 155
gtk_image_menu_item_set_property (GObject         *object,
                                  guint            prop_id,
                                  const GValue    *value,
Tim Janik's avatar
Tim Janik committed
156
                                  GParamSpec      *pspec)
157
{
158 159 160 161 162 163
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_IMAGE:
      {
164 165 166 167 168
        GtkWidget *image;

        image = (GtkWidget*) g_value_get_object (value);

	gtk_image_menu_item_set_image (image_menu_item, image);
169 170 171 172 173 174 175 176 177 178 179
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}
static void
gtk_image_menu_item_get_property (GObject         *object,
                                  guint            prop_id,
                                  GValue          *value,
Tim Janik's avatar
Tim Janik committed
180
                                  GParamSpec      *pspec)
181 182
{
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
183
  
184 185 186 187 188 189 190 191 192 193
  switch (prop_id)
    {
    case PROP_IMAGE:
      g_value_set_object (value,
                          (GObject*) image_menu_item->image);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
194 195
}

Matthias Clasen's avatar
Matthias Clasen committed
196 197 198 199 200 201 202 203 204 205
static gboolean
show_image (GtkImageMenuItem *image_menu_item)
{
  GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
  gboolean show;

  g_object_get (settings, "gtk-menu-images", &show, NULL);

  return show;
}
206

207 208 209 210
static void
gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
					 gint        *requisition)
{
211
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
212 213 214 215 216 217
  GtkPackDirection pack_dir;
  
  if (GTK_IS_MENU_BAR (GTK_WIDGET (menu_item)->parent))
    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (GTK_WIDGET (menu_item)->parent));
  else
    pack_dir = GTK_PACK_DIRECTION_LTR;
218

219 220
  *requisition = 0;

Matthias Clasen's avatar
Matthias Clasen committed
221
  if (image_menu_item->image && show_image (image_menu_item))
222
    {
223
      GtkRequisition image_requisition;
224
      guint toggle_spacing;
225 226 227
      gtk_widget_get_child_requisition (image_menu_item->image,
                                        &image_requisition);

228 229 230 231 232
      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)
233
	{
234 235 236 237 238 239 240
	  if (image_requisition.width > 0)
	    *requisition = image_requisition.width + toggle_spacing;
	}
      else
	{
	  if (image_requisition.height > 0)
	    *requisition = image_requisition.height + toggle_spacing;
241
	}
242
    }
243 244 245 246 247 248 249 250
}


static void
gtk_image_menu_item_size_request (GtkWidget      *widget,
                                  GtkRequisition *requisition)
{
  GtkImageMenuItem *image_menu_item;
251
  gint child_width = 0;
252
  gint child_height = 0;
253
  GtkPackDirection pack_dir;
254
  
255 256 257 258 259
  if (GTK_IS_MENU_BAR (widget->parent))
    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
  else
    pack_dir = GTK_PACK_DIRECTION_LTR;

260 261
  image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
  
Matthias Clasen's avatar
Matthias Clasen committed
262 263 264
  if (image_menu_item->image && 
      GTK_WIDGET_VISIBLE (image_menu_item->image) && 
      show_image (image_menu_item))
265 266 267 268 269 270
    {
      GtkRequisition child_requisition;
      
      gtk_widget_size_request (image_menu_item->image,
                               &child_requisition);

271
      child_width = child_requisition.width;
272 273 274 275 276 277 278 279
      child_height = child_requisition.height;
    }
  
  (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);

  /* not done with height since that happens via the
   * toggle_size_request
   */
280 281 282 283 284
  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);
    
285 286 287 288 289 290 291 292 293 294 295 296
  
  /* Note that GtkMenuShell always size requests before
   * toggle_size_request, so toggle_size_request will be able to use
   * image_menu_item->image->requisition
   */
}

static void
gtk_image_menu_item_size_allocate (GtkWidget     *widget,
                                   GtkAllocation *allocation)
{
  GtkImageMenuItem *image_menu_item;
297 298 299 300 301 302
  GtkPackDirection pack_dir;
  
  if (GTK_IS_MENU_BAR (widget->parent))
    pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
  else
    pack_dir = GTK_PACK_DIRECTION_LTR;
303 304 305 306 307
  
  image_menu_item = GTK_IMAGE_MENU_ITEM (widget);  
  
  (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);

Matthias Clasen's avatar
Matthias Clasen committed
308
  if (image_menu_item->image && show_image (image_menu_item))
309
    {
310 311
      gint x, y, offset;
      GtkRequisition child_requisition;
312
      GtkAllocation child_allocation;
313 314 315
      guint horizontal_padding, toggle_spacing;

      gtk_widget_style_get (widget,
316 317
			    "horizontal-padding", &horizontal_padding,
			    "toggle-spacing", &toggle_spacing,
318
			    NULL);
319 320 321 322
      
      /* Man this is lame hardcoding action, but I can't
       * come up with a solution that's really better.
       */
323 324 325 326

      gtk_widget_get_child_requisition (image_menu_item->image,
                                        &child_requisition);

327 328
      if (pack_dir == GTK_PACK_DIRECTION_LTR ||
	  pack_dir == GTK_PACK_DIRECTION_RTL)
329
	{
330 331 332 333 334 335
	  offset = GTK_CONTAINER (image_menu_item)->border_width +
	    widget->style->xthickness;
	  
	  if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
	      (pack_dir == GTK_PACK_DIRECTION_LTR))
	    x = offset + horizontal_padding +
336
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
337
	       toggle_spacing - child_requisition.width) / 2;
338 339 340 341 342 343 344
	  else
	    x = widget->allocation.width - offset - horizontal_padding -
	      GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
	       toggle_spacing - child_requisition.width) / 2;
	  
	  y = (widget->allocation.height - child_requisition.height) / 2;
345 346 347
	}
      else
	{
348 349 350 351 352 353 354 355 356 357
	  offset = GTK_CONTAINER (image_menu_item)->border_width +
	    widget->style->ythickness;
	  
	  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
	    y = widget->allocation.height - offset - horizontal_padding -
358 359
	      GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
360
	       toggle_spacing - child_requisition.height) / 2;
361

362 363 364
	  x = (widget->allocation.width - child_requisition.width) / 2;
	}
      
365 366
      child_allocation.width = child_requisition.width;
      child_allocation.height = child_requisition.height;
367 368
      child_allocation.x = widget->allocation.x + MAX (x, 0);
      child_allocation.y = widget->allocation.y + MAX (y, 0);
369 370 371 372 373 374 375 376 377 378 379

      gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
    }
}

static void
gtk_image_menu_item_forall (GtkContainer   *container,
                            gboolean	    include_internals,
                            GtkCallback     callback,
                            gpointer        callback_data)
{
380
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
381 382 383 384 385 386 387 388 389 390
  
  (* GTK_CONTAINER_CLASS (parent_class)->forall) (container,
                                                  include_internals,
                                                  callback,
                                                  callback_data);

  if (image_menu_item->image)
    (* callback) (image_menu_item->image, callback_data);
}

Matthias Clasen's avatar
Matthias Clasen committed
391 392 393 394 395 396
/**
 * gtk_image_menu_item_new:
 * @returns: a new #GtkImageMenuItem.
 *
 * Creates a new #GtkImageMenuItem with an empty label.
 **/
397
GtkWidget*
398 399 400 401 402
gtk_image_menu_item_new (void)
{
  return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
403 404 405 406 407 408 409
/**
 * 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. 
 **/
410 411
GtkWidget*
gtk_image_menu_item_new_with_label (const gchar *label)
412 413 414 415
{
  GtkImageMenuItem *image_menu_item;
  GtkWidget *accel_label;
  
Manish Singh's avatar
Manish Singh committed
416
  image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
417 418 419 420 421 422 423 424 425 426 427 428

  accel_label = gtk_accel_label_new (label);
  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);

  gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
                                    GTK_WIDGET (image_menu_item));
  gtk_widget_show (accel_label);

  return GTK_WIDGET(image_menu_item);
}

429 430 431

/**
 * gtk_image_menu_item_new_with_mnemonic:
Matthias Clasen's avatar
Matthias Clasen committed
432
 * @label: the text of the menu item, with an underscore in front of the
433 434 435 436 437 438 439 440 441 442 443 444 445
 *         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)
{
  GtkImageMenuItem *image_menu_item;
  GtkWidget *accel_label;
  
Manish Singh's avatar
Manish Singh committed
446
  image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
447

Manish Singh's avatar
Manish Singh committed
448
  accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
449 450 451 452 453 454 455 456 457 458 459
  gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
  gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);

  gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
  gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
                                    GTK_WIDGET (image_menu_item));
  gtk_widget_show (accel_label);

  return GTK_WIDGET(image_menu_item);
}

Matthias Clasen's avatar
Matthias Clasen committed
460 461 462
/**
 * gtk_image_menu_item_new_from_stock:
 * @stock_id: the name of the stock item.
463 464
 * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
 *   or %NULL.
Matthias Clasen's avatar
Matthias Clasen committed
465 466 467 468 469
 * @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.
470
 *
471 472 473 474 475
 * 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
476
 **/
477 478 479 480 481 482 483 484 485 486 487 488 489 490
GtkWidget*
gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
				    GtkAccelGroup    *accel_group)
{
  GtkWidget *image;
  GtkStockItem stock_item;
  GtkWidget *item;

  g_return_val_if_fail (stock_id != NULL, NULL);

  image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);

  if (gtk_stock_lookup (stock_id, &stock_item))
    {
491
      item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
492 493

      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
494 495 496 497 498 499 500 501 502 503
      
      if (stock_item.keyval && accel_group)
	gtk_widget_add_accelerator (item,
				    "activate",
				    accel_group,
				    stock_item.keyval,
				    stock_item.modifier,
				    GTK_ACCEL_VISIBLE);
    }
  else
504
    {
505
      item = gtk_image_menu_item_new_with_mnemonic (stock_id);
506 507 508 509

      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
    }

510 511 512 513
  gtk_widget_show (image);
  return item;
}

Matthias Clasen's avatar
Matthias Clasen committed
514 515 516 517 518 519
/** 
 * gtk_image_menu_item_set_image:
 * @image_menu_item: a #GtkImageMenuItem.
 * @image: a widget to set as the image for the menu item.
 * 
 * Sets the image of @image_menu_item to the given widget.
Matthias Clasen's avatar
Matthias Clasen committed
520 521
 * 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
522
 **/ 
523
void
524 525
gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
                               GtkWidget        *image)
526 527
{
  g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
528 529 530 531 532 533 534 535 536 537 538 539 540 541

  if (image == image_menu_item->image)
    return;

  if (image_menu_item->image)
    gtk_container_remove (GTK_CONTAINER (image_menu_item),
			  image_menu_item->image);

  image_menu_item->image = image;

  if (image == NULL)
    return;

  gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
Matthias Clasen's avatar
Matthias Clasen committed
542 543
  g_object_set (image, 
		"visible", show_image (image_menu_item),
544
		"no-show-all", TRUE,
Matthias Clasen's avatar
Matthias Clasen committed
545
		NULL);
546

547
  g_object_notify (G_OBJECT (image_menu_item), "image");
548 549
}

Matthias Clasen's avatar
Matthias Clasen committed
550 551 552 553 554 555 556 557
/**
 * 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().
 **/
558 559 560 561 562 563 564 565
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);

  return image_menu_item->image;
}

566
static void
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
gtk_image_menu_item_remove (GtkContainer *container,
                            GtkWidget    *child)
{
  GtkImageMenuItem *image_menu_item;

  image_menu_item = GTK_IMAGE_MENU_ITEM (container);

  if (child == image_menu_item->image)
    {
      gboolean widget_was_visible;
      
      widget_was_visible = GTK_WIDGET_VISIBLE (child);
      
      gtk_widget_unparent (child);
      image_menu_item->image = NULL;
      
583
      if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
584
        gtk_widget_queue_resize (GTK_WIDGET (container));
585 586

      g_object_notify (G_OBJECT (image_menu_item), "image");
587 588 589 590 591 592 593
    }
  else
    {
      (* GTK_CONTAINER_CLASS (parent_class)->remove) (container, child);
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
594 595 596
static void 
show_image_change_notify (GtkImageMenuItem *image_menu_item)
{
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  if (image_menu_item->image)
    {
      if (show_image (image_menu_item))
	gtk_widget_show (image_menu_item->image);
      else
	gtk_widget_hide (image_menu_item->image);
    }
}

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)
{
619
  GList *list, *l;
620 621 622

  list = gtk_window_list_toplevels ();

623 624 625
  for (l = list; l; l = l->next)
    gtk_container_forall (GTK_CONTAINER (l->data), 
			  traverse_container, NULL);
626 627

  g_list_free (list);  
Matthias Clasen's avatar
Matthias Clasen committed
628 629 630 631 632 633 634
}

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

637
  if (!gtk_widget_has_screen (widget))
Matthias Clasen's avatar
Matthias Clasen committed
638
    return;
639

640 641 642
  settings = gtk_widget_get_settings (widget);
  
  show_image_connection = 
643 644
    GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
					 "gtk-image-menu-item-connection"));
645 646 647
  
  if (show_image_connection)
    return;
Matthias Clasen's avatar
Matthias Clasen committed
648

649 650 651 652
  show_image_connection =
    g_signal_connect (settings, "notify::gtk-menu-images",
		      G_CALLBACK (gtk_image_menu_item_setting_changed), 0);
  g_object_set_data (G_OBJECT (settings), 
653
		     I_("gtk-image-menu-item-connection"),
654
		     GUINT_TO_POINTER (show_image_connection));
Matthias Clasen's avatar
Matthias Clasen committed
655 656 657

  show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
}
658

659 660
#define __GTK_IMAGE_MENU_ITEM_C__
#include "gtkaliasdef.c"