]> Pileus Git - ~andy/gtk/blob - gtk/gtkswitch.c
switch: allow the user to toggle the switch by clicking on the handle
[~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 "gtkaccessible.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
51 #define DEFAULT_SLIDER_WIDTH    (36)
52 #define DEFAULT_SLIDER_HEIGHT   (22)
53
54 struct _GtkSwitchPrivate
55 {
56   GdkWindow *event_window;
57   GtkAction *action;
58
59   gint handle_x;
60   gint offset;
61   gint drag_start;
62   gint drag_threshold;
63
64   guint is_active             : 1;
65   guint is_dragging           : 1;
66   guint in_press              : 1;
67   guint in_switch             : 1;
68   guint use_action_appearance : 1;
69 };
70
71 enum
72 {
73   PROP_0,
74   PROP_ACTIVE,
75   PROP_RELATED_ACTION,
76   PROP_USE_ACTION_APPEARANCE,
77   LAST_PROP
78 };
79
80 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
81
82 static GType gtk_switch_accessible_factory_get_type (void);
83
84 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
85
86 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
87                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
88                                                 gtk_switch_activatable_interface_init));
89
90 static gboolean
91 gtk_switch_button_press (GtkWidget      *widget,
92                          GdkEventButton *event)
93 {
94   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
95   GtkAllocation allocation;
96
97   gtk_widget_get_allocation (widget, &allocation);
98
99   if (priv->is_active)
100     {
101       /* if the event occurred in the "off" area, then this
102        * is a direct toggle
103        */
104       if (event->x <= allocation.width / 2)
105         {
106           priv->in_press = TRUE;
107           return FALSE;
108         }
109
110       priv->offset = event->x - allocation.width / 2;
111     }
112   else
113     {
114       /* if the event occurred in the "on" area, then this
115        * is a direct toggle
116        */
117       if (event->x >= allocation.width / 2)
118         {
119           priv->in_press = TRUE;
120           return FALSE;
121         }
122
123       priv->offset = event->x;
124     }
125
126   priv->drag_start = event->x;
127
128   g_object_get (gtk_widget_get_settings (widget),
129                 "gtk-dnd-drag-threshold", &priv->drag_threshold,
130                 NULL);
131
132   return FALSE;
133 }
134
135 static gboolean
136 gtk_switch_motion (GtkWidget      *widget,
137                    GdkEventMotion *event)
138 {
139   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
140
141   /* if this is a direct toggle we don't handle motion */
142   if (priv->in_press)
143     return FALSE;
144
145   if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
146     return TRUE;
147
148   if (event->state & GDK_BUTTON1_MASK)
149     {
150       gint position = event->x - priv->offset;
151       GtkAllocation allocation;
152       GtkStyleContext *context;
153       GtkStateFlags state;
154       GtkBorder padding;
155
156       context = gtk_widget_get_style_context (widget);
157       state = gtk_widget_get_state_flags (widget);
158       gtk_style_context_get_padding (context, state, &padding);
159       gtk_widget_get_allocation (widget, &allocation);
160
161       /* constrain the handle within the trough width */
162       if (position > (allocation.width / 2 - padding.right))
163         priv->handle_x = allocation.width / 2 - padding.right;
164       else if (position < padding.left)
165         priv->handle_x = padding.left;
166       else
167         priv->handle_x = position;
168
169       priv->is_dragging = TRUE;
170
171       /* we need to redraw the handle */
172       gtk_widget_queue_draw (widget);
173
174       return TRUE;
175     }
176
177   return FALSE;
178 }
179
180 static gboolean
181 gtk_switch_button_release (GtkWidget      *widget,
182                            GdkEventButton *event)
183 {
184   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
185   GtkAllocation allocation;
186
187   gtk_widget_get_allocation (widget, &allocation);
188
189   /* dragged toggles have a "soft" grab, so we don't care whether we
190    * are in the switch or not when the button is released; we do care
191    * for direct toggles, instead
192    */
193   if (!priv->is_dragging && !priv->in_switch)
194     return FALSE;
195
196   /* direct toggle */
197   if (priv->in_press)
198     {
199       priv->in_press = FALSE;
200       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
201
202       return TRUE;
203     }
204
205   /* toggle the switch if the handle was clicked but a drag had not been
206    * initiated */
207   if (!priv->is_dragging && !priv->in_press)
208     {
209       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
210
211       return TRUE;
212     }
213
214   /* dragged toggle */
215   if (priv->is_dragging)
216     {
217       priv->is_dragging = FALSE;
218
219       /* if half the handle passed the middle of the switch, then we
220        * consider it to be on
221        */
222       if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
223         {
224           gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
225           priv->handle_x = allocation.width / 2;
226         }
227       else
228         {
229           gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
230           priv->handle_x = 0;
231         }
232
233       gtk_widget_queue_draw (widget);
234
235       return TRUE;
236     }
237
238   return FALSE;
239 }
240
241 static gboolean
242 gtk_switch_enter (GtkWidget        *widget,
243                   GdkEventCrossing *event)
244 {
245   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
246
247   if (event->window == priv->event_window)
248     priv->in_switch = TRUE;
249
250   return FALSE;
251 }
252
253 static gboolean
254 gtk_switch_leave (GtkWidget        *widget,
255                   GdkEventCrossing *event)
256 {
257   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
258
259   if (event->window == priv->event_window)
260     priv->in_switch = FALSE;
261
262   return FALSE;
263 }
264
265 static gboolean
266 gtk_switch_key_release (GtkWidget   *widget,
267                         GdkEventKey *event)
268 {
269   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
270
271   if (event->keyval == GDK_KEY_Return ||
272       event->keyval == GDK_KEY_KP_Enter ||
273       event->keyval == GDK_KEY_ISO_Enter ||
274       event->keyval == GDK_KEY_space ||
275       event->keyval == GDK_KEY_KP_Space)
276     {
277       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
278     }
279
280   return FALSE;
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   GtkStyleContext *context = gtk_widget_get_style_context (widget);
477   GtkStateFlags state;
478
479   state = gtk_widget_get_state_flags (widget);
480
481   gtk_style_context_save (context);
482   gtk_style_context_set_state (context, state);
483   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
484
485   gtk_render_slider (context, cr,
486                      box->x, box->y,
487                      box->width, box->height,
488                      GTK_ORIENTATION_HORIZONTAL);
489
490   gtk_style_context_restore (context);
491 }
492
493 static gboolean
494 gtk_switch_draw (GtkWidget *widget,
495                  cairo_t   *cr)
496 {
497   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
498   GtkStyleContext *context;
499   GdkRectangle handle;
500   PangoLayout *layout;
501   PangoRectangle rect;
502   gint label_x, label_y;
503   GtkStateFlags state;
504   GtkBorder padding;
505   gint focus_width, focus_pad;
506   gint x, y, width, height;
507
508   gtk_widget_style_get (widget,
509                         "focus-line-width", &focus_width,
510                         "focus-padding", &focus_pad,
511                         NULL);
512
513   context = gtk_widget_get_style_context (widget);
514   state = gtk_widget_get_state_flags (widget);
515
516   if (priv->is_active)
517     state |= GTK_STATE_FLAG_ACTIVE;
518
519   gtk_style_context_get_padding (context, state, &padding);
520
521   x = 0;
522   y = 0;
523   width = gtk_widget_get_allocated_width (widget);
524   height = gtk_widget_get_allocated_height (widget);
525
526   if (gtk_widget_has_focus (widget))
527     gtk_render_focus (context, cr, x, y, width, height);
528
529   gtk_style_context_save (context);
530   gtk_style_context_set_state (context, state);
531
532   x += focus_width + focus_pad;
533   y += focus_width + focus_pad;
534   width -= 2 * (focus_width + focus_pad);
535   height -= 2 * (focus_width + focus_pad);
536
537   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
538
539   gtk_render_background (context, cr, x, y, width, height);
540   gtk_render_frame (context, cr, x, y, width, height);
541
542   /* XXX the +1/-1 it's pixel wriggling after checking with the default
543    * theme and xmag
544    */
545   handle.y = y + padding.top + 1;
546   handle.width = (width - padding.left - padding.right) / 2;
547   handle.height = (height - padding.top - padding.bottom) - 1;
548
549   /* Translators: if the "on" state label requires more than three
550    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
551    * the state
552    */
553   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
554   pango_layout_get_extents (layout, NULL, &rect);
555   pango_extents_to_pixels (&rect, NULL);
556
557   label_x = x + padding.left
558           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
559   label_y = y + padding.top
560           + (height - rect.height - padding.top - padding.bottom) / 2;
561
562   gtk_render_layout (context, cr, label_x, label_y, layout);
563
564   g_object_unref (layout);
565
566   /* Translators: if the "off" state label requires more than three
567    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
568    */
569   layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
570   pango_layout_get_extents (layout, NULL, &rect);
571   pango_extents_to_pixels (&rect, NULL);
572
573   label_x = x + padding.left
574           + (width / 2)
575           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
576   label_y = y + padding.top
577           + (height - rect.height - padding.top - padding.bottom) / 2;
578
579   gtk_render_layout (context, cr, label_x, label_y, layout);
580
581   g_object_unref (layout);
582
583   if (priv->is_dragging)
584     handle.x = x + priv->handle_x;
585   else if (priv->is_active)
586     handle.x = x + width - handle.width - padding.right;
587   else
588     handle.x = x + padding.left;
589
590   gtk_style_context_restore (context);
591
592   gtk_switch_paint_handle (widget, cr, &handle);
593
594   return FALSE;
595 }
596
597 static AtkObject *
598 gtk_switch_get_accessible (GtkWidget *widget)
599 {
600   static gboolean first_time = TRUE;
601
602   if (G_UNLIKELY (first_time))
603     {
604       AtkObjectFactory *factory;
605       AtkRegistry *registry;
606       GType derived_type;
607       GType derived_atk_type;
608
609       /* Figure out whether accessibility is enabled by looking at the
610        * type of the accessible object which would be created for the
611        * parent type of GtkSwitch
612        */
613       derived_type = g_type_parent (GTK_TYPE_SWITCH);
614
615       registry = atk_get_default_registry ();
616       factory = atk_registry_get_factory (registry, derived_type);
617       derived_atk_type = atk_object_factory_get_accessible_type (factory);
618       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
619         atk_registry_set_factory_type (registry,
620                                        GTK_TYPE_SWITCH,
621                                        gtk_switch_accessible_factory_get_type ());
622
623       first_time = FALSE;
624     }
625
626   return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
627 }
628
629 static void
630 gtk_switch_set_related_action (GtkSwitch *sw,
631                                GtkAction *action)
632 {
633   GtkSwitchPrivate *priv = sw->priv;
634
635   if (priv->action == action)
636     return;
637
638   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
639
640   priv->action = action;
641 }
642
643 static void
644 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
645                                       gboolean   use_appearance)
646 {
647   GtkSwitchPrivate *priv = sw->priv;
648
649   if (priv->use_action_appearance != use_appearance)
650     {
651       priv->use_action_appearance = use_appearance;
652
653       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
654     }
655 }
656
657 static void
658 gtk_switch_set_property (GObject      *gobject,
659                          guint         prop_id,
660                          const GValue *value,
661                          GParamSpec   *pspec)
662 {
663   GtkSwitch *sw = GTK_SWITCH (gobject);
664
665   switch (prop_id)
666     {
667     case PROP_ACTIVE:
668       gtk_switch_set_active (sw, g_value_get_boolean (value));
669       break;
670
671     case PROP_RELATED_ACTION:
672       gtk_switch_set_related_action (sw, g_value_get_object (value));
673       break;
674
675     case PROP_USE_ACTION_APPEARANCE:
676       gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
677       break;
678
679     default:
680       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
681     }
682 }
683
684 static void
685 gtk_switch_get_property (GObject    *gobject,
686                          guint       prop_id,
687                          GValue     *value,
688                          GParamSpec *pspec)
689 {
690   GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
691
692   switch (prop_id)
693     {
694     case PROP_ACTIVE:
695       g_value_set_boolean (value, priv->is_active);
696       break;
697
698     case PROP_RELATED_ACTION:
699       g_value_set_object (value, priv->action);
700       break;
701
702     case PROP_USE_ACTION_APPEARANCE:
703       g_value_set_boolean (value, priv->use_action_appearance);
704       break;
705
706     default:
707       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
708     }
709 }
710
711 static void
712 gtk_switch_dispose (GObject *object)
713 {
714   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
715
716   if (priv->action)
717     {
718       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
719       priv->action = NULL;
720     }
721
722   G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
723 }
724
725 static void
726 gtk_switch_class_init (GtkSwitchClass *klass)
727 {
728   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
729   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
730   gpointer activatable_iface;
731
732   g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
733
734   activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
735   switch_props[PROP_RELATED_ACTION] =
736     g_param_spec_override ("related-action",
737                            g_object_interface_find_property (activatable_iface,
738                                                              "related-action"));
739
740   switch_props[PROP_USE_ACTION_APPEARANCE] =
741     g_param_spec_override ("use-action-appearance",
742                            g_object_interface_find_property (activatable_iface,
743                                                              "use-action-appearance"));
744
745   /**
746    * GtkSwitch:active:
747    *
748    * Whether the #GtkSwitch widget is in its on or off state.
749    */
750   switch_props[PROP_ACTIVE] =
751     g_param_spec_boolean ("active",
752                           P_("Active"),
753                           P_("Whether the switch is on or off"),
754                           FALSE,
755                           GTK_PARAM_READWRITE);
756
757   gobject_class->set_property = gtk_switch_set_property;
758   gobject_class->get_property = gtk_switch_get_property;
759   gobject_class->dispose = gtk_switch_dispose;
760
761   g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
762
763   widget_class->get_preferred_width = gtk_switch_get_preferred_width;
764   widget_class->get_preferred_height = gtk_switch_get_preferred_height;
765   widget_class->size_allocate = gtk_switch_size_allocate;
766   widget_class->realize = gtk_switch_realize;
767   widget_class->unrealize = gtk_switch_unrealize;
768   widget_class->map = gtk_switch_map;
769   widget_class->unmap = gtk_switch_unmap;
770   widget_class->draw = gtk_switch_draw;
771   widget_class->button_press_event = gtk_switch_button_press;
772   widget_class->button_release_event = gtk_switch_button_release;
773   widget_class->motion_notify_event = gtk_switch_motion;
774   widget_class->enter_notify_event = gtk_switch_enter;
775   widget_class->leave_notify_event = gtk_switch_leave;
776   widget_class->key_release_event = gtk_switch_key_release;
777   widget_class->get_accessible = gtk_switch_get_accessible;
778
779   /**
780    * GtkSwitch:slider-width:
781    *
782    * The minimum width of the #GtkSwitch handle, in pixels.
783    */
784   gtk_widget_class_install_style_property (widget_class,
785                                            g_param_spec_int ("slider-width",
786                                                              P_("Slider Width"),
787                                                              P_("The minimum width of the handle"),
788                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
789                                                              DEFAULT_SLIDER_WIDTH,
790                                                              GTK_PARAM_READABLE));
791 }
792
793 static void
794 gtk_switch_init (GtkSwitch *self)
795 {
796   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
797   self->priv->use_action_appearance = TRUE;
798   gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
799   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
800 }
801
802 /**
803  * gtk_switch_new:
804  *
805  * Creates a new #GtkSwitch widget.
806  *
807  * Return value: the newly created #GtkSwitch instance
808  *
809  * Since: 3.0
810  */
811 GtkWidget *
812 gtk_switch_new (void)
813 {
814   return g_object_new (GTK_TYPE_SWITCH, NULL);
815 }
816
817 /**
818  * gtk_switch_set_active:
819  * @sw: a #GtkSwitch
820  * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
821  *
822  * Changes the state of @sw to the desired one.
823  *
824  * Since: 3.0
825  */
826 void
827 gtk_switch_set_active (GtkSwitch *sw,
828                        gboolean   is_active)
829 {
830   GtkSwitchPrivate *priv;
831
832   g_return_if_fail (GTK_IS_SWITCH (sw));
833
834   is_active = !!is_active;
835
836   priv = sw->priv;
837
838   if (priv->is_active != is_active)
839     {
840       AtkObject *accessible;
841       GtkWidget *widget;
842       GtkStyleContext *context;
843
844       widget = GTK_WIDGET (sw);
845       priv->is_active = is_active;
846
847       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
848
849       if (priv->action)
850         gtk_action_activate (priv->action);
851
852       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
853       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
854
855       if (gtk_widget_get_realized (widget))
856         {
857           context = gtk_widget_get_style_context (widget);
858           gtk_style_context_notify_state_change (context,
859                                                  gtk_widget_get_window (widget),
860                                                  NULL, GTK_STATE_ACTIVE, is_active);
861         }
862
863       gtk_widget_queue_draw (GTK_WIDGET (sw));
864     }
865 }
866
867 /**
868  * gtk_switch_get_active:
869  * @sw: a #GtkSwitch
870  *
871  * Gets whether the #GtkSwitch is in its "on" or "off" state.
872  *
873  * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
874  *
875  * Since: 3.0
876  */
877 gboolean
878 gtk_switch_get_active (GtkSwitch *sw)
879 {
880   g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
881
882   return sw->priv->is_active;
883 }
884
885 static void
886 gtk_switch_update (GtkActivatable *activatable,
887                    GtkAction      *action,
888                    const gchar    *property_name)
889 {
890   if (strcmp (property_name, "visible") == 0)
891     {
892       if (gtk_action_is_visible (action))
893         gtk_widget_show (GTK_WIDGET (activatable));
894       else
895         gtk_widget_hide (GTK_WIDGET (activatable));
896     }
897   else if (strcmp (property_name, "sensitive") == 0)
898     {
899       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
900     }
901   else if (strcmp (property_name, "active") == 0)
902     {
903       gtk_action_block_activate (action);
904       gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
905       gtk_action_unblock_activate (action);
906     }
907 }
908
909 static void
910 gtk_switch_sync_action_properties (GtkActivatable *activatable,
911                                    GtkAction      *action)
912 {
913   if (!action)
914     return;
915
916   if (gtk_action_is_visible (action))
917     gtk_widget_show (GTK_WIDGET (activatable));
918   else
919     gtk_widget_hide (GTK_WIDGET (activatable));
920
921   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
922
923   gtk_action_block_activate (action);
924   gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
925   gtk_action_unblock_activate (action);
926 }
927
928 static void
929 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
930 {
931   iface->update = gtk_switch_update;
932   iface->sync_action_properties = gtk_switch_sync_action_properties;
933 }
934
935 /* accessibility: object */
936
937 /* dummy typedefs */
938 typedef struct _GtkSwitchAccessible             GtkSwitchAccessible;
939 typedef struct _GtkSwitchAccessibleClass        GtkSwitchAccessibleClass;
940
941 ATK_DEFINE_TYPE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET);
942
943 static AtkStateSet *
944 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
945 {
946   AtkStateSet *state_set;
947   GtkWidget *widget;
948
949   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
950
951   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
952   if (widget == NULL)
953     return state_set;
954
955   if (gtk_switch_get_active (GTK_SWITCH (widget)))
956     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
957
958   return state_set;
959 }
960
961 static void
962 _gtk_switch_accessible_initialize (AtkObject *accessible,
963                                    gpointer   widget)
964 {
965   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
966
967   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
968   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
969   atk_object_set_description (accessible, _("Switches between on and off states"));
970 }
971
972 static void
973 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
974 {
975   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
976
977   atk_class->initialize = _gtk_switch_accessible_initialize;
978   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
979 }
980
981 static void
982 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
983 {
984 }
985
986 /* accessibility: factory */
987
988 typedef AtkObjectFactoryClass   GtkSwitchAccessibleFactoryClass;
989 typedef AtkObjectFactory        GtkSwitchAccessibleFactory;
990
991 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
992                gtk_switch_accessible_factory,
993                ATK_TYPE_OBJECT_FACTORY);
994
995 static GType
996 gtk_switch_accessible_factory_get_accessible_type (void)
997 {
998   return _gtk_switch_accessible_get_type ();
999 }
1000
1001 static AtkObject *
1002 gtk_switch_accessible_factory_create_accessible (GObject *obj)
1003 {
1004   AtkObject *accessible;
1005
1006   accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1007   atk_object_initialize (accessible, obj);
1008
1009   return accessible;
1010 }
1011
1012 static void
1013 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1014 {
1015   klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1016   klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1017 }
1018
1019 static void
1020 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)
1021 {
1022 }