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;
117 GTK_BUTTON (check_button)->depress_on_activate = FALSE;
121 gtk_check_button_new (void)
123 return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
128 gtk_check_button_new_with_label (const gchar *label)
130 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
134 * gtk_check_button_new_with_mnemonic:
135 * @label: The text of the button, with an underscore in front of the
137 * @returns: a new #GtkCheckButton
139 * Creates a new #GtkCheckButton containing a label. The label
140 * will be created using gtk_label_new_with_mnemonic(), so underscores
141 * in @label indicate the mnemonic for the check button.
144 gtk_check_button_new_with_mnemonic (const gchar *label)
146 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, "use_underline", TRUE, NULL);
150 /* This should only be called when toggle_button->draw_indicator
154 gtk_check_button_paint (GtkWidget *widget,
157 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
159 if (GTK_WIDGET_DRAWABLE (widget))
166 gtk_widget_style_get (widget,
167 "interior-focus", &interior_focus,
168 "focus-line-width", &focus_width,
169 "focus-padding", &focus_pad,
172 gtk_check_button_draw_indicator (check_button, area);
174 border_width = GTK_CONTAINER (widget)->border_width;
175 if (GTK_WIDGET_HAS_FOCUS (widget))
179 GtkWidget *child = GTK_BIN (widget)->child;
181 if (child && GTK_WIDGET_VISIBLE (child))
182 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
183 NULL, widget, "checkbutton",
184 child->allocation.x - focus_width - focus_pad,
185 child->allocation.y - focus_width - focus_pad,
186 child->allocation.width + 2 * (focus_width + focus_pad),
187 child->allocation.height + 2 * (focus_width + focus_pad));
190 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
191 NULL, widget, "checkbutton",
192 border_width + widget->allocation.x,
193 border_width + widget->allocation.y,
194 widget->allocation.width - 2 * border_width,
195 widget->allocation.height - 2 * border_width);
201 _gtk_check_button_get_props (GtkCheckButton *check_button,
202 gint *indicator_size,
203 gint *indicator_spacing)
205 GtkWidget *widget = GTK_WIDGET (check_button);
208 gtk_widget_style_get (widget, "indicator_size", indicator_size, NULL);
210 if (indicator_spacing)
211 gtk_widget_style_get (widget, "indicator_spacing", indicator_spacing, NULL);
215 gtk_check_button_size_request (GtkWidget *widget,
216 GtkRequisition *requisition)
218 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
220 if (toggle_button->draw_indicator)
225 gint indicator_spacing;
226 gint border_width = GTK_CONTAINER (widget)->border_width;
230 gtk_widget_style_get (GTK_WIDGET (widget),
231 "focus-line-width", &focus_width,
232 "focus-padding", &focus_pad,
235 requisition->width = border_width * 2;
236 requisition->height = border_width * 2;
238 child = GTK_BIN (widget)->child;
239 if (child && GTK_WIDGET_VISIBLE (child))
241 GtkRequisition child_requisition;
243 gtk_widget_size_request (child, &child_requisition);
245 requisition->width += child_requisition.width;
246 requisition->height += child_requisition.height;
249 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
250 &indicator_size, &indicator_spacing);
252 requisition->width += (indicator_size + indicator_spacing * 3 + 2 * (focus_width + focus_pad));
254 temp = indicator_size + indicator_spacing * 2;
255 requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
258 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
262 gtk_check_button_size_allocate (GtkWidget *widget,
263 GtkAllocation *allocation)
265 GtkCheckButton *check_button;
266 GtkToggleButton *toggle_button;
268 GtkAllocation child_allocation;
270 button = GTK_BUTTON (widget);
271 check_button = GTK_CHECK_BUTTON (widget);
272 toggle_button = GTK_TOGGLE_BUTTON (widget);
274 if (toggle_button->draw_indicator)
277 gint indicator_spacing;
281 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
282 gtk_widget_style_get (widget,
283 "focus-line-width", &focus_width,
284 "focus-padding", &focus_pad,
287 widget->allocation = *allocation;
288 if (GTK_WIDGET_REALIZED (widget))
289 gdk_window_move_resize (button->event_window,
290 allocation->x, allocation->y,
291 allocation->width, allocation->height);
293 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
295 GtkRequisition child_requisition;
296 gint border_width = GTK_CONTAINER (widget)->border_width;
298 gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
300 child_allocation.width = MIN (child_requisition.width,
302 ((border_width + focus_width + focus_pad) * 2
303 + indicator_size + indicator_spacing * 3));
305 child_allocation.height = MIN (child_requisition.height,
306 allocation->height - (border_width + focus_width + focus_pad) * 2);
307 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
308 widget->allocation.x + focus_width + focus_pad);
309 child_allocation.y = widget->allocation.y +
310 (allocation->height - child_allocation.height) / 2;
312 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
313 child_allocation.x = allocation->x + allocation->width
314 - (child_allocation.x - allocation->x + child_allocation.width);
316 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
320 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
324 gtk_check_button_expose (GtkWidget *widget,
325 GdkEventExpose *event)
327 GtkCheckButton *check_button;
328 GtkToggleButton *toggle_button;
331 check_button = GTK_CHECK_BUTTON (widget);
332 toggle_button = GTK_TOGGLE_BUTTON (widget);
333 bin = GTK_BIN (widget);
335 if (GTK_WIDGET_DRAWABLE (widget))
337 if (toggle_button->draw_indicator)
339 gtk_check_button_paint (widget, &event->area);
342 gtk_container_propagate_expose (GTK_CONTAINER (widget),
346 else if (GTK_WIDGET_CLASS (parent_class)->expose_event)
347 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
355 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
358 GtkCheckButtonClass *class;
360 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
362 class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
364 if (class->draw_indicator)
365 (* class->draw_indicator) (check_button, area);
369 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
374 GtkToggleButton *toggle_button;
375 GtkStateType state_type;
376 GtkShadowType shadow_type;
379 gint indicator_spacing;
382 gboolean interior_focus;
384 if (GTK_WIDGET_DRAWABLE (check_button))
386 widget = GTK_WIDGET (check_button);
387 button = GTK_BUTTON (check_button);
388 toggle_button = GTK_TOGGLE_BUTTON (check_button);
390 gtk_widget_style_get (widget, "interior_focus", &interior_focus,
391 "focus-line-width", &focus_width,
392 "focus-padding", &focus_pad, NULL);
394 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
396 x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
397 y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
400 x += focus_width + focus_pad;
402 if (toggle_button->inconsistent)
403 shadow_type = GTK_SHADOW_ETCHED_IN;
404 else if (toggle_button->active)
405 shadow_type = GTK_SHADOW_IN;
407 shadow_type = GTK_SHADOW_OUT;
409 if (button->activate_timeout || (button->button_down && button->in_button))
410 state_type = GTK_STATE_ACTIVE;
411 else if (button->in_button)
412 state_type = GTK_STATE_PRELIGHT;
414 state_type = GTK_STATE_NORMAL;
416 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
417 x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
419 if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
421 GdkRectangle restrict_area;
422 GdkRectangle new_area;
424 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
425 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
426 restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
427 restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
429 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
431 gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
432 GTK_SHADOW_ETCHED_OUT,
433 area, widget, "checkbutton",
434 new_area.x, new_area.y,
435 new_area.width, new_area.height);
439 gtk_paint_check (widget->style, widget->window,
440 state_type, shadow_type,
441 area, widget, "checkbutton",
442 x, y, indicator_size, indicator_size);