]> Pileus Git - ~andy/gtk/blob - gtk/gtkimagemenuitem.c
Merge branch 'kill-size-request'
[~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 "config.h"
28
29 #include "gtkimagemenuitem.h"
30
31 #include "gtkaccellabel.h"
32 #include "gtkstock.h"
33 #include "gtkiconfactory.h"
34 #include "gtkimage.h"
35 #include "gtkmenubar.h"
36 #include "gtkcontainer.h"
37 #include "gtkwindow.h"
38 #include "gtkactivatable.h"
39
40 #include "gtkintl.h"
41 #include "gtkprivate.h"
42
43
44
45 struct _GtkImageMenuItemPrivate
46 {
47   GtkWidget     *image;
48
49   gchar         *label;
50   guint          use_stock         : 1;
51   guint          always_show_image : 1;
52 };
53
54 enum {
55   PROP_0,
56   PROP_IMAGE,
57   PROP_USE_STOCK,
58   PROP_ACCEL_GROUP,
59   PROP_ALWAYS_SHOW_IMAGE
60 };
61
62 static GtkActivatableIface *parent_activatable_iface;
63
64 static void gtk_image_menu_item_destroy              (GtkWidget        *widget);
65 static void gtk_image_menu_item_get_preferred_width  (GtkWidget        *widget,
66                                                       gint             *minimum,
67                                                       gint             *natural);
68 static void gtk_image_menu_item_get_preferred_height (GtkWidget        *widget,
69                                                       gint             *minimum,
70                                                       gint             *natural);
71 static void gtk_image_menu_item_get_preferred_height_for_width (GtkWidget *widget,
72                                                                 gint       width,
73                                                                 gint      *minimum,
74                                                                 gint      *natural);
75 static void gtk_image_menu_item_size_allocate        (GtkWidget        *widget,
76                                                       GtkAllocation    *allocation);
77 static void gtk_image_menu_item_map                  (GtkWidget        *widget);
78 static void gtk_image_menu_item_remove               (GtkContainer     *container,
79                                                       GtkWidget        *child);
80 static void gtk_image_menu_item_toggle_size_request  (GtkMenuItem      *menu_item,
81                                                       gint             *requisition);
82 static void gtk_image_menu_item_set_label            (GtkMenuItem      *menu_item,
83                                                       const gchar      *label);
84 static G_CONST_RETURN gchar *gtk_image_menu_item_get_label (GtkMenuItem *menu_item);
85
86 static void gtk_image_menu_item_forall               (GtkContainer    *container,
87                                                       gboolean         include_internals,
88                                                       GtkCallback      callback,
89                                                       gpointer         callback_data);
90
91 static void gtk_image_menu_item_finalize             (GObject         *object);
92 static void gtk_image_menu_item_set_property         (GObject         *object,
93                                                       guint            prop_id,
94                                                       const GValue    *value,
95                                                       GParamSpec      *pspec);
96 static void gtk_image_menu_item_get_property         (GObject         *object,
97                                                       guint            prop_id,
98                                                       GValue          *value,
99                                                       GParamSpec      *pspec);
100 static void gtk_image_menu_item_screen_changed       (GtkWidget        *widget,
101                                                       GdkScreen        *previous_screen);
102
103 static void gtk_image_menu_item_recalculate          (GtkImageMenuItem *image_menu_item);
104
105 static void gtk_image_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
106 static void gtk_image_menu_item_update                     (GtkActivatable       *activatable,
107                                                             GtkAction            *action,
108                                                             const gchar          *property_name);
109 static void gtk_image_menu_item_sync_action_properties     (GtkActivatable       *activatable,
110                                                             GtkAction            *action);
111
112
113 G_DEFINE_TYPE_WITH_CODE (GtkImageMenuItem, gtk_image_menu_item, GTK_TYPE_MENU_ITEM,
114                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
115                                                 gtk_image_menu_item_activatable_interface_init))
116
117
118 static void
119 gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
120 {
121   GObjectClass *gobject_class = (GObjectClass*) klass;
122   GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
123   GtkMenuItemClass *menu_item_class = (GtkMenuItemClass*) klass;
124   GtkContainerClass *container_class = (GtkContainerClass*) klass;
125
126   widget_class->destroy = gtk_image_menu_item_destroy;
127   widget_class->screen_changed = gtk_image_menu_item_screen_changed;
128   widget_class->get_preferred_width = gtk_image_menu_item_get_preferred_width;
129   widget_class->get_preferred_height = gtk_image_menu_item_get_preferred_height;
130   widget_class->get_preferred_height_for_width = gtk_image_menu_item_get_preferred_height_for_width;
131   widget_class->size_allocate = gtk_image_menu_item_size_allocate;
132   widget_class->map = gtk_image_menu_item_map;
133
134   container_class->forall = gtk_image_menu_item_forall;
135   container_class->remove = gtk_image_menu_item_remove;
136   
137   menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
138   menu_item_class->set_label           = gtk_image_menu_item_set_label;
139   menu_item_class->get_label           = gtk_image_menu_item_get_label;
140
141   gobject_class->finalize     = gtk_image_menu_item_finalize;
142   gobject_class->set_property = gtk_image_menu_item_set_property;
143   gobject_class->get_property = gtk_image_menu_item_get_property;
144   
145   g_object_class_install_property (gobject_class,
146                                    PROP_IMAGE,
147                                    g_param_spec_object ("image",
148                                                         P_("Image widget"),
149                                                         P_("Child widget to appear next to the menu text"),
150                                                         GTK_TYPE_WIDGET,
151                                                         GTK_PARAM_READWRITE));
152   /**
153    * GtkImageMenuItem:use-stock:
154    *
155    * If %TRUE, the label set in the menuitem is used as a
156    * stock id to select the stock item for the item.
157    *
158    * Since: 2.16
159    **/
160   g_object_class_install_property (gobject_class,
161                                    PROP_USE_STOCK,
162                                    g_param_spec_boolean ("use-stock",
163                                                          P_("Use stock"),
164                                                          P_("Whether to use the label text to create a stock menu item"),
165                                                          FALSE,
166                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
167
168   /**
169    * GtkImageMenuItem:always-show-image:
170    *
171    * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images 
172    * setting and always show the image, if available.
173    *
174    * Use this property if the menuitem would be useless or hard to use
175    * without the image. 
176    *
177    * Since: 2.16
178    **/
179   g_object_class_install_property (gobject_class,
180                                    PROP_ALWAYS_SHOW_IMAGE,
181                                    g_param_spec_boolean ("always-show-image",
182                                                          P_("Always show image"),
183                                                          P_("Whether the image will always be shown"),
184                                                          FALSE,
185                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
186
187   /**
188    * GtkImageMenuItem:accel-group:
189    *
190    * The Accel Group to use for stock accelerator keys
191    *
192    * Since: 2.16
193    **/
194   g_object_class_install_property (gobject_class,
195                                    PROP_ACCEL_GROUP,
196                                    g_param_spec_object ("accel-group",
197                                                         P_("Accel Group"),
198                                                         P_("The Accel Group to use for stock accelerator keys"),
199                                                         GTK_TYPE_ACCEL_GROUP,
200                                                         GTK_PARAM_WRITABLE));
201
202     g_type_class_add_private (klass, sizeof (GtkImageMenuItemPrivate));
203 }
204
205 static void
206 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
207 {
208   GtkImageMenuItemPrivate *priv;
209
210   image_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (image_menu_item,
211                                                        GTK_TYPE_IMAGE_MENU_ITEM,
212                                                        GtkImageMenuItemPrivate);
213   priv = image_menu_item->priv;
214
215   priv->image = NULL;
216   priv->use_stock = FALSE;
217   priv->label  = NULL;
218 }
219
220 static void 
221 gtk_image_menu_item_finalize (GObject *object)
222 {
223   GtkImageMenuItemPrivate *priv = GTK_IMAGE_MENU_ITEM (object)->priv;
224
225   g_free (priv->label);
226   priv->label  = NULL;
227
228   G_OBJECT_CLASS (gtk_image_menu_item_parent_class)->finalize (object);
229 }
230
231 static void
232 gtk_image_menu_item_set_property (GObject         *object,
233                                   guint            prop_id,
234                                   const GValue    *value,
235                                   GParamSpec      *pspec)
236 {
237   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
238   
239   switch (prop_id)
240     {
241     case PROP_IMAGE:
242       gtk_image_menu_item_set_image (image_menu_item, (GtkWidget *) g_value_get_object (value));
243       break;
244     case PROP_USE_STOCK:
245       gtk_image_menu_item_set_use_stock (image_menu_item, g_value_get_boolean (value));
246       break;
247     case PROP_ALWAYS_SHOW_IMAGE:
248       gtk_image_menu_item_set_always_show_image (image_menu_item, g_value_get_boolean (value));
249       break;
250     case PROP_ACCEL_GROUP:
251       gtk_image_menu_item_set_accel_group (image_menu_item, g_value_get_object (value));
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256     }
257 }
258
259 static void
260 gtk_image_menu_item_get_property (GObject         *object,
261                                   guint            prop_id,
262                                   GValue          *value,
263                                   GParamSpec      *pspec)
264 {
265   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
266   
267   switch (prop_id)
268     {
269     case PROP_IMAGE:
270       g_value_set_object (value, gtk_image_menu_item_get_image (image_menu_item));
271       break;
272     case PROP_USE_STOCK:
273       g_value_set_boolean (value, gtk_image_menu_item_get_use_stock (image_menu_item));      
274       break;
275     case PROP_ALWAYS_SHOW_IMAGE:
276       g_value_set_boolean (value, gtk_image_menu_item_get_always_show_image (image_menu_item));
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281     }
282 }
283
284 static gboolean
285 show_image (GtkImageMenuItem *image_menu_item)
286 {
287   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
288   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
289   gboolean show;
290
291   if (priv->always_show_image)
292     show = TRUE;
293   else
294     g_object_get (settings, "gtk-menu-images", &show, NULL);
295
296   return show;
297 }
298
299 static void
300 gtk_image_menu_item_map (GtkWidget *widget)
301 {
302   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
303   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
304
305   GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->map (widget);
306
307   if (priv->image)
308     g_object_set (priv->image,
309                   "visible", show_image (image_menu_item),
310                   NULL);
311 }
312
313 static void
314 gtk_image_menu_item_destroy (GtkWidget *widget)
315 {
316   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
317   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
318
319   if (priv->image)
320     gtk_container_remove (GTK_CONTAINER (image_menu_item),
321                           priv->image);
322
323   GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->destroy (widget);
324 }
325
326 static void
327 gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
328                                          gint        *requisition)
329 {
330   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
331   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
332   GtkPackDirection pack_dir;
333   GtkWidget *parent;
334   GtkWidget *widget = GTK_WIDGET (menu_item);
335
336   parent = gtk_widget_get_parent (widget);
337
338   if (GTK_IS_MENU_BAR (parent))
339     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
340   else
341     pack_dir = GTK_PACK_DIRECTION_LTR;
342
343   *requisition = 0;
344
345   if (priv->image && gtk_widget_get_visible (priv->image))
346     {
347       GtkRequisition image_requisition;
348       guint toggle_spacing;
349
350       gtk_widget_get_preferred_size (priv->image, &image_requisition, NULL);
351
352       gtk_widget_style_get (GTK_WIDGET (menu_item),
353                             "toggle-spacing", &toggle_spacing,
354                             NULL);
355       
356       if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
357         {
358           if (image_requisition.width > 0)
359             *requisition = image_requisition.width + toggle_spacing;
360         }
361       else
362         {
363           if (image_requisition.height > 0)
364             *requisition = image_requisition.height + toggle_spacing;
365         }
366     }
367 }
368
369 static void
370 gtk_image_menu_item_recalculate (GtkImageMenuItem *image_menu_item)
371 {
372   GtkImageMenuItemPrivate    *priv = image_menu_item->priv;
373   GtkStockItem             stock_item;
374   GtkWidget               *image;
375   const gchar             *resolved_label = priv->label;
376
377   if (priv->use_stock && priv->label)
378     {
379
380       if (!priv->image)
381         {
382           image = gtk_image_new_from_stock (priv->label, GTK_ICON_SIZE_MENU);
383           gtk_image_menu_item_set_image (image_menu_item, image);
384         }
385
386       if (gtk_stock_lookup (priv->label, &stock_item))
387           resolved_label = stock_item.label;
388
389         gtk_menu_item_set_use_underline (GTK_MENU_ITEM (image_menu_item), TRUE);
390     }
391
392   GTK_MENU_ITEM_CLASS
393     (gtk_image_menu_item_parent_class)->set_label (GTK_MENU_ITEM (image_menu_item), resolved_label);
394
395 }
396
397 static void 
398 gtk_image_menu_item_set_label (GtkMenuItem      *menu_item,
399                                const gchar      *label)
400 {
401   GtkImageMenuItemPrivate *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
402
403   if (priv->label != label)
404     {
405       g_free (priv->label);
406       priv->label = g_strdup (label);
407
408       gtk_image_menu_item_recalculate (GTK_IMAGE_MENU_ITEM (menu_item));
409
410       g_object_notify (G_OBJECT (menu_item), "label");
411
412     }
413 }
414
415 static G_CONST_RETURN gchar *
416 gtk_image_menu_item_get_label (GtkMenuItem *menu_item)
417 {
418   GtkImageMenuItemPrivate *priv = GTK_IMAGE_MENU_ITEM (menu_item)->priv;
419
420   return priv->label;
421 }
422
423 static void
424 gtk_image_menu_item_get_preferred_width (GtkWidget        *widget,
425                                          gint             *minimum,
426                                          gint             *natural)
427 {
428   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
429   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
430   gint child_width = 0;
431   GtkPackDirection pack_dir;
432   GtkWidget *parent;
433
434   parent = gtk_widget_get_parent (widget);
435
436   if (GTK_IS_MENU_BAR (parent))
437     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
438   else
439     pack_dir = GTK_PACK_DIRECTION_LTR;
440
441   if (priv->image && gtk_widget_get_visible (priv->image))
442     {
443       GtkRequisition child_requisition;
444
445       gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
446
447       child_width = child_requisition.width;
448     }
449
450   GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->get_preferred_width (widget, minimum, natural);
451
452   if (pack_dir == GTK_PACK_DIRECTION_TTB || pack_dir == GTK_PACK_DIRECTION_BTT)
453     {
454       *minimum = MAX (*minimum, child_width);
455       *natural = MAX (*natural, child_width);
456     }
457 }
458
459 static void
460 gtk_image_menu_item_get_preferred_height (GtkWidget        *widget,
461                                           gint             *minimum,
462                                           gint             *natural)
463 {
464   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
465   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
466   gint child_height = 0;
467   GtkPackDirection pack_dir;
468   GtkWidget *parent;
469
470   parent = gtk_widget_get_parent (widget);
471
472   if (GTK_IS_MENU_BAR (parent))
473     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
474   else
475     pack_dir = GTK_PACK_DIRECTION_LTR;
476
477   if (priv->image && gtk_widget_get_visible (priv->image))
478     {
479       GtkRequisition child_requisition;
480
481       gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
482
483       child_height = child_requisition.height;
484     }
485
486   GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->get_preferred_height (widget, minimum, natural);
487
488   if (pack_dir == GTK_PACK_DIRECTION_RTL || pack_dir == GTK_PACK_DIRECTION_LTR)
489     {
490       *minimum = MAX (*minimum, child_height);
491       *natural = MAX (*natural, child_height);
492     }
493 }
494
495 static void
496 gtk_image_menu_item_get_preferred_height_for_width (GtkWidget        *widget,
497                                                     gint              width,
498                                                     gint             *minimum,
499                                                     gint             *natural)
500 {
501   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
502   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
503   gint child_height = 0;
504   GtkPackDirection pack_dir;
505   GtkWidget *parent;
506
507   parent = gtk_widget_get_parent (widget);
508
509   if (GTK_IS_MENU_BAR (parent))
510     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
511   else
512     pack_dir = GTK_PACK_DIRECTION_LTR;
513
514   if (priv->image && gtk_widget_get_visible (priv->image))
515     {
516       GtkRequisition child_requisition;
517
518       gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
519
520       child_height = child_requisition.height;
521     }
522
523   GTK_WIDGET_CLASS
524     (gtk_image_menu_item_parent_class)->get_preferred_height_for_width (widget, width, minimum, natural);
525
526   if (pack_dir == GTK_PACK_DIRECTION_RTL || pack_dir == GTK_PACK_DIRECTION_LTR)
527     {
528       *minimum = MAX (*minimum, child_height);
529       *natural = MAX (*natural, child_height);
530     }
531 }
532
533
534 static void
535 gtk_image_menu_item_size_allocate (GtkWidget     *widget,
536                                    GtkAllocation *allocation)
537 {
538   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
539   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
540   GtkAllocation widget_allocation;
541   GtkPackDirection pack_dir;
542   GtkWidget *parent;
543
544   parent = gtk_widget_get_parent (widget);
545
546   if (GTK_IS_MENU_BAR (parent))
547     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (parent));
548   else
549     pack_dir = GTK_PACK_DIRECTION_LTR;
550
551   GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_allocate (widget, allocation);
552
553   if (priv->image && gtk_widget_get_visible (priv->image))
554     {
555       gint x, y, offset;
556       GtkRequisition child_requisition;
557       GtkAllocation child_allocation;
558       guint horizontal_padding, toggle_spacing;
559
560       gtk_widget_style_get (widget,
561                             "horizontal-padding", &horizontal_padding,
562                             "toggle-spacing", &toggle_spacing,
563                             NULL);
564       
565       /* Man this is lame hardcoding action, but I can't
566        * come up with a solution that's really better.
567        */
568
569       gtk_widget_get_preferred_size (priv->image, &child_requisition, NULL);
570
571       gtk_widget_get_allocation (widget, &widget_allocation);
572
573       if (pack_dir == GTK_PACK_DIRECTION_LTR ||
574           pack_dir == GTK_PACK_DIRECTION_RTL)
575         {
576           offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
577                    gtk_widget_get_style (widget)->xthickness;
578
579           if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
580               (pack_dir == GTK_PACK_DIRECTION_LTR))
581             x = offset + horizontal_padding +
582               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
583                toggle_spacing - child_requisition.width) / 2;
584           else
585             x = widget_allocation.width - offset - horizontal_padding -
586               GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
587               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
588                toggle_spacing - child_requisition.width) / 2;
589
590           y = (widget_allocation.height - child_requisition.height) / 2;
591         }
592       else
593         {
594           offset = gtk_container_get_border_width (GTK_CONTAINER (image_menu_item)) +
595                    gtk_widget_get_style (widget)->ythickness;
596
597           if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
598               (pack_dir == GTK_PACK_DIRECTION_TTB))
599             y = offset + horizontal_padding +
600               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
601                toggle_spacing - child_requisition.height) / 2;
602           else
603             y = widget_allocation.height - offset - horizontal_padding -
604               GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
605               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
606                toggle_spacing - child_requisition.height) / 2;
607
608           x = (widget_allocation.width - child_requisition.width) / 2;
609         }
610       
611       child_allocation.width = child_requisition.width;
612       child_allocation.height = child_requisition.height;
613       child_allocation.x = widget_allocation.x + MAX (x, 0);
614       child_allocation.y = widget_allocation.y + MAX (y, 0);
615
616       gtk_widget_size_allocate (priv->image, &child_allocation);
617     }
618 }
619
620 static void
621 gtk_image_menu_item_forall (GtkContainer   *container,
622                             gboolean        include_internals,
623                             GtkCallback     callback,
624                             gpointer        callback_data)
625 {
626   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
627   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
628
629   GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->forall (container,
630                                                                   include_internals,
631                                                                   callback,
632                                                                   callback_data);
633
634   if (include_internals && priv->image)
635     (* callback) (priv->image, callback_data);
636 }
637
638
639 static void 
640 gtk_image_menu_item_activatable_interface_init (GtkActivatableIface  *iface)
641 {
642   parent_activatable_iface = g_type_interface_peek_parent (iface);
643   iface->update = gtk_image_menu_item_update;
644   iface->sync_action_properties = gtk_image_menu_item_sync_action_properties;
645 }
646
647 static gboolean
648 activatable_update_stock_id (GtkImageMenuItem *image_menu_item, GtkAction *action)
649 {
650   GtkWidget   *image;
651   const gchar *stock_id  = gtk_action_get_stock_id (action);
652
653   image = gtk_image_menu_item_get_image (image_menu_item);
654           
655   if (GTK_IS_IMAGE (image) &&
656       stock_id && gtk_icon_factory_lookup_default (stock_id))
657     {
658       gtk_image_set_from_stock (GTK_IMAGE (image), stock_id, GTK_ICON_SIZE_MENU);
659       return TRUE;
660     }
661
662   return FALSE;
663 }
664
665 static gboolean
666 activatable_update_gicon (GtkImageMenuItem *image_menu_item, GtkAction *action)
667 {
668   GtkWidget   *image;
669   GIcon       *icon = gtk_action_get_gicon (action);
670   const gchar *stock_id = gtk_action_get_stock_id (action);
671
672   image = gtk_image_menu_item_get_image (image_menu_item);
673
674   if (icon && GTK_IS_IMAGE (image) &&
675       !(stock_id && gtk_icon_factory_lookup_default (stock_id)))
676     {
677       gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_MENU);
678       return TRUE;
679     }
680
681   return FALSE;
682 }
683
684 static void
685 activatable_update_icon_name (GtkImageMenuItem *image_menu_item, GtkAction *action)
686 {
687   GtkWidget   *image;
688   const gchar *icon_name = gtk_action_get_icon_name (action);
689
690   image = gtk_image_menu_item_get_image (image_menu_item);
691           
692   if (GTK_IS_IMAGE (image) && 
693       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
694        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
695     {
696       gtk_image_set_from_icon_name (GTK_IMAGE (image), icon_name, GTK_ICON_SIZE_MENU);
697     }
698 }
699
700 static void
701 gtk_image_menu_item_update (GtkActivatable *activatable,
702                             GtkAction      *action,
703                             const gchar    *property_name)
704 {
705   GtkImageMenuItem *image_menu_item;
706   gboolean   use_appearance;
707
708   image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);
709
710   parent_activatable_iface->update (activatable, action, property_name);
711
712   use_appearance = gtk_activatable_get_use_action_appearance (activatable);
713   if (!use_appearance)
714     return;
715
716   if (strcmp (property_name, "stock-id") == 0)
717     activatable_update_stock_id (image_menu_item, action);
718   else if (strcmp (property_name, "gicon") == 0)
719     activatable_update_gicon (image_menu_item, action);
720   else if (strcmp (property_name, "icon-name") == 0)
721     activatable_update_icon_name (image_menu_item, action);
722 }
723
724 static void 
725 gtk_image_menu_item_sync_action_properties (GtkActivatable *activatable,
726                                             GtkAction      *action)
727 {
728   GtkImageMenuItem *image_menu_item;
729   GtkWidget *image;
730   gboolean   use_appearance;
731
732   image_menu_item = GTK_IMAGE_MENU_ITEM (activatable);
733
734   parent_activatable_iface->sync_action_properties (activatable, action);
735
736   if (!action)
737     return;
738
739   use_appearance = gtk_activatable_get_use_action_appearance (activatable);
740   if (!use_appearance)
741     return;
742
743   image = gtk_image_menu_item_get_image (image_menu_item);
744   if (image && !GTK_IS_IMAGE (image))
745     {
746       gtk_image_menu_item_set_image (image_menu_item, NULL);
747       image = NULL;
748     }
749   
750   if (!image)
751     {
752       image = gtk_image_new ();
753       gtk_widget_show (image);
754       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (activatable),
755                                      image);
756     }
757   
758   if (!activatable_update_stock_id (image_menu_item, action) &&
759       !activatable_update_gicon (image_menu_item, action))
760     activatable_update_icon_name (image_menu_item, action);
761
762   gtk_image_menu_item_set_always_show_image (image_menu_item,
763                                              gtk_action_get_always_show_image (action));
764 }
765
766
767 /**
768  * gtk_image_menu_item_new:
769  * @returns: a new #GtkImageMenuItem.
770  *
771  * Creates a new #GtkImageMenuItem with an empty label.
772  **/
773 GtkWidget*
774 gtk_image_menu_item_new (void)
775 {
776   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
777 }
778
779 /**
780  * gtk_image_menu_item_new_with_label:
781  * @label: the text of the menu item.
782  * @returns: a new #GtkImageMenuItem.
783  *
784  * Creates a new #GtkImageMenuItem containing a label. 
785  **/
786 GtkWidget*
787 gtk_image_menu_item_new_with_label (const gchar *label)
788 {
789   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, 
790                        "label", label,
791                        NULL);
792 }
793
794
795 /**
796  * gtk_image_menu_item_new_with_mnemonic:
797  * @label: the text of the menu item, with an underscore in front of the
798  *         mnemonic character
799  * @returns: a new #GtkImageMenuItem
800  *
801  * Creates a new #GtkImageMenuItem containing a label. The label
802  * will be created using gtk_label_new_with_mnemonic(), so underscores
803  * in @label indicate the mnemonic for the menu item.
804  **/
805 GtkWidget*
806 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
807 {
808   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, 
809                        "use-underline", TRUE,
810                        "label", label,
811                        NULL);
812 }
813
814 /**
815  * gtk_image_menu_item_new_from_stock:
816  * @stock_id: the name of the stock item.
817  * @accel_group: (allow-none): the #GtkAccelGroup to add the menu items 
818  *   accelerator to, or %NULL.
819  * @returns: a new #GtkImageMenuItem.
820  *
821  * Creates a new #GtkImageMenuItem containing the image and text from a 
822  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK 
823  * and #GTK_STOCK_APPLY.
824  *
825  * If you want this menu item to have changeable accelerators, then pass in
826  * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
827  * appropriate path for the menu item, use gtk_stock_lookup() to look up the
828  * standard accelerator for the stock item, and if one is found, call
829  * gtk_accel_map_add_entry() to register it.
830  **/
831 GtkWidget*
832 gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
833                                     GtkAccelGroup    *accel_group)
834 {
835   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, 
836                        "label", stock_id,
837                        "use-stock", TRUE,
838                        "accel-group", accel_group,
839                        NULL);
840 }
841
842 /**
843  * gtk_image_menu_item_set_use_stock:
844  * @image_menu_item: a #GtkImageMenuItem
845  * @use_stock: %TRUE if the menuitem should use a stock item
846  *
847  * If %TRUE, the label set in the menuitem is used as a
848  * stock id to select the stock item for the item.
849  *
850  * Since: 2.16
851  */
852 void
853 gtk_image_menu_item_set_use_stock (GtkImageMenuItem *image_menu_item,
854                                    gboolean          use_stock)
855 {
856   GtkImageMenuItemPrivate *priv;
857
858   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
859
860   priv = image_menu_item->priv;
861
862   if (priv->use_stock != use_stock)
863     {
864       priv->use_stock = use_stock;
865
866       gtk_image_menu_item_recalculate (image_menu_item);
867
868       g_object_notify (G_OBJECT (image_menu_item), "use-stock");
869     }
870 }
871
872 /**
873  * gtk_image_menu_item_get_use_stock:
874  * @image_menu_item: a #GtkImageMenuItem
875  *
876  * Checks whether the label set in the menuitem is used as a
877  * stock id to select the stock item for the item.
878  *
879  * Returns: %TRUE if the label set in the menuitem is used as a
880  *     stock id to select the stock item for the item
881  *
882  * Since: 2.16
883  */
884 gboolean
885 gtk_image_menu_item_get_use_stock (GtkImageMenuItem *image_menu_item)
886 {
887   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
888
889   return image_menu_item->priv->use_stock;
890 }
891
892 /**
893  * gtk_image_menu_item_set_always_show_image:
894  * @image_menu_item: a #GtkImageMenuItem
895  * @always_show: %TRUE if the menuitem should always show the image
896  *
897  * If %TRUE, the menu item will ignore the #GtkSettings:gtk-menu-images 
898  * setting and always show the image, if available.
899  *
900  * Use this property if the menuitem would be useless or hard to use
901  * without the image. 
902  * 
903  * Since: 2.16
904  */
905 void
906 gtk_image_menu_item_set_always_show_image (GtkImageMenuItem *image_menu_item,
907                                            gboolean          always_show)
908 {
909   GtkImageMenuItemPrivate *priv;
910
911   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
912
913   priv = image_menu_item->priv;
914
915   if (priv->always_show_image != always_show)
916     {
917       priv->always_show_image = always_show;
918
919       if (priv->image)
920         {
921           if (show_image (image_menu_item))
922             gtk_widget_show (priv->image);
923           else
924             gtk_widget_hide (priv->image);
925         }
926
927       g_object_notify (G_OBJECT (image_menu_item), "always-show-image");
928     }
929 }
930
931 /**
932  * gtk_image_menu_item_get_always_show_image:
933  * @image_menu_item: a #GtkImageMenuItem
934  *
935  * Returns whether the menu item will ignore the #GtkSettings:gtk-menu-images
936  * setting and always show the image, if available.
937  * 
938  * Returns: %TRUE if the menu item will always show the image
939  *
940  * Since: 2.16
941  */
942 gboolean
943 gtk_image_menu_item_get_always_show_image (GtkImageMenuItem *image_menu_item)
944 {
945   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), FALSE);
946
947   return image_menu_item->priv->always_show_image;
948 }
949
950
951 /**
952  * gtk_image_menu_item_set_accel_group:
953  * @image_menu_item: a #GtkImageMenuItem
954  * @accel_group: the #GtkAccelGroup
955  *
956  * Specifies an @accel_group to add the menu items accelerator to
957  * (this only applies to stock items so a stock item must already
958  * be set, make sure to call gtk_image_menu_item_set_use_stock()
959  * and gtk_menu_item_set_label() with a valid stock item first).
960  *
961  * If you want this menu item to have changeable accelerators then
962  * you shouldnt need this (see gtk_image_menu_item_new_from_stock()).
963  *
964  * Since: 2.16
965  */
966 void
967 gtk_image_menu_item_set_accel_group (GtkImageMenuItem *image_menu_item, 
968                                      GtkAccelGroup    *accel_group)
969 {
970   GtkImageMenuItemPrivate    *priv;
971   GtkStockItem             stock_item;
972
973   /* Silent return for the constructor */
974   if (!accel_group) 
975     return;
976   
977   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
978   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
979
980   priv = image_menu_item->priv;
981
982   if (priv->use_stock && priv->label && gtk_stock_lookup (priv->label, &stock_item))
983     if (stock_item.keyval)
984       {
985         gtk_widget_add_accelerator (GTK_WIDGET (image_menu_item),
986                                     "activate",
987                                     accel_group,
988                                     stock_item.keyval,
989                                     stock_item.modifier,
990                                     GTK_ACCEL_VISIBLE);
991         
992         g_object_notify (G_OBJECT (image_menu_item), "accel-group");
993       }
994 }
995
996 /** 
997  * gtk_image_menu_item_set_image:
998  * @image_menu_item: a #GtkImageMenuItem.
999  * @image: (allow-none): a widget to set as the image for the menu item.
1000  *
1001  * Sets the image of @image_menu_item to the given widget.
1002  * Note that it depends on the show-menu-images setting whether
1003  * the image will be displayed or not.
1004  **/ 
1005 void
1006 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
1007                                GtkWidget        *image)
1008 {
1009   GtkImageMenuItemPrivate *priv;
1010
1011   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
1012
1013   priv = image_menu_item->priv;
1014
1015   if (image == priv->image)
1016     return;
1017
1018   if (priv->image)
1019     gtk_container_remove (GTK_CONTAINER (image_menu_item),
1020                           priv->image);
1021
1022   priv->image = image;
1023
1024   if (image == NULL)
1025     return;
1026
1027   gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
1028   g_object_set (image,
1029                 "visible", show_image (image_menu_item),
1030                 "no-show-all", TRUE,
1031                 NULL);
1032
1033   g_object_notify (G_OBJECT (image_menu_item), "image");
1034 }
1035
1036 /**
1037  * gtk_image_menu_item_get_image:
1038  * @image_menu_item: a #GtkImageMenuItem
1039  *
1040  * Gets the widget that is currently set as the image of @image_menu_item.
1041  * See gtk_image_menu_item_set_image().
1042  *
1043  * Return value: (transfer none): the widget set as image of @image_menu_item
1044  **/
1045 GtkWidget*
1046 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
1047 {
1048   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
1049
1050   return image_menu_item->priv->image;
1051 }
1052
1053 static void
1054 gtk_image_menu_item_remove (GtkContainer *container,
1055                             GtkWidget    *child)
1056 {
1057   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
1058   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
1059
1060   if (child == priv->image)
1061     {
1062       gboolean widget_was_visible;
1063       
1064       widget_was_visible = gtk_widget_get_visible (child);
1065       
1066       gtk_widget_unparent (child);
1067       priv->image = NULL;
1068
1069       if (widget_was_visible &&
1070           gtk_widget_get_visible (GTK_WIDGET (container)))
1071         gtk_widget_queue_resize (GTK_WIDGET (container));
1072
1073       g_object_notify (G_OBJECT (image_menu_item), "image");
1074     }
1075   else
1076     {
1077       GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->remove (container, child);
1078     }
1079 }
1080
1081 static void 
1082 show_image_change_notify (GtkImageMenuItem *image_menu_item)
1083 {
1084   GtkImageMenuItemPrivate *priv = image_menu_item->priv;
1085
1086   if (priv->image)
1087     {
1088       if (show_image (image_menu_item))
1089         gtk_widget_show (priv->image);
1090       else
1091         gtk_widget_hide (priv->image);
1092     }
1093 }
1094
1095 static void
1096 traverse_container (GtkWidget *widget,
1097                     gpointer   data)
1098 {
1099   if (GTK_IS_IMAGE_MENU_ITEM (widget))
1100     show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
1101   else if (GTK_IS_CONTAINER (widget))
1102     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
1103 }
1104
1105 static void
1106 gtk_image_menu_item_setting_changed (GtkSettings *settings)
1107 {
1108   GList *list, *l;
1109
1110   list = gtk_window_list_toplevels ();
1111
1112   for (l = list; l; l = l->next)
1113     gtk_container_forall (GTK_CONTAINER (l->data), 
1114                           traverse_container, NULL);
1115
1116   g_list_free (list);  
1117 }
1118
1119 static void
1120 gtk_image_menu_item_screen_changed (GtkWidget *widget,
1121                                     GdkScreen *previous_screen)
1122 {
1123   GtkSettings *settings;
1124   gulong show_image_connection;
1125
1126   if (!gtk_widget_has_screen (widget))
1127     return;
1128
1129   settings = gtk_widget_get_settings (widget);
1130   
1131   show_image_connection = 
1132     g_signal_handler_find (settings, G_SIGNAL_MATCH_FUNC, 0, 0,
1133                            NULL, gtk_image_menu_item_setting_changed, NULL);
1134   
1135   if (show_image_connection)
1136     return;
1137
1138   g_signal_connect (settings, "notify::gtk-menu-images",
1139                     G_CALLBACK (gtk_image_menu_item_setting_changed), NULL);
1140
1141   show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
1142 }