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