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_draw_indicator (GtkCheckButton *check_button,
42 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
45 static GtkToggleButtonClass *parent_class = NULL;
49 gtk_check_button_get_type (void)
51 static GtkType check_button_type = 0;
53 if (!check_button_type)
55 static const GtkTypeInfo check_button_info =
58 sizeof (GtkCheckButton),
59 sizeof (GtkCheckButtonClass),
60 (GtkClassInitFunc) gtk_check_button_class_init,
61 (GtkObjectInitFunc) gtk_check_button_init,
62 /* reserved_1 */ NULL,
63 /* reserved_2 */ NULL,
64 (GtkClassInitFunc) NULL,
67 check_button_type = gtk_type_unique (GTK_TYPE_TOGGLE_BUTTON, &check_button_info);
70 return check_button_type;
74 gtk_check_button_class_init (GtkCheckButtonClass *class)
76 GtkWidgetClass *widget_class;
78 widget_class = (GtkWidgetClass*) class;
79 parent_class = gtk_type_class (gtk_toggle_button_get_type ());
81 widget_class->draw = gtk_check_button_draw;
82 widget_class->draw_focus = gtk_check_button_draw_focus;
83 widget_class->size_request = gtk_check_button_size_request;
84 widget_class->size_allocate = gtk_check_button_size_allocate;
85 widget_class->expose_event = gtk_check_button_expose;
87 class->indicator_size = INDICATOR_SIZE;
88 class->indicator_spacing = INDICATOR_SPACING;
89 class->draw_indicator = gtk_real_check_button_draw_indicator;
93 gtk_check_button_init (GtkCheckButton *check_button)
95 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
96 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
100 gtk_check_button_new (void)
102 return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
107 gtk_check_button_new_with_label (const gchar *label)
109 GtkWidget *check_button;
110 GtkWidget *label_widget;
112 check_button = gtk_check_button_new ();
113 label_widget = gtk_label_new (label);
114 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
116 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
117 gtk_widget_show (label_widget);
123 gtk_check_button_draw (GtkWidget *widget,
126 GtkCheckButton *check_button;
127 GtkToggleButton *toggle_button;
129 GdkRectangle child_area;
131 g_return_if_fail (widget != NULL);
132 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
133 g_return_if_fail (area != NULL);
135 check_button = GTK_CHECK_BUTTON (widget);
136 toggle_button = GTK_TOGGLE_BUTTON (widget);
137 bin = GTK_BIN (widget);
139 if (GTK_WIDGET_DRAWABLE (widget))
141 if (toggle_button->draw_indicator)
145 gtk_check_button_draw_indicator (check_button, area);
147 border_width = GTK_CONTAINER (widget)->border_width;
148 if (GTK_WIDGET_HAS_FOCUS (widget))
149 gtk_paint_focus (widget->style, widget->window,
150 NULL, widget, "checkbutton",
151 border_width + widget->allocation.x,
152 border_width + widget->allocation.y,
153 widget->allocation.width - 2 * border_width - 1,
154 widget->allocation.height - 2 * border_width - 1);
156 if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) &&
157 gtk_widget_intersect (bin->child, area, &child_area))
158 gtk_widget_draw (bin->child, &child_area);
162 if (GTK_WIDGET_CLASS (parent_class)->draw)
163 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
169 gtk_check_button_draw_focus (GtkWidget *widget)
173 g_return_if_fail (widget != NULL);
174 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
176 border_width = GTK_CONTAINER (widget)->border_width;
177 gtk_widget_queue_clear_area (widget->parent,
178 border_width + widget->allocation.x,
179 border_width + widget->allocation.y,
180 widget->allocation.width - 2 * border_width,
181 widget->allocation.height - 2 * border_width);
185 gtk_check_button_size_request (GtkWidget *widget,
186 GtkRequisition *requisition)
188 GtkToggleButton *toggle_button;
191 g_return_if_fail (widget != NULL);
192 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
193 g_return_if_fail (requisition != NULL);
195 toggle_button = GTK_TOGGLE_BUTTON (widget);
197 if (GTK_WIDGET_CLASS (parent_class)->size_request)
198 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
200 if (toggle_button->draw_indicator)
202 requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
203 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
205 temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
206 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
207 requisition->height = MAX (requisition->height, temp) + 2;
212 gtk_check_button_size_allocate (GtkWidget *widget,
213 GtkAllocation *allocation)
215 GtkCheckButton *check_button;
216 GtkToggleButton *toggle_button;
218 GtkAllocation child_allocation;
220 g_return_if_fail (widget != NULL);
221 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
222 g_return_if_fail (allocation != NULL);
224 check_button = GTK_CHECK_BUTTON (widget);
225 toggle_button = GTK_TOGGLE_BUTTON (widget);
227 if (toggle_button->draw_indicator)
229 widget->allocation = *allocation;
230 if (GTK_WIDGET_REALIZED (widget))
231 gdk_window_move_resize (toggle_button->event_window,
232 allocation->x, allocation->y,
233 allocation->width, allocation->height);
235 button = GTK_BUTTON (widget);
237 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
239 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
240 CHECK_BUTTON_CLASS (widget)->indicator_size +
241 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1 +
242 widget->allocation.x);
243 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1 +
244 widget->allocation.y;
245 child_allocation.width = MAX (1, allocation->width -
246 (GTK_CONTAINER (widget)->border_width +
247 CHECK_BUTTON_CLASS (widget)->indicator_size +
248 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1) -
249 GTK_CONTAINER (widget)->border_width - 1);
250 child_allocation.height = MAX (1, allocation->height - (GTK_CONTAINER (widget)->border_width + 1) * 2);
252 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
257 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
258 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
263 gtk_check_button_expose (GtkWidget *widget,
264 GdkEventExpose *event)
266 GtkCheckButton *check_button;
267 GtkToggleButton *toggle_button;
269 GdkEventExpose child_event;
271 g_return_val_if_fail (widget != NULL, FALSE);
272 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
273 g_return_val_if_fail (event != NULL, FALSE);
275 check_button = GTK_CHECK_BUTTON (widget);
276 toggle_button = GTK_TOGGLE_BUTTON (widget);
277 bin = GTK_BIN (widget);
279 if (GTK_WIDGET_DRAWABLE (widget))
281 if (toggle_button->draw_indicator)
283 gtk_check_button_draw_indicator (check_button, &event->area);
285 child_event = *event;
286 if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) &&
287 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
288 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
290 gtk_widget_draw_focus (widget);
294 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
295 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
304 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
307 GtkCheckButtonClass *class;
309 g_return_if_fail (check_button != NULL);
310 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
312 class = CHECK_BUTTON_CLASS (check_button);
314 if (class->draw_indicator)
315 (* class->draw_indicator) (check_button, area);
319 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
323 GtkToggleButton *toggle_button;
324 GtkStateType state_type;
325 GtkShadowType shadow_type;
326 GdkRectangle restrict_area;
327 GdkRectangle new_area;
332 g_return_if_fail (check_button != NULL);
333 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
335 widget = GTK_WIDGET (check_button);
336 toggle_button = GTK_TOGGLE_BUTTON (check_button);
338 if (GTK_WIDGET_DRAWABLE (check_button))
340 window = widget->window;
342 state_type = GTK_WIDGET_STATE (widget);
343 if (state_type != GTK_STATE_NORMAL &&
344 state_type != GTK_STATE_PRELIGHT)
345 state_type = GTK_STATE_NORMAL;
347 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
348 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
349 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
350 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
352 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
354 if (state_type != GTK_STATE_NORMAL)
355 gtk_paint_flat_box (widget->style, window, state_type,
356 GTK_SHADOW_ETCHED_OUT,
357 area, widget, "checkbutton",
358 new_area.x, new_area.y,
359 new_area.width, new_area.height);
362 x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
363 y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
364 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
365 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
367 if (GTK_TOGGLE_BUTTON (widget)->active)
368 shadow_type = GTK_SHADOW_IN;
370 shadow_type = GTK_SHADOW_OUT;
372 gtk_paint_check (widget->style, window,
373 GTK_WIDGET_STATE (widget), shadow_type,
374 area, widget, "checkbutton",
375 x + 1, y + 1, width, height);