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