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"
34 #define INDICATOR_SIZE 13
35 #define INDICATOR_SPACING 2
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 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
54 gtk_check_button_class_init (GtkCheckButtonClass *class)
56 GtkWidgetClass *widget_class;
58 widget_class = (GtkWidgetClass*) class;
60 widget_class->size_request = gtk_check_button_size_request;
61 widget_class->size_allocate = gtk_check_button_size_allocate;
62 widget_class->expose_event = gtk_check_button_expose;
64 class->draw_indicator = gtk_real_check_button_draw_indicator;
66 gtk_widget_class_install_style_property (widget_class,
67 g_param_spec_int ("indicator-size",
69 P_("Size of check or radio indicator"),
74 gtk_widget_class_install_style_property (widget_class,
75 g_param_spec_int ("indicator-spacing",
76 P_("Indicator Spacing"),
77 P_("Spacing around check or radio indicator"),
85 gtk_check_button_init (GtkCheckButton *check_button)
87 gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
88 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
89 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
90 GTK_BUTTON (check_button)->depress_on_activate = FALSE;
94 gtk_check_button_new (void)
96 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
101 gtk_check_button_new_with_label (const gchar *label)
103 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
107 * gtk_check_button_new_with_mnemonic:
108 * @label: The text of the button, with an underscore in front of the
110 * @returns: a new #GtkCheckButton
112 * Creates a new #GtkCheckButton containing a label. The label
113 * will be created using gtk_label_new_with_mnemonic(), so underscores
114 * in @label indicate the mnemonic for the check button.
117 gtk_check_button_new_with_mnemonic (const gchar *label)
119 return g_object_new (GTK_TYPE_CHECK_BUTTON,
121 "use-underline", TRUE,
126 /* This should only be called when toggle_button->draw_indicator
130 gtk_check_button_paint (GtkWidget *widget,
133 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
135 if (gtk_widget_is_drawable (widget))
142 gtk_widget_style_get (widget,
143 "interior-focus", &interior_focus,
144 "focus-line-width", &focus_width,
145 "focus-padding", &focus_pad,
148 gtk_check_button_draw_indicator (check_button, area);
150 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
151 if (gtk_widget_has_focus (widget))
153 GtkWidget *child = GTK_BIN (widget)->child;
155 if (interior_focus && child && gtk_widget_get_visible (child))
156 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
157 area, widget, "checkbutton",
158 child->allocation.x - focus_width - focus_pad,
159 child->allocation.y - focus_width - focus_pad,
160 child->allocation.width + 2 * (focus_width + focus_pad),
161 child->allocation.height + 2 * (focus_width + focus_pad));
163 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
164 area, widget, "checkbutton",
165 border_width + widget->allocation.x,
166 border_width + widget->allocation.y,
167 widget->allocation.width - 2 * border_width,
168 widget->allocation.height - 2 * border_width);
174 _gtk_check_button_get_props (GtkCheckButton *check_button,
175 gint *indicator_size,
176 gint *indicator_spacing)
178 GtkWidget *widget = GTK_WIDGET (check_button);
181 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
183 if (indicator_spacing)
184 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
188 gtk_check_button_size_request (GtkWidget *widget,
189 GtkRequisition *requisition)
191 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
193 if (toggle_button->draw_indicator)
198 gint indicator_spacing;
203 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
205 gtk_widget_style_get (GTK_WIDGET (widget),
206 "focus-line-width", &focus_width,
207 "focus-padding", &focus_pad,
210 requisition->width = border_width * 2;
211 requisition->height = border_width * 2;
213 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
214 &indicator_size, &indicator_spacing);
216 child = GTK_BIN (widget)->child;
217 if (child && gtk_widget_get_visible (child))
219 GtkRequisition child_requisition;
221 gtk_widget_size_request (child, &child_requisition);
223 requisition->width += child_requisition.width + indicator_spacing;
224 requisition->height += child_requisition.height;
227 requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
229 temp = indicator_size + indicator_spacing * 2;
230 requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
233 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request (widget, requisition);
237 gtk_check_button_size_allocate (GtkWidget *widget,
238 GtkAllocation *allocation)
240 GtkCheckButton *check_button;
241 GtkToggleButton *toggle_button;
243 GtkAllocation child_allocation;
245 button = GTK_BUTTON (widget);
246 check_button = GTK_CHECK_BUTTON (widget);
247 toggle_button = GTK_TOGGLE_BUTTON (widget);
249 if (toggle_button->draw_indicator)
252 gint indicator_spacing;
256 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
257 gtk_widget_style_get (widget,
258 "focus-line-width", &focus_width,
259 "focus-padding", &focus_pad,
262 widget->allocation = *allocation;
263 if (gtk_widget_get_realized (widget))
264 gdk_window_move_resize (button->event_window,
265 allocation->x, allocation->y,
266 allocation->width, allocation->height);
268 if (GTK_BIN (button)->child && gtk_widget_get_visible (GTK_BIN (button)->child))
270 GtkRequisition child_requisition;
271 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
273 gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
275 child_allocation.width = MIN (child_requisition.width,
277 ((border_width + focus_width + focus_pad) * 2
278 + indicator_size + indicator_spacing * 3));
279 child_allocation.width = MAX (child_allocation.width, 1);
281 child_allocation.height = MIN (child_requisition.height,
282 allocation->height - (border_width + focus_width + focus_pad) * 2);
283 child_allocation.height = MAX (child_allocation.height, 1);
285 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
286 widget->allocation.x + focus_width + focus_pad);
287 child_allocation.y = widget->allocation.y +
288 (allocation->height - child_allocation.height) / 2;
290 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
291 child_allocation.x = allocation->x + allocation->width
292 - (child_allocation.x - allocation->x + child_allocation.width);
294 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
298 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
302 gtk_check_button_expose (GtkWidget *widget,
303 GdkEventExpose *event)
305 GtkToggleButton *toggle_button;
308 toggle_button = GTK_TOGGLE_BUTTON (widget);
309 bin = GTK_BIN (widget);
311 if (gtk_widget_is_drawable (widget))
313 if (toggle_button->draw_indicator)
315 gtk_check_button_paint (widget, &event->area);
318 gtk_container_propagate_expose (GTK_CONTAINER (widget),
322 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event)
323 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event (widget, event);
331 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
334 GtkCheckButtonClass *class;
336 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
338 class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
340 if (class->draw_indicator)
341 class->draw_indicator (check_button, area);
345 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
351 GtkToggleButton *toggle_button;
352 GtkStateType state_type;
353 GtkShadowType shadow_type;
356 gint indicator_spacing;
360 gboolean interior_focus;
362 widget = GTK_WIDGET (check_button);
364 if (gtk_widget_is_drawable (widget))
366 button = GTK_BUTTON (check_button);
367 toggle_button = GTK_TOGGLE_BUTTON (check_button);
369 gtk_widget_style_get (widget,
370 "interior-focus", &interior_focus,
371 "focus-line-width", &focus_width,
372 "focus-padding", &focus_pad,
375 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
377 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
379 x = widget->allocation.x + indicator_spacing + border_width;
380 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
382 child = GTK_BIN (check_button)->child;
383 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
384 x += focus_width + focus_pad;
386 if (toggle_button->inconsistent)
387 shadow_type = GTK_SHADOW_ETCHED_IN;
388 else if (toggle_button->active)
389 shadow_type = GTK_SHADOW_IN;
391 shadow_type = GTK_SHADOW_OUT;
393 if (button->activate_timeout || (button->button_down && button->in_button))
394 state_type = GTK_STATE_ACTIVE;
395 else if (button->in_button)
396 state_type = GTK_STATE_PRELIGHT;
397 else if (!gtk_widget_is_sensitive (widget))
398 state_type = GTK_STATE_INSENSITIVE;
400 state_type = GTK_STATE_NORMAL;
402 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
403 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
405 if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
407 GdkRectangle restrict_area;
408 GdkRectangle new_area;
410 restrict_area.x = widget->allocation.x + border_width;
411 restrict_area.y = widget->allocation.y + border_width;
412 restrict_area.width = widget->allocation.width - (2 * border_width);
413 restrict_area.height = widget->allocation.height - (2 * border_width);
415 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
417 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
418 GTK_SHADOW_ETCHED_OUT,
419 area, widget, "checkbutton",
420 new_area.x, new_area.y,
421 new_area.width, new_area.height);
425 gtk_paint_check (widget->style, widget->window,
426 state_type, shadow_type,
427 area, widget, "checkbutton",
428 x, y, indicator_size, indicator_size);