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 #include "gtkalias.h"
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 
G_DEFINE_TYPE_WITH_CODE(GtkCheckMenuItem,gtk_check_menu_item,GTK_TYPE_MENU_ITEM,G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,gtk_check_menu_item_activatable_interface_init))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
gtk_check_menu_item_activatable_interface_init(GtkActivatableIface * iface)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
gtk_check_menu_item_update(GtkActivatable * activatable,GtkAction * action,const gchar * property_name)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
gtk_check_menu_item_sync_action_properties(GtkActivatable * activatable,GtkAction * action)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*
gtk_check_menu_item_new(void)206 gtk_check_menu_item_new (void)
207 {
208   return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
209 }
210 
211 GtkWidget*
gtk_check_menu_item_new_with_label(const gchar * label)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*
gtk_check_menu_item_new_with_mnemonic(const gchar * label)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
gtk_check_menu_item_set_active(GtkCheckMenuItem * check_menu_item,gboolean is_active)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
gtk_check_menu_item_get_active(GtkCheckMenuItem * check_menu_item)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
gtk_check_menu_item_toggle_size_request(GtkMenuItem * menu_item,gint * requisition)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
gtk_check_menu_item_set_show_toggle(GtkCheckMenuItem * menu_item,gboolean always)286 gtk_check_menu_item_set_show_toggle (GtkCheckMenuItem *menu_item,
287 				     gboolean          always)
288 {
289   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
290 
291 #if 0
292   menu_item->always_show_toggle = always != FALSE;
293 #endif
294 }
295 
296 void
gtk_check_menu_item_toggled(GtkCheckMenuItem * check_menu_item)297 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
298 {
299   g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
300 }
301 
302 /**
303  * gtk_check_menu_item_set_inconsistent:
304  * @check_menu_item: a #GtkCheckMenuItem
305  * @setting: %TRUE to display an "inconsistent" third state check
306  *
307  * If the user has selected a range of elements (such as some text or
308  * spreadsheet cells) that are affected by a boolean setting, and the
309  * current values in that range are inconsistent, you may want to
310  * display the check in an "in between" state. This function turns on
311  * "in between" display.  Normally you would turn off the inconsistent
312  * state again if the user explicitly selects a setting. This has to be
313  * done manually, gtk_check_menu_item_set_inconsistent() only affects
314  * visual appearance, it doesn't affect the semantics of the widget.
315  *
316  **/
317 void
gtk_check_menu_item_set_inconsistent(GtkCheckMenuItem * check_menu_item,gboolean setting)318 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
319                                       gboolean          setting)
320 {
321   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
322 
323   setting = setting != FALSE;
324 
325   if (setting != check_menu_item->inconsistent)
326     {
327       check_menu_item->inconsistent = setting;
328       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
329       g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
330     }
331 }
332 
333 /**
334  * gtk_check_menu_item_get_inconsistent:
335  * @check_menu_item: a #GtkCheckMenuItem
336  *
337  * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
338  *
339  * Return value: %TRUE if inconsistent
340  **/
341 gboolean
gtk_check_menu_item_get_inconsistent(GtkCheckMenuItem * check_menu_item)342 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
343 {
344   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
345 
346   return check_menu_item->inconsistent;
347 }
348 
349 /**
350  * gtk_check_menu_item_set_draw_as_radio:
351  * @check_menu_item: a #GtkCheckMenuItem
352  * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
353  *
354  * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
355  *
356  * Since: 2.4
357  **/
358 void
gtk_check_menu_item_set_draw_as_radio(GtkCheckMenuItem * check_menu_item,gboolean draw_as_radio)359 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
360 				       gboolean          draw_as_radio)
361 {
362   g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
363 
364   draw_as_radio = draw_as_radio != FALSE;
365 
366   if (draw_as_radio != check_menu_item->draw_as_radio)
367     {
368       check_menu_item->draw_as_radio = draw_as_radio;
369 
370       gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
371 
372       g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
373     }
374 }
375 
376 /**
377  * gtk_check_menu_item_get_draw_as_radio:
378  * @check_menu_item: a #GtkCheckMenuItem
379  *
380  * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
381  *
382  * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
383  *
384  * Since: 2.4
385  **/
386 gboolean
gtk_check_menu_item_get_draw_as_radio(GtkCheckMenuItem * check_menu_item)387 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
388 {
389   g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
390 
391   return check_menu_item->draw_as_radio;
392 }
393 
394 static void
gtk_check_menu_item_init(GtkCheckMenuItem * check_menu_item)395 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
396 {
397   check_menu_item->active = FALSE;
398   check_menu_item->always_show_toggle = TRUE;
399 }
400 
401 static gint
gtk_check_menu_item_expose(GtkWidget * widget,GdkEventExpose * event)402 gtk_check_menu_item_expose (GtkWidget      *widget,
403 			    GdkEventExpose *event)
404 {
405   if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event)
406     GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event (widget, event);
407 
408   gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);
409 
410   return FALSE;
411 }
412 
413 static void
gtk_check_menu_item_activate(GtkMenuItem * menu_item)414 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
415 {
416   GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
417   check_menu_item->active = !check_menu_item->active;
418 
419   gtk_check_menu_item_toggled (check_menu_item);
420   gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
421 
422   GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
423 
424   g_object_notify (G_OBJECT (check_menu_item), "active");
425 }
426 
427 static void
gtk_check_menu_item_draw_indicator(GtkCheckMenuItem * check_menu_item,GdkRectangle * area)428 gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
429 				    GdkRectangle     *area)
430 {
431   if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
432     GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, area);
433 }
434 
435 static void
gtk_real_check_menu_item_draw_indicator(GtkCheckMenuItem * check_menu_item,GdkRectangle * area)436 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
437 					 GdkRectangle     *area)
438 {
439   GtkWidget *widget;
440   GtkStateType state_type;
441   GtkShadowType shadow_type;
442   gint x, y;
443 
444   widget = GTK_WIDGET (check_menu_item);
445 
446   if (gtk_widget_is_drawable (widget))
447     {
448       guint offset;
449       guint toggle_size;
450       guint toggle_spacing;
451       guint horizontal_padding;
452       guint indicator_size;
453 
454       gtk_widget_style_get (widget,
455  			    "toggle-spacing", &toggle_spacing,
456  			    "horizontal-padding", &horizontal_padding,
457 			    "indicator-size", &indicator_size,
458  			    NULL);
459 
460       toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
461       offset = GTK_CONTAINER (check_menu_item)->border_width +
462 	widget->style->xthickness + 2;
463 
464       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
465 	{
466 	  x = widget->allocation.x + offset + horizontal_padding +
467 	    (toggle_size - toggle_spacing - indicator_size) / 2;
468 	}
469       else
470 	{
471 	  x = widget->allocation.x + widget->allocation.width -
472 	    offset - horizontal_padding - toggle_size + toggle_spacing +
473 	    (toggle_size - toggle_spacing - indicator_size) / 2;
474 	}
475 
476       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
477 
478       if (check_menu_item->active ||
479 	  check_menu_item->always_show_toggle ||
480 	  (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT))
481 	{
482 	  state_type = gtk_widget_get_state (widget);
483 
484 	  if (check_menu_item->inconsistent)
485 	    shadow_type = GTK_SHADOW_ETCHED_IN;
486 	  else if (check_menu_item->active)
487 	    shadow_type = GTK_SHADOW_IN;
488 	  else
489 	    shadow_type = GTK_SHADOW_OUT;
490 
491 	  if (!gtk_widget_is_sensitive (widget))
492 	    state_type = GTK_STATE_INSENSITIVE;
493 
494 	  if (check_menu_item->draw_as_radio)
495 	    {
496 	      gtk_paint_option (widget->style, widget->window,
497 				state_type, shadow_type,
498 				area, widget, "option",
499 				x, y, indicator_size, indicator_size);
500 	    }
501 	  else
502 	    {
503 	      gtk_paint_check (widget->style, widget->window,
504 			       state_type, shadow_type,
505 			       area, widget, "check",
506 			       x, y, indicator_size, indicator_size);
507 	    }
508 	}
509     }
510 }
511 
512 
513 static void
gtk_check_menu_item_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)514 gtk_check_menu_item_get_property (GObject     *object,
515 				  guint        prop_id,
516 				  GValue      *value,
517 				  GParamSpec  *pspec)
518 {
519   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
520 
521   switch (prop_id)
522     {
523     case PROP_ACTIVE:
524       g_value_set_boolean (value, checkitem->active);
525       break;
526     case PROP_INCONSISTENT:
527       g_value_set_boolean (value, checkitem->inconsistent);
528       break;
529     case PROP_DRAW_AS_RADIO:
530       g_value_set_boolean (value, checkitem->draw_as_radio);
531       break;
532     default:
533       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534       break;
535     }
536 }
537 
538 
539 static void
gtk_check_menu_item_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)540 gtk_check_menu_item_set_property (GObject      *object,
541 				  guint         prop_id,
542 				  const GValue *value,
543 				  GParamSpec   *pspec)
544 {
545   GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
546 
547   switch (prop_id)
548     {
549     case PROP_ACTIVE:
550       gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
551       break;
552     case PROP_INCONSISTENT:
553       gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
554       break;
555     case PROP_DRAW_AS_RADIO:
556       gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
557       break;
558     default:
559       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560       break;
561     }
562 }
563 
564 #define __GTK_CHECK_MENU_ITEM_C__
565 #include "gtkaliasdef.c"
566