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