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"
49 static gint gtk_check_menu_item_expose (GtkWidget *widget,
50 GdkEventExpose *event);
51 static void gtk_check_menu_item_activate (GtkMenuItem *menu_item);
52 static void gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
54 static void gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
56 static void gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
58 static void gtk_check_menu_item_set_property (GObject *object,
62 static void gtk_check_menu_item_get_property (GObject *object,
67 static void gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface);
68 static void gtk_check_menu_item_update (GtkActivatable *activatable,
70 const gchar *property_name);
71 static void gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
74 static GtkActivatableIface *parent_activatable_iface;
75 static guint check_menu_item_signals[LAST_SIGNAL] = { 0 };
77 G_DEFINE_TYPE_WITH_CODE (GtkCheckMenuItem, gtk_check_menu_item, GTK_TYPE_MENU_ITEM,
78 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
79 gtk_check_menu_item_activatable_interface_init))
82 gtk_check_menu_item_class_init (GtkCheckMenuItemClass *klass)
84 GObjectClass *gobject_class;
85 GtkWidgetClass *widget_class;
86 GtkMenuItemClass *menu_item_class;
88 gobject_class = G_OBJECT_CLASS (klass);
89 widget_class = (GtkWidgetClass*) klass;
90 menu_item_class = (GtkMenuItemClass*) klass;
92 gobject_class->set_property = gtk_check_menu_item_set_property;
93 gobject_class->get_property = gtk_check_menu_item_get_property;
95 g_object_class_install_property (gobject_class,
97 g_param_spec_boolean ("active",
99 P_("Whether the menu item is checked"),
101 GTK_PARAM_READWRITE));
103 g_object_class_install_property (gobject_class,
105 g_param_spec_boolean ("inconsistent",
107 P_("Whether to display an \"inconsistent\" state"),
109 GTK_PARAM_READWRITE));
111 g_object_class_install_property (gobject_class,
113 g_param_spec_boolean ("draw-as-radio",
114 P_("Draw as radio menu item"),
115 P_("Whether the menu item looks like a radio menu item"),
117 GTK_PARAM_READWRITE));
119 gtk_widget_class_install_style_property (widget_class,
120 g_param_spec_int ("indicator-size",
121 P_("Indicator Size"),
122 P_("Size of check or radio indicator"),
126 GTK_PARAM_READABLE));
128 widget_class->expose_event = gtk_check_menu_item_expose;
130 menu_item_class->activate = gtk_check_menu_item_activate;
131 menu_item_class->hide_on_activate = FALSE;
132 menu_item_class->toggle_size_request = gtk_check_menu_item_toggle_size_request;
134 klass->toggled = NULL;
135 klass->draw_indicator = gtk_real_check_menu_item_draw_indicator;
137 check_menu_item_signals[TOGGLED] =
138 g_signal_new (I_("toggled"),
139 G_OBJECT_CLASS_TYPE (gobject_class),
141 G_STRUCT_OFFSET (GtkCheckMenuItemClass, toggled),
143 _gtk_marshal_VOID__VOID,
148 gtk_check_menu_item_activatable_interface_init (GtkActivatableIface *iface)
150 parent_activatable_iface = g_type_interface_peek_parent (iface);
151 iface->update = gtk_check_menu_item_update;
152 iface->sync_action_properties = gtk_check_menu_item_sync_action_properties;
156 gtk_check_menu_item_update (GtkActivatable *activatable,
158 const gchar *property_name)
160 GtkCheckMenuItem *check_menu_item;
162 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
164 parent_activatable_iface->update (activatable, action, property_name);
166 if (strcmp (property_name, "active") == 0)
168 gtk_action_block_activate (action);
169 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
170 gtk_action_unblock_activate (action);
173 if (!gtk_activatable_get_use_action_appearance (activatable))
176 if (strcmp (property_name, "draw-as-radio") == 0)
177 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
178 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
182 gtk_check_menu_item_sync_action_properties (GtkActivatable *activatable,
185 GtkCheckMenuItem *check_menu_item;
187 check_menu_item = GTK_CHECK_MENU_ITEM (activatable);
189 parent_activatable_iface->sync_action_properties (activatable, action);
191 if (!GTK_IS_TOGGLE_ACTION (action))
194 gtk_action_block_activate (action);
195 gtk_check_menu_item_set_active (check_menu_item, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
196 gtk_action_unblock_activate (action);
198 if (!gtk_activatable_get_use_action_appearance (activatable))
201 gtk_check_menu_item_set_draw_as_radio (check_menu_item,
202 gtk_toggle_action_get_draw_as_radio (GTK_TOGGLE_ACTION (action)));
206 gtk_check_menu_item_new (void)
208 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM, NULL);
212 gtk_check_menu_item_new_with_label (const gchar *label)
214 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
221 * gtk_check_menu_item_new_with_mnemonic:
222 * @label: The text of the button, with an underscore in front of the
224 * @returns: a new #GtkCheckMenuItem
226 * Creates a new #GtkCheckMenuItem containing a label. The label
227 * will be created using gtk_label_new_with_mnemonic(), so underscores
228 * in @label indicate the mnemonic for the menu item.
231 gtk_check_menu_item_new_with_mnemonic (const gchar *label)
233 return g_object_new (GTK_TYPE_CHECK_MENU_ITEM,
235 "use-underline", TRUE,
240 gtk_check_menu_item_set_active (GtkCheckMenuItem *check_menu_item,
243 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
245 is_active = is_active != 0;
247 if (check_menu_item->active != is_active)
248 gtk_menu_item_activate (GTK_MENU_ITEM (check_menu_item));
252 * gtk_check_menu_item_get_active:
253 * @check_menu_item: a #GtkCheckMenuItem
255 * Returns whether the check menu item is active. See
256 * gtk_check_menu_item_set_active ().
258 * Return value: %TRUE if the menu item is checked.
261 gtk_check_menu_item_get_active (GtkCheckMenuItem *check_menu_item)
263 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
265 return check_menu_item->active;
269 gtk_check_menu_item_toggle_size_request (GtkMenuItem *menu_item,
272 guint toggle_spacing;
273 guint indicator_size;
275 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
277 gtk_widget_style_get (GTK_WIDGET (menu_item),
278 "toggle-spacing", &toggle_spacing,
279 "indicator-size", &indicator_size,
282 *requisition = indicator_size + toggle_spacing;
286 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
288 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
292 * gtk_check_menu_item_set_inconsistent:
293 * @check_menu_item: a #GtkCheckMenuItem
294 * @setting: %TRUE to display an "inconsistent" third state check
296 * If the user has selected a range of elements (such as some text or
297 * spreadsheet cells) that are affected by a boolean setting, and the
298 * current values in that range are inconsistent, you may want to
299 * display the check in an "in between" state. This function turns on
300 * "in between" display. Normally you would turn off the inconsistent
301 * state again if the user explicitly selects a setting. This has to be
302 * done manually, gtk_check_menu_item_set_inconsistent() only affects
303 * visual appearance, it doesn't affect the semantics of the widget.
307 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
310 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
312 setting = setting != FALSE;
314 if (setting != check_menu_item->inconsistent)
316 check_menu_item->inconsistent = setting;
317 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
318 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
323 * gtk_check_menu_item_get_inconsistent:
324 * @check_menu_item: a #GtkCheckMenuItem
326 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
328 * Return value: %TRUE if inconsistent
331 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
333 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
335 return check_menu_item->inconsistent;
339 * gtk_check_menu_item_set_draw_as_radio:
340 * @check_menu_item: a #GtkCheckMenuItem
341 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
343 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
348 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
349 gboolean draw_as_radio)
351 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
353 draw_as_radio = draw_as_radio != FALSE;
355 if (draw_as_radio != check_menu_item->draw_as_radio)
357 check_menu_item->draw_as_radio = draw_as_radio;
359 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
361 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
366 * gtk_check_menu_item_get_draw_as_radio:
367 * @check_menu_item: a #GtkCheckMenuItem
369 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
371 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
376 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
378 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
380 return check_menu_item->draw_as_radio;
384 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
386 check_menu_item->active = FALSE;
387 check_menu_item->always_show_toggle = TRUE;
391 gtk_check_menu_item_expose (GtkWidget *widget,
392 GdkEventExpose *event)
394 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event)
395 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event (widget, event);
397 gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);
403 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
405 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
406 check_menu_item->active = !check_menu_item->active;
408 gtk_check_menu_item_toggled (check_menu_item);
409 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
411 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
413 g_object_notify (G_OBJECT (check_menu_item), "active");
417 gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
420 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
421 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, area);
425 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
429 GtkStateType state_type;
430 GtkShadowType shadow_type;
433 widget = GTK_WIDGET (check_menu_item);
435 if (gtk_widget_is_drawable (widget))
439 guint toggle_spacing;
440 guint horizontal_padding;
441 guint indicator_size;
443 gtk_widget_style_get (widget,
444 "toggle-spacing", &toggle_spacing,
445 "horizontal-padding", &horizontal_padding,
446 "indicator-size", &indicator_size,
449 toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
450 offset = GTK_CONTAINER (check_menu_item)->border_width +
451 widget->style->xthickness + 2;
453 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
455 x = widget->allocation.x + offset + horizontal_padding +
456 (toggle_size - toggle_spacing - indicator_size) / 2;
460 x = widget->allocation.x + widget->allocation.width -
461 offset - horizontal_padding - toggle_size + toggle_spacing +
462 (toggle_size - toggle_spacing - indicator_size) / 2;
465 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
467 if (check_menu_item->active ||
468 check_menu_item->always_show_toggle ||
469 (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT))
471 state_type = gtk_widget_get_state (widget);
473 if (check_menu_item->inconsistent)
474 shadow_type = GTK_SHADOW_ETCHED_IN;
475 else if (check_menu_item->active)
476 shadow_type = GTK_SHADOW_IN;
478 shadow_type = GTK_SHADOW_OUT;
480 if (!gtk_widget_is_sensitive (widget))
481 state_type = GTK_STATE_INSENSITIVE;
483 if (check_menu_item->draw_as_radio)
485 gtk_paint_option (widget->style, widget->window,
486 state_type, shadow_type,
487 area, widget, "option",
488 x, y, indicator_size, indicator_size);
492 gtk_paint_check (widget->style, widget->window,
493 state_type, shadow_type,
494 area, widget, "check",
495 x, y, indicator_size, indicator_size);
503 gtk_check_menu_item_get_property (GObject *object,
508 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
513 g_value_set_boolean (value, checkitem->active);
515 case PROP_INCONSISTENT:
516 g_value_set_boolean (value, checkitem->inconsistent);
518 case PROP_DRAW_AS_RADIO:
519 g_value_set_boolean (value, checkitem->draw_as_radio);
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529 gtk_check_menu_item_set_property (GObject *object,
534 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
539 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
541 case PROP_INCONSISTENT:
542 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
544 case PROP_DRAW_AS_RADIO:
545 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
548 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);