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