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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtkcheckbutton.h"
31 #define INDICATOR_SIZE 10
32 #define INDICATOR_SPACING 2
35 static void gtk_check_button_class_init (GtkCheckButtonClass *klass);
36 static void gtk_check_button_init (GtkCheckButton *check_button);
37 static void gtk_check_button_size_request (GtkWidget *widget,
38 GtkRequisition *requisition);
39 static void gtk_check_button_size_allocate (GtkWidget *widget,
40 GtkAllocation *allocation);
41 static gint gtk_check_button_expose (GtkWidget *widget,
42 GdkEventExpose *event);
43 static void gtk_check_button_paint (GtkWidget *widget,
45 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
47 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
50 static GtkToggleButtonClass *parent_class = NULL;
54 gtk_check_button_get_type (void)
56 static GtkType check_button_type = 0;
58 if (!check_button_type)
60 static const GtkTypeInfo check_button_info =
63 sizeof (GtkCheckButton),
64 sizeof (GtkCheckButtonClass),
65 (GtkClassInitFunc) gtk_check_button_class_init,
66 (GtkObjectInitFunc) gtk_check_button_init,
67 /* reserved_1 */ NULL,
68 /* reserved_2 */ NULL,
69 (GtkClassInitFunc) NULL,
72 check_button_type = gtk_type_unique (GTK_TYPE_TOGGLE_BUTTON, &check_button_info);
75 return check_button_type;
79 gtk_check_button_class_init (GtkCheckButtonClass *class)
81 GtkWidgetClass *widget_class;
83 widget_class = (GtkWidgetClass*) class;
84 parent_class = gtk_type_class (gtk_toggle_button_get_type ());
86 widget_class->size_request = gtk_check_button_size_request;
87 widget_class->size_allocate = gtk_check_button_size_allocate;
88 widget_class->expose_event = gtk_check_button_expose;
90 class->indicator_size = INDICATOR_SIZE;
91 class->indicator_spacing = INDICATOR_SPACING;
92 class->draw_indicator = gtk_real_check_button_draw_indicator;
96 gtk_check_button_init (GtkCheckButton *check_button)
98 GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
99 GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
100 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
104 gtk_check_button_new (void)
106 return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
111 gtk_check_button_new_with_label (const gchar *label)
113 GtkWidget *check_button;
114 GtkWidget *label_widget;
116 check_button = gtk_check_button_new ();
117 label_widget = gtk_label_new (label);
118 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
120 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
121 gtk_widget_show (label_widget);
126 /* This should only be called when toggle_button->draw_indicator
130 gtk_check_button_paint (GtkWidget *widget,
133 GtkCheckButton *check_button;
135 g_return_if_fail (widget != NULL);
136 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
138 check_button = GTK_CHECK_BUTTON (widget);
140 if (GTK_WIDGET_DRAWABLE (widget))
144 gtk_check_button_draw_indicator (check_button, area);
146 border_width = GTK_CONTAINER (widget)->border_width;
147 if (GTK_WIDGET_HAS_FOCUS (widget))
148 gtk_paint_focus (widget->style, widget->window,
149 NULL, widget, "checkbutton",
150 border_width + widget->allocation.x,
151 border_width + widget->allocation.y,
152 widget->allocation.width - 2 * border_width - 1,
153 widget->allocation.height - 2 * border_width - 1);
158 gtk_check_button_size_request (GtkWidget *widget,
159 GtkRequisition *requisition)
161 GtkToggleButton *toggle_button;
164 g_return_if_fail (widget != NULL);
165 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
166 g_return_if_fail (requisition != NULL);
168 toggle_button = GTK_TOGGLE_BUTTON (widget);
170 if (GTK_WIDGET_CLASS (parent_class)->size_request)
171 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
173 if (toggle_button->draw_indicator)
175 requisition->width += (GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
176 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 3 + 2);
178 temp = (GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
179 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 2);
180 requisition->height = MAX (requisition->height, temp) + 2;
185 gtk_check_button_size_allocate (GtkWidget *widget,
186 GtkAllocation *allocation)
188 GtkCheckButton *check_button;
189 GtkToggleButton *toggle_button;
191 GtkAllocation child_allocation;
193 g_return_if_fail (widget != NULL);
194 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
195 g_return_if_fail (allocation != NULL);
197 check_button = GTK_CHECK_BUTTON (widget);
198 toggle_button = GTK_TOGGLE_BUTTON (widget);
200 if (toggle_button->draw_indicator)
202 widget->allocation = *allocation;
203 if (GTK_WIDGET_REALIZED (widget))
204 gdk_window_move_resize (toggle_button->event_window,
205 allocation->x, allocation->y,
206 allocation->width, allocation->height);
208 button = GTK_BUTTON (widget);
210 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
212 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
213 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
214 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 3 + 1 +
215 widget->allocation.x);
216 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1 +
217 widget->allocation.y;
218 child_allocation.width = MAX (1, allocation->width -
219 (GTK_CONTAINER (widget)->border_width +
220 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
221 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 3 + 1) -
222 GTK_CONTAINER (widget)->border_width - 1);
223 child_allocation.height = MAX (1, allocation->height - (GTK_CONTAINER (widget)->border_width + 1) * 2);
225 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
226 child_allocation.x = allocation->x + allocation->width
227 - (child_allocation.x - allocation->x + child_allocation.width);
229 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
234 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
235 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
240 gtk_check_button_expose (GtkWidget *widget,
241 GdkEventExpose *event)
243 GtkCheckButton *check_button;
244 GtkToggleButton *toggle_button;
247 g_return_val_if_fail (widget != NULL, FALSE);
248 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
249 g_return_val_if_fail (event != NULL, FALSE);
251 check_button = GTK_CHECK_BUTTON (widget);
252 toggle_button = GTK_TOGGLE_BUTTON (widget);
253 bin = GTK_BIN (widget);
255 if (GTK_WIDGET_DRAWABLE (widget))
257 if (toggle_button->draw_indicator)
259 gtk_check_button_paint (widget, &event->area);
262 gtk_container_propagate_expose (GTK_CONTAINER (widget),
266 else if (GTK_WIDGET_CLASS (parent_class)->expose_event)
267 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
275 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
278 GtkCheckButtonClass *class;
280 g_return_if_fail (check_button != NULL);
281 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
283 class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
285 if (class->draw_indicator)
286 (* class->draw_indicator) (check_button, area);
290 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
294 GtkToggleButton *toggle_button;
295 GtkStateType state_type;
296 GtkShadowType shadow_type;
297 GdkRectangle restrict_area;
298 GdkRectangle new_area;
303 g_return_if_fail (check_button != NULL);
304 g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
306 widget = GTK_WIDGET (check_button);
307 toggle_button = GTK_TOGGLE_BUTTON (check_button);
309 if (GTK_WIDGET_DRAWABLE (check_button))
311 window = widget->window;
313 state_type = GTK_WIDGET_STATE (widget);
314 if (state_type != GTK_STATE_NORMAL &&
315 state_type != GTK_STATE_PRELIGHT)
316 state_type = GTK_STATE_NORMAL;
318 restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
319 restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
320 restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
321 restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
323 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
325 if (state_type != GTK_STATE_NORMAL)
326 gtk_paint_flat_box (widget->style, window, state_type,
327 GTK_SHADOW_ETCHED_OUT,
328 area, widget, "checkbutton",
329 new_area.x, new_area.y,
330 new_area.width, new_area.height);
333 x = widget->allocation.x + GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
334 y = widget->allocation.y + (widget->allocation.height - GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size) / 2;
335 width = GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size;
336 height = GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size;
338 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
340 state_type = GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE ? GTK_STATE_NORMAL : GTK_WIDGET_STATE (widget);
341 shadow_type = GTK_SHADOW_ETCHED_IN;
343 else if (GTK_TOGGLE_BUTTON (widget)->active)
345 state_type = GTK_STATE_ACTIVE;
346 shadow_type = GTK_SHADOW_IN;
350 shadow_type = GTK_SHADOW_OUT;
351 state_type = GTK_WIDGET_STATE (widget);
354 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
355 x = widget->allocation.x + widget->allocation.width - (width + x - widget->allocation.x);
357 gtk_paint_check (widget->style, window,
358 state_type, shadow_type,
359 area, widget, "checkbutton",
360 x, y, width, height);