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