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