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"
33 #include "gtkprivate.h"
37 #define INDICATOR_SIZE 13
38 #define INDICATOR_SPACING 2
41 static void gtk_check_button_size_request (GtkWidget *widget,
42 GtkRequisition *requisition);
43 static void gtk_check_button_size_allocate (GtkWidget *widget,
44 GtkAllocation *allocation);
45 static gboolean gtk_check_button_draw (GtkWidget *widget,
47 static void gtk_check_button_paint (GtkWidget *widget,
49 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
51 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
54 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
57 gtk_check_button_class_init (GtkCheckButtonClass *class)
59 GtkWidgetClass *widget_class;
61 widget_class = (GtkWidgetClass*) class;
63 widget_class->size_request = gtk_check_button_size_request;
64 widget_class->size_allocate = gtk_check_button_size_allocate;
65 widget_class->draw = gtk_check_button_draw;
67 class->draw_indicator = gtk_real_check_button_draw_indicator;
69 gtk_widget_class_install_style_property (widget_class,
70 g_param_spec_int ("indicator-size",
72 P_("Size of check or radio indicator"),
77 gtk_widget_class_install_style_property (widget_class,
78 g_param_spec_int ("indicator-spacing",
79 P_("Indicator Spacing"),
80 P_("Spacing around check or radio indicator"),
88 gtk_check_button_init (GtkCheckButton *check_button)
90 gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
91 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
92 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
96 gtk_check_button_new (void)
98 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
103 gtk_check_button_new_with_label (const gchar *label)
105 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
109 * gtk_check_button_new_with_mnemonic:
110 * @label: The text of the button, with an underscore in front of the
112 * @returns: a new #GtkCheckButton
114 * Creates a new #GtkCheckButton containing a label. The label
115 * will be created using gtk_label_new_with_mnemonic(), so underscores
116 * in @label indicate the mnemonic for the check button.
119 gtk_check_button_new_with_mnemonic (const gchar *label)
121 return g_object_new (GTK_TYPE_CHECK_BUTTON,
123 "use-underline", TRUE,
128 /* This should only be called when toggle_button->draw_indicator
132 gtk_check_button_paint (GtkWidget *widget,
135 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
141 gtk_widget_style_get (widget,
142 "interior-focus", &interior_focus,
143 "focus-line-width", &focus_width,
144 "focus-padding", &focus_pad,
147 gtk_check_button_draw_indicator (check_button, cr);
149 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
150 if (gtk_widget_has_focus (widget))
152 GtkStateType state = gtk_widget_get_state (widget);
153 GtkStyle *style = gtk_widget_get_style (widget);
154 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
155 GtkAllocation allocation;
157 gtk_widget_get_allocation (widget, &allocation);
159 if (interior_focus && child && gtk_widget_get_visible (child))
161 GtkAllocation child_allocation;
163 gtk_widget_get_allocation (child, &child_allocation);
164 gtk_paint_focus (style, cr, state,
165 widget, "checkbutton",
166 child_allocation.x - allocation.x - focus_width - focus_pad,
167 child_allocation.y - allocation.y - focus_width - focus_pad,
168 child_allocation.width + 2 * (focus_width + focus_pad),
169 child_allocation.height + 2 * (focus_width + focus_pad));
173 gtk_paint_focus (style, cr, state,
174 widget, "checkbutton",
177 allocation.width - 2 * border_width,
178 allocation.height - 2 * border_width);
184 _gtk_check_button_get_props (GtkCheckButton *check_button,
185 gint *indicator_size,
186 gint *indicator_spacing)
188 GtkWidget *widget = GTK_WIDGET (check_button);
191 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
193 if (indicator_spacing)
194 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
198 gtk_check_button_size_request (GtkWidget *widget,
199 GtkRequisition *requisition)
201 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
203 if (gtk_toggle_button_get_mode (toggle_button))
208 gint indicator_spacing;
213 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
215 gtk_widget_style_get (GTK_WIDGET (widget),
216 "focus-line-width", &focus_width,
217 "focus-padding", &focus_pad,
220 requisition->width = border_width * 2;
221 requisition->height = border_width * 2;
223 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
224 &indicator_size, &indicator_spacing);
226 child = gtk_bin_get_child (GTK_BIN (widget));
227 if (child && gtk_widget_get_visible (child))
229 GtkRequisition child_requisition;
231 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
233 requisition->width += child_requisition.width + indicator_spacing;
234 requisition->height += child_requisition.height;
237 requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
239 temp = indicator_size + indicator_spacing * 2;
240 requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
243 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request (widget, requisition);
247 gtk_check_button_size_allocate (GtkWidget *widget,
248 GtkAllocation *allocation)
250 GtkCheckButton *check_button;
251 GtkToggleButton *toggle_button;
253 GtkAllocation child_allocation;
255 button = GTK_BUTTON (widget);
256 check_button = GTK_CHECK_BUTTON (widget);
257 toggle_button = GTK_TOGGLE_BUTTON (widget);
259 if (gtk_toggle_button_get_mode (toggle_button))
263 gint indicator_spacing;
267 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
268 gtk_widget_style_get (widget,
269 "focus-line-width", &focus_width,
270 "focus-padding", &focus_pad,
273 gtk_widget_set_allocation (widget, allocation);
275 if (gtk_widget_get_realized (widget))
276 gdk_window_move_resize (gtk_button_get_event_window (button),
277 allocation->x, allocation->y,
278 allocation->width, allocation->height);
280 child = gtk_bin_get_child (GTK_BIN (button));
281 if (child && gtk_widget_get_visible (child))
283 GtkRequisition child_requisition;
284 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
286 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
288 child_allocation.width = MIN (child_requisition.width,
290 ((border_width + focus_width + focus_pad) * 2
291 + indicator_size + indicator_spacing * 3));
292 child_allocation.width = MAX (child_allocation.width, 1);
294 child_allocation.height = MIN (child_requisition.height,
295 allocation->height - (border_width + focus_width + focus_pad) * 2);
296 child_allocation.height = MAX (child_allocation.height, 1);
298 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
299 allocation->x + focus_width + focus_pad);
300 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
302 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
303 child_allocation.x = allocation->x + allocation->width
304 - (child_allocation.x - allocation->x + child_allocation.width);
306 gtk_widget_size_allocate (child, &child_allocation);
310 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
314 gtk_check_button_draw (GtkWidget *widget,
317 GtkToggleButton *toggle_button;
321 toggle_button = GTK_TOGGLE_BUTTON (widget);
322 bin = GTK_BIN (widget);
324 if (gtk_toggle_button_get_mode (toggle_button))
326 gtk_check_button_paint (widget, cr);
328 child = gtk_bin_get_child (bin);
330 gtk_container_propagate_draw (GTK_CONTAINER (widget),
334 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
335 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
342 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
345 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
347 if (class->draw_indicator)
348 class->draw_indicator (check_button, cr);
352 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
358 GtkToggleButton *toggle_button;
359 GtkStateType state_type;
360 GtkShadowType shadow_type;
363 gint indicator_spacing;
367 gboolean interior_focus;
368 GtkAllocation allocation;
372 widget = GTK_WIDGET (check_button);
373 button = GTK_BUTTON (check_button);
374 toggle_button = GTK_TOGGLE_BUTTON (check_button);
376 gtk_widget_get_allocation (widget, &allocation);
377 style = gtk_widget_get_style (widget);
378 window = gtk_widget_get_window (widget);
380 gtk_widget_style_get (widget,
381 "interior-focus", &interior_focus,
382 "focus-line-width", &focus_width,
383 "focus-padding", &focus_pad,
386 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
388 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
390 x = indicator_spacing + border_width;
391 y = (allocation.height - indicator_size) / 2;
393 child = gtk_bin_get_child (GTK_BIN (check_button));
394 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
395 x += focus_width + focus_pad;
397 if (gtk_toggle_button_get_inconsistent (toggle_button))
398 shadow_type = GTK_SHADOW_ETCHED_IN;
399 else if (gtk_toggle_button_get_active (toggle_button))
400 shadow_type = GTK_SHADOW_IN;
402 shadow_type = GTK_SHADOW_OUT;
404 if (button->activate_timeout || (button->button_down && button->in_button))
405 state_type = GTK_STATE_ACTIVE;
406 else if (button->in_button)
407 state_type = GTK_STATE_PRELIGHT;
408 else if (!gtk_widget_is_sensitive (widget))
409 state_type = GTK_STATE_INSENSITIVE;
411 state_type = GTK_STATE_NORMAL;
413 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
414 x = allocation.width - (indicator_size + x);
416 if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
419 gtk_paint_flat_box (style, cr, GTK_STATE_PRELIGHT,
420 GTK_SHADOW_ETCHED_OUT,
421 widget, "checkbutton",
422 border_width, border_width,
423 allocation.width - (2 * border_width),
424 allocation.height - (2 * border_width));
427 gtk_paint_check (style, cr,
428 state_type, shadow_type,
429 widget, "checkbutton",
430 x, y, indicator_size, indicator_size);