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