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