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)
188 *requisition = image_menu_item->image->requisition.width;
195 gtk_image_menu_item_size_request (GtkWidget *widget,
196 GtkRequisition *requisition)
198 GtkImageMenuItem *image_menu_item;
199 gint child_height = 0;
201 image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
203 if (image_menu_item->image && GTK_WIDGET_VISIBLE (image_menu_item->image))
205 GtkRequisition child_requisition;
207 gtk_widget_size_request (image_menu_item->image,
210 child_height = child_requisition.height;
213 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
215 /* not done with height since that happens via the
216 * toggle_size_request
218 requisition->height = MAX (requisition->height, child_height);
220 /* Note that GtkMenuShell always size requests before
221 * toggle_size_request, so toggle_size_request will be able to use
222 * image_menu_item->image->requisition
227 gtk_image_menu_item_size_allocate (GtkWidget *widget,
228 GtkAllocation *allocation)
230 GtkImageMenuItem *image_menu_item;
232 image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
234 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
236 if (image_menu_item->image)
238 gint width, height, x, y;
239 GtkAllocation child_allocation;
241 /* Man this is lame hardcoding action, but I can't
242 * come up with a solution that's really better.
245 width = image_menu_item->image->requisition.width;
246 height = image_menu_item->image->requisition.height;
248 x = (GTK_CONTAINER (image_menu_item)->border_width +
249 widget->style->xthickness) +
250 (GTK_MENU_ITEM (image_menu_item)->toggle_size - width) / 2;
251 y = (widget->allocation.height - height) / 2;
253 child_allocation.width = width;
254 child_allocation.height = height;
255 child_allocation.x = widget->allocation.x + MAX (x, 0);
256 child_allocation.y = widget->allocation.y + MAX (y, 0);
258 gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
263 gtk_image_menu_item_forall (GtkContainer *container,
264 gboolean include_internals,
265 GtkCallback callback,
266 gpointer callback_data)
268 GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
270 (* GTK_CONTAINER_CLASS (parent_class)->forall) (container,
275 if (image_menu_item->image)
276 (* callback) (image_menu_item->image, callback_data);
280 * gtk_image_menu_item_new:
281 * @returns: a new #GtkImageMenuItem.
283 * Creates a new #GtkImageMenuItem with an empty label.
286 gtk_image_menu_item_new (void)
288 return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
292 * gtk_image_menu_item_new_with_label:
293 * @label: the text of the menu item.
294 * @returns: a new #GtkImageMenuItem.
296 * Creates a new #GtkImageMenuItem containing a label.
299 gtk_image_menu_item_new_with_label (const gchar *label)
301 GtkImageMenuItem *image_menu_item;
302 GtkWidget *accel_label;
304 image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
306 accel_label = gtk_accel_label_new (label);
307 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
309 gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
310 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
311 GTK_WIDGET (image_menu_item));
312 gtk_widget_show (accel_label);
314 return GTK_WIDGET(image_menu_item);
319 * gtk_image_menu_item_new_with_mnemonic:
320 * @label: the text of the menu item, with an underscore in front of the
322 * @returns: a new #GtkImageMenuItem
324 * Creates a new #GtkImageMenuItem containing a label. The label
325 * will be created using gtk_label_new_with_mnemonic(), so underscores
326 * in @label indicate the mnemonic for the menu item.
329 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
331 GtkImageMenuItem *image_menu_item;
332 GtkWidget *accel_label;
334 image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
336 accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
337 gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
338 gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
340 gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
341 gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
342 GTK_WIDGET (image_menu_item));
343 gtk_widget_show (accel_label);
345 return GTK_WIDGET(image_menu_item);
349 * gtk_image_menu_item_new_from_stock:
350 * @stock_id: the name of the stock item.
351 * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
353 * @returns: a new #GtkImageMenuItem.
355 * Creates a new #GtkImageMenuItem containing the image and text from a
356 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
357 * and #GTK_STOCK_APPLY.
359 * If you want this menu item to have changeable accelerators, then
360 * pass in %NULL for @accel_group call gtk_menu_item_set_accel_path()
361 * with an appropriate path for the menu item, then use gtk_stock_lookup()
362 * too look up the standard accelerator for the stock item and
363 * if one is found, call gtk_accel_map_add_entry() to register it.
366 gtk_image_menu_item_new_from_stock (const gchar *stock_id,
367 GtkAccelGroup *accel_group)
370 GtkStockItem stock_item;
373 g_return_val_if_fail (stock_id != NULL, NULL);
375 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
377 if (gtk_stock_lookup (stock_id, &stock_item))
379 item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
381 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
383 if (stock_item.keyval && accel_group)
384 gtk_widget_add_accelerator (item,
393 item = gtk_image_menu_item_new_with_mnemonic (stock_id);
395 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
398 gtk_widget_show (image);
403 * gtk_image_menu_item_set_image:
404 * @image_menu_item: a #GtkImageMenuItem.
405 * @image: a widget to set as the image for the menu item.
407 * Sets the image of @image_menu_item to the given widget.
410 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
413 g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
415 if (image == image_menu_item->image)
418 if (image_menu_item->image)
419 gtk_container_remove (GTK_CONTAINER (image_menu_item),
420 image_menu_item->image);
422 image_menu_item->image = image;
427 gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
429 g_object_notify (G_OBJECT (image_menu_item), "image");
433 * gtk_image_menu_item_get_image:
434 * @image_menu_item: a #GtkImageMenuItem.
435 * @returns: the widget set as image of @image_menu_item.
437 * Gets the widget that is currently set as the image of @image_menu_item.
438 * See gtk_image_menu_item_set_image().
441 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
443 g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
445 return image_menu_item->image;
449 gtk_image_menu_item_remove (GtkContainer *container,
452 GtkImageMenuItem *image_menu_item;
454 image_menu_item = GTK_IMAGE_MENU_ITEM (container);
456 if (child == image_menu_item->image)
458 gboolean widget_was_visible;
460 widget_was_visible = GTK_WIDGET_VISIBLE (child);
462 gtk_widget_unparent (child);
463 image_menu_item->image = NULL;
465 if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
466 gtk_widget_queue_resize (GTK_WIDGET (container));
468 g_object_notify (G_OBJECT (image_menu_item), "image");
472 (* GTK_CONTAINER_CLASS (parent_class)->remove) (container, child);