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