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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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"
31 #define INDICATOR_SIZE 10
32 #define INDICATOR_SPACING 2
34 #define CHECK_BUTTON_CLASS(w) GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
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_draw (GtkWidget *widget,
41 static void gtk_check_button_draw_focus (GtkWidget *widget);
42 static void gtk_check_button_size_request (GtkWidget *widget,
43 GtkRequisition *requisition);
44 static void gtk_check_button_size_allocate (GtkWidget *widget,
45 GtkAllocation *allocation);
46 static gint gtk_check_button_expose (GtkWidget *widget,
47 GdkEventExpose *event);
48 static void gtk_check_button_paint (GtkWidget *widget,
50 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
52 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
55 static GtkToggleButtonClass *parent_class = NULL;
59 gtk_check_button_get_type (void)
61 static GtkType check_button_type = 0;
63 if (!check_button_type)
65 static const GtkTypeInfo check_button_info =
68 sizeof (GtkCheckButton),
69 sizeof (GtkCheckButtonClass),
70 (GtkClassInitFunc) gtk_check_button_class_init,
71 (GtkObjectInitFunc) gtk_check_button_init,
72 /* reserved_1 */ NULL,
73 /* reserved_2 */ NULL,
74 (GtkClassInitFunc) NULL,
77 check_button_type = gtk_type_unique (GTK_TYPE_TOGGLE_BUTTON, &check_button_info);
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 = gtk_type_class (gtk_toggle_button_get_type ());
91 widget_class->draw = gtk_check_button_draw;
92 widget_class->draw_focus = gtk_check_button_draw_focus;
93 widget_class->size_request = gtk_check_button_size_request;
94 widget_class->size_allocate = gtk_check_button_size_allocate;
95 widget_class->expose_event = gtk_check_button_expose;
97 class->indicator_size = INDICATOR_SIZE;
98 class->indicator_spacing = INDICATOR_SPACING;
99 class->draw_indicator = gtk_real_check_button_draw_indicator;
103 gtk_check_button_init (GtkCheckButton *check_button)
105 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
106 GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
107 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
111 gtk_check_button_new (void)
113 return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
118 gtk_check_button_new_with_label (const gchar *label)
120 GtkWidget *check_button;
121 GtkWidget *label_widget;
123 check_button = gtk_check_button_new ();
124 label_widget = gtk_label_new (label);
125 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
127 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
128 gtk_widget_show (label_widget);
133 /* This should only be called when toggle_button->draw_indicator
137 gtk_check_button_paint (GtkWidget *widget,
140 GtkCheckButton *check_button;
142 g_return_if_fail (widget != NULL);
143 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
145 check_button = GTK_CHECK_BUTTON (widget);
147 if (GTK_WIDGET_DRAWABLE (widget))
151 gtk_check_button_draw_indicator (check_button, area);
153 border_width = GTK_CONTAINER (widget)->border_width;
154 if (GTK_WIDGET_HAS_FOCUS (widget))
155 gtk_paint_focus (widget->style, widget->window,
156 NULL, widget, "checkbutton",
157 border_width + widget->allocation.x,
158 border_width + widget->allocation.y,
159 widget->allocation.width - 2 * border_width - 1,
160 widget->allocation.height - 2 * border_width - 1);
165 gtk_check_button_draw (GtkWidget *widget,
168 GtkCheckButton *check_button;
169 GtkToggleButton *toggle_button;
171 GdkRectangle child_area;
173 g_return_if_fail (widget != NULL);
174 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
175 g_return_if_fail (area != NULL);
177 check_button = GTK_CHECK_BUTTON (widget);
178 toggle_button = GTK_TOGGLE_BUTTON (widget);
179 bin = GTK_BIN (widget);
181 if (GTK_WIDGET_DRAWABLE (widget))
183 if (toggle_button->draw_indicator)
185 gtk_check_button_paint (widget, area);
187 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
188 gtk_widget_draw (bin->child, &child_area);
192 if (GTK_WIDGET_CLASS (parent_class)->draw)
193 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
199 gtk_check_button_draw_focus (GtkWidget *widget)
203 g_return_if_fail (widget != NULL);
204 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
206 border_width = GTK_CONTAINER (widget)->border_width;
207 gtk_widget_queue_clear_area (widget->parent,
208 border_width + widget->allocation.x,
209 border_width + widget->allocation.y,
210 widget->allocation.width - 2 * border_width,
211 widget->allocation.height - 2 * border_width);
215 gtk_check_button_size_request (GtkWidget *widget,
216 GtkRequisition *requisition)
218 GtkToggleButton *toggle_button;
221 g_return_if_fail (widget != NULL);
222 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
223 g_return_if_fail (requisition != NULL);
225 toggle_button = GTK_TOGGLE_BUTTON (widget);
227 if (GTK_WIDGET_CLASS (parent_class)->size_request)
228 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
230 if (toggle_button->draw_indicator)
232 requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
233 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
235 temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
236 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
237 requisition->height = MAX (requisition->height, temp) + 2;
242 gtk_check_button_size_allocate (GtkWidget *widget,
243 GtkAllocation *allocation)
245 GtkCheckButton *check_button;
246 GtkToggleButton *toggle_button;
248 GtkAllocation child_allocation;
250 g_return_if_fail (widget != NULL);
251 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
252 g_return_if_fail (allocation != NULL);
254 check_button = GTK_CHECK_BUTTON (widget);
255 toggle_button = GTK_TOGGLE_BUTTON (widget);
257 if (toggle_button->draw_indicator)
259 widget->allocation = *allocation;
260 if (GTK_WIDGET_REALIZED (widget))
261 gdk_window_move_resize (toggle_button->event_window,
262 allocation->x, allocation->y,
263 allocation->width, allocation->height);
265 button = GTK_BUTTON (widget);
267 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
269 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
270 CHECK_BUTTON_CLASS (widget)->indicator_size +
271 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1 +
272 widget->allocation.x);
273 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1 +
274 widget->allocation.y;
275 child_allocation.width = MAX (1, allocation->width -
276 (GTK_CONTAINER (widget)->border_width +
277 CHECK_BUTTON_CLASS (widget)->indicator_size +
278 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1) -
279 GTK_CONTAINER (widget)->border_width - 1);
280 child_allocation.height = MAX (1, allocation->height - (GTK_CONTAINER (widget)->border_width + 1) * 2);
282 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
287 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
288 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
293 gtk_check_button_expose (GtkWidget *widget,
294 GdkEventExpose *event)
296 GtkCheckButton *check_button;
297 GtkToggleButton *toggle_button;
299 GdkEventExpose child_event;
301 g_return_val_if_fail (widget != NULL, FALSE);
302 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
303 g_return_val_if_fail (event != NULL, FALSE);
305 check_button = GTK_CHECK_BUTTON (widget);
306 toggle_button = GTK_TOGGLE_BUTTON (widget);
307 bin = GTK_BIN (widget);
309 if (GTK_WIDGET_DRAWABLE (widget))
311 if (toggle_button->draw_indicator)
313 gtk_check_button_paint (widget, &event->area);
315 child_event = *event;
316 if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) &&
317 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
318 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
322 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
323 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
332 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
335 GtkCheckButtonClass *class;
337 g_return_if_fail (check_button != NULL);
338 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
340 class = CHECK_BUTTON_CLASS (check_button);
342 if (class->draw_indicator)
343 (* class->draw_indicator) (check_button, area);
347 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
351 GtkToggleButton *toggle_button;
352 GtkStateType state_type;
353 GtkShadowType shadow_type;
354 GdkRectangle restrict_area;
355 GdkRectangle new_area;
360 g_return_if_fail (check_button != NULL);
361 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
363 widget = GTK_WIDGET (check_button);
364 toggle_button = GTK_TOGGLE_BUTTON (check_button);
366 if (GTK_WIDGET_DRAWABLE (check_button))
368 window = widget->window;
370 state_type = GTK_WIDGET_STATE (widget);
371 if (state_type != GTK_STATE_NORMAL &&
372 state_type != GTK_STATE_PRELIGHT)
373 state_type = GTK_STATE_NORMAL;
375 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
376 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
377 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
378 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
380 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
382 if (state_type != GTK_STATE_NORMAL)
383 gtk_paint_flat_box (widget->style, window, state_type,
384 GTK_SHADOW_ETCHED_OUT,
385 area, widget, "checkbutton",
386 new_area.x, new_area.y,
387 new_area.width, new_area.height);
390 x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
391 y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
392 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
393 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
395 if (GTK_TOGGLE_BUTTON (widget)->active)
397 state_type = GTK_STATE_ACTIVE;
398 shadow_type = GTK_SHADOW_IN;
402 shadow_type = GTK_SHADOW_OUT;
403 state_type = GTK_WIDGET_STATE (widget);
406 gtk_paint_check (widget->style, window,
407 state_type, shadow_type,
408 area, widget, "checkbutton",
409 x + 1, y + 1, width, height);