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