gtkimagemenuitem.c 15.6 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 27 28
/* 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/. 
 */

#include "gtkimagemenuitem.h"
#include "gtkaccellabel.h"
29
#include "gtkintl.h"
30 31 32
#include "gtkstock.h"
#include "gtkiconfactory.h"
#include "gtkimage.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

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

50 51 52
static void gtk_image_menu_item_set_property (GObject         *object,
                                              guint            prop_id,
                                              const GValue    *value,
Tim Janik's avatar
Tim Janik committed
53
                                              GParamSpec      *pspec);
54 55 56
static void gtk_image_menu_item_get_property (GObject         *object,
                                              guint            prop_id,
                                              GValue          *value,
Tim Janik's avatar
Tim Janik committed
57
                                              GParamSpec      *pspec);
58 59 60 61 62 63 64


enum {
  PROP_ZERO,
  PROP_IMAGE
};

65 66
static GtkMenuItemClass *parent_class = NULL;

Manish Singh's avatar
Manish Singh committed
67
GType
68 69
gtk_image_menu_item_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
70
  static GType image_menu_item_type = 0;
71 72 73

  if (!image_menu_item_type)
    {
Manish Singh's avatar
Manish Singh committed
74
      static const GTypeInfo image_menu_item_info =
75 76
      {
        sizeof (GtkImageMenuItemClass),
Manish Singh's avatar
Manish Singh committed
77 78 79 80 81 82 83 84
	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,
85 86
      };

Manish Singh's avatar
Manish Singh committed
87 88 89
      image_menu_item_type =
	g_type_register_static (GTK_TYPE_MENU_ITEM, "GtkImageMenuItem",
				&image_menu_item_info, 0);
90 91 92 93 94 95 96 97
    }

  return image_menu_item_type;
}

static void
gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
{
98
  GObjectClass *gobject_class;
99 100 101
  GtkWidgetClass *widget_class;
  GtkMenuItemClass *menu_item_class;
  GtkContainerClass *container_class;
102 103

  gobject_class = (GObjectClass*) klass;
104 105 106 107
  widget_class = (GtkWidgetClass*) klass;
  menu_item_class = (GtkMenuItemClass*) klass;
  container_class = (GtkContainerClass*) klass;
  
Manish Singh's avatar
Manish Singh committed
108
  parent_class = g_type_class_peek_parent (klass);
109 110 111 112 113 114 115 116
  
  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;
117 118 119 120 121 122 123 124 125 126 127

  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",
                                                        _("Image widget"),
                                                        _("Child widget to appear next to the menu text"),
                                                        GTK_TYPE_WIDGET,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
128 129 130 131 132 133 134 135 136
}

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

static void
137 138 139
gtk_image_menu_item_set_property (GObject         *object,
                                  guint            prop_id,
                                  const GValue    *value,
Tim Janik's avatar
Tim Janik committed
140
                                  GParamSpec      *pspec)
141
{
142 143 144 145 146 147
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
  
  switch (prop_id)
    {
    case PROP_IMAGE:
      {
148 149 150 151 152
        GtkWidget *image;

        image = (GtkWidget*) g_value_get_object (value);

	gtk_image_menu_item_set_image (image_menu_item, image);
153 154 155 156 157 158 159 160 161 162 163
      }
      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
164
                                  GParamSpec      *pspec)
165 166
{
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
167
  
168 169 170 171 172 173 174 175 176 177
  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;
    }
178 179
}

180

181 182 183 184
static void
gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
					 gint        *requisition)
{
185
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
186

187 188
  *requisition = 0;

189
  if (image_menu_item->image)
190
    {
191 192 193 194 195 196 197 198 199 200 201
      guint image_width = image_menu_item->image->requisition.width;

      if (image_width > 0)
	{
	  guint toggle_spacing;
	  gtk_widget_style_get (GTK_WIDGET (menu_item),
				"toggle_spacing", &toggle_spacing,
				NULL);
	  
	  *requisition = image_width + toggle_spacing;
	}
202
    }
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
}


static void
gtk_image_menu_item_size_request (GtkWidget      *widget,
                                  GtkRequisition *requisition)
{
  GtkImageMenuItem *image_menu_item;
  gint child_height = 0;
  
  image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
  
  if (image_menu_item->image && GTK_WIDGET_VISIBLE (image_menu_item->image))
    {
      GtkRequisition child_requisition;
      
      gtk_widget_size_request (image_menu_item->image,
                               &child_requisition);

      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
   */
  requisition->height = MAX (requisition->height, child_height);
  
  /* 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;
  
  image_menu_item = GTK_IMAGE_MENU_ITEM (widget);  
  
  (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);

  if (image_menu_item->image)
    {
250
      gint width, height, x, y, offset;
251
      GtkAllocation child_allocation;
252 253 254 255 256 257
      guint horizontal_padding, toggle_spacing;

      gtk_widget_style_get (widget,
			    "horizontal_padding", &horizontal_padding,
			    "toggle_spacing", &toggle_spacing,
			    NULL);
258 259 260 261 262 263 264
      
      /* Man this is lame hardcoding action, but I can't
       * come up with a solution that's really better.
       */
      
      width = image_menu_item->image->requisition.width;
      height = image_menu_item->image->requisition.height;
265
      offset = GTK_CONTAINER (image_menu_item)->border_width +
266
	widget->style->xthickness;
267 268

      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) 
269 270 271 272 273 274 275 276 277 278 279 280 281
	{
	  x = offset + horizontal_padding +
	      (GTK_MENU_ITEM (image_menu_item)->toggle_size -
	       toggle_spacing - width) / 2;
	}
      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 - width) / 2;
	}

282 283 284 285
      y = (widget->allocation.height - height) / 2;

      child_allocation.width = width;
      child_allocation.height = height;
286 287
      child_allocation.x = widget->allocation.x + MAX (x, 0);
      child_allocation.y = widget->allocation.y + MAX (y, 0);
288 289 290 291 292 293 294 295 296 297 298

      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)
{
299
  GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
300 301 302 303 304 305 306 307 308 309
  
  (* 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
310 311 312 313 314 315
/**
 * gtk_image_menu_item_new:
 * @returns: a new #GtkImageMenuItem.
 *
 * Creates a new #GtkImageMenuItem with an empty label.
 **/
316
GtkWidget*
317 318 319 320 321
gtk_image_menu_item_new (void)
{
  return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
322 323 324 325 326 327 328
/**
 * 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. 
 **/
329 330
GtkWidget*
gtk_image_menu_item_new_with_label (const gchar *label)
331 332 333 334
{
  GtkImageMenuItem *image_menu_item;
  GtkWidget *accel_label;
  
Manish Singh's avatar
Manish Singh committed
335
  image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
336 337 338 339 340 341 342 343 344 345 346 347

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

348 349 350

/**
 * gtk_image_menu_item_new_with_mnemonic:
Matthias Clasen's avatar
Matthias Clasen committed
351
 * @label: the text of the menu item, with an underscore in front of the
352 353 354 355 356 357 358 359 360 361 362 363 364
 *         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
365
  image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
366

Manish Singh's avatar
Manish Singh committed
367
  accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
368 369 370 371 372 373 374 375 376 377 378
  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
379 380 381
/**
 * gtk_image_menu_item_new_from_stock:
 * @stock_id: the name of the stock item.
382 383
 * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
 *   or %NULL.
Matthias Clasen's avatar
Matthias Clasen committed
384 385 386 387 388
 * @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.
389
 *
Matthias Clasen's avatar
Matthias Clasen committed
390 391 392 393 394
 * 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
395
 **/
396 397 398 399 400 401 402 403 404 405 406 407 408 409
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))
    {
410
      item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
411 412

      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
413 414 415 416 417 418 419 420 421 422
      
      if (stock_item.keyval && accel_group)
	gtk_widget_add_accelerator (item,
				    "activate",
				    accel_group,
				    stock_item.keyval,
				    stock_item.modifier,
				    GTK_ACCEL_VISIBLE);
    }
  else
423
    {
424
      item = gtk_image_menu_item_new_with_mnemonic (stock_id);
425 426 427 428

      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
    }

429 430 431 432
  gtk_widget_show (image);
  return item;
}

Matthias Clasen's avatar
Matthias Clasen committed
433 434 435 436 437 438 439
/** 
 * 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.
 **/ 
440
void
441 442
gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
                               GtkWidget        *image)
443 444
{
  g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
445 446 447 448 449 450 451 452 453 454 455 456 457 458

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

460
  g_object_notify (G_OBJECT (image_menu_item), "image");
461 462
}

Matthias Clasen's avatar
Matthias Clasen committed
463 464 465 466 467 468 469 470
/**
 * 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().
 **/
471 472 473 474 475 476 477 478
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;
}

479
static void
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
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;
      
496
      if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
497
        gtk_widget_queue_resize (GTK_WIDGET (container));
498 499

      g_object_notify (G_OBJECT (image_menu_item), "image");
500 501 502 503 504 505 506 507
    }
  else
    {
      (* GTK_CONTAINER_CLASS (parent_class)->remove) (container, child);
    }
}