]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
Use accessor functions to acces GtkContainer
[~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           GtkWidget *child = GTK_BIN (widget)->child;
154           
155           if (interior_focus && child && gtk_widget_get_visible (child))
156             gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
157                              area, widget, "checkbutton",
158                              child->allocation.x - focus_width - focus_pad,
159                              child->allocation.y - focus_width - focus_pad,
160                              child->allocation.width + 2 * (focus_width + focus_pad),
161                              child->allocation.height + 2 * (focus_width + focus_pad));
162           else
163             gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
164                              area, widget, "checkbutton",
165                              border_width + widget->allocation.x,
166                              border_width + widget->allocation.y,
167                              widget->allocation.width - 2 * border_width,
168                              widget->allocation.height - 2 * border_width);
169         }
170     }
171 }
172
173 void
174 _gtk_check_button_get_props (GtkCheckButton *check_button,
175                              gint           *indicator_size,
176                              gint           *indicator_spacing)
177 {
178   GtkWidget *widget =  GTK_WIDGET (check_button);
179
180   if (indicator_size)
181     gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
182
183   if (indicator_spacing)
184     gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
185 }
186
187 static void
188 gtk_check_button_size_request (GtkWidget      *widget,
189                                GtkRequisition *requisition)
190 {
191   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
192   
193   if (toggle_button->draw_indicator)
194     {
195       GtkWidget *child;
196       gint temp;
197       gint indicator_size;
198       gint indicator_spacing;
199       gint focus_width;
200       gint focus_pad;
201       guint border_width;
202
203       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
204
205       gtk_widget_style_get (GTK_WIDGET (widget),
206                             "focus-line-width", &focus_width,
207                             "focus-padding", &focus_pad,
208                             NULL);
209  
210       requisition->width = border_width * 2;
211       requisition->height = border_width * 2;
212
213       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
214                                    &indicator_size, &indicator_spacing);
215       
216       child = GTK_BIN (widget)->child;
217       if (child && gtk_widget_get_visible (child))
218         {
219           GtkRequisition child_requisition;
220           
221           gtk_widget_size_request (child, &child_requisition);
222
223           requisition->width += child_requisition.width + indicator_spacing;
224           requisition->height += child_requisition.height;
225         }
226       
227       requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
228       
229       temp = indicator_size + indicator_spacing * 2;
230       requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
231     }
232   else
233     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request (widget, requisition);
234 }
235
236 static void
237 gtk_check_button_size_allocate (GtkWidget     *widget,
238                                 GtkAllocation *allocation)
239 {
240   GtkCheckButton *check_button;
241   GtkToggleButton *toggle_button;
242   GtkButton *button;
243   GtkAllocation child_allocation;
244
245   button = GTK_BUTTON (widget);
246   check_button = GTK_CHECK_BUTTON (widget);
247   toggle_button = GTK_TOGGLE_BUTTON (widget);
248
249   if (toggle_button->draw_indicator)
250     {
251       gint indicator_size;
252       gint indicator_spacing;
253       gint focus_width;
254       gint focus_pad;
255       
256       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
257       gtk_widget_style_get (widget,
258                             "focus-line-width", &focus_width,
259                             "focus-padding", &focus_pad,
260                             NULL);
261                                                     
262       widget->allocation = *allocation;
263       if (gtk_widget_get_realized (widget))
264         gdk_window_move_resize (button->event_window,
265                                 allocation->x, allocation->y,
266                                 allocation->width, allocation->height);
267       
268       if (GTK_BIN (button)->child && gtk_widget_get_visible (GTK_BIN (button)->child))
269         {
270           GtkRequisition child_requisition;
271           guint border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
272
273           gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
274  
275           child_allocation.width = MIN (child_requisition.width,
276                                         allocation->width -
277                                         ((border_width + focus_width + focus_pad) * 2
278                                          + indicator_size + indicator_spacing * 3));
279           child_allocation.width = MAX (child_allocation.width, 1);
280
281           child_allocation.height = MIN (child_requisition.height,
282                                          allocation->height - (border_width + focus_width + focus_pad) * 2);
283           child_allocation.height = MAX (child_allocation.height, 1);
284           
285           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
286                                 widget->allocation.x + focus_width + focus_pad);
287           child_allocation.y = widget->allocation.y +
288                   (allocation->height - child_allocation.height) / 2;
289
290           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
291             child_allocation.x = allocation->x + allocation->width
292               - (child_allocation.x - allocation->x + child_allocation.width);
293           
294           gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
295         }
296     }
297   else
298     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
299 }
300
301 static gint
302 gtk_check_button_expose (GtkWidget      *widget,
303                          GdkEventExpose *event)
304 {
305   GtkToggleButton *toggle_button;
306   GtkBin *bin;
307   
308   toggle_button = GTK_TOGGLE_BUTTON (widget);
309   bin = GTK_BIN (widget);
310   
311   if (gtk_widget_is_drawable (widget))
312     {
313       if (toggle_button->draw_indicator)
314         {
315           gtk_check_button_paint (widget, &event->area);
316           
317           if (bin->child)
318             gtk_container_propagate_expose (GTK_CONTAINER (widget),
319                                             bin->child,
320                                             event);
321         }
322       else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event)
323         GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event (widget, event);
324     }
325   
326   return FALSE;
327 }
328
329
330 static void
331 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
332                                  GdkRectangle   *area)
333 {
334   GtkCheckButtonClass *class;
335   
336   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
337   
338   class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
339
340   if (class->draw_indicator)
341     class->draw_indicator (check_button, area);
342 }
343
344 static void
345 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
346                                       GdkRectangle   *area)
347 {
348   GtkWidget *widget;
349   GtkWidget *child;
350   GtkButton *button;
351   GtkToggleButton *toggle_button;
352   GtkStateType state_type;
353   GtkShadowType shadow_type;
354   gint x, y;
355   gint indicator_size;
356   gint indicator_spacing;
357   gint focus_width;
358   gint focus_pad;
359   guint border_width;
360   gboolean interior_focus;
361
362   widget = GTK_WIDGET (check_button);
363
364   if (gtk_widget_is_drawable (widget))
365     {
366       button = GTK_BUTTON (check_button);
367       toggle_button = GTK_TOGGLE_BUTTON (check_button);
368   
369       gtk_widget_style_get (widget, 
370                             "interior-focus", &interior_focus,
371                             "focus-line-width", &focus_width, 
372                             "focus-padding", &focus_pad, 
373                             NULL);
374
375       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
376
377       border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
378
379       x = widget->allocation.x + indicator_spacing + border_width;
380       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
381
382       child = GTK_BIN (check_button)->child;
383       if (!interior_focus || !(child && gtk_widget_get_visible (child)))
384         x += focus_width + focus_pad;      
385
386       if (toggle_button->inconsistent)
387         shadow_type = GTK_SHADOW_ETCHED_IN;
388       else if (toggle_button->active)
389         shadow_type = GTK_SHADOW_IN;
390       else
391         shadow_type = GTK_SHADOW_OUT;
392
393       if (button->activate_timeout || (button->button_down && button->in_button))
394         state_type = GTK_STATE_ACTIVE;
395       else if (button->in_button)
396         state_type = GTK_STATE_PRELIGHT;
397       else if (!gtk_widget_is_sensitive (widget))
398         state_type = GTK_STATE_INSENSITIVE;
399       else
400         state_type = GTK_STATE_NORMAL;
401       
402       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
403         x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
404
405       if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
406         {
407           GdkRectangle restrict_area;
408           GdkRectangle new_area;
409               
410           restrict_area.x = widget->allocation.x + border_width;
411           restrict_area.y = widget->allocation.y + border_width;
412           restrict_area.width = widget->allocation.width - (2 * border_width);
413           restrict_area.height = widget->allocation.height - (2 * border_width);
414           
415           if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
416             {
417               gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
418                                   GTK_SHADOW_ETCHED_OUT, 
419                                   area, widget, "checkbutton",
420                                   new_area.x, new_area.y,
421                                   new_area.width, new_area.height);
422             }
423         }
424
425       gtk_paint_check (widget->style, widget->window,
426                        state_type, shadow_type,
427                        area, widget, "checkbutton",
428                        x, y, indicator_size, indicator_size);
429     }
430 }