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"
30 #include "gtkprivate.h"
35 #define INDICATOR_SIZE 13
36 #define INDICATOR_SPACING 2
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 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON);
55 gtk_check_button_class_init (GtkCheckButtonClass *class)
57 GtkWidgetClass *widget_class;
59 widget_class = (GtkWidgetClass*) class;
61 widget_class->size_request = gtk_check_button_size_request;
62 widget_class->size_allocate = gtk_check_button_size_allocate;
63 widget_class->expose_event = gtk_check_button_expose;
65 class->draw_indicator = gtk_real_check_button_draw_indicator;
67 gtk_widget_class_install_style_property (widget_class,
68 g_param_spec_int ("indicator-size",
70 P_("Size of check or radio indicator"),
75 gtk_widget_class_install_style_property (widget_class,
76 g_param_spec_int ("indicator-spacing",
77 P_("Indicator Spacing"),
78 P_("Spacing around check or radio indicator"),
86 gtk_check_button_init (GtkCheckButton *check_button)
88 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
89 GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
90 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
91 GTK_BUTTON (check_button)->depress_on_activate = FALSE;
95 gtk_check_button_new (void)
97 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
102 gtk_check_button_new_with_label (const gchar *label)
104 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
108 * gtk_check_button_new_with_mnemonic:
109 * @label: The text of the button, with an underscore in front of the
111 * @returns: a new #GtkCheckButton
113 * Creates a new #GtkCheckButton containing a label. The label
114 * will be created using gtk_label_new_with_mnemonic(), so underscores
115 * in @label indicate the mnemonic for the check button.
118 gtk_check_button_new_with_mnemonic (const gchar *label)
120 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, "use_underline", TRUE, NULL);
124 /* This should only be called when toggle_button->draw_indicator
128 gtk_check_button_paint (GtkWidget *widget,
131 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
133 if (GTK_WIDGET_DRAWABLE (widget))
140 gtk_widget_style_get (widget,
141 "interior-focus", &interior_focus,
142 "focus-line-width", &focus_width,
143 "focus-padding", &focus_pad,
146 gtk_check_button_draw_indicator (check_button, area);
148 border_width = GTK_CONTAINER (widget)->border_width;
149 if (GTK_WIDGET_HAS_FOCUS (widget))
151 GtkWidget *child = GTK_BIN (widget)->child;
153 if (interior_focus && child && GTK_WIDGET_VISIBLE (child))
154 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
155 NULL, widget, "checkbutton",
156 child->allocation.x - focus_width - focus_pad,
157 child->allocation.y - focus_width - focus_pad,
158 child->allocation.width + 2 * (focus_width + focus_pad),
159 child->allocation.height + 2 * (focus_width + focus_pad));
161 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
162 NULL, widget, "checkbutton",
163 border_width + widget->allocation.x,
164 border_width + widget->allocation.y,
165 widget->allocation.width - 2 * border_width,
166 widget->allocation.height - 2 * border_width);
172 _gtk_check_button_get_props (GtkCheckButton *check_button,
173 gint *indicator_size,
174 gint *indicator_spacing)
176 GtkWidget *widget = GTK_WIDGET (check_button);
179 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
181 if (indicator_spacing)
182 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
186 gtk_check_button_size_request (GtkWidget *widget,
187 GtkRequisition *requisition)
189 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
191 if (toggle_button->draw_indicator)
196 gint indicator_spacing;
197 gint border_width = GTK_CONTAINER (widget)->border_width;
201 gtk_widget_style_get (GTK_WIDGET (widget),
202 "focus-line-width", &focus_width,
203 "focus-padding", &focus_pad,
206 requisition->width = border_width * 2;
207 requisition->height = border_width * 2;
209 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
210 &indicator_size, &indicator_spacing);
212 child = GTK_BIN (widget)->child;
213 if (child && GTK_WIDGET_VISIBLE (child))
215 GtkRequisition child_requisition;
217 gtk_widget_size_request (child, &child_requisition);
219 requisition->width += child_requisition.width + indicator_spacing;
220 requisition->height += child_requisition.height;
223 requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
225 temp = indicator_size + indicator_spacing * 2;
226 requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
229 (* GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request) (widget, requisition);
233 gtk_check_button_size_allocate (GtkWidget *widget,
234 GtkAllocation *allocation)
236 GtkCheckButton *check_button;
237 GtkToggleButton *toggle_button;
239 GtkAllocation child_allocation;
241 button = GTK_BUTTON (widget);
242 check_button = GTK_CHECK_BUTTON (widget);
243 toggle_button = GTK_TOGGLE_BUTTON (widget);
245 if (toggle_button->draw_indicator)
248 gint indicator_spacing;
252 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
253 gtk_widget_style_get (widget,
254 "focus-line-width", &focus_width,
255 "focus-padding", &focus_pad,
258 widget->allocation = *allocation;
259 if (GTK_WIDGET_REALIZED (widget))
260 gdk_window_move_resize (button->event_window,
261 allocation->x, allocation->y,
262 allocation->width, allocation->height);
264 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
266 GtkRequisition child_requisition;
267 gint border_width = GTK_CONTAINER (widget)->border_width;
269 gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
271 child_allocation.width = MIN (child_requisition.width,
273 ((border_width + focus_width + focus_pad) * 2
274 + indicator_size + indicator_spacing * 3));
275 child_allocation.width = MAX (child_allocation.width, 1);
277 child_allocation.height = MIN (child_requisition.height,
278 allocation->height - (border_width + focus_width + focus_pad) * 2);
279 child_allocation.height = MAX (child_allocation.height, 1);
281 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
282 widget->allocation.x + focus_width + focus_pad);
283 child_allocation.y = widget->allocation.y +
284 (allocation->height - child_allocation.height) / 2;
286 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
287 child_allocation.x = allocation->x + allocation->width
288 - (child_allocation.x - allocation->x + child_allocation.width);
290 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
294 (* GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate) (widget, allocation);
298 gtk_check_button_expose (GtkWidget *widget,
299 GdkEventExpose *event)
301 GtkToggleButton *toggle_button;
304 toggle_button = GTK_TOGGLE_BUTTON (widget);
305 bin = GTK_BIN (widget);
307 if (GTK_WIDGET_DRAWABLE (widget))
309 if (toggle_button->draw_indicator)
311 gtk_check_button_paint (widget, &event->area);
314 gtk_container_propagate_expose (GTK_CONTAINER (widget),
318 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event)
319 (* GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event) (widget, event);
327 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
330 GtkCheckButtonClass *class;
332 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
334 class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
336 if (class->draw_indicator)
337 (* class->draw_indicator) (check_button, area);
341 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
347 GtkToggleButton *toggle_button;
348 GtkStateType state_type;
349 GtkShadowType shadow_type;
352 gint indicator_spacing;
355 gboolean interior_focus;
357 if (GTK_WIDGET_DRAWABLE (check_button))
359 widget = GTK_WIDGET (check_button);
360 button = GTK_BUTTON (check_button);
361 toggle_button = GTK_TOGGLE_BUTTON (check_button);
363 gtk_widget_style_get (widget,
364 "interior-focus", &interior_focus,
365 "focus-line-width", &focus_width,
366 "focus-padding", &focus_pad,
369 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
371 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
372 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
374 child = GTK_BIN (check_button)->child;
375 if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
376 x += focus_width + focus_pad;
378 if (toggle_button->inconsistent)
379 shadow_type = GTK_SHADOW_ETCHED_IN;
380 else if (toggle_button->active)
381 shadow_type = GTK_SHADOW_IN;
383 shadow_type = GTK_SHADOW_OUT;
385 if (button->activate_timeout || (button->button_down && button->in_button))
386 state_type = GTK_STATE_ACTIVE;
387 else if (button->in_button)
388 state_type = GTK_STATE_PRELIGHT;
389 else if (!GTK_WIDGET_IS_SENSITIVE (widget))
390 state_type = GTK_STATE_INSENSITIVE;
392 state_type = GTK_STATE_NORMAL;
394 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
395 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
397 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
399 GdkRectangle restrict_area;
400 GdkRectangle new_area;
402 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
403 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
404 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
405 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
407 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
409 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
410 GTK_SHADOW_ETCHED_OUT,
411 area, widget, "checkbutton",
412 new_area.x, new_area.y,
413 new_area.width, new_area.height);
417 gtk_paint_check (widget->style, widget->window,
418 state_type, shadow_type,
419 area, widget, "checkbutton",
420 x, y, indicator_size, indicator_size);
424 #define __GTK_CHECK_BUTTON_C__
425 #include "gtkaliasdef.c"