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