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