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