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-2000. 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/.
29 #include "gtkcheckbutton.h"
31 #include "gtkbuttonprivate.h"
34 #include "gtkprivate.h"
39 * SECTION:gtkcheckbutton
40 * @Short_description: Create widgets with a discrete toggle button
41 * @Title: GtkCheckButton
42 * @See_also: #GtkCheckMenuItem, #GtkButton, #GtkToggleButton, #GtkRadioButton
44 * A #GtkCheckButton places a discrete #GtkToggleButton next to a widget,
45 * (usually a #GtkLabel). See the section on #GtkToggleButton widgets for
46 * more information about toggle/check buttons.
48 * The important signal ( #GtkToggleButton::toggled ) is also inherited from
53 #define INDICATOR_SIZE 16
54 #define INDICATOR_SPACING 2
57 static void gtk_check_button_get_preferred_width (GtkWidget *widget,
60 static void gtk_check_button_get_preferred_height (GtkWidget *widget,
63 static void gtk_check_button_size_allocate (GtkWidget *widget,
64 GtkAllocation *allocation);
65 static gboolean gtk_check_button_draw (GtkWidget *widget,
67 static void gtk_check_button_paint (GtkWidget *widget,
69 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
71 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
74 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
77 gtk_check_button_class_init (GtkCheckButtonClass *class)
79 GtkWidgetClass *widget_class;
81 widget_class = (GtkWidgetClass*) class;
83 widget_class->get_preferred_width = gtk_check_button_get_preferred_width;
84 widget_class->get_preferred_height = gtk_check_button_get_preferred_height;
85 widget_class->size_allocate = gtk_check_button_size_allocate;
86 widget_class->draw = gtk_check_button_draw;
88 class->draw_indicator = gtk_real_check_button_draw_indicator;
90 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_CHECK_BOX);
92 gtk_widget_class_install_style_property (widget_class,
93 g_param_spec_int ("indicator-size",
95 P_("Size of check or radio indicator"),
100 gtk_widget_class_install_style_property (widget_class,
101 g_param_spec_int ("indicator-spacing",
102 P_("Indicator Spacing"),
103 P_("Spacing around check or radio indicator"),
107 GTK_PARAM_READABLE));
111 gtk_check_button_init (GtkCheckButton *check_button)
113 gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
114 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
115 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
116 gtk_button_set_alignment (GTK_BUTTON (check_button), 0.0, 0.5);
120 * gtk_check_button_new:
122 * Creates a new #GtkCheckButton.
124 * Returns: a #GtkWidget.
127 gtk_check_button_new (void)
129 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
134 * gtk_check_button_new_with_label:
135 * @label: the text for the check button.
137 * Creates a new #GtkCheckButton with a #GtkLabel to the right of it.
139 * Returns: a #GtkWidget.
142 gtk_check_button_new_with_label (const gchar *label)
144 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
148 * gtk_check_button_new_with_mnemonic:
149 * @label: The text of the button, with an underscore in front of the
151 * @returns: a new #GtkCheckButton
153 * Creates a new #GtkCheckButton containing a label. The label
154 * will be created using gtk_label_new_with_mnemonic(), so underscores
155 * in @label indicate the mnemonic for the check button.
158 gtk_check_button_new_with_mnemonic (const gchar *label)
160 return g_object_new (GTK_TYPE_CHECK_BUTTON,
162 "use-underline", TRUE,
167 /* This should only be called when toggle_button->draw_indicator
171 gtk_check_button_paint (GtkWidget *widget,
174 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
176 gtk_check_button_draw_indicator (check_button, cr);
178 if (gtk_widget_has_visible_focus (widget))
180 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
181 GtkStyleContext *context;
183 GtkAllocation allocation;
189 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
191 gtk_widget_style_get (widget,
192 "interior-focus", &interior_focus,
193 "focus-line-width", &focus_width,
194 "focus-padding", &focus_pad,
197 gtk_widget_get_allocation (widget, &allocation);
198 context = gtk_widget_get_style_context (widget);
199 state = gtk_widget_get_state_flags (widget);
201 gtk_style_context_set_state (context, state);
203 if (interior_focus && child && gtk_widget_get_visible (child))
205 GtkAllocation child_allocation;
207 gtk_widget_get_allocation (child, &child_allocation);
208 gtk_render_focus (context, cr,
209 child_allocation.x - allocation.x - focus_width - focus_pad,
210 child_allocation.y - allocation.y - focus_width - focus_pad,
211 child_allocation.width + 2 * (focus_width + focus_pad),
212 child_allocation.height + 2 * (focus_width + focus_pad));
215 gtk_render_focus (context, cr,
216 border_width, border_width,
217 allocation.width - 2 * border_width,
218 allocation.height - 2 * border_width);
223 _gtk_check_button_get_props (GtkCheckButton *check_button,
224 gint *indicator_size,
225 gint *indicator_spacing)
227 GtkWidget *widget = GTK_WIDGET (check_button);
230 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
232 if (indicator_spacing)
233 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
237 gtk_check_button_get_preferred_width (GtkWidget *widget,
241 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
243 if (gtk_toggle_button_get_mode (toggle_button))
247 gint indicator_spacing;
252 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
254 gtk_widget_style_get (GTK_WIDGET (widget),
255 "focus-line-width", &focus_width,
256 "focus-padding", &focus_pad,
258 *minimum = 2 * border_width;
259 *natural = 2 * border_width;
261 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
262 &indicator_size, &indicator_spacing);
264 child = gtk_bin_get_child (GTK_BIN (widget));
265 if (child && gtk_widget_get_visible (child))
267 gint child_min, child_nat;
269 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
271 *minimum += child_min + indicator_spacing;
272 *natural += child_nat + indicator_spacing;
275 *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
276 *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
279 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
283 gtk_check_button_get_preferred_height (GtkWidget *widget,
287 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
289 if (gtk_toggle_button_get_mode (toggle_button))
294 gint indicator_spacing;
299 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
301 gtk_widget_style_get (GTK_WIDGET (widget),
302 "focus-line-width", &focus_width,
303 "focus-padding", &focus_pad,
306 *minimum = border_width * 2;
307 *natural = border_width * 2;
309 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
310 &indicator_size, &indicator_spacing);
312 child = gtk_bin_get_child (GTK_BIN (widget));
313 if (child && gtk_widget_get_visible (child))
315 gint child_min, child_nat;
317 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
319 *minimum += child_min;
320 *natural += child_nat;
323 temp = indicator_size + indicator_spacing * 2;
324 *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
325 *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
328 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
332 gtk_check_button_size_allocate (GtkWidget *widget,
333 GtkAllocation *allocation)
335 GtkCheckButton *check_button;
336 GtkToggleButton *toggle_button;
338 GtkAllocation child_allocation;
340 button = GTK_BUTTON (widget);
341 check_button = GTK_CHECK_BUTTON (widget);
342 toggle_button = GTK_TOGGLE_BUTTON (widget);
344 if (gtk_toggle_button_get_mode (toggle_button))
348 gint indicator_spacing;
352 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
353 gtk_widget_style_get (widget,
354 "focus-line-width", &focus_width,
355 "focus-padding", &focus_pad,
358 gtk_widget_set_allocation (widget, allocation);
360 if (gtk_widget_get_realized (widget))
361 gdk_window_move_resize (gtk_button_get_event_window (button),
362 allocation->x, allocation->y,
363 allocation->width, allocation->height);
365 child = gtk_bin_get_child (GTK_BIN (button));
366 if (child && gtk_widget_get_visible (child))
368 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
370 child_allocation.width = allocation->width -
371 ((border_width + focus_width + focus_pad) * 2 + indicator_size + indicator_spacing * 3);
372 child_allocation.width = MAX (child_allocation.width, 1);
374 child_allocation.height = allocation->height - (border_width + focus_width + focus_pad) * 2;
375 child_allocation.height = MAX (child_allocation.height, 1);
377 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
378 allocation->x + focus_width + focus_pad);
379 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
381 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
382 child_allocation.x = allocation->x + allocation->width
383 - (child_allocation.x - allocation->x + child_allocation.width);
385 gtk_widget_size_allocate (child, &child_allocation);
389 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
393 gtk_check_button_draw (GtkWidget *widget,
396 GtkToggleButton *toggle_button;
400 toggle_button = GTK_TOGGLE_BUTTON (widget);
401 bin = GTK_BIN (widget);
403 if (gtk_toggle_button_get_mode (toggle_button))
405 gtk_check_button_paint (widget, cr);
407 child = gtk_bin_get_child (bin);
409 gtk_container_propagate_draw (GTK_CONTAINER (widget),
413 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
414 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
421 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
424 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
426 if (class->draw_indicator)
427 class->draw_indicator (check_button, cr);
431 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
437 GtkToggleButton *toggle_button;
438 GtkStateFlags state = 0;
441 gint indicator_spacing;
445 gboolean interior_focus;
446 GtkAllocation allocation;
447 GtkStyleContext *context;
449 widget = GTK_WIDGET (check_button);
450 button = GTK_BUTTON (check_button);
451 toggle_button = GTK_TOGGLE_BUTTON (check_button);
453 gtk_widget_get_allocation (widget, &allocation);
454 context = gtk_widget_get_style_context (widget);
456 gtk_widget_style_get (widget,
457 "interior-focus", &interior_focus,
458 "focus-line-width", &focus_width,
459 "focus-padding", &focus_pad,
462 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
464 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
466 x = indicator_spacing + border_width;
467 y = (allocation.height - indicator_size) / 2;
469 child = gtk_bin_get_child (GTK_BIN (check_button));
470 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
471 x += focus_width + focus_pad;
473 if (gtk_toggle_button_get_inconsistent (toggle_button))
474 state |= GTK_STATE_FLAG_INCONSISTENT;
475 else if (gtk_toggle_button_get_active (toggle_button) ||
476 (button->priv->button_down && button->priv->in_button))
477 state |= GTK_STATE_FLAG_ACTIVE;
479 if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
480 state |= GTK_STATE_FLAG_SELECTED;
482 if (button->priv->in_button)
483 state |= GTK_STATE_FLAG_PRELIGHT;
484 else if (!gtk_widget_is_sensitive (widget))
485 state |= GTK_STATE_FLAG_INSENSITIVE;
487 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
488 x = allocation.width - (indicator_size + x);
490 gtk_style_context_set_state (context, state);
492 if (state & GTK_STATE_FLAG_PRELIGHT)
493 gtk_render_background (context, cr,
494 border_width, border_width,
495 allocation.width - (2 * border_width),
496 allocation.height - (2 * border_width));
498 gtk_style_context_save (context);
499 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
501 gtk_render_check (context, cr,
502 x, y, indicator_size, indicator_size);
504 gtk_style_context_restore (context);