]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
dc3d8c90da795e7b07e16677d3d8533748f451e8
[~andy/gtk] / gtk / gtktogglebutton.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 "gtklabel.h"
28 #include "gtkmain.h"
29 #include "gtksignal.h"
30 #include "gtktogglebutton.h"
31 #include "gtkintl.h"
32
33 #define DEFAULT_LEFT_POS  4
34 #define DEFAULT_TOP_POS   4
35 #define DEFAULT_SPACING   7
36
37 enum {
38   TOGGLED,
39   LAST_SIGNAL
40 };
41
42 enum {
43   PROP_0,
44   PROP_ACTIVE,
45   PROP_INCONSISTENT,
46   PROP_DRAW_INDICATOR
47 };
48
49
50 static void gtk_toggle_button_class_init    (GtkToggleButtonClass *klass);
51 static void gtk_toggle_button_init          (GtkToggleButton      *toggle_button);
52 static void gtk_toggle_button_paint         (GtkWidget            *widget,
53                                              GdkRectangle         *area);
54 static gint gtk_toggle_button_expose        (GtkWidget            *widget,
55                                              GdkEventExpose       *event);
56 static void gtk_toggle_button_pressed       (GtkButton            *button);
57 static void gtk_toggle_button_released      (GtkButton            *button);
58 static void gtk_toggle_button_clicked       (GtkButton            *button);
59 static void gtk_toggle_button_set_property  (GObject              *object,
60                                              guint                 prop_id,
61                                              const GValue         *value,
62                                              GParamSpec           *pspec);
63 static void gtk_toggle_button_get_property  (GObject              *object,
64                                              guint                 prop_id,
65                                              GValue               *value,
66                                              GParamSpec           *pspec);
67 static void gtk_toggle_button_update_state  (GtkButton            *button);
68
69 static guint toggle_button_signals[LAST_SIGNAL] = { 0 };
70 static GtkContainerClass *parent_class = NULL;
71
72 GtkType
73 gtk_toggle_button_get_type (void)
74 {
75   static GtkType toggle_button_type = 0;
76
77   if (!toggle_button_type)
78     {
79       static const GtkTypeInfo toggle_button_info =
80       {
81         "GtkToggleButton",
82         sizeof (GtkToggleButton),
83         sizeof (GtkToggleButtonClass),
84         (GtkClassInitFunc) gtk_toggle_button_class_init,
85         (GtkObjectInitFunc) gtk_toggle_button_init,
86         /* reserved_1 */ NULL,
87         /* reserved_2 */ NULL,
88         (GtkClassInitFunc) NULL,
89       };
90
91       toggle_button_type = gtk_type_unique (GTK_TYPE_BUTTON, &toggle_button_info);
92     }
93
94   return toggle_button_type;
95 }
96
97 static void
98 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
99 {
100   GtkObjectClass *object_class;
101   GObjectClass   *gobject_class;
102   GtkWidgetClass *widget_class;
103   GtkContainerClass *container_class;
104   GtkButtonClass *button_class;
105
106   object_class = (GtkObjectClass*) class;
107   gobject_class = G_OBJECT_CLASS (class);
108   widget_class = (GtkWidgetClass*) class;
109   container_class = (GtkContainerClass*) class;
110   button_class = (GtkButtonClass*) class;
111
112   parent_class = gtk_type_class (GTK_TYPE_BUTTON);
113
114
115   gobject_class->set_property = gtk_toggle_button_set_property;
116   gobject_class->get_property = gtk_toggle_button_get_property;
117
118   widget_class->expose_event = gtk_toggle_button_expose;
119
120   button_class->pressed = gtk_toggle_button_pressed;
121   button_class->released = gtk_toggle_button_released;
122   button_class->clicked = gtk_toggle_button_clicked;
123   button_class->enter = gtk_toggle_button_update_state;
124   button_class->leave = gtk_toggle_button_update_state;
125
126   class->toggled = NULL;
127
128   g_object_class_install_property (gobject_class,
129                                    PROP_ACTIVE,
130                                    g_param_spec_boolean ("active",
131                                                          _("Active"),
132                                                          _("If the toggle button should be pressed in or not"),
133                                                          FALSE,
134                                                          G_PARAM_READWRITE));
135
136   g_object_class_install_property (gobject_class,
137                                    PROP_INCONSISTENT,
138                                    g_param_spec_boolean ("inconsistent",
139                                                          _("Inconsistent"),
140                                                          _("If the toggle button is in an \"in between\" state."),
141                                                          FALSE,
142                                                          G_PARAM_READWRITE));
143
144   g_object_class_install_property (gobject_class,
145                                    PROP_DRAW_INDICATOR,
146                                    g_param_spec_boolean ("draw_indicator",
147                                                          _("Draw Indicator"),
148                                                          _("If the toggle part of the button is displayed"),
149                                                          FALSE,
150                                                          G_PARAM_READWRITE));
151
152   toggle_button_signals[TOGGLED] =
153     gtk_signal_new ("toggled",
154                     GTK_RUN_FIRST,
155                     GTK_CLASS_TYPE (object_class),
156                     GTK_SIGNAL_OFFSET (GtkToggleButtonClass, toggled),
157                     gtk_marshal_VOID__VOID,
158                     GTK_TYPE_NONE, 0);
159 }
160
161 static void
162 gtk_toggle_button_init (GtkToggleButton *toggle_button)
163 {
164   toggle_button->active = FALSE;
165   toggle_button->draw_indicator = FALSE;
166 }
167
168
169 GtkWidget*
170 gtk_toggle_button_new (void)
171 {
172   return GTK_WIDGET (gtk_type_new (gtk_toggle_button_get_type ()));
173 }
174
175 GtkWidget*
176 gtk_toggle_button_new_with_label (const gchar *label)
177 {
178   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
179 }
180
181 /**
182  * gtk_toggle_button_new_with_mnemonic:
183  * @label: the text of the button, with an underscore in front of the
184  *         mnemonic character
185  * @returns: a new #GtkToggleButton
186  *
187  * Creates a new #GtkToggleButton containing a label. The label
188  * will be created using gtk_label_new_with_mnemonic(), so underscores
189  * in @label indicate the mnemonic for the button.
190  **/
191 GtkWidget*
192 gtk_toggle_button_new_with_mnemonic (const gchar *label)
193 {
194   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
195 }
196
197 static void
198 gtk_toggle_button_set_property (GObject      *object,
199                                 guint         prop_id,
200                                 const GValue *value,
201                                 GParamSpec   *pspec)
202 {
203   GtkToggleButton *tb;
204
205   tb = GTK_TOGGLE_BUTTON (object);
206
207   switch (prop_id)
208     {
209     case PROP_ACTIVE:
210       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
211       break;
212     case PROP_INCONSISTENT:
213       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
214       break;
215     case PROP_DRAW_INDICATOR:
216       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
217       break;
218     default:
219       break;
220     }
221 }
222
223 static void
224 gtk_toggle_button_get_property (GObject      *object,
225                                 guint         prop_id,
226                                 GValue       *value,
227                                 GParamSpec   *pspec)
228 {
229   GtkToggleButton *tb;
230
231   tb = GTK_TOGGLE_BUTTON (object);
232
233   switch (prop_id)
234     {
235     case PROP_ACTIVE:
236       g_value_set_boolean (value, tb->active);
237       break;
238     case PROP_INCONSISTENT:
239       g_value_set_boolean (value, tb->inconsistent);
240       break;
241     case PROP_DRAW_INDICATOR:
242       g_value_set_boolean (value, tb->draw_indicator);
243       break;
244     default:
245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246       break;
247     }
248 }
249
250 void
251 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
252                             gboolean         draw_indicator)
253 {
254   GtkWidget *widget;
255
256   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
257
258   widget = GTK_WIDGET (toggle_button);
259
260   draw_indicator = draw_indicator ? TRUE : FALSE;
261
262   if (toggle_button->draw_indicator != draw_indicator)
263     {
264       toggle_button->draw_indicator = draw_indicator;
265
266       if (GTK_WIDGET_VISIBLE (toggle_button))
267         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
268
269       g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
270     }
271 }
272
273 /**
274  * gtk_toggle_button_get_mode:
275  * @toggle_button: a #GtkToggleButton
276  *
277  * Retrieves whether the button is displayed as a separate indicator
278  * and label. See gtk_toggle_button_set_mode().
279  *
280  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
281  *   and label.
282  **/
283 gboolean
284 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
285 {
286   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
287
288   return toggle_button->draw_indicator;
289 }
290
291 void
292 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
293                               gboolean         is_active)
294 {
295   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
296
297   is_active = is_active != FALSE;
298
299   if (toggle_button->active != is_active)
300     gtk_button_clicked (GTK_BUTTON (toggle_button));
301 }
302
303
304 gboolean
305 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
306 {
307   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
308
309   return (toggle_button->active) ? TRUE : FALSE;
310 }
311
312
313 void
314 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
315 {
316   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
317
318   gtk_signal_emit (GTK_OBJECT (toggle_button), toggle_button_signals[TOGGLED]);
319 }
320
321 /**
322  * gtk_toggle_button_set_inconsistent:
323  * @toggle_button: a #GtkToggleButton
324  * @setting: %TRUE if state is inconsistent
325  *
326  * If the user has selected a range of elements (such as some text or
327  * spreadsheet cells) that are affected by a toggle button, and the
328  * current values in that range are inconsistent, you may want to
329  * display the toggle in an "in between" state. This function turns on
330  * "in between" display.  Normally you would turn off the inconsistent
331  * state again if the user toggles the toggle button. This has to be
332  * done manually, gtk_toggle_button_set_inconsistent() only affects
333  * visual appearance, it doesn't affect the semantics of the button.
334  * 
335  **/
336 void
337 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
338                                     gboolean         setting)
339 {
340   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
341   
342   setting = setting != FALSE;
343
344   if (setting != toggle_button->inconsistent)
345     {
346       toggle_button->inconsistent = setting;
347       
348       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
349       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
350
351       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
352     }
353 }
354
355 /**
356  * gtk_toggle_button_get_inconsistent:
357  * @toggle_button: a #GtkToggleButton
358  * 
359  * Gets the value set by gtk_toggle_button_set_inconsistent().
360  * 
361  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
362  **/
363 gboolean
364 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
365 {
366   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
367
368   return toggle_button->inconsistent;
369 }
370
371 static void
372 gtk_toggle_button_paint (GtkWidget    *widget,
373                          GdkRectangle *area)
374 {
375   GtkButton *button;
376   GtkToggleButton *toggle_button;
377   GtkShadowType shadow_type;
378   GtkStateType state_type;
379   gint width, height;
380   gboolean interior_focus;
381   gint border_width;
382   gint x, y;
383
384   button = GTK_BUTTON (widget);
385   toggle_button = GTK_TOGGLE_BUTTON (widget);
386
387   if (GTK_WIDGET_DRAWABLE (widget))
388     {
389       border_width = GTK_CONTAINER (widget)->border_width;
390       
391       gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
392       
393       x = widget->allocation.x + border_width;
394       y = widget->allocation.y + border_width;
395       width = widget->allocation.width - border_width * 2;
396       height = widget->allocation.height - border_width * 2;
397
398       if (GTK_WIDGET_HAS_DEFAULT (widget) &&
399           GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
400         {
401           gtk_paint_box (widget->style, widget->window,
402                          GTK_STATE_NORMAL, GTK_SHADOW_IN,
403                          area, widget, "togglebuttondefault",
404                          x, y, width, height);
405         }
406
407       if (GTK_WIDGET_CAN_DEFAULT (widget))
408         {
409           x += widget->style->xthickness;
410           y += widget->style->ythickness;
411           width -= 2 * x + DEFAULT_SPACING;
412           height -= 2 * y + DEFAULT_SPACING;
413           x += DEFAULT_LEFT_POS;
414           y += DEFAULT_TOP_POS;
415         }
416
417       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
418         {
419           x += 1;
420           y += 1;
421           width -= 2;
422           height -= 2;
423         }
424
425       state_type = GTK_WIDGET_STATE (widget);
426       
427       if (toggle_button->inconsistent)
428         {
429           if (state_type == GTK_STATE_ACTIVE)
430             state_type = GTK_STATE_NORMAL;
431           shadow_type = GTK_SHADOW_ETCHED_IN;
432         }
433       else
434         shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
435
436       if (button->relief != GTK_RELIEF_NONE ||
437           (GTK_WIDGET_STATE(widget) != GTK_STATE_NORMAL &&
438            GTK_WIDGET_STATE(widget) != GTK_STATE_INSENSITIVE))
439         gtk_paint_box (widget->style, widget->window,
440                        state_type,
441                        shadow_type, area, widget, "togglebutton",
442                        x, y, width, height);
443       
444       if (GTK_WIDGET_HAS_FOCUS (widget))
445         {
446           if (interior_focus)
447             {
448               x += widget->style->xthickness + 1;
449               y += widget->style->xthickness + 1;
450               width -= 2 * (widget->style->xthickness + 1);
451               height -= 2 * (widget->style->ythickness + 1);
452             }
453           else
454             {
455               x -= 1;
456               y -= 1;
457               width += 2;
458               height += 2;
459             }
460
461           gtk_paint_focus (widget->style, widget->window,
462                            area, widget, "togglebutton",
463                            x, y, width - 1, height - 1);
464         }
465     }
466 }
467
468 static gint
469 gtk_toggle_button_expose (GtkWidget      *widget,
470                           GdkEventExpose *event)
471 {
472   if (GTK_WIDGET_DRAWABLE (widget))
473     {
474       GtkWidget *child = GTK_BIN (widget)->child;
475
476       gtk_toggle_button_paint (widget, &event->area);
477
478       if (child)
479         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
480     }
481   
482   return TRUE;
483 }
484
485 static void
486 gtk_toggle_button_pressed (GtkButton *button)
487 {
488   button->button_down = TRUE;
489
490   gtk_toggle_button_update_state (button);
491 }
492
493 static void
494 gtk_toggle_button_released (GtkButton *button)
495 {
496   if (button->button_down)
497     {
498       button->button_down = FALSE;
499
500       if (button->in_button)
501         gtk_button_clicked (button);
502
503       gtk_toggle_button_update_state (button);
504     }
505 }
506
507 static void
508 gtk_toggle_button_clicked (GtkButton *button)
509 {
510   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
511   toggle_button->active = !toggle_button->active;
512
513   gtk_toggle_button_toggled (toggle_button);
514
515   gtk_toggle_button_update_state (button);
516
517   g_object_notify (G_OBJECT (toggle_button), "active");
518 }
519
520 static void
521 gtk_toggle_button_update_state (GtkButton *button)
522 {
523   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
524   gboolean depressed;
525   GtkStateType new_state;
526
527   if (toggle_button->inconsistent)
528     depressed = FALSE;
529   else if (button->in_button && button->button_down)
530     depressed = !toggle_button->active;
531   else
532     depressed = toggle_button->active;
533       
534   if (!button->button_down && button->in_button)
535     new_state = GTK_STATE_PRELIGHT;
536   else
537     new_state = depressed ? GTK_STATE_ACTIVE: GTK_STATE_NORMAL;
538
539   _gtk_button_set_depressed (button, depressed); 
540   gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
541 }