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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
28 #include "gtkcheckmenuitem.h"
29 #include "gtkmenuitemprivate.h"
30 #include "gtkaccellabel.h"
31 #include "gtkactivatable.h"
32 #include "gtktoggleaction.h"
33 #include "gtkmarshalers.h"
34 #include "gtkprivate.h"
36 #include "a11y/gtkcheckmenuitemaccessible.h"
39 * SECTION:gtkcheckmenuitem
40 * @Short_description: A menu item with a check box
41 * @Title: GtkCheckMenuItem
43 * A #GtkCheckMenuItem is a menu item that maintains the state of a boolean
44 * value in addition to a #GtkMenuItem usual role in activating application
47 * A check box indicating the state of the boolean value is displayed
48 * at the left side of the #GtkMenuItem. Activating the #GtkMenuItem
53 #define INDICATOR_SIZE 16
55 struct _GtkCheckMenuItemPrivate
58 guint always_show_toggle : 1;
59 guint draw_as_radio : 1;
60 guint inconsistent : 1;
75 static gint gtk_check_menu_item_draw (GtkWidget *widget,
77 static void gtk_check_menu_item_activate (GtkMenuItem *menu_item);
78 static void gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
80 static void gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
82 static void gtk_check_menu_item_set_property (GObject *object,
86 static void gtk_check_menu_item_get_property (GObject *object,
91 static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface);
92 static void gtk_check_menu_item_update (GtkActivatable *activatable,
94 const gchar *property_name);
95 static void gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
98 static GtkActivatableIface *parent_activatable_iface;
99 static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
101 G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
102 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
103 gtk_check_menu_item_activatable_interface_init))
106 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
108 GObjectClass *gobject_class;
109 GtkWidgetClass *widget_class;
110 GtkMenuItemClass *menu_item_class;
112 gobject_class = G_OBJECT_CLASS (klass);
113 widget_class = (GtkWidgetClass*) klass;
114 menu_item_class = (GtkMenuItemClass*) klass;
116 gobject_class->set_property = gtk_check_menu_item_set_property;
117 gobject_class->get_property = gtk_check_menu_item_get_property;
119 g_object_class_install_property (gobject_class,
121 g_param_spec_boolean ("active",
123 P_("Whether the menu item is checked"),
125 GTK_PARAM_READWRITE));
127 g_object_class_install_property (gobject_class,
129 g_param_spec_boolean ("inconsistent",
131 P_("Whether to display an \"inconsistent\" state"),
133 GTK_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class,
137 g_param_spec_boolean ("draw-as-radio",
138 P_("Draw as radio menu item"),
139 P_("Whether the menu item looks like a radio menu item"),
141 GTK_PARAM_READWRITE));
143 gtk_widget_class_install_style_property (widget_class,
144 g_param_spec_int ("indicator-size",
145 P_("Indicator Size"),
146 P_("Size of check or radio indicator"),
150 GTK_PARAM_READABLE));
152 widget_class->draw = gtk_check_menu_item_draw;
154 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_CHECK_MENU_ITEM_ACCESSIBLE);
156 menu_item_class->activate = gtk_check_menu_item_activate;
157 menu_item_class->hide_on_activate = FALSE;
158 menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
160 klass->toggled = NULL;
161 klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
164 * GtkCheckMenuItem::toggled:
165 * @checkmenuitem: the object which received the signal.
167 * This signal is emitted when the state of the check box is changed.
169 * A signal handler can use gtk_check_menu_item_get_active()
170 * to discover the new state.
172 check_menu_item_signals[TOGGLED] =
173 g_signal_new (I_("toggled"),
174 G_OBJECT_CLASS_TYPE (gobject_class),
176 G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
178 _gtk_marshal_VOID__VOID,
181 g_type_class_add_private (klass, sizeof (GtkCheckMenuItemPrivate));
185 gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface)
187 parent_activatable_iface = g_type_interface_peek_parent (iface);
188 iface->update = gtk_check_menu_item_update;
189 iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
193 gtk_check_menu_item_update (GtkActivatable *activatable,
195 const gchar *property_name)
197 GtkCheckMenuItem *check_menu_item;
199 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
201 parent_activatable_iface->update (activatable, action, property_name);
203 if (strcmp (property_name, "active") == 0)
205 gtk_action_block_activate (action);
206 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
207 gtk_action_unblock_activate (action);
210 if (!gtk_activatable_get_use_action_appearance (activatable))
213 if (strcmp (property_name, "draw-as-radio") == 0)
214 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
215 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
219 gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
222 GtkCheckMenuItem *check_menu_item;
224 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
226 parent_activatable_iface->sync_action_properties (activatable, action);
228 if (!GTK_IS_TOGGLE_ACTION (action))
231 gtk_action_block_activate (action);
232 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
233 gtk_action_unblock_activate (action);
235 if (!gtk_activatable_get_use_action_appearance (activatable))
238 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
239 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
243 * gtk_check_menu_item_new:
245 * Creates a new #GtkCheckMenuItem.
247 * Returns: a new #GtkCheckMenuItem.
250 gtk_check_menu_item_new (void)
252 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
256 * gtk_check_menu_item_new_with_label:
257 * @label: the string to use for the label.
259 * Creates a new #GtkCheckMenuItem with a label.
261 * Returns: a new #GtkCheckMenuItem.
264 gtk_check_menu_item_new_with_label (const gchar *label)
266 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
273 * gtk_check_menu_item_new_with_mnemonic:
274 * @label: The text of the button, with an underscore in front of the
276 * @returns: a new #GtkCheckMenuItem
278 * Creates a new #GtkCheckMenuItem containing a label. The label
279 * will be created using gtk_label_new_with_mnemonic(), so underscores
280 * in @label indicate the mnemonic for the menu item.
283 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
285 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
287 "use-underline", TRUE,
292 * gtk_check_menu_item_set_active:
293 * @check_menu_item: a #GtkCheckMenuItem.
294 * @is_active: boolean value indicating whether the check box is active.
296 * Sets the active state of the menu item's check box.
299 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
302 GtkCheckMenuItemPrivate *priv;
304 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
306 priv = check_menu_item->priv;
308 is_active = is_active != 0;
310 if (priv->active != is_active)
311 gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
315 * gtk_check_menu_item_get_active:
316 * @check_menu_item: a #GtkCheckMenuItem
318 * Returns whether the check menu item is active. See
319 * gtk_check_menu_item_set_active ().
321 * Return value: %TRUE if the menu item is checked.
324 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
326 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
328 return check_menu_item->priv->active;
332 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
335 guint toggle_spacing;
336 guint indicator_size;
338 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
340 gtk_widget_style_get (GTK_WIDGET (menu_item),
341 "toggle-spacing", &toggle_spacing,
342 "indicator-size", &indicator_size,
345 *requisition = indicator_size + toggle_spacing;
349 * gtk_check_menu_item_toggled:
350 * @check_menu_item: a #GtkCheckMenuItem.
352 * Emits the #GtkCheckMenuItem::toggled signal.
355 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
357 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
361 * gtk_check_menu_item_set_inconsistent:
362 * @check_menu_item: a #GtkCheckMenuItem
363 * @setting: %TRUE to display an "inconsistent" third state check
365 * If the user has selected a range of elements (such as some text or
366 * spreadsheet cells) that are affected by a boolean setting, and the
367 * current values in that range are inconsistent, you may want to
368 * display the check in an "in between" state. This function turns on
369 * "in between" display. Normally you would turn off the inconsistent
370 * state again if the user explicitly selects a setting. This has to be
371 * done manually, gtk_check_menu_item_set_inconsistent() only affects
372 * visual appearance, it doesn't affect the semantics of the widget.
376 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
379 GtkCheckMenuItemPrivate *priv;
381 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
383 priv = check_menu_item->priv;
385 setting = setting != FALSE;
387 if (setting != priv->inconsistent)
389 priv->inconsistent = setting;
390 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
391 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
396 * gtk_check_menu_item_get_inconsistent:
397 * @check_menu_item: a #GtkCheckMenuItem
399 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
401 * Return value: %TRUE if inconsistent
404 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
406 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
408 return check_menu_item->priv->inconsistent;
412 * gtk_check_menu_item_set_draw_as_radio:
413 * @check_menu_item: a #GtkCheckMenuItem
414 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
416 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
421 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
422 gboolean draw_as_radio)
424 GtkCheckMenuItemPrivate *priv;
426 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
428 priv = check_menu_item->priv;
430 draw_as_radio = draw_as_radio != FALSE;
432 if (draw_as_radio != priv->draw_as_radio)
434 priv->draw_as_radio = draw_as_radio;
436 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
438 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
443 * gtk_check_menu_item_get_draw_as_radio:
444 * @check_menu_item: a #GtkCheckMenuItem
446 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
448 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
453 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
455 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
457 return check_menu_item->priv->draw_as_radio;
461 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
463 GtkCheckMenuItemPrivate *priv;
465 check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
466 GTK_TYPE_CHECK_MENU_ITEM,
467 GtkCheckMenuItemPrivate);
468 priv = check_menu_item->priv;
470 priv->active = FALSE;
471 priv->always_show_toggle = TRUE;
475 gtk_check_menu_item_draw (GtkWidget *widget,
478 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (widget);
480 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw)
481 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw (widget, cr);
483 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
484 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, cr);
490 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
492 GtkCheckMenuItemPrivate *priv;
494 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
495 priv = check_menu_item->priv;
497 priv->active = !priv->active;
499 gtk_check_menu_item_toggled (check_menu_item);
500 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
502 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
504 g_object_notify (G_OBJECT (check_menu_item), "active");
508 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
511 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
515 widget = GTK_WIDGET (check_menu_item);
517 if (gtk_widget_is_drawable (widget))
519 GtkAllocation allocation;
520 GtkStyleContext *context;
524 guint toggle_spacing;
525 guint horizontal_padding;
526 guint indicator_size;
530 context = gtk_widget_get_style_context (widget);
531 state = gtk_widget_get_state_flags (widget);
532 gtk_style_context_get_padding (context, state, &padding);
534 gtk_widget_get_allocation (widget, &allocation);
536 gtk_widget_style_get (widget,
537 "toggle-spacing", &toggle_spacing,
538 "horizontal-padding", &horizontal_padding,
539 "indicator-size", &indicator_size,
542 toggle_size = GTK_MENU_ITEM (check_menu_item)->priv->toggle_size;
543 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
544 offset = border_width + padding.left + 2;
546 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
548 x = offset + horizontal_padding +
549 (toggle_size - toggle_spacing - indicator_size) / 2;
553 x = allocation.width -
554 offset - horizontal_padding - toggle_size + toggle_spacing +
555 (toggle_size - toggle_spacing - indicator_size) / 2;
558 y = (allocation.height - indicator_size) / 2;
561 priv->always_show_toggle ||
562 (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT))
564 gtk_style_context_save (context);
566 if (priv->inconsistent)
567 state |= GTK_STATE_FLAG_INCONSISTENT;
568 else if (priv->active)
569 state |= GTK_STATE_FLAG_ACTIVE;
571 if (!gtk_widget_is_sensitive (widget))
572 state |= GTK_STATE_FLAG_INSENSITIVE;
574 gtk_style_context_set_state (context, state);
576 if (priv->draw_as_radio)
578 gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
579 gtk_render_option (context, cr, x, y,
580 indicator_size, indicator_size);
584 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
585 gtk_render_check (context, cr, x, y,
586 indicator_size, indicator_size);
589 gtk_style_context_restore (context);
596 gtk_check_menu_item_get_property (GObject *object,
601 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
602 GtkCheckMenuItemPrivate *priv = checkitem->priv;
607 g_value_set_boolean (value, priv->active);
609 case PROP_INCONSISTENT:
610 g_value_set_boolean (value, priv->inconsistent);
612 case PROP_DRAW_AS_RADIO:
613 g_value_set_boolean (value, priv->draw_as_radio);
616 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
623 gtk_check_menu_item_set_property (GObject *object,
628 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
633 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
635 case PROP_INCONSISTENT:
636 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
638 case PROP_DRAW_AS_RADIO:
639 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
642 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
651 * _gtk_check_menu_item_set_active:
652 * @check_menu_item: a #GtkCheckMenuItem
653 * @is_active: whether the action is active or not
655 * Sets the #GtkCheckMenuItem:active property directly. This function does
656 * not emit signals or notifications: it is left to the caller to do so.
659 _gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
662 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
664 priv->active = is_active;