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_DRAWABLE (widget))
166 check_button = GTK_CHECK_BUTTON (widget);
167 if (check_button->toggle_button.draw_indicator)
171 border_width = GTK_CONTAINER (widget)->border_width;
172 if (GTK_WIDGET_HAS_FOCUS (widget))
173 gdk_draw_rectangle (widget->window,
174 widget->style->black_gc, FALSE,
175 border_width, border_width,
176 widget->allocation.width - 2 * border_width - 1,
177 widget->allocation.height - 2 * border_width - 1);
179 gdk_draw_rectangle (widget->window,
180 widget->style->bg_gc[GTK_STATE_NORMAL], FALSE,
181 border_width, border_width,
182 widget->allocation.width - 2 * border_width - 1,
183 widget->allocation.height - 2 * border_width - 1);
187 if (GTK_WIDGET_CLASS (parent_class)->draw_focus)
188 (* GTK_WIDGET_CLASS (parent_class)->draw_focus) (widget);
194 gtk_check_button_size_request (GtkWidget *widget,
195 GtkRequisition *requisition)
197 GtkCheckButton *check_button;
201 g_return_if_fail (widget != NULL);
202 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
203 g_return_if_fail (requisition != NULL);
205 check_button = GTK_CHECK_BUTTON (widget);
207 if (GTK_WIDGET_CLASS (parent_class)->size_request)
208 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
210 if (check_button->toggle_button.draw_indicator)
212 button = GTK_BUTTON (widget);
214 requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
215 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
217 temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
218 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
219 requisition->height = MAX (requisition->height, temp) + 2;
224 gtk_check_button_size_allocate (GtkWidget *widget,
225 GtkAllocation *allocation)
227 GtkCheckButton *check_button;
229 GtkAllocation child_allocation;
231 g_return_if_fail (widget != NULL);
232 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
233 g_return_if_fail (allocation != NULL);
235 check_button = GTK_CHECK_BUTTON (widget);
236 if (check_button->toggle_button.draw_indicator)
238 widget->allocation = *allocation;
239 if (GTK_WIDGET_REALIZED (widget))
240 gdk_window_move_resize (widget->window,
241 allocation->x, allocation->y,
242 allocation->width, allocation->height);
244 button = GTK_BUTTON (widget);
246 if (button->child && GTK_WIDGET_VISIBLE (button->child))
248 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
249 CHECK_BUTTON_CLASS (widget)->indicator_size +
250 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1);
251 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1;
252 child_allocation.width = (allocation->width - child_allocation.x -
253 GTK_CONTAINER (widget)->border_width - 1);
254 child_allocation.height = allocation->height - child_allocation.y * 2;
256 gtk_widget_size_allocate (button->child, &child_allocation);
261 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
262 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
267 gtk_check_button_expose (GtkWidget *widget,
268 GdkEventExpose *event)
271 GtkCheckButton *check_button;
272 GdkEventExpose child_event;
274 g_return_val_if_fail (widget != NULL, FALSE);
275 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
276 g_return_val_if_fail (event != NULL, FALSE);
278 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
280 check_button = GTK_CHECK_BUTTON (widget);
282 if (check_button->toggle_button.draw_indicator)
284 button = GTK_BUTTON (widget);
286 gtk_check_button_draw_indicator (check_button, &event->area);
288 child_event = *event;
289 if (button->child && GTK_WIDGET_NO_WINDOW (button->child) &&
290 gtk_widget_intersect (button->child, &event->area, &child_event.area))
291 gtk_widget_event (button->child, (GdkEvent*) &child_event);
293 gtk_widget_draw_focus (widget);
297 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
298 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
307 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
310 GtkCheckButtonClass *class;
312 g_return_if_fail (check_button != NULL);
313 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
314 g_return_if_fail (CHECK_BUTTON_CLASS (check_button) != NULL);
316 class = CHECK_BUTTON_CLASS (check_button);
318 if (class->draw_indicator)
319 (* class->draw_indicator) (check_button, area);
323 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
327 GtkToggleButton *toggle_button;
328 GtkStateType state_type;
329 GtkShadowType shadow_type;
330 GdkRectangle restrict_area;
331 GdkRectangle new_area;
335 g_return_if_fail (check_button != NULL);
336 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
338 if (GTK_WIDGET_DRAWABLE (check_button))
340 widget = GTK_WIDGET (check_button);
341 toggle_button = GTK_TOGGLE_BUTTON (check_button);
343 state_type = GTK_WIDGET_STATE (widget);
344 if ((state_type != GTK_STATE_NORMAL) &&
345 (state_type != GTK_STATE_PRELIGHT))
346 state_type = GTK_STATE_NORMAL;
348 restrict_area.x = GTK_CONTAINER (widget)->border_width;
349 restrict_area.y = restrict_area.x;
350 restrict_area.width = widget->allocation.width - restrict_area.x * 2;
351 restrict_area.height = widget->allocation.height - restrict_area.x * 2;
353 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
355 gtk_style_set_background (widget->style, widget->window, state_type);
356 gdk_window_clear_area (widget->window, new_area.x, new_area.y,
357 new_area.width, new_area.height);
360 x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
361 y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
362 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
363 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
365 if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
366 shadow_type = GTK_SHADOW_IN;
367 else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT) && toggle_button->active)
368 shadow_type = GTK_SHADOW_IN;
370 shadow_type = GTK_SHADOW_OUT;
372 gdk_draw_rectangle (widget->window,
373 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
374 TRUE, x + 1, y + 1, width, height);
375 gtk_draw_shadow (widget->style, widget->window,
376 GTK_WIDGET_STATE (widget), shadow_type,
377 x + 1, y + 1, width, height);