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 struct _GtkCheckMenuItemPrivate
42 guint always_show_toggle : 1;
43 guint draw_as_radio : 1;
44 guint inconsistent : 1;
59 static gint gtk_check_menu_item_draw (GtkWidget *widget,
61 static void gtk_check_menu_item_activate (GtkMenuItem *menu_item);
62 static void gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
64 static void gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
66 static void gtk_check_menu_item_set_property (GObject *object,
70 static void gtk_check_menu_item_get_property (GObject *object,
75 static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface);
76 static void gtk_check_menu_item_update (GtkActivatable *activatable,
78 const gchar *property_name);
79 static void gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
82 static GtkActivatableIface *parent_activatable_iface;
83 static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
85 G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
86 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
87 gtk_check_menu_item_activatable_interface_init))
90 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
92 GObjectClass *gobject_class;
93 GtkWidgetClass *widget_class;
94 GtkMenuItemClass *menu_item_class;
96 gobject_class = G_OBJECT_CLASS (klass);
97 widget_class = (GtkWidgetClass*) klass;
98 menu_item_class = (GtkMenuItemClass*) klass;
100 gobject_class->set_property = gtk_check_menu_item_set_property;
101 gobject_class->get_property = gtk_check_menu_item_get_property;
103 g_object_class_install_property (gobject_class,
105 g_param_spec_boolean ("active",
107 P_("Whether the menu item is checked"),
109 GTK_PARAM_READWRITE));
111 g_object_class_install_property (gobject_class,
113 g_param_spec_boolean ("inconsistent",
115 P_("Whether to display an \"inconsistent\" state"),
117 GTK_PARAM_READWRITE));
119 g_object_class_install_property (gobject_class,
121 g_param_spec_boolean ("draw-as-radio",
122 P_("Draw as radio menu item"),
123 P_("Whether the menu item looks like a radio menu item"),
125 GTK_PARAM_READWRITE));
127 gtk_widget_class_install_style_property (widget_class,
128 g_param_spec_int ("indicator-size",
129 P_("Indicator Size"),
130 P_("Size of check or radio indicator"),
134 GTK_PARAM_READABLE));
136 widget_class->draw = gtk_check_menu_item_draw;
138 menu_item_class->activate = gtk_check_menu_item_activate;
139 menu_item_class->hide_on_activate = FALSE;
140 menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
142 klass->toggled = NULL;
143 klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
145 check_menu_item_signals[TOGGLED] =
146 g_signal_new (I_("toggled"),
147 G_OBJECT_CLASS_TYPE (gobject_class),
149 G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
151 _gtk_marshal_VOID__VOID,
154 g_type_class_add_private (klass, sizeof (GtkCheckMenuItemPrivate));
158 gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface)
160 parent_activatable_iface = g_type_interface_peek_parent (iface);
161 iface->update = gtk_check_menu_item_update;
162 iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
166 gtk_check_menu_item_update (GtkActivatable *activatable,
168 const gchar *property_name)
170 GtkCheckMenuItem *check_menu_item;
172 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
174 parent_activatable_iface->update (activatable, action, property_name);
176 if (strcmp (property_name, "active") == 0)
178 gtk_action_block_activate (action);
179 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
180 gtk_action_unblock_activate (action);
183 if (!gtk_activatable_get_use_action_appearance (activatable))
186 if (strcmp (property_name, "draw-as-radio") == 0)
187 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
188 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
192 gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
195 GtkCheckMenuItem *check_menu_item;
197 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
199 parent_activatable_iface->sync_action_properties (activatable, action);
201 if (!GTK_IS_TOGGLE_ACTION (action))
204 gtk_action_block_activate (action);
205 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
206 gtk_action_unblock_activate (action);
208 if (!gtk_activatable_get_use_action_appearance (activatable))
211 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
212 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
216 gtk_check_menu_item_new (void)
218 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
222 gtk_check_menu_item_new_with_label (const gchar *label)
224 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
231 * gtk_check_menu_item_new_with_mnemonic:
232 * @label: The text of the button, with an underscore in front of the
234 * @returns: a new #GtkCheckMenuItem
236 * Creates a new #GtkCheckMenuItem containing a label. The label
237 * will be created using gtk_label_new_with_mnemonic(), so underscores
238 * in @label indicate the mnemonic for the menu item.
241 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
243 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
245 "use-underline", TRUE,
250 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
253 GtkCheckMenuItemPrivate *priv;
255 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
257 priv = check_menu_item->priv;
259 is_active = is_active != 0;
261 if (priv->active != is_active)
262 gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
266 * gtk_check_menu_item_get_active:
267 * @check_menu_item: a #GtkCheckMenuItem
269 * Returns whether the check menu item is active. See
270 * gtk_check_menu_item_set_active ().
272 * Return value: %TRUE if the menu item is checked.
275 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
277 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
279 return check_menu_item->priv->active;
283 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
286 guint toggle_spacing;
287 guint indicator_size;
289 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
291 gtk_widget_style_get (GTK_WIDGET (menu_item),
292 "toggle-spacing", &toggle_spacing,
293 "indicator-size", &indicator_size,
296 *requisition = indicator_size + toggle_spacing;
300 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
302 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
306 * gtk_check_menu_item_set_inconsistent:
307 * @check_menu_item: a #GtkCheckMenuItem
308 * @setting: %TRUE to display an "inconsistent" third state check
310 * If the user has selected a range of elements (such as some text or
311 * spreadsheet cells) that are affected by a boolean setting, and the
312 * current values in that range are inconsistent, you may want to
313 * display the check in an "in between" state. This function turns on
314 * "in between" display. Normally you would turn off the inconsistent
315 * state again if the user explicitly selects a setting. This has to be
316 * done manually, gtk_check_menu_item_set_inconsistent() only affects
317 * visual appearance, it doesn't affect the semantics of the widget.
321 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
324 GtkCheckMenuItemPrivate *priv;
326 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
328 priv = check_menu_item->priv;
330 setting = setting != FALSE;
332 if (setting != priv->inconsistent)
334 priv->inconsistent = setting;
335 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
336 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
341 * gtk_check_menu_item_get_inconsistent:
342 * @check_menu_item: a #GtkCheckMenuItem
344 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
346 * Return value: %TRUE if inconsistent
349 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
351 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
353 return check_menu_item->priv->inconsistent;
357 * gtk_check_menu_item_set_draw_as_radio:
358 * @check_menu_item: a #GtkCheckMenuItem
359 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
361 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
366 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
367 gboolean draw_as_radio)
369 GtkCheckMenuItemPrivate *priv;
371 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
373 priv = check_menu_item->priv;
375 draw_as_radio = draw_as_radio != FALSE;
377 if (draw_as_radio != priv->draw_as_radio)
379 priv->draw_as_radio = draw_as_radio;
381 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
383 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
388 * gtk_check_menu_item_get_draw_as_radio:
389 * @check_menu_item: a #GtkCheckMenuItem
391 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
393 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
398 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
400 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
402 return check_menu_item->priv->draw_as_radio;
406 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
408 GtkCheckMenuItemPrivate *priv;
410 check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
411 GTK_TYPE_CHECK_MENU_ITEM,
412 GtkCheckMenuItemPrivate);
413 priv = check_menu_item->priv;
415 priv->active = FALSE;
416 priv->always_show_toggle = TRUE;
420 gtk_check_menu_item_draw (GtkWidget *widget,
423 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (widget);
425 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw)
426 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->draw (widget, cr);
428 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
429 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, cr);
435 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
437 GtkCheckMenuItemPrivate *priv;
439 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
440 priv = check_menu_item->priv;
442 priv->active = !priv->active;
444 gtk_check_menu_item_toggled (check_menu_item);
445 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
447 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
449 g_object_notify (G_OBJECT (check_menu_item), "active");
453 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
456 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
460 widget = GTK_WIDGET (check_menu_item);
462 if (gtk_widget_is_drawable (widget))
464 GtkAllocation allocation;
465 GtkStyleContext *context;
469 guint toggle_spacing;
470 guint horizontal_padding;
471 guint indicator_size;
475 context = gtk_widget_get_style_context (widget);
476 state = gtk_widget_get_state_flags (widget);
477 gtk_style_context_get_padding (context, state, &padding);
479 gtk_widget_get_allocation (widget, &allocation);
481 gtk_widget_style_get (widget,
482 "toggle-spacing", &toggle_spacing,
483 "horizontal-padding", &horizontal_padding,
484 "indicator-size", &indicator_size,
487 toggle_size = GTK_MENU_ITEM (check_menu_item)->priv->toggle_size;
488 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
489 offset = border_width + padding.left + 2;
491 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
493 x = offset + horizontal_padding +
494 (toggle_size - toggle_spacing - indicator_size) / 2;
498 x = allocation.width -
499 offset - horizontal_padding - toggle_size + toggle_spacing +
500 (toggle_size - toggle_spacing - indicator_size) / 2;
503 y = (allocation.height - indicator_size) / 2;
506 priv->always_show_toggle ||
507 (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT))
511 window = gtk_widget_get_window (widget);
512 gtk_style_context_save (context);
514 if (priv->inconsistent)
515 state |= GTK_STATE_FLAG_INCONSISTENT;
516 else if (priv->active)
517 state |= GTK_STATE_FLAG_ACTIVE;
519 if (!gtk_widget_is_sensitive (widget))
520 state |= GTK_STATE_FLAG_INSENSITIVE;
522 gtk_style_context_set_state (context, state);
524 if (priv->draw_as_radio)
526 gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
527 gtk_render_option (context, cr, x, y,
528 indicator_size, indicator_size);
532 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
533 gtk_render_check (context, cr, x, y,
534 indicator_size, indicator_size);
537 gtk_style_context_restore (context);
544 gtk_check_menu_item_get_property (GObject *object,
549 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
550 GtkCheckMenuItemPrivate *priv = checkitem->priv;
555 g_value_set_boolean (value, priv->active);
557 case PROP_INCONSISTENT:
558 g_value_set_boolean (value, priv->inconsistent);
560 case PROP_DRAW_AS_RADIO:
561 g_value_set_boolean (value, priv->draw_as_radio);
564 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571 gtk_check_menu_item_set_property (GObject *object,
576 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
581 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
583 case PROP_INCONSISTENT:
584 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
586 case PROP_DRAW_AS_RADIO:
587 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
590 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
599 * _gtk_check_menu_item_set_active:
600 * @check_menu_item: a #GtkCheckMenuItem
601 * @is_active: whether the action is active or not
603 * Sets the #GtkCheckMenuItem:active property directly. This function does
604 * not emit signals or notifications: it is left to the caller to do so.
607 _gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
610 GtkCheckMenuItemPrivate *priv = check_menu_item->priv;
612 priv->active = is_active;