]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckmenuitem.c
9f78ccee0aff8d596ad8e19aab8e93d4725c84bb
[~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 "gtkintl.h"
31 #include "gtkmarshalers.h"
32 #include "gtkalias.h"
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   gtk_widget_class_install_style_property (widget_class,
140                                            g_param_spec_int ("indicator-size",
141                                                              P_("Indicator Size")
142 ,
143                                                              P_("Size of check or radio indicator"),
144                                                              0,
145                                                              G_MAXINT,
146                                                              12,
147                                                              G_PARAM_READABLE));
148
149   widget_class->expose_event = gtk_check_menu_item_expose;
150   
151   menu_item_class->activate = gtk_check_menu_item_activate;
152   menu_item_class->hide_on_activate = FALSE;
153   menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
154   
155   klass->toggled = NULL;
156   klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
157
158   check_menu_item_signals[TOGGLED] =
159     g_signal_new ("toggled",
160                   G_OBJECT_CLASS_TYPE (gobject_class),
161                   G_SIGNAL_RUN_FIRST,
162                   G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
163                   NULL, NULL,
164                   _gtk_marshal_VOID__VOID,
165                   G_TYPE_NONE, 0);
166 }
167
168 GtkWidget*
169 gtk_check_menu_item_new (void)
170 {
171   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
172 }
173
174 GtkWidget*
175 gtk_check_menu_item_new_with_label (const gchar *label)
176 {
177   GtkWidget *check_menu_item;
178   GtkWidget *accel_label;
179
180   check_menu_item = gtk_check_menu_item_new ();
181   accel_label = gtk_accel_label_new (label);
182   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
183
184   gtk_container_add (GTK_CONTAINER (check_menu_item), accel_label);
185   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), check_menu_item);
186   gtk_widget_show (accel_label);
187
188   return check_menu_item;
189 }
190
191
192 /**
193  * gtk_check_menu_item_new_with_mnemonic:
194  * @label: The text of the button, with an underscore in front of the
195  *         mnemonic character
196  * @returns: a new #GtkCheckMenuItem
197  *
198  * Creates a new #GtkCheckMenuItem containing a label. The label
199  * will be created using gtk_label_new_with_mnemonic(), so underscores
200  * in @label indicate the mnemonic for the menu item.
201  **/
202 GtkWidget*
203 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
204 {
205   GtkWidget *check_menu_item;
206   GtkWidget *accel_label;
207
208   check_menu_item = gtk_check_menu_item_new ();
209   accel_label = g_object_new (GTK_TYPE_ACCEL_LABEL, NULL);
210   gtk_label_set_text_with_mnemonic (GTK_LABEL (accel_label), label);
211   gtk_misc_set_alignment (GTK_MISC (accel_label), 0.0, 0.5);
212
213   gtk_container_add (GTK_CONTAINER (check_menu_item), accel_label);
214   gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (accel_label), check_menu_item);
215   gtk_widget_show (accel_label);
216
217   return check_menu_item;
218 }
219
220 void
221 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
222                                 gboolean          is_active)
223 {
224   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
225
226   is_active = is_active != 0;
227
228   if (check_menu_item->active != is_active)
229     gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
230 }
231
232 /**
233  * gtk_check_menu_item_get_active:
234  * @check_menu_item: a #GtkCheckMenuItem
235  * 
236  * Returns whether the check menu item is active. See
237  * gtk_check_menu_item_set_active ().
238  * 
239  * Return value: %TRUE if the menu item is checked.
240  */
241 gboolean
242 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
243 {
244   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
245
246   return check_menu_item->active;
247 }
248
249 static void
250 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
251                                          gint        *requisition)
252 {
253   guint toggle_spacing;
254   guint indicator_size;
255   
256   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
257   
258   gtk_widget_style_get (GTK_WIDGET (menu_item),
259                         "toggle_spacing", &toggle_spacing,
260                         "indicator_size", &indicator_size,
261                         NULL);
262
263   *requisition = indicator_size + toggle_spacing;
264 }
265
266 void
267 gtk_check_menu_item_set_show_toggle (GtkCheckMenuItem *menu_item,
268                                      gboolean          always)
269 {
270   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
271
272 #if 0
273   menu_item->always_show_toggle = always != FALSE;
274 #endif  
275 }
276
277 void
278 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
279 {
280   g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
281 }
282
283 /**
284  * gtk_check_menu_item_set_inconsistent:
285  * @check_menu_item: a #GtkCheckMenuItem
286  * @setting: %TRUE to display an "inconsistent" third state check
287  *
288  * If the user has selected a range of elements (such as some text or
289  * spreadsheet cells) that are affected by a boolean setting, and the
290  * current values in that range are inconsistent, you may want to
291  * display the check in an "in between" state. This function turns on
292  * "in between" display.  Normally you would turn off the inconsistent
293  * state again if the user explicitly selects a setting. This has to be
294  * done manually, gtk_check_menu_item_set_inconsistent() only affects
295  * visual appearance, it doesn't affect the semantics of the widget.
296  * 
297  **/
298 void
299 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
300                                       gboolean          setting)
301 {
302   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
303   
304   setting = setting != FALSE;
305
306   if (setting != check_menu_item->inconsistent)
307     {
308       check_menu_item->inconsistent = setting;
309       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
310       g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
311     }
312 }
313
314 /**
315  * gtk_check_menu_item_get_inconsistent:
316  * @check_menu_item: a #GtkCheckMenuItem
317  * 
318  * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
319  * 
320  * Return value: %TRUE if inconsistent
321  **/
322 gboolean
323 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
324 {
325   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
326
327   return check_menu_item->inconsistent;
328 }
329
330 /**
331  * gtk_check_menu_item_set_draw_as_radio:
332  * @check_menu_item: a #GtkCheckMenuItem
333  * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
334  *
335  * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
336  *
337  * Since: 2.4
338  **/
339 void
340 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
341                                        gboolean          draw_as_radio)
342 {
343   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
344   
345   draw_as_radio = draw_as_radio != FALSE;
346
347   if (draw_as_radio != check_menu_item->draw_as_radio)
348     {
349       check_menu_item->draw_as_radio = draw_as_radio;
350
351       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
352
353       g_object_notify (G_OBJECT (check_menu_item), "draw_as_radio");
354     }
355 }
356
357 /**
358  * gtk_check_menu_item_get_draw_as_radio:
359  * @check_menu_item: a #GtkCheckMenuItem
360  * 
361  * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
362  * 
363  * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
364  * 
365  * Since: 2.4
366  **/
367 gboolean
368 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
369 {
370   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
371   
372   return check_menu_item->draw_as_radio;
373 }
374
375 static void
376 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
377 {
378   check_menu_item->active = FALSE;
379   check_menu_item->always_show_toggle = TRUE;
380 }
381
382 static gint
383 gtk_check_menu_item_expose (GtkWidget      *widget,
384                             GdkEventExpose *event)
385 {
386   if (GTK_WIDGET_CLASS (parent_class)->expose_event)
387     (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
388
389   gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);
390
391   return FALSE;
392 }
393
394 static void
395 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
396 {
397   GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
398   check_menu_item->active = !check_menu_item->active;
399
400   gtk_check_menu_item_toggled (check_menu_item);
401   gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
402
403   g_object_notify (G_OBJECT (check_menu_item), "active");
404 }
405
406 static void
407 gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
408                                     GdkRectangle     *area)
409 {
410   if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
411     (* GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator) (check_menu_item, area);
412 }
413
414 static void
415 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
416                                          GdkRectangle     *area)
417 {
418   GtkWidget *widget;
419   GtkStateType state_type;
420   GtkShadowType shadow_type;
421   gint x, y;
422
423   if (GTK_WIDGET_DRAWABLE (check_menu_item))
424     {
425       guint offset;
426       guint toggle_size;
427       guint toggle_spacing;
428       guint horizontal_padding;
429       guint indicator_size;
430       
431       widget = GTK_WIDGET (check_menu_item);
432
433       gtk_widget_style_get (GTK_WIDGET (check_menu_item),
434                             "toggle_spacing", &toggle_spacing,
435                             "horizontal_padding", &horizontal_padding,
436                             "indicator_size", &indicator_size,
437                             NULL);
438
439       toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
440       offset = GTK_CONTAINER (check_menu_item)->border_width + widget->style->xthickness;
441       
442       offset = GTK_CONTAINER (check_menu_item)->border_width +
443         widget->style->xthickness + 2; 
444
445       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
446         {
447           x = widget->allocation.x + offset + horizontal_padding +
448             (toggle_size - toggle_spacing - indicator_size) / 2;
449         }
450       else 
451         {
452           x = widget->allocation.x + widget->allocation.width -
453             offset - horizontal_padding - toggle_size + toggle_spacing +
454             (toggle_size - toggle_spacing - indicator_size) / 2;
455         }
456       
457       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
458
459       if (check_menu_item->active ||
460           check_menu_item->always_show_toggle ||
461           (GTK_WIDGET_STATE (check_menu_item) == GTK_STATE_PRELIGHT))
462         {
463           state_type = GTK_WIDGET_STATE (widget);
464           
465           if (check_menu_item->inconsistent)
466             shadow_type = GTK_SHADOW_ETCHED_IN;
467           else if (check_menu_item->active)
468             shadow_type = GTK_SHADOW_IN;
469           else 
470             shadow_type = GTK_SHADOW_OUT;
471           
472           if (!GTK_WIDGET_IS_SENSITIVE (widget))
473             state_type = GTK_STATE_INSENSITIVE;
474
475           if (check_menu_item->draw_as_radio)
476             {
477               gtk_paint_option (widget->style, widget->window,
478                                 state_type, shadow_type,
479                                 area, widget, "option",
480                                 x, y, indicator_size, indicator_size);
481             }
482           else
483             {
484               gtk_paint_check (widget->style, widget->window,
485                                state_type, shadow_type,
486                                area, widget, "check",
487                                x, y, indicator_size, indicator_size);
488             }
489         }
490     }
491 }
492
493
494 static void
495 gtk_check_menu_item_get_property (GObject     *object,
496                                   guint        prop_id,
497                                   GValue      *value,
498                                   GParamSpec  *pspec)
499 {
500   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
501   
502   switch (prop_id)
503     {
504     case PROP_ACTIVE:
505       g_value_set_boolean (value, checkitem->active);
506       break;
507     case PROP_INCONSISTENT:
508       g_value_set_boolean (value, checkitem->inconsistent);
509       break;
510     case PROP_DRAW_AS_RADIO:
511       g_value_set_boolean (value, checkitem->draw_as_radio);
512       break;
513     default:
514       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515       break;
516     }
517 }
518
519
520 static void
521 gtk_check_menu_item_set_property (GObject      *object,
522                                   guint         prop_id,
523                                   const GValue *value,
524                                   GParamSpec   *pspec)
525 {
526   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
527   
528   switch (prop_id)
529     {
530     case PROP_ACTIVE:
531       gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
532       break;
533     case PROP_INCONSISTENT:
534       gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
535       break;
536     case PROP_DRAW_AS_RADIO:
537       gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
538       break;
539     default:
540       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
541       break;
542     }
543 }
544
545 #define __GTK_CHECK_MENU_ITEM_C__
546 #include "gtkaliasdef.c"