]> Pileus Git - ~andy/gtk/blob - gtk/gtkimagemenuitem.c
Revert name change
[~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 #include "gtkimagemenuitem.h"
29 #include "gtkaccellabel.h"
30 #include "gtkintl.h"
31 #include "gtkstock.h"
32 #include "gtkiconfactory.h"
33 #include "gtkimage.h"
34 #include "gtkmenubar.h"
35 #include "gtkcontainer.h"
36 #include "gtkwindow.h"
37 #include "gtkprivate.h"
38 #include "gtkalias.h"
39
40 static void gtk_image_menu_item_size_request         (GtkWidget        *widget,
41                                                       GtkRequisition   *requisition);
42 static void gtk_image_menu_item_size_allocate        (GtkWidget        *widget,
43                                                       GtkAllocation    *allocation);
44 static void gtk_image_menu_item_remove               (GtkContainer          *container,
45                                                       GtkWidget             *child);
46 static void gtk_image_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
47                                                       gint                  *requisition);
48
49 static void gtk_image_menu_item_forall     (GtkContainer   *container,
50                                             gboolean        include_internals,
51                                             GtkCallback     callback,
52                                             gpointer        callback_data);
53
54 static void gtk_image_menu_item_set_property (GObject         *object,
55                                               guint            prop_id,
56                                               const GValue    *value,
57                                               GParamSpec      *pspec);
58 static void gtk_image_menu_item_get_property (GObject         *object,
59                                               guint            prop_id,
60                                               GValue          *value,
61                                               GParamSpec      *pspec);
62 static void gtk_image_menu_item_screen_changed (GtkWidget        *widget,
63                                                 GdkScreen        *previous_screen);
64
65
66 enum {
67   PROP_0,
68   PROP_IMAGE
69 };
70
71 G_DEFINE_TYPE (GtkImageMenuItem, gtk_image_menu_item, GTK_TYPE_MENU_ITEM)
72
73 static void
74 gtk_image_menu_item_class_init (GtkImageMenuItemClass *klass)
75 {
76   GObjectClass *gobject_class;
77   GtkWidgetClass *widget_class;
78   GtkMenuItemClass *menu_item_class;
79   GtkContainerClass *container_class;
80
81   gobject_class = (GObjectClass*) klass;
82   widget_class = (GtkWidgetClass*) klass;
83   menu_item_class = (GtkMenuItemClass*) klass;
84   container_class = (GtkContainerClass*) klass;
85   
86   widget_class->screen_changed = gtk_image_menu_item_screen_changed;
87   widget_class->size_request = gtk_image_menu_item_size_request;
88   widget_class->size_allocate = gtk_image_menu_item_size_allocate;
89
90   container_class->forall = gtk_image_menu_item_forall;
91   container_class->remove = gtk_image_menu_item_remove;
92   
93   menu_item_class->toggle_size_request = gtk_image_menu_item_toggle_size_request;
94
95   gobject_class->set_property = gtk_image_menu_item_set_property;
96   gobject_class->get_property = gtk_image_menu_item_get_property;
97   
98   g_object_class_install_property (gobject_class,
99                                    PROP_IMAGE,
100                                    g_param_spec_object ("image",
101                                                         P_("Image widget"),
102                                                         P_("Child widget to appear next to the menu text"),
103                                                         GTK_TYPE_WIDGET,
104                                                         GTK_PARAM_READWRITE));
105
106   gtk_settings_install_property (g_param_spec_boolean ("gtk-menu-images",
107                                                        P_("Show menu images"),
108                                                        P_("Whether images should be shown in menus"),
109                                                        TRUE,
110                                                        GTK_PARAM_READWRITE));
111   
112 }
113
114 static void
115 gtk_image_menu_item_init (GtkImageMenuItem *image_menu_item)
116 {
117   image_menu_item->image = NULL;
118 }
119
120 static void
121 gtk_image_menu_item_set_property (GObject         *object,
122                                   guint            prop_id,
123                                   const GValue    *value,
124                                   GParamSpec      *pspec)
125 {
126   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
127   
128   switch (prop_id)
129     {
130     case PROP_IMAGE:
131       {
132         GtkWidget *image;
133
134         image = (GtkWidget*) g_value_get_object (value);
135
136         gtk_image_menu_item_set_image (image_menu_item, image);
137       }
138       break;
139     default:
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141       break;
142     }
143 }
144 static void
145 gtk_image_menu_item_get_property (GObject         *object,
146                                   guint            prop_id,
147                                   GValue          *value,
148                                   GParamSpec      *pspec)
149 {
150   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (object);
151   
152   switch (prop_id)
153     {
154     case PROP_IMAGE:
155       g_value_set_object (value,
156                           (GObject*) image_menu_item->image);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161     }
162 }
163
164 static gboolean
165 show_image (GtkImageMenuItem *image_menu_item)
166 {
167   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (image_menu_item));
168   gboolean show;
169
170   g_object_get (settings, "gtk-menu-images", &show, NULL);
171
172   return show;
173 }
174
175 static void
176 gtk_image_menu_item_toggle_size_request (GtkMenuItem *menu_item,
177                                          gint        *requisition)
178 {
179   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (menu_item);
180   GtkPackDirection pack_dir;
181   
182   if (GTK_IS_MENU_BAR (GTK_WIDGET (menu_item)->parent))
183     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (GTK_WIDGET (menu_item)->parent));
184   else
185     pack_dir = GTK_PACK_DIRECTION_LTR;
186
187   *requisition = 0;
188
189   if (image_menu_item->image && show_image (image_menu_item))
190     {
191       GtkRequisition image_requisition;
192       guint toggle_spacing;
193       gtk_widget_get_child_requisition (image_menu_item->image,
194                                         &image_requisition);
195
196       gtk_widget_style_get (GTK_WIDGET (menu_item),
197                             "toggle-spacing", &toggle_spacing,
198                             NULL);
199       
200       if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
201         {
202           if (image_requisition.width > 0)
203             *requisition = image_requisition.width + toggle_spacing;
204         }
205       else
206         {
207           if (image_requisition.height > 0)
208             *requisition = image_requisition.height + toggle_spacing;
209         }
210     }
211 }
212
213
214 static void
215 gtk_image_menu_item_size_request (GtkWidget      *widget,
216                                   GtkRequisition *requisition)
217 {
218   GtkImageMenuItem *image_menu_item;
219   gint child_width = 0;
220   gint child_height = 0;
221   GtkPackDirection pack_dir;
222   
223   if (GTK_IS_MENU_BAR (widget->parent))
224     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
225   else
226     pack_dir = GTK_PACK_DIRECTION_LTR;
227
228   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);
229   
230   if (image_menu_item->image && 
231       GTK_WIDGET_VISIBLE (image_menu_item->image) && 
232       show_image (image_menu_item))
233     {
234       GtkRequisition child_requisition;
235       
236       gtk_widget_size_request (image_menu_item->image,
237                                &child_requisition);
238
239       child_width = child_requisition.width;
240       child_height = child_requisition.height;
241     }
242   
243   (* GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_request) (widget, requisition);
244
245   /* not done with height since that happens via the
246    * toggle_size_request
247    */
248   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
249     requisition->height = MAX (requisition->height, child_height);
250   else
251     requisition->width = MAX (requisition->width, child_width);
252     
253   
254   /* Note that GtkMenuShell always size requests before
255    * toggle_size_request, so toggle_size_request will be able to use
256    * image_menu_item->image->requisition
257    */
258 }
259
260 static void
261 gtk_image_menu_item_size_allocate (GtkWidget     *widget,
262                                    GtkAllocation *allocation)
263 {
264   GtkImageMenuItem *image_menu_item;
265   GtkPackDirection pack_dir;
266   
267   if (GTK_IS_MENU_BAR (widget->parent))
268     pack_dir = gtk_menu_bar_get_child_pack_direction (GTK_MENU_BAR (widget->parent));
269   else
270     pack_dir = GTK_PACK_DIRECTION_LTR;
271   
272   image_menu_item = GTK_IMAGE_MENU_ITEM (widget);  
273   
274   (* GTK_WIDGET_CLASS (gtk_image_menu_item_parent_class)->size_allocate) (widget, allocation);
275
276   if (image_menu_item->image && show_image (image_menu_item))
277     {
278       gint x, y, offset;
279       GtkRequisition child_requisition;
280       GtkAllocation child_allocation;
281       guint horizontal_padding, toggle_spacing;
282
283       gtk_widget_style_get (widget,
284                             "horizontal-padding", &horizontal_padding,
285                             "toggle-spacing", &toggle_spacing,
286                             NULL);
287       
288       /* Man this is lame hardcoding action, but I can't
289        * come up with a solution that's really better.
290        */
291
292       gtk_widget_get_child_requisition (image_menu_item->image,
293                                         &child_requisition);
294
295       if (pack_dir == GTK_PACK_DIRECTION_LTR ||
296           pack_dir == GTK_PACK_DIRECTION_RTL)
297         {
298           offset = GTK_CONTAINER (image_menu_item)->border_width +
299             widget->style->xthickness;
300           
301           if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
302               (pack_dir == GTK_PACK_DIRECTION_LTR))
303             x = offset + horizontal_padding +
304               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
305                toggle_spacing - child_requisition.width) / 2;
306           else
307             x = widget->allocation.width - offset - horizontal_padding -
308               GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
309               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
310                toggle_spacing - child_requisition.width) / 2;
311           
312           y = (widget->allocation.height - child_requisition.height) / 2;
313         }
314       else
315         {
316           offset = GTK_CONTAINER (image_menu_item)->border_width +
317             widget->style->ythickness;
318           
319           if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR) ==
320               (pack_dir == GTK_PACK_DIRECTION_TTB))
321             y = offset + horizontal_padding +
322               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
323                toggle_spacing - child_requisition.height) / 2;
324           else
325             y = widget->allocation.height - offset - horizontal_padding -
326               GTK_MENU_ITEM (image_menu_item)->toggle_size + toggle_spacing +
327               (GTK_MENU_ITEM (image_menu_item)->toggle_size -
328                toggle_spacing - child_requisition.height) / 2;
329
330           x = (widget->allocation.width - child_requisition.width) / 2;
331         }
332       
333       child_allocation.width = child_requisition.width;
334       child_allocation.height = child_requisition.height;
335       child_allocation.x = widget->allocation.x + MAX (x, 0);
336       child_allocation.y = widget->allocation.y + MAX (y, 0);
337
338       gtk_widget_size_allocate (image_menu_item->image, &child_allocation);
339     }
340 }
341
342 static void
343 gtk_image_menu_item_forall (GtkContainer   *container,
344                             gboolean        include_internals,
345                             GtkCallback     callback,
346                             gpointer        callback_data)
347 {
348   GtkImageMenuItem *image_menu_item = GTK_IMAGE_MENU_ITEM (container);
349   
350   (* GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->forall) (container,
351                                                                       include_internals,
352                                                                       callback,
353                                                                       callback_data);
354
355   if (image_menu_item->image)
356     (* callback) (image_menu_item->image, callback_data);
357 }
358
359 /**
360  * gtk_image_menu_item_new:
361  * @returns: a new #GtkImageMenuItem.
362  *
363  * Creates a new #GtkImageMenuItem with an empty label.
364  **/
365 GtkWidget*
366 gtk_image_menu_item_new (void)
367 {
368   return g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
369 }
370
371 /**
372  * gtk_image_menu_item_new_with_label:
373  * @label: the text of the menu item.
374  * @returns: a new #GtkImageMenuItem.
375  *
376  * Creates a new #GtkImageMenuItem containing a label. 
377  **/
378 GtkWidget*
379 gtk_image_menu_item_new_with_label (const gchar *label)
380 {
381   GtkImageMenuItem *image_menu_item;
382   GtkWidget *accel_label;
383   
384   image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
385
386   accel_label = gtk_accel_label_new (label);
387   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
388
389   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
390   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
391                                     GTK_WIDGET (image_menu_item));
392   gtk_widget_show (accel_label);
393
394   return GTK_WIDGET(image_menu_item);
395 }
396
397
398 /**
399  * gtk_image_menu_item_new_with_mnemonic:
400  * @label: the text of the menu item, with an underscore in front of the
401  *         mnemonic character
402  * @returns: a new #GtkImageMenuItem
403  *
404  * Creates a new #GtkImageMenuItem containing a label. The label
405  * will be created using gtk_label_new_with_mnemonic(), so underscores
406  * in @label indicate the mnemonic for the menu item.
407  **/
408 GtkWidget*
409 gtk_image_menu_item_new_with_mnemonic (const gchar *label)
410 {
411   GtkImageMenuItem *image_menu_item;
412   GtkWidget *accel_label;
413   
414   image_menu_item = g_object_new (GTK_TYPE_IMAGE_MENU_ITEM, NULL);
415
416   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
417   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
418   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
419
420   gtk_container_add (GTK_CONTAINER (image_menu_item), accel_label);
421   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label),
422                                     GTK_WIDGET (image_menu_item));
423   gtk_widget_show (accel_label);
424
425   return GTK_WIDGET(image_menu_item);
426 }
427
428 /**
429  * gtk_image_menu_item_new_from_stock:
430  * @stock_id: the name of the stock item.
431  * @accel_group: the #GtkAccelGroup to add the menu items accelerator to,
432  *   or %NULL.
433  * @returns: a new #GtkImageMenuItem.
434  *
435  * Creates a new #GtkImageMenuItem containing the image and text from a 
436  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK 
437  * and #GTK_STOCK_APPLY.
438  *
439  * If you want this menu item to have changeable accelerators, then pass in
440  * %NULL for accel_group. Next call gtk_menu_item_set_accel_path() with an
441  * appropriate path for the menu item, use gtk_stock_lookup() to look up the
442  * standard accelerator for the stock item, and if one is found, call
443  * gtk_accel_map_add_entry() to register it.
444  **/
445 GtkWidget*
446 gtk_image_menu_item_new_from_stock (const gchar      *stock_id,
447                                     GtkAccelGroup    *accel_group)
448 {
449   GtkWidget *image;
450   GtkStockItem stock_item;
451   GtkWidget *item;
452
453   g_return_val_if_fail (stock_id != NULL, NULL);
454
455   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
456
457   if (gtk_stock_lookup (stock_id, &stock_item))
458     {
459       item = gtk_image_menu_item_new_with_mnemonic (stock_item.label);
460
461       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
462       
463       if (stock_item.keyval && accel_group)
464         gtk_widget_add_accelerator (item,
465                                     "activate",
466                                     accel_group,
467                                     stock_item.keyval,
468                                     stock_item.modifier,
469                                     GTK_ACCEL_VISIBLE);
470     }
471   else
472     {
473       item = gtk_image_menu_item_new_with_mnemonic (stock_id);
474
475       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
476     }
477
478   gtk_widget_show (image);
479   return item;
480 }
481
482 /** 
483  * gtk_image_menu_item_set_image:
484  * @image_menu_item: a #GtkImageMenuItem.
485  * @image: a widget to set as the image for the menu item.
486  * 
487  * Sets the image of @image_menu_item to the given widget.
488  * Note that it depends on the show-menu-images setting whether
489  * the image will be displayed or not.
490  **/ 
491 void
492 gtk_image_menu_item_set_image (GtkImageMenuItem *image_menu_item,
493                                GtkWidget        *image)
494 {
495   g_return_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item));
496
497   if (image == image_menu_item->image)
498     return;
499
500   if (image_menu_item->image)
501     gtk_container_remove (GTK_CONTAINER (image_menu_item),
502                           image_menu_item->image);
503
504   image_menu_item->image = image;
505
506   if (image == NULL)
507     return;
508
509   gtk_widget_set_parent (image, GTK_WIDGET (image_menu_item));
510   g_object_set (image, 
511                 "visible", show_image (image_menu_item),
512                 "no-show-all", TRUE,
513                 NULL);
514
515   g_object_notify (G_OBJECT (image_menu_item), "image");
516 }
517
518 /**
519  * gtk_image_menu_item_get_image:
520  * @image_menu_item: a #GtkImageMenuItem.
521  * @returns: the widget set as image of @image_menu_item.
522  *
523  * Gets the widget that is currently set as the image of @image_menu_item.
524  * See gtk_image_menu_item_set_image().
525  **/
526 GtkWidget*
527 gtk_image_menu_item_get_image (GtkImageMenuItem *image_menu_item)
528 {
529   g_return_val_if_fail (GTK_IS_IMAGE_MENU_ITEM (image_menu_item), NULL);
530
531   return image_menu_item->image;
532 }
533
534 static void
535 gtk_image_menu_item_remove (GtkContainer *container,
536                             GtkWidget    *child)
537 {
538   GtkImageMenuItem *image_menu_item;
539
540   image_menu_item = GTK_IMAGE_MENU_ITEM (container);
541
542   if (child == image_menu_item->image)
543     {
544       gboolean widget_was_visible;
545       
546       widget_was_visible = GTK_WIDGET_VISIBLE (child);
547       
548       gtk_widget_unparent (child);
549       image_menu_item->image = NULL;
550       
551       if (GTK_WIDGET_VISIBLE (container) && widget_was_visible)
552         gtk_widget_queue_resize (GTK_WIDGET (container));
553
554       g_object_notify (G_OBJECT (image_menu_item), "image");
555     }
556   else
557     {
558       (* GTK_CONTAINER_CLASS (gtk_image_menu_item_parent_class)->remove) (container, child);
559     }
560 }
561
562 static void 
563 show_image_change_notify (GtkImageMenuItem *image_menu_item)
564 {
565   if (image_menu_item->image)
566     {
567       if (show_image (image_menu_item))
568         gtk_widget_show (image_menu_item->image);
569       else
570         gtk_widget_hide (image_menu_item->image);
571     }
572 }
573
574 static void
575 traverse_container (GtkWidget *widget,
576                     gpointer   data)
577 {
578   if (GTK_IS_IMAGE_MENU_ITEM (widget))
579     show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
580   else if (GTK_IS_CONTAINER (widget))
581     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
582 }
583
584 static void
585 gtk_image_menu_item_setting_changed (GtkSettings *settings)
586 {
587   GList *list, *l;
588
589   list = gtk_window_list_toplevels ();
590
591   for (l = list; l; l = l->next)
592     gtk_container_forall (GTK_CONTAINER (l->data), 
593                           traverse_container, NULL);
594
595   g_list_free (list);  
596 }
597
598 static void
599 gtk_image_menu_item_screen_changed (GtkWidget *widget,
600                                     GdkScreen *previous_screen)
601 {
602   GtkSettings *settings;
603   guint show_image_connection;
604
605   if (!gtk_widget_has_screen (widget))
606     return;
607
608   settings = gtk_widget_get_settings (widget);
609   
610   show_image_connection = 
611     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
612                                          "gtk-image-menu-item-connection"));
613   
614   if (show_image_connection)
615     return;
616
617   show_image_connection =
618     g_signal_connect (settings, "notify::gtk-menu-images",
619                       G_CALLBACK (gtk_image_menu_item_setting_changed), NULL);
620   g_object_set_data (G_OBJECT (settings), 
621                      I_("gtk-image-menu-item-connection"),
622                      GUINT_TO_POINTER (show_image_connection));
623
624   show_image_change_notify (GTK_IMAGE_MENU_ITEM (widget));
625 }
626
627 #define __GTK_IMAGE_MENU_ITEM_C__
628 #include "gtkaliasdef.c"