]> Pileus Git - ~andy/gtk/blob - gtk/gtkimagemenuitem.c
Make Menu items NO_WINDOW widgets.
[~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;
186   
187   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (menu_item));
188
189   image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
190
191   if (image_menu_item->image)
192     *requisition = image_menu_item->image->requisition.width;
193   else
194     *requisition = 0;
195 }
196
197
198 static void
199 gtk_image_menu_item_size_request (GtkWidget      *widget,
200                                   GtkRequisition *requisition)
201 {
202   GtkImageMenuItem *image_menu_item;
203   gint child_height = 0;
204   
205   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
206   
207   if (image_menu_item->image && GTK_WIDGET_VISIBLE (image_menu_item->image))
208     {
209       GtkRequisition child_requisition;
210       
211       gtk_widget_size_request (image_menu_item->image,
212                                &child_requisition);
213
214       child_height = child_requisition.height;
215     }
216   
217   (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
218
219   /* not done with height since that happens via the
220    * toggle_size_request
221    */
222   requisition->height = MAX (requisition->height, child_height);
223   
224   /* Note that GtkMenuShell always size requests before
225    * toggle_size_request, so toggle_size_request will be able to use
226    * image_menu_item->image->requisition
227    */
228 }
229
230 static void
231 gtk_image_menu_item_size_allocate (GtkWidget     *widget,
232                                    GtkAllocation *allocation)
233 {
234   GtkImageMenuItem *image_menu_item;
235   
236   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);  
237   
238   (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
239
240   if (image_menu_item->image)
241     {
242       gint width, height, x, y;
243       GtkAllocation child_allocation;
244       
245       /* Man this is lame hardcoding action, but I can't
246        * come up with a solution that's really better.
247        */
248       
249       width = image_menu_item->image->requisition.width;
250       height = image_menu_item->image->requisition.height;
251
252       x = (GTK_CONTAINER (image_menu_item)->border_width +
253            widget->style->xthickness) +
254         (GTK_MENU_ITEM (image_menu_item)->toggle_size - width) / 2;
255       y = (widget->allocation.height - height) / 2;
256
257       child_allocation.width = width;
258       child_allocation.height = height;
259       child_allocation.x = widget->allocation.x + MAX (x, 0);
260       child_allocation.y = widget->allocation.y + MAX (y, 0);
261
262       gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
263     }
264 }
265
266 static void
267 gtk_image_menu_item_forall (GtkContainer   *container,
268                             gboolean        include_internals,
269                             GtkCallback     callback,
270                             gpointer        callback_data)
271 {
272   GtkImageMenuItem *image_menu_item;
273
274   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (container));
275
276   image_menu_item = GTK_IMAGE_MENU_ITEM (container);
277   
278   (* GTK_CONTAINER_CLASS (parent_class)->forall) (container,
279                                                   include_internals,
280                                                   callback,
281                                                   callback_data);
282
283   if (image_menu_item->image)
284     (* callback) (image_menu_item->image, callback_data);
285 }
286
287 /**
288  * gtk_image_menu_item_new:
289  * @returns: a new #GtkImageMenuItem.
290  *
291  * Creates a new #GtkImageMenuItem with an empty label.
292  **/
293 GtkWidget*
294 gtk_image_menu_item_new (void)
295 {
296   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
297 }
298
299 /**
300  * gtk_image_menu_item_new_with_label:
301  * @label: the text of the menu item.
302  * @returns: a new #GtkImageMenuItem.
303  *
304  * Creates a new #GtkImageMenuItem containing a label. 
305  **/
306 GtkWidget*
307 gtk_image_menu_item_new_with_label (const gchar *label)
308 {
309   GtkImageMenuItem *image_menu_item;
310   GtkWidget *accel_label;
311   
312   image_menu_item = GTK_IMAGE_MENU_ITEM (g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
313                                                        NULL));
314
315   accel_label = gtk_accel_label_new (label);
316   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
317
318   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
319   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
320                                     GTK_WIDGET (image_menu_item));
321   gtk_widget_show (accel_label);
322
323   return GTK_WIDGET(image_menu_item);
324 }
325
326
327 /**
328  * gtk_image_menu_item_new_with_mnemonic:
329  * @label: the text of the menu item, with an underscore in front of the
330  *         mnemonic character
331  * @returns: a new #GtkImageMenuItem
332  *
333  * Creates a new #GtkImageMenuItem containing a label. The label
334  * will be created using gtk_label_new_with_mnemonic(), so underscores
335  * in @label indicate the mnemonic for the menu item.
336  **/
337 GtkWidget*
338 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
339 {
340   GtkImageMenuItem *image_menu_item;
341   GtkWidget *accel_label;
342   
343   image_menu_item = GTK_IMAGE_MENU_ITEM (g_object_new (GTK_TYPE_IMAGE_MENU_ITEM,
344                                                        NULL));
345
346   accel_label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
347   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
348   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
349
350   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
351   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
352                                     GTK_WIDGET (image_menu_item));
353   gtk_widget_show (accel_label);
354
355   return GTK_WIDGET(image_menu_item);
356 }
357
358 /**
359  * gtk_image_menu_item_new_from_stock:
360  * @stock_id: the name of the stock item.
361  * @accel_group: the #GtkAccelGroup to add the menu items accelerator to.
362  * @returns: a new #GtkImageMenuItem.
363  *
364  * Creates a new #GtkImageMenuItem containing the image and text from a 
365  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK 
366  * and #GTK_STOCK_APPLY.
367  **/
368 GtkWidget*
369 gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
370                                     GtkAccelGroup    *accel_group)
371 {
372   GtkWidget *image;
373   GtkStockItem stock_item;
374   GtkWidget *item;
375
376   g_return_val_if_fail (stock_id != NULL, NULL);
377
378   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
379
380   if (gtk_stock_lookup (stock_id, &stock_item))
381     {
382       item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
383
384       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
385       
386       if (stock_item.keyval && accel_group)
387         gtk_widget_add_accelerator (item,
388                                     "activate",
389                                     accel_group,
390                                     stock_item.keyval,
391                                     stock_item.modifier,
392                                     GTK_ACCEL_VISIBLE);
393     }
394   else
395     {
396       item = gtk_image_menu_item_new_with_label (stock_id);
397
398       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
399     }
400
401   gtk_widget_show (image);
402   return item;
403 }
404
405 /** 
406  * gtk_image_menu_item_set_image:
407  * @image_menu_item: a #GtkImageMenuItem.
408  * @image: a widget to set as the image for the menu item.
409  * 
410  * Sets the image of @image_menu_item to the given widget.
411  **/ 
412 void
413 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
414                                GtkWidget        *image)
415 {
416   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
417
418   if (image == image_menu_item->image)
419     return;
420
421   if (image_menu_item->image)
422     gtk_container_remove (GTK_CONTAINER (image_menu_item),
423                           image_menu_item->image);
424
425   image_menu_item->image = image;
426
427   if (image == NULL)
428     return;
429
430   gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
431
432   g_object_notify (G_OBJECT (image_menu_item), "image");
433 }
434
435 /**
436  * gtk_image_menu_item_get_image:
437  * @image_menu_item: a #GtkImageMenuItem.
438  * @returns: the widget set as image of @image_menu_item.
439  *
440  * Gets the widget that is currently set as the image of @image_menu_item.
441  * See gtk_image_menu_item_set_image().
442  **/
443 GtkWidget*
444 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
445 {
446   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
447
448   return image_menu_item->image;
449 }
450
451 static void
452 gtk_image_menu_item_remove (GtkContainer *container,
453                             GtkWidget    *child)
454 {
455   GtkImageMenuItem *image_menu_item;
456
457   image_menu_item = GTK_IMAGE_MENU_ITEM (container);
458
459   if (child == image_menu_item->image)
460     {
461       gboolean widget_was_visible;
462       
463       widget_was_visible = GTK_WIDGET_VISIBLE (child);
464       
465       gtk_widget_unparent (child);
466       image_menu_item->image = NULL;
467       
468       if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
469         gtk_widget_queue_resize (GTK_WIDGET (container));
470
471       g_object_notify (G_OBJECT (image_menu_item), "image");
472     }
473   else
474     {
475       (* GTK_CONTAINER_CLASS (parent_class)->remove) (container, child);
476     }
477 }
478
479