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 draw_indicator_changed (GObject *object,
113 GtkButton *button = GTK_BUTTON (object);
115 if (gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (button)))
116 gtk_button_set_alignment (button, 0.0, 0.5);
118 gtk_button_set_alignment (button, 0.5, 0.5);
122 gtk_check_button_init (GtkCheckButton *check_button)
124 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
125 g_signal_connect (check_button, "notify::draw-indicator", G_CALLBACK (draw_indicator_changed), NULL);
126 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
130 * gtk_check_button_new:
132 * Creates a new #GtkCheckButton.
134 * Returns: a #GtkWidget.
137 gtk_check_button_new (void)
139 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
144 * gtk_check_button_new_with_label:
145 * @label: the text for the check button.
147 * Creates a new #GtkCheckButton with a #GtkLabel to the right of it.
149 * Returns: a #GtkWidget.
152 gtk_check_button_new_with_label (const gchar *label)
154 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
158 * gtk_check_button_new_with_mnemonic:
159 * @label: The text of the button, with an underscore in front of the
162 * Creates a new #GtkCheckButton containing a label. The label
163 * will be created using gtk_label_new_with_mnemonic(), so underscores
164 * in @label indicate the mnemonic for the check button.
166 * Returns: a new #GtkCheckButton
169 gtk_check_button_new_with_mnemonic (const gchar *label)
171 return g_object_new (GTK_TYPE_CHECK_BUTTON,
173 "use-underline", TRUE,
178 /* This should only be called when toggle_button->draw_indicator
182 gtk_check_button_paint (GtkWidget *widget,
185 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
187 gtk_check_button_draw_indicator (check_button, cr);
189 if (gtk_widget_has_visible_focus (widget))
191 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
192 GtkStyleContext *context;
193 GtkAllocation allocation;
199 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
201 gtk_widget_style_get (widget,
202 "interior-focus", &interior_focus,
203 "focus-line-width", &focus_width,
204 "focus-padding", &focus_pad,
207 gtk_widget_get_allocation (widget, &allocation);
208 context = gtk_widget_get_style_context (widget);
210 if (interior_focus && child && gtk_widget_get_visible (child))
212 GtkAllocation child_allocation;
214 gtk_widget_get_allocation (child, &child_allocation);
215 gtk_render_focus (context, cr,
216 child_allocation.x - allocation.x - focus_width - focus_pad,
217 child_allocation.y - allocation.y - focus_width - focus_pad,
218 child_allocation.width + 2 * (focus_width + focus_pad),
219 child_allocation.height + 2 * (focus_width + focus_pad));
222 gtk_render_focus (context, cr,
223 border_width, border_width,
224 allocation.width - 2 * border_width,
225 allocation.height - 2 * border_width);
230 _gtk_check_button_get_props (GtkCheckButton *check_button,
231 gint *indicator_size,
232 gint *indicator_spacing)
234 GtkWidget *widget = GTK_WIDGET (check_button);
237 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
239 if (indicator_spacing)
240 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
244 gtk_check_button_get_preferred_width (GtkWidget *widget,
248 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
250 if (gtk_toggle_button_get_mode (toggle_button))
254 gint indicator_spacing;
259 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
261 gtk_widget_style_get (GTK_WIDGET (widget),
262 "focus-line-width", &focus_width,
263 "focus-padding", &focus_pad,
265 *minimum = 2 * border_width;
266 *natural = 2 * border_width;
268 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
269 &indicator_size, &indicator_spacing);
271 child = gtk_bin_get_child (GTK_BIN (widget));
272 if (child && gtk_widget_get_visible (child))
274 gint child_min, child_nat;
276 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
278 *minimum += child_min + indicator_spacing;
279 *natural += child_nat + indicator_spacing;
282 *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
283 *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
286 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
290 gtk_check_button_get_preferred_height (GtkWidget *widget,
294 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
296 if (gtk_toggle_button_get_mode (toggle_button))
301 gint indicator_spacing;
306 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
308 gtk_widget_style_get (GTK_WIDGET (widget),
309 "focus-line-width", &focus_width,
310 "focus-padding", &focus_pad,
313 *minimum = border_width * 2;
314 *natural = border_width * 2;
316 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
317 &indicator_size, &indicator_spacing);
319 child = gtk_bin_get_child (GTK_BIN (widget));
320 if (child && gtk_widget_get_visible (child))
322 gint child_min, child_nat;
324 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
326 *minimum += child_min;
327 *natural += child_nat;
330 temp = indicator_size + indicator_spacing * 2;
331 *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
332 *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
335 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
339 gtk_check_button_size_allocate (GtkWidget *widget,
340 GtkAllocation *allocation)
342 GtkCheckButton *check_button;
343 GtkToggleButton *toggle_button;
345 GtkAllocation child_allocation;
347 button = GTK_BUTTON (widget);
348 check_button = GTK_CHECK_BUTTON (widget);
349 toggle_button = GTK_TOGGLE_BUTTON (widget);
351 if (gtk_toggle_button_get_mode (toggle_button))
355 gint indicator_spacing;
359 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
360 gtk_widget_style_get (widget,
361 "focus-line-width", &focus_width,
362 "focus-padding", &focus_pad,
365 gtk_widget_set_allocation (widget, allocation);
367 if (gtk_widget_get_realized (widget))
368 gdk_window_move_resize (gtk_button_get_event_window (button),
369 allocation->x, allocation->y,
370 allocation->width, allocation->height);
372 child = gtk_bin_get_child (GTK_BIN (button));
373 if (child && gtk_widget_get_visible (child))
375 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
377 child_allocation.width = allocation->width -
378 ((border_width + focus_width + focus_pad) * 2 + indicator_size + indicator_spacing * 3);
379 child_allocation.width = MAX (child_allocation.width, 1);
381 child_allocation.height = allocation->height - (border_width + focus_width + focus_pad) * 2;
382 child_allocation.height = MAX (child_allocation.height, 1);
384 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
385 allocation->x + focus_width + focus_pad);
386 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
388 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
389 child_allocation.x = allocation->x + allocation->width
390 - (child_allocation.x - allocation->x + child_allocation.width);
392 gtk_widget_size_allocate (child, &child_allocation);
396 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
400 gtk_check_button_draw (GtkWidget *widget,
403 GtkToggleButton *toggle_button;
407 toggle_button = GTK_TOGGLE_BUTTON (widget);
408 bin = GTK_BIN (widget);
410 if (gtk_toggle_button_get_mode (toggle_button))
412 gtk_check_button_paint (widget, cr);
414 child = gtk_bin_get_child (bin);
416 gtk_container_propagate_draw (GTK_CONTAINER (widget),
420 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
421 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
428 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
431 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
433 if (class->draw_indicator)
434 class->draw_indicator (check_button, cr);
438 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
444 GtkToggleButton *toggle_button;
445 GtkStateFlags state = 0;
448 gint indicator_spacing;
452 gboolean interior_focus;
453 GtkAllocation allocation;
454 GtkStyleContext *context;
456 widget = GTK_WIDGET (check_button);
457 button = GTK_BUTTON (check_button);
458 toggle_button = GTK_TOGGLE_BUTTON (check_button);
460 gtk_widget_get_allocation (widget, &allocation);
461 context = gtk_widget_get_style_context (widget);
462 state = gtk_widget_get_state_flags (widget);
464 gtk_widget_style_get (widget,
465 "interior-focus", &interior_focus,
466 "focus-line-width", &focus_width,
467 "focus-padding", &focus_pad,
470 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
472 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
474 x = indicator_spacing + border_width;
475 y = (allocation.height - indicator_size) / 2;
477 child = gtk_bin_get_child (GTK_BIN (check_button));
478 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
479 x += focus_width + focus_pad;
481 state &= ~(GTK_STATE_FLAG_INCONSISTENT |
482 GTK_STATE_FLAG_ACTIVE |
483 GTK_STATE_FLAG_SELECTED |
484 GTK_STATE_FLAG_PRELIGHT);
486 if (gtk_toggle_button_get_inconsistent (toggle_button))
487 state |= GTK_STATE_FLAG_INCONSISTENT;
488 else if (gtk_toggle_button_get_active (toggle_button) ||
489 (button->priv->button_down && button->priv->in_button))
490 state |= GTK_STATE_FLAG_ACTIVE;
492 if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
493 state |= GTK_STATE_FLAG_SELECTED;
495 if (button->priv->in_button)
496 state |= GTK_STATE_FLAG_PRELIGHT;
498 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
499 x = allocation.width - (indicator_size + x);
501 gtk_style_context_save (context);
502 gtk_style_context_set_state (context, state);
504 if (state & GTK_STATE_FLAG_PRELIGHT)
505 gtk_render_background (context, cr,
506 border_width, border_width,
507 allocation.width - (2 * border_width),
508 allocation.height - (2 * border_width));
510 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
512 gtk_render_check (context, cr,
513 x, y, indicator_size, indicator_size);
515 gtk_style_context_restore (context);