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
|
// 7 april 2015
#include "uipriv_unix.h"
struct checkbox {
uiCheckbox c;
void (*onToggled)(uiCheckbox *, void *);
void *onToggledData;
gulong onToggledSignal;
};
static void onToggled(GtkToggleButton *b, gpointer data)
{
struct checkbox *c = (struct checkbox *) data;
(*(c->onToggled))(uiCheckbox(c), c->onToggledData);
}
static void defaultOnToggled(uiCheckbox *c, void *data)
{
// do nothing
}
static void onDestroy(GtkWidget *widget, gpointer data)
{
struct checkbox *c = (struct checkbox *) data;
uiFree(c);
}
#define CHECKBOX(c) GTK_CHECK_BUTTON(uiControlHandle(uiControl(c)))
static char *getText(uiCheckbox *c)
{
return g_strdup(gtk_button_get_label(GTK_BUTTON(CHECKBOX(c))));
}
static void setText(uiCheckbox *c, const char *text)
{
gtk_button_set_label(GTK_BUTTON(CHECKBOX(c)), text);
}
static void setOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
{
struct checkbox *c = (struct checkbox *) cc;
c->onToggled = f;
c->onToggledData = data;
}
static int getChecked(uiCheckbox *c)
{
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(CHECKBOX(c))) != FALSE;
}
static void setChecked(uiCheckbox *cc, int checked)
{
struct checkbox *c = (struct checkbox *) cc;
GtkToggleButton *button;
gboolean active;
active = FALSE;
if (checked)
active = TRUE;
// we need to inhibit sending of ::toggled because this WILL send a ::toggled otherwise
button = GTK_TOGGLE_BUTTON(CHECKBOX(c));
g_signal_handler_block(button, c->onToggledSignal);
gtk_toggle_button_set_active(button, active);
g_signal_handler_unblock(button, c->onToggledSignal);
}
uiCheckbox *uiNewCheckbox(const char *text)
{
struct checkbox *c;
GtkWidget *widget;
c = uiNew(struct checkbox);
uiUnixNewControl(uiControl(c), GTK_TYPE_CHECK_BUTTON,
FALSE, FALSE,
"label", text,
NULL);
widget = GTK_WIDGET(CHECKBOX(c));
g_signal_connect(widget, "destroy", G_CALLBACK(onDestroy), c);
c->onToggledSignal = g_signal_connect(widget, "toggled", G_CALLBACK(onToggled), c);
c->onToggled = defaultOnToggled;
uiCheckbox(c)->Text = getText;
uiCheckbox(c)->SetText = setText;
uiCheckbox(c)->OnToggled = setOnToggled;
uiCheckbox(c)->Checked = getChecked;
uiCheckbox(c)->SetChecked = setChecked;
return uiCheckbox(c);
}
|