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