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 "gtkaccellabel.h"
30 #include "gtkactivatable.h"
31 #include "gtktoggleaction.h"
32 #include "gtkmarshalers.h"
33 #include "gtkprivate.h"
38 struct _GtkCheckMenuItemPriv
41 guint always_show_toggle : 1;
42 guint draw_as_radio : 1;
43 guint inconsistent : 1;
58 static gint gtk_check_menu_item_expose (GtkWidget *widget,
59 GdkEventExpose *event);
60 static void gtk_check_menu_item_activate (GtkMenuItem *menu_item);
61 static void gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
63 static void gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
65 static void gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
67 static void gtk_check_menu_item_set_property (GObject *object,
71 static void gtk_check_menu_item_get_property (GObject *object,
76 static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface);
77 static void gtk_check_menu_item_update (GtkActivatable *activatable,
79 const gchar *property_name);
80 static void gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
83 static GtkActivatableIface *parent_activatable_iface;
84 static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
86 G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
87 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
88 gtk_check_menu_item_activatable_interface_init))
91 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
93 GObjectClass *gobject_class;
94 GtkWidgetClass *widget_class;
95 GtkMenuItemClass *menu_item_class;
97 gobject_class = G_OBJECT_CLASS (klass);
98 widget_class = (GtkWidgetClass*) klass;
99 menu_item_class = (GtkMenuItemClass*) klass;
101 gobject_class->set_property = gtk_check_menu_item_set_property;
102 gobject_class->get_property = gtk_check_menu_item_get_property;
104 g_object_class_install_property (gobject_class,
106 g_param_spec_boolean ("active",
108 P_("Whether the menu item is checked"),
110 GTK_PARAM_READWRITE));
112 g_object_class_install_property (gobject_class,
114 g_param_spec_boolean ("inconsistent",
116 P_("Whether to display an \"inconsistent\" state"),
118 GTK_PARAM_READWRITE));
120 g_object_class_install_property (gobject_class,
122 g_param_spec_boolean ("draw-as-radio",
123 P_("Draw as radio menu item"),
124 P_("Whether the menu item looks like a radio menu item"),
126 GTK_PARAM_READWRITE));
128 gtk_widget_class_install_style_property (widget_class,
129 g_param_spec_int ("indicator-size",
130 P_("Indicator Size"),
131 P_("Size of check or radio indicator"),
135 GTK_PARAM_READABLE));
137 widget_class->expose_event = gtk_check_menu_item_expose;
139 menu_item_class->activate = gtk_check_menu_item_activate;
140 menu_item_class->hide_on_activate = FALSE;
141 menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
143 klass->toggled = NULL;
144 klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
146 check_menu_item_signals[TOGGLED] =
147 g_signal_new (I_("toggled"),
148 G_OBJECT_CLASS_TYPE (gobject_class),
150 G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
152 _gtk_marshal_VOID__VOID,
155 g_type_class_add_private (klass, sizeof (GtkCheckMenuItemPriv));
159 gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface)
161 parent_activatable_iface = g_type_interface_peek_parent (iface);
162 iface->update = gtk_check_menu_item_update;
163 iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
167 gtk_check_menu_item_update (GtkActivatable *activatable,
169 const gchar *property_name)
171 GtkCheckMenuItem *check_menu_item;
173 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
175 parent_activatable_iface->update (activatable, action, property_name);
177 if (strcmp (property_name, "active") == 0)
179 gtk_action_block_activate (action);
180 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
181 gtk_action_unblock_activate (action);
184 if (!gtk_activatable_get_use_action_appearance (activatable))
187 if (strcmp (property_name, "draw-as-radio") == 0)
188 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
189 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
193 gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
196 GtkCheckMenuItem *check_menu_item;
198 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
200 parent_activatable_iface->sync_action_properties (activatable, action);
202 if (!GTK_IS_TOGGLE_ACTION (action))
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);
209 if (!gtk_activatable_get_use_action_appearance (activatable))
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_new (void)
219 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
223 gtk_check_menu_item_new_with_label (const gchar *label)
225 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
232 * gtk_check_menu_item_new_with_mnemonic:
233 * @label: The text of the button, with an underscore in front of the
235 * @returns: a new #GtkCheckMenuItem
237 * Creates a new #GtkCheckMenuItem containing a label. The label
238 * will be created using gtk_label_new_with_mnemonic(), so underscores
239 * in @label indicate the mnemonic for the menu item.
242 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
244 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
246 "use-underline", TRUE,
251 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
254 GtkCheckMenuItemPriv *priv;
256 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
258 priv = check_menu_item->priv;
260 is_active = is_active != 0;
262 if (priv->active != is_active)
263 gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
267 * gtk_check_menu_item_get_active:
268 * @check_menu_item: a #GtkCheckMenuItem
270 * Returns whether the check menu item is active. See
271 * gtk_check_menu_item_set_active ().
273 * Return value: %TRUE if the menu item is checked.
276 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
278 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
280 return check_menu_item->priv->active;
284 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
287 guint toggle_spacing;
288 guint indicator_size;
290 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
292 gtk_widget_style_get (GTK_WIDGET (menu_item),
293 "toggle-spacing", &toggle_spacing,
294 "indicator-size", &indicator_size,
297 *requisition = indicator_size + toggle_spacing;
301 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
303 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
307 * gtk_check_menu_item_set_inconsistent:
308 * @check_menu_item: a #GtkCheckMenuItem
309 * @setting: %TRUE to display an "inconsistent" third state check
311 * If the user has selected a range of elements (such as some text or
312 * spreadsheet cells) that are affected by a boolean setting, and the
313 * current values in that range are inconsistent, you may want to
314 * display the check in an "in between" state. This function turns on
315 * "in between" display. Normally you would turn off the inconsistent
316 * state again if the user explicitly selects a setting. This has to be
317 * done manually, gtk_check_menu_item_set_inconsistent() only affects
318 * visual appearance, it doesn't affect the semantics of the widget.
322 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
325 GtkCheckMenuItemPriv *priv;
327 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
329 priv = check_menu_item->priv;
331 setting = setting != FALSE;
333 if (setting != priv->inconsistent)
335 priv->inconsistent = setting;
336 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
337 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
342 * gtk_check_menu_item_get_inconsistent:
343 * @check_menu_item: a #GtkCheckMenuItem
345 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
347 * Return value: %TRUE if inconsistent
350 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
352 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
354 return check_menu_item->priv->inconsistent;
358 * gtk_check_menu_item_set_draw_as_radio:
359 * @check_menu_item: a #GtkCheckMenuItem
360 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
362 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
367 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
368 gboolean draw_as_radio)
370 GtkCheckMenuItemPriv *priv;
372 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
374 priv = check_menu_item->priv;
376 draw_as_radio = draw_as_radio != FALSE;
378 if (draw_as_radio != priv->draw_as_radio)
380 priv->draw_as_radio = draw_as_radio;
382 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
384 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
389 * gtk_check_menu_item_get_draw_as_radio:
390 * @check_menu_item: a #GtkCheckMenuItem
392 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
394 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
399 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
401 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
403 return check_menu_item->priv->draw_as_radio;
407 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
409 GtkCheckMenuItemPriv *priv;
411 check_menu_item->priv = G_TYPE_INSTANCE_GET_PRIVATE (check_menu_item,
412 GTK_TYPE_CHECK_MENU_ITEM,
413 GtkCheckMenuItemPriv);
414 priv = check_menu_item->priv;
416 priv->active = FALSE;
417 priv->always_show_toggle = TRUE;
421 gtk_check_menu_item_expose (GtkWidget *widget,
422 GdkEventExpose *event)
424 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event)
425 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event (widget, event);
427 gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);
433 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
435 GtkCheckMenuItemPriv *priv;
437 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
438 priv = check_menu_item->priv;
440 priv->active = !priv->active;
442 gtk_check_menu_item_toggled (check_menu_item);
443 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
445 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
447 g_object_notify (G_OBJECT (check_menu_item), "active");
451 gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
454 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
455 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, area);
459 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
462 GtkCheckMenuItemPriv *priv = check_menu_item->priv;
464 GtkStateType state_type;
465 GtkShadowType shadow_type;
468 widget = GTK_WIDGET (check_menu_item);
470 if (gtk_widget_is_drawable (widget))
475 guint toggle_spacing;
476 guint horizontal_padding;
477 guint indicator_size;
479 gtk_widget_style_get (widget,
480 "toggle-spacing", &toggle_spacing,
481 "horizontal-padding", &horizontal_padding,
482 "indicator-size", &indicator_size,
485 toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
486 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
487 offset = border_width + widget->style->xthickness + 2;
489 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
491 x = widget->allocation.x + offset + horizontal_padding +
492 (toggle_size - toggle_spacing - indicator_size) / 2;
496 x = widget->allocation.x + widget->allocation.width -
497 offset - horizontal_padding - toggle_size + toggle_spacing +
498 (toggle_size - toggle_spacing - indicator_size) / 2;
501 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
504 priv->always_show_toggle ||
505 (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT))
507 state_type = gtk_widget_get_state (widget);
509 if (priv->inconsistent)
510 shadow_type = GTK_SHADOW_ETCHED_IN;
511 else if (priv->active)
512 shadow_type = GTK_SHADOW_IN;
514 shadow_type = GTK_SHADOW_OUT;
516 if (!gtk_widget_is_sensitive (widget))
517 state_type = GTK_STATE_INSENSITIVE;
519 if (priv->draw_as_radio)
521 gtk_paint_option (widget->style, widget->window,
522 state_type, shadow_type,
523 area, widget, "option",
524 x, y, indicator_size, indicator_size);
528 gtk_paint_check (widget->style, widget->window,
529 state_type, shadow_type,
530 area, widget, "check",
531 x, y, indicator_size, indicator_size);
539 gtk_check_menu_item_get_property (GObject *object,
544 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
545 GtkCheckMenuItemPriv *priv = checkitem->priv;
550 g_value_set_boolean (value, priv->active);
552 case PROP_INCONSISTENT:
553 g_value_set_boolean (value, priv->inconsistent);
555 case PROP_DRAW_AS_RADIO:
556 g_value_set_boolean (value, priv->draw_as_radio);
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
566 gtk_check_menu_item_set_property (GObject *object,
571 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
576 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
578 case PROP_INCONSISTENT:
579 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
581 case PROP_DRAW_AS_RADIO:
582 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
585 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);