aboutsummaryrefslogtreecommitdiff
path: root/src/libui_sdl/libui/unix/checkbox.c
blob: 47f851456a78897ddab3523f9593e62f89131ede (plain)
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
// 10 june 2015
#include "uipriv_unix.h"

struct uiCheckbox {
	uiUnixControl c;
	GtkWidget *widget;
	GtkButton *button;
	GtkToggleButton *toggleButton;
	GtkCheckButton *checkButton;
	void (*onToggled)(uiCheckbox *, void *);
	void *onToggledData;
	gulong onToggledSignal;
};

uiUnixControlAllDefaults(uiCheckbox)

static void onToggled(GtkToggleButton *b, gpointer data)
{
	uiCheckbox *c = uiCheckbox(data);

	(*(c->onToggled))(c, c->onToggledData);
}

static void defaultOnToggled(uiCheckbox *c, void *data)
{
	// do nothing
}

char *uiCheckboxText(uiCheckbox *c)
{
	return uiUnixStrdupText(gtk_button_get_label(c->button));
}

void uiCheckboxSetText(uiCheckbox *c, const char *text)
{
	gtk_button_set_label(GTK_BUTTON(c->button), text);
}

void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data)
{
	c->onToggled = f;
	c->onToggledData = data;
}

int uiCheckboxChecked(uiCheckbox *c)
{
	return gtk_toggle_button_get_active(c->toggleButton) != FALSE;
}

void uiCheckboxSetChecked(uiCheckbox *c, int checked)
{
	gboolean active;

	active = FALSE;
	if (checked)
		active = TRUE;
	// we need to inhibit sending of ::toggled because this WILL send a ::toggled otherwise
	g_signal_handler_block(c->toggleButton, c->onToggledSignal);
	gtk_toggle_button_set_active(c->toggleButton, active);
	g_signal_handler_unblock(c->toggleButton, c->onToggledSignal);
}

uiCheckbox *uiNewCheckbox(const char *text)
{
	uiCheckbox *c;

	uiUnixNewControl(uiCheckbox, c);

	c->widget = gtk_check_button_new_with_label(text);
	c->button = GTK_BUTTON(c->widget);
	c->toggleButton = GTK_TOGGLE_BUTTON(c->widget);
	c->checkButton = GTK_CHECK_BUTTON(c->widget);

	c->onToggledSignal = g_signal_connect(c->widget, "toggled", G_CALLBACK(onToggled), c);
	uiCheckboxOnToggled(c, defaultOnToggled, NULL);

	return c;
}