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_set_show_toggle (GtkCheckMenuItem *menu_item,
289 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (menu_item));
292 menu_item->always_show_toggle = always != FALSE;
297 gtk_check_menu_item_toggled (GtkCheckMenuItem *check_menu_item)
299 g_signal_emit (check_menu_item, check_menu_item_signals[TOGGLED], 0);
303 * gtk_check_menu_item_set_inconsistent:
304 * @check_menu_item: a #GtkCheckMenuItem
305 * @setting: %TRUE to display an "inconsistent" third state check
307 * If the user has selected a range of elements (such as some text or
308 * spreadsheet cells) that are affected by a boolean setting, and the
309 * current values in that range are inconsistent, you may want to
310 * display the check in an "in between" state. This function turns on
311 * "in between" display. Normally you would turn off the inconsistent
312 * state again if the user explicitly selects a setting. This has to be
313 * done manually, gtk_check_menu_item_set_inconsistent() only affects
314 * visual appearance, it doesn't affect the semantics of the widget.
318 gtk_check_menu_item_set_inconsistent (GtkCheckMenuItem *check_menu_item,
321 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
323 setting = setting != FALSE;
325 if (setting != check_menu_item->inconsistent)
327 check_menu_item->inconsistent = setting;
328 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
329 g_object_notify (G_OBJECT (check_menu_item), "inconsistent");
334 * gtk_check_menu_item_get_inconsistent:
335 * @check_menu_item: a #GtkCheckMenuItem
337 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
339 * Return value: %TRUE if inconsistent
342 gtk_check_menu_item_get_inconsistent (GtkCheckMenuItem *check_menu_item)
344 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
346 return check_menu_item->inconsistent;
350 * gtk_check_menu_item_set_draw_as_radio:
351 * @check_menu_item: a #GtkCheckMenuItem
352 * @draw_as_radio: whether @check_menu_item is drawn like a #GtkRadioMenuItem
354 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
359 gtk_check_menu_item_set_draw_as_radio (GtkCheckMenuItem *check_menu_item,
360 gboolean draw_as_radio)
362 g_return_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item));
364 draw_as_radio = draw_as_radio != FALSE;
366 if (draw_as_radio != check_menu_item->draw_as_radio)
368 check_menu_item->draw_as_radio = draw_as_radio;
370 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
372 g_object_notify (G_OBJECT (check_menu_item), "draw-as-radio");
377 * gtk_check_menu_item_get_draw_as_radio:
378 * @check_menu_item: a #GtkCheckMenuItem
380 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
382 * Return value: Whether @check_menu_item looks like a #GtkRadioMenuItem
387 gtk_check_menu_item_get_draw_as_radio (GtkCheckMenuItem *check_menu_item)
389 g_return_val_if_fail (GTK_IS_CHECK_MENU_ITEM (check_menu_item), FALSE);
391 return check_menu_item->draw_as_radio;
395 gtk_check_menu_item_init (GtkCheckMenuItem *check_menu_item)
397 check_menu_item->active = FALSE;
398 check_menu_item->always_show_toggle = TRUE;
402 gtk_check_menu_item_expose (GtkWidget *widget,
403 GdkEventExpose *event)
405 if (GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event)
406 GTK_WIDGET_CLASS (gtk_check_menu_item_parent_class)->expose_event (widget, event);
408 gtk_check_menu_item_draw_indicator (GTK_CHECK_MENU_ITEM (widget), &event->area);
414 gtk_check_menu_item_activate (GtkMenuItem *menu_item)
416 GtkCheckMenuItem *check_menu_item = GTK_CHECK_MENU_ITEM (menu_item);
417 check_menu_item->active = !check_menu_item->active;
419 gtk_check_menu_item_toggled (check_menu_item);
420 gtk_widget_queue_draw (GTK_WIDGET (check_menu_item));
422 GTK_MENU_ITEM_CLASS (gtk_check_menu_item_parent_class)->activate (menu_item);
424 g_object_notify (G_OBJECT (check_menu_item), "active");
428 gtk_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
431 if (GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator)
432 GTK_CHECK_MENU_ITEM_GET_CLASS (check_menu_item)->draw_indicator (check_menu_item, area);
436 gtk_real_check_menu_item_draw_indicator (GtkCheckMenuItem *check_menu_item,
440 GtkStateType state_type;
441 GtkShadowType shadow_type;
444 if (GTK_WIDGET_DRAWABLE (check_menu_item))
448 guint toggle_spacing;
449 guint horizontal_padding;
450 guint indicator_size;
452 widget = GTK_WIDGET (check_menu_item);
454 gtk_widget_style_get (GTK_WIDGET (check_menu_item),
455 "toggle-spacing", &toggle_spacing,
456 "horizontal-padding", &horizontal_padding,
457 "indicator-size", &indicator_size,
460 toggle_size = GTK_MENU_ITEM (check_menu_item)->toggle_size;
461 offset = GTK_CONTAINER (check_menu_item)->border_width +
462 widget->style->xthickness + 2;
464 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
466 x = widget->allocation.x + offset + horizontal_padding +
467 (toggle_size - toggle_spacing - indicator_size) / 2;
471 x = widget->allocation.x + widget->allocation.width -
472 offset - horizontal_padding - toggle_size + toggle_spacing +
473 (toggle_size - toggle_spacing - indicator_size) / 2;
476 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
478 if (check_menu_item->active ||
479 check_menu_item->always_show_toggle ||
480 (GTK_WIDGET_STATE (check_menu_item) == GTK_STATE_PRELIGHT))
482 state_type = GTK_WIDGET_STATE (widget);
484 if (check_menu_item->inconsistent)
485 shadow_type = GTK_SHADOW_ETCHED_IN;
486 else if (check_menu_item->active)
487 shadow_type = GTK_SHADOW_IN;
489 shadow_type = GTK_SHADOW_OUT;
491 if (!GTK_WIDGET_IS_SENSITIVE (widget))
492 state_type = GTK_STATE_INSENSITIVE;
494 if (check_menu_item->draw_as_radio)
496 gtk_paint_option (widget->style, widget->window,
497 state_type, shadow_type,
498 area, widget, "option",
499 x, y, indicator_size, indicator_size);
503 gtk_paint_check (widget->style, widget->window,
504 state_type, shadow_type,
505 area, widget, "check",
506 x, y, indicator_size, indicator_size);
514 gtk_check_menu_item_get_property (GObject *object,
519 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
524 g_value_set_boolean (value, checkitem->active);
526 case PROP_INCONSISTENT:
527 g_value_set_boolean (value, checkitem->inconsistent);
529 case PROP_DRAW_AS_RADIO:
530 g_value_set_boolean (value, checkitem->draw_as_radio);
533 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
540 gtk_check_menu_item_set_property (GObject *object,
545 GtkCheckMenuItem *checkitem = GTK_CHECK_MENU_ITEM (object);
550 gtk_check_menu_item_set_active (checkitem, g_value_get_boolean (value));
552 case PROP_INCONSISTENT:
553 gtk_check_menu_item_set_inconsistent (checkitem, g_value_get_boolean (value));
555 case PROP_DRAW_AS_RADIO:
556 gtk_check_menu_item_set_draw_as_radio (checkitem, g_value_get_boolean (value));
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
564 #define __GTK_CHECK_MENU_ITEM_C__
565 #include "gtkaliasdef.c"