]> Pileus Git - ~andy/gtk/blob - gtk/gtkswitch.c
Initial conversion of GailWidget to GtkWidgetAccessible
[~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 "gtkprivate.h"
47 #include "gtktoggleaction.h"
48 #include "gtkwidget.h"
49 #include "gtkmarshalers.h"
50 #include "a11y/gtkwidgetaccessible.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 GType _gtk_switch_accessible_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 void
664 gtk_switch_set_related_action (GtkSwitch *sw,
665                                GtkAction *action)
666 {
667   GtkSwitchPrivate *priv = sw->priv;
668
669   if (priv->action == action)
670     return;
671
672   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
673
674   priv->action = action;
675 }
676
677 static void
678 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
679                                       gboolean   use_appearance)
680 {
681   GtkSwitchPrivate *priv = sw->priv;
682
683   if (priv->use_action_appearance != use_appearance)
684     {
685       priv->use_action_appearance = use_appearance;
686
687       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
688     }
689 }
690
691 static void
692 gtk_switch_set_property (GObject      *gobject,
693                          guint         prop_id,
694                          const GValue *value,
695                          GParamSpec   *pspec)
696 {
697   GtkSwitch *sw = GTK_SWITCH (gobject);
698
699   switch (prop_id)
700     {
701     case PROP_ACTIVE:
702       gtk_switch_set_active (sw, g_value_get_boolean (value));
703       break;
704
705     case PROP_RELATED_ACTION:
706       gtk_switch_set_related_action (sw, g_value_get_object (value));
707       break;
708
709     case PROP_USE_ACTION_APPEARANCE:
710       gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
711       break;
712
713     default:
714       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
715     }
716 }
717
718 static void
719 gtk_switch_get_property (GObject    *gobject,
720                          guint       prop_id,
721                          GValue     *value,
722                          GParamSpec *pspec)
723 {
724   GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
725
726   switch (prop_id)
727     {
728     case PROP_ACTIVE:
729       g_value_set_boolean (value, priv->is_active);
730       break;
731
732     case PROP_RELATED_ACTION:
733       g_value_set_object (value, priv->action);
734       break;
735
736     case PROP_USE_ACTION_APPEARANCE:
737       g_value_set_boolean (value, priv->use_action_appearance);
738       break;
739
740     default:
741       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
742     }
743 }
744
745 static void
746 gtk_switch_dispose (GObject *object)
747 {
748   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
749
750   if (priv->action)
751     {
752       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
753       priv->action = NULL;
754     }
755
756   G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
757 }
758
759 static void
760 gtk_switch_class_init (GtkSwitchClass *klass)
761 {
762   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
763   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
764   gpointer activatable_iface;
765
766   g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
767
768   activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
769   switch_props[PROP_RELATED_ACTION] =
770     g_param_spec_override ("related-action",
771                            g_object_interface_find_property (activatable_iface,
772                                                              "related-action"));
773
774   switch_props[PROP_USE_ACTION_APPEARANCE] =
775     g_param_spec_override ("use-action-appearance",
776                            g_object_interface_find_property (activatable_iface,
777                                                              "use-action-appearance"));
778
779   /**
780    * GtkSwitch:active:
781    *
782    * Whether the #GtkSwitch widget is in its on or off state.
783    */
784   switch_props[PROP_ACTIVE] =
785     g_param_spec_boolean ("active",
786                           P_("Active"),
787                           P_("Whether the switch is on or off"),
788                           FALSE,
789                           GTK_PARAM_READWRITE);
790
791   gobject_class->set_property = gtk_switch_set_property;
792   gobject_class->get_property = gtk_switch_get_property;
793   gobject_class->dispose = gtk_switch_dispose;
794
795   g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
796
797   widget_class->get_preferred_width = gtk_switch_get_preferred_width;
798   widget_class->get_preferred_height = gtk_switch_get_preferred_height;
799   widget_class->size_allocate = gtk_switch_size_allocate;
800   widget_class->realize = gtk_switch_realize;
801   widget_class->unrealize = gtk_switch_unrealize;
802   widget_class->map = gtk_switch_map;
803   widget_class->unmap = gtk_switch_unmap;
804   widget_class->draw = gtk_switch_draw;
805   widget_class->button_press_event = gtk_switch_button_press;
806   widget_class->button_release_event = gtk_switch_button_release;
807   widget_class->motion_notify_event = gtk_switch_motion;
808   widget_class->enter_notify_event = gtk_switch_enter;
809   widget_class->leave_notify_event = gtk_switch_leave;
810
811   klass->activate = gtk_switch_activate;
812
813   /**
814    * GtkSwitch:slider-width:
815    *
816    * The minimum width of the #GtkSwitch handle, in pixels.
817    */
818   gtk_widget_class_install_style_property (widget_class,
819                                            g_param_spec_int ("slider-width",
820                                                              P_("Slider Width"),
821                                                              P_("The minimum width of the handle"),
822                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
823                                                              DEFAULT_SLIDER_WIDTH,
824                                                              GTK_PARAM_READABLE));
825
826   /**
827    * GtkSwitch::activate:
828    * @widget: the object which received the signal.
829    *
830    * The ::activate signal on GtkSwitch is an action signal and
831    * emitting it causes the switch to animate.
832    * Applications should never connect to this signal, but use the
833    * notify::active signal.
834    */
835   signals[ACTIVATE] =
836     g_signal_new (I_("activate"),
837                   G_OBJECT_CLASS_TYPE (gobject_class),
838                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
839                   G_STRUCT_OFFSET (GtkSwitchClass, activate),
840                   NULL, NULL,
841                   _gtk_marshal_VOID__VOID,
842                   G_TYPE_NONE, 0);
843   widget_class->activate_signal = signals[ACTIVATE];
844
845   gtk_widget_class_set_accessible_type (widget_class, _gtk_switch_accessible_get_type ());
846 }
847
848 static void
849 gtk_switch_init (GtkSwitch *self)
850 {
851   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
852   self->priv->use_action_appearance = TRUE;
853   gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
854   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
855 }
856
857 /**
858  * gtk_switch_new:
859  *
860  * Creates a new #GtkSwitch widget.
861  *
862  * Return value: the newly created #GtkSwitch instance
863  *
864  * Since: 3.0
865  */
866 GtkWidget *
867 gtk_switch_new (void)
868 {
869   return g_object_new (GTK_TYPE_SWITCH, NULL);
870 }
871
872 /**
873  * gtk_switch_set_active:
874  * @sw: a #GtkSwitch
875  * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
876  *
877  * Changes the state of @sw to the desired one.
878  *
879  * Since: 3.0
880  */
881 void
882 gtk_switch_set_active (GtkSwitch *sw,
883                        gboolean   is_active)
884 {
885   GtkSwitchPrivate *priv;
886
887   g_return_if_fail (GTK_IS_SWITCH (sw));
888
889   is_active = !!is_active;
890
891   priv = sw->priv;
892
893   if (priv->is_active != is_active)
894     {
895       AtkObject *accessible;
896       GtkWidget *widget;
897       GtkStyleContext *context;
898
899       widget = GTK_WIDGET (sw);
900       priv->is_active = is_active;
901
902       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
903
904       if (priv->action)
905         gtk_action_activate (priv->action);
906
907       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
908       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
909
910       if (gtk_widget_get_realized (widget))
911         {
912           context = gtk_widget_get_style_context (widget);
913           gtk_style_context_notify_state_change (context,
914                                                  gtk_widget_get_window (widget),
915                                                  NULL, GTK_STATE_ACTIVE, is_active);
916         }
917
918       gtk_widget_queue_draw (GTK_WIDGET (sw));
919     }
920 }
921
922 /**
923  * gtk_switch_get_active:
924  * @sw: a #GtkSwitch
925  *
926  * Gets whether the #GtkSwitch is in its "on" or "off" state.
927  *
928  * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
929  *
930  * Since: 3.0
931  */
932 gboolean
933 gtk_switch_get_active (GtkSwitch *sw)
934 {
935   g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
936
937   return sw->priv->is_active;
938 }
939
940 static void
941 gtk_switch_update (GtkActivatable *activatable,
942                    GtkAction      *action,
943                    const gchar    *property_name)
944 {
945   if (strcmp (property_name, "visible") == 0)
946     {
947       if (gtk_action_is_visible (action))
948         gtk_widget_show (GTK_WIDGET (activatable));
949       else
950         gtk_widget_hide (GTK_WIDGET (activatable));
951     }
952   else if (strcmp (property_name, "sensitive") == 0)
953     {
954       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
955     }
956   else if (strcmp (property_name, "active") == 0)
957     {
958       gtk_action_block_activate (action);
959       gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
960       gtk_action_unblock_activate (action);
961     }
962 }
963
964 static void
965 gtk_switch_sync_action_properties (GtkActivatable *activatable,
966                                    GtkAction      *action)
967 {
968   if (!action)
969     return;
970
971   if (gtk_action_is_visible (action))
972     gtk_widget_show (GTK_WIDGET (activatable));
973   else
974     gtk_widget_hide (GTK_WIDGET (activatable));
975
976   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
977
978   gtk_action_block_activate (action);
979   gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
980   gtk_action_unblock_activate (action);
981 }
982
983 static void
984 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
985 {
986   iface->update = gtk_switch_update;
987   iface->sync_action_properties = gtk_switch_sync_action_properties;
988 }
989
990 /* accessibility: object */
991
992 typedef struct _GtkSwitchAccessible      GtkSwitchAccessible;
993 typedef struct _GtkSwitchAccessibleClass GtkSwitchAccessibleClass;
994
995 struct _GtkSwitchAccessible
996 {
997   GtkAccessible object;
998
999   gchar *description;
1000   guint  action_idle;
1001 };
1002
1003 /* FIXME: We really want to use GailWidgetClass here */
1004 struct _GtkSwitchAccessibleClass
1005 {
1006   GtkAccessibleClass parent_class;
1007
1008   gpointer f1;
1009   gpointer f2;
1010 };
1011
1012 static void atk_action_interface_init (AtkActionIface *iface);
1013
1014 G_DEFINE_TYPE_WITH_CODE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET_ACCESSIBLE,
1015                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init))
1016
1017 static AtkStateSet *
1018 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
1019 {
1020   AtkStateSet *state_set;
1021   GtkWidget *widget;
1022
1023   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
1024
1025   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1026   if (widget == NULL)
1027     return state_set;
1028
1029   if (gtk_switch_get_active (GTK_SWITCH (widget)))
1030     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
1031
1032   return state_set;
1033 }
1034
1035 static void
1036 gtk_switch_accessible_finalize (GObject *obj)
1037 {
1038   GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)obj;
1039
1040   g_free (accessible->description);
1041
1042   if (accessible->action_idle)
1043     g_source_remove (accessible->action_idle);
1044
1045   G_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->finalize (obj);
1046 }
1047
1048 static void
1049 _gtk_switch_accessible_initialize (AtkObject *accessible,
1050                                    gpointer   widget)
1051 {
1052   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
1053
1054   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
1055   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
1056   atk_object_set_description (accessible, _("Switches between on and off states"));
1057 }
1058
1059 static void
1060 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
1061 {
1062   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1063   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
1064
1065   object_class->finalize = gtk_switch_accessible_finalize;
1066
1067   atk_class->initialize = _gtk_switch_accessible_initialize;
1068   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
1069 }
1070
1071 static void
1072 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
1073 {
1074   self->description = NULL;
1075   self->action_idle = 0;
1076 }
1077
1078 /* accessibility: action interface */
1079
1080 static gint
1081 gtk_switch_action_get_n_actions (AtkAction *action)
1082 {
1083   return 1;
1084 }
1085
1086 static const gchar *
1087 gtk_switch_action_get_name (AtkAction *action,
1088                             gint       i)
1089 {
1090   return "toggle";
1091 }
1092
1093 static const gchar *
1094 gtk_switch_action_get_description (AtkAction *action,
1095                                    gint       i)
1096 {
1097   GtkSwitchAccessible *accessible = (GtkSwitchAccessible *)action;
1098
1099   return accessible->description;
1100 }
1101
1102 static gboolean
1103 gtk_switch_action_set_description (AtkAction   *action,
1104                                    gint         i,
1105                                    const gchar *description)
1106 {
1107   GtkSwitchAccessible *accessible = (GtkSwitchAccessible*)action;
1108
1109   g_free (accessible->description);
1110   accessible->description = g_strdup (description);
1111
1112   return TRUE;
1113 }
1114
1115 static gboolean
1116 idle_do_action (gpointer data)
1117 {
1118   GtkSwitchAccessible *accessible = data;
1119   GtkWidget *widget;
1120   GtkSwitch *sw;
1121
1122   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (data));
1123   sw = GTK_SWITCH (widget);
1124
1125   accessible->action_idle = 0;
1126
1127   if (widget == NULL ||
1128       !gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1129     return FALSE;
1130
1131   gtk_switch_set_active (sw, !gtk_switch_get_active (sw));
1132
1133   return FALSE;
1134 }
1135
1136 static gboolean
1137 gtk_switch_action_do_action (AtkAction *action,
1138                              gint       i)
1139 {
1140   GtkSwitchAccessible *accessible;
1141   GtkWidget *widget;
1142
1143   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
1144   if (widget == NULL)
1145     return FALSE;
1146
1147   if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
1148     return FALSE;
1149
1150   accessible = (GtkSwitchAccessible *)action;
1151
1152   if (!accessible->action_idle)
1153     accessible->action_idle = gdk_threads_add_idle (idle_do_action, accessible);
1154
1155   return TRUE;
1156 }
1157
1158 static void
1159 atk_action_interface_init (AtkActionIface *iface)
1160 {
1161   iface->do_action = gtk_switch_action_do_action;
1162   iface->get_n_actions = gtk_switch_action_get_n_actions;
1163   iface->get_name = gtk_switch_action_get_name;
1164   iface->get_description = gtk_switch_action_get_description;
1165   iface->set_description = gtk_switch_action_set_description;
1166 }