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);
127 * gtk_check_button_new_with_mnemonic:
128 * @label: The text of the button, with an underscore in front of the
130 * @returns: a new #GtkCheckButton
132 * Creates a new #GtkCheckButton containing a label.
133 * If characters in @label are preceded by an underscore, they are underlined
134 * indicating that they represent a keyboard accelerator called a mnemonic.
135 * Pressing Alt and that key activates the checkbutton.
138 gtk_check_button_new_with_mnemonic (const gchar *label)
140 GtkWidget *check_button;
141 GtkWidget *label_widget;
143 check_button = gtk_check_button_new ();
144 label_widget = gtk_label_new_with_mnemonic (label);
145 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
146 gtk_label_set_mnemonic_widget (GTK_LABEL (label_widget), check_button);
148 gtk_container_add (GTK_CONTAINER (check_button), label_widget);
149 gtk_widget_show (label_widget);
155 /* This should only be called when toggle_button->draw_indicator
159 gtk_check_button_paint (GtkWidget *widget,
162 GtkCheckButton *check_button;
164 g_return_if_fail (widget != NULL);
165 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
167 check_button = GTK_CHECK_BUTTON (widget);
169 if (GTK_WIDGET_DRAWABLE (widget))
173 gtk_check_button_draw_indicator (check_button, area);
175 border_width = GTK_CONTAINER (widget)->border_width;
176 if (GTK_WIDGET_HAS_FOCUS (widget))
177 gtk_paint_focus (widget->style, widget->window,
178 NULL, widget, "checkbutton",
179 border_width + widget->allocation.x,
180 border_width + widget->allocation.y,
181 widget->allocation.width - 2 * border_width - 1,
182 widget->allocation.height - 2 * border_width - 1);
187 gtk_check_button_size_request (GtkWidget *widget,
188 GtkRequisition *requisition)
190 GtkToggleButton *toggle_button;
193 g_return_if_fail (widget != NULL);
194 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
195 g_return_if_fail (requisition != NULL);
197 toggle_button = GTK_TOGGLE_BUTTON (widget);
199 if (GTK_WIDGET_CLASS (parent_class)->size_request)
200 (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
202 if (toggle_button->draw_indicator)
204 requisition->width += (GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
205 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 3 + 2);
207 temp = (GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
208 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 2);
209 requisition->height = MAX (requisition->height, temp) + 2;
214 gtk_check_button_size_allocate (GtkWidget *widget,
215 GtkAllocation *allocation)
217 GtkCheckButton *check_button;
218 GtkToggleButton *toggle_button;
220 GtkAllocation child_allocation;
222 g_return_if_fail (widget != NULL);
223 g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
224 g_return_if_fail (allocation != NULL);
226 check_button = GTK_CHECK_BUTTON (widget);
227 toggle_button = GTK_TOGGLE_BUTTON (widget);
229 if (toggle_button->draw_indicator)
231 widget->allocation = *allocation;
232 if (GTK_WIDGET_REALIZED (widget))
233 gdk_window_move_resize (toggle_button->event_window,
234 allocation->x, allocation->y,
235 allocation->width, allocation->height);
237 button = GTK_BUTTON (widget);
239 if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
241 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
242 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
243 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 3 + 1 +
244 widget->allocation.x);
245 child_allocation.y = GTK_CONTAINER (widget)->border_width + 1 +
246 widget->allocation.y;
247 child_allocation.width = MAX (1, allocation->width -
248 (GTK_CONTAINER (widget)->border_width +
249 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size +
250 GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing * 3 + 1) -
251 GTK_CONTAINER (widget)->border_width - 1);
252 child_allocation.height = MAX (1, allocation->height - (GTK_CONTAINER (widget)->border_width + 1) * 2);
254 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
255 child_allocation.x = allocation->x + allocation->width
256 - (child_allocation.x - allocation->x + child_allocation.width);
258 gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
263 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
264 (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
269 gtk_check_button_expose (GtkWidget *widget,
270 GdkEventExpose *event)
272 GtkCheckButton *check_button;
273 GtkToggleButton *toggle_button;
276 g_return_val_if_fail (widget != NULL, FALSE);
277 g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
278 g_return_val_if_fail (event != NULL, FALSE);
280 check_button = GTK_CHECK_BUTTON (widget);
281 toggle_button = GTK_TOGGLE_BUTTON (widget);
282 bin = GTK_BIN (widget);
284 if (GTK_WIDGET_DRAWABLE (widget))
286 if (toggle_button->draw_indicator)
288 gtk_check_button_paint (widget, &event->area);
291 gtk_container_propagate_expose (GTK_CONTAINER (widget),
295 else if (GTK_WIDGET_CLASS (parent_class)->expose_event)
296 (* 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 = GTK_CHECK_BUTTON_GET_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 + GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
363 y = widget->allocation.y + (widget->allocation.height - GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size) / 2;
364 width = GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size;
365 height = GTK_CHECK_BUTTON_GET_CLASS (widget)->indicator_size;
367 if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
369 state_type = GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE ? GTK_STATE_NORMAL : GTK_WIDGET_STATE (widget);
370 shadow_type = GTK_SHADOW_ETCHED_IN;
372 else if (GTK_TOGGLE_BUTTON (widget)->active)
374 state_type = GTK_STATE_ACTIVE;
375 shadow_type = GTK_SHADOW_IN;
379 shadow_type = GTK_SHADOW_OUT;
380 state_type = GTK_WIDGET_STATE (widget);
383 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
384 x = widget->allocation.x + widget->allocation.width - (width + x - widget->allocation.x);
386 gtk_paint_check (widget->style, window,
387 state_type, shadow_type,
388 area, widget, "checkbutton",
389 x, y, width, height);