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,
46 static GtkToggleButtonClass *parent_class = NULL;
50 gtk_check_button_get_type (void)
52 static GtkType check_button_type = 0;
54 if (!check_button_type)
56 GtkTypeInfo check_button_info =
59 sizeof (GtkCheckButton),
60 sizeof (GtkCheckButtonClass),
61 (GtkClassInitFunc) gtk_check_button_class_init,
62 (GtkObjectInitFunc) gtk_check_button_init,
67 check_button_type = gtk_type_unique (gtk_toggle_button_get_type (), &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 check_button->toggle_button.draw_indicator = TRUE;
99 gtk_check_button_new (void)
101 return GTK_WIDGET (gtk_type_new (gtk_check_button_get_type ()));
106 gtk_check_button_new_with_label (const gchar *label)
108 GtkWidget *check_button;
109 GtkWidget *label_widget;
111 check_button = gtk_check_button_new ();
112 label_widget = gtk_label_new (label);
113 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
115 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
116 gtk_widget_show (label_widget);
122 gtk_check_button_draw (GtkWidget *widget,
126 GtkCheckButton *check_button;
127 GdkRectangle child_area;
129 g_return_if_fail (widget != NULL);
130 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
131 g_return_if_fail (area != NULL);
133 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
135 check_button = GTK_CHECK_BUTTON (widget);
137 if (check_button->toggle_button.draw_indicator)
139 button = GTK_BUTTON (widget);
141 gtk_check_button_draw_indicator (check_button, area);
143 if (button->child && GTK_WIDGET_NO_WINDOW (button->child) &&
144 gtk_widget_intersect (button->child, area, &child_area))
145 gtk_widget_draw (button->child, &child_area);
147 gtk_widget_draw_focus (widget);
151 if (GTK_WIDGET_CLASS (parent_class)->draw)
152 (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
158 gtk_check_button_draw_focus (GtkWidget *widget)
160 GtkCheckButton *check_button;
162 g_return_if_fail (widget != NULL);
163 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
165 if (GTK_WIDGET_DRAWABLE (widget))
167 check_button = GTK_CHECK_BUTTON (widget);
168 if (check_button->toggle_button.draw_indicator)
172 border_width = GTK_CONTAINER (widget)->border_width;
173 if (GTK_WIDGET_HAS_FOCUS (widget))
174 gdk_draw_rectangle (widget->window,
175 widget->style->black_gc, FALSE,
176 border_width, border_width,
177 widget->allocation.width - 2 * border_width - 1,
178 widget->allocation.height - 2 * border_width - 1);
180 gdk_draw_rectangle (widget->window,
181 widget->style->bg_gc[GTK_STATE_NORMAL], FALSE,
182 border_width, border_width,
183 widget->allocation.width - 2 * border_width - 1,
184 widget->allocation.height - 2 * border_width - 1);
188 if (GTK_WIDGET_CLASS (parent_class)->draw_focus)
189 (* GTK_WIDGET_CLASS (parent_class)->draw_focus) (widget);
195 gtk_check_button_size_request (GtkWidget *widget,
196 GtkRequisition *requisition)
198 GtkCheckButton *check_button;
202 g_return_if_fail (widget != NULL);
203 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
204 g_return_if_fail (requisition != NULL);
206 check_button = GTK_CHECK_BUTTON (widget);
208 if (GTK_WIDGET_CLASS (parent_class)->size_request)
209 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
211 if (check_button->toggle_button.draw_indicator)
213 button = GTK_BUTTON (widget);
215 requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
216 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
218 temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
219 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
220 requisition->height = MAX (requisition->height, temp) + 2;
225 gtk_check_button_size_allocate (GtkWidget *widget,
226 GtkAllocation *allocation)
228 GtkCheckButton *check_button;
230 GtkAllocation child_allocation;
232 g_return_if_fail (widget != NULL);
233 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
234 g_return_if_fail (allocation != NULL);
236 check_button = GTK_CHECK_BUTTON (widget);
237 if (check_button->toggle_button.draw_indicator)
239 widget->allocation = *allocation;
240 if (GTK_WIDGET_REALIZED (widget))
241 gdk_window_move_resize (widget->window,
242 allocation->x, allocation->y,
243 allocation->width, allocation->height);
245 button = GTK_BUTTON (widget);
247 if (button->child && GTK_WIDGET_VISIBLE (button->child))
249 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
250 CHECK_BUTTON_CLASS (widget)->indicator_size +
251 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1);
252 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1;
253 child_allocation.width = MAX (1, allocation->width - child_allocation.x -
254 GTK_CONTAINER (widget)->border_width - 1);
255 child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2);
257 gtk_widget_size_allocate (button->child, &child_allocation);
262 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
263 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
268 gtk_check_button_expose (GtkWidget *widget,
269 GdkEventExpose *event)
272 GtkCheckButton *check_button;
273 GdkEventExpose child_event;
275 g_return_val_if_fail (widget != NULL, FALSE);
276 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
277 g_return_val_if_fail (event != NULL, FALSE);
279 if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
281 check_button = GTK_CHECK_BUTTON (widget);
283 if (check_button->toggle_button.draw_indicator)
285 button = GTK_BUTTON (widget);
287 gtk_check_button_draw_indicator (check_button, &event->area);
289 child_event = *event;
290 if (button->child && GTK_WIDGET_NO_WINDOW (button->child) &&
291 gtk_widget_intersect (button->child, &event->area, &child_event.area))
292 gtk_widget_event (button->child, (GdkEvent*) &child_event);
294 gtk_widget_draw_focus (widget);
298 if (GTK_WIDGET_CLASS (parent_class)->expose_event)
299 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
308 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
311 GtkCheckButtonClass *class;
313 g_return_if_fail (check_button != NULL);
314 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
315 g_return_if_fail (CHECK_BUTTON_CLASS (check_button) != NULL);
317 class = CHECK_BUTTON_CLASS (check_button);
319 if (class->draw_indicator)
320 (* class->draw_indicator) (check_button, area);
324 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
328 GtkToggleButton *toggle_button;
329 GtkStateType state_type;
330 GtkShadowType shadow_type;
331 GdkRectangle restrict_area;
332 GdkRectangle new_area;
336 g_return_if_fail (check_button != NULL);
337 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
339 if (GTK_WIDGET_DRAWABLE (check_button))
341 widget = GTK_WIDGET (check_button);
342 toggle_button = GTK_TOGGLE_BUTTON (check_button);
344 state_type = GTK_WIDGET_STATE (widget);
345 if ((state_type != GTK_STATE_NORMAL) &&
346 (state_type != GTK_STATE_PRELIGHT))
347 state_type = GTK_STATE_NORMAL;
349 restrict_area.x = GTK_CONTAINER (widget)->border_width;
350 restrict_area.y = restrict_area.x;
351 restrict_area.width = widget->allocation.width - restrict_area.x * 2;
352 restrict_area.height = widget->allocation.height - restrict_area.x * 2;
354 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
356 gtk_style_set_background (widget->style, widget->window, state_type);
357 gdk_window_clear_area (widget->window, new_area.x, new_area.y,
358 new_area.width, new_area.height);
361 x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
362 y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
363 width = CHECK_BUTTON_CLASS (widget)->indicator_size;
364 height = CHECK_BUTTON_CLASS (widget)->indicator_size;
366 if (GTK_TOGGLE_BUTTON (widget)->active)
367 shadow_type = GTK_SHADOW_IN;
369 shadow_type = GTK_SHADOW_OUT;
371 gdk_draw_rectangle (widget->window,
372 widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
373 TRUE, x + 1, y + 1, width, height);
374 gtk_draw_shadow (widget->style, widget->window,
375 GTK_WIDGET_STATE (widget), shadow_type,
376 x + 1, y + 1, width, height);