]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckmenuitem.c
The first part of the fix for #114351 (see also gdk-pixbuf/ChangeLog and
[~andy/gtk] / gtk / gtkcheckmenuitem.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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-2001.  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 "gtkcheckmenuitem.h"
28 #include "gtkaccellabel.h"
29 #include "gtkintl.h"
30 #include "gtkmarshalers.h"
31
32 #define CHECK_TOGGLE_SIZE 12
33
34 enum {
35   TOGGLED,
36   LAST_SIGNAL
37 };
38
39 enum {
40   PROP_0,
41   PROP_ACTIVE,
42   PROP_INCONSISTENT,
43   PROP_DRAW_AS_RADIO
44 };
45
46 static void gtk_check_menu_item_class_init           (GtkCheckMenuItemClass *klass);
47 static void gtk_check_menu_item_init                 (GtkCheckMenuItem      *check_menu_item);
48 static gint gtk_check_menu_item_expose               (GtkWidget             *widget,
49                                                       GdkEventExpose        *event);
50 static void gtk_check_menu_item_activate             (GtkMenuItem           *menu_item);
51 static void gtk_check_menu_item_toggle_size_request  (GtkMenuItem           *menu_item,
52                                                       gint                  *requisition);
53 static void gtk_check_menu_item_draw_indicator       (GtkCheckMenuItem      *check_menu_item,
54                                                       GdkRectangle          *area);
55 static void gtk_real_check_menu_item_draw_indicator  (GtkCheckMenuItem      *check_menu_item,
56                                                       GdkRectangle          *area);
57 static void gtk_check_menu_item_set_property (GObject         *object,
58                                               guint            prop_id,
59                                               const GValue    *value,
60                                               GParamSpec      *pspec);
61 static void gtk_check_menu_item_get_property (GObject         *object,
62                                               guint            prop_id,
63                                               GValue          *value,
64                                               GParamSpec      *pspec);
65
66
67 static GtkMenuItemClass *parent_class = NULL;
68 static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
69
70
71 GType
72 gtk_check_menu_item_get_type (void)
73 {
74   static GType check_menu_item_type = 0;
75
76   if (!check_menu_item_type)
77     {
78       static const GTypeInfo check_menu_item_info =
79       {
80         sizeof (GtkCheckMenuItemClass),
81         NULL,           /* base_init */
82         NULL,           /* base_finalize */
83         (GClassInitFunc) gtk_check_menu_item_class_init,
84         NULL,           /* class_finalize */
85         NULL,           /* class_data */
86         sizeof (GtkCheckMenuItem),
87         0,              /* n_preallocs */
88         (GInstanceInitFunc) gtk_check_menu_item_init,
89       };
90
91       check_menu_item_type =
92         g_type_register_static (GTK_TYPE_MENU_ITEM, "GtkCheckMenuItem",
93                                 &check_menu_item_info, 0);
94     }
95
96   return check_menu_item_type;
97 }
98
99 static void
100 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
101 {
102   GObjectClass *gobject_class;
103   GtkWidgetClass *widget_class;
104   GtkMenuItemClass *menu_item_class;
105   
106   gobject_class = G_OBJECT_CLASS (klass);
107   widget_class = (GtkWidgetClass*) klass;
108   menu_item_class = (GtkMenuItemClass*) klass;
109   
110   parent_class = g_type_class_peek_parent (klass);
111
112   gobject_class->set_property = gtk_check_menu_item_set_property;
113   gobject_class->get_property = gtk_check_menu_item_get_property;
114
115   g_object_class_install_property (gobject_class,
116                                    PROP_ACTIVE,
117                                    g_param_spec_boolean ("active",
118                                                          P_("Active"),
119                                                          P_("Whether the menu item is checked"),
120                                                          FALSE,
121                                                          G_PARAM_READWRITE));
122   
123   g_object_class_install_property (gobject_class,
124                                    PROP_INCONSISTENT,
125                                    g_param_spec_boolean ("inconsistent",
126                                                          P_("Inconsistent"),
127                                                          P_("Whether to display an \"inconsistent\" state"),
128                                                          FALSE,
129                                                          G_PARAM_READWRITE));
130   
131   g_object_class_install_property (gobject_class,
132                                    PROP_DRAW_AS_RADIO,
133                                    g_param_spec_boolean ("draw_as_radio",
134                                                          P_("Draw as radio menu item"),
135                                                          P_("Whether the menu item looks like a radio menu item"),
136                                                          FALSE,
137                                                          G_PARAM_READWRITE));
138   
139   widget_class->expose_event = gtk_check_menu_item_expose;
140   
141   menu_item_class->activate = gtk_check_menu_item_activate;
142   menu_item_class->hide_on_activate = FALSE;
143   menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
144   
145   klass->toggled = NULL;
146   klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
147
148   check_menu_item_signals[TOGGLED] =
149     g_signal_new ("toggled",
150                   G_OBJECT_CLASS_TYPE (gobject_class),
151                   G_SIGNAL_RUN_FIRST,
152                   G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
153                   NULL, NULL,
154                   _gtk_marshal_VOID__VOID,
155                   G_TYPE_NONE, 0);
156 }
157
158 GtkWidget*
159 gtk_check_menu_item_new (void)
160 {
161   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
162 }
163
164 GtkWidget*
165 gtk_check_menu_item_new_with_label (const gchar *label)
166 {
167   GtkWidget *check_menu_item;
168   GtkWidget *accel_label;
169
170   check_menu_item = gtk_check_menu_item_new ();
171   accel_label = gtk_accel_label_new (label);
172   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
173
174   gtk_container_add (GTK_CONTAINER (check_menu_item), accel_label);
175   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), check_menu_item);
176   gtk_widget_show (accel_label);
177
178   return check_menu_item;
179 }
180
181
182 /**
183  * gtk_check_menu_item_new_with_mnemonic:
184  * @label: The text of the button, with an underscore in front of the
185  *         mnemonic character
186  * @returns: a new #GtkCheckMenuItem
187  *
188  * Creates a new #GtkCheckMenuItem containing a label. The label
189  * will be created using gtk_label_new_with_mnemonic(), so underscores
190  * in @label indicate the mnemonic for the menu item.
191  **/
192 GtkWidget*
193 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
194 {
195   GtkWidget *check_menu_item;
196   GtkWidget *accel_label;
197
198   check_menu_item = gtk_check_menu_item_new ();
199   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
200   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
201   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
202
203   gtk_container_add (GTK_CONTAINER (check_menu_item), accel_label);
204   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), check_menu_item);
205   gtk_widget_show (accel_label);
206
207   return check_menu_item;
208 }
209
210 void
211 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
212                                 gboolean          is_active)
213 {
214   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
215
216   is_active = is_active != 0;
217
218   if (check_menu_item->active != is_active)
219     gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
220 }
221
222 /**
223  * gtk_check_menu_item_get_active:
224  * @check_menu_item: a #GtkCheckMenuItem
225  * 
226  * Returns whether the check menu item is active. See
227  * gtk_check_menu_item_set_active ().
228  * 
229  * Return value: %TRUE if the menu item is checked.
230  */
231 gboolean
232 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
233 {
234   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
235
236   return check_menu_item->active;
237 }
238
239 static void
240 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
241                                          gint        *requisition)
242 {
243   guint toggle_spacing;
244   
245   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
246   
247   gtk_widget_style_get (GTK_WIDGET (menu_item),
248                         "toggle_spacing", &toggle_spacing,
249                         NULL);
250   
251   *requisition = CHECK_TOGGLE_SIZE + toggle_spacing;
252 }
253
254 void
255 gtk_check_menu_item_set_show_toggle (GtkCheckMenuItem *menu_item,
256                                      gboolean          always)
257 {
258   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
259
260 #if 0
261   menu_item->always_show_toggle = always != FALSE;
262 #endif  
263 }
264
265 void
266 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
267 {
268   g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
269 }
270
271 /**
272  * gtk_check_menu_item_set_inconsistent:
273  * @check_menu_item: a #GtkCheckMenuItem
274  * @setting: %TRUE to display an "inconsistent" third state check
275  *
276  * If the user has selected a range of elements (such as some text or
277  * spreadsheet cells) that are affected by a boolean setting, and the
278  * current values in that range are inconsistent, you may want to
279  * display the check in an "in between" state. This function turns on
280  * "in between" display.  Normally you would turn off the inconsistent
281  * state again if the user explicitly selects a setting. This has to be
282  * done manually, gtk_check_menu_item_set_inconsistent() only affects
283  * visual appearance, it doesn't affect the semantics of the widget.
284  * 
285  **/
286 void
287 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
288                                       gboolean          setting)
289 {
290   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
291   
292   setting = setting != FALSE;
293
294   if (setting != check_menu_item->inconsistent)
295     {
296       check_menu_item->inconsistent = setting;
297       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
298       g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
299     }
300 }
301
302 /**
303  * gtk_check_menu_item_get_inconsistent:
304  * @check_menu_item: a #GtkCheckMenuItem
305  * 
306  * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
307  * 
308  * Return value: %TRUE if inconsistent
309  **/
310 gboolean
311 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
312 {
313   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
314
315   return check_menu_item->inconsistent;
316 }
317
318 /**
319  * gtk_check_menu_item_set_draw_as_radio:
320  * @check_menu_item: a #GtkCheckMenuItem
321  * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
322  *
323  * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
324  *
325  * Since: 2.4
326  **/
327 void
328 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
329                                        gboolean          draw_as_radio)
330 {
331   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
332   
333   draw_as_radio = draw_as_radio != FALSE;
334
335   if (draw_as_radio != check_menu_item->draw_as_radio)
336     {
337       check_menu_item->draw_as_radio = draw_as_radio;
338
339       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
340
341       g_object_notify (G_OBJECT (check_menu_item), "draw_as_radio");
342     }
343 }
344
345 /**
346  * gtk_check_menu_item_get_draw_as_radio:
347  * @check_menu_item: a #GtkCheckMenuItem
348  * 
349  * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
350  * 
351  * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
352  * 
353  * Since: 2.4
354  **/
355 gboolean
356 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
357 {
358   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
359   
360   return check_menu_item->draw_as_radio;
361 }
362
363 static void
364 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
365 {
366   check_menu_item->active = FALSE;
367   check_menu_item->always_show_toggle = TRUE;
368 }
369
370 static gint
371 gtk_check_menu_item_expose (GtkWidget      *widget,
372                             GdkEventExpose *event)
373 {
374   if (GTK_WIDGET_CLASS (parent_class)->expose_event)
375     (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
376
377   gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);
378
379   return FALSE;
380 }
381
382 static void
383 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
384 {
385   GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
386   check_menu_item->active = !check_menu_item->active;
387
388   gtk_check_menu_item_toggled (check_menu_item);
389   gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
390
391   g_object_notify (G_OBJECT (check_menu_item), "active");
392 }
393
394 static void
395 gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
396                                     GdkRectangle     *area)
397 {
398   if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
399     (* GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator) (check_menu_item, area);
400 }
401
402 static void
403 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
404                                          GdkRectangle     *area)
405 {
406   GtkWidget *widget;
407   GtkStateType state_type;
408   GtkShadowType shadow_type;
409   gint width, height;
410   gint x, y;
411
412   if (GTK_WIDGET_DRAWABLE (check_menu_item))
413     {
414       guint offset;
415       guint toggle_size;
416       guint toggle_spacing;
417       guint horizontal_padding;
418       
419       widget = GTK_WIDGET (check_menu_item);
420
421       gtk_widget_style_get (GTK_WIDGET (check_menu_item),
422                             "toggle_spacing", &toggle_spacing,
423                             "horizontal_padding", &horizontal_padding,
424                             NULL);
425
426       width = 8;
427       height = 8;
428
429       toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
430       offset = GTK_CONTAINER (check_menu_item)->border_width + widget->style->xthickness;
431       
432       offset = GTK_CONTAINER (check_menu_item)->border_width +
433         widget->style->xthickness + 2; 
434
435       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
436         {
437           x = widget->allocation.x + offset + horizontal_padding +
438             (toggle_size - toggle_spacing - width) / 2;
439         }
440       else 
441         {
442           x = widget->allocation.x + widget->allocation.width -
443             offset - horizontal_padding - toggle_size + toggle_spacing +
444             (toggle_size - toggle_spacing - width) / 2;
445         }
446       
447       y = widget->allocation.y + (widget->allocation.height - height) / 2;
448
449       if (check_menu_item->active ||
450           check_menu_item->always_show_toggle ||
451           (GTK_WIDGET_STATE (check_menu_item) == GTK_STATE_PRELIGHT))
452         {
453           state_type = GTK_WIDGET_STATE (widget);
454           
455           if (check_menu_item->inconsistent)
456             shadow_type = GTK_SHADOW_ETCHED_IN;
457           else if (check_menu_item->active)
458             shadow_type = GTK_SHADOW_IN;
459           else 
460             shadow_type = GTK_SHADOW_OUT;
461           
462           if (!GTK_WIDGET_IS_SENSITIVE (widget))
463             state_type = GTK_STATE_INSENSITIVE;
464
465           if (check_menu_item->draw_as_radio)
466             {
467               gtk_paint_option (widget->style, widget->window,
468                                 state_type, shadow_type,
469                                 area, widget, "option",
470                                 x, y, width, height);
471             }
472           else
473             {
474               gtk_paint_check (widget->style, widget->window,
475                                state_type, shadow_type,
476                                area, widget, "check",
477                                x, y, width, height);
478             }
479         }
480     }
481 }
482
483
484 static void
485 gtk_check_menu_item_get_property (GObject     *object,
486                                   guint        prop_id,
487                                   GValue      *value,
488                                   GParamSpec  *pspec)
489 {
490   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
491   
492   switch (prop_id)
493     {
494     case PROP_ACTIVE:
495       g_value_set_boolean (value, checkitem->active);
496       break;
497     case PROP_INCONSISTENT:
498       g_value_set_boolean (value, checkitem->inconsistent);
499       break;
500     case PROP_DRAW_AS_RADIO:
501       g_value_set_boolean (value, checkitem->draw_as_radio);
502       break;
503     default:
504       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
505       break;
506     }
507 }
508
509
510 static void
511 gtk_check_menu_item_set_property (GObject      *object,
512                                   guint         prop_id,
513                                   const GValue *value,
514                                   GParamSpec   *pspec)
515 {
516   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
517   
518   switch (prop_id)
519     {
520     case PROP_ACTIVE:
521       gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
522       break;
523     case PROP_INCONSISTENT:
524       gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
525       break;
526     case PROP_DRAW_AS_RADIO:
527       gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
528       break;
529     default:
530       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
531       break;
532     }
533 }