]> Pileus Git - ~andy/gtk/blob - gtk/gtkswitch.c
switch: propagate the active state to the slider
[~andy/gtk] / gtk / gtkswitch.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2010  Intel Corporation
4  * Copyright (C) 2010  RedHat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA  02111-1307, USA.
20  *
21  * Author:
22  *      Emmanuele Bassi <ebassi@linux.intel.com>
23  *      Matthias Clasen <mclasen@redhat.com>
24  *
25  * Based on similar code from Mx.
26  */
27
28 /**
29  * SECTION:gtkswitch
30  * @Short_Description: A "light switch" style toggle
31  * @Title: GtkSwitch
32  * @See_Also: #GtkToggleButton
33  *
34  * #GtkSwitch is a widget that has two states: on or off. The user can control
35  * which state should be active by clicking the empty area, or by dragging the
36  * handle.
37  */
38
39 #include "config.h"
40
41 #include "gtkswitch.h"
42
43 #include "gtkaccessibleprivate.h"
44 #include "gtkactivatable.h"
45 #include "gtkintl.h"
46 #include "gtkstyle.h"
47 #include "gtkprivate.h"
48 #include "gtktoggleaction.h"
49 #include "gtkwidget.h"
50 #include "gtkmarshalers.h"
51
52
53 #define DEFAULT_SLIDER_WIDTH    (36)
54 #define DEFAULT_SLIDER_HEIGHT   (22)
55
56 struct _GtkSwitchPrivate
57 {
58   GdkWindow *event_window;
59   GtkAction *action;
60
61   gint handle_x;
62   gint offset;
63   gint drag_start;
64   gint drag_threshold;
65
66   guint is_active             : 1;
67   guint is_dragging           : 1;
68   guint in_press              : 1;
69   guint in_switch             : 1;
70   guint use_action_appearance : 1;
71 };
72
73 enum
74 {
75   PROP_0,
76   PROP_ACTIVE,
77   PROP_RELATED_ACTION,
78   PROP_USE_ACTION_APPEARANCE,
79   LAST_PROP
80 };
81
82 enum
83 {
84   ACTIVATE,
85   LAST_SIGNAL
86 };
87
88 static guint signals[LAST_SIGNAL] = { 0 };
89
90 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
91
92 static GType gtk_switch_accessible_factory_get_type (void);
93
94 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
95
96 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
97                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
98                                                 gtk_switch_activatable_interface_init));
99
100 static gboolean
101 gtk_switch_button_press (GtkWidget      *widget,
102                          GdkEventButton *event)
103 {
104   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
105   GtkAllocation allocation;
106
107   gtk_widget_get_allocation (widget, &allocation);
108
109   if (priv->is_active)
110     {
111       /* if the event occurred in the "off" area, then this
112        * is a direct toggle
113        */
114       if (event->x <= allocation.width / 2)
115         {
116           priv->in_press = TRUE;
117           return FALSE;
118         }
119
120       priv->offset = event->x - allocation.width / 2;
121     }
122   else
123     {
124       /* if the event occurred in the "on" area, then this
125        * is a direct toggle
126        */
127       if (event->x >= allocation.width / 2)
128         {
129           priv->in_press = TRUE;
130           return FALSE;
131         }
132
133       priv->offset = event->x;
134     }
135
136   priv->drag_start = event->x;
137
138   g_object_get (gtk_widget_get_settings (widget),
139                 "gtk-dnd-drag-threshold", &priv->drag_threshold,
140                 NULL);
141
142   return FALSE;
143 }
144
145 static gboolean
146 gtk_switch_motion (GtkWidget      *widget,
147                    GdkEventMotion *event)
148 {
149   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
150
151   /* if this is a direct toggle we don't handle motion */
152   if (priv->in_press)
153     return FALSE;
154
155   if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
156     return TRUE;
157
158   if (event->state & GDK_BUTTON1_MASK)
159     {
160       gint position = event->x - priv->offset;
161       GtkAllocation allocation;
162       GtkStyleContext *context;
163       GtkStateFlags state;
164       GtkBorder padding;
165
166       context = gtk_widget_get_style_context (widget);
167       state = gtk_widget_get_state_flags (widget);
168       gtk_style_context_get_padding (context, state, &padding);
169       gtk_widget_get_allocation (widget, &allocation);
170
171       /* constrain the handle within the trough width */
172       if (position > (allocation.width / 2 - padding.right))
173         priv->handle_x = allocation.width / 2 - padding.right;
174       else if (position < padding.left)
175         priv->handle_x = padding.left;
176       else
177         priv->handle_x = position;
178
179       priv->is_dragging = TRUE;
180
181       /* we need to redraw the handle */
182       gtk_widget_queue_draw (widget);
183
184       return TRUE;
185     }
186
187   return FALSE;
188 }
189
190 static gboolean
191 gtk_switch_button_release (GtkWidget      *widget,
192                            GdkEventButton *event)
193 {
194   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
195   GtkAllocation allocation;
196
197   gtk_widget_get_allocation (widget, &allocation);
198
199   /* dragged toggles have a "soft" grab, so we don't care whether we
200    * are in the switch or not when the button is released; we do care
201    * for direct toggles, instead
202    */
203   if (!priv->is_dragging && !priv->in_switch)
204     return FALSE;
205
206   /* direct toggle */
207   if (priv->in_press)
208     {
209       priv->in_press = FALSE;
210       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
211
212       return TRUE;
213     }
214
215   /* toggle the switch if the handle was clicked but a drag had not been
216    * initiated */
217   if (!priv->is_dragging && !priv->in_press)
218     {
219       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
220
221       return TRUE;
222     }
223
224   /* dragged toggle */
225   if (priv->is_dragging)
226     {
227       priv->is_dragging = FALSE;
228
229       /* if half the handle passed the middle of the switch, then we
230        * consider it to be on
231        */
232       if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
233         {
234           gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
235           priv->handle_x = allocation.width / 2;
236         }
237       else
238         {
239           gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
240           priv->handle_x = 0;
241         }
242
243       gtk_widget_queue_draw (widget);
244
245       return TRUE;
246     }
247
248   return FALSE;
249 }
250
251 static gboolean
252 gtk_switch_enter (GtkWidget        *widget,
253                   GdkEventCrossing *event)
254 {
255   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
256
257   if (event->window == priv->event_window)
258     priv->in_switch = TRUE;
259
260   return FALSE;
261 }
262
263 static gboolean
264 gtk_switch_leave (GtkWidget        *widget,
265                   GdkEventCrossing *event)
266 {
267   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
268
269   if (event->window == priv->event_window)
270     priv->in_switch = FALSE;
271
272   return FALSE;
273 }
274
275 static void
276 gtk_switch_activate (GtkSwitch *sw)
277 {
278   GtkSwitchPrivate *priv = sw->priv;
279
280   gtk_switch_set_active (sw, !priv->is_active);
281 }
282
283 static void
284 gtk_switch_get_preferred_width (GtkWidget *widget,
285                                 gint      *minimum,
286                                 gint      *natural)
287 {
288   GtkStyleContext *context;
289   GtkStateFlags state;
290   GtkBorder padding;
291   gint width, slider_width, focus_width, focus_pad;
292   PangoLayout *layout;
293   PangoRectangle logical_rect;
294
295   context = gtk_widget_get_style_context (widget);
296   state = gtk_widget_get_state_flags (widget);
297   gtk_style_context_get_padding (context, state, &padding);
298
299   width = padding.left + padding.right;
300
301   gtk_widget_style_get (widget,
302                         "slider-width", &slider_width,
303                         "focus-line-width", &focus_width,
304                         "focus-padding", &focus_pad,
305                         NULL);
306
307   width += 2 * (focus_width + focus_pad);
308
309   /* Translators: if the "on" state label requires more than three
310    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
311    * the state
312    */
313   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
314   pango_layout_get_extents (layout, NULL, &logical_rect);
315   pango_extents_to_pixels (&logical_rect, NULL);
316   width += MAX (logical_rect.width, slider_width);
317
318   /* Translators: if the "off" state label requires more than three
319    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
320    */
321   pango_layout_set_text (layout, C_("switch", "OFF"), -1);
322   pango_layout_get_extents (layout, NULL, &logical_rect);
323   pango_extents_to_pixels (&logical_rect, NULL);
324   width += MAX (logical_rect.width, slider_width);
325
326   g_object_unref (layout);
327
328   if (minimum)
329     *minimum = width;
330
331   if (natural)
332     *natural = width;
333 }
334
335 static void
336 gtk_switch_get_preferred_height (GtkWidget *widget,
337                                  gint      *minimum,
338                                  gint      *natural)
339 {
340   GtkStyleContext *context;
341   GtkStateFlags state;
342   GtkBorder padding;
343   gint height, focus_width, focus_pad;
344   PangoLayout *layout;
345   PangoRectangle logical_rect;
346   gchar *str;
347
348   context = gtk_widget_get_style_context (widget);
349   state = gtk_widget_get_state_flags (widget);
350   gtk_style_context_get_padding (context, state, &padding);
351
352   height = padding.top + padding.bottom;
353
354   gtk_widget_style_get (widget,
355                         "focus-line-width", &focus_width,
356                         "focus-padding", &focus_pad,
357                         NULL);
358
359   height += 2 * (focus_width + focus_pad);
360
361   str = g_strdup_printf ("%s%s",
362                          C_("switch", "ON"),
363                          C_("switch", "OFF"));
364
365   layout = gtk_widget_create_pango_layout (widget, str);
366   pango_layout_get_extents (layout, NULL, &logical_rect);
367   pango_extents_to_pixels (&logical_rect, NULL);
368   height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
369
370   g_object_unref (layout);
371   g_free (str);
372
373   if (minimum)
374     *minimum = height;
375
376   if (natural)
377     *natural = height;
378 }
379
380 static void
381 gtk_switch_size_allocate (GtkWidget     *widget,
382                           GtkAllocation *allocation)
383 {
384   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
385
386   gtk_widget_set_allocation (widget, allocation);
387
388   if (gtk_widget_get_realized (widget))
389     gdk_window_move_resize (priv->event_window,
390                             allocation->x,
391                             allocation->y,
392                             allocation->width,
393                             allocation->height);
394 }
395
396 static void
397 gtk_switch_realize (GtkWidget *widget)
398 {
399   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
400   GdkWindow *parent_window;
401   GdkWindowAttr attributes;
402   gint attributes_mask;
403   GtkAllocation allocation;
404
405   gtk_widget_set_realized (widget, TRUE);
406   parent_window = gtk_widget_get_parent_window (widget);
407   gtk_widget_set_window (widget, parent_window);
408   g_object_ref (parent_window);
409
410   gtk_widget_get_allocation (widget, &allocation);
411
412   attributes.window_type = GDK_WINDOW_CHILD;
413   attributes.wclass = GDK_INPUT_ONLY;
414   attributes.x = allocation.x;
415   attributes.y = allocation.y;
416   attributes.width = allocation.width;
417   attributes.height = allocation.height;
418   attributes.event_mask = gtk_widget_get_events (widget);
419   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
420                             GDK_BUTTON_RELEASE_MASK |
421                             GDK_BUTTON1_MOTION_MASK |
422                             GDK_POINTER_MOTION_HINT_MASK |
423                             GDK_POINTER_MOTION_MASK |
424                             GDK_ENTER_NOTIFY_MASK |
425                             GDK_LEAVE_NOTIFY_MASK);
426   attributes_mask = GDK_WA_X | GDK_WA_Y;
427
428   priv->event_window = gdk_window_new (parent_window,
429                                        &attributes,
430                                        attributes_mask);
431   gdk_window_set_user_data (priv->event_window, widget);
432 }
433
434 static void
435 gtk_switch_unrealize (GtkWidget *widget)
436 {
437   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
438
439   if (priv->event_window != NULL)
440     {
441       gdk_window_set_user_data (priv->event_window, NULL);
442       gdk_window_destroy (priv->event_window);
443       priv->event_window = NULL;
444     }
445
446   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
447 }
448
449 static void
450 gtk_switch_map (GtkWidget *widget)
451 {
452   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
453
454   GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
455
456   if (priv->event_window)
457     gdk_window_show (priv->event_window);
458 }
459
460 static void
461 gtk_switch_unmap (GtkWidget *widget)
462 {
463   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
464
465   if (priv->event_window)
466     gdk_window_hide (priv->event_window);
467
468   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
469 }
470
471 static inline void
472 gtk_switch_paint_handle (GtkWidget    *widget,
473                          cairo_t      *cr,
474                          GdkRectangle *box)
475 {
476   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
477   GtkStyleContext *context = gtk_widget_get_style_context (widget);
478   GtkStateFlags state;
479
480   state = gtk_widget_get_state_flags (widget);
481
482   if (priv->is_active)
483     state |= GTK_STATE_FLAG_ACTIVE;
484
485   gtk_style_context_save (context);
486   gtk_style_context_set_state (context, state);
487   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
488
489   gtk_render_slider (context, cr,
490                      box->x, box->y,
491                      box->width, box->height,
492                      GTK_ORIENTATION_HORIZONTAL);
493
494   gtk_style_context_restore (context);
495 }
496
497 static gboolean
498 gtk_switch_draw (GtkWidget *widget,
499                  cairo_t   *cr)
500 {
501   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
502   GtkStyleContext *context;
503   GdkRectangle handle;
504   PangoLayout *layout;
505   PangoRectangle rect;
506   gint label_x, label_y;
507   GtkStateFlags state;
508   GtkBorder padding;
509   gint focus_width, focus_pad;
510   gint x, y, width, height;
511
512   gtk_widget_style_get (widget,
513                         "focus-line-width", &focus_width,
514                         "focus-padding", &focus_pad,
515                         NULL);
516
517   context = gtk_widget_get_style_context (widget);
518   state = gtk_widget_get_state_flags (widget);
519
520   if (priv->is_active)
521     state |= GTK_STATE_FLAG_ACTIVE;
522
523   gtk_style_context_get_padding (context, state, &padding);
524
525   x = 0;
526   y = 0;
527   width = gtk_widget_get_allocated_width (widget);
528   height = gtk_widget_get_allocated_height (widget);
529
530   if (gtk_widget_has_focus (widget))
531     gtk_render_focus (context, cr, x, y, width, height);
532
533   gtk_style_context_save (context);
534   gtk_style_context_set_state (context, state);
535
536   x += focus_width + focus_pad;
537   y += focus_width + focus_pad;
538   width -= 2 * (focus_width + focus_pad);
539   height -= 2 * (focus_width + focus_pad);
540
541   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
542
543   gtk_render_background (context, cr, x, y, width, height);
544   gtk_render_frame (context, cr, x, y, width, height);
545
546   /* XXX the +1/-1 it's pixel wriggling after checking with the default
547    * theme and xmag
548    */
549   handle.y = y + padding.top + 1;
550   handle.width = (width - padding.left - padding.right) / 2;
551   handle.height = (height - padding.top - padding.bottom) - 1;
552
553   /* Translators: if the "on" state label requires more than three
554    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
555    * the state
556    */
557   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
558   pango_layout_get_extents (layout, NULL, &rect);
559   pango_extents_to_pixels (&rect, NULL);
560
561   label_x = x + padding.left
562           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
563   label_y = y + padding.top
564           + (height - rect.height - padding.top - padding.bottom) / 2;
565
566   gtk_render_layout (context, cr, label_x, label_y, layout);
567
568   g_object_unref (layout);
569
570   /* Translators: if the "off" state label requires more than three
571    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
572    */
573   layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
574   pango_layout_get_extents (layout, NULL, &rect);
575   pango_extents_to_pixels (&rect, NULL);
576
577   label_x = x + padding.left
578           + (width / 2)
579           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
580   label_y = y + padding.top
581           + (height - rect.height - padding.top - padding.bottom) / 2;
582
583   gtk_render_layout (context, cr, label_x, label_y, layout);
584
585   g_object_unref (layout);
586
587   if (priv->is_dragging)
588     handle.x = x + priv->handle_x;
589   else if (priv->is_active)
590     handle.x = x + width - handle.width - padding.right;
591   else
592     handle.x = x + padding.left;
593
594   gtk_style_context_restore (context);
595
596   gtk_switch_paint_handle (widget, cr, &handle);
597
598   return FALSE;
599 }
600
601 static AtkObject *
602 gtk_switch_get_accessible (GtkWidget *widget)
603 {
604   static gboolean first_time = TRUE;
605
606   if (G_UNLIKELY (first_time))
607     {
608       _gtk_accessible_set_factory_type (GTK_TYPE_SWITCH,
609                                         gtk_switch_accessible_factory_get_type ());
610       first_time = FALSE;
611     }
612
613   return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
614 }
615
616 static void
617 gtk_switch_set_related_action (GtkSwitch *sw,
618                                GtkAction *action)
619 {
620   GtkSwitchPrivate *priv = sw->priv;
621
622   if (priv->action == action)
623     return;
624
625   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
626
627   priv->action = action;
628 }
629
630 static void
631 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
632                                       gboolean   use_appearance)
633 {
634   GtkSwitchPrivate *priv = sw->priv;
635
636   if (priv->use_action_appearance != use_appearance)
637     {
638       priv->use_action_appearance = use_appearance;
639
640       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
641     }
642 }
643
644 static void
645 gtk_switch_set_property (GObject      *gobject,
646                          guint         prop_id,
647                          const GValue *value,
648                          GParamSpec   *pspec)
649 {
650   GtkSwitch *sw = GTK_SWITCH (gobject);
651
652   switch (prop_id)
653     {
654     case PROP_ACTIVE:
655       gtk_switch_set_active (sw, g_value_get_boolean (value));
656       break;
657
658     case PROP_RELATED_ACTION:
659       gtk_switch_set_related_action (sw, g_value_get_object (value));
660       break;
661
662     case PROP_USE_ACTION_APPEARANCE:
663       gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
664       break;
665
666     default:
667       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
668     }
669 }
670
671 static void
672 gtk_switch_get_property (GObject    *gobject,
673                          guint       prop_id,
674                          GValue     *value,
675                          GParamSpec *pspec)
676 {
677   GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
678
679   switch (prop_id)
680     {
681     case PROP_ACTIVE:
682       g_value_set_boolean (value, priv->is_active);
683       break;
684
685     case PROP_RELATED_ACTION:
686       g_value_set_object (value, priv->action);
687       break;
688
689     case PROP_USE_ACTION_APPEARANCE:
690       g_value_set_boolean (value, priv->use_action_appearance);
691       break;
692
693     default:
694       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
695     }
696 }
697
698 static void
699 gtk_switch_dispose (GObject *object)
700 {
701   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
702
703   if (priv->action)
704     {
705       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
706       priv->action = NULL;
707     }
708
709   G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
710 }
711
712 static void
713 gtk_switch_class_init (GtkSwitchClass *klass)
714 {
715   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
716   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
717   gpointer activatable_iface;
718
719   g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
720
721   activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
722   switch_props[PROP_RELATED_ACTION] =
723     g_param_spec_override ("related-action",
724                            g_object_interface_find_property (activatable_iface,
725                                                              "related-action"));
726
727   switch_props[PROP_USE_ACTION_APPEARANCE] =
728     g_param_spec_override ("use-action-appearance",
729                            g_object_interface_find_property (activatable_iface,
730                                                              "use-action-appearance"));
731
732   /**
733    * GtkSwitch:active:
734    *
735    * Whether the #GtkSwitch widget is in its on or off state.
736    */
737   switch_props[PROP_ACTIVE] =
738     g_param_spec_boolean ("active",
739                           P_("Active"),
740                           P_("Whether the switch is on or off"),
741                           FALSE,
742                           GTK_PARAM_READWRITE);
743
744   gobject_class->set_property = gtk_switch_set_property;
745   gobject_class->get_property = gtk_switch_get_property;
746   gobject_class->dispose = gtk_switch_dispose;
747
748   g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
749
750   widget_class->get_preferred_width = gtk_switch_get_preferred_width;
751   widget_class->get_preferred_height = gtk_switch_get_preferred_height;
752   widget_class->size_allocate = gtk_switch_size_allocate;
753   widget_class->realize = gtk_switch_realize;
754   widget_class->unrealize = gtk_switch_unrealize;
755   widget_class->map = gtk_switch_map;
756   widget_class->unmap = gtk_switch_unmap;
757   widget_class->draw = gtk_switch_draw;
758   widget_class->button_press_event = gtk_switch_button_press;
759   widget_class->button_release_event = gtk_switch_button_release;
760   widget_class->motion_notify_event = gtk_switch_motion;
761   widget_class->enter_notify_event = gtk_switch_enter;
762   widget_class->leave_notify_event = gtk_switch_leave;
763   widget_class->get_accessible = gtk_switch_get_accessible;
764
765   klass->activate = gtk_switch_activate;
766
767   /**
768    * GtkSwitch:slider-width:
769    *
770    * The minimum width of the #GtkSwitch handle, in pixels.
771    */
772   gtk_widget_class_install_style_property (widget_class,
773                                            g_param_spec_int ("slider-width",
774                                                              P_("Slider Width"),
775                                                              P_("The minimum width of the handle"),
776                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
777                                                              DEFAULT_SLIDER_WIDTH,
778                                                              GTK_PARAM_READABLE));
779
780   /**
781    * GtkSwitch::activate:
782    * @widget: the object which received the signal.
783    *
784    * The ::activate signal on GtkSwitch is an action signal and
785    * emitting it causes the switch to animate.
786    * Applications should never connect to this signal, but use the
787    * notify::active signal.
788    */
789   signals[ACTIVATE] =
790     g_signal_new (I_("activate"),
791                   G_OBJECT_CLASS_TYPE (gobject_class),
792                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
793                   G_STRUCT_OFFSET (GtkSwitchClass, activate),
794                   NULL, NULL,
795                   _gtk_marshal_VOID__VOID,
796                   G_TYPE_NONE, 0);
797   widget_class->activate_signal = signals[ACTIVATE];
798
799 }
800
801 static void
802 gtk_switch_init (GtkSwitch *self)
803 {
804   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
805   self->priv->use_action_appearance = TRUE;
806   gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
807   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
808 }
809
810 /**
811  * gtk_switch_new:
812  *
813  * Creates a new #GtkSwitch widget.
814  *
815  * Return value: the newly created #GtkSwitch instance
816  *
817  * Since: 3.0
818  */
819 GtkWidget *
820 gtk_switch_new (void)
821 {
822   return g_object_new (GTK_TYPE_SWITCH, NULL);
823 }
824
825 /**
826  * gtk_switch_set_active:
827  * @sw: a #GtkSwitch
828  * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
829  *
830  * Changes the state of @sw to the desired one.
831  *
832  * Since: 3.0
833  */
834 void
835 gtk_switch_set_active (GtkSwitch *sw,
836                        gboolean   is_active)
837 {
838   GtkSwitchPrivate *priv;
839
840   g_return_if_fail (GTK_IS_SWITCH (sw));
841
842   is_active = !!is_active;
843
844   priv = sw->priv;
845
846   if (priv->is_active != is_active)
847     {
848       AtkObject *accessible;
849       GtkWidget *widget;
850       GtkStyleContext *context;
851
852       widget = GTK_WIDGET (sw);
853       priv->is_active = is_active;
854
855       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
856
857       if (priv->action)
858         gtk_action_activate (priv->action);
859
860       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
861       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
862
863       if (gtk_widget_get_realized (widget))
864         {
865           context = gtk_widget_get_style_context (widget);
866           gtk_style_context_notify_state_change (context,
867                                                  gtk_widget_get_window (widget),
868                                                  NULL, GTK_STATE_ACTIVE, is_active);
869         }
870
871       gtk_widget_queue_draw (GTK_WIDGET (sw));
872     }
873 }
874
875 /**
876  * gtk_switch_get_active:
877  * @sw: a #GtkSwitch
878  *
879  * Gets whether the #GtkSwitch is in its "on" or "off" state.
880  *
881  * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
882  *
883  * Since: 3.0
884  */
885 gboolean
886 gtk_switch_get_active (GtkSwitch *sw)
887 {
888   g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
889
890   return sw->priv->is_active;
891 }
892
893 static void
894 gtk_switch_update (GtkActivatable *activatable,
895                    GtkAction      *action,
896                    const gchar    *property_name)
897 {
898   if (strcmp (property_name, "visible") == 0)
899     {
900       if (gtk_action_is_visible (action))
901         gtk_widget_show (GTK_WIDGET (activatable));
902       else
903         gtk_widget_hide (GTK_WIDGET (activatable));
904     }
905   else if (strcmp (property_name, "sensitive") == 0)
906     {
907       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
908     }
909   else if (strcmp (property_name, "active") == 0)
910     {
911       gtk_action_block_activate (action);
912       gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
913       gtk_action_unblock_activate (action);
914     }
915 }
916
917 static void
918 gtk_switch_sync_action_properties (GtkActivatable *activatable,
919                                    GtkAction      *action)
920 {
921   if (!action)
922     return;
923
924   if (gtk_action_is_visible (action))
925     gtk_widget_show (GTK_WIDGET (activatable));
926   else
927     gtk_widget_hide (GTK_WIDGET (activatable));
928
929   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
930
931   gtk_action_block_activate (action);
932   gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
933   gtk_action_unblock_activate (action);
934 }
935
936 static void
937 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
938 {
939   iface->update = gtk_switch_update;
940   iface->sync_action_properties = gtk_switch_sync_action_properties;
941 }
942
943 /* accessibility: object */
944
945 typedef struct _GtkSwitchAccessible      GtkSwitchAccessible;
946 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
947
948 struct _GtkSwitchAccessible
949 {
950   GtkAccessible object;
951
952   gchar *description;
953   guint  action_idle;
954 };
955
956 static void atk_action_interface_init (AtkActionIface *iface);
957
958 ATK_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_SWITCH,
959                            G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
960
961 static AtkStateSet *
962 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
963 {
964   AtkStateSet *state_set;
965   GtkWidget *widget;
966
967   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
968
969   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
970   if (widget == NULL)
971     return state_set;
972
973   if (gtk_switch_get_active (GTK_SWITCH (widget)))
974     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
975
976   return state_set;
977 }
978
979 static void
980 gtk_switch_accessible_finalize (GObject *obj)
981 {
982   GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)obj;
983
984   g_free (accessible->description);
985
986   if (accessible->action_idle)
987     g_source_remove (accessible->action_idle);
988
989   G_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->finalize (obj);
990 }
991
992 static void
993 _gtk_switch_accessible_initialize (AtkObject *accessible,
994                                    gpointer   widget)
995 {
996   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
997
998   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
999   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
1000   atk_object_set_description (accessible, _("Switches between on and off states"));
1001 }
1002
1003 static void
1004 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
1005 {
1006   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1007   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
1008
1009   object_class->finalize = gtk_switch_accessible_finalize;
1010
1011   atk_class->initialize = _gtk_switch_accessible_initialize;
1012   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
1013 }
1014
1015 static void
1016 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
1017 {
1018   self->description = NULL;
1019   self->action_idle = 0;
1020 }
1021
1022 /* accessibility: action interface */
1023
1024 static gint
1025 gtk_switch_action_get_n_actions (AtkAction *action)
1026 {
1027   return 1;
1028 }
1029
1030 static const gchar *
1031 gtk_switch_action_get_name (AtkAction *action,
1032                             gint       i)
1033 {
1034   return "toggle";
1035 }
1036
1037 static const gchar *
1038 gtk_switch_action_get_description (AtkAction *action,
1039                                    gint       i)
1040 {
1041   GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1042
1043   return accessible->description;
1044 }
1045
1046 static gboolean
1047 gtk_switch_action_set_description (AtkAction   *action,
1048                                    gint         i,
1049                                    const gchar *description)
1050 {
1051   GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1052
1053   g_free (accessible->description);
1054   accessible->description = g_strdup (description);
1055
1056   return TRUE;
1057 }
1058
1059 static gboolean
1060 idle_do_action (gpointer data)
1061 {
1062   GtkSwitchAccessible *accessible = data;
1063   GtkWidget *widget;
1064   GtkSwitch *sw;
1065
1066   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (data));
1067   sw = GTK_SWITCH (widget);
1068
1069   accessible->action_idle = 0;
1070
1071   if (widget == NULL ||
1072       !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1073     return FALSE;
1074
1075   gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
1076
1077   return FALSE;
1078 }
1079
1080 static gboolean
1081 gtk_switch_action_do_action (AtkAction *action,
1082                              gint       i)
1083 {
1084   GtkSwitchAccessible *accessible;
1085   GtkWidget *widget;
1086
1087   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1088   if (widget == NULL)
1089     return FALSE;
1090
1091   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1092     return FALSE;
1093
1094   accessible = (GtkSwitchAccessible *)action;
1095
1096   if (!accessible->action_idle)
1097     accessible->action_idle = gdk_threads_add_idle (idle_do_action, accessible);
1098
1099   return TRUE;
1100 }
1101
1102 static void
1103 atk_action_interface_init (AtkActionIface *iface)
1104 {
1105   iface->do_action = gtk_switch_action_do_action;
1106   iface->get_n_actions = gtk_switch_action_get_n_actions;
1107   iface->get_name = gtk_switch_action_get_name;
1108   iface->get_description = gtk_switch_action_get_description;
1109   iface->set_description = gtk_switch_action_set_description;
1110 }
1111
1112 /* accessibility: factory */
1113
1114 typedef AtkObjectFactoryClass   GtkSwitchAccessibleFactoryClass;
1115 typedef AtkObjectFactory        GtkSwitchAccessibleFactory;
1116
1117 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
1118                gtk_switch_accessible_factory,
1119                ATK_TYPE_OBJECT_FACTORY);
1120
1121 static GType
1122 gtk_switch_accessible_factory_get_accessible_type (void)
1123 {
1124   return _gtk_switch_accessible_get_type ();
1125 }
1126
1127 static AtkObject *
1128 gtk_switch_accessible_factory_create_accessible (GObject *obj)
1129 {
1130   AtkObject *accessible;
1131
1132   accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1133   atk_object_initialize (accessible, obj);
1134
1135   return accessible;
1136 }
1137
1138 static void
1139 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1140 {
1141   klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1142   klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1143 }
1144
1145 static void
1146 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)
1147 {
1148 }