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.
19 #include "gtkcheckbutton.h"
23 #define INDICATOR_SIZE 10
24 #define INDICATOR_SPACING 2
26 #define CHECK_BUTTON_CLASS(w) GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
29 static void gtk_check_button_class_init (GtkCheckButtonClass *klass);
30 static void gtk_check_button_init (GtkCheckButton *check_button);
31 static void gtk_check_button_draw (GtkWidget *widget,
33 static void gtk_check_button_draw_focus (GtkWidget *widget);
34 static void gtk_check_button_size_request (GtkWidget *widget,
35 GtkRequisition *requisition);
36 static void gtk_check_button_size_allocate (GtkWidget *widget,
37 GtkAllocation *allocation);
38 static gint gtk_check_button_expose (GtkWidget *widget,
39 GdkEventExpose *event);
40 static void gtk_check_button_paint (GtkWidget *widget,
42 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
44 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
47 static GtkToggleButtonClass *parent_class = NULL;
51 gtk_check_button_get_type (void)
53 static GtkType check_button_type = 0;
55 if (!check_button_type)
57 static const GtkTypeInfo check_button_info =
60 sizeof (GtkCheckButton),
61 sizeof (GtkCheckButtonClass),
62 (GtkClassInitFunc) gtk_check_button_class_init,
63 (GtkObjectInitFunc) gtk_check_button_init,
64 /* reserved_1 */ NULL,
65 /* reserved_2 */ NULL,
66 (GtkClassInitFunc) NULL,
69 check_button_type = gtk_type_unique (GTK_TYPE_TOGGLE_BUTTON, &check_button_info);
72 return check_button_type;
76 gtk_check_button_class_init (GtkCheckButtonClass *class)
78 GtkWidgetClass *widget_class;
80 widget_class = (GtkWidgetClass*) class;
81 parent_class = gtk_type_class (gtk_toggle_button_get_type ());
83 widget_class->draw = gtk_check_button_draw;
84 widget_class->draw_focus = gtk_check_button_draw_focus;
85 widget_class->size_request = gtk_check_button_size_request;
86 widget_class->size_allocate = gtk_check_button_size_allocate;
87 widget_class->expose_event = gtk_check_button_expose;
89 class->indicator_size = INDICATOR_SIZE;
90 class->indicator_spacing = INDICATOR_SPACING;
91 class->draw_indicator = gtk_real_check_button_draw_indicator;
95 gtk_check_button_init (GtkCheckButton *check_button)
97 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
98 GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
99 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
103 gtk_check_button_new (void)
105 return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
110 gtk_check_button_new_with_label (const gchar *label)
112 GtkWidget *check_button;
113 GtkWidget *label_widget;
115 check_button = gtk_check_button_new ();
116 label_widget = gtk_label_new (label);
117 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
119 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
120 gtk_widget_show (label_widget);
125 /* This should only be called when toggle_button->draw_indicator
129 gtk_check_button_paint (GtkWidget *widget,
132 GtkCheckButton *check_button;
134 g_return_if_fail (widget != NULL);
135 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
137 check_button = GTK_CHECK_BUTTON (widget);
139 if (GTK_WIDGET_DRAWABLE (widget))
143 gtk_check_button_draw_indicator (check_button, area);
145 border_width = GTK_CONTAINER (widget)->border_width;
146 if (GTK_WIDGET_HAS_FOCUS (widget))
147 gtk_paint_focus (widget->style, widget->window,
148 NULL, widget, "checkbutton",
149 border_width + widget->allocation.x,
150 border_width + widget->allocation.y,
151 widget->allocation.width - 2 * border_width - 1,
152 widget->allocation.height - 2 * border_width - 1);
157 gtk_check_button_draw (GtkWidget *widget,
160 GtkCheckButton *check_button;
161 GtkToggleButton *toggle_button;
163 GdkRectangle child_area;
165 g_return_if_fail (widget != NULL);
166 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
167 g_return_if_fail (area != NULL);
169 check_button = GTK_CHECK_BUTTON (widget);
170 toggle_button = GTK_TOGGLE_BUTTON (widget);
171 bin = GTK_BIN (widget);
173 if (GTK_WIDGET_DRAWABLE (widget))
175 if (toggle_button->draw_indicator)
177 gtk_check_button_paint (widget, area);
179 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
180 gtk_widget_draw (bin->child, &child_area);
184 if (GTK_WIDGET_CLASS (parent_class)->draw)
185 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
191 gtk_check_button_draw_focus (GtkWidget *widget)
195 g_return_if_fail (widget != NULL);
196 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
198 border_width = GTK_CONTAINER (widget)->border_width;
199 gtk_widget_queue_clear_area (widget->parent,
200 border_width + widget->allocation.x,
201 border_width + widget->allocation.y,
202 widget->allocation.width - 2 * border_width,
203 widget->allocation.height - 2 * border_width);
207 gtk_check_button_size_request (GtkWidget *widget,
208 GtkRequisition *requisition)
210 GtkToggleButton *toggle_button;
213 g_return_if_fail (widget != NULL);
214 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
215 g_return_if_fail (requisition != NULL);
217 toggle_button = GTK_TOGGLE_BUTTON (widget);
219 if (GTK_WIDGET_CLASS (parent_class)->size_request)
220 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
222 if (toggle_button->draw_indicator)
224 requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
225 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
227 temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
228 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
229 requisition->height = MAX (requisition->height, temp) + 2;
234 gtk_check_button_size_allocate (GtkWidget *widget,
235 GtkAllocation *allocation)
237 GtkCheckButton *check_button;
238 GtkToggleButton *toggle_button;
240 GtkAllocation child_allocation;
242 g_return_if_fail (widget != NULL);
243 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
244 g_return_if_fail (allocation != NULL);
246 check_button = GTK_CHECK_BUTTON (widget);
247 toggle_button = GTK_TOGGLE_BUTTON (widget);
249 if (toggle_button->draw_indicator)
251 widget->allocation = *allocation;
252 if (GTK_WIDGET_REALIZED (widget))
253 gdk_window_move_resize (toggle_button->event_window,
254 allocation->x, allocation->y,
255 allocation->width, allocation->height);
257 button = GTK_BUTTON (widget);
259 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
261 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
262 CHECK_BUTTON_CLASS (widget)->indicator_size +
263 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1 +
264 widget->allocation.x);
265 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1 +
266 widget->allocation.y;
267 child_allocation.width = MAX (1, allocation->width -
268 (GTK_CONTAINER (widget)->border_width +
269 CHECK_BUTTON_CLASS (widget)->indicator_size +
270 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1) -
271 GTK_CONTAINER (widget)->border_width - 1);
272 child_allocation.height = MAX (1, allocation->height - (GTK_CONTAINER (widget)->border_width + 1) * 2);
274 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
279 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
280 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
285 gtk_check_button_expose (GtkWidget *widget,
286 GdkEventExpose *event)
288 GtkCheckButton *check_button;
289 GtkToggleButton *toggle_button;
291 GdkEventExpose child_event;
293 g_return_val_if_fail (widget != NULL, FALSE);
294 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
295 g_return_val_if_fail (event != NULL, FALSE);
297 check_button = GTK_CHECK_BUTTON (widget);
298 toggle_button = GTK_TOGGLE_BUTTON (widget);
299 bin = GTK_BIN (widget);
301 if (GTK_WIDGET_DRAWABLE (widget))
303 if (toggle_button->draw_indicator)
305 gtk_check_button_paint (widget, &event->area);
307 child_event = *event;
308 if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) &&
309 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
310 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
314 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
315 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
324 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
327 GtkCheckButtonClass *class;
329 g_return_if_fail (check_button != NULL);
330 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
332 class = CHECK_BUTTON_CLASS (check_button);
334 if (class->draw_indicator)
335 (* class->draw_indicator) (check_button, area);
339 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
343 GtkToggleButton *toggle_button;
344 GtkStateType state_type;
345 GtkShadowType shadow_type;
346 GdkRectangle restrict_area;
347 GdkRectangle new_area;
352 g_return_if_fail (check_button != NULL);
353 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
355 widget = GTK_WIDGET (check_button);
356 toggle_button = GTK_TOGGLE_BUTTON (check_button);
358 if (GTK_WIDGET_DRAWABLE (check_button))
360 window = widget->window;
362 state_type = GTK_WIDGET_STATE (widget);
363 if (state_type != GTK_STATE_NORMAL &&
364 state_type != GTK_STATE_PRELIGHT)
365 state_type = GTK_STATE_NORMAL;
367 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
368 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
369 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
370 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
372 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
374 if (state_type != GTK_STATE_NORMAL)
375 gtk_paint_flat_box (widget->style, window, state_type,
376 GTK_SHADOW_ETCHED_OUT,
377 area, widget, "checkbutton",
378 new_area.x, new_area.y,
379 new_area.width, new_area.height);
382 x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
383 y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
384 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
385 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
387 if (GTK_TOGGLE_BUTTON (widget)->active)
389 state_type = GTK_STATE_ACTIVE;
390 shadow_type = GTK_SHADOW_IN;
394 shadow_type = GTK_SHADOW_OUT;
395 state_type = GTK_WIDGET_STATE (widget);
398 gtk_paint_check (widget->style, window,
399 state_type, shadow_type,
400 area, widget, "checkbutton",
401 x + 1, y + 1, width, height);