1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2001. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 #include "gtkcheckmenuitem.h"
27 #include "gtkmenuitemprivate.h"
28 #include "gtkaccellabel.h"
29 #include "gtkactivatable.h"
30 #include "gtktoggleaction.h"
31 #include "gtkmarshalers.h"
32 #include "gtkprivate.h"
34 #include "a11y/gtkcheckmenuitemaccessible.h"
37 * SECTION:gtkcheckmenuitem
38 * @Short_description: A menu item with a check box
39 * @Title: GtkCheckMenuItem
41 * A #GtkCheckMenuItem is a menu item that maintains the state of a boolean
42 * value in addition to a #GtkMenuItem usual role in activating application
45 * A check box indicating the state of the boolean value is displayed
46 * at the left side of the #GtkMenuItem. Activating the #GtkMenuItem
51 #define INDICATOR_SIZE 16
53 struct _GtkCheckMenuItemPrivate
56 guint always_show_toggle : 1;
57 guint draw_as_radio : 1;
58 guint inconsistent : 1;
73 static gint gtk_check_menu_item_draw (GtkWidget *widget,
75 static void gtk_check_menu_item_activate (GtkMenuItem *menu_item);
76 static void gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
78 static void gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
80 static void gtk_check_menu_item_set_property (GObject *object,
84 static void gtk_check_menu_item_get_property (GObject *object,
89 static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface);
90 static void gtk_check_menu_item_update (GtkActivatable *activatable,
92 const gchar *property_name);
93 static void gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
96 static GtkActivatableIface *parent_activatable_iface;
97 static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
99 G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
100 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
101 gtk_check_menu_item_activatable_interface_init))
104 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
106 GObjectClass *gobject_class;
107 GtkWidgetClass *widget_class;
108 GtkMenuItemClass *menu_item_class;
110 gobject_class = G_OBJECT_CLASS (klass);
111 widget_class = (GtkWidgetClass*) klass;
112 menu_item_class = (GtkMenuItemClass*) klass;
114 gobject_class->set_property = gtk_check_menu_item_set_property;
115 gobject_class->get_property = gtk_check_menu_item_get_property;
117 g_object_class_install_property (gobject_class,
119 g_param_spec_boolean ("active",
121 P_("Whether the menu item is checked"),
123 GTK_PARAM_READWRITE));
125 g_object_class_install_property (gobject_class,
127 g_param_spec_boolean ("inconsistent",
129 P_("Whether to display an \"inconsistent\" state"),
131 GTK_PARAM_READWRITE));
133 g_object_class_install_property (gobject_class,
135 g_param_spec_boolean ("draw-as-radio",
136 P_("Draw as radio menu item"),
137 P_("Whether the menu item looks like a radio menu item"),
139 GTK_PARAM_READWRITE));
141 gtk_widget_class_install_style_property (widget_class,
142 g_param_spec_int ("indicator-size",
143 P_("Indicator Size"),
144 P_("Size of check or radio indicator"),
148 GTK_PARAM_READABLE));
150 widget_class->draw = gtk_check_menu_item_draw;
152 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CHECK_MENU_ITEM_ACCESSIBLE);
154 menu_item_class->activate = gtk_check_menu_item_activate;
155 menu_item_class->hide_on_activate = FALSE;
156 menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
158 klass->toggled = NULL;
159 klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
162 * GtkCheckMenuItem::toggled:
163 * @checkmenuitem: the object which received the signal.
165 * This signal is emitted when the state of the check box is changed.
167 * A signal handler can use gtk_check_menu_item_get_active()
168 * to discover the new state.
170 check_menu_item_signals[TOGGLED] =
171 g_signal_new (I_("toggled"),
172 G_OBJECT_CLASS_TYPE (gobject_class),
174 G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
176 _gtk_marshal_VOID__VOID,
179 g_type_class_add_private (klass, sizeof (GtkCheckMenuItemPrivate));
183 gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface)
185 parent_activatable_iface = g_type_interface_peek_parent (iface);
186 iface->update = gtk_check_menu_item_update;
187 iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
191 gtk_check_menu_item_update (GtkActivatable *activatable,
193 const gchar *property_name)
195 GtkCheckMenuItem *check_menu_item;
197 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
199 parent_activatable_iface->update (activatable, action, property_name);
201 if (strcmp (property_name, "active") == 0)
203 gtk_action_block_activate (action);
204 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
205 gtk_action_unblock_activate (action);
208 if (!gtk_activatable_get_use_action_appearance (activatable))
211 if (strcmp (property_name, "draw-as-radio") == 0)
212 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
213 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
217 gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
220 GtkCheckMenuItem *check_menu_item;
222 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
224 parent_activatable_iface->sync_action_properties (activatable, action);
226 if (!GTK_IS_TOGGLE_ACTION (action))
229 gtk_action_block_activate (action);
230 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
231 gtk_action_unblock_activate (action);
233 if (!gtk_activatable_get_use_action_appearance (activatable))
236 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
237 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
241 * gtk_check_menu_item_new:
243 * Creates a new #GtkCheckMenuItem.
245 * Returns: a new #GtkCheckMenuItem.
248 gtk_check_menu_item_new (void)
250 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
254 * gtk_check_menu_item_new_with_label:
255 * @label: the string to use for the label.
257 * Creates a new #GtkCheckMenuItem with a label.
259 * Returns: a new #GtkCheckMenuItem.
262 gtk_check_menu_item_new_with_label (const gchar *label)
264 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
271 * gtk_check_menu_item_new_with_mnemonic:
272 * @label: The text of the button, with an underscore in front of the
275 * Creates a new #GtkCheckMenuItem containing a label. The label
276 * will be created using gtk_label_new_with_mnemonic(), so underscores
277 * in @label indicate the mnemonic for the menu item.
279 * Returns: a new #GtkCheckMenuItem
282 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
284 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
286 "use-underline", TRUE,
291 * gtk_check_menu_item_set_active:
292 * @check_menu_item: a #GtkCheckMenuItem.
293 * @is_active: boolean value indicating whether the check box is active.
295 * Sets the active state of the menu item's check box.
298 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
301 GtkCheckMenuItemPrivate *priv;
303 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
305 priv = check_menu_item->priv;
307 is_active = is_active != 0;
309 if (priv->active != is_active)
310 gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
314 * gtk_check_menu_item_get_active:
315 * @check_menu_item: a #GtkCheckMenuItem
317 * Returns whether the check menu item is active. See
318 * gtk_check_menu_item_set_active ().
320 * Return value: %TRUE if the menu item is checked.
323 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
325 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
327 return check_menu_item->priv->active;
331 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
334 guint toggle_spacing;
335 guint indicator_size;
337 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
339 gtk_widget_style_get (GTK_WIDGET (menu_item),
340 "toggle-spacing", &toggle_spacing,
341 "indicator-size", &indicator_size,
344 *requisition = indicator_size + toggle_spacing;
348 * gtk_check_menu_item_toggled:
349 * @check_menu_item: a #GtkCheckMenuItem.
351 * Emits the #GtkCheckMenuItem::toggled signal.
354 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
356 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
360 * gtk_check_menu_item_set_inconsistent:
361 * @check_menu_item: a #GtkCheckMenuItem
362 * @setting: %TRUE to display an "inconsistent" third state check
364 * If the user has selected a range of elements (such as some text or
365 * spreadsheet cells) that are affected by a boolean setting, and the
366 * current values in that range are inconsistent, you may want to
367 * display the check in an "in between" state. This function turns on
368 * "in between" display. Normally you would turn off the inconsistent
369 * state again if the user explicitly selects a setting. This has to be
370 * done manually, gtk_check_menu_item_set_inconsistent() only affects
371 * visual appearance, it doesn't affect the semantics of the widget.
375 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
378 GtkCheckMenuItemPrivate *priv;
380 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
382 priv = check_menu_item->priv;
384 setting = setting != FALSE;
386 if (setting != priv->inconsistent)
388 priv->inconsistent = setting;
389 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
390 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
395 * gtk_check_menu_item_get_inconsistent:
396 * @check_menu_item: a #GtkCheckMenuItem
398 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
400 * Return value: %TRUE if inconsistent
403 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
405 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
407 return check_menu_item->priv->inconsistent;
411 * gtk_check_menu_item_set_draw_as_radio:
412 * @check_menu_item: a #GtkCheckMenuItem
413 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
415 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
420 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
421 gboolean draw_as_radio)
423 GtkCheckMenuItemPrivate *priv;
425 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
427 priv = check_menu_item->priv;
429 draw_as_radio = draw_as_radio != FALSE;
431 if (draw_as_radio != priv->draw_as_radio)
433 priv->draw_as_radio = draw_as_radio;
435 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
437 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
442 * gtk_check_menu_item_get_draw_as_radio:
443 * @check_menu_item: a #GtkCheckMenuItem
445 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
447 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
452 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
454 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
456 return check_menu_item->priv->draw_as_radio;
460 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
462 GtkCheckMenuItemPrivate *priv;
464 check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
465 GTK_TYPE_CHECK_MENU_ITEM,
466 GtkCheckMenuItemPrivate);
467 priv = check_menu_item->priv;
469 priv->active = FALSE;
470 priv->always_show_toggle = TRUE;
474 gtk_check_menu_item_draw (GtkWidget *widget,
477 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (widget);
479 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw)
480 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw (widget, cr);
482 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
483 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, cr);
489 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
491 GtkCheckMenuItemPrivate *priv;
493 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
494 priv = check_menu_item->priv;
496 priv->active = !priv->active;
498 gtk_check_menu_item_toggled (check_menu_item);
499 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
501 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
503 g_object_notify (G_OBJECT (check_menu_item), "active");
507 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
510 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
514 widget = GTK_WIDGET (check_menu_item);
516 if (gtk_widget_is_drawable (widget))
518 GtkAllocation allocation;
519 GtkStyleContext *context;
523 guint toggle_spacing;
524 guint horizontal_padding;
525 guint indicator_size;
529 context = gtk_widget_get_style_context (widget);
530 state = gtk_widget_get_state_flags (widget);
531 gtk_style_context_get_padding (context, state, &padding);
533 gtk_widget_get_allocation (widget, &allocation);
535 gtk_widget_style_get (widget,
536 "toggle-spacing", &toggle_spacing,
537 "horizontal-padding", &horizontal_padding,
538 "indicator-size", &indicator_size,
541 toggle_size = GTK_MENU_ITEM (check_menu_item)->priv->toggle_size;
542 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
543 offset = border_width + padding.left + 2;
545 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
547 x = offset + horizontal_padding +
548 (toggle_size - toggle_spacing - indicator_size) / 2;
552 x = allocation.width -
553 offset - horizontal_padding - toggle_size + toggle_spacing +
554 (toggle_size - toggle_spacing - indicator_size) / 2;
557 y = (allocation.height - indicator_size) / 2;
560 priv->always_show_toggle ||
561 (state & GTK_STATE_FLAG_PRELIGHT))
563 gtk_style_context_save (context);
565 if (priv->inconsistent)
566 state |= GTK_STATE_FLAG_INCONSISTENT;
567 else if (priv->active)
568 state |= GTK_STATE_FLAG_ACTIVE;
570 gtk_style_context_set_state (context, state);
572 if (priv->draw_as_radio)
574 gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
575 gtk_render_option (context, cr, x, y,
576 indicator_size, indicator_size);
580 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
581 gtk_render_check (context, cr, x, y,
582 indicator_size, indicator_size);
585 gtk_style_context_restore (context);
592 gtk_check_menu_item_get_property (GObject *object,
597 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
598 GtkCheckMenuItemPrivate *priv = checkitem->priv;
603 g_value_set_boolean (value, priv->active);
605 case PROP_INCONSISTENT:
606 g_value_set_boolean (value, priv->inconsistent);
608 case PROP_DRAW_AS_RADIO:
609 g_value_set_boolean (value, priv->draw_as_radio);
612 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619 gtk_check_menu_item_set_property (GObject *object,
624 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
629 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
631 case PROP_INCONSISTENT:
632 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
634 case PROP_DRAW_AS_RADIO:
635 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
638 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
647 * _gtk_check_menu_item_set_active:
648 * @check_menu_item: a #GtkCheckMenuItem
649 * @is_active: whether the action is active or not
651 * Sets the #GtkCheckMenuItem:active property directly. This function does
652 * not emit signals or notifications: it is left to the caller to do so.
655 _gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
658 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
660 priv->active = is_active;