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/.
29 #include "gtkcheckbutton.h"
33 #include "gtkprivate.h"
37 #define INDICATOR_SIZE 13
38 #define INDICATOR_SPACING 2
41 static void gtk_check_button_size_request (GtkWidget *widget,
42 GtkRequisition *requisition);
43 static void gtk_check_button_size_allocate (GtkWidget *widget,
44 GtkAllocation *allocation);
45 static gboolean gtk_check_button_draw (GtkWidget *widget,
47 static void gtk_check_button_paint (GtkWidget *widget,
49 static void gtk_check_button_draw_indicator (GtkCheckButton *check_button,
51 static void gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
54 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
57 gtk_check_button_class_init (GtkCheckButtonClass *class)
59 GtkWidgetClass *widget_class;
61 widget_class = (GtkWidgetClass*) class;
63 widget_class->size_request = gtk_check_button_size_request;
64 widget_class->size_allocate = gtk_check_button_size_allocate;
65 widget_class->draw = gtk_check_button_draw;
67 class->draw_indicator = gtk_real_check_button_draw_indicator;
69 gtk_widget_class_install_style_property (widget_class,
70 g_param_spec_int ("indicator-size",
72 P_("Size of check or radio indicator"),
77 gtk_widget_class_install_style_property (widget_class,
78 g_param_spec_int ("indicator-spacing",
79 P_("Indicator Spacing"),
80 P_("Spacing around check or radio indicator"),
88 gtk_check_button_init (GtkCheckButton *check_button)
90 gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
91 gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
92 GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
93 GTK_BUTTON (check_button)->depress_on_activate = FALSE;
97 gtk_check_button_new (void)
99 return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
104 gtk_check_button_new_with_label (const gchar *label)
106 return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
110 * gtk_check_button_new_with_mnemonic:
111 * @label: The text of the button, with an underscore in front of the
113 * @returns: a new #GtkCheckButton
115 * Creates a new #GtkCheckButton containing a label. The label
116 * will be created using gtk_label_new_with_mnemonic(), so underscores
117 * in @label indicate the mnemonic for the check button.
120 gtk_check_button_new_with_mnemonic (const gchar *label)
122 return g_object_new (GTK_TYPE_CHECK_BUTTON,
124 "use-underline", TRUE,
129 /* This should only be called when toggle_button->draw_indicator
133 gtk_check_button_paint (GtkWidget *widget,
136 GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
142 gtk_widget_style_get (widget,
143 "interior-focus", &interior_focus,
144 "focus-line-width", &focus_width,
145 "focus-padding", &focus_pad,
148 gtk_check_button_draw_indicator (check_button, cr);
150 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
151 if (gtk_widget_has_focus (widget))
153 GtkStateType state = gtk_widget_get_state (widget);
154 GtkStyle *style = gtk_widget_get_style (widget);
155 GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
156 GtkAllocation allocation;
158 gtk_widget_get_allocation (widget, &allocation);
160 if (interior_focus && child && gtk_widget_get_visible (child))
162 GtkAllocation child_allocation;
164 gtk_widget_get_allocation (child, &child_allocation);
165 gtk_paint_focus (style, cr, state,
166 widget, "checkbutton",
167 child_allocation.x - allocation.x - focus_width - focus_pad,
168 child_allocation.y - allocation.y - focus_width - focus_pad,
169 child_allocation.width + 2 * (focus_width + focus_pad),
170 child_allocation.height + 2 * (focus_width + focus_pad));
174 gtk_paint_focus (style, cr, state,
175 widget, "checkbutton",
178 allocation.width - 2 * border_width,
179 allocation.height - 2 * border_width);
185 _gtk_check_button_get_props (GtkCheckButton *check_button,
186 gint *indicator_size,
187 gint *indicator_spacing)
189 GtkWidget *widget = GTK_WIDGET (check_button);
192 gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
194 if (indicator_spacing)
195 gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
199 gtk_check_button_size_request (GtkWidget *widget,
200 GtkRequisition *requisition)
202 GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
204 if (toggle_button->draw_indicator)
209 gint indicator_spacing;
214 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
216 gtk_widget_style_get (GTK_WIDGET (widget),
217 "focus-line-width", &focus_width,
218 "focus-padding", &focus_pad,
221 requisition->width = border_width * 2;
222 requisition->height = border_width * 2;
224 _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
225 &indicator_size, &indicator_spacing);
227 child = gtk_bin_get_child (GTK_BIN (widget));
228 if (child && gtk_widget_get_visible (child))
230 GtkRequisition child_requisition;
232 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
234 requisition->width += child_requisition.width + indicator_spacing;
235 requisition->height += child_requisition.height;
238 requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
240 temp = indicator_size + indicator_spacing * 2;
241 requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
244 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request (widget, requisition);
248 gtk_check_button_size_allocate (GtkWidget *widget,
249 GtkAllocation *allocation)
251 GtkCheckButton *check_button;
252 GtkToggleButton *toggle_button;
254 GtkAllocation child_allocation;
256 button = GTK_BUTTON (widget);
257 check_button = GTK_CHECK_BUTTON (widget);
258 toggle_button = GTK_TOGGLE_BUTTON (widget);
260 if (toggle_button->draw_indicator)
264 gint indicator_spacing;
268 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
269 gtk_widget_style_get (widget,
270 "focus-line-width", &focus_width,
271 "focus-padding", &focus_pad,
274 gtk_widget_set_allocation (widget, allocation);
276 if (gtk_widget_get_realized (widget))
277 gdk_window_move_resize (button->event_window,
278 allocation->x, allocation->y,
279 allocation->width, allocation->height);
281 child = gtk_bin_get_child (GTK_BIN (button));
282 if (child && gtk_widget_get_visible (child))
284 GtkRequisition child_requisition;
285 guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
287 gtk_widget_get_preferred_size (child, &child_requisition, NULL);
289 child_allocation.width = MIN (child_requisition.width,
291 ((border_width + focus_width + focus_pad) * 2
292 + indicator_size + indicator_spacing * 3));
293 child_allocation.width = MAX (child_allocation.width, 1);
295 child_allocation.height = MIN (child_requisition.height,
296 allocation->height - (border_width + focus_width + focus_pad) * 2);
297 child_allocation.height = MAX (child_allocation.height, 1);
299 child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
300 allocation->x + focus_width + focus_pad);
301 child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
303 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
304 child_allocation.x = allocation->x + allocation->width
305 - (child_allocation.x - allocation->x + child_allocation.width);
307 gtk_widget_size_allocate (child, &child_allocation);
311 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
315 gtk_check_button_draw (GtkWidget *widget,
318 GtkToggleButton *toggle_button;
322 toggle_button = GTK_TOGGLE_BUTTON (widget);
323 bin = GTK_BIN (widget);
325 if (toggle_button->draw_indicator)
327 gtk_check_button_paint (widget, cr);
329 child = gtk_bin_get_child (bin);
331 gtk_container_propagate_draw (GTK_CONTAINER (widget),
335 else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
336 GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
343 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
346 GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
348 if (class->draw_indicator)
349 class->draw_indicator (check_button, cr);
353 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
359 GtkToggleButton *toggle_button;
360 GtkStateType state_type;
361 GtkShadowType shadow_type;
364 gint indicator_spacing;
368 gboolean interior_focus;
369 GtkAllocation allocation;
373 widget = GTK_WIDGET (check_button);
374 button = GTK_BUTTON (check_button);
375 toggle_button = GTK_TOGGLE_BUTTON (check_button);
377 gtk_widget_get_allocation (widget, &allocation);
378 style = gtk_widget_get_style (widget);
379 window = gtk_widget_get_window (widget);
381 gtk_widget_style_get (widget,
382 "interior-focus", &interior_focus,
383 "focus-line-width", &focus_width,
384 "focus-padding", &focus_pad,
387 _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
389 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
391 x = indicator_spacing + border_width;
392 y = (allocation.height - indicator_size) / 2;
394 child = gtk_bin_get_child (GTK_BIN (check_button));
395 if (!interior_focus || !(child && gtk_widget_get_visible (child)))
396 x += focus_width + focus_pad;
398 if (toggle_button->inconsistent)
399 shadow_type = GTK_SHADOW_ETCHED_IN;
400 else if (toggle_button->active)
401 shadow_type = GTK_SHADOW_IN;
403 shadow_type = GTK_SHADOW_OUT;
405 if (button->activate_timeout || (button->button_down && button->in_button))
406 state_type = GTK_STATE_ACTIVE;
407 else if (button->in_button)
408 state_type = GTK_STATE_PRELIGHT;
409 else if (!gtk_widget_is_sensitive (widget))
410 state_type = GTK_STATE_INSENSITIVE;
412 state_type = GTK_STATE_NORMAL;
414 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
415 x = allocation.width - (indicator_size + x);
417 if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
420 gtk_paint_flat_box (style, cr, GTK_STATE_PRELIGHT,
421 GTK_SHADOW_ETCHED_OUT,
422 widget, "checkbutton",
423 border_width, border_width,
424 allocation.width - (2 * border_width),
425 allocation.height - (2 * border_width));
428 gtk_paint_check (style, cr,
429 state_type, shadow_type,
430 widget, "checkbutton",
431 x, y, indicator_size, indicator_size);