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