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