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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkcheckbutton.h"
29 #include "gtkbuttonprivate.h"
32 #include "gtkprivate.h"
37 * SECTION:gtkcheckbutton
38 * @Short_description: Create widgets with a discrete toggle button
39 * @Title: GtkCheckButton
40 * @See_also: #GtkCheckMenuItem, #GtkButton, #GtkToggleButton, #GtkRadioButton
42 * A #GtkCheckButton places a discrete #GtkToggleButton next to a widget,
43 * (usually a #GtkLabel). See the section on #GtkToggleButton widgets for
44 * more information about toggle/check buttons.
46 * The important signal ( #GtkToggleButton::toggled ) is also inherited from
51 #define INDICATOR_SIZE 16
52 #define INDICATOR_SPACING 2
55 static void gtk_check_button_get_preferred_width (GtkWidget *widget,
58 static void gtk_check_button_get_preferred_height (GtkWidget *widget,
61 static void gtk_check_button_size_allocate (GtkWidget *widget,
62 GtkAllocation *allocation);
63 static gboolean gtk_check_button_draw (GtkWidget *widget,
65 static void gtk_check_button_paint (GtkWidget *widget,
67 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
69 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
72 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
75 gtk_check_button_class_init (GtkCheckButtonClass *class)
77 GtkWidgetClass *widget_class;
79 widget_class = (GtkWidgetClass*) class;
81 widget_class->get_preferred_width = gtk_check_button_get_preferred_width;
82 widget_class->get_preferred_height = gtk_check_button_get_preferred_height;
83 widget_class->size_allocate = gtk_check_button_size_allocate;
84 widget_class->draw = gtk_check_button_draw;
86 class->draw_indicator = gtk_real_check_button_draw_indicator;
88 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_CHECK_BOX);
90 gtk_widget_class_install_style_property (widget_class,
91 g_param_spec_int ("indicator-size",
93 P_("Size of check or radio indicator"),
98 gtk_widget_class_install_style_property (widget_class,
99 g_param_spec_int ("indicator-spacing",
100 P_("Indicator Spacing"),
101 P_("Spacing around check or radio indicator"),
105 GTK_PARAM_READABLE));
109 gtk_check_button_init (GtkCheckButton *check_button)
111 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
112 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
113 gtk_button_set_alignment (GTK_BUTTON (check_button), 0.0, 0.5);
117 * gtk_check_button_new:
119 * Creates a new #GtkCheckButton.
121 * Returns: a #GtkWidget.
124 gtk_check_button_new (void)
126 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
131 * gtk_check_button_new_with_label:
132 * @label: the text for the check button.
134 * Creates a new #GtkCheckButton with a #GtkLabel to the right of it.
136 * Returns: a #GtkWidget.
139 gtk_check_button_new_with_label (const gchar *label)
141 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
145 * gtk_check_button_new_with_mnemonic:
146 * @label: The text of the button, with an underscore in front of the
149 * Creates a new #GtkCheckButton containing a label. The label
150 * will be created using gtk_label_new_with_mnemonic(), so underscores
151 * in @label indicate the mnemonic for the check button.
153 * Returns: a new #GtkCheckButton
156 gtk_check_button_new_with_mnemonic (const gchar *label)
158 return g_object_new (GTK_TYPE_CHECK_BUTTON,
160 "use-underline", TRUE,
165 /* This should only be called when toggle_button->draw_indicator
169 gtk_check_button_paint (GtkWidget *widget,
172 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
174 gtk_check_button_draw_indicator (check_button, cr);
176 if (gtk_widget_has_visible_focus (widget))
178 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
179 GtkStyleContext *context;
180 GtkAllocation allocation;
186 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
188 gtk_widget_style_get (widget,
189 "interior-focus", &interior_focus,
190 "focus-line-width", &focus_width,
191 "focus-padding", &focus_pad,
194 gtk_widget_get_allocation (widget, &allocation);
195 context = gtk_widget_get_style_context (widget);
197 if (interior_focus && child && gtk_widget_get_visible (child))
199 GtkAllocation child_allocation;
201 gtk_widget_get_allocation (child, &child_allocation);
202 gtk_render_focus (context, cr,
203 child_allocation.x - allocation.x - focus_width - focus_pad,
204 child_allocation.y - allocation.y - focus_width - focus_pad,
205 child_allocation.width + 2 * (focus_width + focus_pad),
206 child_allocation.height + 2 * (focus_width + focus_pad));
209 gtk_render_focus (context, cr,
210 border_width, border_width,
211 allocation.width - 2 * border_width,
212 allocation.height - 2 * border_width);
217 _gtk_check_button_get_props (GtkCheckButton *check_button,
218 gint *indicator_size,
219 gint *indicator_spacing)
221 GtkWidget *widget = GTK_WIDGET (check_button);
224 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
226 if (indicator_spacing)
227 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
231 gtk_check_button_get_preferred_width (GtkWidget *widget,
235 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
237 if (gtk_toggle_button_get_mode (toggle_button))
241 gint indicator_spacing;
246 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
248 gtk_widget_style_get (GTK_WIDGET (widget),
249 "focus-line-width", &focus_width,
250 "focus-padding", &focus_pad,
252 *minimum = 2 * border_width;
253 *natural = 2 * border_width;
255 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
256 &indicator_size, &indicator_spacing);
258 child = gtk_bin_get_child (GTK_BIN (widget));
259 if (child && gtk_widget_get_visible (child))
261 gint child_min, child_nat;
263 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
265 *minimum += child_min + indicator_spacing;
266 *natural += child_nat + indicator_spacing;
269 *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
270 *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
273 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
277 gtk_check_button_get_preferred_height (GtkWidget *widget,
281 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
283 if (gtk_toggle_button_get_mode (toggle_button))
288 gint indicator_spacing;
293 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
295 gtk_widget_style_get (GTK_WIDGET (widget),
296 "focus-line-width", &focus_width,
297 "focus-padding", &focus_pad,
300 *minimum = border_width * 2;
301 *natural = border_width * 2;
303 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
304 &indicator_size, &indicator_spacing);
306 child = gtk_bin_get_child (GTK_BIN (widget));
307 if (child && gtk_widget_get_visible (child))
309 gint child_min, child_nat;
311 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
313 *minimum += child_min;
314 *natural += child_nat;
317 temp = indicator_size + indicator_spacing * 2;
318 *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
319 *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
322 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
326 gtk_check_button_size_allocate (GtkWidget *widget,
327 GtkAllocation *allocation)
329 GtkCheckButton *check_button;
330 GtkToggleButton *toggle_button;
332 GtkAllocation child_allocation;
334 button = GTK_BUTTON (widget);
335 check_button = GTK_CHECK_BUTTON (widget);
336 toggle_button = GTK_TOGGLE_BUTTON (widget);
338 if (gtk_toggle_button_get_mode (toggle_button))
342 gint indicator_spacing;
346 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
347 gtk_widget_style_get (widget,
348 "focus-line-width", &focus_width,
349 "focus-padding", &focus_pad,
352 gtk_widget_set_allocation (widget, allocation);
354 if (gtk_widget_get_realized (widget))
355 gdk_window_move_resize (gtk_button_get_event_window (button),
356 allocation->x, allocation->y,
357 allocation->width, allocation->height);
359 child = gtk_bin_get_child (GTK_BIN (button));
360 if (child && gtk_widget_get_visible (child))
362 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
364 child_allocation.width = allocation->width -
365 ((border_width + focus_width + focus_pad) * 2 + indicator_size + indicator_spacing * 3);
366 child_allocation.width = MAX (child_allocation.width, 1);
368 child_allocation.height = allocation->height - (border_width + focus_width + focus_pad) * 2;
369 child_allocation.height = MAX (child_allocation.height, 1);
371 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
372 allocation->x + focus_width + focus_pad);
373 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
375 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
376 child_allocation.x = allocation->x + allocation->width
377 - (child_allocation.x - allocation->x + child_allocation.width);
379 gtk_widget_size_allocate (child, &child_allocation);
383 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
387 gtk_check_button_draw (GtkWidget *widget,
390 GtkToggleButton *toggle_button;
394 toggle_button = GTK_TOGGLE_BUTTON (widget);
395 bin = GTK_BIN (widget);
397 if (gtk_toggle_button_get_mode (toggle_button))
399 gtk_check_button_paint (widget, cr);
401 child = gtk_bin_get_child (bin);
403 gtk_container_propagate_draw (GTK_CONTAINER (widget),
407 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
408 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
415 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
418 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
420 if (class->draw_indicator)
421 class->draw_indicator (check_button, cr);
425 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
431 GtkToggleButton *toggle_button;
432 GtkStateFlags state = 0;
435 gint indicator_spacing;
439 gboolean interior_focus;
440 GtkAllocation allocation;
441 GtkStyleContext *context;
443 widget = GTK_WIDGET (check_button);
444 button = GTK_BUTTON (check_button);
445 toggle_button = GTK_TOGGLE_BUTTON (check_button);
447 gtk_widget_get_allocation (widget, &allocation);
448 context = gtk_widget_get_style_context (widget);
449 state = gtk_widget_get_state_flags (widget);
451 gtk_widget_style_get (widget,
452 "interior-focus", &interior_focus,
453 "focus-line-width", &focus_width,
454 "focus-padding", &focus_pad,
457 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
459 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
461 x = indicator_spacing + border_width;
462 y = (allocation.height - indicator_size) / 2;
464 child = gtk_bin_get_child (GTK_BIN (check_button));
465 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
466 x += focus_width + focus_pad;
468 state &= ~(GTK_STATE_FLAG_INCONSISTENT |
469 GTK_STATE_FLAG_ACTIVE |
470 GTK_STATE_FLAG_SELECTED |
471 GTK_STATE_FLAG_PRELIGHT);
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;
485 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
486 x = allocation.width - (indicator_size + x);
488 gtk_style_context_save (context);
489 gtk_style_context_set_state (context, state);
491 if (state & GTK_STATE_FLAG_PRELIGHT)
492 gtk_render_background (context, cr,
493 border_width, border_width,
494 allocation.width - (2 * border_width),
495 allocation.height - (2 * border_width));
497 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
499 gtk_render_check (context, cr,
500 x, y, indicator_size, indicator_size);
502 gtk_style_context_restore (context);