]> Pileus Git - ~andy/gtk/blob - gtk/gtkimagemenuitem.c
a51ccf0248021f9dfcafdbb376ca3b2aab62ae8b
[~andy/gtk] / gtk / gtkimagemenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "gtkimagemenuitem.h"
28 #include "gtkaccellabel.h"
29 #include "gtksignal.h"
30 #include "gtkintl.h"
31 #include "gtkstock.h"
32 #include "gtkiconfactory.h"
33 #include "gtkimage.h"
34
35 static void gtk_image_menu_item_class_init           (GtkImageMenuItemClass *klass);
36 static void gtk_image_menu_item_init                 (GtkImageMenuItem      *image_menu_item);
37 static void gtk_image_menu_item_size_request         (GtkWidget        *widget,
38                                                       GtkRequisition   *requisition);
39 static void gtk_image_menu_item_size_allocate        (GtkWidget        *widget,
40                                                       GtkAllocation    *allocation);
41 static void gtk_image_menu_item_remove               (GtkContainer          *container,
42                                                       GtkWidget             *child);
43 static void gtk_image_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
44                                                       gint                  *requisition);
45
46 static void gtk_image_menu_item_forall     (GtkContainer   *container,
47                                             gboolean        include_internals,
48                                             GtkCallback     callback,
49                                             gpointer        callback_data);
50
51 static void gtk_image_menu_item_set_property (GObject         *object,
52                                               guint            prop_id,
53                                               const GValue    *value,
54                                               GParamSpec      *pspec);
55 static void gtk_image_menu_item_get_property (GObject         *object,
56                                               guint            prop_id,
57                                               GValue          *value,
58                                               GParamSpec      *pspec);
59
60
61 enum {
62   PROP_ZERO,
63   PROP_IMAGE
64 };
65
66 static GtkMenuItemClass *parent_class = NULL;
67
68 GtkType
69 gtk_image_menu_item_get_type (void)
70 {
71   static GtkType image_menu_item_type = 0;
72
73   if (!image_menu_item_type)
74     {
75       static const GtkTypeInfo image_menu_item_info =
76       {
77         "GtkImageMenuItem",
78         sizeof (GtkImageMenuItem),
79         sizeof (GtkImageMenuItemClass),
80         (GtkClassInitFunc) gtk_image_menu_item_class_init,
81         (GtkObjectInitFunc) gtk_image_menu_item_init,
82         /* reserved_1 */ NULL,
83         /* reserved_2 */ NULL,
84         (GtkClassInitFunc) NULL,
85       };
86
87       image_menu_item_type = gtk_type_unique (GTK_TYPE_MENU_ITEM, &image_menu_item_info);
88     }
89
90   return image_menu_item_type;
91 }
92
93 static void
94 gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
95 {
96   GObjectClass *gobject_class;
97   GtkObjectClass *object_class;
98   GtkWidgetClass *widget_class;
99   GtkMenuItemClass *menu_item_class;
100   GtkContainerClass *container_class;
101
102   gobject_class = (GObjectClass*) klass;
103   object_class = (GtkObjectClass*) klass;
104   widget_class = (GtkWidgetClass*) klass;
105   menu_item_class = (GtkMenuItemClass*) klass;
106   container_class = (GtkContainerClass*) klass;
107   
108   parent_class = gtk_type_class (GTK_TYPE_MENU_ITEM);
109   
110   widget_class->size_request = gtk_image_menu_item_size_request;
111   widget_class->size_allocate = gtk_image_menu_item_size_allocate;
112
113   container_class->forall = gtk_image_menu_item_forall;
114   container_class->remove = gtk_image_menu_item_remove;
115   
116   menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
117
118   gobject_class->set_property = gtk_image_menu_item_set_property;
119   gobject_class->get_property = gtk_image_menu_item_get_property;
120   
121   g_object_class_install_property (gobject_class,
122                                    PROP_IMAGE,
123                                    g_param_spec_object ("image",
124                                                         _("Image widget"),
125                                                         _("Child widget to appear next to the menu text"),
126                                                         GTK_TYPE_WIDGET,
127                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
128 }
129
130 static void
131 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
132 {
133   image_menu_item->image = NULL;
134 }
135
136 static void
137 gtk_image_menu_item_set_property (GObject         *object,
138                                   guint            prop_id,
139                                   const GValue    *value,
140                                   GParamSpec      *pspec)
141 {
142   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
143   
144   switch (prop_id)
145     {
146     case PROP_IMAGE:
147       {
148         GtkWidget *image;
149
150         image = (GtkWidget*) g_value_get_object (value);
151
152         gtk_image_menu_item_set_image (image_menu_item, image);
153       }
154       break;
155     default:
156       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157       break;
158     }
159 }
160 static void
161 gtk_image_menu_item_get_property (GObject         *object,
162                                   guint            prop_id,
163                                   GValue          *value,
164                                   GParamSpec      *pspec)
165 {
166   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
167   
168   switch (prop_id)
169     {
170     case PROP_IMAGE:
171       g_value_set_object (value,
172                           (GObject*) image_menu_item->image);
173       break;
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177     }
178 }
179
180
181 static void
182 gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
183                                          gint        *requisition)
184 {
185   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
186
187   if (image_menu_item->image)
188     *requisition = image_menu_item->image->requisition.width;
189   else
190     *requisition = 0;
191 }
192
193
194 static void
195 gtk_image_menu_item_size_request (GtkWidget      *widget,
196                                   GtkRequisition *requisition)
197 {
198   GtkImageMenuItem *image_menu_item;
199   gint child_height = 0;
200   
201   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
202   
203   if (image_menu_item->image && GTK_WIDGET_VISIBLE (image_menu_item->image))
204     {
205       GtkRequisition child_requisition;
206       
207       gtk_widget_size_request (image_menu_item->image,
208                                &child_requisition);
209
210       child_height = child_requisition.height;
211     }
212   
213   (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
214
215   /* not done with height since that happens via the
216    * toggle_size_request
217    */
218   requisition->height = MAX (requisition->height, child_height);
219   
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
223    */
224 }
225
226 static void
227 gtk_image_menu_item_size_allocate (GtkWidget     *widget,
228                                    GtkAllocation *allocation)
229 {
230   GtkImageMenuItem *image_menu_item;
231   
232   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);  
233   
234   (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
235
236   if (image_menu_item->image)
237     {
238       gint width, height, x, y;
239       GtkAllocation child_allocation;
240       
241       /* Man this is lame hardcoding action, but I can't
242        * come up with a solution that's really better.
243        */
244       
245       width = image_menu_item->image->requisition.width;
246       height = image_menu_item->image->requisition.height;
247
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;
252
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);
257
258       gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
259     }
260 }
261
262 static void
263 gtk_image_menu_item_forall (GtkContainer   *container,
264                             gboolean        include_internals,
265                             GtkCallback     callback,
266                             gpointer        callback_data)
267 {
268   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
269   
270   (* GTK_CONTAINER_CLASS (parent_class)->forall) (container,
271                                                   include_internals,
272                                                   callback,
273                                                   callback_data);
274
275   if (image_menu_item->image)
276     (* callback) (image_menu_item->image, callback_data);
277 }
278
279 /**
280  * gtk_image_menu_item_new:
281  * @returns: a new #GtkImageMenuItem.
282  *
283  * Creates a new #GtkImageMenuItem with an empty label.
284  **/
285 GtkWidget*
286 gtk_image_menu_item_new (void)
287 {
288   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
289 }
290
291 /**
292  * gtk_image_menu_item_new_with_label:
293  * @label: the text of the menu item.
294  * @returns: a new #GtkImageMenuItem.
295  *
296  * Creates a new #GtkImageMenuItem containing a label. 
297  **/
298 GtkWidget*
299 gtk_image_menu_item_new_with_label (const gchar *label)
300 {
301   GtkImageMenuItem *image_menu_item;
302   GtkWidget *accel_label;
303   
304   image_menu_item = GTK_IMAGE_MENU_ITEM (g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
305                                                        NULL));
306
307   accel_label = gtk_accel_label_new (label);
308   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
309
310   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
311   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
312                                     GTK_WIDGET (image_menu_item));
313   gtk_widget_show (accel_label);
314
315   return GTK_WIDGET(image_menu_item);
316 }
317
318
319 /**
320  * gtk_image_menu_item_new_with_mnemonic:
321  * @label: the text of the menu item, with an underscore in front of the
322  *         mnemonic character
323  * @returns: a new #GtkImageMenuItem
324  *
325  * Creates a new #GtkImageMenuItem containing a label. The label
326  * will be created using gtk_label_new_with_mnemonic(), so underscores
327  * in @label indicate the mnemonic for the menu item.
328  **/
329 GtkWidget*
330 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
331 {
332   GtkImageMenuItem *image_menu_item;
333   GtkWidget *accel_label;
334   
335   image_menu_item = GTK_IMAGE_MENU_ITEM (g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
336                                                        NULL));
337
338   accel_label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
339   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
340   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
341
342   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
343   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
344                                     GTK_WIDGET (image_menu_item));
345   gtk_widget_show (accel_label);
346
347   return GTK_WIDGET(image_menu_item);
348 }
349
350 /**
351  * gtk_image_menu_item_new_from_stock:
352  * @stock_id: the name of the stock item.
353  * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
354  *   or %NULL.
355  * @returns: a new #GtkImageMenuItem.
356  *
357  * Creates a new #GtkImageMenuItem containing the image and text from a 
358  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK 
359  * and #GTK_STOCK_APPLY.
360  *
361  * If you want this menu item to have changeable accelerators, then
362  * pass in %NULL for @accel_group call gtk_menu_item_set_accel_path()
363  * with an appropriate path for the menu item, then use gtk_stock_lookup()
364  * too look up the standard accelerator for the stock item and
365  * if one is found, call gtk_accel_map_add_entry() to register it.
366  **/
367 GtkWidget*
368 gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
369                                     GtkAccelGroup    *accel_group)
370 {
371   GtkWidget *image;
372   GtkStockItem stock_item;
373   GtkWidget *item;
374
375   g_return_val_if_fail (stock_id != NULL, NULL);
376
377   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
378
379   if (gtk_stock_lookup (stock_id, &stock_item))
380     {
381       item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
382
383       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
384       
385       if (stock_item.keyval && accel_group)
386         gtk_widget_add_accelerator (item,
387                                     "activate",
388                                     accel_group,
389                                     stock_item.keyval,
390                                     stock_item.modifier,
391                                     GTK_ACCEL_VISIBLE);
392     }
393   else
394     {
395       item = gtk_image_menu_item_new_with_mnemonic (stock_id);
396
397       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
398     }
399
400   gtk_widget_show (image);
401   return item;
402 }
403
404 /** 
405  * gtk_image_menu_item_set_image:
406  * @image_menu_item: a #GtkImageMenuItem.
407  * @image: a widget to set as the image for the menu item.
408  * 
409  * Sets the image of @image_menu_item to the given widget.
410  **/ 
411 void
412 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
413                                GtkWidget        *image)
414 {
415   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
416
417   if (image == image_menu_item->image)
418     return;
419
420   if (image_menu_item->image)
421     gtk_container_remove (GTK_CONTAINER (image_menu_item),
422                           image_menu_item->image);
423
424   image_menu_item->image = image;
425
426   if (image == NULL)
427     return;
428
429   gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
430
431   g_object_notify (G_OBJECT (image_menu_item), "image");
432 }
433
434 /**
435  * gtk_image_menu_item_get_image:
436  * @image_menu_item: a #GtkImageMenuItem.
437  * @returns: the widget set as image of @image_menu_item.
438  *
439  * Gets the widget that is currently set as the image of @image_menu_item.
440  * See gtk_image_menu_item_set_image().
441  **/
442 GtkWidget*
443 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
444 {
445   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
446
447   return image_menu_item->image;
448 }
449
450 static void
451 gtk_image_menu_item_remove (GtkContainer *container,
452                             GtkWidget    *child)
453 {
454   GtkImageMenuItem *image_menu_item;
455
456   image_menu_item = GTK_IMAGE_MENU_ITEM (container);
457
458   if (child == image_menu_item->image)
459     {
460       gboolean widget_was_visible;
461       
462       widget_was_visible = GTK_WIDGET_VISIBLE (child);
463       
464       gtk_widget_unparent (child);
465       image_menu_item->image = NULL;
466       
467       if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
468         gtk_widget_queue_resize (GTK_WIDGET (container));
469
470       g_object_notify (G_OBJECT (image_menu_item), "image");
471     }
472   else
473     {
474       (* GTK_CONTAINER_CLASS (parent_class)->remove) (container, child);
475     }
476 }
477
478