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