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