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 13
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);
102 gtk_check_button_new (void)
104 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
109 gtk_check_button_new_with_label (const gchar *label)
111 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
115 * gtk_check_button_new_with_mnemonic:
116 * @label: The text of the button, with an underscore in front of the
118 * @returns: a new #GtkCheckButton
120 * Creates a new #GtkCheckButton containing a label. The label
121 * will be created using gtk_label_new_with_mnemonic(), so underscores
122 * in @label indicate the mnemonic for the check button.
125 gtk_check_button_new_with_mnemonic (const gchar *label)
127 return g_object_new (GTK_TYPE_CHECK_BUTTON,
129 "use-underline", TRUE,
134 /* This should only be called when toggle_button->draw_indicator
138 gtk_check_button_paint (GtkWidget *widget,
141 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
147 gtk_widget_style_get (widget,
148 "interior-focus", &interior_focus,
149 "focus-line-width", &focus_width,
150 "focus-padding", &focus_pad,
153 gtk_check_button_draw_indicator (check_button, cr);
155 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
156 if (gtk_widget_has_focus (widget))
158 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
159 GtkStyleContext *context;
161 GtkAllocation allocation;
163 gtk_widget_get_allocation (widget, &allocation);
164 context = gtk_widget_get_style_context (widget);
165 state = gtk_widget_get_state_flags (widget);
167 gtk_style_context_set_state (context, state);
169 if (interior_focus && child && gtk_widget_get_visible (child))
171 GtkAllocation child_allocation;
173 gtk_widget_get_allocation (child, &child_allocation);
174 gtk_render_focus (context, cr,
175 child_allocation.x - allocation.x - focus_width - focus_pad,
176 child_allocation.y - allocation.y - focus_width - focus_pad,
177 child_allocation.width + 2 * (focus_width + focus_pad),
178 child_allocation.height + 2 * (focus_width + focus_pad));
181 gtk_render_focus (context, cr,
182 border_width, border_width,
183 allocation.width - 2 * border_width,
184 allocation.height - 2 * border_width);
189 _gtk_check_button_get_props (GtkCheckButton *check_button,
190 gint *indicator_size,
191 gint *indicator_spacing)
193 GtkWidget *widget = GTK_WIDGET (check_button);
196 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
198 if (indicator_spacing)
199 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
203 gtk_check_button_get_preferred_width (GtkWidget *widget,
207 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
209 if (gtk_toggle_button_get_mode (toggle_button))
213 gint indicator_spacing;
218 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
220 gtk_widget_style_get (GTK_WIDGET (widget),
221 "focus-line-width", &focus_width,
222 "focus-padding", &focus_pad,
224 *minimum = 2 * border_width;
225 *natural = 2 * border_width;
227 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
228 &indicator_size, &indicator_spacing);
230 child = gtk_bin_get_child (GTK_BIN (widget));
231 if (child && gtk_widget_get_visible (child))
233 gint child_min, child_nat;
235 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
237 *minimum += child_min + indicator_spacing;
238 *natural += child_nat + indicator_spacing;
241 *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
242 *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
245 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
249 gtk_check_button_get_preferred_height (GtkWidget *widget,
253 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
255 if (gtk_toggle_button_get_mode (toggle_button))
260 gint indicator_spacing;
265 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
267 gtk_widget_style_get (GTK_WIDGET (widget),
268 "focus-line-width", &focus_width,
269 "focus-padding", &focus_pad,
272 *minimum = border_width * 2;
273 *natural = border_width * 2;
275 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
276 &indicator_size, &indicator_spacing);
278 child = gtk_bin_get_child (GTK_BIN (widget));
279 if (child && gtk_widget_get_visible (child))
281 gint child_min, child_nat;
283 gtk_widget_get_preferred_height (child, &child_min, &child_nat);
285 *minimum += child_min;
286 *natural += child_nat;
289 temp = indicator_size + indicator_spacing * 2;
290 *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
291 *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
294 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
298 gtk_check_button_size_allocate (GtkWidget *widget,
299 GtkAllocation *allocation)
301 GtkCheckButton *check_button;
302 GtkToggleButton *toggle_button;
304 GtkAllocation child_allocation;
306 button = GTK_BUTTON (widget);
307 check_button = GTK_CHECK_BUTTON (widget);
308 toggle_button = GTK_TOGGLE_BUTTON (widget);
310 if (gtk_toggle_button_get_mode (toggle_button))
314 gint indicator_spacing;
318 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
319 gtk_widget_style_get (widget,
320 "focus-line-width", &focus_width,
321 "focus-padding", &focus_pad,
324 gtk_widget_set_allocation (widget, allocation);
326 if (gtk_widget_get_realized (widget))
327 gdk_window_move_resize (gtk_button_get_event_window (button),
328 allocation->x, allocation->y,
329 allocation->width, allocation->height);
331 child = gtk_bin_get_child (GTK_BIN (button));
332 if (child && gtk_widget_get_visible (child))
334 GtkRequisition child_requisition;
335 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
337 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
339 child_allocation.width = MIN (child_requisition.width,
341 ((border_width + focus_width + focus_pad) * 2
342 + indicator_size + indicator_spacing * 3));
343 child_allocation.width = MAX (child_allocation.width, 1);
345 child_allocation.height = MIN (child_requisition.height,
346 allocation->height - (border_width + focus_width + focus_pad) * 2);
347 child_allocation.height = MAX (child_allocation.height, 1);
349 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
350 allocation->x + focus_width + focus_pad);
351 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
353 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
354 child_allocation.x = allocation->x + allocation->width
355 - (child_allocation.x - allocation->x + child_allocation.width);
357 gtk_widget_size_allocate (child, &child_allocation);
361 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
365 gtk_check_button_draw (GtkWidget *widget,
368 GtkToggleButton *toggle_button;
372 toggle_button = GTK_TOGGLE_BUTTON (widget);
373 bin = GTK_BIN (widget);
375 if (gtk_toggle_button_get_mode (toggle_button))
377 gtk_check_button_paint (widget, cr);
379 child = gtk_bin_get_child (bin);
381 gtk_container_propagate_draw (GTK_CONTAINER (widget),
385 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
386 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
393 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
396 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
398 if (class->draw_indicator)
399 class->draw_indicator (check_button, cr);
403 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
409 GtkToggleButton *toggle_button;
410 GtkStateFlags state = 0;
413 gint indicator_spacing;
417 gboolean interior_focus;
418 GtkAllocation allocation;
419 GtkStyleContext *context;
421 widget = GTK_WIDGET (check_button);
422 button = GTK_BUTTON (check_button);
423 toggle_button = GTK_TOGGLE_BUTTON (check_button);
425 gtk_widget_get_allocation (widget, &allocation);
426 context = gtk_widget_get_style_context (widget);
428 gtk_widget_style_get (widget,
429 "interior-focus", &interior_focus,
430 "focus-line-width", &focus_width,
431 "focus-padding", &focus_pad,
434 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
436 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
438 x = indicator_spacing + border_width;
439 y = (allocation.height - indicator_size) / 2;
441 child = gtk_bin_get_child (GTK_BIN (check_button));
442 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
443 x += focus_width + focus_pad;
445 if (gtk_toggle_button_get_inconsistent (toggle_button))
446 state |= GTK_STATE_FLAG_INCONSISTENT;
447 else if (gtk_toggle_button_get_active (toggle_button))
448 state |= GTK_STATE_FLAG_ACTIVE;
450 if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
451 state |= GTK_STATE_FLAG_SELECTED;
453 if (button->priv->in_button)
454 state |= GTK_STATE_FLAG_PRELIGHT;
455 else if (!gtk_widget_is_sensitive (widget))
456 state |= GTK_STATE_FLAG_INSENSITIVE;
458 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
459 x = allocation.width - (indicator_size + x);
461 gtk_style_context_set_state (context, state);
463 if (state & GTK_STATE_FLAG_PRELIGHT)
464 gtk_render_background (context, cr,
465 border_width, border_width,
466 allocation.width - (2 * border_width),
467 allocation.height - (2 * border_width));
469 gtk_style_context_save (context);
470 gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
472 gtk_render_check (context, cr,
473 x, y, indicator_size, indicator_size);
475 gtk_style_context_restore (context);