]> Pileus Git - ~andy/gtk/blob - gtk/gtkswitch.c
Make GtkSwitch use GtkStyleContext
[~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   gtk_widget_style_attach (widget);
427 }
428
429 static void
430 gtk_switch_unrealize (GtkWidget *widget)
431 {
432   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
433
434   if (priv->event_window != NULL)
435     {
436       gdk_window_set_user_data (priv->event_window, NULL);
437       gdk_window_destroy (priv->event_window);
438       priv->event_window = NULL;
439     }
440
441   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
442 }
443
444 static void
445 gtk_switch_map (GtkWidget *widget)
446 {
447   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
448
449   GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);
450
451   if (priv->event_window)
452     gdk_window_show (priv->event_window);
453 }
454
455 static void
456 gtk_switch_unmap (GtkWidget *widget)
457 {
458   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
459
460   if (priv->event_window)
461     gdk_window_hide (priv->event_window);
462
463   GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
464 }
465
466 static inline void
467 gtk_switch_paint_handle (GtkWidget    *widget,
468                          cairo_t      *cr,
469                          GdkRectangle *box)
470 {
471   GtkStyleContext *context = gtk_widget_get_style_context (widget);
472   GtkStateFlags state;
473
474   state = gtk_widget_get_state_flags (widget);
475
476   gtk_style_context_save (context);
477   gtk_style_context_set_state (context, state);
478   gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
479
480   gtk_render_slider (context, cr,
481                      box->x, box->y,
482                      box->width, box->height,
483                      GTK_ORIENTATION_HORIZONTAL);
484
485   gtk_style_context_restore (context);
486 }
487
488 static gboolean
489 gtk_switch_draw (GtkWidget *widget,
490                  cairo_t   *cr)
491 {
492   GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
493   GtkStyleContext *context;
494   GdkRectangle handle;
495   PangoLayout *layout;
496   PangoRectangle rect;
497   gint label_x, label_y;
498   GtkStateFlags state;
499   GtkBorder padding;
500   gint focus_width, focus_pad;
501   gint x, y, width, height;
502
503   gtk_widget_style_get (widget,
504                         "focus-line-width", &focus_width,
505                         "focus-padding", &focus_pad,
506                         NULL);
507
508   context = gtk_widget_get_style_context (widget);
509   state = gtk_widget_get_state_flags (widget);
510
511   if (priv->is_active)
512     state |= GTK_STATE_FLAG_ACTIVE;
513
514   gtk_style_context_get_padding (context, state, &padding);
515
516   x = 0;
517   y = 0;
518   width = gtk_widget_get_allocated_width (widget);
519   height = gtk_widget_get_allocated_height (widget);
520
521   if (gtk_widget_has_focus (widget))
522     gtk_render_focus (context, cr, x, y, width, height);
523
524   gtk_style_context_save (context);
525   gtk_style_context_set_state (context, state);
526
527   x += focus_width + focus_pad;
528   y += focus_width + focus_pad;
529   width -= 2 * (focus_width + focus_pad);
530   height -= 2 * (focus_width + focus_pad);
531
532   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
533
534   gtk_render_background (context, cr, x, y, width, height);
535   gtk_render_frame (context, cr, x, y, width, height);
536
537   /* XXX the +1/-1 it's pixel wriggling after checking with the default
538    * theme and xmag
539    */
540   handle.y = y + padding.top + 1;
541   handle.width = (width - padding.left - padding.right) / 2;
542   handle.height = (height - padding.top - padding.bottom) - 1;
543
544   /* Translators: if the "on" state label requires more than three
545    * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
546    * the state
547    */
548   layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
549   pango_layout_get_extents (layout, NULL, &rect);
550   pango_extents_to_pixels (&rect, NULL);
551
552   label_x = x + padding.left
553           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
554   label_y = y + padding.top
555           + (height - rect.height - padding.top - padding.bottom) / 2;
556
557   gtk_render_layout (context, cr, label_x, label_y, layout);
558
559   g_object_unref (layout);
560
561   /* Translators: if the "off" state label requires more than three
562    * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
563    */
564   layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
565   pango_layout_get_extents (layout, NULL, &rect);
566   pango_extents_to_pixels (&rect, NULL);
567
568   label_x = x + padding.left
569           + (width / 2)
570           + ((width / 2) - rect.width - padding.left - padding.right) / 2;
571   label_y = y + padding.top
572           + (height - rect.height - padding.top - padding.bottom) / 2;
573
574   gtk_render_layout (context, cr, label_x, label_y, layout);
575
576   g_object_unref (layout);
577
578   if (priv->is_dragging)
579     handle.x = x + priv->handle_x;
580   else if (priv->is_active)
581     handle.x = x + width - handle.width - padding.right;
582   else
583     handle.x = x + padding.left;
584
585   gtk_style_context_restore (context);
586
587   gtk_switch_paint_handle (widget, cr, &handle);
588
589   return FALSE;
590 }
591
592 static AtkObject *
593 gtk_switch_get_accessible (GtkWidget *widget)
594 {
595   static gboolean first_time = TRUE;
596
597   if (G_UNLIKELY (first_time))
598     {
599       AtkObjectFactory *factory;
600       AtkRegistry *registry;
601       GType derived_type;
602       GType derived_atk_type;
603
604       /* Figure out whether accessibility is enabled by looking at the
605        * type of the accessible object which would be created for the
606        * parent type of GtkSwitch
607        */
608       derived_type = g_type_parent (GTK_TYPE_SWITCH);
609
610       registry = atk_get_default_registry ();
611       factory = atk_registry_get_factory (registry, derived_type);
612       derived_atk_type = atk_object_factory_get_accessible_type (factory);
613       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
614         atk_registry_set_factory_type (registry,
615                                        GTK_TYPE_SWITCH,
616                                        gtk_switch_accessible_factory_get_type ());
617
618       first_time = FALSE;
619     }
620
621   return GTK_WIDGET_CLASS (gtk_switch_parent_class)->get_accessible (widget);
622 }
623
624 static void
625 gtk_switch_set_related_action (GtkSwitch *sw,
626                                GtkAction *action)
627 {
628   GtkSwitchPrivate *priv = sw->priv;
629
630   if (priv->action == action)
631     return;
632
633   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
634
635   priv->action = action;
636 }
637
638 static void
639 gtk_switch_set_use_action_appearance (GtkSwitch *sw,
640                                       gboolean   use_appearance)
641 {
642   GtkSwitchPrivate *priv = sw->priv;
643
644   if (priv->use_action_appearance != use_appearance)
645     {
646       priv->use_action_appearance = use_appearance;
647
648       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
649     }
650 }
651
652 static void
653 gtk_switch_set_property (GObject      *gobject,
654                          guint         prop_id,
655                          const GValue *value,
656                          GParamSpec   *pspec)
657 {
658   GtkSwitch *sw = GTK_SWITCH (gobject);
659
660   switch (prop_id)
661     {
662     case PROP_ACTIVE:
663       gtk_switch_set_active (sw, g_value_get_boolean (value));
664       break;
665
666     case PROP_RELATED_ACTION:
667       gtk_switch_set_related_action (sw, g_value_get_object (value));
668       break;
669
670     case PROP_USE_ACTION_APPEARANCE:
671       gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
672       break;
673
674     default:
675       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
676     }
677 }
678
679 static void
680 gtk_switch_get_property (GObject    *gobject,
681                          guint       prop_id,
682                          GValue     *value,
683                          GParamSpec *pspec)
684 {
685   GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;
686
687   switch (prop_id)
688     {
689     case PROP_ACTIVE:
690       g_value_set_boolean (value, priv->is_active);
691       break;
692
693     case PROP_RELATED_ACTION:
694       g_value_set_object (value, priv->action);
695       break;
696
697     case PROP_USE_ACTION_APPEARANCE:
698       g_value_set_boolean (value, priv->use_action_appearance);
699       break;
700
701     default:
702       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
703     }
704 }
705
706 static void
707 gtk_switch_dispose (GObject *object)
708 {
709   GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;
710
711   if (priv->action)
712     {
713       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
714       priv->action = NULL;
715     }
716
717   G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
718 }
719
720 static void
721 gtk_switch_class_init (GtkSwitchClass *klass)
722 {
723   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
724   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
725   gpointer activatable_iface;
726
727   g_type_class_add_private (klass, sizeof (GtkSwitchPrivate));
728
729   activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
730   switch_props[PROP_RELATED_ACTION] =
731     g_param_spec_override ("related-action",
732                            g_object_interface_find_property (activatable_iface,
733                                                              "related-action"));
734
735   switch_props[PROP_USE_ACTION_APPEARANCE] =
736     g_param_spec_override ("use-action-appearance",
737                            g_object_interface_find_property (activatable_iface,
738                                                              "use-action-appearance"));
739
740   /**
741    * GtkSwitch:active:
742    *
743    * Whether the #GtkSwitch widget is in its on or off state.
744    */
745   switch_props[PROP_ACTIVE] =
746     g_param_spec_boolean ("active",
747                           P_("Active"),
748                           P_("Whether the switch is on or off"),
749                           FALSE,
750                           GTK_PARAM_READWRITE);
751
752   gobject_class->set_property = gtk_switch_set_property;
753   gobject_class->get_property = gtk_switch_get_property;
754   gobject_class->dispose = gtk_switch_dispose;
755
756   g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
757
758   widget_class->get_preferred_width = gtk_switch_get_preferred_width;
759   widget_class->get_preferred_height = gtk_switch_get_preferred_height;
760   widget_class->size_allocate = gtk_switch_size_allocate;
761   widget_class->realize = gtk_switch_realize;
762   widget_class->unrealize = gtk_switch_unrealize;
763   widget_class->map = gtk_switch_map;
764   widget_class->unmap = gtk_switch_unmap;
765   widget_class->draw = gtk_switch_draw;
766   widget_class->button_press_event = gtk_switch_button_press;
767   widget_class->button_release_event = gtk_switch_button_release;
768   widget_class->motion_notify_event = gtk_switch_motion;
769   widget_class->enter_notify_event = gtk_switch_enter;
770   widget_class->leave_notify_event = gtk_switch_leave;
771   widget_class->key_release_event = gtk_switch_key_release;
772   widget_class->get_accessible = gtk_switch_get_accessible;
773
774   /**
775    * GtkSwitch:slider-width:
776    *
777    * The minimum width of the #GtkSwitch handle, in pixels.
778    */
779   gtk_widget_class_install_style_property (widget_class,
780                                            g_param_spec_int ("slider-width",
781                                                              P_("Slider Width"),
782                                                              P_("The minimum width of the handle"),
783                                                              DEFAULT_SLIDER_WIDTH, G_MAXINT,
784                                                              DEFAULT_SLIDER_WIDTH,
785                                                              GTK_PARAM_READABLE));
786 }
787
788 static void
789 gtk_switch_init (GtkSwitch *self)
790 {
791   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_SWITCH, GtkSwitchPrivate);
792   self->priv->use_action_appearance = TRUE;
793   gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
794   gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
795 }
796
797 /**
798  * gtk_switch_new:
799  *
800  * Creates a new #GtkSwitch widget.
801  *
802  * Return value: the newly created #GtkSwitch instance
803  *
804  * Since: 3.0
805  */
806 GtkWidget *
807 gtk_switch_new (void)
808 {
809   return g_object_new (GTK_TYPE_SWITCH, NULL);
810 }
811
812 /**
813  * gtk_switch_set_active:
814  * @sw: a #GtkSwitch
815  * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
816  *
817  * Changes the state of @sw to the desired one.
818  *
819  * Since: 3.0
820  */
821 void
822 gtk_switch_set_active (GtkSwitch *sw,
823                        gboolean   is_active)
824 {
825   GtkSwitchPrivate *priv;
826
827   g_return_if_fail (GTK_IS_SWITCH (sw));
828
829   is_active = !!is_active;
830
831   priv = sw->priv;
832
833   if (priv->is_active != is_active)
834     {
835       AtkObject *accessible;
836       GtkWidget *widget;
837       GtkStyleContext *context;
838
839       widget = GTK_WIDGET (sw);
840       priv->is_active = is_active;
841
842       g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);
843
844       if (priv->action)
845         gtk_action_activate (priv->action);
846
847       accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
848       atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);
849
850       if (gtk_widget_get_realized (widget))
851         {
852           context = gtk_widget_get_style_context (widget);
853           gtk_style_context_notify_state_change (context,
854                                                  gtk_widget_get_window (widget),
855                                                  NULL, GTK_STATE_ACTIVE, is_active);
856         }
857
858       gtk_widget_queue_draw (GTK_WIDGET (sw));
859     }
860 }
861
862 /**
863  * gtk_switch_get_active:
864  * @sw: a #GtkSwitch
865  *
866  * Gets whether the #GtkSwitch is in its "on" or "off" state.
867  *
868  * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
869  *
870  * Since: 3.0
871  */
872 gboolean
873 gtk_switch_get_active (GtkSwitch *sw)
874 {
875   g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);
876
877   return sw->priv->is_active;
878 }
879
880 static void
881 gtk_switch_update (GtkActivatable *activatable,
882                    GtkAction      *action,
883                    const gchar    *property_name)
884 {
885   if (strcmp (property_name, "visible") == 0)
886     {
887       if (gtk_action_is_visible (action))
888         gtk_widget_show (GTK_WIDGET (activatable));
889       else
890         gtk_widget_hide (GTK_WIDGET (activatable));
891     }
892   else if (strcmp (property_name, "sensitive") == 0)
893     {
894       gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
895     }
896   else if (strcmp (property_name, "active") == 0)
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
904 static void
905 gtk_switch_sync_action_properties (GtkActivatable *activatable,
906                                    GtkAction      *action)
907 {
908   if (!action)
909     return;
910
911   if (gtk_action_is_visible (action))
912     gtk_widget_show (GTK_WIDGET (activatable));
913   else
914     gtk_widget_hide (GTK_WIDGET (activatable));
915
916   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
917
918   gtk_action_block_activate (action);
919   gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
920   gtk_action_unblock_activate (action);
921 }
922
923 static void
924 gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
925 {
926   iface->update = gtk_switch_update;
927   iface->sync_action_properties = gtk_switch_sync_action_properties;
928 }
929
930 /* accessibility: object */
931
932 /* dummy typedefs */
933 typedef struct _GtkSwitchAccessible             GtkSwitchAccessible;
934 typedef struct _GtkSwitchAccessibleClass        GtkSwitchAccessibleClass;
935
936 ATK_DEFINE_TYPE (GtkSwitchAccessible, _gtk_switch_accessible, GTK_TYPE_WIDGET);
937
938 static AtkStateSet *
939 gtk_switch_accessible_ref_state_set (AtkObject *accessible)
940 {
941   AtkStateSet *state_set;
942   GtkWidget *widget;
943
944   state_set = ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->ref_state_set (accessible);
945
946   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
947   if (widget == NULL)
948     return state_set;
949
950   if (gtk_switch_get_active (GTK_SWITCH (widget)))
951     atk_state_set_add_state (state_set, ATK_STATE_CHECKED);
952
953   return state_set;
954 }
955
956 static void
957 _gtk_switch_accessible_initialize (AtkObject *accessible,
958                                    gpointer   widget)
959 {
960   ATK_OBJECT_CLASS (_gtk_switch_accessible_parent_class)->initialize (accessible, widget);
961
962   atk_object_set_role (accessible, ATK_ROLE_TOGGLE_BUTTON);
963   atk_object_set_name (accessible, C_("light switch widget", "Switch"));
964   atk_object_set_description (accessible, _("Switches between on and off states"));
965 }
966
967 static void
968 _gtk_switch_accessible_class_init (GtkSwitchAccessibleClass *klass)
969 {
970   AtkObjectClass *atk_class = ATK_OBJECT_CLASS (klass);
971
972   atk_class->initialize = _gtk_switch_accessible_initialize;
973   atk_class->ref_state_set = gtk_switch_accessible_ref_state_set;
974 }
975
976 static void
977 _gtk_switch_accessible_init (GtkSwitchAccessible *self)
978 {
979 }
980
981 /* accessibility: factory */
982
983 typedef AtkObjectFactoryClass   GtkSwitchAccessibleFactoryClass;
984 typedef AtkObjectFactory        GtkSwitchAccessibleFactory;
985
986 G_DEFINE_TYPE (GtkSwitchAccessibleFactory,
987                gtk_switch_accessible_factory,
988                ATK_TYPE_OBJECT_FACTORY);
989
990 static GType
991 gtk_switch_accessible_factory_get_accessible_type (void)
992 {
993   return _gtk_switch_accessible_get_type ();
994 }
995
996 static AtkObject *
997 gtk_switch_accessible_factory_create_accessible (GObject *obj)
998 {
999   AtkObject *accessible;
1000
1001   accessible = g_object_new (_gtk_switch_accessible_get_type (), NULL);
1002   atk_object_initialize (accessible, obj);
1003
1004   return accessible;
1005 }
1006
1007 static void
1008 gtk_switch_accessible_factory_class_init (AtkObjectFactoryClass *klass)
1009 {
1010   klass->create_accessible = gtk_switch_accessible_factory_create_accessible;
1011   klass->get_accessible_type = gtk_switch_accessible_factory_get_accessible_type;
1012 }
1013
1014 static void
1015 gtk_switch_accessible_factory_init (AtkObjectFactory *factory)
1016 {
1017 }