gtkswitch.c 29.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* GTK - The GIMP Toolkit
 *
 * Copyright (C) 2010  Intel Corporation
 * Copyright (C) 2010  RedHat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22
 *
 * Author:
 *      Emmanuele Bassi <ebassi@linux.intel.com>
 *      Matthias Clasen <mclasen@redhat.com>
 *
23
 * Based on similar code from Mx.
24 25 26 27
 */

/**
 * SECTION:gtkswitch
28
 * @Short_Description: A “light switch” style toggle
29 30 31 32 33 34 35 36 37 38 39 40
 * @Title: GtkSwitch
 * @See_Also: #GtkToggleButton
 *
 * #GtkSwitch is a widget that has two states: on or off. The user can control
 * which state should be active by clicking the empty area, or by dragging the
 * handle.
 */

#include "config.h"

#include "gtkswitch.h"

41
#include "deprecated/gtkactivatable.h"
42 43
#include "gtkintl.h"
#include "gtkprivate.h"
44
#include "deprecated/gtktoggleaction.h"
45
#include "gtkwidget.h"
46
#include "gtkmarshalers.h"
47 48
#include "gtkapplicationprivate.h"
#include "gtkactionable.h"
49
#include "a11y/gtkswitchaccessible.h"
50
#include "gtkactionhelper.h"
51

52
#include <math.h>
53 54 55 56 57 58 59

#define DEFAULT_SLIDER_WIDTH    (36)

struct _GtkSwitchPrivate
{
  GdkWindow *event_window;
  GtkAction *action;
60
  GtkActionHelper *action_helper;
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  gint handle_x;
  gint offset;
  gint drag_start;
  gint drag_threshold;

  guint is_active             : 1;
  guint is_dragging           : 1;
  guint in_press              : 1;
  guint in_switch             : 1;
  guint use_action_appearance : 1;
};

enum
{
  PROP_0,
  PROP_ACTIVE,
  PROP_RELATED_ACTION,
  PROP_USE_ACTION_APPEARANCE,
80 81 82
  LAST_PROP,
  PROP_ACTION_NAME,
  PROP_ACTION_TARGET
83 84
};

85 86 87 88 89 90 91 92
enum
{
  ACTIVATE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

93 94
static GParamSpec *switch_props[LAST_PROP] = { NULL, };

95
static void gtk_switch_actionable_iface_init (GtkActionableInterface *iface);
96 97
static void gtk_switch_activatable_interface_init (GtkActivatableIface *iface);

98
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
99
G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
100
                         G_ADD_PRIVATE (GtkSwitch)
101 102
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE,
                                                gtk_switch_actionable_iface_init)
103 104
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
                                                gtk_switch_activatable_interface_init));
105
G_GNUC_END_IGNORE_DEPRECATIONS;
106 107 108 109 110 111 112 113

static gboolean
gtk_switch_button_press (GtkWidget      *widget,
                         GdkEventButton *event)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
  GtkAllocation allocation;

114 115 116 117
  /* Don't handle extra mouse buttons events, let them bubble up */
  if (event->button > 5)
    return GDK_EVENT_PROPAGATE;

118 119 120 121 122 123 124 125 126 127
  gtk_widget_get_allocation (widget, &allocation);

  if (priv->is_active)
    {
      /* if the event occurred in the "off" area, then this
       * is a direct toggle
       */
      if (event->x <= allocation.width / 2)
        {
          priv->in_press = TRUE;
128
          return GDK_EVENT_STOP;
129 130 131 132 133 134 135 136 137 138 139 140
        }

      priv->offset = event->x - allocation.width / 2;
    }
  else
    {
      /* if the event occurred in the "on" area, then this
       * is a direct toggle
       */
      if (event->x >= allocation.width / 2)
        {
          priv->in_press = TRUE;
141
          return GDK_EVENT_STOP;
142 143 144 145 146 147 148 149 150 151 152
        }

      priv->offset = event->x;
    }

  priv->drag_start = event->x;

  g_object_get (gtk_widget_get_settings (widget),
                "gtk-dnd-drag-threshold", &priv->drag_threshold,
                NULL);

153
  return GDK_EVENT_STOP;
154 155 156 157 158 159 160 161 162 163
}

static gboolean
gtk_switch_motion (GtkWidget      *widget,
                   GdkEventMotion *event)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  /* if this is a direct toggle we don't handle motion */
  if (priv->in_press)
164
    return GDK_EVENT_PROPAGATE;
165 166

  if (ABS (event->x - priv->drag_start) < priv->drag_threshold)
167
    return GDK_EVENT_STOP;
168 169 170 171 172

  if (event->state & GDK_BUTTON1_MASK)
    {
      gint position = event->x - priv->offset;
      GtkAllocation allocation;
173 174 175
      GtkStyleContext *context;
      GtkStateFlags state;
      GtkBorder padding;
176 177 178 179 180 181
      gint width, focus_width, focus_pad;

      gtk_widget_style_get (widget,
                            "focus-line-width", &focus_width,
                            "focus-padding", &focus_pad,
                            NULL);
182

183 184
      context = gtk_widget_get_style_context (widget);
      state = gtk_widget_get_state_flags (widget);
185 186 187

      gtk_style_context_save (context);
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
188
      gtk_style_context_get_padding (context, state, &padding);
189 190
      gtk_style_context_restore (context);
      
191 192
      gtk_widget_get_allocation (widget, &allocation);

193 194
      width = allocation.width - 2 * (focus_width + focus_pad);

195
      /* constrain the handle within the trough width */
196 197
      if (position > (width / 2) - padding.right)
        priv->handle_x = width / 2 - padding.right;
198
      else if (position < padding.left)
199
        priv->handle_x = 0;
200 201 202 203 204 205 206 207
      else
        priv->handle_x = position;

      priv->is_dragging = TRUE;

      /* we need to redraw the handle */
      gtk_widget_queue_draw (widget);

208
      return GDK_EVENT_STOP;
209 210
    }

211
  return GDK_EVENT_PROPAGATE;
212 213 214 215 216 217 218 219 220
}

static gboolean
gtk_switch_button_release (GtkWidget      *widget,
                           GdkEventButton *event)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
  GtkAllocation allocation;

221 222 223 224
  /* Don't handle extra mouse buttons events, let them bubble up */
  if (event->button > 5)
    return GDK_EVENT_PROPAGATE;

225 226 227 228 229 230 231
  gtk_widget_get_allocation (widget, &allocation);

  /* dragged toggles have a "soft" grab, so we don't care whether we
   * are in the switch or not when the button is released; we do care
   * for direct toggles, instead
   */
  if (!priv->is_dragging && !priv->in_switch)
232
    return GDK_EVENT_PROPAGATE;
233 234 235 236 237 238 239

  /* direct toggle */
  if (priv->in_press)
    {
      priv->in_press = FALSE;
      gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);

240
      return GDK_EVENT_STOP;
241 242
    }

243 244 245 246 247 248
  /* toggle the switch if the handle was clicked but a drag had not been
   * initiated */
  if (!priv->is_dragging && !priv->in_press)
    {
      gtk_switch_set_active (GTK_SWITCH (widget), !priv->is_active);

249
      return GDK_EVENT_STOP;
250 251
    }

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  /* dragged toggle */
  if (priv->is_dragging)
    {
      priv->is_dragging = FALSE;

      /* if half the handle passed the middle of the switch, then we
       * consider it to be on
       */
      if ((priv->handle_x + (allocation.width / 4)) >= (allocation.width / 2))
        {
          gtk_switch_set_active (GTK_SWITCH (widget), TRUE);
          priv->handle_x = allocation.width / 2;
        }
      else
        {
          gtk_switch_set_active (GTK_SWITCH (widget), FALSE);
          priv->handle_x = 0;
        }

      gtk_widget_queue_draw (widget);

273
      return GDK_EVENT_STOP;
274 275
    }

276
  return GDK_EVENT_PROPAGATE;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}

static gboolean
gtk_switch_enter (GtkWidget        *widget,
                  GdkEventCrossing *event)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  if (event->window == priv->event_window)
    priv->in_switch = TRUE;

  return FALSE;
}

static gboolean
gtk_switch_leave (GtkWidget        *widget,
                  GdkEventCrossing *event)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  if (event->window == priv->event_window)
    priv->in_switch = FALSE;

  return FALSE;
}

303 304
static void
gtk_switch_activate (GtkSwitch *sw)
305
{
306
  GtkSwitchPrivate *priv = sw->priv;
307

308
  gtk_switch_set_active (sw, !priv->is_active);
309 310 311 312 313 314 315
}

static void
gtk_switch_get_preferred_width (GtkWidget *widget,
                                gint      *minimum,
                                gint      *natural)
{
316 317
  GtkStyleContext *context;
  GtkStateFlags state;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
318
  GtkBorder padding;
319 320 321 322
  gint width, slider_width, focus_width, focus_pad;
  PangoLayout *layout;
  PangoRectangle logical_rect;

323
  context = gtk_widget_get_style_context (widget);
324
  state = gtk_style_context_get_state (context);
325 326 327 328

  gtk_style_context_save (context);

  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
329 330 331
  gtk_style_context_get_padding (context, state, &padding);

  width = padding.left + padding.right;
332

333 334
  gtk_style_context_restore (context);

335 336 337 338 339 340
  gtk_widget_style_get (widget,
                        "slider-width", &slider_width,
                        "focus-line-width", &focus_width,
                        "focus-padding", &focus_pad,
                        NULL);

341
  slider_width = MAX (slider_width, 3 * (focus_width + focus_pad));
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

  /* Translators: if the "on" state label requires more than three
   * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
   * the state
   */
  layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
  pango_layout_get_extents (layout, NULL, &logical_rect);
  pango_extents_to_pixels (&logical_rect, NULL);
  width += MAX (logical_rect.width, slider_width);

  /* Translators: if the "off" state label requires more than three
   * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
   */
  pango_layout_set_text (layout, C_("switch", "OFF"), -1);
  pango_layout_get_extents (layout, NULL, &logical_rect);
  pango_extents_to_pixels (&logical_rect, NULL);
  width += MAX (logical_rect.width, slider_width);

  g_object_unref (layout);

  if (minimum)
    *minimum = width;

  if (natural)
    *natural = width;
}

static void
gtk_switch_get_preferred_height (GtkWidget *widget,
                                 gint      *minimum,
                                 gint      *natural)
{
374 375 376
  GtkStyleContext *context;
  GtkStateFlags state;
  GtkBorder padding;
377
  gint height, focus_width, focus_pad, slider_width, min_height;
378 379 380 381
  PangoLayout *layout;
  PangoRectangle logical_rect;
  gchar *str;

382
  context = gtk_widget_get_style_context (widget);
383
  state = gtk_style_context_get_state (context);
384 385 386 387

  gtk_style_context_save (context);

  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);
388 389 390
  gtk_style_context_get_padding (context, state, &padding);

  height = padding.top + padding.bottom;
391

392 393
  gtk_style_context_restore (context);

394
  gtk_widget_style_get (widget,
395
                        "slider-width", &slider_width,
396 397 398 399
                        "focus-line-width", &focus_width,
                        "focus-padding", &focus_pad,
                        NULL);

400
  min_height = MAX (slider_width * 0.6, 3 * (focus_width + focus_pad));
401 402 403 404 405 406 407 408

  str = g_strdup_printf ("%s%s",
                         C_("switch", "ON"),
                         C_("switch", "OFF"));

  layout = gtk_widget_create_pango_layout (widget, str);
  pango_layout_get_extents (layout, NULL, &logical_rect);
  pango_extents_to_pixels (&logical_rect, NULL);
409
  height += MAX (min_height, logical_rect.height);
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

  g_object_unref (layout);
  g_free (str);

  if (minimum)
    *minimum = height;

  if (natural)
    *natural = height;
}

static void
gtk_switch_size_allocate (GtkWidget     *widget,
                          GtkAllocation *allocation)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  gtk_widget_set_allocation (widget, allocation);

  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (priv->event_window,
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);
}

static void
gtk_switch_realize (GtkWidget *widget)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
  GdkWindow *parent_window;
  GdkWindowAttr attributes;
  gint attributes_mask;
  GtkAllocation allocation;

  gtk_widget_set_realized (widget, TRUE);
  parent_window = gtk_widget_get_parent_window (widget);
  gtk_widget_set_window (widget, parent_window);
  g_object_ref (parent_window);

  gtk_widget_get_allocation (widget, &allocation);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
                            GDK_BUTTON_RELEASE_MASK |
                            GDK_BUTTON1_MOTION_MASK |
                            GDK_POINTER_MOTION_HINT_MASK |
                            GDK_POINTER_MOTION_MASK |
                            GDK_ENTER_NOTIFY_MASK |
                            GDK_LEAVE_NOTIFY_MASK);
  attributes_mask = GDK_WA_X | GDK_WA_Y;

  priv->event_window = gdk_window_new (parent_window,
                                       &attributes,
                                       attributes_mask);
472
  gtk_widget_register_window (widget, priv->event_window);
473 474 475 476 477 478 479 480 481
}

static void
gtk_switch_unrealize (GtkWidget *widget)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  if (priv->event_window != NULL)
    {
482
      gtk_widget_unregister_window (widget, priv->event_window);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
      gdk_window_destroy (priv->event_window);
      priv->event_window = NULL;
    }

  GTK_WIDGET_CLASS (gtk_switch_parent_class)->unrealize (widget);
}

static void
gtk_switch_map (GtkWidget *widget)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  GTK_WIDGET_CLASS (gtk_switch_parent_class)->map (widget);

  if (priv->event_window)
    gdk_window_show (priv->event_window);
}

static void
gtk_switch_unmap (GtkWidget *widget)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;

  if (priv->event_window)
    gdk_window_hide (priv->event_window);

  GTK_WIDGET_CLASS (gtk_switch_parent_class)->unmap (widget);
}

static inline void
gtk_switch_paint_handle (GtkWidget    *widget,
                         cairo_t      *cr,
                         GdkRectangle *box)
{
517
  GtkStyleContext *context = gtk_widget_get_style_context (widget);
518

519 520 521 522 523 524 525 526 527
  gtk_style_context_save (context);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);

  gtk_render_slider (context, cr,
                     box->x, box->y,
                     box->width, box->height,
                     GTK_ORIENTATION_HORIZONTAL);

  gtk_style_context_restore (context);
528 529 530 531 532 533 534
}

static gboolean
gtk_switch_draw (GtkWidget *widget,
                 cairo_t   *cr)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (widget)->priv;
535
  GtkStyleContext *context;
536 537 538 539
  GdkRectangle handle;
  PangoLayout *layout;
  PangoRectangle rect;
  gint label_x, label_y;
540
  GtkBorder padding;
541
  GtkStateFlags state;
542 543
  gint x, y, width, height;

544 545 546
  context = gtk_widget_get_style_context (widget);
  state = gtk_widget_get_state_flags (widget);

547 548 549 550
  gtk_style_context_save (context);

  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SLIDER);

551
  gtk_style_context_get_padding (context, state, &padding);
552

553 554
  gtk_style_context_restore (context);

555 556 557 558 559
  x = 0;
  y = 0;
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);

560
  gtk_style_context_save (context);
561
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
562

563 564
  gtk_render_background (context, cr, x, y, width, height);
  gtk_render_frame (context, cr, x, y, width, height);
565

566 567 568 569 570 571 572 573 574
  width -= padding.left + padding.right;
  height -= padding.top + padding.bottom;

  x += padding.left;
  y += padding.top;

  handle.y = y;
  handle.width = width / 2;
  handle.height = height;
575 576 577 578 579 580

  /* Translators: if the "on" state label requires more than three
   * glyphs then use MEDIUM VERTICAL BAR (U+2759) as the text for
   * the state
   */
  layout = gtk_widget_create_pango_layout (widget, C_("switch", "ON"));
581

582 583 584
  pango_layout_get_extents (layout, NULL, &rect);
  pango_extents_to_pixels (&rect, NULL);

585 586
  label_x = x +  ((width / 2) - rect.width) / 2;
  label_y = y + (height - rect.height) / 2;
587

588
  gtk_render_layout (context, cr, label_x, label_y, layout);
589 590 591 592 593 594 595

  g_object_unref (layout);

  /* Translators: if the "off" state label requires more than three
   * glyphs then use WHITE CIRCLE (U+25CB) as the text for the state
   */
  layout = gtk_widget_create_pango_layout (widget, C_("switch", "OFF"));
596

597 598 599
  pango_layout_get_extents (layout, NULL, &rect);
  pango_extents_to_pixels (&rect, NULL);

600 601
  label_x = x +  (width / 2) + ((width / 2) - rect.width) / 2;
  label_y = y + (height - rect.height) / 2;
602

603
  gtk_render_layout (context, cr, label_x, label_y, layout);
604 605 606 607 608 609

  g_object_unref (layout);

  if (priv->is_dragging)
    handle.x = x + priv->handle_x;
  else if (priv->is_active)
610
    handle.x = x + width - handle.width;
611
  else
612
    handle.x = x;
613 614

  gtk_style_context_restore (context);
615 616 617

  gtk_switch_paint_handle (widget, cr, &handle);

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  if (gtk_widget_has_visible_focus (widget))
    {
      gint focus_width, focus_pad, pad;

      gtk_widget_style_get (widget,
                            "focus-line-width", &focus_width,
                            "focus-padding", &focus_pad,
                            NULL);

      pad = focus_pad + focus_width;

      gtk_render_focus (context, cr,
                        handle.x + pad, handle.y + pad,
                        handle.width - pad*2, handle.height - pad*2);
    }

634 635 636 637 638 639 640 641 642 643 644 645
  return FALSE;
}

static void
gtk_switch_set_related_action (GtkSwitch *sw,
                               GtkAction *action)
{
  GtkSwitchPrivate *priv = sw->priv;

  if (priv->action == action)
    return;

646
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
647
  gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (sw), action);
648
  G_GNUC_END_IGNORE_DEPRECATIONS;
649 650 651 652 653 654 655 656 657 658 659 660 661 662

  priv->action = action;
}

static void
gtk_switch_set_use_action_appearance (GtkSwitch *sw,
                                      gboolean   use_appearance)
{
  GtkSwitchPrivate *priv = sw->priv;

  if (priv->use_action_appearance != use_appearance)
    {
      priv->use_action_appearance = use_appearance;

663
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
664
      gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (sw), priv->action);
665
      G_GNUC_END_IGNORE_DEPRECATIONS;
666 667 668
    }
}

669 670 671 672 673 674
static void
gtk_switch_set_action_name (GtkActionable *actionable,
                            const gchar   *action_name)
{
  GtkSwitch *sw = GTK_SWITCH (actionable);

675 676
  if (!sw->priv->action_helper)
    sw->priv->action_helper = gtk_action_helper_new (actionable);
677

678
  gtk_action_helper_set_action_name (sw->priv->action_helper, action_name);
679 680 681 682 683 684 685 686
}

static void
gtk_switch_set_action_target_value (GtkActionable *actionable,
                                    GVariant      *action_target)
{
  GtkSwitch *sw = GTK_SWITCH (actionable);

687 688
  if (!sw->priv->action_helper)
    sw->priv->action_helper = gtk_action_helper_new (actionable);
689

690
  gtk_action_helper_set_action_target_value (sw->priv->action_helper, action_target);
691 692 693 694 695 696 697
}

static const gchar *
gtk_switch_get_action_name (GtkActionable *actionable)
{
  GtkSwitch *sw = GTK_SWITCH (actionable);

698
  return gtk_action_helper_get_action_name (sw->priv->action_helper);
699 700 701 702 703 704 705
}

static GVariant *
gtk_switch_get_action_target_value (GtkActionable *actionable)
{
  GtkSwitch *sw = GTK_SWITCH (actionable);

706
  return gtk_action_helper_get_action_target_value (sw->priv->action_helper);
707 708 709 710 711 712 713 714 715 716 717
}

static void
gtk_switch_actionable_iface_init (GtkActionableInterface *iface)
{
  iface->get_action_name = gtk_switch_get_action_name;
  iface->set_action_name = gtk_switch_set_action_name;
  iface->get_action_target_value = gtk_switch_get_action_target_value;
  iface->set_action_target_value = gtk_switch_set_action_target_value;
}

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
static void
gtk_switch_set_property (GObject      *gobject,
                         guint         prop_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  GtkSwitch *sw = GTK_SWITCH (gobject);

  switch (prop_id)
    {
    case PROP_ACTIVE:
      gtk_switch_set_active (sw, g_value_get_boolean (value));
      break;

    case PROP_RELATED_ACTION:
      gtk_switch_set_related_action (sw, g_value_get_object (value));
      break;

    case PROP_USE_ACTION_APPEARANCE:
      gtk_switch_set_use_action_appearance (sw, g_value_get_boolean (value));
      break;

740 741 742 743 744 745 746 747
    case PROP_ACTION_NAME:
      gtk_switch_set_action_name (GTK_ACTIONABLE (sw), g_value_get_string (value));
      break;

    case PROP_ACTION_TARGET:
      gtk_switch_set_action_target_value (GTK_ACTIONABLE (sw), g_value_get_variant (value));
      break;

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
gtk_switch_get_property (GObject    *gobject,
                         guint       prop_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (gobject)->priv;

  switch (prop_id)
    {
    case PROP_ACTIVE:
      g_value_set_boolean (value, priv->is_active);
      break;

    case PROP_RELATED_ACTION:
      g_value_set_object (value, priv->action);
      break;

    case PROP_USE_ACTION_APPEARANCE:
      g_value_set_boolean (value, priv->use_action_appearance);
      break;

775
    case PROP_ACTION_NAME:
776
      g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
777 778 779
      break;

    case PROP_ACTION_TARGET:
780
      g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
781 782
      break;

783 784 785 786 787 788 789 790 791 792
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
    }
}

static void
gtk_switch_dispose (GObject *object)
{
  GtkSwitchPrivate *priv = GTK_SWITCH (object)->priv;

793
  g_clear_object (&priv->action_helper);
794

795 796
  if (priv->action)
    {
797
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
798
      gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (object), NULL);
799
      G_GNUC_END_IGNORE_DEPRECATIONS;
800 801 802 803 804 805 806 807 808 809 810 811 812
      priv->action = NULL;
    }

  G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
}

static void
gtk_switch_class_init (GtkSwitchClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  gpointer activatable_iface;

813
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
814
  activatable_iface = g_type_default_interface_peek (GTK_TYPE_ACTIVATABLE);
815 816
  G_GNUC_END_IGNORE_DEPRECATIONS;

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
  switch_props[PROP_RELATED_ACTION] =
    g_param_spec_override ("related-action",
                           g_object_interface_find_property (activatable_iface,
                                                             "related-action"));

  switch_props[PROP_USE_ACTION_APPEARANCE] =
    g_param_spec_override ("use-action-appearance",
                           g_object_interface_find_property (activatable_iface,
                                                             "use-action-appearance"));

  /**
   * GtkSwitch:active:
   *
   * Whether the #GtkSwitch widget is in its on or off state.
   */
  switch_props[PROP_ACTIVE] =
    g_param_spec_boolean ("active",
                          P_("Active"),
                          P_("Whether the switch is on or off"),
                          FALSE,
                          GTK_PARAM_READWRITE);

  gobject_class->set_property = gtk_switch_set_property;
  gobject_class->get_property = gtk_switch_get_property;
  gobject_class->dispose = gtk_switch_dispose;

  g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);

  widget_class->get_preferred_width = gtk_switch_get_preferred_width;
  widget_class->get_preferred_height = gtk_switch_get_preferred_height;
  widget_class->size_allocate = gtk_switch_size_allocate;
  widget_class->realize = gtk_switch_realize;
  widget_class->unrealize = gtk_switch_unrealize;
  widget_class->map = gtk_switch_map;
  widget_class->unmap = gtk_switch_unmap;
  widget_class->draw = gtk_switch_draw;
  widget_class->button_press_event = gtk_switch_button_press;
  widget_class->button_release_event = gtk_switch_button_release;
  widget_class->motion_notify_event = gtk_switch_motion;
  widget_class->enter_notify_event = gtk_switch_enter;
  widget_class->leave_notify_event = gtk_switch_leave;

859 860
  klass->activate = gtk_switch_activate;

861 862 863 864 865 866 867 868 869 870 871 872
  /**
   * GtkSwitch:slider-width:
   *
   * The minimum width of the #GtkSwitch handle, in pixels.
   */
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("slider-width",
                                                             P_("Slider Width"),
                                                             P_("The minimum width of the handle"),
                                                             DEFAULT_SLIDER_WIDTH, G_MAXINT,
                                                             DEFAULT_SLIDER_WIDTH,
                                                             GTK_PARAM_READABLE));
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

  /**
   * GtkSwitch::activate:
   * @widget: the object which received the signal.
   *
   * The ::activate signal on GtkSwitch is an action signal and
   * emitting it causes the switch to animate.
   * Applications should never connect to this signal, but use the
   * notify::active signal.
   */
  signals[ACTIVATE] =
    g_signal_new (I_("activate"),
                  G_OBJECT_CLASS_TYPE (gobject_class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkSwitchClass, activate),
                  NULL, NULL,
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
  widget_class->activate_signal = signals[ACTIVATE];

893 894 895
  g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
  g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");

896
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SWITCH_ACCESSIBLE);
897
  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOGGLE_BUTTON);
898 899 900 901 902
}

static void
gtk_switch_init (GtkSwitch *self)
{
903
  self->priv = gtk_switch_get_instance_private (self);
904 905 906 907 908 909 910 911 912 913 914
  self->priv->use_action_appearance = TRUE;
  gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);
  gtk_widget_set_can_focus (GTK_WIDGET (self), TRUE);
}

/**
 * gtk_switch_new:
 *
 * Creates a new #GtkSwitch widget.
 *
 * Return value: the newly created #GtkSwitch instance
915 916
 *
 * Since: 3.0
917 918 919 920 921 922 923 924 925 926 927 928 929
 */
GtkWidget *
gtk_switch_new (void)
{
  return g_object_new (GTK_TYPE_SWITCH, NULL);
}

/**
 * gtk_switch_set_active:
 * @sw: a #GtkSwitch
 * @is_active: %TRUE if @sw should be active, and %FALSE otherwise
 *
 * Changes the state of @sw to the desired one.
930 931
 *
 * Since: 3.0
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
 */
void
gtk_switch_set_active (GtkSwitch *sw,
                       gboolean   is_active)
{
  GtkSwitchPrivate *priv;

  g_return_if_fail (GTK_IS_SWITCH (sw));

  is_active = !!is_active;

  priv = sw->priv;

  if (priv->is_active != is_active)
    {
947 948
      AtkObject *accessible;

949 950 951 952
      priv->is_active = is_active;

      g_object_notify_by_pspec (G_OBJECT (sw), switch_props[PROP_ACTIVE]);

953 954
      if (priv->action_helper)
        gtk_action_helper_activate (priv->action_helper);
955

956
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
957 958
      if (priv->action)
        gtk_action_activate (priv->action);
959
      G_GNUC_END_IGNORE_DEPRECATIONS;
960

961 962 963
      accessible = gtk_widget_get_accessible (GTK_WIDGET (sw));
      atk_object_notify_state_change (accessible, ATK_STATE_CHECKED, priv->is_active);

964 965 966 967 968
      if (priv->is_active)
        gtk_widget_set_state_flags (GTK_WIDGET (sw), GTK_STATE_FLAG_ACTIVE, FALSE);
      else
        gtk_widget_unset_state_flags (GTK_WIDGET (sw), GTK_STATE_FLAG_ACTIVE);

969 970 971 972 973 974 975 976
      gtk_widget_queue_draw (GTK_WIDGET (sw));
    }
}

/**
 * gtk_switch_get_active:
 * @sw: a #GtkSwitch
 *
977
 * Gets whether the #GtkSwitch is in its “on” or “off” state.
978 979
 *
 * Return value: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
980 981
 *
 * Since: 3.0
982 983 984 985 986 987 988 989 990 991 992 993 994 995
 */
gboolean
gtk_switch_get_active (GtkSwitch *sw)
{
  g_return_val_if_fail (GTK_IS_SWITCH (sw), FALSE);

  return sw->priv->is_active;
}

static void
gtk_switch_update (GtkActivatable *activatable,
                   GtkAction      *action,
                   const gchar    *property_name)
{
996 997
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
  if (strcmp (property_name, "visible") == 0)
    {
      if (gtk_action_is_visible (action))
        gtk_widget_show (GTK_WIDGET (activatable));
      else
        gtk_widget_hide (GTK_WIDGET (activatable));
    }
  else if (strcmp (property_name, "sensitive") == 0)
    {
      gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
    }
  else if (strcmp (property_name, "active") == 0)
    {
      gtk_action_block_activate (action);
      gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
      gtk_action_unblock_activate (action);
    }
1015 1016

  G_GNUC_END_IGNORE_DEPRECATIONS;
1017 1018 1019 1020 1021 1022 1023 1024 1025
}

static void
gtk_switch_sync_action_properties (GtkActivatable *activatable,
                                   GtkAction      *action)
{
  if (!action)
    return;

1026 1027
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
  if (gtk_action_is_visible (action))
    gtk_widget_show (GTK_WIDGET (activatable));
  else
    gtk_widget_hide (GTK_WIDGET (activatable));

  gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));

  gtk_action_block_activate (action);
  gtk_switch_set_active (GTK_SWITCH (activatable), gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
  gtk_action_unblock_activate (action);
1038 1039

  G_GNUC_END_IGNORE_DEPRECATIONS;
1040 1041 1042 1043 1044 1045 1046 1047
}

static void
gtk_switch_activatable_interface_init (GtkActivatableIface *iface)
{
  iface->update = gtk_switch_update;
  iface->sync_action_properties = gtk_switch_sync_action_properties;
}