]> Pileus Git - ~andy/gtk/blob - gtk/gtkswitch.c
switch: fetch the padding values from 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   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
289   GtkStyleContext *context;
290   GtkStateFlags state;
291   GtkBorder padding, border;
292   gint width, slider_width, focus_width, focus_pad;
293   PangoLayout *layout;
294   PangoRectangle logical_rect;
295
296   context = gtk_widget_get_style_context (widget);
297   state = gtk_widget_get_state_flags (widget);
298
299   if (priv->is_active)
300     state |= GTK_STATE_FLAG_ACTIVE;
301
302   gtk_style_context_save (context);
303
304   gtk_style_context_set_state (context, state);
305   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
306   gtk_style_context_get_padding (context, state, &padding);
307
308   width = padding.left + padding.right;
309
310   gtk_style_context_restore (context);
311
312   gtk_widget_style_get (widget,
313                         "slider-width", &slider_width,
314                         "focus-line-width", &focus_width,
315                         "focus-padding", &focus_pad,
316                         NULL);
317
318   width += 2 * (focus_width + focus_pad);
319
320   /* Translators: if the "on" state label requires more than three
321    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
322    * the state
323    */
324   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
325   pango_layout_get_extents (layout, NULL, &logical_rect);
326   pango_extents_to_pixels (&logical_rect, NULL);
327   width += MAX (logical_rect.width, slider_width);
328
329   /* Translators: if the "off" state label requires more than three
330    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
331    */
332   pango_layout_set_text (layout, C_("switch", "OFF"), -1);
333   pango_layout_get_extents (layout, NULL, &logical_rect);
334   pango_extents_to_pixels (&logical_rect, NULL);
335   width += MAX (logical_rect.width, slider_width);
336
337   g_object_unref (layout);
338
339   if (minimum)
340     *minimum = width;
341
342   if (natural)
343     *natural = width;
344 }
345
346 static void
347 gtk_switch_get_preferred_height (GtkWidget *widget,
348                                  gint      *minimum,
349                                  gint      *natural)
350 {
351   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
352   GtkStyleContext *context;
353   GtkStateFlags state;
354   GtkBorder padding;
355   gint height, focus_width, focus_pad;
356   PangoLayout *layout;
357   PangoRectangle logical_rect;
358   gchar *str;
359
360   context = gtk_widget_get_style_context (widget);
361   state = gtk_widget_get_state_flags (widget);
362
363   if (priv->is_active)
364     state |= GTK_STATE_FLAG_ACTIVE;
365
366   gtk_style_context_save (context);
367
368   gtk_style_context_set_state (context, state);
369   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
370   gtk_style_context_get_padding (context, state, &padding);
371
372   height = padding.top + padding.bottom;
373
374   gtk_style_context_restore (context);
375
376   gtk_widget_style_get (widget,
377                         "focus-line-width", &focus_width,
378                         "focus-padding", &focus_pad,
379                         NULL);
380
381   height += 2 * (focus_width + focus_pad);
382
383   str = g_strdup_printf ("%s%s",
384                          C_("switch", "ON"),
385                          C_("switch", "OFF"));
386
387   layout = gtk_widget_create_pango_layout (widget, str);
388   pango_layout_get_extents (layout, NULL, &logical_rect);
389   pango_extents_to_pixels (&logical_rect, NULL);
390   height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
391
392   g_object_unref (layout);
393   g_free (str);
394
395   if (minimum)
396     *minimum = height;
397
398   if (natural)
399     *natural = height;
400 }
401
402 static void
403 gtk_switch_size_allocate (GtkWidget     *widget,
404                           GtkAllocation *allocation)
405 {
406   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
407
408   gtk_widget_set_allocation (widget, allocation);
409
410   if (gtk_widget_get_realized (widget))
411     gdk_window_move_resize (priv->event_window,
412                             allocation->x,
413                             allocation->y,
414                             allocation->width,
415                             allocation->height);
416 }
417
418 static void
419 gtk_switch_realize (GtkWidget *widget)
420 {
421   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
422   GdkWindow *parent_window;
423   GdkWindowAttr attributes;
424   gint attributes_mask;
425   GtkAllocation allocation;
426
427   gtk_widget_set_realized (widget, TRUE);
428   parent_window = gtk_widget_get_parent_window (widget);
429   gtk_widget_set_window (widget, parent_window);
430   g_object_ref (parent_window);
431
432   gtk_widget_get_allocation (widget, &allocation);
433
434   attributes.window_type = GDK_WINDOW_CHILD;
435   attributes.wclass = GDK_INPUT_ONLY;
436   attributes.x = allocation.x;
437   attributes.y = allocation.y;
438   attributes.width = allocation.width;
439   attributes.height = allocation.height;
440   attributes.event_mask = gtk_widget_get_events (widget);
441   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
442                             GDK_BUTTON_RELEASE_MASK |
443                             GDK_BUTTON1_MOTION_MASK |
444                             GDK_POINTER_MOTION_HINT_MASK |
445                             GDK_POINTER_MOTION_MASK |
446                             GDK_ENTER_NOTIFY_MASK |
447                             GDK_LEAVE_NOTIFY_MASK);
448   attributes_mask = GDK_WA_X | GDK_WA_Y;
449
450   priv->event_window = gdk_window_new (parent_window,
451                                        &attributes,
452                                        attributes_mask);
453   gdk_window_set_user_data (priv->event_window, widget);
454 }
455
456 static void
457 gtk_switch_unrealize (GtkWidget *widget)
458 {
459   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
460
461   if (priv->event_window != NULL)
462     {
463       gdk_window_set_user_data (priv->event_window, NULL);
464       gdk_window_destroy (priv->event_window);
465       priv->event_window = NULL;
466     }
467
468   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
469 }
470
471 static void
472 gtk_switch_map (GtkWidget *widget)
473 {
474   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
475
476   GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
477
478   if (priv->event_window)
479     gdk_window_show (priv->event_window);
480 }
481
482 static void
483 gtk_switch_unmap (GtkWidget *widget)
484 {
485   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
486
487   if (priv->event_window)
488     gdk_window_hide (priv->event_window);
489
490   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
491 }
492
493 static inline void
494 gtk_switch_paint_handle (GtkWidget    *widget,
495                          cairo_t      *cr,
496                          GdkRectangle *box)
497 {
498   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
499   GtkStyleContext *context = gtk_widget_get_style_context (widget);
500   GtkStateFlags state;
501
502   state = gtk_widget_get_state_flags (widget);
503
504   if (priv->is_active)
505     state |= GTK_STATE_FLAG_ACTIVE;
506
507   gtk_style_context_save (context);
508   gtk_style_context_set_state (context, state);
509   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
510
511   gtk_render_slider (context, cr,
512                      box->x, box->y,
513                      box->width, box->height,
514                      GTK_ORIENTATION_HORIZONTAL);
515
516   gtk_style_context_restore (context);
517 }
518
519 static gboolean
520 gtk_switch_draw (GtkWidget *widget,
521                  cairo_t   *cr)
522 {
523   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
524   GtkStyleContext *context;
525   GdkRectangle handle;
526   PangoLayout *layout;
527   PangoRectangle rect;
528   gint label_x, label_y;
529   GtkStateFlags state;
530   GtkBorder padding;
531   gint focus_width, focus_pad;
532   gint x, y, width, height;
533
534   gtk_widget_style_get (widget,
535                         "focus-line-width", &focus_width,
536                         "focus-padding", &focus_pad,
537                         NULL);
538
539   context = gtk_widget_get_style_context (widget);
540   state = gtk_widget_get_state_flags (widget);
541
542   if (priv->is_active)
543     state |= GTK_STATE_FLAG_ACTIVE;
544
545   gtk_style_context_save (context);
546
547   gtk_style_context_set_state (context, state);
548   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
549
550   gtk_style_context_get_padding (context, state, &padding);
551
552   gtk_style_context_restore (context);
553
554   x = 0;
555   y = 0;
556   width = gtk_widget_get_allocated_width (widget);
557   height = gtk_widget_get_allocated_height (widget);
558
559   if (gtk_widget_has_focus (widget))
560     gtk_render_focus (context, cr, x, y, width, height);
561
562   x += focus_width + focus_pad;
563   y += focus_width + focus_pad;
564   width -= 2 * (focus_width + focus_pad);
565   height -= 2 * (focus_width + focus_pad);
566
567   gtk_style_context_save (context);
568   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
569   gtk_style_context_set_state (context, state);
570
571   gtk_render_background (context, cr, x, y, width, height);
572   gtk_render_frame (context, cr, x, y, width, height);
573
574   /* XXX the +1/-1 it's pixel wriggling after checking with the default
575    * theme and xmag
576    */
577   handle.y = y + padding.top + 1;
578   handle.width = (width - padding.left - padding.right) / 2;
579   handle.height = (height - padding.top - padding.bottom) - 1;
580
581   /* Translators: if the "on" state label requires more than three
582    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
583    * the state
584    */
585   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
586   pango_layout_get_extents (layout, NULL, &rect);
587   pango_extents_to_pixels (&rect, NULL);
588
589   label_x = x + padding.left
590           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
591   label_y = y + padding.top
592           + (height - rect.height - padding.top - padding.bottom) / 2;
593
594   gtk_render_layout (context, cr, label_x, label_y, layout);
595
596   g_object_unref (layout);
597
598   /* Translators: if the "off" state label requires more than three
599    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
600    */
601   layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
602   pango_layout_get_extents (layout, NULL, &rect);
603   pango_extents_to_pixels (&rect, NULL);
604
605   label_x = x + padding.left
606           + (width / 2)
607           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
608   label_y = y + padding.top
609           + (height - rect.height - padding.top - padding.bottom) / 2;
610
611   gtk_render_layout (context, cr, label_x, label_y, layout);
612
613   g_object_unref (layout);
614
615   if (priv->is_dragging)
616     handle.x = x + priv->handle_x;
617   else if (priv->is_active)
618     handle.x = x + width - handle.width - padding.right;
619   else
620     handle.x = x + padding.left;
621
622   gtk_style_context_restore (context);
623
624   gtk_switch_paint_handle (widget, cr, &handle);
625
626   return FALSE;
627 }
628
629 static AtkObject *
630 gtk_switch_get_accessible (GtkWidget *widget)
631 {
632   static gboolean first_time = TRUE;
633
634   if (G_UNLIKELY (first_time))
635     {
636       _gtk_accessible_set_factory_type (GTK_TYPE_SWITCH,
637                                         gtk_switch_accessible_factory_get_type ());
638       first_time = FALSE;
639     }
640
641   return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
642 }
643
644 static void
645 gtk_switch_set_related_action (GtkSwitch *sw,
646                                GtkAction *action)
647 {
648   GtkSwitchPrivate *priv = sw->priv;
649
650   if (priv->action == action)
651     return;
652
653   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
654
655   priv->action = action;
656 }
657
658 static void
659 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
660                                       gboolean   use_appearance)
661 {
662   GtkSwitchPrivate *priv = sw->priv;
663
664   if (priv->use_action_appearance != use_appearance)
665     {
666       priv->use_action_appearance = use_appearance;
667
668       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
669     }
670 }
671
672 static void
673 gtk_switch_set_property (GObject      *gobject,
674                          guint         prop_id,
675                          const GValue *value,
676                          GParamSpec   *pspec)
677 {
678   GtkSwitch *sw = GTK_SWITCH (gobject);
679
680   switch (prop_id)
681     {
682     case PROP_ACTIVE:
683       gtk_switch_set_active (sw, g_value_get_boolean (value));
684       break;
685
686     case PROP_RELATED_ACTION:
687       gtk_switch_set_related_action (sw, g_value_get_object (value));
688       break;
689
690     case PROP_USE_ACTION_APPEARANCE:
691       gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
692       break;
693
694     default:
695       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
696     }
697 }
698
699 static void
700 gtk_switch_get_property (GObject    *gobject,
701                          guint       prop_id,
702                          GValue     *value,
703                          GParamSpec *pspec)
704 {
705   GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
706
707   switch (prop_id)
708     {
709     case PROP_ACTIVE:
710       g_value_set_boolean (value, priv->is_active);
711       break;
712
713     case PROP_RELATED_ACTION:
714       g_value_set_object (value, priv->action);
715       break;
716
717     case PROP_USE_ACTION_APPEARANCE:
718       g_value_set_boolean (value, priv->use_action_appearance);
719       break;
720
721     default:
722       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
723     }
724 }
725
726 static void
727 gtk_switch_dispose (GObject *object)
728 {
729   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
730
731   if (priv->action)
732     {
733       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
734       priv->action = NULL;
735     }
736
737   G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
738 }
739
740 static void
741 gtk_switch_class_init (GtkSwitchClass *klass)
742 {
743   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
744   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
745   gpointer activatable_iface;
746
747   g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
748
749   activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
750   switch_props[PROP_RELATED_ACTION] =
751     g_param_spec_override ("related-action",
752                            g_object_interface_find_property (activatable_iface,
753                                                              "related-action"));
754
755   switch_props[PROP_USE_ACTION_APPEARANCE] =
756     g_param_spec_override ("use-action-appearance",
757                            g_object_interface_find_property (activatable_iface,
758                                                              "use-action-appearance"));
759
760   /**
761    * GtkSwitch:active:
762    *
763    * Whether the #GtkSwitch widget is in its on or off state.
764    */
765   switch_props[PROP_ACTIVE] =
766     g_param_spec_boolean ("active",
767                           P_("Active"),
768                           P_("Whether the switch is on or off"),
769                           FALSE,
770                           GTK_PARAM_READWRITE);
771
772   gobject_class->set_property = gtk_switch_set_property;
773   gobject_class->get_property = gtk_switch_get_property;
774   gobject_class->dispose = gtk_switch_dispose;
775
776   g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
777
778   widget_class->get_preferred_width = gtk_switch_get_preferred_width;
779   widget_class->get_preferred_height = gtk_switch_get_preferred_height;
780   widget_class->size_allocate = gtk_switch_size_allocate;
781   widget_class->realize = gtk_switch_realize;
782   widget_class->unrealize = gtk_switch_unrealize;
783   widget_class->map = gtk_switch_map;
784   widget_class->unmap = gtk_switch_unmap;
785   widget_class->draw = gtk_switch_draw;
786   widget_class->button_press_event = gtk_switch_button_press;
787   widget_class->button_release_event = gtk_switch_button_release;
788   widget_class->motion_notify_event = gtk_switch_motion;
789   widget_class->enter_notify_event = gtk_switch_enter;
790   widget_class->leave_notify_event = gtk_switch_leave;
791   widget_class->get_accessible = gtk_switch_get_accessible;
792
793   klass->activate = gtk_switch_activate;
794
795   /**
796    * GtkSwitch:slider-width:
797    *
798    * The minimum width of the #GtkSwitch handle, in pixels.
799    */
800   gtk_widget_class_install_style_property (widget_class,
801                                            g_param_spec_int ("slider-width",
802                                                              P_("Slider Width"),
803                                                              P_("The minimum width of the handle"),
804                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
805                                                              DEFAULT_SLIDER_WIDTH,
806                                                              GTK_PARAM_READABLE));
807
808   /**
809    * GtkSwitch::activate:
810    * @widget: the object which received the signal.
811    *
812    * The ::activate signal on GtkSwitch is an action signal and
813    * emitting it causes the switch to animate.
814    * Applications should never connect to this signal, but use the
815    * notify::active signal.
816    */
817   signals[ACTIVATE] =
818     g_signal_new (I_("activate"),
819                   G_OBJECT_CLASS_TYPE (gobject_class),
820                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
821                   G_STRUCT_OFFSET (GtkSwitchClass, activate),
822                   NULL, NULL,
823                   _gtk_marshal_VOID__VOID,
824                   G_TYPE_NONE, 0);
825   widget_class->activate_signal = signals[ACTIVATE];
826
827 }
828
829 static void
830 gtk_switch_init (GtkSwitch *self)
831 {
832   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
833   self->priv->use_action_appearance = TRUE;
834   gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
835   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
836 }
837
838 /**
839  * gtk_switch_new:
840  *
841  * Creates a new #GtkSwitch widget.
842  *
843  * Return value: the newly created #GtkSwitch instance
844  *
845  * Since: 3.0
846  */
847 GtkWidget *
848 gtk_switch_new (void)
849 {
850   return g_object_new (GTK_TYPE_SWITCH, NULL);
851 }
852
853 /**
854  * gtk_switch_set_active:
855  * @sw: a #GtkSwitch
856  * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
857  *
858  * Changes the state of @sw to the desired one.
859  *
860  * Since: 3.0
861  */
862 void
863 gtk_switch_set_active (GtkSwitch *sw,
864                        gboolean   is_active)
865 {
866   GtkSwitchPrivate *priv;
867
868   g_return_if_fail (GTK_IS_SWITCH (sw));
869
870   is_active = !!is_active;
871
872   priv = sw->priv;
873
874   if (priv->is_active != is_active)
875     {
876       AtkObject *accessible;
877       GtkWidget *widget;
878       GtkStyleContext *context;
879
880       widget = GTK_WIDGET (sw);
881       priv->is_active = is_active;
882
883       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
884
885       if (priv->action)
886         gtk_action_activate (priv->action);
887
888       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
889       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
890
891       if (gtk_widget_get_realized (widget))
892         {
893           context = gtk_widget_get_style_context (widget);
894           gtk_style_context_notify_state_change (context,
895                                                  gtk_widget_get_window (widget),
896                                                  NULL, GTK_STATE_ACTIVE, is_active);
897         }
898
899       gtk_widget_queue_draw (GTK_WIDGET (sw));
900     }
901 }
902
903 /**
904  * gtk_switch_get_active:
905  * @sw: a #GtkSwitch
906  *
907  * Gets whether the #GtkSwitch is in its "on" or "off" state.
908  *
909  * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
910  *
911  * Since: 3.0
912  */
913 gboolean
914 gtk_switch_get_active (GtkSwitch *sw)
915 {
916   g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
917
918   return sw->priv->is_active;
919 }
920
921 static void
922 gtk_switch_update (GtkActivatable *activatable,
923                    GtkAction      *action,
924                    const gchar    *property_name)
925 {
926   if (strcmp (property_name, "visible") == 0)
927     {
928       if (gtk_action_is_visible (action))
929         gtk_widget_show (GTK_WIDGET (activatable));
930       else
931         gtk_widget_hide (GTK_WIDGET (activatable));
932     }
933   else if (strcmp (property_name, "sensitive") == 0)
934     {
935       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
936     }
937   else if (strcmp (property_name, "active") == 0)
938     {
939       gtk_action_block_activate (action);
940       gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
941       gtk_action_unblock_activate (action);
942     }
943 }
944
945 static void
946 gtk_switch_sync_action_properties (GtkActivatable *activatable,
947                                    GtkAction      *action)
948 {
949   if (!action)
950     return;
951
952   if (gtk_action_is_visible (action))
953     gtk_widget_show (GTK_WIDGET (activatable));
954   else
955     gtk_widget_hide (GTK_WIDGET (activatable));
956
957   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
958
959   gtk_action_block_activate (action);
960   gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
961   gtk_action_unblock_activate (action);
962 }
963
964 static void
965 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
966 {
967   iface->update = gtk_switch_update;
968   iface->sync_action_properties = gtk_switch_sync_action_properties;
969 }
970
971 /* accessibility: object */
972
973 typedef struct _GtkSwitchAccessible      GtkSwitchAccessible;
974 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
975
976 struct _GtkSwitchAccessible
977 {
978   GtkAccessible object;
979
980   gchar *description;
981   guint  action_idle;
982 };
983
984 static void atk_action_interface_init (AtkActionIface *iface);
985
986 ATK_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_SWITCH,
987                            G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
988
989 static AtkStateSet *
990 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
991 {
992   AtkStateSet *state_set;
993   GtkWidget *widget;
994
995   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
996
997   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
998   if (widget == NULL)
999     return state_set;
1000
1001   if (gtk_switch_get_active (GTK_SWITCH (widget)))
1002     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
1003
1004   return state_set;
1005 }
1006
1007 static void
1008 gtk_switch_accessible_finalize (GObject *obj)
1009 {
1010   GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)obj;
1011
1012   g_free (accessible->description);
1013
1014   if (accessible->action_idle)
1015     g_source_remove (accessible->action_idle);
1016
1017   G_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->finalize (obj);
1018 }
1019
1020 static void
1021 _gtk_switch_accessible_initialize (AtkObject *accessible,
1022                                    gpointer   widget)
1023 {
1024   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
1025
1026   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
1027   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
1028   atk_object_set_description (accessible, _("Switches between on and off states"));
1029 }
1030
1031 static void
1032 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
1033 {
1034   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1035   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
1036
1037   object_class->finalize = gtk_switch_accessible_finalize;
1038
1039   atk_class->initialize = _gtk_switch_accessible_initialize;
1040   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
1041 }
1042
1043 static void
1044 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
1045 {
1046   self->description = NULL;
1047   self->action_idle = 0;
1048 }
1049
1050 /* accessibility: action interface */
1051
1052 static gint
1053 gtk_switch_action_get_n_actions (AtkAction *action)
1054 {
1055   return 1;
1056 }
1057
1058 static const gchar *
1059 gtk_switch_action_get_name (AtkAction *action,
1060                             gint       i)
1061 {
1062   return "toggle";
1063 }
1064
1065 static const gchar *
1066 gtk_switch_action_get_description (AtkAction *action,
1067                                    gint       i)
1068 {
1069   GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1070
1071   return accessible->description;
1072 }
1073
1074 static gboolean
1075 gtk_switch_action_set_description (AtkAction   *action,
1076                                    gint         i,
1077                                    const gchar *description)
1078 {
1079   GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1080
1081   g_free (accessible->description);
1082   accessible->description = g_strdup (description);
1083
1084   return TRUE;
1085 }
1086
1087 static gboolean
1088 idle_do_action (gpointer data)
1089 {
1090   GtkSwitchAccessible *accessible = data;
1091   GtkWidget *widget;
1092   GtkSwitch *sw;
1093
1094   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (data));
1095   sw = GTK_SWITCH (widget);
1096
1097   accessible->action_idle = 0;
1098
1099   if (widget == NULL ||
1100       !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1101     return FALSE;
1102
1103   gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
1104
1105   return FALSE;
1106 }
1107
1108 static gboolean
1109 gtk_switch_action_do_action (AtkAction *action,
1110                              gint       i)
1111 {
1112   GtkSwitchAccessible *accessible;
1113   GtkWidget *widget;
1114
1115   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1116   if (widget == NULL)
1117     return FALSE;
1118
1119   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1120     return FALSE;
1121
1122   accessible = (GtkSwitchAccessible *)action;
1123
1124   if (!accessible->action_idle)
1125     accessible->action_idle = gdk_threads_add_idle (idle_do_action, accessible);
1126
1127   return TRUE;
1128 }
1129
1130 static void
1131 atk_action_interface_init (AtkActionIface *iface)
1132 {
1133   iface->do_action = gtk_switch_action_do_action;
1134   iface->get_n_actions = gtk_switch_action_get_n_actions;
1135   iface->get_name = gtk_switch_action_get_name;
1136   iface->get_description = gtk_switch_action_get_description;
1137   iface->set_description = gtk_switch_action_set_description;
1138 }
1139
1140 /* accessibility: factory */
1141
1142 typedef AtkObjectFactoryClass   GtkSwitchAccessibleFactoryClass;
1143 typedef AtkObjectFactory        GtkSwitchAccessibleFactory;
1144
1145 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
1146                gtk_switch_accessible_factory,
1147                ATK_TYPE_OBJECT_FACTORY);
1148
1149 static GType
1150 gtk_switch_accessible_factory_get_accessible_type (void)
1151 {
1152   return _gtk_switch_accessible_get_type ();
1153 }
1154
1155 static AtkObject *
1156 gtk_switch_accessible_factory_create_accessible (GObject *obj)
1157 {
1158   AtkObject *accessible;
1159
1160   accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1161   atk_object_initialize (accessible, obj);
1162
1163   return accessible;
1164 }
1165
1166 static void
1167 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1168 {
1169   klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1170   klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1171 }
1172
1173 static void
1174 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)
1175 {
1176 }