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