]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
Merge branch 'master' into treeview-refactor
[~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 "gtklabel.h"
32
33 #include "gtkprivate.h"
34 #include "gtkintl.h"
35
36
37 #define INDICATOR_SIZE     13
38 #define INDICATOR_SPACING  2
39
40
41 static void gtk_check_button_size_request        (GtkWidget           *widget,
42                                                   GtkRequisition      *requisition);
43 static void gtk_check_button_size_allocate       (GtkWidget           *widget,
44                                                   GtkAllocation       *allocation);
45 static gboolean gtk_check_button_draw            (GtkWidget           *widget,
46                                                   cairo_t             *cr);
47 static void gtk_check_button_paint               (GtkWidget           *widget,
48                                                   cairo_t             *cr);
49 static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
50                                                   cairo_t             *cr);
51 static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
52                                                   cairo_t             *cr);
53
54 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
55
56 static void
57 gtk_check_button_class_init (GtkCheckButtonClass *class)
58 {
59   GtkWidgetClass *widget_class;
60   
61   widget_class = (GtkWidgetClass*) class;
62   
63   widget_class->size_request = gtk_check_button_size_request;
64   widget_class->size_allocate = gtk_check_button_size_allocate;
65   widget_class->draw = gtk_check_button_draw;
66
67   class->draw_indicator = gtk_real_check_button_draw_indicator;
68
69   gtk_widget_class_install_style_property (widget_class,
70                                            g_param_spec_int ("indicator-size",
71                                                              P_("Indicator Size"),
72                                                              P_("Size of check or radio indicator"),
73                                                              0,
74                                                              G_MAXINT,
75                                                              INDICATOR_SIZE,
76                                                              GTK_PARAM_READABLE));
77   gtk_widget_class_install_style_property (widget_class,
78                                            g_param_spec_int ("indicator-spacing",
79                                                              P_("Indicator Spacing"),
80                                                              P_("Spacing around check or radio indicator"),
81                                                              0,
82                                                              G_MAXINT,
83                                                              INDICATOR_SPACING,
84                                                              GTK_PARAM_READABLE));
85 }
86
87 static void
88 gtk_check_button_init (GtkCheckButton *check_button)
89 {
90   gtk_widget_set_has_window (GTK_WIDGET (check_button), FALSE);
91   gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE);
92   gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE);
93 }
94
95 GtkWidget*
96 gtk_check_button_new (void)
97 {
98   return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
99 }
100
101
102 GtkWidget*
103 gtk_check_button_new_with_label (const gchar *label)
104 {
105   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
106 }
107
108 /**
109  * gtk_check_button_new_with_mnemonic:
110  * @label: The text of the button, with an underscore in front of the
111  *         mnemonic character
112  * @returns: a new #GtkCheckButton
113  *
114  * Creates a new #GtkCheckButton containing a label. The label
115  * will be created using gtk_label_new_with_mnemonic(), so underscores
116  * in @label indicate the mnemonic for the check button.
117  **/
118 GtkWidget*
119 gtk_check_button_new_with_mnemonic (const gchar *label)
120 {
121   return g_object_new (GTK_TYPE_CHECK_BUTTON, 
122                        "label", label, 
123                        "use-underline", TRUE, 
124                        NULL);
125 }
126
127
128 /* This should only be called when toggle_button->draw_indicator
129  * is true.
130  */
131 static void
132 gtk_check_button_paint (GtkWidget    *widget,
133                         cairo_t      *cr)
134 {
135   GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
136   gint border_width;
137   gint interior_focus;
138   gint focus_width;
139   gint focus_pad;
140       
141   gtk_widget_style_get (widget,
142                         "interior-focus", &interior_focus,
143                         "focus-line-width", &focus_width,
144                         "focus-padding", &focus_pad,
145                         NULL);
146
147   gtk_check_button_draw_indicator (check_button, cr);
148
149   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
150   if (gtk_widget_has_focus (widget))
151     {
152       GtkStateType state = gtk_widget_get_state (widget);
153       GtkStyle *style = gtk_widget_get_style (widget);
154       GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
155       GtkAllocation allocation;
156
157       gtk_widget_get_allocation (widget, &allocation);
158
159       if (interior_focus && child && gtk_widget_get_visible (child))
160         {
161           GtkAllocation child_allocation;
162
163           gtk_widget_get_allocation (child, &child_allocation);
164           gtk_paint_focus (style, cr, state,
165                            widget, "checkbutton",
166                            child_allocation.x - allocation.x - focus_width - focus_pad,
167                            child_allocation.y - allocation.y - focus_width - focus_pad,
168                            child_allocation.width + 2 * (focus_width + focus_pad),
169                            child_allocation.height + 2 * (focus_width + focus_pad));
170         }
171       else
172         {
173           gtk_paint_focus (style, cr, state,
174                            widget, "checkbutton",
175                            border_width,
176                            border_width,
177                            allocation.width - 2 * border_width,
178                            allocation.height - 2 * border_width);
179         }
180     }
181 }
182
183 void
184 _gtk_check_button_get_props (GtkCheckButton *check_button,
185                              gint           *indicator_size,
186                              gint           *indicator_spacing)
187 {
188   GtkWidget *widget =  GTK_WIDGET (check_button);
189
190   if (indicator_size)
191     gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
192
193   if (indicator_spacing)
194     gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
195 }
196
197 static void
198 gtk_check_button_size_request (GtkWidget      *widget,
199                                GtkRequisition *requisition)
200 {
201   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
202   
203   if (gtk_toggle_button_get_mode (toggle_button))
204     {
205       GtkWidget *child;
206       gint temp;
207       gint indicator_size;
208       gint indicator_spacing;
209       gint focus_width;
210       gint focus_pad;
211       guint border_width;
212
213       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
214
215       gtk_widget_style_get (GTK_WIDGET (widget),
216                             "focus-line-width", &focus_width,
217                             "focus-padding", &focus_pad,
218                             NULL);
219  
220       requisition->width = border_width * 2;
221       requisition->height = border_width * 2;
222
223       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
224                                    &indicator_size, &indicator_spacing);
225       
226       child = gtk_bin_get_child (GTK_BIN (widget));
227       if (child && gtk_widget_get_visible (child))
228         {
229           GtkRequisition child_requisition;
230
231           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
232
233           requisition->width += child_requisition.width + indicator_spacing;
234           requisition->height += child_requisition.height;
235         }
236       
237       requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
238       
239       temp = indicator_size + indicator_spacing * 2;
240       requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
241     }
242   else
243     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request (widget, requisition);
244 }
245
246 static void
247 gtk_check_button_size_allocate (GtkWidget     *widget,
248                                 GtkAllocation *allocation)
249 {
250   GtkCheckButton *check_button;
251   GtkToggleButton *toggle_button;
252   GtkButton *button;
253   GtkAllocation child_allocation;
254
255   button = GTK_BUTTON (widget);
256   check_button = GTK_CHECK_BUTTON (widget);
257   toggle_button = GTK_TOGGLE_BUTTON (widget);
258
259   if (gtk_toggle_button_get_mode (toggle_button))
260     {
261       GtkWidget *child;
262       gint indicator_size;
263       gint indicator_spacing;
264       gint focus_width;
265       gint focus_pad;
266       
267       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
268       gtk_widget_style_get (widget,
269                             "focus-line-width", &focus_width,
270                             "focus-padding", &focus_pad,
271                             NULL);
272
273       gtk_widget_set_allocation (widget, allocation);
274
275       if (gtk_widget_get_realized (widget))
276         gdk_window_move_resize (gtk_button_get_event_window (button),
277                                 allocation->x, allocation->y,
278                                 allocation->width, allocation->height);
279
280       child = gtk_bin_get_child (GTK_BIN (button));
281       if (child && gtk_widget_get_visible (child))
282         {
283           GtkRequisition child_requisition;
284           guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
285
286           gtk_widget_get_preferred_size (child, &child_requisition, NULL);
287
288           child_allocation.width = MIN (child_requisition.width,
289                                         allocation->width -
290                                         ((border_width + focus_width + focus_pad) * 2
291                                          + indicator_size + indicator_spacing * 3));
292           child_allocation.width = MAX (child_allocation.width, 1);
293
294           child_allocation.height = MIN (child_requisition.height,
295                                          allocation->height - (border_width + focus_width + focus_pad) * 2);
296           child_allocation.height = MAX (child_allocation.height, 1);
297           
298           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
299                                 allocation->x + focus_width + focus_pad);
300           child_allocation.y = allocation->y + (allocation->height - child_allocation.height) / 2;
301
302           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
303             child_allocation.x = allocation->x + allocation->width
304               - (child_allocation.x - allocation->x + child_allocation.width);
305           
306           gtk_widget_size_allocate (child, &child_allocation);
307         }
308     }
309   else
310     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
311 }
312
313 static gint
314 gtk_check_button_draw (GtkWidget *widget,
315                        cairo_t   *cr)
316 {
317   GtkToggleButton *toggle_button;
318   GtkBin *bin;
319   GtkWidget *child;
320   
321   toggle_button = GTK_TOGGLE_BUTTON (widget);
322   bin = GTK_BIN (widget);
323
324   if (gtk_toggle_button_get_mode (toggle_button))
325     {
326       gtk_check_button_paint (widget, cr);
327
328       child = gtk_bin_get_child (bin);
329       if (child)
330         gtk_container_propagate_draw (GTK_CONTAINER (widget),
331                                       child,
332                                       cr);
333     }
334   else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw)
335     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->draw (widget, cr);
336
337   return FALSE;
338 }
339
340
341 static void
342 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
343                                  cairo_t        *cr)
344 {
345   GtkCheckButtonClass *class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
346
347   if (class->draw_indicator)
348     class->draw_indicator (check_button, cr);
349 }
350
351 static void
352 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
353                                       cairo_t        *cr)
354 {
355   GtkWidget *widget;
356   GtkWidget *child;
357   GtkButton *button;
358   GtkToggleButton *toggle_button;
359   GtkStateType state_type;
360   GtkShadowType shadow_type;
361   gint x, y;
362   gint indicator_size;
363   gint indicator_spacing;
364   gint focus_width;
365   gint focus_pad;
366   guint border_width;
367   gboolean interior_focus;
368   GtkAllocation allocation;
369   GtkStyle *style;
370   GdkWindow *window;
371
372   widget = GTK_WIDGET (check_button);
373   button = GTK_BUTTON (check_button);
374   toggle_button = GTK_TOGGLE_BUTTON (check_button);
375
376   gtk_widget_get_allocation (widget, &allocation);
377   style = gtk_widget_get_style (widget);
378   window = gtk_widget_get_window (widget);
379
380   gtk_widget_style_get (widget, 
381                         "interior-focus", &interior_focus,
382                         "focus-line-width", &focus_width, 
383                         "focus-padding", &focus_pad, 
384                         NULL);
385
386   _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
387
388   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
389
390   x = indicator_spacing + border_width;
391   y = (allocation.height - indicator_size) / 2;
392
393   child = gtk_bin_get_child (GTK_BIN (check_button));
394   if (!interior_focus || !(child && gtk_widget_get_visible (child)))
395     x += focus_width + focus_pad;      
396
397   if (gtk_toggle_button_get_inconsistent (toggle_button))
398     shadow_type = GTK_SHADOW_ETCHED_IN;
399   else if (gtk_toggle_button_get_active (toggle_button))
400     shadow_type = GTK_SHADOW_IN;
401   else
402     shadow_type = GTK_SHADOW_OUT;
403
404   if (button->activate_timeout || (button->button_down && button->in_button))
405     state_type = GTK_STATE_ACTIVE;
406   else if (button->in_button)
407     state_type = GTK_STATE_PRELIGHT;
408   else if (!gtk_widget_is_sensitive (widget))
409     state_type = GTK_STATE_INSENSITIVE;
410   else
411     state_type = GTK_STATE_NORMAL;
412   
413   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
414     x = allocation.width - (indicator_size + x);
415
416   if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
417     {
418
419       gtk_paint_flat_box (style, cr, GTK_STATE_PRELIGHT,
420                           GTK_SHADOW_ETCHED_OUT, 
421                           widget, "checkbutton",
422                           border_width, border_width,
423                           allocation.width - (2 * border_width),
424                           allocation.height - (2 * border_width));
425     }
426
427   gtk_paint_check (style, cr,
428                    state_type, shadow_type,
429                    widget, "checkbutton",
430                    x, y, indicator_size, indicator_size);
431 }