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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include "gtkcheckbutton.h"
22 #define INDICATOR_SIZE 10
23 #define INDICATOR_SPACING 2
25 #define CHECK_BUTTON_CLASS(w) GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
28 static void gtk_check_button_class_init (GtkCheckButtonClass *klass);
29 static void gtk_check_button_init (GtkCheckButton *check_button);
30 static void gtk_check_button_draw (GtkWidget *widget,
32 static void gtk_check_button_draw_focus (GtkWidget *widget);
33 static void gtk_check_button_size_request (GtkWidget *widget,
34 GtkRequisition *requisition);
35 static void gtk_check_button_size_allocate (GtkWidget *widget,
36 GtkAllocation *allocation);
37 static gint gtk_check_button_expose (GtkWidget *widget,
38 GdkEventExpose *event);
39 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
41 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
45 static GtkToggleButtonClass *parent_class = NULL;
49 gtk_check_button_get_type ()
51 static guint check_button_type = 0;
53 if (!check_button_type)
55 GtkTypeInfo check_button_info =
58 sizeof (GtkCheckButton),
59 sizeof (GtkCheckButtonClass),
60 (GtkClassInitFunc) gtk_check_button_class_init,
61 (GtkObjectInitFunc) gtk_check_button_init,
66 check_button_type = gtk_type_unique (gtk_toggle_button_get_type (), &check_button_info);
69 return check_button_type;
73 gtk_check_button_class_init (GtkCheckButtonClass *class)
75 GtkWidgetClass *widget_class;
77 widget_class = (GtkWidgetClass*) class;
78 parent_class = gtk_type_class (gtk_toggle_button_get_type ());
80 widget_class->draw = gtk_check_button_draw;
81 widget_class->draw_focus = gtk_check_button_draw_focus;
82 widget_class->size_request = gtk_check_button_size_request;
83 widget_class->size_allocate = gtk_check_button_size_allocate;
84 widget_class->expose_event = gtk_check_button_expose;
86 class->indicator_size = INDICATOR_SIZE;
87 class->indicator_spacing = INDICATOR_SPACING;
88 class->draw_indicator = gtk_real_check_button_draw_indicator;
92 gtk_check_button_init (GtkCheckButton *check_button)
94 check_button->toggle_button.draw_indicator = TRUE;
98 gtk_check_button_new ()
100 return GTK_WIDGET (gtk_type_new (gtk_check_button_get_type ()));
105 gtk_check_button_new_with_label (const gchar *label)
107 GtkWidget *check_button;
108 GtkWidget *label_widget;
110 check_button = gtk_check_button_new ();
111 label_widget = gtk_label_new (label);
112 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
114 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
115 gtk_widget_show (label_widget);
121 gtk_check_button_draw (GtkWidget *widget,
125 GtkCheckButton *check_button;
126 GdkRectangle child_area;
128 g_return_if_fail (widget != NULL);
129 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
130 g_return_if_fail (area != NULL);
132 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
134 check_button = GTK_CHECK_BUTTON (widget);
136 if (check_button->toggle_button.draw_indicator)
138 button = GTK_BUTTON (widget);
140 gtk_check_button_draw_indicator (check_button, area);
142 if (button->child && GTK_WIDGET_NO_WINDOW (button->child) &&
143 gtk_widget_intersect (button->child, area, &child_area))
144 gtk_widget_draw (button->child, &child_area);
146 gtk_widget_draw_focus (widget);
150 if (GTK_WIDGET_CLASS (parent_class)->draw)
151 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
157 gtk_check_button_draw_focus (GtkWidget *widget)
159 GtkCheckButton *check_button;
161 g_return_if_fail (widget != NULL);
162 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
164 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
166 check_button = GTK_CHECK_BUTTON (widget);
167 if (check_button->toggle_button.draw_indicator)
169 if (GTK_WIDGET_HAS_FOCUS (widget))
170 gdk_draw_rectangle (widget->window,
171 widget->style->black_gc, FALSE, 0, 0,
172 widget->allocation.width - 1,
173 widget->allocation.height - 1);
175 gdk_draw_rectangle (widget->window,
176 widget->style->bg_gc[GTK_STATE_NORMAL], FALSE, 0, 0,
177 widget->allocation.width - 1,
178 widget->allocation.height - 1);
182 if (GTK_WIDGET_CLASS (parent_class)->draw_focus)
183 (* GTK_WIDGET_CLASS (parent_class)->draw_focus) (widget);
189 gtk_check_button_size_request (GtkWidget *widget,
190 GtkRequisition *requisition)
192 GtkCheckButton *check_button;
196 g_return_if_fail (widget != NULL);
197 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
198 g_return_if_fail (requisition != NULL);
200 check_button = GTK_CHECK_BUTTON (widget);
202 if (GTK_WIDGET_CLASS (parent_class)->size_request)
203 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
205 if (check_button->toggle_button.draw_indicator)
207 button = GTK_BUTTON (widget);
209 requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
210 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
212 temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
213 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
214 requisition->height = MAX (requisition->height, temp) + 2;
219 gtk_check_button_size_allocate (GtkWidget *widget,
220 GtkAllocation *allocation)
222 GtkCheckButton *check_button;
224 GtkAllocation child_allocation;
226 g_return_if_fail (widget != NULL);
227 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
228 g_return_if_fail (allocation != NULL);
230 check_button = GTK_CHECK_BUTTON (widget);
231 if (check_button->toggle_button.draw_indicator)
233 widget->allocation = *allocation;
234 if (GTK_WIDGET_REALIZED (widget))
235 gdk_window_move_resize (widget->window,
236 allocation->x, allocation->y,
237 allocation->width, allocation->height);
239 button = GTK_BUTTON (widget);
241 if (button->child && GTK_WIDGET_VISIBLE (button->child))
243 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
244 CHECK_BUTTON_CLASS (widget)->indicator_size +
245 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1);
246 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1;
247 child_allocation.width = (allocation->width - child_allocation.x -
248 GTK_CONTAINER (widget)->border_width - 1);
249 child_allocation.height = allocation->height - child_allocation.y * 2;
251 gtk_widget_size_allocate (button->child, &child_allocation);
256 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
257 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
262 gtk_check_button_expose (GtkWidget *widget,
263 GdkEventExpose *event)
266 GtkCheckButton *check_button;
267 GdkEventExpose child_event;
269 g_return_val_if_fail (widget != NULL, FALSE);
270 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
271 g_return_val_if_fail (event != NULL, FALSE);
273 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
275 check_button = GTK_CHECK_BUTTON (widget);
277 if (check_button->toggle_button.draw_indicator)
279 button = GTK_BUTTON (widget);
281 gtk_check_button_draw_indicator (check_button, &event->area);
283 child_event = *event;
284 if (button->child && GTK_WIDGET_NO_WINDOW (button->child) &&
285 gtk_widget_intersect (button->child, &event->area, &child_event.area))
286 gtk_widget_event (button->child, (GdkEvent*) &child_event);
288 gtk_widget_draw_focus (widget);
292 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
293 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
302 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
305 GtkCheckButtonClass *class;
307 g_return_if_fail (check_button != NULL);
308 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
309 g_return_if_fail (CHECK_BUTTON_CLASS (check_button) != NULL);
311 class = CHECK_BUTTON_CLASS (check_button);
313 if (class->draw_indicator)
314 (* class->draw_indicator) (check_button, area);
318 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
322 GtkToggleButton *toggle_button;
323 GtkStateType state_type;
324 GtkShadowType shadow_type;
328 g_return_if_fail (check_button != NULL);
329 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
331 if (GTK_WIDGET_DRAWABLE (check_button))
333 widget = GTK_WIDGET (check_button);
334 toggle_button = GTK_TOGGLE_BUTTON (check_button);
336 state_type = GTK_WIDGET_STATE (widget);
337 if ((state_type != GTK_STATE_NORMAL) &&
338 (state_type != GTK_STATE_PRELIGHT))
339 state_type = GTK_STATE_NORMAL;
341 gtk_style_set_background (widget->style, widget->window, state_type);
342 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
344 x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
345 y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
346 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
347 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
349 if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
350 shadow_type = GTK_SHADOW_IN;
351 else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT) && toggle_button->active)
352 shadow_type = GTK_SHADOW_IN;
354 shadow_type = GTK_SHADOW_OUT;
356 gdk_draw_rectangle (widget->window,
357 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
358 TRUE, x + 1, y + 1, width, height);
359 gtk_draw_shadow (widget->style, widget->window,
360 GTK_WIDGET_STATE (widget), shadow_type,
361 x + 1, y + 1, width, height);