]> Pileus Git - ~andy/gtk/blob - gtk/gtktogglebutton.c
gtktogglebutton: Add _gtk_toggle_button_set_active private accessor
[~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
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_draw         (GtkWidget            *widget,
56                                             cairo_t              *cr);
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_update                 (GtkActivatable       *activatable,
75                                                       GtkAction            *action,
76                                                       const gchar          *property_name);
77 static void gtk_toggle_button_sync_action_properties (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->draw = gtk_toggle_button_draw;
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"),
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 static void
155 gtk_toggle_button_activatable_interface_init (GtkActivatableIface *iface)
156 {
157   parent_activatable_iface = g_type_interface_peek_parent (iface);
158   iface->update = gtk_toggle_button_update;
159   iface->sync_action_properties = gtk_toggle_button_sync_action_properties;
160 }
161
162 static void
163 gtk_toggle_button_update (GtkActivatable *activatable,
164                           GtkAction      *action,
165                           const gchar    *property_name)
166 {
167   GtkToggleButton *button;
168
169   parent_activatable_iface->update (activatable, action, property_name);
170
171   button = GTK_TOGGLE_BUTTON (activatable);
172
173   if (strcmp (property_name, "active") == 0)
174     {
175       gtk_action_block_activate (action);
176       gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
177       gtk_action_unblock_activate (action);
178     }
179
180 }
181
182 static void
183 gtk_toggle_button_sync_action_properties (GtkActivatable *activatable,
184                                           GtkAction      *action)
185 {
186   GtkToggleButton *button;
187
188   parent_activatable_iface->sync_action_properties (activatable, action);
189
190   if (!GTK_IS_TOGGLE_ACTION (action))
191     return;
192
193   button = GTK_TOGGLE_BUTTON (activatable);
194
195   gtk_action_block_activate (action);
196   gtk_toggle_button_set_active (button, gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
197   gtk_action_unblock_activate (action);
198 }
199
200
201 GtkWidget*
202 gtk_toggle_button_new (void)
203 {
204   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL);
205 }
206
207 GtkWidget*
208 gtk_toggle_button_new_with_label (const gchar *label)
209 {
210   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, "label", label, NULL);
211 }
212
213 /**
214  * gtk_toggle_button_new_with_mnemonic:
215  * @label: the text of the button, with an underscore in front of the
216  *         mnemonic character
217  * @returns: a new #GtkToggleButton
218  *
219  * Creates a new #GtkToggleButton containing a label. The label
220  * will be created using gtk_label_new_with_mnemonic(), so underscores
221  * in @label indicate the mnemonic for the button.
222  **/
223 GtkWidget*
224 gtk_toggle_button_new_with_mnemonic (const gchar *label)
225 {
226   return g_object_new (GTK_TYPE_TOGGLE_BUTTON, 
227                        "label", label, 
228                        "use-underline", TRUE, 
229                        NULL);
230 }
231
232 static void
233 gtk_toggle_button_set_property (GObject      *object,
234                                 guint         prop_id,
235                                 const GValue *value,
236                                 GParamSpec   *pspec)
237 {
238   GtkToggleButton *tb;
239
240   tb = GTK_TOGGLE_BUTTON (object);
241
242   switch (prop_id)
243     {
244     case PROP_ACTIVE:
245       gtk_toggle_button_set_active (tb, g_value_get_boolean (value));
246       break;
247     case PROP_INCONSISTENT:
248       gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value));
249       break;
250     case PROP_DRAW_INDICATOR:
251       gtk_toggle_button_set_mode (tb, g_value_get_boolean (value));
252       break;
253     default:
254       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
255       break;
256     }
257 }
258
259 static void
260 gtk_toggle_button_get_property (GObject      *object,
261                                 guint         prop_id,
262                                 GValue       *value,
263                                 GParamSpec   *pspec)
264 {
265   GtkToggleButton *tb;
266
267   tb = GTK_TOGGLE_BUTTON (object);
268
269   switch (prop_id)
270     {
271     case PROP_ACTIVE:
272       g_value_set_boolean (value, tb->active);
273       break;
274     case PROP_INCONSISTENT:
275       g_value_set_boolean (value, tb->inconsistent);
276       break;
277     case PROP_DRAW_INDICATOR:
278       g_value_set_boolean (value, tb->draw_indicator);
279       break;
280     default:
281       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282       break;
283     }
284 }
285
286 /**
287  * gtk_toggle_button_set_mode:
288  * @toggle_button: a #GtkToggleButton
289  * @draw_indicator: if %TRUE, draw the button as a separate indicator
290  * and label; if %FALSE, draw the button like a normal button
291  *
292  * Sets whether the button is displayed as a separate indicator and label.
293  * You can call this function on a checkbutton or a radiobutton with
294  * @draw_indicator = %FALSE to make the button look like a normal button
295  *
296  * This function only affects instances of classes like #GtkCheckButton
297  * and #GtkRadioButton that derive from #GtkToggleButton,
298  * not instances of #GtkToggleButton itself.
299  */
300 void
301 gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
302                             gboolean         draw_indicator)
303 {
304   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
305
306   draw_indicator = draw_indicator ? TRUE : FALSE;
307
308   if (toggle_button->draw_indicator != draw_indicator)
309     {
310       toggle_button->draw_indicator = draw_indicator;
311       GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator;
312       
313       if (gtk_widget_get_visible (GTK_WIDGET (toggle_button)))
314         gtk_widget_queue_resize (GTK_WIDGET (toggle_button));
315
316       g_object_notify (G_OBJECT (toggle_button), "draw-indicator");
317     }
318 }
319
320 /**
321  * gtk_toggle_button_get_mode:
322  * @toggle_button: a #GtkToggleButton
323  *
324  * Retrieves whether the button is displayed as a separate indicator
325  * and label. See gtk_toggle_button_set_mode().
326  *
327  * Return value: %TRUE if the togglebutton is drawn as a separate indicator
328  *   and label.
329  **/
330 gboolean
331 gtk_toggle_button_get_mode (GtkToggleButton *toggle_button)
332 {
333   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
334
335   return toggle_button->draw_indicator;
336 }
337
338 void
339 gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
340                               gboolean         is_active)
341 {
342   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
343
344   is_active = is_active != FALSE;
345
346   if (toggle_button->active != is_active)
347     gtk_button_clicked (GTK_BUTTON (toggle_button));
348 }
349
350 void
351 _gtk_toggle_button_set_active (GtkToggleButton *toggle_button,
352                                gboolean         is_active)
353 {
354   toggle_button->active = is_active;
355 }
356
357
358 gboolean
359 gtk_toggle_button_get_active (GtkToggleButton *toggle_button)
360 {
361   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
362
363   return (toggle_button->active) ? TRUE : FALSE;
364 }
365
366
367 void
368 gtk_toggle_button_toggled (GtkToggleButton *toggle_button)
369 {
370   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
371
372   g_signal_emit (toggle_button, toggle_button_signals[TOGGLED], 0);
373 }
374
375 /**
376  * gtk_toggle_button_set_inconsistent:
377  * @toggle_button: a #GtkToggleButton
378  * @setting: %TRUE if state is inconsistent
379  *
380  * If the user has selected a range of elements (such as some text or
381  * spreadsheet cells) that are affected by a toggle button, and the
382  * current values in that range are inconsistent, you may want to
383  * display the toggle in an "in between" state. This function turns on
384  * "in between" display.  Normally you would turn off the inconsistent
385  * state again if the user toggles the toggle button. This has to be
386  * done manually, gtk_toggle_button_set_inconsistent() only affects
387  * visual appearance, it doesn't affect the semantics of the button.
388  * 
389  **/
390 void
391 gtk_toggle_button_set_inconsistent (GtkToggleButton *toggle_button,
392                                     gboolean         setting)
393 {
394   g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button));
395   
396   setting = setting != FALSE;
397
398   if (setting != toggle_button->inconsistent)
399     {
400       toggle_button->inconsistent = setting;
401       
402       gtk_toggle_button_update_state (GTK_BUTTON (toggle_button));
403       gtk_widget_queue_draw (GTK_WIDGET (toggle_button));
404
405       g_object_notify (G_OBJECT (toggle_button), "inconsistent");      
406     }
407 }
408
409 /**
410  * gtk_toggle_button_get_inconsistent:
411  * @toggle_button: a #GtkToggleButton
412  * 
413  * Gets the value set by gtk_toggle_button_set_inconsistent().
414  * 
415  * Return value: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
416  **/
417 gboolean
418 gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button)
419 {
420   g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE);
421
422   return toggle_button->inconsistent;
423 }
424
425 static gint
426 gtk_toggle_button_draw (GtkWidget *widget,
427                         cairo_t   *cr)
428 {
429   GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
430   GtkButton *button = GTK_BUTTON (widget);
431   GtkStateType state_type;
432   GtkShadowType shadow_type;
433
434   state_type = gtk_widget_get_state (widget);
435   
436   if (GTK_TOGGLE_BUTTON (widget)->inconsistent)
437     {
438       if (state_type == GTK_STATE_ACTIVE)
439         state_type = GTK_STATE_NORMAL;
440       shadow_type = GTK_SHADOW_ETCHED_IN;
441     }
442   else
443     shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT;
444
445   _gtk_button_paint (button, cr,
446                      gtk_widget_get_allocated_width (widget),
447                      gtk_widget_get_allocated_height (widget),
448                      state_type, shadow_type,
449                      "togglebutton", "togglebuttondefault");
450
451   if (child)
452     gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);
453
454   return FALSE;
455 }
456
457 static gboolean
458 gtk_toggle_button_mnemonic_activate (GtkWidget *widget,
459                                      gboolean   group_cycling)
460 {
461   /*
462    * We override the standard implementation in 
463    * gtk_widget_real_mnemonic_activate() in order to focus the widget even
464    * if there is no mnemonic conflict.
465    */
466   if (gtk_widget_get_can_focus (widget))
467     gtk_widget_grab_focus (widget);
468
469   if (!group_cycling)
470     gtk_widget_activate (widget);
471
472   return TRUE;
473 }
474
475 static void
476 gtk_toggle_button_pressed (GtkButton *button)
477 {
478   button->button_down = TRUE;
479
480   gtk_toggle_button_update_state (button);
481   gtk_widget_queue_draw (GTK_WIDGET (button));
482 }
483
484 static void
485 gtk_toggle_button_released (GtkButton *button)
486 {
487   if (button->button_down)
488     {
489       button->button_down = FALSE;
490
491       if (button->in_button)
492         gtk_button_clicked (button);
493
494       gtk_toggle_button_update_state (button);
495       gtk_widget_queue_draw (GTK_WIDGET (button));
496     }
497 }
498
499 static void
500 gtk_toggle_button_clicked (GtkButton *button)
501 {
502   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
503   toggle_button->active = !toggle_button->active;
504
505   gtk_toggle_button_toggled (toggle_button);
506
507   gtk_toggle_button_update_state (button);
508
509   g_object_notify (G_OBJECT (toggle_button), "active");
510
511   if (GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked)
512     GTK_BUTTON_CLASS (gtk_toggle_button_parent_class)->clicked (button);
513 }
514
515 static void
516 gtk_toggle_button_update_state (GtkButton *button)
517 {
518   GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button);
519   gboolean depressed, touchscreen;
520   GtkStateType new_state;
521
522   g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)),
523                 "gtk-touchscreen-mode", &touchscreen,
524                 NULL);
525
526   if (toggle_button->inconsistent)
527     depressed = FALSE;
528   else if (button->in_button && button->button_down)
529     depressed = TRUE;
530   else
531     depressed = toggle_button->active;
532       
533   if (!touchscreen && button->in_button && (!button->button_down || toggle_button->draw_indicator))
534     new_state = GTK_STATE_PRELIGHT;
535   else
536     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
537
538   _gtk_button_set_depressed (button, depressed); 
539   gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state);
540 }