]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
checkbutton: Use the widget state flags as a base for drawing the indicator
[~andy/gtk] / gtk / gtkcheckbutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28
29 #include "gtkcheckbutton.h"
30
31 #include "gtkbuttonprivate.h"
32 #include "gtklabel.h"
33
34 #include "gtkprivate.h"
35 #include "gtkintl.h"
36
37
38 /**
39  * SECTION:gtkcheckbutton
40  * @Short_description: Create widgets with a discrete toggle button
41  * @Title: GtkCheckButton
42  * @See_also: #GtkCheckMenuItem, #GtkButton, #GtkToggleButton, #GtkRadioButton
43  *
44  * A #GtkCheckButton places a discrete #GtkToggleButton next to a widget,
45  * (usually a #GtkLabel). See the section on #GtkToggleButton widgets for
46  * more information about toggle/check buttons.
47  *
48  * The important signal ( #GtkToggleButton::toggled ) is also inherited from
49  * #GtkToggleButton.
50  */
51
52
53 #define INDICATOR_SIZE     16
54 #define INDICATOR_SPACING  2
55
56
57 static void gtk_check_button_get_preferred_width  (GtkWidget          *widget,
58                                                    gint               *minimum,
59                                                    gint               *natural);
60 static void gtk_check_button_get_preferred_height (GtkWidget          *widget,
61                                                    gint               *minimum,
62                                                    gint               *natural);
63 static void gtk_check_button_size_allocate       (GtkWidget           *widget,
64                                                   GtkAllocation       *allocation);
65 static gboolean gtk_check_button_draw            (GtkWidget           *widget,
66                                                   cairo_t             *cr);
67 static void gtk_check_button_paint               (GtkWidget           *widget,
68                                                   cairo_t             *cr);
69 static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
70                                                   cairo_t             *cr);
71 static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
72                                                   cairo_t             *cr);
73
74 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
75
76 static void
77 gtk_check_button_class_init (GtkCheckButtonClass *class)
78 {
79   GtkWidgetClass *widget_class;
80
81   widget_class = (GtkWidgetClass*) class;
82
83   widget_class->get_preferred_width = gtk_check_button_get_preferred_width;
84   widget_class->get_preferred_height = gtk_check_button_get_preferred_height;
85   widget_class->size_allocate = gtk_check_button_size_allocate;
86   widget_class->draw = gtk_check_button_draw;
87
88   class->draw_indicator = gtk_real_check_button_draw_indicator;
89
90   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_CHECK_BOX);
91
92   gtk_widget_class_install_style_property (widget_class,
93                                            g_param_spec_int ("indicator-size",
94                                                              P_("Indicator Size"),
95                                                              P_("Size of check or radio indicator"),
96                                                              0,
97                                                              G_MAXINT,
98                                                              INDICATOR_SIZE,
99                                                              GTK_PARAM_READABLE));
100   gtk_widget_class_install_style_property (widget_class,
101                                            g_param_spec_int ("indicator-spacing",
102                                                              P_("Indicator Spacing"),
103                                                              P_("Spacing around check or radio indicator"),
104                                                              0,
105                                                              G_MAXINT,
106                                                              INDICATOR_SPACING,
107                                                              GTK_PARAM_READABLE));
108 }
109
110 static void
111 gtk_check_button_init (GtkCheckButton *check_button)
112 {
113   gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
114   gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
115   gtk_button_set_alignment (GTK_BUTTON (check_button), 0.0, 0.5);
116 }
117
118 /**
119  * gtk_check_button_new:
120  *
121  * Creates a new #GtkCheckButton.
122  *
123  * Returns: a #GtkWidget.
124  */
125 GtkWidget*
126 gtk_check_button_new (void)
127 {
128   return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
129 }
130
131
132 /**
133  * gtk_check_button_new_with_label:
134  * @label: the text for the check button.
135  *
136  * Creates a new #GtkCheckButton with a #GtkLabel to the right of it.
137  *
138  * Returns: a #GtkWidget.
139  */
140 GtkWidget*
141 gtk_check_button_new_with_label (const gchar *label)
142 {
143   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
144 }
145
146 /**
147  * gtk_check_button_new_with_mnemonic:
148  * @label: The text of the button, with an underscore in front of the
149  *   mnemonic character
150  *
151  * Creates a new #GtkCheckButton containing a label. The label
152  * will be created using gtk_label_new_with_mnemonic(), so underscores
153  * in @label indicate the mnemonic for the check button.
154  *
155  * Returns: a new #GtkCheckButton
156  */
157 GtkWidget*
158 gtk_check_button_new_with_mnemonic (const gchar *label)
159 {
160   return g_object_new (GTK_TYPE_CHECK_BUTTON, 
161                        "label", label, 
162                        "use-underline", TRUE, 
163                        NULL);
164 }
165
166
167 /* This should only be called when toggle_button->draw_indicator
168  * is true.
169  */
170 static void
171 gtk_check_button_paint (GtkWidget    *widget,
172                         cairo_t      *cr)
173 {
174   GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
175
176   gtk_check_button_draw_indicator (check_button, cr);
177
178   if (gtk_widget_has_visible_focus (widget))
179     {
180       GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
181       GtkStyleContext *context;
182       GtkAllocation allocation;
183       gint border_width;
184       gint interior_focus;
185       gint focus_width;
186       gint focus_pad;
187
188       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
189
190       gtk_widget_style_get (widget,
191                             "interior-focus", &interior_focus,
192                             "focus-line-width", &focus_width,
193                             "focus-padding", &focus_pad,
194                             NULL);
195
196       gtk_widget_get_allocation (widget, &allocation);
197       context = gtk_widget_get_style_context (widget);
198
199       if (interior_focus && child && gtk_widget_get_visible (child))
200         {
201           GtkAllocation child_allocation;
202
203           gtk_widget_get_allocation (child, &child_allocation);
204           gtk_render_focus (context, cr,
205                             child_allocation.x - allocation.x - focus_width - focus_pad,
206                             child_allocation.y - allocation.y - focus_width - focus_pad,
207                             child_allocation.width + 2 * (focus_width + focus_pad),
208                             child_allocation.height + 2 * (focus_width + focus_pad));
209         }
210       else
211         gtk_render_focus (context, cr,
212                           border_width, border_width,
213                           allocation.width - 2 * border_width,
214                           allocation.height - 2 * border_width);
215     }
216 }
217
218 void
219 _gtk_check_button_get_props (GtkCheckButton *check_button,
220                              gint           *indicator_size,
221                              gint           *indicator_spacing)
222 {
223   GtkWidget *widget =  GTK_WIDGET (check_button);
224
225   if (indicator_size)
226     gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
227
228   if (indicator_spacing)
229     gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
230 }
231
232 static void
233 gtk_check_button_get_preferred_width (GtkWidget *widget,
234                                       gint      *minimum,
235                                       gint      *natural)
236 {
237   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
238   
239   if (gtk_toggle_button_get_mode (toggle_button))
240     {
241       GtkWidget *child;
242       gint indicator_size;
243       gint indicator_spacing;
244       gint focus_width;
245       gint focus_pad;
246       guint border_width;
247
248       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
249
250       gtk_widget_style_get (GTK_WIDGET (widget),
251                             "focus-line-width", &focus_width,
252                             "focus-padding", &focus_pad,
253                             NULL);
254       *minimum = 2 * border_width;
255       *natural = 2 * border_width;
256
257       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
258                                    &indicator_size, &indicator_spacing);
259
260       child = gtk_bin_get_child (GTK_BIN (widget));
261       if (child && gtk_widget_get_visible (child))
262         {
263           gint child_min, child_nat;
264
265           gtk_widget_get_preferred_width (child, &child_min, &child_nat);
266
267           *minimum += child_min + indicator_spacing;
268           *natural += child_nat + indicator_spacing;
269         }
270
271       *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
272       *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
273     }
274   else
275     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
276 }
277
278 static void
279 gtk_check_button_get_preferred_height (GtkWidget *widget,
280                                        gint      *minimum,
281                                        gint      *natural)
282 {
283   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
284
285   if (gtk_toggle_button_get_mode (toggle_button))
286     {
287       GtkWidget *child;
288       gint temp;
289       gint indicator_size;
290       gint indicator_spacing;
291       gint focus_width;
292       gint focus_pad;
293       guint border_width;
294
295       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
296
297       gtk_widget_style_get (GTK_WIDGET (widget),
298                             "focus-line-width", &focus_width,
299                             "focus-padding", &focus_pad,
300                             NULL);
301
302       *minimum = border_width * 2;
303       *natural = border_width * 2;
304
305       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
306                                    &indicator_size, &indicator_spacing);
307
308       child = gtk_bin_get_child (GTK_BIN (widget));
309       if (child && gtk_widget_get_visible (child))
310         {
311           gint child_min, child_nat;
312
313           gtk_widget_get_preferred_height (child, &child_min, &child_nat);
314
315           *minimum += child_min;
316           *natural += child_nat;
317         }
318
319       temp = indicator_size + indicator_spacing * 2;
320       *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
321       *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
322     }
323   else
324     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
325 }
326
327 static void
328 gtk_check_button_size_allocate (GtkWidget     *widget,
329                                 GtkAllocation *allocation)
330 {
331   GtkCheckButton *check_button;
332   GtkToggleButton *toggle_button;
333   GtkButton *button;
334   GtkAllocation child_allocation;
335
336   button = GTK_BUTTON (widget);
337   check_button = GTK_CHECK_BUTTON (widget);
338   toggle_button = GTK_TOGGLE_BUTTON (widget);
339
340   if (gtk_toggle_button_get_mode (toggle_button))
341     {
342       GtkWidget *child;
343       gint indicator_size;
344       gint indicator_spacing;
345       gint focus_width;
346       gint focus_pad;
347       
348       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
349       gtk_widget_style_get (widget,
350                             "focus-line-width", &focus_width,
351                             "focus-padding", &focus_pad,
352                             NULL);
353
354       gtk_widget_set_allocation (widget, allocation);
355
356       if (gtk_widget_get_realized (widget))
357         gdk_window_move_resize (gtk_button_get_event_window (button),
358                                 allocation->x, allocation->y,
359                                 allocation->width, allocation->height);
360
361       child = gtk_bin_get_child (GTK_BIN (button));
362       if (child && gtk_widget_get_visible (child))
363         {
364           guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
365
366           child_allocation.width = allocation->width -
367             ((border_width + focus_width + focus_pad) * 2 + indicator_size + indicator_spacing * 3);
368           child_allocation.width = MAX (child_allocation.width, 1);
369
370           child_allocation.height = allocation->height - (border_width + focus_width + focus_pad) * 2;
371           child_allocation.height = MAX (child_allocation.height, 1);
372           
373           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
374                                 allocation->x + focus_width + focus_pad);
375           child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
376
377           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
378             child_allocation.x = allocation->x + allocation->width
379               - (child_allocation.x - allocation->x + child_allocation.width);
380           
381           gtk_widget_size_allocate (child, &child_allocation);
382         }
383     }
384   else
385     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
386 }
387
388 static gint
389 gtk_check_button_draw (GtkWidget *widget,
390                        cairo_t   *cr)
391 {
392   GtkToggleButton *toggle_button;
393   GtkBin *bin;
394   GtkWidget *child;
395   
396   toggle_button = GTK_TOGGLE_BUTTON (widget);
397   bin = GTK_BIN (widget);
398
399   if (gtk_toggle_button_get_mode (toggle_button))
400     {
401       gtk_check_button_paint (widget, cr);
402
403       child = gtk_bin_get_child (bin);
404       if (child)
405         gtk_container_propagate_draw (GTK_CONTAINER (widget),
406                                       child,
407                                       cr);
408     }
409   else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
410     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
411
412   return FALSE;
413 }
414
415
416 static void
417 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
418                                  cairo_t        *cr)
419 {
420   GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
421
422   if (class->draw_indicator)
423     class->draw_indicator (check_button, cr);
424 }
425
426 static void
427 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
428                                       cairo_t        *cr)
429 {
430   GtkWidget *widget;
431   GtkWidget *child;
432   GtkButton *button;
433   GtkToggleButton *toggle_button;
434   GtkStateFlags state = 0;
435   gint x, y;
436   gint indicator_size;
437   gint indicator_spacing;
438   gint focus_width;
439   gint focus_pad;
440   guint border_width;
441   gboolean interior_focus;
442   GtkAllocation allocation;
443   GtkStyleContext *context;
444
445   widget = GTK_WIDGET (check_button);
446   button = GTK_BUTTON (check_button);
447   toggle_button = GTK_TOGGLE_BUTTON (check_button);
448
449   gtk_widget_get_allocation (widget, &allocation);
450   context = gtk_widget_get_style_context (widget);
451   state = gtk_widget_get_state_flags (widget);
452
453   gtk_widget_style_get (widget, 
454                         "interior-focus", &interior_focus,
455                         "focus-line-width", &focus_width, 
456                         "focus-padding", &focus_pad, 
457                         NULL);
458
459   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
460
461   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
462
463   x = indicator_spacing + border_width;
464   y = (allocation.height - indicator_size) / 2;
465
466   child = gtk_bin_get_child (GTK_BIN (check_button));
467   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
468     x += focus_width + focus_pad;      
469
470   state &= ~(GTK_STATE_FLAG_INCONSISTENT |
471              GTK_STATE_FLAG_ACTIVE |
472              GTK_STATE_FLAG_SELECTED |
473              GTK_STATE_FLAG_PRELIGHT);
474
475   if (gtk_toggle_button_get_inconsistent (toggle_button))
476     state |= GTK_STATE_FLAG_INCONSISTENT;
477   else if (gtk_toggle_button_get_active (toggle_button) ||
478            (button->priv->button_down && button->priv->in_button))
479     state |= GTK_STATE_FLAG_ACTIVE;
480
481   if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
482     state |= GTK_STATE_FLAG_SELECTED;
483
484   if (button->priv->in_button)
485     state |= GTK_STATE_FLAG_PRELIGHT;
486
487   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
488     x = allocation.width - (indicator_size + x);
489
490   gtk_style_context_save (context);
491   gtk_style_context_set_state (context, state);
492
493   if (state & GTK_STATE_FLAG_PRELIGHT)
494     gtk_render_background (context, cr,
495                            border_width, border_width,
496                            allocation.width - (2 * border_width),
497                            allocation.height - (2 * border_width));
498
499   gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
500
501   gtk_render_check (context, cr,
502                     x, y, indicator_size, indicator_size);
503
504   gtk_style_context_restore (context);
505 }