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