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"
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 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
274 * @returns: a new #GtkCheckMenuItem
276 * Creates a new #GtkCheckMenuItem containing a label. The label
277 * will be created using gtk_label_new_with_mnemonic(), so underscores
278 * in @label indicate the mnemonic for the menu item.
281 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
283 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
285 "use-underline", TRUE,
290 * gtk_check_menu_item_set_active:
291 * @check_menu_item: a #GtkCheckMenuItem.
292 * @is_active: boolean value indicating whether the check box is active.
294 * Sets the active state of the menu item's check box.
297 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
300 GtkCheckMenuItemPrivate *priv;
302 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
304 priv = check_menu_item->priv;
306 is_active = is_active != 0;
308 if (priv->active != is_active)
309 gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
313 * gtk_check_menu_item_get_active:
314 * @check_menu_item: a #GtkCheckMenuItem
316 * Returns whether the check menu item is active. See
317 * gtk_check_menu_item_set_active ().
319 * Return value: %TRUE if the menu item is checked.
322 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
324 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
326 return check_menu_item->priv->active;
330 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
333 guint toggle_spacing;
334 guint indicator_size;
336 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
338 gtk_widget_style_get (GTK_WIDGET (menu_item),
339 "toggle-spacing", &toggle_spacing,
340 "indicator-size", &indicator_size,
343 *requisition = indicator_size + toggle_spacing;
347 * gtk_check_menu_item_toggled:
348 * @check_menu_item: a #GtkCheckMenuItem.
350 * Emits the #GtkCheckMenuItem::toggled signal.
353 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
355 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
359 * gtk_check_menu_item_set_inconsistent:
360 * @check_menu_item: a #GtkCheckMenuItem
361 * @setting: %TRUE to display an "inconsistent" third state check
363 * If the user has selected a range of elements (such as some text or
364 * spreadsheet cells) that are affected by a boolean setting, and the
365 * current values in that range are inconsistent, you may want to
366 * display the check in an "in between" state. This function turns on
367 * "in between" display. Normally you would turn off the inconsistent
368 * state again if the user explicitly selects a setting. This has to be
369 * done manually, gtk_check_menu_item_set_inconsistent() only affects
370 * visual appearance, it doesn't affect the semantics of the widget.
374 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
377 GtkCheckMenuItemPrivate *priv;
379 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
381 priv = check_menu_item->priv;
383 setting = setting != FALSE;
385 if (setting != priv->inconsistent)
387 priv->inconsistent = setting;
388 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
389 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
394 * gtk_check_menu_item_get_inconsistent:
395 * @check_menu_item: a #GtkCheckMenuItem
397 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
399 * Return value: %TRUE if inconsistent
402 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
404 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
406 return check_menu_item->priv->inconsistent;
410 * gtk_check_menu_item_set_draw_as_radio:
411 * @check_menu_item: a #GtkCheckMenuItem
412 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
414 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
419 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
420 gboolean draw_as_radio)
422 GtkCheckMenuItemPrivate *priv;
424 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
426 priv = check_menu_item->priv;
428 draw_as_radio = draw_as_radio != FALSE;
430 if (draw_as_radio != priv->draw_as_radio)
432 priv->draw_as_radio = draw_as_radio;
434 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
436 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
441 * gtk_check_menu_item_get_draw_as_radio:
442 * @check_menu_item: a #GtkCheckMenuItem
444 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
446 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
451 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
453 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
455 return check_menu_item->priv->draw_as_radio;
459 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
461 GtkCheckMenuItemPrivate *priv;
463 check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
464 GTK_TYPE_CHECK_MENU_ITEM,
465 GtkCheckMenuItemPrivate);
466 priv = check_menu_item->priv;
468 priv->active = FALSE;
469 priv->always_show_toggle = TRUE;
473 gtk_check_menu_item_draw (GtkWidget *widget,
476 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (widget);
478 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw)
479 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw (widget, cr);
481 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
482 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, cr);
488 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
490 GtkCheckMenuItemPrivate *priv;
492 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
493 priv = check_menu_item->priv;
495 priv->active = !priv->active;
497 gtk_check_menu_item_toggled (check_menu_item);
498 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
500 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
502 g_object_notify (G_OBJECT (check_menu_item), "active");
506 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
509 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
513 widget = GTK_WIDGET (check_menu_item);
515 if (gtk_widget_is_drawable (widget))
517 GtkAllocation allocation;
518 GtkStyleContext *context;
522 guint toggle_spacing;
523 guint horizontal_padding;
524 guint indicator_size;
528 context = gtk_widget_get_style_context (widget);
529 state = gtk_widget_get_state_flags (widget);
530 gtk_style_context_get_padding (context, state, &padding);
532 gtk_widget_get_allocation (widget, &allocation);
534 gtk_widget_style_get (widget,
535 "toggle-spacing", &toggle_spacing,
536 "horizontal-padding", &horizontal_padding,
537 "indicator-size", &indicator_size,
540 toggle_size = GTK_MENU_ITEM (check_menu_item)->priv->toggle_size;
541 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
542 offset = border_width + padding.left + 2;
544 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
546 x = offset + horizontal_padding +
547 (toggle_size - toggle_spacing - indicator_size) / 2;
551 x = allocation.width -
552 offset - horizontal_padding - toggle_size + toggle_spacing +
553 (toggle_size - toggle_spacing - indicator_size) / 2;
556 y = (allocation.height - indicator_size) / 2;
559 priv->always_show_toggle ||
560 (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT))
562 gtk_style_context_save (context);
564 if (priv->inconsistent)
565 state |= GTK_STATE_FLAG_INCONSISTENT;
566 else if (priv->active)
567 state |= GTK_STATE_FLAG_ACTIVE;
569 if (!gtk_widget_is_sensitive (widget))
570 state |= GTK_STATE_FLAG_INSENSITIVE;
572 gtk_style_context_set_state (context, state);
574 if (priv->draw_as_radio)
576 gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
577 gtk_render_option (context, cr, x, y,
578 indicator_size, indicator_size);
582 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
583 gtk_render_check (context, cr, x, y,
584 indicator_size, indicator_size);
587 gtk_style_context_restore (context);
594 gtk_check_menu_item_get_property (GObject *object,
599 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
600 GtkCheckMenuItemPrivate *priv = checkitem->priv;
605 g_value_set_boolean (value, priv->active);
607 case PROP_INCONSISTENT:
608 g_value_set_boolean (value, priv->inconsistent);
610 case PROP_DRAW_AS_RADIO:
611 g_value_set_boolean (value, priv->draw_as_radio);
614 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
621 gtk_check_menu_item_set_property (GObject *object,
626 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
631 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
633 case PROP_INCONSISTENT:
634 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
636 case PROP_DRAW_AS_RADIO:
637 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
640 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
649 * _gtk_check_menu_item_set_active:
650 * @check_menu_item: a #GtkCheckMenuItem
651 * @is_active: whether the action is active or not
653 * Sets the #GtkCheckMenuItem:active property directly. This function does
654 * not emit signals or notifications: it is left to the caller to do so.
657 _gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
660 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
662 priv->active = is_active;