]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
set RECEIVES_DEFAULT on init, and a button press on the widget doesn't
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gtkcheckbutton.h"
20 #include "gtklabel.h"
21
22
23 #define INDICATOR_SIZE     10
24 #define INDICATOR_SPACING  2
25
26 #define CHECK_BUTTON_CLASS(w)  GTK_CHECK_BUTTON_CLASS (GTK_OBJECT (w)->klass)
27
28
29 static void gtk_check_button_class_init          (GtkCheckButtonClass *klass);
30 static void gtk_check_button_init                (GtkCheckButton      *check_button);
31 static void gtk_check_button_draw                (GtkWidget           *widget,
32                                                   GdkRectangle        *area);
33 static void gtk_check_button_draw_focus          (GtkWidget           *widget);
34 static void gtk_check_button_size_request        (GtkWidget           *widget,
35                                                   GtkRequisition      *requisition);
36 static void gtk_check_button_size_allocate       (GtkWidget           *widget,
37                                                   GtkAllocation       *allocation);
38 static gint gtk_check_button_expose              (GtkWidget           *widget,
39                                                   GdkEventExpose      *event);
40 static void gtk_check_button_paint               (GtkWidget           *widget,
41                                                   GdkRectangle        *area);
42 static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
43                                                   GdkRectangle        *area);
44 static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
45                                                   GdkRectangle        *area);
46
47 static GtkToggleButtonClass *parent_class = NULL;
48
49
50 GtkType
51 gtk_check_button_get_type (void)
52 {
53   static GtkType check_button_type = 0;
54   
55   if (!check_button_type)
56     {
57       static const GtkTypeInfo check_button_info =
58       {
59         "GtkCheckButton",
60         sizeof (GtkCheckButton),
61         sizeof (GtkCheckButtonClass),
62         (GtkClassInitFunc) gtk_check_button_class_init,
63         (GtkObjectInitFunc) gtk_check_button_init,
64         /* reserved_1 */ NULL,
65         /* reserved_2 */ NULL,
66         (GtkClassInitFunc) NULL,
67       };
68       
69       check_button_type = gtk_type_unique (GTK_TYPE_TOGGLE_BUTTON, &check_button_info);
70     }
71   
72   return check_button_type;
73 }
74
75 static void
76 gtk_check_button_class_init (GtkCheckButtonClass *class)
77 {
78   GtkWidgetClass *widget_class;
79   
80   widget_class = (GtkWidgetClass*) class;
81   parent_class = gtk_type_class (gtk_toggle_button_get_type ());
82   
83   widget_class->draw = gtk_check_button_draw;
84   widget_class->draw_focus = gtk_check_button_draw_focus;
85   widget_class->size_request = gtk_check_button_size_request;
86   widget_class->size_allocate = gtk_check_button_size_allocate;
87   widget_class->expose_event = gtk_check_button_expose;
88   
89   class->indicator_size = INDICATOR_SIZE;
90   class->indicator_spacing = INDICATOR_SPACING;
91   class->draw_indicator = gtk_real_check_button_draw_indicator;
92 }
93
94 static void
95 gtk_check_button_init (GtkCheckButton *check_button)
96 {
97   GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
98   GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
99   GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
100 }
101
102 GtkWidget*
103 gtk_check_button_new (void)
104 {
105   return gtk_widget_new (GTK_TYPE_CHECK_BUTTON, NULL);
106 }
107
108
109 GtkWidget*
110 gtk_check_button_new_with_label (const gchar *label)
111 {
112   GtkWidget *check_button;
113   GtkWidget *label_widget;
114   
115   check_button = gtk_check_button_new ();
116   label_widget = gtk_label_new (label);
117   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
118   
119   gtk_container_add (GTK_CONTAINER (check_button), label_widget);
120   gtk_widget_show (label_widget);
121   
122   return check_button;
123 }
124
125 /* This should only be called when toggle_button->draw_indicator
126  * is true.
127  */
128 static void
129 gtk_check_button_paint (GtkWidget    *widget,
130                         GdkRectangle *area)
131 {
132   GtkCheckButton *check_button;
133   
134   g_return_if_fail (widget != NULL);
135   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
136   
137   check_button = GTK_CHECK_BUTTON (widget);
138   
139   if (GTK_WIDGET_DRAWABLE (widget))
140     {
141       gint border_width;
142           
143       gtk_check_button_draw_indicator (check_button, area);
144       
145       border_width = GTK_CONTAINER (widget)->border_width;
146       if (GTK_WIDGET_HAS_FOCUS (widget))
147         gtk_paint_focus (widget->style, widget->window,
148                          NULL, widget, "checkbutton",
149                          border_width + widget->allocation.x,
150                          border_width + widget->allocation.y,
151                          widget->allocation.width - 2 * border_width - 1,
152                          widget->allocation.height - 2 * border_width - 1);
153     }
154 }
155
156 static void
157 gtk_check_button_draw (GtkWidget    *widget,
158                        GdkRectangle *area)
159 {
160   GtkCheckButton *check_button;
161   GtkToggleButton *toggle_button;
162   GtkBin *bin;
163   GdkRectangle child_area;
164   
165   g_return_if_fail (widget != NULL);
166   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
167   g_return_if_fail (area != NULL);
168   
169   check_button = GTK_CHECK_BUTTON (widget);
170   toggle_button = GTK_TOGGLE_BUTTON (widget);
171   bin = GTK_BIN (widget);
172   
173   if (GTK_WIDGET_DRAWABLE (widget))
174     {
175       if (toggle_button->draw_indicator)
176         {
177           gtk_check_button_paint (widget, area);
178
179           if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
180             gtk_widget_draw (bin->child, &child_area);
181         }
182       else
183         {
184           if (GTK_WIDGET_CLASS (parent_class)->draw)
185             (* GTK_WIDGET_CLASS (parent_class)->draw) (widget, area);
186         }
187     }
188 }
189
190 static void
191 gtk_check_button_draw_focus (GtkWidget *widget)
192 {
193   gint border_width;
194   
195   g_return_if_fail (widget != NULL);
196   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
197   
198   border_width = GTK_CONTAINER (widget)->border_width;
199   gtk_widget_queue_clear_area (widget->parent, 
200                                border_width + widget->allocation.x,
201                                border_width + widget->allocation.y,
202                                widget->allocation.width - 2 * border_width,
203                                widget->allocation.height - 2 * border_width);
204 }
205
206 static void
207 gtk_check_button_size_request (GtkWidget      *widget,
208                                GtkRequisition *requisition)
209 {
210   GtkToggleButton *toggle_button;
211   gint temp;
212   
213   g_return_if_fail (widget != NULL);
214   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
215   g_return_if_fail (requisition != NULL);
216   
217   toggle_button = GTK_TOGGLE_BUTTON (widget);
218   
219   if (GTK_WIDGET_CLASS (parent_class)->size_request)
220     (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
221   
222   if (toggle_button->draw_indicator)
223     {
224       requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
225                              CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
226       
227       temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
228               CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
229       requisition->height = MAX (requisition->height, temp) + 2;
230     }
231 }
232
233 static void
234 gtk_check_button_size_allocate (GtkWidget     *widget,
235                                 GtkAllocation *allocation)
236 {
237   GtkCheckButton *check_button;
238   GtkToggleButton *toggle_button;
239   GtkButton *button;
240   GtkAllocation child_allocation;
241   
242   g_return_if_fail (widget != NULL);
243   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
244   g_return_if_fail (allocation != NULL);
245   
246   check_button = GTK_CHECK_BUTTON (widget);
247   toggle_button = GTK_TOGGLE_BUTTON (widget);
248
249   if (toggle_button->draw_indicator)
250     {
251       widget->allocation = *allocation;
252       if (GTK_WIDGET_REALIZED (widget))
253         gdk_window_move_resize (toggle_button->event_window,
254                                 allocation->x, allocation->y,
255                                 allocation->width, allocation->height);
256       
257       button = GTK_BUTTON (widget);
258       
259       if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
260         {
261           child_allocation.x = (GTK_CONTAINER (widget)->border_width +
262                                 CHECK_BUTTON_CLASS (widget)->indicator_size +
263                                 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1 +
264                                 widget->allocation.x);
265           child_allocation.y = GTK_CONTAINER (widget)->border_width + 1 +
266             widget->allocation.y;
267           child_allocation.width = MAX (1, allocation->width - 
268                                         (GTK_CONTAINER (widget)->border_width +
269                                          CHECK_BUTTON_CLASS (widget)->indicator_size +
270                                          CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1)  -
271                                         GTK_CONTAINER (widget)->border_width - 1);
272           child_allocation.height = MAX (1, allocation->height - (GTK_CONTAINER (widget)->border_width + 1) * 2);
273           
274           gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
275         }
276     }
277   else
278     {
279       if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
280         (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
281     }
282 }
283
284 static gint
285 gtk_check_button_expose (GtkWidget      *widget,
286                          GdkEventExpose *event)
287 {
288   GtkCheckButton *check_button;
289   GtkToggleButton *toggle_button;
290   GtkBin *bin;
291   GdkEventExpose child_event;
292   
293   g_return_val_if_fail (widget != NULL, FALSE);
294   g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
295   g_return_val_if_fail (event != NULL, FALSE);
296   
297   check_button = GTK_CHECK_BUTTON (widget);
298   toggle_button = GTK_TOGGLE_BUTTON (widget);
299   bin = GTK_BIN (widget);
300   
301   if (GTK_WIDGET_DRAWABLE (widget))
302     {
303       if (toggle_button->draw_indicator)
304         {
305           gtk_check_button_paint (widget, &event->area);
306           
307           child_event = *event;
308           if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child) &&
309               gtk_widget_intersect (bin->child, &event->area, &child_event.area))
310             gtk_widget_event (bin->child, (GdkEvent*) &child_event);
311         }
312       else
313         {
314           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
315             (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
316         }
317     }
318   
319   return FALSE;
320 }
321
322
323 static void
324 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
325                                  GdkRectangle   *area)
326 {
327   GtkCheckButtonClass *class;
328   
329   g_return_if_fail (check_button != NULL);
330   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
331   
332   class = CHECK_BUTTON_CLASS (check_button);
333   
334   if (class->draw_indicator)
335     (* class->draw_indicator) (check_button, area);
336 }
337
338 static void
339 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
340                                       GdkRectangle   *area)
341 {
342   GtkWidget *widget;
343   GtkToggleButton *toggle_button;
344   GtkStateType state_type;
345   GtkShadowType shadow_type;
346   GdkRectangle restrict_area;
347   GdkRectangle new_area;
348   gint width, height;
349   gint x, y;
350   GdkWindow *window;
351   
352   g_return_if_fail (check_button != NULL);
353   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
354   
355   widget = GTK_WIDGET (check_button);
356   toggle_button = GTK_TOGGLE_BUTTON (check_button);
357   
358   if (GTK_WIDGET_DRAWABLE (check_button))
359     {
360       window = widget->window;
361       
362       state_type = GTK_WIDGET_STATE (widget);
363       if (state_type != GTK_STATE_NORMAL &&
364           state_type != GTK_STATE_PRELIGHT)
365         state_type = GTK_STATE_NORMAL;
366       
367       restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
368       restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
369       restrict_area.width = widget->allocation.width - ( 2 * GTK_CONTAINER (widget)->border_width);
370       restrict_area.height = widget->allocation.height - ( 2 * GTK_CONTAINER (widget)->border_width);
371       
372       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
373         {
374           if (state_type != GTK_STATE_NORMAL)
375             gtk_paint_flat_box (widget->style, window, state_type, 
376                                 GTK_SHADOW_ETCHED_OUT, 
377                                 area, widget, "checkbutton",
378                                 new_area.x, new_area.y,
379                                 new_area.width, new_area.height);
380         }
381       
382       x = widget->allocation.x + CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
383       y = widget->allocation.y + (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
384       width = CHECK_BUTTON_CLASS (widget)->indicator_size;
385       height = CHECK_BUTTON_CLASS (widget)->indicator_size;
386       
387       if (GTK_TOGGLE_BUTTON (widget)->active)
388         {
389           state_type = GTK_STATE_ACTIVE;
390           shadow_type = GTK_SHADOW_IN;
391         }
392       else
393         {
394           shadow_type = GTK_SHADOW_OUT;
395           state_type = GTK_WIDGET_STATE (widget);
396         }
397
398       gtk_paint_check (widget->style, window,
399                        state_type, shadow_type,
400                        area, widget, "checkbutton",
401                        x + 1, y + 1, width, height);
402     }
403 }