]> Pileus Git - ~andy/gtk/blob - gtk/gtkswitch.c
Merge branch 'master' into treeview-refactor
[~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 <gdk/gdkkeysyms.h>
44
45 #include "gtkaccessible.h"
46 #include "gtkactivatable.h"
47 #include "gtkintl.h"
48 #include "gtkstyle.h"
49 #include "gtkprivate.h"
50 #include "gtktoggleaction.h"
51 #include "gtkwidget.h"
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 static GParamSpec *switch_props[LAST_PROP] = { NULL, };
83
84 static GType gtk_switch_accessible_factory_get_type (void);
85
86 static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);
87
88 G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
89                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
90                                                 gtk_switch_activatable_interface_init));
91
92 static gboolean
93 gtk_switch_button_press (GtkWidget      *widget,
94                          GdkEventButton *event)
95 {
96   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
97   GtkAllocation allocation;
98
99   gtk_widget_get_allocation (widget, &allocation);
100
101   if (priv->is_active)
102     {
103       /* if the event occurred in the "off" area, then this
104        * is a direct toggle
105        */
106       if (event->x <= allocation.width / 2)
107         {
108           priv->in_press = TRUE;
109           return FALSE;
110         }
111
112       priv->offset = event->x - allocation.width / 2;
113     }
114   else
115     {
116       /* if the event occurred in the "on" area, then this
117        * is a direct toggle
118        */
119       if (event->x >= allocation.width / 2)
120         {
121           priv->in_press = TRUE;
122           return FALSE;
123         }
124
125       priv->offset = event->x;
126     }
127
128   priv->drag_start = event->x;
129
130   g_object_get (gtk_widget_get_settings (widget),
131                 "gtk-dnd-drag-threshold", &priv->drag_threshold,
132                 NULL);
133
134   return FALSE;
135 }
136
137 static gboolean
138 gtk_switch_motion (GtkWidget      *widget,
139                    GdkEventMotion *event)
140 {
141   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
142
143   /* if this is a direct toggle we don't handle motion */
144   if (priv->in_press)
145     return FALSE;
146
147   if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
148     return TRUE;
149
150   if (event->state & GDK_BUTTON1_MASK)
151     {
152       gint position = event->x - priv->offset;
153       GtkAllocation allocation;
154       GtkStyle *style;
155
156       style = gtk_widget_get_style (widget);
157       gtk_widget_get_allocation (widget, &allocation);
158
159       /* constrain the handle within the trough width */
160       if (position > (allocation.width / 2 - style->xthickness))
161         priv->handle_x = allocation.width / 2 - style->xthickness;
162       else if (position < style->xthickness)
163         priv->handle_x = style->xthickness;
164       else
165         priv->handle_x = position;
166
167       priv->is_dragging = TRUE;
168
169       /* we need to redraw the handle */
170       gtk_widget_queue_draw (widget);
171
172       return TRUE;
173     }
174
175   return FALSE;
176 }
177
178 static gboolean
179 gtk_switch_button_release (GtkWidget      *widget,
180                            GdkEventButton *event)
181 {
182   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
183   GtkAllocation allocation;
184
185   gtk_widget_get_allocation (widget, &allocation);
186
187   /* dragged toggles have a "soft" grab, so we don't care whether we
188    * are in the switch or not when the button is released; we do care
189    * for direct toggles, instead
190    */
191   if (!priv->is_dragging && !priv->in_switch)
192     return FALSE;
193
194   /* direct toggle */
195   if (priv->in_press)
196     {
197       priv->in_press = FALSE;
198       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
199
200       return TRUE;
201     }
202
203   /* dragged toggle */
204   if (priv->is_dragging)
205     {
206       priv->is_dragging = FALSE;
207
208       /* if half the handle passed the middle of the switch, then we
209        * consider it to be on
210        */
211       if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
212         {
213           gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
214           priv->handle_x = allocation.width / 2;
215         }
216       else
217         {
218           gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
219           priv->handle_x = 0;
220         }
221
222       gtk_widget_queue_draw (widget);
223
224       return TRUE;
225     }
226
227   return FALSE;
228 }
229
230 static gboolean
231 gtk_switch_enter (GtkWidget        *widget,
232                   GdkEventCrossing *event)
233 {
234   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
235
236   if (event->window == priv->event_window)
237     priv->in_switch = TRUE;
238
239   return FALSE;
240 }
241
242 static gboolean
243 gtk_switch_leave (GtkWidget        *widget,
244                   GdkEventCrossing *event)
245 {
246   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
247
248   if (event->window == priv->event_window)
249     priv->in_switch = FALSE;
250
251   return FALSE;
252 }
253
254 static gboolean
255 gtk_switch_key_release (GtkWidget   *widget,
256                         GdkEventKey *event)
257 {
258   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
259
260   if (event->keyval == GDK_KEY_Return ||
261       event->keyval == GDK_KEY_KP_Enter ||
262       event->keyval == GDK_KEY_ISO_Enter ||
263       event->keyval == GDK_KEY_space ||
264       event->keyval == GDK_KEY_KP_Space)
265     {
266       gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);
267     }
268
269   return FALSE;
270 }
271
272 static void
273 gtk_switch_get_preferred_width (GtkWidget *widget,
274                                 gint      *minimum,
275                                 gint      *natural)
276 {
277   GtkStyle *style = gtk_widget_get_style (widget);
278   gint width, slider_width, focus_width, focus_pad;
279   PangoLayout *layout;
280   PangoRectangle logical_rect;
281
282   width = style->xthickness * 2;
283
284   gtk_widget_style_get (widget,
285                         "slider-width", &slider_width,
286                         "focus-line-width", &focus_width,
287                         "focus-padding", &focus_pad,
288                         NULL);
289
290   width += 2 * (focus_width + focus_pad);
291
292   /* Translators: if the "on" state label requires more than three
293    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
294    * the state
295    */
296   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
297   pango_layout_get_extents (layout, NULL, &logical_rect);
298   pango_extents_to_pixels (&logical_rect, NULL);
299   width += MAX (logical_rect.width, slider_width);
300
301   /* Translators: if the "off" state label requires more than three
302    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
303    */
304   pango_layout_set_text (layout, C_("switch", "OFF"), -1);
305   pango_layout_get_extents (layout, NULL, &logical_rect);
306   pango_extents_to_pixels (&logical_rect, NULL);
307   width += MAX (logical_rect.width, slider_width);
308
309   g_object_unref (layout);
310
311   if (minimum)
312     *minimum = width;
313
314   if (natural)
315     *natural = width;
316 }
317
318 static void
319 gtk_switch_get_preferred_height (GtkWidget *widget,
320                                  gint      *minimum,
321                                  gint      *natural)
322 {
323   GtkStyle *style = gtk_widget_get_style (widget);
324   gint height, focus_width, focus_pad;
325   PangoLayout *layout;
326   PangoRectangle logical_rect;
327   gchar *str;
328
329   height = style->ythickness * 2;
330
331   gtk_widget_style_get (widget,
332                         "focus-line-width", &focus_width,
333                         "focus-padding", &focus_pad,
334                         NULL);
335
336   height += 2 * (focus_width + focus_pad);
337
338   str = g_strdup_printf ("%s%s",
339                          C_("switch", "ON"),
340                          C_("switch", "OFF"));
341
342   layout = gtk_widget_create_pango_layout (widget, str);
343   pango_layout_get_extents (layout, NULL, &logical_rect);
344   pango_extents_to_pixels (&logical_rect, NULL);
345   height += MAX (DEFAULT_SLIDER_HEIGHT, logical_rect.height);
346
347   g_object_unref (layout);
348   g_free (str);
349
350   if (minimum)
351     *minimum = height;
352
353   if (natural)
354     *natural = height;
355 }
356
357 static void
358 gtk_switch_size_allocate (GtkWidget     *widget,
359                           GtkAllocation *allocation)
360 {
361   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
362
363   gtk_widget_set_allocation (widget, allocation);
364
365   if (gtk_widget_get_realized (widget))
366     gdk_window_move_resize (priv->event_window,
367                             allocation->x,
368                             allocation->y,
369                             allocation->width,
370                             allocation->height);
371 }
372
373 static void
374 gtk_switch_realize (GtkWidget *widget)
375 {
376   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
377   GdkWindow *parent_window;
378   GdkWindowAttr attributes;
379   gint attributes_mask;
380   GtkAllocation allocation;
381
382   gtk_widget_set_realized (widget, TRUE);
383   parent_window = gtk_widget_get_parent_window (widget);
384   gtk_widget_set_window (widget, parent_window);
385   g_object_ref (parent_window);
386
387   gtk_widget_get_allocation (widget, &allocation);
388
389   attributes.window_type = GDK_WINDOW_CHILD;
390   attributes.wclass = GDK_INPUT_ONLY;
391   attributes.x = allocation.x;
392   attributes.y = allocation.y;
393   attributes.width = allocation.width;
394   attributes.height = allocation.height;
395   attributes.event_mask = gtk_widget_get_events (widget);
396   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
397                             GDK_BUTTON_RELEASE_MASK |
398                             GDK_BUTTON1_MOTION_MASK |
399                             GDK_POINTER_MOTION_HINT_MASK |
400                             GDK_POINTER_MOTION_MASK |
401                             GDK_ENTER_NOTIFY_MASK |
402                             GDK_LEAVE_NOTIFY_MASK);
403   attributes_mask = GDK_WA_X | GDK_WA_Y;
404
405   priv->event_window = gdk_window_new (parent_window,
406                                        &attributes,
407                                        attributes_mask);
408   gdk_window_set_user_data (priv->event_window, widget);
409
410   gtk_widget_style_attach (widget);
411 }
412
413 static void
414 gtk_switch_unrealize (GtkWidget *widget)
415 {
416   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
417
418   if (priv->event_window != NULL)
419     {
420       gdk_window_set_user_data (priv->event_window, NULL);
421       gdk_window_destroy (priv->event_window);
422       priv->event_window = NULL;
423     }
424
425   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
426 }
427
428 static void
429 gtk_switch_map (GtkWidget *widget)
430 {
431   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
432
433   GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
434
435   if (priv->event_window)
436     gdk_window_show (priv->event_window);
437 }
438
439 static void
440 gtk_switch_unmap (GtkWidget *widget)
441 {
442   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
443
444   if (priv->event_window)
445     gdk_window_hide (priv->event_window);
446
447   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
448 }
449
450 static inline void
451 gtk_switch_paint_handle (GtkWidget    *widget,
452                          cairo_t      *cr,
453                          GdkRectangle *box)
454 {
455   GtkStyle *style = gtk_widget_get_style (widget);
456
457   gtk_paint_slider (style, cr,
458                     gtk_widget_get_state (widget),
459                     GTK_SHADOW_OUT,
460                     GTK_WIDGET (widget), "switch-slider",
461                     box->x,
462                     box->y,
463                     box->width,
464                     box->height,
465                     GTK_ORIENTATION_HORIZONTAL);
466 }
467
468 static gboolean
469 gtk_switch_draw (GtkWidget *widget,
470                  cairo_t   *cr)
471 {
472   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
473   GtkStyle *style;
474   GdkRectangle handle;
475   PangoLayout *layout;
476   PangoRectangle rect;
477   gint label_x, label_y;
478   GtkStateType state;
479   gint focus_width, focus_pad;
480   gint x, y, width, height;
481
482   gtk_widget_style_get (widget,
483                         "focus-line-width", &focus_width,
484                         "focus-padding", &focus_pad,
485                         NULL);
486
487   style = gtk_widget_get_style (widget);
488
489   x = 0;
490   y = 0;
491   width = gtk_widget_get_allocated_width (widget);
492   height = gtk_widget_get_allocated_height (widget);
493
494   if (gtk_widget_has_focus (widget))
495     gtk_paint_focus (style, cr,
496                      gtk_widget_get_state (widget),
497                      widget, "button",
498                      x, y, width, height);
499
500   x += focus_width + focus_pad;
501   y += focus_width + focus_pad;
502   width -= 2 * (focus_width + focus_pad);
503   height -= 2 * (focus_width + focus_pad);
504
505   state = priv->is_active ? GTK_STATE_SELECTED : gtk_widget_get_state (widget);
506
507   /* background - XXX should this be a flat box instead? we're missing
508    * the border given by the shadow with that, which would require
509    * fixing all theme engines to add a subtle border for this widget
510    */
511   gtk_paint_box (style, cr,
512                  state,
513                  GTK_SHADOW_IN,
514                  widget, "switch-background",
515                  x, y, width, height);
516
517   if (!gtk_widget_is_sensitive (widget))
518     state = GTK_STATE_INSENSITIVE;
519
520   /* XXX the +1/-1 it's pixel wriggling after checking with the default
521    * theme and xmag
522    */
523   handle.y = y + style->ythickness + 1;
524   handle.width = (width - style->xthickness * 2) / 2;
525   handle.height = (height - style->ythickness * 2) - 1;
526
527   /* Translators: if the "on" state label requires more than three
528    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
529    * the state
530    */
531   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
532   pango_layout_get_extents (layout, NULL, &rect);
533   pango_extents_to_pixels (&rect, NULL);
534
535   label_x = x + style->xthickness
536           + ((width / 2) - rect.width - (style->xthickness * 2)) / 2;
537   label_y = y + style->ythickness
538           + (height - rect.height - (style->ythickness * 2)) / 2;
539
540   gtk_paint_layout (style, cr,
541                     state,
542                     FALSE,
543                     widget, "switch-on-label",
544                     label_x, label_y,
545                     layout);
546
547   g_object_unref (layout);
548
549   /* Translators: if the "off" state label requires more than three
550    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
551    */
552   layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
553   pango_layout_get_extents (layout, NULL, &rect);
554   pango_extents_to_pixels (&rect, NULL);
555
556   label_x = x + style->xthickness
557           + (width / 2)
558           + ((width / 2) - rect.width - (style->xthickness * 2)) / 2;
559   label_y = y + style->ythickness
560           + (height - rect.height - (style->ythickness * 2)) / 2;
561
562   gtk_paint_layout (style, cr,
563                     state,
564                     FALSE,
565                     widget, "switch-off-label",
566                     label_x, label_y,
567                     layout);
568
569   g_object_unref (layout);
570
571   if (priv->is_dragging)
572     handle.x = x + priv->handle_x;
573   else if (priv->is_active)
574     handle.x = x + width - handle.width - style->xthickness;
575   else
576     handle.x = x + style->xthickness;
577
578   gtk_switch_paint_handle (widget, cr, &handle);
579
580   return FALSE;
581 }
582
583 static AtkObject *
584 gtk_switch_get_accessible (GtkWidget *widget)
585 {
586   static gboolean first_time = TRUE;
587
588   if (G_UNLIKELY (first_time))
589     {
590       AtkObjectFactory *factory;
591       AtkRegistry *registry;
592       GType derived_type;
593       GType derived_atk_type;
594
595       /* Figure out whether accessibility is enabled by looking at the
596        * type of the accessible object which would be created for the
597        * parent type of GtkSwitch
598        */
599       derived_type = g_type_parent (GTK_TYPE_SWITCH);
600
601       registry = atk_get_default_registry ();
602       factory = atk_registry_get_factory (registry, derived_type);
603       derived_atk_type = atk_object_factory_get_accessible_type (factory);
604       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
605         atk_registry_set_factory_type (registry,
606                                        GTK_TYPE_SWITCH,
607                                        gtk_switch_accessible_factory_get_type ());
608
609       first_time = FALSE;
610     }
611
612   return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
613 }
614
615 static void
616 gtk_switch_set_related_action (GtkSwitch *sw,
617                                GtkAction *action)
618 {
619   GtkSwitchPrivate *priv = sw->priv;
620
621   if (priv->action == action)
622     return;
623
624   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
625
626   priv->action = action;
627 }
628
629 static void
630 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
631                                       gboolean   use_appearance)
632 {
633   GtkSwitchPrivate *priv = sw->priv;
634
635   if (priv->use_action_appearance != use_appearance)
636     {
637       priv->use_action_appearance = use_appearance;
638
639       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
640     }
641 }
642
643 static void
644 gtk_switch_set_property (GObject      *gobject,
645                          guint         prop_id,
646                          const GValue *value,
647                          GParamSpec   *pspec)
648 {
649   GtkSwitch *sw = GTK_SWITCH (gobject);
650
651   switch (prop_id)
652     {
653     case PROP_ACTIVE:
654       gtk_switch_set_active (sw, g_value_get_boolean (value));
655       break;
656
657     case PROP_RELATED_ACTION:
658       gtk_switch_set_related_action (sw, g_value_get_object (value));
659       break;
660
661     case PROP_USE_ACTION_APPEARANCE:
662       gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
663       break;
664
665     default:
666       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
667     }
668 }
669
670 static void
671 gtk_switch_get_property (GObject    *gobject,
672                          guint       prop_id,
673                          GValue     *value,
674                          GParamSpec *pspec)
675 {
676   GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
677
678   switch (prop_id)
679     {
680     case PROP_ACTIVE:
681       g_value_set_boolean (value, priv->is_active);
682       break;
683
684     case PROP_RELATED_ACTION:
685       g_value_set_object (value, priv->action);
686       break;
687
688     case PROP_USE_ACTION_APPEARANCE:
689       g_value_set_boolean (value, priv->use_action_appearance);
690       break;
691
692     default:
693       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
694     }
695 }
696
697 static void
698 gtk_switch_dispose (GObject *object)
699 {
700   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
701
702   if (priv->action)
703     {
704       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
705       priv->action = NULL;
706     }
707
708   G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
709 }
710
711 static void
712 gtk_switch_class_init (GtkSwitchClass *klass)
713 {
714   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
715   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
716   gpointer activatable_iface;
717
718   g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
719
720   activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
721   switch_props[PROP_RELATED_ACTION] =
722     g_param_spec_override ("related-action",
723                            g_object_interface_find_property (activatable_iface,
724                                                              "related-action"));
725
726   switch_props[PROP_USE_ACTION_APPEARANCE] =
727     g_param_spec_override ("use-action-appearance",
728                            g_object_interface_find_property (activatable_iface,
729                                                              "use-action-appearance"));
730
731   /**
732    * GtkSwitch:active:
733    *
734    * Whether the #GtkSwitch widget is in its on or off state.
735    */
736   switch_props[PROP_ACTIVE] =
737     g_param_spec_boolean ("active",
738                           P_("Active"),
739                           P_("Whether the switch is on or off"),
740                           FALSE,
741                           GTK_PARAM_READWRITE);
742
743   gobject_class->set_property = gtk_switch_set_property;
744   gobject_class->get_property = gtk_switch_get_property;
745   gobject_class->dispose = gtk_switch_dispose;
746
747   g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
748
749   widget_class->get_preferred_width = gtk_switch_get_preferred_width;
750   widget_class->get_preferred_height = gtk_switch_get_preferred_height;
751   widget_class->size_allocate = gtk_switch_size_allocate;
752   widget_class->realize = gtk_switch_realize;
753   widget_class->unrealize = gtk_switch_unrealize;
754   widget_class->map = gtk_switch_map;
755   widget_class->unmap = gtk_switch_unmap;
756   widget_class->draw = gtk_switch_draw;
757   widget_class->button_press_event = gtk_switch_button_press;
758   widget_class->button_release_event = gtk_switch_button_release;
759   widget_class->motion_notify_event = gtk_switch_motion;
760   widget_class->enter_notify_event = gtk_switch_enter;
761   widget_class->leave_notify_event = gtk_switch_leave;
762   widget_class->key_release_event = gtk_switch_key_release;
763   widget_class->get_accessible = gtk_switch_get_accessible;
764
765   /**
766    * GtkSwitch:slider-width:
767    *
768    * The minimum width of the #GtkSwitch handle, in pixels.
769    */
770   gtk_widget_class_install_style_property (widget_class,
771                                            g_param_spec_int ("slider-width",
772                                                              P_("Slider Width"),
773                                                              P_("The minimum width of the handle"),
774                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
775                                                              DEFAULT_SLIDER_WIDTH,
776                                                              GTK_PARAM_READABLE));
777 }
778
779 static void
780 gtk_switch_init (GtkSwitch *self)
781 {
782   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
783   self->priv->use_action_appearance = TRUE;
784   gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
785   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
786 }
787
788 /**
789  * gtk_switch_new:
790  *
791  * Creates a new #GtkSwitch widget.
792  *
793  * Return value: the newly created #GtkSwitch instance
794  *
795  * Since: 3.0
796  */
797 GtkWidget *
798 gtk_switch_new (void)
799 {
800   return g_object_new (GTK_TYPE_SWITCH, NULL);
801 }
802
803 /**
804  * gtk_switch_set_active:
805  * @sw: a #GtkSwitch
806  * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
807  *
808  * Changes the state of @sw to the desired one.
809  *
810  * Since: 3.0
811  */
812 void
813 gtk_switch_set_active (GtkSwitch *sw,
814                        gboolean   is_active)
815 {
816   GtkSwitchPrivate *priv;
817
818   g_return_if_fail (GTK_IS_SWITCH (sw));
819
820   is_active = !!is_active;
821
822   priv = sw->priv;
823
824   if (priv->is_active != is_active)
825     {
826       AtkObject *accessible;
827
828       priv->is_active = is_active;
829
830       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
831
832       if (priv->action)
833         gtk_action_activate (priv->action);
834
835       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
836       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
837
838       gtk_widget_queue_draw (GTK_WIDGET (sw));
839     }
840 }
841
842 /**
843  * gtk_switch_get_active:
844  * @sw: a #GtkSwitch
845  *
846  * Gets whether the #GtkSwitch is in its "on" or "off" state.
847  *
848  * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
849  *
850  * Since: 3.0
851  */
852 gboolean
853 gtk_switch_get_active (GtkSwitch *sw)
854 {
855   g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
856
857   return sw->priv->is_active;
858 }
859
860 static void
861 gtk_switch_update (GtkActivatable *activatable,
862                    GtkAction      *action,
863                    const gchar    *property_name)
864 {
865   if (strcmp (property_name, "visible") == 0)
866     {
867       if (gtk_action_is_visible (action))
868         gtk_widget_show (GTK_WIDGET (activatable));
869       else
870         gtk_widget_hide (GTK_WIDGET (activatable));
871     }
872   else if (strcmp (property_name, "sensitive") == 0)
873     {
874       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
875     }
876   else if (strcmp (property_name, "active") == 0)
877     {
878       gtk_action_block_activate (action);
879       gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
880       gtk_action_unblock_activate (action);
881     }
882 }
883
884 static void
885 gtk_switch_sync_action_properties (GtkActivatable *activatable,
886                                    GtkAction      *action)
887 {
888   if (!action)
889     return;
890
891   if (gtk_action_is_visible (action))
892     gtk_widget_show (GTK_WIDGET (activatable));
893   else
894     gtk_widget_hide (GTK_WIDGET (activatable));
895
896   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
897
898   gtk_action_block_activate (action);
899   gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
900   gtk_action_unblock_activate (action);
901 }
902
903 static void
904 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
905 {
906   iface->update = gtk_switch_update;
907   iface->sync_action_properties = gtk_switch_sync_action_properties;
908 }
909
910 /* accessibility: object */
911
912 /* dummy typedefs */
913 typedef struct _GtkSwitchAccessible             GtkSwitchAccessible;
914 typedef struct _GtkSwitchAccessibleClass        GtkSwitchAccessibleClass;
915
916 ATK_DEFINE_TYPE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET);
917
918 static AtkStateSet *
919 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
920 {
921   AtkStateSet *state_set;
922   GtkWidget *widget;
923
924   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
925
926   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
927   if (widget == NULL)
928     return state_set;
929
930   if (gtk_switch_get_active (GTK_SWITCH (widget)))
931     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
932
933   return state_set;
934 }
935
936 static void
937 _gtk_switch_accessible_initialize (AtkObject *accessible,
938                                    gpointer   widget)
939 {
940   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
941
942   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
943   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
944   atk_object_set_description (accessible, _("Switches between on and off states"));
945 }
946
947 static void
948 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
949 {
950   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
951
952   atk_class->initialize = _gtk_switch_accessible_initialize;
953   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
954 }
955
956 static void
957 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
958 {
959 }
960
961 /* accessibility: factory */
962
963 typedef AtkObjectFactoryClass   GtkSwitchAccessibleFactoryClass;
964 typedef AtkObjectFactory        GtkSwitchAccessibleFactory;
965
966 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
967                gtk_switch_accessible_factory,
968                ATK_TYPE_OBJECT_FACTORY);
969
970 static GType
971 gtk_switch_accessible_factory_get_accessible_type (void)
972 {
973   return _gtk_switch_accessible_get_type ();
974 }
975
976 static AtkObject *
977 gtk_switch_accessible_factory_create_accessible (GObject *obj)
978 {
979   AtkObject *accessible;
980
981   accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
982   atk_object_initialize (accessible, obj);
983
984   return accessible;
985 }
986
987 static void
988 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
989 {
990   klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
991   klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
992 }
993
994 static void
995 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)
996 {
997 }