1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include <stdlib.h>
#include <gtk/gtk.h>
static gboolean
boolean_to_text (GBinding *binding,
const GValue *source,
GValue *target,
gpointer dummy G_GNUC_UNUSED)
{
if (g_value_get_boolean (source))
g_value_set_string (target, "Enabled");
else
g_value_set_string (target, "Disabled");
return TRUE;
}
static GtkWidget *
make_switch (gboolean is_on,
gboolean is_sensitive)
{
GtkWidget *hbox;
GtkWidget *sw, *label;
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
sw = gtk_switch_new ();
gtk_switch_set_active (GTK_SWITCH (sw), is_on);
gtk_box_pack_start (GTK_BOX (hbox), sw);
gtk_widget_set_sensitive (sw, is_sensitive);
label = gtk_label_new (is_on ? "Enabled" : "Disabled");
gtk_widget_set_hexpand (label, TRUE);
gtk_box_pack_end (GTK_BOX (hbox), label);
g_object_bind_property_full (sw, "active",
label, "label",
G_BINDING_DEFAULT,
boolean_to_text,
NULL,
NULL, NULL);
return hbox;
}
typedef struct {
GtkSwitch *sw;
gboolean state;
} SetStateData;
static gboolean
set_state_delayed (gpointer data)
{
SetStateData *d = data;
gtk_switch_set_state (d->sw, d->state);
g_object_set_data (G_OBJECT (d->sw), "timeout", NULL);
return G_SOURCE_REMOVE;
}
static gboolean
set_state (GtkSwitch *sw, gboolean state, gpointer data)
{
SetStateData *d;
guint id;
id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (sw), "timeout"));
if (id != 0)
g_source_remove (id);
d = g_new (SetStateData, 1);
d->sw = sw;
d->state = state;
id = g_timeout_add_full (G_PRIORITY_DEFAULT, 2000, set_state_delayed, d, g_free);
g_object_set_data (G_OBJECT (sw), "timeout", GUINT_TO_POINTER (id));
return TRUE;
}
static void
sw_delay_notify (GObject *obj, GParamSpec *pspec, gpointer data)
{
GtkWidget *spinner = data;
gboolean active;
gboolean state;
g_object_get (obj,
"active", &active,
"state", &state,
NULL);
if (active != state)
{
gtk_spinner_start (GTK_SPINNER (spinner));
gtk_widget_set_opacity (spinner, 1.0);
}
else
{
gtk_widget_set_opacity (spinner, 0.0);
gtk_spinner_stop (GTK_SPINNER (spinner));
}
}
static GtkWidget *
make_delayed_switch (gboolean is_on,
gboolean is_sensitive)
{
GtkWidget *hbox;
GtkWidget *sw, *label, *spinner, *check;
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
sw = gtk_switch_new ();
gtk_switch_set_active (GTK_SWITCH (sw), is_on);
gtk_box_pack_start (GTK_BOX (hbox), sw);
gtk_widget_set_sensitive (sw, is_sensitive);
g_signal_connect (sw, "state-set", G_CALLBACK (set_state), NULL);
spinner = gtk_spinner_new ();
gtk_box_pack_start (GTK_BOX (hbox), spinner);
gtk_widget_set_opacity (spinner, 0.0);
check = gtk_check_button_new ();
gtk_box_pack_end (GTK_BOX (hbox), check);
g_object_bind_property (sw, "state",
check, "active",
G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
label = gtk_label_new (is_on ? "Enabled" : "Disabled");
gtk_widget_set_hexpand (label, TRUE);
gtk_box_pack_end (GTK_BOX (hbox), label);
g_object_bind_property_full (sw, "active",
label, "label",
G_BINDING_DEFAULT,
boolean_to_text,
NULL,
NULL, NULL);
g_signal_connect (sw, "notify", G_CALLBACK (sw_delay_notify), spinner);
return hbox;
}
int main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vbox, *hbox;
gtk_init ();
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "GtkSwitch");
gtk_window_set_default_size (GTK_WINDOW (window), 400, -1);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show (window);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
hbox = make_switch (FALSE, TRUE);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_widget_show (hbox);
hbox = make_switch (TRUE, TRUE);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_widget_show (hbox);
hbox = make_switch (FALSE, FALSE);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_widget_show (hbox);
hbox = make_switch (TRUE, FALSE);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_widget_show (hbox);
hbox = make_delayed_switch (FALSE, TRUE);
gtk_container_add (GTK_CONTAINER (vbox), hbox);
gtk_widget_show (hbox);
gtk_main ();
return EXIT_SUCCESS;
}
|