]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
The first part of the fix for #114351 (see also gdk-pixbuf/ChangeLog and
[~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 "gtkmarshalers.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 gint gtk_toggle_button_expose        (GtkWidget            *widget,
53                                              GdkEventExpose       *event);
54 static gboolean gtk_toggle_button_mnemonic_activate  (GtkWidget            *widget,
55                                                       gboolean              group_cycling);
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 GType
73 gtk_toggle_button_get_type (void)
74 {
75   static GType toggle_button_type = 0;
76
77   if (!toggle_button_type)
78     {
79       static const GTypeInfo toggle_button_info =
80       {
81         sizeof (GtkToggleButtonClass),
82         NULL,           /* base_init */
83         NULL,           /* base_finalize */
84         (GClassInitFunc) gtk_toggle_button_class_init,
85         NULL,           /* class_finalize */
86         NULL,           /* class_data */
87         sizeof (GtkToggleButton),
88         0,              /* n_preallocs */
89         (GInstanceInitFunc) gtk_toggle_button_init,
90       };
91
92       toggle_button_type =
93         g_type_register_static (GTK_TYPE_BUTTON, "GtkToggleButton",
94                                 &toggle_button_info, 0);
95     }
96
97   return toggle_button_type;
98 }
99
100 static void
101 gtk_toggle_button_class_init (GtkToggleButtonClass *class)
102 {
103   GObjectClass *gobject_class;
104   GtkWidgetClass *widget_class;
105   GtkContainerClass *container_class;
106   GtkButtonClass *button_class;
107
108   gobject_class = G_OBJECT_CLASS (class);
109   widget_class = (GtkWidgetClass*) class;
110   container_class = (GtkContainerClass*) class;
111   button_class = (GtkButtonClass*) class;
112
113   parent_class = g_type_class_peek_parent (class);
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   widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate;
120
121   button_class->pressed = gtk_toggle_button_pressed;
122   button_class->released = gtk_toggle_button_released;
123   button_class->clicked = gtk_toggle_button_clicked;
124   button_class->enter = gtk_toggle_button_update_state;
125   button_class->leave = gtk_toggle_button_update_state;
126
127   class->toggled = NULL;
128
129   g_object_class_install_property (gobject_class,
130                                    PROP_ACTIVE,
131                                    g_param_spec_boolean ("active",
132                                                          P_("Active"),
133                                                          P_("If the toggle button should be pressed in or not"),
134                                                          FALSE,
135                                                          G_PARAM_READWRITE));
136
137   g_object_class_install_property (gobject_class,
138                                    PROP_INCONSISTENT,
139                                    g_param_spec_boolean ("inconsistent",
140                                                          P_("Inconsistent"),
141                                                          P_("If the toggle button is in an \"in between\" state"),
142                                                          FALSE,
143                                                          G_PARAM_READWRITE));
144
145   g_object_class_install_property (gobject_class,
146                                    PROP_DRAW_INDICATOR,
147                                    g_param_spec_boolean ("draw_indicator",
148                                                          P_("Draw Indicator"),
149                                                          P_("If the toggle part of the button is displayed"),
150                                                          FALSE,
151                                                          G_PARAM_READWRITE));
152
153   toggle_button_signals[TOGGLED] =
154     g_signal_new ("toggled",
155                   G_OBJECT_CLASS_TYPE (gobject_class),
156                   G_SIGNAL_RUN_FIRST,
157                   G_STRUCT_OFFSET (GtkToggleButtonClass, toggled),
158                   NULL, NULL,
159                   _gtk_marshal_VOID__VOID,
160                   G_TYPE_NONE, 0);
161 }
162
163 static void
164 gtk_toggle_button_init (GtkToggleButton *toggle_button)
165 {
166   toggle_button->active = FALSE;
167   toggle_button->draw_indicator = FALSE;
168   GTK_BUTTON (toggle_button)->depress_on_activate = TRUE;
169 }
170
171
172 GtkWidget*
173 gtk_toggle_button_new (void)
174 {
175   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
176 }
177
178 GtkWidget*
179 gtk_toggle_button_new_with_label (const gchar *label)
180 {
181   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
182 }
183
184 /**
185  * gtk_toggle_button_new_with_mnemonic:
186  * @label: the text of the button, with an underscore in front of the
187  *         mnemonic character
188  * @returns: a new #GtkToggleButton
189  *
190  * Creates a new #GtkToggleButton containing a label. The label
191  * will be created using gtk_label_new_with_mnemonic(), so underscores
192  * in @label indicate the mnemonic for the button.
193  **/
194 GtkWidget*
195 gtk_toggle_button_new_with_mnemonic (const gchar *label)
196 {
197   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, "use_underline", TRUE, NULL);
198 }
199
200 static void
201 gtk_toggle_button_set_property (GObject      *object,
202                                 guint         prop_id,
203                                 const GValue *value,
204                                 GParamSpec   *pspec)
205 {
206   GtkToggleButton *tb;
207
208   tb = GTK_TOGGLE_BUTTON (object);
209
210   switch (prop_id)
211     {
212     case PROP_ACTIVE:
213       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
214       break;
215     case PROP_INCONSISTENT:
216       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
217       break;
218     case PROP_DRAW_INDICATOR:
219       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
220       break;
221     default:
222       break;
223     }
224 }
225
226 static void
227 gtk_toggle_button_get_property (GObject      *object,
228                                 guint         prop_id,
229                                 GValue       *value,
230                                 GParamSpec   *pspec)
231 {
232   GtkToggleButton *tb;
233
234   tb = GTK_TOGGLE_BUTTON (object);
235
236   switch (prop_id)
237     {
238     case PROP_ACTIVE:
239       g_value_set_boolean (value, tb->active);
240       break;
241     case PROP_INCONSISTENT:
242       g_value_set_boolean (value, tb->inconsistent);
243       break;
244     case PROP_DRAW_INDICATOR:
245       g_value_set_boolean (value, tb->draw_indicator);
246       break;
247     default:
248       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249       break;
250     }
251 }
252
253 /**
254  * gtk_toggle_button_set_mode:
255  * @toggle_button: a #GtkToggleButton
256  * @draw_indicator: if %TRUE, draw the button as a separate indicator
257  * and label; if %FALSE, draw the button like a normal button
258  *
259  * Sets whether the button is displayed as a separate indicator and label.
260  * You can call this function on a checkbutton or a radiobutton with
261  * @draw_indicator = %FALSE to make the button look like a normal button
262  *
263  * This function only effects instances of classes like #GtkCheckButton
264  * and #GtkRadioButton that derive from #GtkToggleButton,
265  * not instances of #GtkToggleButton itself.
266  */
267 void
268 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
269                             gboolean         draw_indicator)
270 {
271   GtkWidget *widget;
272
273   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
274
275   widget = GTK_WIDGET (toggle_button);
276
277   draw_indicator = draw_indicator ? TRUE : FALSE;
278
279   if (toggle_button->draw_indicator != draw_indicator)
280     {
281       toggle_button->draw_indicator = draw_indicator;
282       GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
283       
284       if (GTK_WIDGET_VISIBLE (toggle_button))
285         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
286
287       g_object_notify (G_OBJECT (toggle_button), "draw_indicator");
288     }
289 }
290
291 /**
292  * gtk_toggle_button_get_mode:
293  * @toggle_button: a #GtkToggleButton
294  *
295  * Retrieves whether the button is displayed as a separate indicator
296  * and label. See gtk_toggle_button_set_mode().
297  *
298  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
299  *   and label.
300  **/
301 gboolean
302 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
303 {
304   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
305
306   return toggle_button->draw_indicator;
307 }
308
309 void
310 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
311                               gboolean         is_active)
312 {
313   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
314
315   is_active = is_active != FALSE;
316
317   if (toggle_button->active != is_active)
318     gtk_button_clicked (GTK_BUTTON (toggle_button));
319 }
320
321
322 gboolean
323 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
324 {
325   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
326
327   return (toggle_button->active) ? TRUE : FALSE;
328 }
329
330
331 void
332 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
333 {
334   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
335
336   g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
337 }
338
339 /**
340  * gtk_toggle_button_set_inconsistent:
341  * @toggle_button: a #GtkToggleButton
342  * @setting: %TRUE if state is inconsistent
343  *
344  * If the user has selected a range of elements (such as some text or
345  * spreadsheet cells) that are affected by a toggle button, and the
346  * current values in that range are inconsistent, you may want to
347  * display the toggle in an "in between" state. This function turns on
348  * "in between" display.  Normally you would turn off the inconsistent
349  * state again if the user toggles the toggle button. This has to be
350  * done manually, gtk_toggle_button_set_inconsistent() only affects
351  * visual appearance, it doesn't affect the semantics of the button.
352  * 
353  **/
354 void
355 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
356                                     gboolean         setting)
357 {
358   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
359   
360   setting = setting != FALSE;
361
362   if (setting != toggle_button->inconsistent)
363     {
364       toggle_button->inconsistent = setting;
365       
366       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
367       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
368
369       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
370     }
371 }
372
373 /**
374  * gtk_toggle_button_get_inconsistent:
375  * @toggle_button: a #GtkToggleButton
376  * 
377  * Gets the value set by gtk_toggle_button_set_inconsistent().
378  * 
379  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
380  **/
381 gboolean
382 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
383 {
384   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
385
386   return toggle_button->inconsistent;
387 }
388
389 static gint
390 gtk_toggle_button_expose (GtkWidget      *widget,
391                           GdkEventExpose *event)
392 {
393   if (GTK_WIDGET_DRAWABLE (widget))
394     {
395       GtkWidget *child = GTK_BIN (widget)->child;
396       GtkButton *button = GTK_BUTTON (widget);
397       GtkStateType state_type;
398       GtkShadowType shadow_type;
399
400       state_type = GTK_WIDGET_STATE (widget);
401       
402       if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
403         {
404           if (state_type == GTK_STATE_ACTIVE)
405             state_type = GTK_STATE_NORMAL;
406           shadow_type = GTK_SHADOW_ETCHED_IN;
407         }
408       else
409         shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
410
411       _gtk_button_paint (button, &event->area, state_type, shadow_type,
412                          "togglebutton", "togglebuttondefault");
413
414       if (child)
415         gtk_container_propagate_expose (GTK_CONTAINER (widget), child, event);
416     }
417   
418   return FALSE;
419 }
420
421 static gboolean
422 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
423                                      gboolean   group_cycling)
424 {
425   /*
426    * We override the standard implementation in 
427    * gtk_widget_real_mnemonic_activate() in order to focus the widget even
428    * if there is no mnemonic conflict.
429    */
430   if (GTK_WIDGET_CAN_FOCUS (widget))
431     gtk_widget_grab_focus (widget);
432
433   if (!group_cycling)
434     gtk_widget_activate (widget);
435
436   return TRUE;
437 }
438
439 static void
440 gtk_toggle_button_pressed (GtkButton *button)
441 {
442   button->button_down = TRUE;
443
444   gtk_toggle_button_update_state (button);
445   gtk_widget_queue_draw (GTK_WIDGET (button));
446 }
447
448 static void
449 gtk_toggle_button_released (GtkButton *button)
450 {
451   if (button->button_down)
452     {
453       button->button_down = FALSE;
454
455       if (button->in_button)
456         gtk_button_clicked (button);
457
458       gtk_toggle_button_update_state (button);
459       gtk_widget_queue_draw (GTK_WIDGET (button));
460     }
461 }
462
463 static void
464 gtk_toggle_button_clicked (GtkButton *button)
465 {
466   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
467   toggle_button->active = !toggle_button->active;
468
469   gtk_toggle_button_toggled (toggle_button);
470
471   gtk_toggle_button_update_state (button);
472
473   g_object_notify (G_OBJECT (toggle_button), "active");
474 }
475
476 static void
477 gtk_toggle_button_update_state (GtkButton *button)
478 {
479   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
480   gboolean depressed;
481   GtkStateType new_state;
482
483   if (toggle_button->inconsistent)
484     depressed = FALSE;
485   else if (button->in_button && button->button_down)
486     depressed = TRUE;
487   else
488     depressed = toggle_button->active;
489       
490   if (button->in_button && (!button->button_down || toggle_button->draw_indicator))
491     new_state = GTK_STATE_PRELIGHT;
492   else
493     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
494
495   _gtk_button_set_depressed (button, depressed); 
496   gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
497 }