]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
checkbutton: Use gtk_widget_class_set_accessible_role()
[~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_has_window (GTK_WIDGET (check_button), FALSE);
114   gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
115   gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
116   gtk_button_set_alignment (GTK_BUTTON (check_button), 0.0, 0.5);
117 }
118
119 /**
120  * gtk_check_button_new:
121  *
122  * Creates a new #GtkCheckButton.
123  *
124  * Returns: a #GtkWidget.
125  */
126 GtkWidget*
127 gtk_check_button_new (void)
128 {
129   return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
130 }
131
132
133 /**
134  * gtk_check_button_new_with_label:
135  * @label: the text for the check button.
136  *
137  * Creates a new #GtkCheckButton with a #GtkLabel to the right of it.
138  *
139  * Returns: a #GtkWidget.
140  */
141 GtkWidget*
142 gtk_check_button_new_with_label (const gchar *label)
143 {
144   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
145 }
146
147 /**
148  * gtk_check_button_new_with_mnemonic:
149  * @label: The text of the button, with an underscore in front of the
150  *   mnemonic character
151  * @returns: a new #GtkCheckButton
152  *
153  * Creates a new #GtkCheckButton containing a label. The label
154  * will be created using gtk_label_new_with_mnemonic(), so underscores
155  * in @label indicate the mnemonic for the check button.
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   gint border_width;
176   gint interior_focus;
177   gint focus_width;
178   gint focus_pad;
179       
180   gtk_widget_style_get (widget,
181                         "interior-focus", &interior_focus,
182                         "focus-line-width", &focus_width,
183                         "focus-padding", &focus_pad,
184                         NULL);
185
186   gtk_check_button_draw_indicator (check_button, cr);
187
188   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
189   if (gtk_widget_has_focus (widget))
190     {
191       GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
192       GtkStyleContext *context;
193       GtkStateFlags state;
194       GtkAllocation allocation;
195
196       gtk_widget_get_allocation (widget, &allocation);
197       context = gtk_widget_get_style_context (widget);
198       state = gtk_widget_get_state_flags (widget);
199
200       gtk_style_context_set_state (context, state);
201
202       if (interior_focus && child && gtk_widget_get_visible (child))
203         {
204           GtkAllocation child_allocation;
205
206           gtk_widget_get_allocation (child, &child_allocation);
207           gtk_render_focus (context, cr,
208                             child_allocation.x - allocation.x - focus_width - focus_pad,
209                             child_allocation.y - allocation.y - focus_width - focus_pad,
210                             child_allocation.width + 2 * (focus_width + focus_pad),
211                             child_allocation.height + 2 * (focus_width + focus_pad));
212         }
213       else
214         gtk_render_focus (context, cr,
215                           border_width, border_width,
216                           allocation.width - 2 * border_width,
217                           allocation.height - 2 * border_width);
218     }
219 }
220
221 void
222 _gtk_check_button_get_props (GtkCheckButton *check_button,
223                              gint           *indicator_size,
224                              gint           *indicator_spacing)
225 {
226   GtkWidget *widget =  GTK_WIDGET (check_button);
227
228   if (indicator_size)
229     gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
230
231   if (indicator_spacing)
232     gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
233 }
234
235 static void
236 gtk_check_button_get_preferred_width (GtkWidget *widget,
237                                       gint      *minimum,
238                                       gint      *natural)
239 {
240   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
241   
242   if (gtk_toggle_button_get_mode (toggle_button))
243     {
244       GtkWidget *child;
245       gint indicator_size;
246       gint indicator_spacing;
247       gint focus_width;
248       gint focus_pad;
249       guint border_width;
250
251       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
252
253       gtk_widget_style_get (GTK_WIDGET (widget),
254                             "focus-line-width", &focus_width,
255                             "focus-padding", &focus_pad,
256                             NULL);
257       *minimum = 2 * border_width;
258       *natural = 2 * border_width;
259
260       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
261                                    &indicator_size, &indicator_spacing);
262
263       child = gtk_bin_get_child (GTK_BIN (widget));
264       if (child && gtk_widget_get_visible (child))
265         {
266           gint child_min, child_nat;
267
268           gtk_widget_get_preferred_width (child, &child_min, &child_nat);
269
270           *minimum += child_min + indicator_spacing;
271           *natural += child_nat + indicator_spacing;
272         }
273
274       *minimum += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
275       *natural += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
276     }
277   else
278     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_width (widget, minimum, natural);
279 }
280
281 static void
282 gtk_check_button_get_preferred_height (GtkWidget *widget,
283                                        gint      *minimum,
284                                        gint      *natural)
285 {
286   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
287
288   if (gtk_toggle_button_get_mode (toggle_button))
289     {
290       GtkWidget *child;
291       gint temp;
292       gint indicator_size;
293       gint indicator_spacing;
294       gint focus_width;
295       gint focus_pad;
296       guint border_width;
297
298       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
299
300       gtk_widget_style_get (GTK_WIDGET (widget),
301                             "focus-line-width", &focus_width,
302                             "focus-padding", &focus_pad,
303                             NULL);
304
305       *minimum = border_width * 2;
306       *natural = border_width * 2;
307
308       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
309                                    &indicator_size, &indicator_spacing);
310
311       child = gtk_bin_get_child (GTK_BIN (widget));
312       if (child && gtk_widget_get_visible (child))
313         {
314           gint child_min, child_nat;
315
316           gtk_widget_get_preferred_height (child, &child_min, &child_nat);
317
318           *minimum += child_min;
319           *natural += child_nat;
320         }
321
322       temp = indicator_size + indicator_spacing * 2;
323       *minimum = MAX (*minimum, temp) + 2 * (focus_width + focus_pad);
324       *natural = MAX (*natural, temp) + 2 * (focus_width + focus_pad);
325     }
326   else
327     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->get_preferred_height (widget, minimum, natural);
328 }
329
330 static void
331 gtk_check_button_size_allocate (GtkWidget     *widget,
332                                 GtkAllocation *allocation)
333 {
334   GtkCheckButton *check_button;
335   GtkToggleButton *toggle_button;
336   GtkButton *button;
337   GtkAllocation child_allocation;
338
339   button = GTK_BUTTON (widget);
340   check_button = GTK_CHECK_BUTTON (widget);
341   toggle_button = GTK_TOGGLE_BUTTON (widget);
342
343   if (gtk_toggle_button_get_mode (toggle_button))
344     {
345       GtkWidget *child;
346       gint indicator_size;
347       gint indicator_spacing;
348       gint focus_width;
349       gint focus_pad;
350       
351       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
352       gtk_widget_style_get (widget,
353                             "focus-line-width", &focus_width,
354                             "focus-padding", &focus_pad,
355                             NULL);
356
357       gtk_widget_set_allocation (widget, allocation);
358
359       if (gtk_widget_get_realized (widget))
360         gdk_window_move_resize (gtk_button_get_event_window (button),
361                                 allocation->x, allocation->y,
362                                 allocation->width, allocation->height);
363
364       child = gtk_bin_get_child (GTK_BIN (button));
365       if (child && gtk_widget_get_visible (child))
366         {
367           guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
368
369           child_allocation.width = allocation->width -
370             ((border_width + focus_width + focus_pad) * 2 + indicator_size + indicator_spacing * 3);
371           child_allocation.width = MAX (child_allocation.width, 1);
372
373           child_allocation.height = allocation->height - (border_width + focus_width + focus_pad) * 2;
374           child_allocation.height = MAX (child_allocation.height, 1);
375           
376           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
377                                 allocation->x + focus_width + focus_pad);
378           child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
379
380           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
381             child_allocation.x = allocation->x + allocation->width
382               - (child_allocation.x - allocation->x + child_allocation.width);
383           
384           gtk_widget_size_allocate (child, &child_allocation);
385         }
386     }
387   else
388     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
389 }
390
391 static gint
392 gtk_check_button_draw (GtkWidget *widget,
393                        cairo_t   *cr)
394 {
395   GtkToggleButton *toggle_button;
396   GtkBin *bin;
397   GtkWidget *child;
398   
399   toggle_button = GTK_TOGGLE_BUTTON (widget);
400   bin = GTK_BIN (widget);
401
402   if (gtk_toggle_button_get_mode (toggle_button))
403     {
404       gtk_check_button_paint (widget, cr);
405
406       child = gtk_bin_get_child (bin);
407       if (child)
408         gtk_container_propagate_draw (GTK_CONTAINER (widget),
409                                       child,
410                                       cr);
411     }
412   else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
413     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
414
415   return FALSE;
416 }
417
418
419 static void
420 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
421                                  cairo_t        *cr)
422 {
423   GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
424
425   if (class->draw_indicator)
426     class->draw_indicator (check_button, cr);
427 }
428
429 static void
430 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
431                                       cairo_t        *cr)
432 {
433   GtkWidget *widget;
434   GtkWidget *child;
435   GtkButton *button;
436   GtkToggleButton *toggle_button;
437   GtkStateFlags state = 0;
438   gint x, y;
439   gint indicator_size;
440   gint indicator_spacing;
441   gint focus_width;
442   gint focus_pad;
443   guint border_width;
444   gboolean interior_focus;
445   GtkAllocation allocation;
446   GtkStyleContext *context;
447
448   widget = GTK_WIDGET (check_button);
449   button = GTK_BUTTON (check_button);
450   toggle_button = GTK_TOGGLE_BUTTON (check_button);
451
452   gtk_widget_get_allocation (widget, &allocation);
453   context = gtk_widget_get_style_context (widget);
454
455   gtk_widget_style_get (widget, 
456                         "interior-focus", &interior_focus,
457                         "focus-line-width", &focus_width, 
458                         "focus-padding", &focus_pad, 
459                         NULL);
460
461   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
462
463   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
464
465   x = indicator_spacing + border_width;
466   y = (allocation.height - indicator_size) / 2;
467
468   child = gtk_bin_get_child (GTK_BIN (check_button));
469   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
470     x += focus_width + focus_pad;      
471
472   if (gtk_toggle_button_get_inconsistent (toggle_button))
473     state |= GTK_STATE_FLAG_INCONSISTENT;
474   else if (gtk_toggle_button_get_active (toggle_button) ||
475            (button->priv->button_down && button->priv->in_button))
476     state |= GTK_STATE_FLAG_ACTIVE;
477
478   if (button->priv->activate_timeout || (button->priv->button_down && button->priv->in_button))
479     state |= GTK_STATE_FLAG_SELECTED;
480
481   if (button->priv->in_button)
482     state |= GTK_STATE_FLAG_PRELIGHT;
483   else if (!gtk_widget_is_sensitive (widget))
484     state |= GTK_STATE_FLAG_INSENSITIVE;
485
486   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
487     x = allocation.width - (indicator_size + x);
488
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_save (context);
498   gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
499
500   gtk_render_check (context, cr,
501                     x, y, indicator_size, indicator_size);
502
503   gtk_style_context_restore (context);
504 }