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/.
27 #include "gtkcheckbutton.h"
32 #define INDICATOR_SIZE 13
33 #define INDICATOR_SPACING 2
36 static void gtk_check_button_class_init (GtkCheckButtonClass *klass);
37 static void gtk_check_button_init (GtkCheckButton *check_button);
38 static void gtk_check_button_size_request (GtkWidget *widget,
39 GtkRequisition *requisition);
40 static void gtk_check_button_size_allocate (GtkWidget *widget,
41 GtkAllocation *allocation);
42 static gint gtk_check_button_expose (GtkWidget *widget,
43 GdkEventExpose *event);
44 static void gtk_check_button_paint (GtkWidget *widget,
46 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
48 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
51 static GtkToggleButtonClass *parent_class = NULL;
55 gtk_check_button_get_type (void)
57 static GtkType check_button_type = 0;
59 if (!check_button_type)
61 static const GtkTypeInfo check_button_info =
64 sizeof (GtkCheckButton),
65 sizeof (GtkCheckButtonClass),
66 (GtkClassInitFunc) gtk_check_button_class_init,
67 (GtkObjectInitFunc) gtk_check_button_init,
68 /* reserved_1 */ NULL,
69 /* reserved_2 */ NULL,
70 (GtkClassInitFunc) NULL,
73 check_button_type = gtk_type_unique (GTK_TYPE_TOGGLE_BUTTON, &check_button_info);
76 return check_button_type;
80 gtk_check_button_class_init (GtkCheckButtonClass *class)
82 GtkWidgetClass *widget_class;
84 widget_class = (GtkWidgetClass*) class;
85 parent_class = gtk_type_class (gtk_toggle_button_get_type ());
87 widget_class->size_request = gtk_check_button_size_request;
88 widget_class->size_allocate = gtk_check_button_size_allocate;
89 widget_class->expose_event = gtk_check_button_expose;
91 class->draw_indicator = gtk_real_check_button_draw_indicator;
93 gtk_widget_class_install_style_property (widget_class,
94 g_param_spec_int ("indicator_size",
96 _("Size of check or radio indicator"),
101 gtk_widget_class_install_style_property (widget_class,
102 g_param_spec_int ("indicator_spacing",
103 _("Indicator Spacing"),
104 _("Spacing around check or radio indicator"),
112 gtk_check_button_init (GtkCheckButton *check_button)
114 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
115 GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
116 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
120 gtk_check_button_new (void)
122 return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
127 gtk_check_button_new_with_label (const gchar *label)
129 GtkWidget *check_button;
130 GtkWidget *label_widget;
132 check_button = gtk_check_button_new ();
133 label_widget = gtk_label_new (label);
134 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
136 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
137 gtk_widget_show (label_widget);
143 * gtk_check_button_new_with_mnemonic:
144 * @label: The text of the button, with an underscore in front of the
146 * @returns: a new #GtkCheckButton
148 * Creates a new #GtkCheckButton containing a label. The label
149 * will be created using gtk_label_new_with_mnemonic(), so underscores
150 * in @label indicate the mnemonic for the check button.
153 gtk_check_button_new_with_mnemonic (const gchar *label)
155 GtkWidget *check_button;
156 GtkWidget *label_widget;
158 check_button = gtk_check_button_new ();
159 label_widget = gtk_label_new_with_mnemonic (label);
160 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
162 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
163 gtk_widget_show (label_widget);
169 /* This should only be called when toggle_button->draw_indicator
173 gtk_check_button_paint (GtkWidget *widget,
176 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
178 if (GTK_WIDGET_DRAWABLE (widget))
183 gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
185 gtk_check_button_draw_indicator (check_button, area);
187 border_width = GTK_CONTAINER (widget)->border_width;
188 if (GTK_WIDGET_HAS_FOCUS (widget))
192 GtkWidget *child = GTK_BIN (widget)->child;
194 if (child && GTK_WIDGET_VISIBLE (child))
195 gtk_paint_focus (widget->style, widget->window,
196 NULL, widget, "checkbutton",
197 child->allocation.x - 1,
198 child->allocation.y - 1,
199 child->allocation.width + 1,
200 child->allocation.height + 1);
203 gtk_paint_focus (widget->style, widget->window,
204 NULL, widget, "checkbutton",
205 border_width + widget->allocation.x,
206 border_width + widget->allocation.y,
207 widget->allocation.width - 2 * border_width - 1,
208 widget->allocation.height - 2 * border_width - 1);
214 _gtk_check_button_get_props (GtkCheckButton *check_button,
215 gint *indicator_size,
216 gint *indicator_spacing)
218 GtkWidget *widget = GTK_WIDGET (check_button);
221 gtk_widget_style_get (widget, "indicator_size", indicator_size, NULL);
223 if (indicator_spacing)
224 gtk_widget_style_get (widget, "indicator_spacing", indicator_spacing, NULL);
228 gtk_check_button_size_request (GtkWidget *widget,
229 GtkRequisition *requisition)
231 GtkToggleButton *toggle_button;
233 g_return_if_fail (widget != NULL);
234 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
235 g_return_if_fail (requisition != NULL);
237 toggle_button = GTK_TOGGLE_BUTTON (widget);
239 if (toggle_button->draw_indicator)
244 gint indicator_spacing;
245 gint border_width = GTK_CONTAINER (widget)->border_width;
247 requisition->width = border_width * 2 + 2;
248 requisition->height = border_width * 2 + 2;
250 child = GTK_BIN (widget)->child;
251 if (child && GTK_WIDGET_VISIBLE (child))
253 GtkRequisition child_requisition;
255 gtk_widget_size_request (child, &child_requisition);
257 requisition->width += child_requisition.width;
258 requisition->height += child_requisition.height;
261 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
262 &indicator_size, &indicator_spacing);
264 requisition->width += (indicator_size + indicator_spacing * 3 + 2);
266 temp = (indicator_size + indicator_spacing * 2);
267 requisition->height = MAX (requisition->height, temp) + 2;
270 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
274 gtk_check_button_size_allocate (GtkWidget *widget,
275 GtkAllocation *allocation)
277 GtkCheckButton *check_button;
278 GtkToggleButton *toggle_button;
280 GtkAllocation child_allocation;
282 g_return_if_fail (widget != NULL);
283 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
284 g_return_if_fail (allocation != NULL);
286 check_button = GTK_CHECK_BUTTON (widget);
287 toggle_button = GTK_TOGGLE_BUTTON (widget);
289 if (toggle_button->draw_indicator)
292 gint indicator_spacing;
294 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
296 widget->allocation = *allocation;
297 if (GTK_WIDGET_REALIZED (widget))
298 gdk_window_move_resize (toggle_button->event_window,
299 allocation->x, allocation->y,
300 allocation->width, allocation->height);
302 button = GTK_BUTTON (widget);
304 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
306 gint border_width = GTK_CONTAINER (widget)->border_width;
308 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 + 1 +
309 widget->allocation.x);
310 child_allocation.y = border_width + 1 + widget->allocation.y;
311 child_allocation.width = MAX (1, allocation->width -
312 (border_width + indicator_size + indicator_spacing * 3 + 1) -
314 child_allocation.height = MAX (1, allocation->height - (border_width + 1) * 2);
316 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
317 child_allocation.x = allocation->x + allocation->width
318 - (child_allocation.x - allocation->x + child_allocation.width);
320 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
324 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
328 gtk_check_button_expose (GtkWidget *widget,
329 GdkEventExpose *event)
331 GtkCheckButton *check_button;
332 GtkToggleButton *toggle_button;
335 g_return_val_if_fail (widget != NULL, FALSE);
336 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
337 g_return_val_if_fail (event != NULL, FALSE);
339 check_button = GTK_CHECK_BUTTON (widget);
340 toggle_button = GTK_TOGGLE_BUTTON (widget);
341 bin = GTK_BIN (widget);
343 if (GTK_WIDGET_DRAWABLE (widget))
345 if (toggle_button->draw_indicator)
347 gtk_check_button_paint (widget, &event->area);
350 gtk_container_propagate_expose (GTK_CONTAINER (widget),
354 else if (GTK_WIDGET_CLASS (parent_class)->expose_event)
355 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
363 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
366 GtkCheckButtonClass *class;
368 g_return_if_fail (check_button != NULL);
369 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
371 class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
373 if (class->draw_indicator)
374 (* class->draw_indicator) (check_button, area);
378 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
382 GtkToggleButton *toggle_button;
383 GtkStateType state_type;
384 GtkShadowType shadow_type;
385 GdkRectangle restrict_area;
386 GdkRectangle new_area;
390 gint indicator_spacing;
393 g_return_if_fail (check_button != NULL);
394 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
396 widget = GTK_WIDGET (check_button);
397 toggle_button = GTK_TOGGLE_BUTTON (check_button);
399 if (GTK_WIDGET_DRAWABLE (check_button))
401 window = widget->window;
403 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
405 state_type = GTK_WIDGET_STATE (widget);
406 if (state_type != GTK_STATE_NORMAL &&
407 state_type != GTK_STATE_PRELIGHT)
408 state_type = GTK_STATE_NORMAL;
410 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
411 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
412 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
413 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
415 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
417 if (state_type != GTK_STATE_NORMAL)
418 gtk_paint_flat_box (widget->style, window, state_type,
419 GTK_SHADOW_ETCHED_OUT,
420 area, widget, "checkbutton",
421 new_area.x, new_area.y,
422 new_area.width, new_area.height);
425 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
426 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
427 width = indicator_size;
428 height = indicator_size;
430 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
432 state_type = GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE ? GTK_STATE_NORMAL : GTK_WIDGET_STATE (widget);
433 shadow_type = GTK_SHADOW_ETCHED_IN;
435 else if (GTK_TOGGLE_BUTTON (widget)->active)
437 state_type = GTK_STATE_ACTIVE;
438 shadow_type = GTK_SHADOW_IN;
442 shadow_type = GTK_SHADOW_OUT;
443 state_type = GTK_WIDGET_STATE (widget);
446 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
447 x = widget->allocation.x + widget->allocation.width - (width + x - widget->allocation.x);
449 gtk_paint_check (widget->style, window,
450 state_type, shadow_type,
451 area, widget, "checkbutton",
452 x, y, width, height);