2010-11-26 13:24:04 +00:00
|
|
|
/* 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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Emmanuele Bassi <ebassi@linux.intel.com>
|
|
|
|
* Matthias Clasen <mclasen@redhat.com>
|
|
|
|
*
|
2010-11-30 21:54:48 +00:00
|
|
|
* Based on similar code from Mx.
|
2010-11-26 13:24:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-02-22 00:43:19 +00:00
|
|
|
* GtkSwitch:
|
|
|
|
*
|
|
|
|
* `GtkSwitch` is a "light switch" that has two states: on or off.
|
|
|
|
*
|
|
|
|
* ![An example GtkSwitch](switch.png)
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* The user can control which state should be active by clicking the
|
|
|
|
* empty area, or by dragging the handle.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* `GtkSwitch` can also handle situations where the underlying state
|
|
|
|
* changes with a delay. See [signal@GtkSwitch::state-set] for details.
|
2015-10-23 03:07:54 +00:00
|
|
|
*
|
|
|
|
* # CSS nodes
|
|
|
|
*
|
2021-02-28 01:06:39 +00:00
|
|
|
* ```
|
2015-11-03 18:28:52 +00:00
|
|
|
* switch
|
2017-03-31 15:03:49 +00:00
|
|
|
* ├── label
|
2017-06-28 13:57:42 +00:00
|
|
|
* ├── label
|
|
|
|
* ╰── slider
|
2021-02-28 01:06:39 +00:00
|
|
|
* ```
|
2015-11-03 18:28:52 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* `GtkSwitch` has four css nodes, the main node with the name switch and
|
|
|
|
* subnodes for the slider and the on and off labels. Neither of them is
|
|
|
|
* using any style classes.
|
2020-07-27 14:46:52 +00:00
|
|
|
*
|
|
|
|
* # Accessibility
|
|
|
|
*
|
2021-02-28 01:06:39 +00:00
|
|
|
* `GtkSwitch` uses the %GTK_ACCESSIBLE_ROLE_SWITCH role.
|
2010-11-26 13:24:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkswitch.h"
|
|
|
|
|
2012-01-11 05:10:34 +00:00
|
|
|
#include "gtkactionable.h"
|
2018-02-02 09:08:41 +00:00
|
|
|
#include "gtkactionhelperprivate.h"
|
2019-05-29 17:10:46 +00:00
|
|
|
#include "gtkgestureclick.h"
|
2018-02-08 13:39:45 +00:00
|
|
|
#include "gtkgesturepan.h"
|
|
|
|
#include "gtkgesturesingle.h"
|
|
|
|
#include "gtkgizmoprivate.h"
|
|
|
|
#include "gtkintl.h"
|
2019-01-24 21:12:47 +00:00
|
|
|
#include "gtkimage.h"
|
2019-03-20 15:37:15 +00:00
|
|
|
#include "gtkcustomlayout.h"
|
2018-02-08 13:39:45 +00:00
|
|
|
#include "gtkmarshalers.h"
|
|
|
|
#include "gtkprivate.h"
|
2016-03-28 21:38:22 +00:00
|
|
|
#include "gtkprogresstrackerprivate.h"
|
2016-05-01 04:40:08 +00:00
|
|
|
#include "gtksettingsprivate.h"
|
2018-02-08 13:39:45 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
|
2019-05-26 19:34:31 +00:00
|
|
|
typedef struct _GtkSwitchClass GtkSwitchClass;
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
struct _GtkSwitch
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
2012-08-17 22:26:53 +00:00
|
|
|
GtkActionHelper *action_helper;
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
GtkGesture *pan_gesture;
|
2019-05-29 17:10:46 +00:00
|
|
|
GtkGesture *click_gesture;
|
2014-05-19 14:08:56 +00:00
|
|
|
|
2015-02-16 05:25:44 +00:00
|
|
|
double handle_pos;
|
2014-08-03 08:20:38 +00:00
|
|
|
guint tick_id;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
guint state : 1;
|
2010-11-26 13:24:04 +00:00
|
|
|
guint is_active : 1;
|
2016-11-15 15:53:03 +00:00
|
|
|
|
2020-07-25 02:57:34 +00:00
|
|
|
GtkProgressTracker tracker;
|
|
|
|
|
2019-01-24 21:12:47 +00:00
|
|
|
GtkWidget *on_image;
|
|
|
|
GtkWidget *off_image;
|
2017-01-08 15:54:34 +00:00
|
|
|
GtkWidget *slider;
|
2010-11-26 13:24:04 +00:00
|
|
|
};
|
2019-05-26 19:34:31 +00:00
|
|
|
|
|
|
|
struct _GtkSwitchClass
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
void (* activate) (GtkSwitch *self);
|
2019-05-26 19:34:31 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gboolean (* state_set) (GtkSwitch *self,
|
|
|
|
gboolean state);
|
2019-05-26 19:34:31 +00:00
|
|
|
};
|
2010-11-26 13:24:04 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ACTIVE,
|
2014-03-29 17:40:08 +00:00
|
|
|
PROP_STATE,
|
2012-01-11 05:10:34 +00:00
|
|
|
LAST_PROP,
|
|
|
|
PROP_ACTION_NAME,
|
|
|
|
PROP_ACTION_TARGET
|
2010-11-26 13:24:04 +00:00
|
|
|
};
|
|
|
|
|
2011-02-25 20:47:03 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ACTIVATE,
|
2014-03-29 17:40:08 +00:00
|
|
|
STATE_SET,
|
2011-02-25 20:47:03 +00:00
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
static GParamSpec *switch_props[LAST_PROP] = { NULL, };
|
|
|
|
|
2012-01-11 05:10:34 +00:00
|
|
|
static void gtk_switch_actionable_iface_init (GtkActionableInterface *iface);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkSwitch, gtk_switch, GTK_TYPE_WIDGET,
|
2012-01-11 05:10:34 +00:00
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE,
|
2016-10-13 14:10:31 +00:00
|
|
|
gtk_switch_actionable_iface_init))
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-08-03 08:20:38 +00:00
|
|
|
static void
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_end_toggle_animation (GtkSwitch *self)
|
2014-08-03 08:20:38 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->tick_id != 0)
|
2014-08-03 08:20:38 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_remove_tick_callback (GTK_WIDGET (self), self->tick_id);
|
|
|
|
self->tick_id = 0;
|
2014-08-03 08:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 15:22:12 +00:00
|
|
|
static gboolean
|
|
|
|
gtk_switch_on_frame_clock_update (GtkWidget *widget,
|
|
|
|
GdkFrameClock *clock,
|
|
|
|
gpointer user_data)
|
2014-08-03 08:20:38 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (widget);
|
2014-08-03 08:20:38 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_progress_tracker_advance_frame (&self->tracker,
|
2016-03-28 21:38:22 +00:00
|
|
|
gdk_frame_clock_get_frame_time (clock));
|
2014-08-03 08:20:38 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (gtk_progress_tracker_get_state (&self->tracker) != GTK_PROGRESS_STATE_AFTER)
|
2014-08-03 08:20:38 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->is_active)
|
|
|
|
self->handle_pos = 1.0 - gtk_progress_tracker_get_ease_out_cubic (&self->tracker, FALSE);
|
2015-02-16 05:25:44 +00:00
|
|
|
else
|
2019-09-10 07:55:37 +00:00
|
|
|
self->handle_pos = gtk_progress_tracker_get_ease_out_cubic (&self->tracker, FALSE);
|
2014-08-03 08:20:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_active (self, !self->is_active);
|
2014-08-03 08:20:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_queue_allocate (GTK_WIDGET (self));
|
2014-09-23 15:22:12 +00:00
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
2014-08-03 08:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ANIMATION_DURATION 100
|
|
|
|
|
|
|
|
static void
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_begin_toggle_animation (GtkSwitch *self)
|
2014-08-03 08:20:38 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
if (gtk_settings_get_enable_animations (gtk_widget_get_settings (GTK_WIDGET (self))))
|
2014-08-03 08:20:38 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_progress_tracker_start (&self->tracker, 1000 * ANIMATION_DURATION, 0, 1.0);
|
|
|
|
if (self->tick_id == 0)
|
|
|
|
self->tick_id = gtk_widget_add_tick_callback (GTK_WIDGET (self),
|
2014-09-23 15:22:12 +00:00
|
|
|
gtk_switch_on_frame_clock_update,
|
|
|
|
NULL, NULL);
|
2014-08-03 08:20:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_active (self, !self->is_active);
|
2014-08-03 08:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
static void
|
2019-05-29 17:10:46 +00:00
|
|
|
gtk_switch_click_gesture_pressed (GtkGestureClick *gesture,
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_press,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2018-04-07 11:37:24 +00:00
|
|
|
graphene_rect_t switch_bounds;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (!gtk_widget_compute_bounds (GTK_WIDGET (self), GTK_WIDGET (self), &switch_bounds))
|
2019-02-20 03:53:47 +00:00
|
|
|
return;
|
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
/* If the press didn't happen in the draggable handle,
|
|
|
|
* cancel the pan gesture right away
|
|
|
|
*/
|
2019-09-10 07:55:37 +00:00
|
|
|
if ((self->is_active && x <= switch_bounds.size.width / 2.0) ||
|
|
|
|
(!self->is_active && x > switch_bounds.size.width / 2.0))
|
|
|
|
gtk_gesture_set_state (self->pan_gesture, GTK_EVENT_SEQUENCE_DENIED);
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
static void
|
2019-05-29 17:10:46 +00:00
|
|
|
gtk_switch_click_gesture_released (GtkGestureClick *gesture,
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_press,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2014-05-19 14:08:56 +00:00
|
|
|
GdkEventSequence *sequence;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (gtk_widget_contains (GTK_WIDGET (self), x, y) &&
|
2014-05-19 14:08:56 +00:00
|
|
|
gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
|
2020-06-26 15:35:05 +00:00
|
|
|
{
|
|
|
|
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
|
|
|
|
gtk_switch_begin_toggle_animation (self);
|
|
|
|
}
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
static void
|
|
|
|
gtk_switch_pan_gesture_pan (GtkGesturePan *gesture,
|
|
|
|
GtkPanDirection direction,
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset,
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (self);
|
2017-12-04 11:42:52 +00:00
|
|
|
int width;
|
2014-05-19 14:08:56 +00:00
|
|
|
|
2017-12-04 11:42:52 +00:00
|
|
|
width = gtk_widget_get_width (widget);
|
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
if (direction == GTK_PAN_DIRECTION_LEFT)
|
|
|
|
offset = -offset;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->is_active)
|
2015-02-16 05:25:44 +00:00
|
|
|
offset += width / 2;
|
|
|
|
|
|
|
|
offset /= width / 2;
|
2014-05-19 14:08:56 +00:00
|
|
|
/* constrain the handle within the trough width */
|
2019-09-10 07:55:37 +00:00
|
|
|
self->handle_pos = CLAMP (offset, 0, 1.0);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
/* we need to redraw the handle */
|
2015-03-09 05:16:50 +00:00
|
|
|
gtk_widget_queue_allocate (widget);
|
2014-05-19 14:08:56 +00:00
|
|
|
}
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
static void
|
|
|
|
gtk_switch_pan_gesture_drag_end (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self)
|
2014-05-19 14:08:56 +00:00
|
|
|
{
|
|
|
|
GdkEventSequence *sequence;
|
2015-02-16 05:25:44 +00:00
|
|
|
gboolean active;
|
2011-01-11 11:34:23 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
2011-01-11 11:34:23 +00:00
|
|
|
|
2014-05-19 14:08:56 +00:00
|
|
|
if (gtk_gesture_get_sequence_state (GTK_GESTURE (gesture), sequence) == GTK_EVENT_SEQUENCE_CLAIMED)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
|
|
|
/* if half the handle passed the middle of the switch, then we
|
|
|
|
* consider it to be on
|
|
|
|
*/
|
2019-09-10 07:55:37 +00:00
|
|
|
active = self->handle_pos >= 0.5;
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
2019-09-10 07:55:37 +00:00
|
|
|
else if (!gtk_gesture_handles_sequence (self->click_gesture, sequence))
|
|
|
|
active = self->is_active;
|
2014-05-19 14:08:56 +00:00
|
|
|
else
|
|
|
|
return;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
self->handle_pos = active ? 1.0 : 0.0;
|
|
|
|
gtk_switch_set_active (self, active);
|
|
|
|
gtk_widget_queue_allocate (GTK_WIDGET (self));
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 20:47:03 +00:00
|
|
|
static void
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_activate (GtkSwitch *self)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_begin_toggle_animation (self);
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-09 05:16:50 +00:00
|
|
|
static void
|
2017-05-03 20:22:49 +00:00
|
|
|
gtk_switch_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2017-09-30 17:34:10 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (widget);
|
2020-07-24 13:54:49 +00:00
|
|
|
int slider_minimum, slider_natural;
|
2016-11-15 15:53:03 +00:00
|
|
|
int on_nat, off_nat;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_measure (self->slider, orientation, -1,
|
2017-01-08 15:54:34 +00:00
|
|
|
&slider_minimum, &slider_natural,
|
|
|
|
NULL, NULL);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_measure (self->on_image, orientation, for_size, NULL, &on_nat, NULL, NULL);
|
|
|
|
gtk_widget_measure (self->off_image, orientation, for_size, NULL, &off_nat, NULL, NULL);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2015-03-09 05:16:50 +00:00
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
{
|
2016-11-15 15:53:03 +00:00
|
|
|
int text_width = MAX (on_nat, off_nat);
|
2015-03-09 05:16:50 +00:00
|
|
|
*minimum = 2 * MAX (slider_minimum, text_width);
|
|
|
|
*natural = 2 * MAX (slider_natural, text_width);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-15 15:53:03 +00:00
|
|
|
int text_height = MAX (on_nat, off_nat);
|
2015-03-09 05:16:50 +00:00
|
|
|
*minimum = MAX (slider_minimum, text_height);
|
|
|
|
*natural = MAX (slider_natural, text_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-03-04 13:36:52 +00:00
|
|
|
gtk_switch_allocate (GtkWidget *widget,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int baseline)
|
2015-03-09 05:16:50 +00:00
|
|
|
{
|
2017-05-03 20:22:49 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (widget);
|
2016-11-15 15:53:03 +00:00
|
|
|
GtkAllocation child_alloc;
|
|
|
|
int min;
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_size_allocate (self->slider,
|
2018-11-20 17:11:50 +00:00
|
|
|
&(GtkAllocation) {
|
2019-09-10 07:55:37 +00:00
|
|
|
round (self->handle_pos * (width / 2)), 0,
|
2018-11-20 17:11:50 +00:00
|
|
|
width / 2, height
|
|
|
|
}, -1);
|
2016-01-12 20:36:37 +00:00
|
|
|
|
2019-01-24 21:12:47 +00:00
|
|
|
/* Center ON icon in left half */
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_measure (self->on_image, GTK_ORIENTATION_HORIZONTAL, -1, &min, NULL, NULL, NULL);
|
2018-08-16 04:53:03 +00:00
|
|
|
child_alloc.x = ((width / 2) - min) / 2;
|
2016-11-15 15:53:03 +00:00
|
|
|
child_alloc.width = min;
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_measure (self->on_image, GTK_ORIENTATION_VERTICAL, min, &min, NULL, NULL, NULL);
|
2018-08-16 04:53:03 +00:00
|
|
|
child_alloc.y = (height - min) / 2;
|
2016-11-15 15:53:03 +00:00
|
|
|
child_alloc.height = min;
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_size_allocate (self->on_image, &child_alloc, -1);
|
2016-11-15 15:53:03 +00:00
|
|
|
|
2019-01-24 21:12:47 +00:00
|
|
|
/* Center OFF icon in right half */
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_measure (self->off_image, GTK_ORIENTATION_HORIZONTAL, -1, &min, NULL, NULL, NULL);
|
2018-08-16 04:53:03 +00:00
|
|
|
child_alloc.x = (width / 2) + ((width / 2) - min) / 2;
|
2016-11-15 15:53:03 +00:00
|
|
|
child_alloc.width = min;
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_measure (self->off_image, GTK_ORIENTATION_VERTICAL, min, &min, NULL, NULL, NULL);
|
2018-08-16 04:53:03 +00:00
|
|
|
child_alloc.y = (height - min) / 2;
|
2016-11-15 15:53:03 +00:00
|
|
|
child_alloc.height = min;
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_size_allocate (self->off_image, &child_alloc, -1);
|
2019-01-18 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2012-01-11 05:10:34 +00:00
|
|
|
static void
|
|
|
|
gtk_switch_set_action_name (GtkActionable *actionable,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *action_name)
|
2012-01-11 05:10:34 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (actionable);
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (!self->action_helper)
|
|
|
|
self->action_helper = gtk_action_helper_new (actionable);
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_action_helper_set_action_name (self->action_helper, action_name);
|
2012-01-11 05:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_switch_set_action_target_value (GtkActionable *actionable,
|
|
|
|
GVariant *action_target)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (actionable);
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (!self->action_helper)
|
|
|
|
self->action_helper = gtk_action_helper_new (actionable);
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_action_helper_set_action_target_value (self->action_helper, action_target);
|
2012-01-11 05:10:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
static const char *
|
2012-01-11 05:10:34 +00:00
|
|
|
gtk_switch_get_action_name (GtkActionable *actionable)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (actionable);
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
return gtk_action_helper_get_action_name (self->action_helper);
|
2012-01-11 05:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GVariant *
|
|
|
|
gtk_switch_get_action_target_value (GtkActionable *actionable)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (actionable);
|
2012-01-11 05:10:34 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
return gtk_action_helper_get_action_target_value (self->action_helper);
|
2012-01-11 05:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
static void
|
|
|
|
gtk_switch_set_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (gobject);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_ACTIVE:
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_active (self, g_value_get_boolean (value));
|
2010-11-26 13:24:04 +00:00
|
|
|
break;
|
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
case PROP_STATE:
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_state (self, g_value_get_boolean (value));
|
2014-03-29 17:40:08 +00:00
|
|
|
break;
|
|
|
|
|
2012-01-11 05:10:34 +00:00
|
|
|
case PROP_ACTION_NAME:
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_action_name (GTK_ACTIONABLE (self), g_value_get_string (value));
|
2012-01-11 05:10:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ACTION_TARGET:
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_action_target_value (GTK_ACTIONABLE (self), g_value_get_variant (value));
|
2012-01-11 05:10:34 +00:00
|
|
|
break;
|
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (gobject);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_ACTIVE:
|
2019-09-10 07:55:37 +00:00
|
|
|
g_value_set_boolean (value, self->is_active);
|
2010-11-26 13:24:04 +00:00
|
|
|
break;
|
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
case PROP_STATE:
|
2019-09-10 07:55:37 +00:00
|
|
|
g_value_set_boolean (value, self->state);
|
2014-03-29 17:40:08 +00:00
|
|
|
break;
|
|
|
|
|
2012-01-11 05:10:34 +00:00
|
|
|
case PROP_ACTION_NAME:
|
2019-09-10 07:55:37 +00:00
|
|
|
g_value_set_string (value, gtk_action_helper_get_action_name (self->action_helper));
|
2012-01-11 05:10:34 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_ACTION_TARGET:
|
2019-09-10 07:55:37 +00:00
|
|
|
g_value_set_variant (value, gtk_action_helper_get_action_target_value (self->action_helper));
|
2012-01-11 05:10:34 +00:00
|
|
|
break;
|
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_switch_dispose (GObject *object)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (object);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
g_clear_object (&self->action_helper);
|
2015-03-09 05:16:50 +00:00
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
G_OBJECT_CLASS (gtk_switch_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2014-08-04 12:25:10 +00:00
|
|
|
static void
|
|
|
|
gtk_switch_finalize (GObject *object)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
GtkSwitch *self = GTK_SWITCH (object);
|
2017-01-07 14:58:55 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_end_toggle_animation (self);
|
2014-08-04 12:25:10 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_unparent (self->on_image);
|
|
|
|
gtk_widget_unparent (self->off_image);
|
|
|
|
gtk_widget_unparent (self->slider);
|
2017-01-07 14:58:55 +00:00
|
|
|
|
2014-08-04 12:25:10 +00:00
|
|
|
G_OBJECT_CLASS (gtk_switch_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
static gboolean
|
2019-09-10 07:55:37 +00:00
|
|
|
state_set (GtkSwitch *self,
|
|
|
|
gboolean state)
|
2014-03-29 17:40:08 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->action_helper)
|
|
|
|
gtk_action_helper_activate (self->action_helper);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_state (self, state);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
static void
|
|
|
|
gtk_switch_class_init (GtkSwitchClass *klass)
|
|
|
|
{
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
/**
|
2021-02-25 17:23:50 +00:00
|
|
|
* GtkSwitch:active: (attributes org.gtk.Property.get=gtk_switch_get_active org.gtk.Property.set=gtk_switch_set_active)
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Whether the `GtkSwitch` widget is in its on or off state.
|
2010-11-26 13:24:04 +00:00
|
|
|
*/
|
|
|
|
switch_props[PROP_ACTIVE] =
|
|
|
|
g_param_spec_boolean ("active",
|
|
|
|
P_("Active"),
|
|
|
|
P_("Whether the switch is on or off"),
|
|
|
|
FALSE,
|
2014-06-09 15:48:09 +00:00
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
/**
|
2021-02-25 17:23:50 +00:00
|
|
|
* GtkSwitch:state: (attributes org.gtk.Property.get=gtk_switch_get_state org.gtk.Property.set=gtk_switch_set_state)
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* The backend state that is controlled by the switch.
|
|
|
|
*
|
|
|
|
* See [signal@GtkSwitch::state-set] for details.
|
2014-03-29 17:40:08 +00:00
|
|
|
*/
|
|
|
|
switch_props[PROP_STATE] =
|
|
|
|
g_param_spec_boolean ("state",
|
|
|
|
P_("State"),
|
|
|
|
P_("The backend state"),
|
|
|
|
FALSE,
|
2014-06-09 15:48:09 +00:00
|
|
|
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
gobject_class->set_property = gtk_switch_set_property;
|
|
|
|
gobject_class->get_property = gtk_switch_get_property;
|
|
|
|
gobject_class->dispose = gtk_switch_dispose;
|
2014-08-04 12:25:10 +00:00
|
|
|
gobject_class->finalize = gtk_switch_finalize;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, LAST_PROP, switch_props);
|
|
|
|
|
2011-02-25 20:47:03 +00:00
|
|
|
klass->activate = gtk_switch_activate;
|
2014-03-29 17:40:08 +00:00
|
|
|
klass->state_set = state_set;
|
2011-02-25 20:47:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkSwitch::activate:
|
2021-02-22 00:43:19 +00:00
|
|
|
* @widget: the object which received the signal
|
2011-02-25 20:47:03 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Emitted to animate the switch.
|
|
|
|
*
|
|
|
|
* Applications should never connect to this signal,
|
|
|
|
* but use the [property@Gtk.Switch:active] property.
|
2011-02-25 20:47:03 +00:00
|
|
|
*/
|
|
|
|
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,
|
2016-08-29 14:00:17 +00:00
|
|
|
NULL,
|
2011-02-25 20:47:03 +00:00
|
|
|
G_TYPE_NONE, 0);
|
2020-12-04 23:12:20 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_activate_signal (widget_class, signals[ACTIVATE]);
|
2011-02-25 20:47:03 +00:00
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
/**
|
|
|
|
* GtkSwitch::state-set:
|
|
|
|
* @widget: the object on which the signal was emitted
|
2014-06-29 22:35:31 +00:00
|
|
|
* @state: the new state of the switch
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Emitted to change the underlying state.
|
|
|
|
*
|
|
|
|
* The ::state-set signal is emitted when the user changes the switch
|
|
|
|
* position. The default handler keeps the state in sync with the
|
|
|
|
* [property@Gtk.Switch:active] property.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* To implement delayed state change, applications can connect to this
|
|
|
|
* signal, initiate the change of the underlying state, and call
|
|
|
|
* [method@Gtk.Switch.set_state] when the underlying state change is
|
|
|
|
* complete. The signal handler should return %TRUE to prevent the
|
|
|
|
* default handler from running.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
|
|
|
* Visually, the underlying state is represented by the trough color of
|
2021-02-22 00:43:19 +00:00
|
|
|
* the switch, while the [property@Gtk.Switch`:active] property is
|
|
|
|
* represented by the position of the switch.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE to stop the signal emission
|
|
|
|
*/
|
|
|
|
signals[STATE_SET] =
|
|
|
|
g_signal_new (I_("state-set"),
|
|
|
|
G_OBJECT_CLASS_TYPE (gobject_class),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GtkSwitchClass, state_set),
|
|
|
|
_gtk_boolean_handled_accumulator, NULL,
|
|
|
|
_gtk_marshal_BOOLEAN__BOOLEAN,
|
|
|
|
G_TYPE_BOOLEAN, 1,
|
|
|
|
G_TYPE_BOOLEAN);
|
2019-06-06 22:27:54 +00:00
|
|
|
g_signal_set_va_marshaller (signals[STATE_SET],
|
|
|
|
G_TYPE_FROM_CLASS (gobject_class),
|
|
|
|
_gtk_marshal_BOOLEAN__BOOLEANv);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
2012-01-11 05:10:34 +00:00
|
|
|
g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
|
|
|
|
g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
|
|
|
|
|
2017-11-18 03:49:57 +00:00
|
|
|
gtk_widget_class_set_css_name (widget_class, I_("switch"));
|
2020-07-17 13:31:02 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_SWITCH);
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_switch_init (GtkSwitch *self)
|
|
|
|
{
|
2019-03-04 13:36:52 +00:00
|
|
|
GtkLayoutManager *layout;
|
2014-05-19 14:08:56 +00:00
|
|
|
GtkGesture *gesture;
|
|
|
|
|
2020-05-05 18:57:21 +00:00
|
|
|
gtk_widget_set_focusable (GTK_WIDGET (self), TRUE);
|
2014-05-19 14:08:56 +00:00
|
|
|
|
2019-05-29 17:10:46 +00:00
|
|
|
gesture = gtk_gesture_click_new ();
|
2014-05-19 14:08:56 +00:00
|
|
|
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (gesture), FALSE);
|
|
|
|
gtk_gesture_single_set_exclusive (GTK_GESTURE_SINGLE (gesture), TRUE);
|
|
|
|
g_signal_connect (gesture, "pressed",
|
2019-05-29 17:10:46 +00:00
|
|
|
G_CALLBACK (gtk_switch_click_gesture_pressed), self);
|
2014-05-19 14:08:56 +00:00
|
|
|
g_signal_connect (gesture, "released",
|
2019-05-29 17:10:46 +00:00
|
|
|
G_CALLBACK (gtk_switch_click_gesture_released), self);
|
2014-05-26 12:02:30 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
2014-05-27 10:38:49 +00:00
|
|
|
GTK_PHASE_BUBBLE);
|
2018-03-09 05:14:59 +00:00
|
|
|
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
|
2019-09-10 07:55:37 +00:00
|
|
|
self->click_gesture = gesture;
|
2014-05-19 14:08:56 +00:00
|
|
|
|
2018-03-08 23:03:15 +00:00
|
|
|
gesture = gtk_gesture_pan_new (GTK_ORIENTATION_HORIZONTAL);
|
2014-05-19 14:08:56 +00:00
|
|
|
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (gesture), FALSE);
|
|
|
|
gtk_gesture_single_set_exclusive (GTK_GESTURE_SINGLE (gesture), TRUE);
|
|
|
|
g_signal_connect (gesture, "pan",
|
|
|
|
G_CALLBACK (gtk_switch_pan_gesture_pan), self);
|
|
|
|
g_signal_connect (gesture, "drag-end",
|
|
|
|
G_CALLBACK (gtk_switch_pan_gesture_drag_end), self);
|
2014-05-26 12:02:30 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
2017-04-02 14:35:16 +00:00
|
|
|
GTK_PHASE_CAPTURE);
|
2018-03-08 23:03:15 +00:00
|
|
|
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (gesture));
|
2019-09-10 07:55:37 +00:00
|
|
|
self->pan_gesture = gesture;
|
2016-05-04 13:25:59 +00:00
|
|
|
|
2019-03-20 15:37:15 +00:00
|
|
|
layout = gtk_custom_layout_new (NULL,
|
2019-03-04 13:36:52 +00:00
|
|
|
gtk_switch_measure,
|
|
|
|
gtk_switch_allocate);
|
|
|
|
gtk_widget_set_layout_manager (GTK_WIDGET (self), layout);
|
2019-01-15 22:16:05 +00:00
|
|
|
|
2020-10-21 18:55:13 +00:00
|
|
|
self->on_image = g_object_new (GTK_TYPE_IMAGE,
|
|
|
|
"accessible-role", GTK_ACCESSIBLE_ROLE_NONE,
|
|
|
|
"icon-name", "switch-on-symbolic",
|
|
|
|
NULL);
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_set_parent (self->on_image, GTK_WIDGET (self));
|
2016-11-15 15:53:03 +00:00
|
|
|
|
2020-10-21 18:55:13 +00:00
|
|
|
self->off_image = g_object_new (GTK_TYPE_IMAGE,
|
|
|
|
"accessible-role", GTK_ACCESSIBLE_ROLE_NONE,
|
|
|
|
"icon-name", "switch-off-symbolic",
|
|
|
|
NULL);
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_set_parent (self->off_image, GTK_WIDGET (self));
|
2017-06-28 13:57:42 +00:00
|
|
|
|
2020-10-21 18:55:13 +00:00
|
|
|
self->slider = gtk_gizmo_new_with_role ("slider",
|
|
|
|
GTK_ACCESSIBLE_ROLE_NONE,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL);
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_set_parent (self->slider, GTK_WIDGET (self));
|
2020-07-27 23:57:13 +00:00
|
|
|
|
|
|
|
gtk_accessible_update_state (GTK_ACCESSIBLE (self),
|
|
|
|
GTK_ACCESSIBLE_STATE_CHECKED, FALSE,
|
|
|
|
-1);
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_switch_new:
|
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Creates a new `GtkSwitch` widget.
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Returns: the newly created `GtkSwitch` instance
|
2010-11-26 13:24:04 +00:00
|
|
|
*/
|
|
|
|
GtkWidget *
|
|
|
|
gtk_switch_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_SWITCH, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 17:23:50 +00:00
|
|
|
* gtk_switch_set_active: (attributes org.gtk.Method.set_property=active)
|
2021-02-22 00:43:19 +00:00
|
|
|
* @self: a `GtkSwitch`
|
2019-09-10 07:55:37 +00:00
|
|
|
* @is_active: %TRUE if @self should be active, and %FALSE otherwise
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
2019-09-10 07:55:37 +00:00
|
|
|
* Changes the state of @self to the desired one.
|
2010-11-26 13:24:04 +00:00
|
|
|
*/
|
|
|
|
void
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_active (GtkSwitch *self,
|
2010-11-26 13:24:04 +00:00
|
|
|
gboolean is_active)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
g_return_if_fail (GTK_IS_SWITCH (self));
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_end_toggle_animation (self);
|
2014-08-04 12:21:59 +00:00
|
|
|
|
2010-11-26 13:24:04 +00:00
|
|
|
is_active = !!is_active;
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->is_active != is_active)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2014-03-29 17:40:08 +00:00
|
|
|
gboolean handled;
|
2010-11-29 00:18:56 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
self->is_active = is_active;
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->is_active)
|
|
|
|
self->handle_pos = 1.0;
|
2014-08-04 12:30:23 +00:00
|
|
|
else
|
2019-09-10 07:55:37 +00:00
|
|
|
self->handle_pos = 0.0;
|
2014-08-04 12:30:23 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
g_signal_emit (self, signals[STATE_SET], 0, is_active, &handled);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), switch_props[PROP_ACTIVE]);
|
2015-07-02 01:02:57 +00:00
|
|
|
|
2020-07-17 13:31:02 +00:00
|
|
|
gtk_accessible_update_state (GTK_ACCESSIBLE (self),
|
|
|
|
GTK_ACCESSIBLE_STATE_CHECKED, is_active,
|
|
|
|
-1);
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_queue_allocate (GTK_WIDGET (self));
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 17:23:50 +00:00
|
|
|
* gtk_switch_get_active: (attributes org.gtk.Method.get_property=active)
|
2021-02-22 00:43:19 +00:00
|
|
|
* @self: a `GtkSwitch`
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Gets whether the `GtkSwitch` is in its “on” or “off” state.
|
2010-11-26 13:24:04 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Returns: %TRUE if the `GtkSwitch` is active, and %FALSE otherwise
|
2010-11-26 13:24:04 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_get_active (GtkSwitch *self)
|
2010-11-26 13:24:04 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_SWITCH (self), FALSE);
|
2010-11-26 13:24:04 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
return self->is_active;
|
2010-11-26 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 17:40:08 +00:00
|
|
|
/**
|
2021-02-25 17:23:50 +00:00
|
|
|
* gtk_switch_set_state: (attributes org.gtk.Method.set_property=state)
|
2021-02-22 00:43:19 +00:00
|
|
|
* @self: a `GtkSwitch`
|
2014-03-29 17:40:08 +00:00
|
|
|
* @state: the new state
|
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Sets the underlying state of the `GtkSwitch`.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Normally, this is the same as [property@Gtk.Switch:active], unless
|
|
|
|
* the switch is set up for delayed state changes. This function is
|
2021-05-25 20:29:46 +00:00
|
|
|
* typically called from a [signal@Gtk.Switch::state-set] signal handler.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* See [signal@Gtk.Switch::state-set] for details.
|
2014-03-29 17:40:08 +00:00
|
|
|
*/
|
|
|
|
void
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_state (GtkSwitch *self,
|
2014-03-29 17:40:08 +00:00
|
|
|
gboolean state)
|
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
g_return_if_fail (GTK_IS_SWITCH (self));
|
2014-03-29 17:40:08 +00:00
|
|
|
|
|
|
|
state = state != FALSE;
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
if (self->state == state)
|
2014-03-29 17:40:08 +00:00
|
|
|
return;
|
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
self->state = state;
|
2014-03-29 17:40:08 +00:00
|
|
|
|
|
|
|
/* This will be a no-op if we're switching the state in response
|
|
|
|
* to a UI change. We're setting active anyway, to catch 'spontaneous'
|
|
|
|
* state changes.
|
|
|
|
*/
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_set_active (self, state);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
|
|
|
if (state)
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_set_state_flags (GTK_WIDGET (self), GTK_STATE_FLAG_CHECKED, FALSE);
|
2014-03-29 17:40:08 +00:00
|
|
|
else
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_widget_unset_state_flags (GTK_WIDGET (self), GTK_STATE_FLAG_CHECKED);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), switch_props[PROP_STATE]);
|
2014-03-29 17:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 17:23:50 +00:00
|
|
|
* gtk_switch_get_state: (attributes org.gtk.Method.get_property=state)
|
2021-02-22 00:43:19 +00:00
|
|
|
* @self: a `GtkSwitch`
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
2021-02-22 00:43:19 +00:00
|
|
|
* Gets the underlying state of the `GtkSwitch`.
|
2014-03-29 17:40:08 +00:00
|
|
|
*
|
|
|
|
* Returns: the underlying state
|
|
|
|
*/
|
|
|
|
gboolean
|
2019-09-10 07:55:37 +00:00
|
|
|
gtk_switch_get_state (GtkSwitch *self)
|
2014-03-29 17:40:08 +00:00
|
|
|
{
|
2019-09-10 07:55:37 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_SWITCH (self), FALSE);
|
2014-03-29 17:40:08 +00:00
|
|
|
|
2019-09-10 07:55:37 +00:00
|
|
|
return self->state;
|
2014-03-29 17:40:08 +00:00
|
|
|
}
|