]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
applied patch to fix draw_indicator behaviour from Lars Hamann and Stefan
[~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_DRAWABLE (widget))
165     {
166       check_button = GTK_CHECK_BUTTON (widget);
167       if (check_button->toggle_button.draw_indicator)
168         {
169           gint border_width;
170           
171           border_width = GTK_CONTAINER (widget)->border_width;
172           if (GTK_WIDGET_HAS_FOCUS (widget))
173             gdk_draw_rectangle (widget->window,
174                                 widget->style->black_gc, FALSE,
175                                 border_width, border_width,
176                                 widget->allocation.width - 2 * border_width - 1,
177                                 widget->allocation.height - 2 * border_width - 1);
178           else
179             gdk_draw_rectangle (widget->window,
180                                 widget->style->bg_gc[GTK_STATE_NORMAL], FALSE,
181                                 border_width, border_width,
182                                 widget->allocation.width - 2 * border_width - 1,
183                                 widget->allocation.height - 2 * border_width - 1);
184         }
185       else
186         {
187           if (GTK_WIDGET_CLASS (parent_class)->draw_focus)
188             (* GTK_WIDGET_CLASS (parent_class)->draw_focus) (widget);
189         }
190     }
191 }
192
193 static void
194 gtk_check_button_size_request (GtkWidget      *widget,
195                                GtkRequisition *requisition)
196 {
197   GtkCheckButton *check_button;
198   GtkButton *button;
199   gint temp;
200
201   g_return_if_fail (widget != NULL);
202   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
203   g_return_if_fail (requisition != NULL);
204
205   check_button = GTK_CHECK_BUTTON (widget);
206
207   if (GTK_WIDGET_CLASS (parent_class)->size_request)
208     (* GTK_WIDGET_CLASS (parent_class)->size_request) (widget, requisition);
209
210   if (check_button->toggle_button.draw_indicator)
211     {
212       button = GTK_BUTTON (widget);
213
214       requisition->width += (CHECK_BUTTON_CLASS (widget)->indicator_size +
215                              CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 2);
216
217       temp = (CHECK_BUTTON_CLASS (widget)->indicator_size +
218               CHECK_BUTTON_CLASS (widget)->indicator_spacing * 2);
219       requisition->height = MAX (requisition->height, temp) + 2;
220     }
221 }
222
223 static void
224 gtk_check_button_size_allocate (GtkWidget     *widget,
225                                 GtkAllocation *allocation)
226 {
227   GtkCheckButton *check_button;
228   GtkButton *button;
229   GtkAllocation child_allocation;
230
231   g_return_if_fail (widget != NULL);
232   g_return_if_fail (GTK_IS_CHECK_BUTTON (widget));
233   g_return_if_fail (allocation != NULL);
234
235   check_button = GTK_CHECK_BUTTON (widget);
236   if (check_button->toggle_button.draw_indicator)
237     {
238       widget->allocation = *allocation;
239       if (GTK_WIDGET_REALIZED (widget))
240         gdk_window_move_resize (widget->window,
241                                 allocation->x, allocation->y,
242                                 allocation->width, allocation->height);
243
244       button = GTK_BUTTON (widget);
245
246       if (button->child && GTK_WIDGET_VISIBLE (button->child))
247         {
248           child_allocation.x = (GTK_CONTAINER (widget)->border_width +
249                                 CHECK_BUTTON_CLASS (widget)->indicator_size +
250                                 CHECK_BUTTON_CLASS (widget)->indicator_spacing * 3 + 1);
251           child_allocation.y = GTK_CONTAINER (widget)->border_width + 1;
252           child_allocation.width = (allocation->width - child_allocation.x  -
253                                     GTK_CONTAINER (widget)->border_width - 1);
254           child_allocation.height = allocation->height - child_allocation.y * 2;
255
256           gtk_widget_size_allocate (button->child, &child_allocation);
257         }
258     }
259   else
260     {
261       if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
262         (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);
263     }
264 }
265
266 static gint
267 gtk_check_button_expose (GtkWidget      *widget,
268                          GdkEventExpose *event)
269 {
270   GtkButton *button;
271   GtkCheckButton *check_button;
272   GdkEventExpose child_event;
273
274   g_return_val_if_fail (widget != NULL, FALSE);
275   g_return_val_if_fail (GTK_IS_CHECK_BUTTON (widget), FALSE);
276   g_return_val_if_fail (event != NULL, FALSE);
277
278   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
279     {
280       check_button = GTK_CHECK_BUTTON (widget);
281
282       if (check_button->toggle_button.draw_indicator)
283         {
284           button = GTK_BUTTON (widget);
285
286           gtk_check_button_draw_indicator (check_button, &event->area);
287
288           child_event = *event;
289           if (button->child && GTK_WIDGET_NO_WINDOW (button->child) &&
290               gtk_widget_intersect (button->child, &event->area, &child_event.area))
291             gtk_widget_event (button->child, (GdkEvent*) &child_event);
292
293           gtk_widget_draw_focus (widget);
294         }
295       else
296         {
297           if (GTK_WIDGET_CLASS (parent_class)->expose_event)
298             (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
299         }
300     }
301
302   return FALSE;
303 }
304
305
306 static void
307 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
308                                  GdkRectangle   *area)
309 {
310   GtkCheckButtonClass *class;
311
312   g_return_if_fail (check_button != NULL);
313   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
314   g_return_if_fail (CHECK_BUTTON_CLASS (check_button) != NULL);
315
316   class = CHECK_BUTTON_CLASS (check_button);
317
318   if (class->draw_indicator)
319     (* class->draw_indicator) (check_button, area);
320 }
321
322 static void
323 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
324                                       GdkRectangle    *area)
325 {
326   GtkWidget *widget;
327   GtkToggleButton *toggle_button;
328   GtkStateType state_type;
329   GtkShadowType shadow_type;
330   GdkRectangle restrict_area;
331   GdkRectangle new_area;
332   gint width, height;
333   gint x, y;
334
335   g_return_if_fail (check_button != NULL);
336   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
337
338   if (GTK_WIDGET_DRAWABLE (check_button))
339     {
340       widget = GTK_WIDGET (check_button);
341       toggle_button = GTK_TOGGLE_BUTTON (check_button);
342
343       state_type = GTK_WIDGET_STATE (widget);
344       if ((state_type != GTK_STATE_NORMAL) &&
345           (state_type != GTK_STATE_PRELIGHT))
346         state_type = GTK_STATE_NORMAL;
347
348       restrict_area.x = GTK_CONTAINER (widget)->border_width;
349       restrict_area.y = restrict_area.x;
350       restrict_area.width = widget->allocation.width - restrict_area.x * 2;
351       restrict_area.height = widget->allocation.height - restrict_area.x * 2;
352
353       if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
354         {
355           gtk_style_set_background (widget->style, widget->window, state_type);
356           gdk_window_clear_area (widget->window, new_area.x, new_area.y,
357                                  new_area.width, new_area.height);
358         }
359       
360       x = CHECK_BUTTON_CLASS (widget)->indicator_spacing + GTK_CONTAINER (widget)->border_width;
361       y = (widget->allocation.height - CHECK_BUTTON_CLASS (widget)->indicator_size) / 2;
362       width = CHECK_BUTTON_CLASS (widget)->indicator_size;
363       height = CHECK_BUTTON_CLASS (widget)->indicator_size;
364
365       if (GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE)
366         shadow_type = GTK_SHADOW_IN;
367       else if ((GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT) && toggle_button->active)
368         shadow_type = GTK_SHADOW_IN;
369       else
370         shadow_type = GTK_SHADOW_OUT;
371
372       gdk_draw_rectangle (widget->window,
373                           widget->style->bg_gc[GTK_WIDGET_STATE (widget)],
374                           TRUE, x + 1, y + 1, width, height);
375       gtk_draw_shadow (widget->style, widget->window,
376                        GTK_WIDGET_STATE (widget), shadow_type,
377                        x + 1, y + 1, width, height);
378     }
379 }