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