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