1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkimagemenuitem.h"
28 #include "gtkaccellabel.h"
31 #include "gtkiconfactory.h"
34 static void gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass);
35 static void gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item);
36 static void gtk_image_menu_item_size_request (GtkWidget *widget,
37 GtkRequisition *requisition);
38 static void gtk_image_menu_item_size_allocate (GtkWidget *widget,
39 GtkAllocation *allocation);
40 static void gtk_image_menu_item_remove (GtkContainer *container,
42 static void gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
45 static void gtk_image_menu_item_forall (GtkContainer *container,
46 gboolean include_internals,
48 gpointer callback_data);
50 static void gtk_image_menu_item_set_property (GObject *object,
54 static void gtk_image_menu_item_get_property (GObject *object,
65 static GtkMenuItemClass *parent_class = NULL;
68 gtk_image_menu_item_get_type (void)
70 static GType image_menu_item_type = 0;
72 if (!image_menu_item_type)
74 static const GTypeInfo image_menu_item_info =
76 sizeof (GtkImageMenuItemClass),
78 NULL, /* base_finalize */
79 (GClassInitFunc) gtk_image_menu_item_class_init,
80 NULL, /* class_finalize */
81 NULL, /* class_data */
82 sizeof (GtkImageMenuItem),
84 (GInstanceInitFunc) gtk_image_menu_item_init,
87 image_menu_item_type =
88 g_type_register_static (GTK_TYPE_MENU_ITEM, "GtkImageMenuItem",
89 &image_menu_item_info, 0);
92 return image_menu_item_type;
96 gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
98 GObjectClass *gobject_class;
99 GtkWidgetClass *widget_class;
100 GtkMenuItemClass *menu_item_class;
101 GtkContainerClass *container_class;
103 gobject_class = (GObjectClass*) klass;
104 widget_class = (GtkWidgetClass*) klass;
105 menu_item_class = (GtkMenuItemClass*) klass;
106 container_class = (GtkContainerClass*) klass;
108 parent_class = g_type_class_peek_parent (klass);
110 widget_class->size_request = gtk_image_menu_item_size_request;
111 widget_class->size_allocate = gtk_image_menu_item_size_allocate;
113 container_class->forall = gtk_image_menu_item_forall;
114 container_class->remove = gtk_image_menu_item_remove;
116 menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
118 gobject_class->set_property = gtk_image_menu_item_set_property;
119 gobject_class->get_property = gtk_image_menu_item_get_property;
121 g_object_class_install_property (gobject_class,
123 g_param_spec_object ("image",
125 _("Child widget to appear next to the menu text"),
127 G_PARAM_READABLE | G_PARAM_WRITABLE));
131 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
133 image_menu_item->image = NULL;
137 gtk_image_menu_item_set_property (GObject *object,
142 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
150 image = (GtkWidget*) g_value_get_object (value);
152 gtk_image_menu_item_set_image (image_menu_item, image);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 gtk_image_menu_item_get_property (GObject *object,
166 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
171 g_value_set_object (value,
172 (GObject*) image_menu_item->image);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
185 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
187 if (image_menu_item->image)
189 guint toggle_spacing;
191 gtk_widget_style_get (GTK_WIDGET (menu_item),
192 "toggle_spacing", &toggle_spacing,
195 *requisition = image_menu_item->image->requisition.width + toggle_spacing;
203 gtk_image_menu_item_size_request (GtkWidget *widget,
204 GtkRequisition *requisition)
206 GtkImageMenuItem *image_menu_item;
207 gint child_height = 0;
209 image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
211 if (image_menu_item->image && GTK_WIDGET_VISIBLE (image_menu_item->image))
213 GtkRequisition child_requisition;
215 gtk_widget_size_request (image_menu_item->image,
218 child_height = child_requisition.height;
221 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
223 /* not done with height since that happens via the
224 * toggle_size_request
226 requisition->height = MAX (requisition->height, child_height);
228 /* Note that GtkMenuShell always size requests before
229 * toggle_size_request, so toggle_size_request will be able to use
230 * image_menu_item->image->requisition
235 gtk_image_menu_item_size_allocate (GtkWidget *widget,
236 GtkAllocation *allocation)
238 GtkImageMenuItem *image_menu_item;
240 image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
242 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
244 if (image_menu_item->image)
246 gint width, height, x, y, offset;
247 GtkAllocation child_allocation;
248 guint horizontal_padding, toggle_spacing;
250 gtk_widget_style_get (widget,
251 "horizontal_padding", &horizontal_padding,
252 "toggle_spacing", &toggle_spacing,
255 /* Man this is lame hardcoding action, but I can't
256 * come up with a solution that's really better.
259 width = image_menu_item->image->requisition.width;
260 height = image_menu_item->image->requisition.height;
261 offset = GTK_CONTAINER (image_menu_item)->border_width +
262 widget->style->xthickness;
264 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
266 x = offset + horizontal_padding +
267 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
268 toggle_spacing - width) / 2;
272 x = widget->allocation.width - offset - horizontal_padding -
273 GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
274 (GTK_MENU_ITEM (image_menu_item)->toggle_size -
275 toggle_spacing - width) / 2;
278 y = (widget->allocation.height - height) / 2;
280 child_allocation.width = width;
281 child_allocation.height = height;
282 child_allocation.x = widget->allocation.x + MAX (x, 0);
283 child_allocation.y = widget->allocation.y + MAX (y, 0);
285 gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
290 gtk_image_menu_item_forall (GtkContainer *container,
291 gboolean include_internals,
292 GtkCallback callback,
293 gpointer callback_data)
295 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
297 (* GTK_CONTAINER_CLASS (parent_class)->forall) (container,
302 if (image_menu_item->image)
303 (* callback) (image_menu_item->image, callback_data);
307 * gtk_image_menu_item_new:
308 * @returns: a new #GtkImageMenuItem.
310 * Creates a new #GtkImageMenuItem with an empty label.
313 gtk_image_menu_item_new (void)
315 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
319 * gtk_image_menu_item_new_with_label:
320 * @label: the text of the menu item.
321 * @returns: a new #GtkImageMenuItem.
323 * Creates a new #GtkImageMenuItem containing a label.
326 gtk_image_menu_item_new_with_label (const gchar *label)
328 GtkImageMenuItem *image_menu_item;
329 GtkWidget *accel_label;
331 image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
333 accel_label = gtk_accel_label_new (label);
334 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
336 gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
337 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
338 GTK_WIDGET (image_menu_item));
339 gtk_widget_show (accel_label);
341 return GTK_WIDGET(image_menu_item);
346 * gtk_image_menu_item_new_with_mnemonic:
347 * @label: the text of the menu item, with an underscore in front of the
349 * @returns: a new #GtkImageMenuItem
351 * Creates a new #GtkImageMenuItem containing a label. The label
352 * will be created using gtk_label_new_with_mnemonic(), so underscores
353 * in @label indicate the mnemonic for the menu item.
356 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
358 GtkImageMenuItem *image_menu_item;
359 GtkWidget *accel_label;
361 image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
363 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
364 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
365 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
367 gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
368 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
369 GTK_WIDGET (image_menu_item));
370 gtk_widget_show (accel_label);
372 return GTK_WIDGET(image_menu_item);
376 * gtk_image_menu_item_new_from_stock:
377 * @stock_id: the name of the stock item.
378 * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
380 * @returns: a new #GtkImageMenuItem.
382 * Creates a new #GtkImageMenuItem containing the image and text from a
383 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
384 * and #GTK_STOCK_APPLY.
386 * If you want this menu item to have changeable accelerators, then pass in
387 * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
388 * appropriate path for the menu item, use gtk_stock_lookup() to look up the
389 * standard accelerator for the stock item, and if one is found, call
390 * gtk_accel_map_add_entry() to register it.
393 gtk_image_menu_item_new_from_stock (const gchar *stock_id,
394 GtkAccelGroup *accel_group)
397 GtkStockItem stock_item;
400 g_return_val_if_fail (stock_id != NULL, NULL);
402 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
404 if (gtk_stock_lookup (stock_id, &stock_item))
406 item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
408 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
410 if (stock_item.keyval && accel_group)
411 gtk_widget_add_accelerator (item,
420 item = gtk_image_menu_item_new_with_mnemonic (stock_id);
422 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
425 gtk_widget_show (image);
430 * gtk_image_menu_item_set_image:
431 * @image_menu_item: a #GtkImageMenuItem.
432 * @image: a widget to set as the image for the menu item.
434 * Sets the image of @image_menu_item to the given widget.
437 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
440 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
442 if (image == image_menu_item->image)
445 if (image_menu_item->image)
446 gtk_container_remove (GTK_CONTAINER (image_menu_item),
447 image_menu_item->image);
449 image_menu_item->image = image;
454 gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
456 g_object_notify (G_OBJECT (image_menu_item), "image");
460 * gtk_image_menu_item_get_image:
461 * @image_menu_item: a #GtkImageMenuItem.
462 * @returns: the widget set as image of @image_menu_item.
464 * Gets the widget that is currently set as the image of @image_menu_item.
465 * See gtk_image_menu_item_set_image().
468 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
470 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
472 return image_menu_item->image;
476 gtk_image_menu_item_remove (GtkContainer *container,
479 GtkImageMenuItem *image_menu_item;
481 image_menu_item = GTK_IMAGE_MENU_ITEM (container);
483 if (child == image_menu_item->image)
485 gboolean widget_was_visible;
487 widget_was_visible = GTK_WIDGET_VISIBLE (child);
489 gtk_widget_unparent (child);
490 image_menu_item->image = NULL;
492 if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
493 gtk_widget_queue_resize (GTK_WIDGET (container));
495 g_object_notify (G_OBJECT (image_menu_item), "image");
499 (* GTK_CONTAINER_CLASS (parent_class)->remove) (container, child);