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