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