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/.
28 #include "gtkcheckbutton.h"
33 #define INDICATOR_SIZE 13
34 #define INDICATOR_SPACING 2
37 static void gtk_check_button_class_init (GtkCheckButtonClass *klass);
38 static void gtk_check_button_init (GtkCheckButton *check_button);
39 static void gtk_check_button_size_request (GtkWidget *widget,
40 GtkRequisition *requisition);
41 static void gtk_check_button_size_allocate (GtkWidget *widget,
42 GtkAllocation *allocation);
43 static gint gtk_check_button_expose (GtkWidget *widget,
44 GdkEventExpose *event);
45 static void gtk_check_button_paint (GtkWidget *widget,
47 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
49 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
52 static GtkToggleButtonClass *parent_class = NULL;
56 gtk_check_button_get_type (void)
58 static GType check_button_type = 0;
60 if (!check_button_type)
62 static const GTypeInfo check_button_info =
64 sizeof (GtkCheckButtonClass),
66 NULL, /* base_finalize */
67 (GClassInitFunc) gtk_check_button_class_init,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
70 sizeof (GtkCheckButton),
72 (GInstanceInitFunc) gtk_check_button_init,
76 g_type_register_static (GTK_TYPE_TOGGLE_BUTTON, "GtkCheckButton",
77 &check_button_info, 0);
80 return check_button_type;
84 gtk_check_button_class_init (GtkCheckButtonClass *class)
86 GtkWidgetClass *widget_class;
88 widget_class = (GtkWidgetClass*) class;
89 parent_class = g_type_class_peek_parent (class);
91 widget_class->size_request = gtk_check_button_size_request;
92 widget_class->size_allocate = gtk_check_button_size_allocate;
93 widget_class->expose_event = gtk_check_button_expose;
95 class->draw_indicator = gtk_real_check_button_draw_indicator;
97 gtk_widget_class_install_style_property (widget_class,
98 g_param_spec_int ("indicator_size",
100 P_("Size of check or radio indicator"),
105 gtk_widget_class_install_style_property (widget_class,
106 g_param_spec_int ("indicator_spacing",
107 P_("Indicator Spacing"),
108 P_("Spacing around check or radio indicator"),
116 gtk_check_button_init (GtkCheckButton *check_button)
118 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
119 GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
120 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
121 GTK_BUTTON (check_button)->depress_on_activate = FALSE;
125 gtk_check_button_new (void)
127 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
132 gtk_check_button_new_with_label (const gchar *label)
134 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
138 * gtk_check_button_new_with_mnemonic:
139 * @label: The text of the button, with an underscore in front of the
141 * @returns: a new #GtkCheckButton
143 * Creates a new #GtkCheckButton containing a label. The label
144 * will be created using gtk_label_new_with_mnemonic(), so underscores
145 * in @label indicate the mnemonic for the check button.
148 gtk_check_button_new_with_mnemonic (const gchar *label)
150 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, "use_underline", TRUE, NULL);
154 /* This should only be called when toggle_button->draw_indicator
158 gtk_check_button_paint (GtkWidget *widget,
161 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
163 if (GTK_WIDGET_DRAWABLE (widget))
170 gtk_widget_style_get (widget,
171 "interior-focus", &interior_focus,
172 "focus-line-width", &focus_width,
173 "focus-padding", &focus_pad,
176 gtk_check_button_draw_indicator (check_button, area);
178 border_width = GTK_CONTAINER (widget)->border_width;
179 if (GTK_WIDGET_HAS_FOCUS (widget))
181 GtkWidget *child = GTK_BIN (widget)->child;
183 if (interior_focus && child && GTK_WIDGET_VISIBLE (child))
184 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
185 NULL, widget, "checkbutton",
186 child->allocation.x - focus_width - focus_pad,
187 child->allocation.y - focus_width - focus_pad,
188 child->allocation.width + 2 * (focus_width + focus_pad),
189 child->allocation.height + 2 * (focus_width + focus_pad));
191 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
192 NULL, widget, "checkbutton",
193 border_width + widget->allocation.x,
194 border_width + widget->allocation.y,
195 widget->allocation.width - 2 * border_width,
196 widget->allocation.height - 2 * border_width);
202 _gtk_check_button_get_props (GtkCheckButton *check_button,
203 gint *indicator_size,
204 gint *indicator_spacing)
206 GtkWidget *widget = GTK_WIDGET (check_button);
209 gtk_widget_style_get (widget, "indicator_size", indicator_size, NULL);
211 if (indicator_spacing)
212 gtk_widget_style_get (widget, "indicator_spacing", indicator_spacing, NULL);
216 gtk_check_button_size_request (GtkWidget *widget,
217 GtkRequisition *requisition)
219 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
221 if (toggle_button->draw_indicator)
226 gint indicator_spacing;
227 gint border_width = GTK_CONTAINER (widget)->border_width;
231 gtk_widget_style_get (GTK_WIDGET (widget),
232 "focus-line-width", &focus_width,
233 "focus-padding", &focus_pad,
236 requisition->width = border_width * 2;
237 requisition->height = border_width * 2;
239 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
240 &indicator_size, &indicator_spacing);
242 child = GTK_BIN (widget)->child;
243 if (child && GTK_WIDGET_VISIBLE (child))
245 GtkRequisition child_requisition;
247 gtk_widget_size_request (child, &child_requisition);
249 requisition->width += child_requisition.width + indicator_spacing;
250 requisition->height += child_requisition.height;
253 requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
255 temp = indicator_size + indicator_spacing * 2;
256 requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
259 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
263 gtk_check_button_size_allocate (GtkWidget *widget,
264 GtkAllocation *allocation)
266 GtkCheckButton *check_button;
267 GtkToggleButton *toggle_button;
269 GtkAllocation child_allocation;
271 button = GTK_BUTTON (widget);
272 check_button = GTK_CHECK_BUTTON (widget);
273 toggle_button = GTK_TOGGLE_BUTTON (widget);
275 if (toggle_button->draw_indicator)
278 gint indicator_spacing;
282 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
283 gtk_widget_style_get (widget,
284 "focus-line-width", &focus_width,
285 "focus-padding", &focus_pad,
288 widget->allocation = *allocation;
289 if (GTK_WIDGET_REALIZED (widget))
290 gdk_window_move_resize (button->event_window,
291 allocation->x, allocation->y,
292 allocation->width, allocation->height);
294 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
296 GtkRequisition child_requisition;
297 gint border_width = GTK_CONTAINER (widget)->border_width;
299 gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
301 child_allocation.width = MIN (child_requisition.width,
303 ((border_width + focus_width + focus_pad) * 2
304 + indicator_size + indicator_spacing * 3));
305 child_allocation.width = MAX (child_allocation.width, 1);
307 child_allocation.height = MIN (child_requisition.height,
308 allocation->height - (border_width + focus_width + focus_pad) * 2);
309 child_allocation.height = MAX (child_allocation.height, 1);
311 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
312 widget->allocation.x + focus_width + focus_pad);
313 child_allocation.y = widget->allocation.y +
314 (allocation->height - child_allocation.height) / 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 check_button = GTK_CHECK_BUTTON (widget);
336 toggle_button = GTK_TOGGLE_BUTTON (widget);
337 bin = GTK_BIN (widget);
339 if (GTK_WIDGET_DRAWABLE (widget))
341 if (toggle_button->draw_indicator)
343 gtk_check_button_paint (widget, &event->area);
346 gtk_container_propagate_expose (GTK_CONTAINER (widget),
350 else if (GTK_WIDGET_CLASS (parent_class)->expose_event)
351 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
359 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
362 GtkCheckButtonClass *class;
364 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
366 class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
368 if (class->draw_indicator)
369 (* class->draw_indicator) (check_button, area);
373 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
379 GtkToggleButton *toggle_button;
380 GtkStateType state_type;
381 GtkShadowType shadow_type;
384 gint indicator_spacing;
387 gboolean interior_focus;
389 if (GTK_WIDGET_DRAWABLE (check_button))
391 widget = GTK_WIDGET (check_button);
392 button = GTK_BUTTON (check_button);
393 toggle_button = GTK_TOGGLE_BUTTON (check_button);
395 gtk_widget_style_get (widget, "interior_focus", &interior_focus,
396 "focus-line-width", &focus_width,
397 "focus-padding", &focus_pad, NULL);
399 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
401 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
402 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
404 child = GTK_BIN (check_button)->child;
405 if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
406 x += focus_width + focus_pad;
408 if (toggle_button->inconsistent)
409 shadow_type = GTK_SHADOW_ETCHED_IN;
410 else if (toggle_button->active)
411 shadow_type = GTK_SHADOW_IN;
413 shadow_type = GTK_SHADOW_OUT;
415 if (button->activate_timeout || (button->button_down && button->in_button))
416 state_type = GTK_STATE_ACTIVE;
417 else if (button->in_button)
418 state_type = GTK_STATE_PRELIGHT;
419 else if (!GTK_WIDGET_IS_SENSITIVE (widget))
420 state_type = GTK_STATE_INSENSITIVE;
422 state_type = GTK_STATE_NORMAL;
424 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
425 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
427 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
429 GdkRectangle restrict_area;
430 GdkRectangle new_area;
432 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
433 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
434 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
435 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
437 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
439 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
440 GTK_SHADOW_ETCHED_OUT,
441 area, widget, "checkbutton",
442 new_area.x, new_area.y,
443 new_area.width, new_area.height);
447 gtk_paint_check (widget->style, widget->window,
448 state_type, shadow_type,
449 area, widget, "checkbutton",
450 x, y, indicator_size, indicator_size);