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