]> Pileus Git - ~andy/gtk/blob - gtk/gtkcheckbutton.c
a9e34b29d2390f050b8129d849c89f119c07e447
[~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 "config.h"
28 #include "gtkcheckbutton.h"
29 #include "gtklabel.h"
30 #include "gtkprivate.h"
31 #include "gtkintl.h"
32 #include "gtkalias.h"
33
34
35 #define INDICATOR_SIZE     13
36 #define INDICATOR_SPACING  2
37
38
39 static void gtk_check_button_size_request        (GtkWidget           *widget,
40                                                   GtkRequisition      *requisition);
41 static void gtk_check_button_size_allocate       (GtkWidget           *widget,
42                                                   GtkAllocation       *allocation);
43 static gint gtk_check_button_expose              (GtkWidget           *widget,
44                                                   GdkEventExpose      *event);
45 static void gtk_check_button_paint               (GtkWidget           *widget,
46                                                   GdkRectangle        *area);
47 static void gtk_check_button_draw_indicator      (GtkCheckButton      *check_button,
48                                                   GdkRectangle        *area);
49 static void gtk_real_check_button_draw_indicator (GtkCheckButton      *check_button,
50                                                   GdkRectangle        *area);
51
52 G_DEFINE_TYPE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON)
53
54 static void
55 gtk_check_button_class_init (GtkCheckButtonClass *class)
56 {
57   GtkWidgetClass *widget_class;
58   
59   widget_class = (GtkWidgetClass*) class;
60   
61   widget_class->size_request = gtk_check_button_size_request;
62   widget_class->size_allocate = gtk_check_button_size_allocate;
63   widget_class->expose_event = gtk_check_button_expose;
64
65   class->draw_indicator = gtk_real_check_button_draw_indicator;
66
67   gtk_widget_class_install_style_property (widget_class,
68                                            g_param_spec_int ("indicator-size",
69                                                              P_("Indicator Size"),
70                                                              P_("Size of check or radio indicator"),
71                                                              0,
72                                                              G_MAXINT,
73                                                              INDICATOR_SIZE,
74                                                              GTK_PARAM_READABLE));
75   gtk_widget_class_install_style_property (widget_class,
76                                            g_param_spec_int ("indicator-spacing",
77                                                              P_("Indicator Spacing"),
78                                                              P_("Spacing around check or radio indicator"),
79                                                              0,
80                                                              G_MAXINT,
81                                                              INDICATOR_SPACING,
82                                                              GTK_PARAM_READABLE));
83 }
84
85 static void
86 gtk_check_button_init (GtkCheckButton *check_button)
87 {
88   GTK_WIDGET_SET_FLAGS (check_button, GTK_NO_WINDOW);
89   GTK_WIDGET_UNSET_FLAGS (check_button, GTK_RECEIVES_DEFAULT);
90   GTK_TOGGLE_BUTTON (check_button)->draw_indicator = TRUE;
91   GTK_BUTTON (check_button)->depress_on_activate = FALSE;
92 }
93
94 GtkWidget*
95 gtk_check_button_new (void)
96 {
97   return g_object_new (GTK_TYPE_CHECK_BUTTON, NULL);
98 }
99
100
101 GtkWidget*
102 gtk_check_button_new_with_label (const gchar *label)
103 {
104   return g_object_new (GTK_TYPE_CHECK_BUTTON, "label", label, NULL);
105 }
106
107 /**
108  * gtk_check_button_new_with_mnemonic:
109  * @label: The text of the button, with an underscore in front of the
110  *         mnemonic character
111  * @returns: a new #GtkCheckButton
112  *
113  * Creates a new #GtkCheckButton containing a label. The label
114  * will be created using gtk_label_new_with_mnemonic(), so underscores
115  * in @label indicate the mnemonic for the check button.
116  **/
117 GtkWidget*
118 gtk_check_button_new_with_mnemonic (const gchar *label)
119 {
120   return g_object_new (GTK_TYPE_CHECK_BUTTON, 
121                        "label", label, 
122                        "use-underline", TRUE, 
123                        NULL);
124 }
125
126
127 /* This should only be called when toggle_button->draw_indicator
128  * is true.
129  */
130 static void
131 gtk_check_button_paint (GtkWidget    *widget,
132                         GdkRectangle *area)
133 {
134   GtkCheckButton *check_button = GTK_CHECK_BUTTON (widget);
135   
136   if (gtk_widget_is_drawable (widget))
137     {
138       gint border_width;
139       gint interior_focus;
140       gint focus_width;
141       gint focus_pad;
142           
143       gtk_widget_style_get (widget,
144                             "interior-focus", &interior_focus,
145                             "focus-line-width", &focus_width,
146                             "focus-padding", &focus_pad,
147                             NULL);
148
149       gtk_check_button_draw_indicator (check_button, area);
150       
151       border_width = GTK_CONTAINER (widget)->border_width;
152       if (gtk_widget_has_focus (widget))
153         {
154           GtkWidget *child = GTK_BIN (widget)->child;
155           
156           if (interior_focus && child && GTK_WIDGET_VISIBLE (child))
157             gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
158                              area, widget, "checkbutton",
159                              child->allocation.x - focus_width - focus_pad,
160                              child->allocation.y - focus_width - focus_pad,
161                              child->allocation.width + 2 * (focus_width + focus_pad),
162                              child->allocation.height + 2 * (focus_width + focus_pad));
163           else
164             gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
165                              area, widget, "checkbutton",
166                              border_width + widget->allocation.x,
167                              border_width + widget->allocation.y,
168                              widget->allocation.width - 2 * border_width,
169                              widget->allocation.height - 2 * border_width);
170         }
171     }
172 }
173
174 void
175 _gtk_check_button_get_props (GtkCheckButton *check_button,
176                              gint           *indicator_size,
177                              gint           *indicator_spacing)
178 {
179   GtkWidget *widget =  GTK_WIDGET (check_button);
180
181   if (indicator_size)
182     gtk_widget_style_get (widget, "indicator-size", indicator_size, NULL);
183
184   if (indicator_spacing)
185     gtk_widget_style_get (widget, "indicator-spacing", indicator_spacing, NULL);
186 }
187
188 static void
189 gtk_check_button_size_request (GtkWidget      *widget,
190                                GtkRequisition *requisition)
191 {
192   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget);
193   
194   if (toggle_button->draw_indicator)
195     {
196       GtkWidget *child;
197       gint temp;
198       gint indicator_size;
199       gint indicator_spacing;
200       gint border_width = GTK_CONTAINER (widget)->border_width;
201       gint focus_width;
202       gint focus_pad;
203
204       gtk_widget_style_get (GTK_WIDGET (widget),
205                             "focus-line-width", &focus_width,
206                             "focus-padding", &focus_pad,
207                             NULL);
208  
209       requisition->width = border_width * 2;
210       requisition->height = border_width * 2;
211
212       _gtk_check_button_get_props (GTK_CHECK_BUTTON (widget),
213                                    &indicator_size, &indicator_spacing);
214       
215       child = GTK_BIN (widget)->child;
216       if (child && GTK_WIDGET_VISIBLE (child))
217         {
218           GtkRequisition child_requisition;
219           
220           gtk_widget_size_request (child, &child_requisition);
221
222           requisition->width += child_requisition.width + indicator_spacing;
223           requisition->height += child_requisition.height;
224         }
225       
226       requisition->width += (indicator_size + indicator_spacing * 2 + 2 * (focus_width + focus_pad));
227       
228       temp = indicator_size + indicator_spacing * 2;
229       requisition->height = MAX (requisition->height, temp) + 2 * (focus_width + focus_pad);
230     }
231   else
232     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_request (widget, requisition);
233 }
234
235 static void
236 gtk_check_button_size_allocate (GtkWidget     *widget,
237                                 GtkAllocation *allocation)
238 {
239   GtkCheckButton *check_button;
240   GtkToggleButton *toggle_button;
241   GtkButton *button;
242   GtkAllocation child_allocation;
243
244   button = GTK_BUTTON (widget);
245   check_button = GTK_CHECK_BUTTON (widget);
246   toggle_button = GTK_TOGGLE_BUTTON (widget);
247
248   if (toggle_button->draw_indicator)
249     {
250       gint indicator_size;
251       gint indicator_spacing;
252       gint focus_width;
253       gint focus_pad;
254       
255       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
256       gtk_widget_style_get (widget,
257                             "focus-line-width", &focus_width,
258                             "focus-padding", &focus_pad,
259                             NULL);
260                                                     
261       widget->allocation = *allocation;
262       if (GTK_WIDGET_REALIZED (widget))
263         gdk_window_move_resize (button->event_window,
264                                 allocation->x, allocation->y,
265                                 allocation->width, allocation->height);
266       
267       if (GTK_BIN (button)->child && GTK_WIDGET_VISIBLE (GTK_BIN (button)->child))
268         {
269           GtkRequisition child_requisition;
270           gint border_width = GTK_CONTAINER (widget)->border_width;
271
272           gtk_widget_get_child_requisition (GTK_BIN (button)->child, &child_requisition);
273  
274           child_allocation.width = MIN (child_requisition.width,
275                                         allocation->width -
276                                         ((border_width + focus_width + focus_pad) * 2
277                                          + indicator_size + indicator_spacing * 3));
278           child_allocation.width = MAX (child_allocation.width, 1);
279
280           child_allocation.height = MIN (child_requisition.height,
281                                          allocation->height - (border_width + focus_width + focus_pad) * 2);
282           child_allocation.height = MAX (child_allocation.height, 1);
283           
284           child_allocation.x = (border_width + indicator_size + indicator_spacing * 3 +
285                                 widget->allocation.x + focus_width + focus_pad);
286           child_allocation.y = widget->allocation.y +
287                   (allocation->height - child_allocation.height) / 2;
288
289           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
290             child_allocation.x = allocation->x + allocation->width
291               - (child_allocation.x - allocation->x + child_allocation.width);
292           
293           gtk_widget_size_allocate (GTK_BIN (button)->child, &child_allocation);
294         }
295     }
296   else
297     GTK_WIDGET_CLASS (gtk_check_button_parent_class)->size_allocate (widget, allocation);
298 }
299
300 static gint
301 gtk_check_button_expose (GtkWidget      *widget,
302                          GdkEventExpose *event)
303 {
304   GtkToggleButton *toggle_button;
305   GtkBin *bin;
306   
307   toggle_button = GTK_TOGGLE_BUTTON (widget);
308   bin = GTK_BIN (widget);
309   
310   if (gtk_widget_is_drawable (widget))
311     {
312       if (toggle_button->draw_indicator)
313         {
314           gtk_check_button_paint (widget, &event->area);
315           
316           if (bin->child)
317             gtk_container_propagate_expose (GTK_CONTAINER (widget),
318                                             bin->child,
319                                             event);
320         }
321       else if (GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event)
322         GTK_WIDGET_CLASS (gtk_check_button_parent_class)->expose_event (widget, event);
323     }
324   
325   return FALSE;
326 }
327
328
329 static void
330 gtk_check_button_draw_indicator (GtkCheckButton *check_button,
331                                  GdkRectangle   *area)
332 {
333   GtkCheckButtonClass *class;
334   
335   g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button));
336   
337   class = GTK_CHECK_BUTTON_GET_CLASS (check_button);
338
339   if (class->draw_indicator)
340     class->draw_indicator (check_button, area);
341 }
342
343 static void
344 gtk_real_check_button_draw_indicator (GtkCheckButton *check_button,
345                                       GdkRectangle   *area)
346 {
347   GtkWidget *widget;
348   GtkWidget *child;
349   GtkButton *button;
350   GtkToggleButton *toggle_button;
351   GtkStateType state_type;
352   GtkShadowType shadow_type;
353   gint x, y;
354   gint indicator_size;
355   gint indicator_spacing;
356   gint focus_width;
357   gint focus_pad;
358   gboolean interior_focus;
359
360   widget = GTK_WIDGET (check_button);
361
362   if (gtk_widget_is_drawable (widget))
363     {
364       button = GTK_BUTTON (check_button);
365       toggle_button = GTK_TOGGLE_BUTTON (check_button);
366   
367       gtk_widget_style_get (widget, 
368                             "interior-focus", &interior_focus,
369                             "focus-line-width", &focus_width, 
370                             "focus-padding", &focus_pad, 
371                             NULL);
372
373       _gtk_check_button_get_props (check_button, &indicator_size, &indicator_spacing);
374
375       x = widget->allocation.x + indicator_spacing + GTK_CONTAINER (widget)->border_width;
376       y = widget->allocation.y + (widget->allocation.height - indicator_size) / 2;
377
378       child = GTK_BIN (check_button)->child;
379       if (!interior_focus || !(child && GTK_WIDGET_VISIBLE (child)))
380         x += focus_width + focus_pad;      
381
382       if (toggle_button->inconsistent)
383         shadow_type = GTK_SHADOW_ETCHED_IN;
384       else if (toggle_button->active)
385         shadow_type = GTK_SHADOW_IN;
386       else
387         shadow_type = GTK_SHADOW_OUT;
388
389       if (button->activate_timeout || (button->button_down && button->in_button))
390         state_type = GTK_STATE_ACTIVE;
391       else if (button->in_button)
392         state_type = GTK_STATE_PRELIGHT;
393       else if (!gtk_widget_is_sensitive (widget))
394         state_type = GTK_STATE_INSENSITIVE;
395       else
396         state_type = GTK_STATE_NORMAL;
397       
398       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
399         x = widget->allocation.x + widget->allocation.width - (indicator_size + x - widget->allocation.x);
400
401       if (GTK_WIDGET_STATE (toggle_button) == GTK_STATE_PRELIGHT)
402         {
403           GdkRectangle restrict_area;
404           GdkRectangle new_area;
405               
406           restrict_area.x = widget->allocation.x + GTK_CONTAINER (widget)->border_width;
407           restrict_area.y = widget->allocation.y + GTK_CONTAINER (widget)->border_width;
408           restrict_area.width = widget->allocation.width - (2 * GTK_CONTAINER (widget)->border_width);
409           restrict_area.height = widget->allocation.height - (2 * GTK_CONTAINER (widget)->border_width);
410           
411           if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
412             {
413               gtk_paint_flat_box (widget->style, widget->window, GTK_STATE_PRELIGHT,
414                                   GTK_SHADOW_ETCHED_OUT, 
415                                   area, widget, "checkbutton",
416                                   new_area.x, new_area.y,
417                                   new_area.width, new_area.height);
418             }
419         }
420
421       gtk_paint_check (widget->style, widget->window,
422                        state_type, shadow_type,
423                        area, widget, "checkbutton",
424                        x, y, indicator_size, indicator_size);
425     }
426 }
427
428 #define __GTK_CHECK_BUTTON_C__
429 #include "gtkaliasdef.c"