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"
38 #define INDICATOR_SIZE 16
39 #define INDICATOR_SPACING 2
42 static void gtk_check_button_get_preferred_width (GtkWidget *widget,
45 static void gtk_check_button_get_preferred_height (GtkWidget *widget,
48 static void gtk_check_button_size_allocate (GtkWidget *widget,
49 GtkAllocation *allocation);
50 static gboolean gtk_check_button_draw (GtkWidget *widget,
52 static void gtk_check_button_paint (GtkWidget *widget,
54 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
56 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
59 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
62 gtk_check_button_class_init (GtkCheckButtonClass *class)
64 GtkWidgetClass *widget_class;
66 widget_class = (GtkWidgetClass*) class;
68 widget_class->get_preferred_width = gtk_check_button_get_preferred_width;
69 widget_class->get_preferred_height = gtk_check_button_get_preferred_height;
70 widget_class->size_allocate = gtk_check_button_size_allocate;
71 widget_class->draw = gtk_check_button_draw;
73 class->draw_indicator = gtk_real_check_button_draw_indicator;
75 gtk_widget_class_install_style_property (widget_class,
76 g_param_spec_int ("indicator-size",
78 P_("Size of check or radio indicator"),
83 gtk_widget_class_install_style_property (widget_class,
84 g_param_spec_int ("indicator-spacing",
85 P_("Indicator Spacing"),
86 P_("Spacing around check or radio indicator"),
94 gtk_check_button_init (GtkCheckButton *check_button)
96 gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
97 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
98 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
99 gtk_button_set_alignment (GTK_BUTTON (check_button), 0.0, 0.5);
103 gtk_check_button_new (void)
105 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
110 gtk_check_button_new_with_label (const gchar *label)
112 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
116 * gtk_check_button_new_with_mnemonic:
117 * @label: The text of the button, with an underscore in front of the
119 * @returns: a new #GtkCheckButton
121 * Creates a new #GtkCheckButton containing a label. The label
122 * will be created using gtk_label_new_with_mnemonic(), so underscores
123 * in @label indicate the mnemonic for the check button.
126 gtk_check_button_new_with_mnemonic (const gchar *label)
128 return g_object_new (GTK_TYPE_CHECK_BUTTON,
130 "use-underline", TRUE,
135 /* This should only be called when toggle_button->draw_indicator
139 gtk_check_button_paint (GtkWidget *widget,
142 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
148 gtk_widget_style_get (widget,
149 "interior-focus", &interior_focus,
150 "focus-line-width", &focus_width,
151 "focus-padding", &focus_pad,
154 gtk_check_button_draw_indicator (check_button, cr);
156 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
157 if (gtk_widget_has_focus (widget))
159 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
160 GtkStyleContext *context;
162 GtkAllocation allocation;
164 gtk_widget_get_allocation (widget, &allocation);
165 context = gtk_widget_get_style_context (widget);
166 state = gtk_widget_get_state_flags (widget);
168 gtk_style_context_set_state (context, state);
170 if (interior_focus && child && gtk_widget_get_visible (child))
172 GtkAllocation child_allocation;
174 gtk_widget_get_allocation (child, &child_allocation);
175 gtk_render_focus (context, cr,
176 child_allocation.x - allocation.x - focus_width - focus_pad,
177 child_allocation.y - allocation.y - focus_width - focus_pad,
178 child_allocation.width + 2 * (focus_width + focus_pad),
179 child_allocation.height + 2 * (focus_width + focus_pad));
182 gtk_render_focus (context, cr,
183 border_width, border_width,
184 allocation.width - 2 * border_width,
185 allocation.height - 2 * border_width);
190 _gtk_check_button_get_props (GtkCheckButton *check_button,
191 gint *indicator_size,
192 gint *indicator_spacing)
194 GtkWidget *widget = GTK_WIDGET (check_button);
197 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
199 if (indicator_spacing)
200 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
204 gtk_check_button_get_preferred_width (GtkWidget *widget,
208 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
210 if (gtk_toggle_button_get_mode (toggle_button))
214 gint indicator_spacing;
219 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
221 gtk_widget_style_get (GTK_WIDGET (widget),
222 "focus-line-width", &focus_width,
223 "focus-padding", &focus_pad,
225 *minimum = 2 * border_width;
226 *natural = 2 * border_width;
228 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
229 &indicator_size, &indicator_spacing);
231 child = gtk_bin_get_child (GTK_BIN (widget));
232 if (child && gtk_widget_get_visible (child))
234 gint child_min, child_nat;
236 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
238 *minimum += child_min + indicator_spacing;
239 *natural += child_nat + indicator_spacing;
242 *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
243 *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
246 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
250 gtk_check_button_get_preferred_height (GtkWidget *widget,
254 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
256 if (gtk_toggle_button_get_mode (toggle_button))
261 gint indicator_spacing;
266 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
268 gtk_widget_style_get (GTK_WIDGET (widget),
269 "focus-line-width", &focus_width,
270 "focus-padding", &focus_pad,
273 *minimum = border_width * 2;
274 *natural = border_width * 2;
276 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
277 &indicator_size, &indicator_spacing);
279 child = gtk_bin_get_child (GTK_BIN (widget));
280 if (child && gtk_widget_get_visible (child))
282 gint child_min, child_nat;
284 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
286 *minimum += child_min;
287 *natural += child_nat;
290 temp = indicator_size + indicator_spacing * 2;
291 *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
292 *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
295 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
299 gtk_check_button_size_allocate (GtkWidget *widget,
300 GtkAllocation *allocation)
302 GtkCheckButton *check_button;
303 GtkToggleButton *toggle_button;
305 GtkAllocation child_allocation;
307 button = GTK_BUTTON (widget);
308 check_button = GTK_CHECK_BUTTON (widget);
309 toggle_button = GTK_TOGGLE_BUTTON (widget);
311 if (gtk_toggle_button_get_mode (toggle_button))
315 gint indicator_spacing;
319 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
320 gtk_widget_style_get (widget,
321 "focus-line-width", &focus_width,
322 "focus-padding", &focus_pad,
325 gtk_widget_set_allocation (widget, allocation);
327 if (gtk_widget_get_realized (widget))
328 gdk_window_move_resize (gtk_button_get_event_window (button),
329 allocation->x, allocation->y,
330 allocation->width, allocation->height);
332 child = gtk_bin_get_child (GTK_BIN (button));
333 if (child && gtk_widget_get_visible (child))
335 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
337 child_allocation.width = allocation->width -
338 ((border_width + focus_width + focus_pad) * 2 + indicator_size + indicator_spacing * 3);
339 child_allocation.width = MAX (child_allocation.width, 1);
341 child_allocation.height = allocation->height - (border_width + focus_width + focus_pad) * 2;
342 child_allocation.height = MAX (child_allocation.height, 1);
344 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
345 allocation->x + focus_width + focus_pad);
346 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
348 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
349 child_allocation.x = allocation->x + allocation->width
350 - (child_allocation.x - allocation->x + child_allocation.width);
352 gtk_widget_size_allocate (child, &child_allocation);
356 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
360 gtk_check_button_draw (GtkWidget *widget,
363 GtkToggleButton *toggle_button;
367 toggle_button = GTK_TOGGLE_BUTTON (widget);
368 bin = GTK_BIN (widget);
370 if (gtk_toggle_button_get_mode (toggle_button))
372 gtk_check_button_paint (widget, cr);
374 child = gtk_bin_get_child (bin);
376 gtk_container_propagate_draw (GTK_CONTAINER (widget),
380 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
381 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
388 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
391 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
393 if (class->draw_indicator)
394 class->draw_indicator (check_button, cr);
398 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
404 GtkToggleButton *toggle_button;
405 GtkStateFlags state = 0;
408 gint indicator_spacing;
412 gboolean interior_focus;
413 GtkAllocation allocation;
414 GtkStyleContext *context;
416 widget = GTK_WIDGET (check_button);
417 button = GTK_BUTTON (check_button);
418 toggle_button = GTK_TOGGLE_BUTTON (check_button);
420 gtk_widget_get_allocation (widget, &allocation);
421 context = gtk_widget_get_style_context (widget);
423 gtk_widget_style_get (widget,
424 "interior-focus", &interior_focus,
425 "focus-line-width", &focus_width,
426 "focus-padding", &focus_pad,
429 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
431 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
433 x = indicator_spacing + border_width;
434 y = (allocation.height - indicator_size) / 2;
436 child = gtk_bin_get_child (GTK_BIN (check_button));
437 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
438 x += focus_width + focus_pad;
440 if (gtk_toggle_button_get_inconsistent (toggle_button))
441 state |= GTK_STATE_FLAG_INCONSISTENT;
442 else if (gtk_toggle_button_get_active (toggle_button) ||
443 (button->priv->button_down && button->priv->in_button))
444 state |= GTK_STATE_FLAG_ACTIVE;
446 if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
447 state |= GTK_STATE_FLAG_SELECTED;
449 if (button->priv->in_button)
450 state |= GTK_STATE_FLAG_PRELIGHT;
451 else if (!gtk_widget_is_sensitive (widget))
452 state |= GTK_STATE_FLAG_INSENSITIVE;
454 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
455 x = allocation.width - (indicator_size + x);
457 gtk_style_context_set_state (context, state);
459 if (state & GTK_STATE_FLAG_PRELIGHT)
460 gtk_render_background (context, cr,
461 border_width, border_width,
462 allocation.width - (2 * border_width),
463 allocation.height - (2 * border_width));
465 gtk_style_context_save (context);
466 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
468 gtk_render_check (context, cr,
469 x, y, indicator_size, indicator_size);
471 gtk_style_context_restore (context);