summaryrefslogtreecommitdiff
path: root/new/unix/button.c
blob: 150e049efe63f36a734654ee2f3821bd74b16f71 (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
// 7 april 2015
#include "uipriv_unix.h"

struct button {
	void (*onClicked)(uiControl *, void *);
	void *onClickedData;
};

static void onClicked(GtkButton *button, gpointer data)
{
	uiControl *c = uiControl(data);
	struct button *b = (struct button *) (c->data);

	(*(b->onClicked))(c, b->onClickedData);
}

static void defaultOnClicked(uiControl *c, void *data)
{
	// do nothing
}

static void onDestroy(GtkWidget *widget, gpointer data)
{
	struct button *b = (struct button *) data;

	uiFree(b);
}

static char *getText(uiButton *b)
{
	return g_strdup(gtk_button_get_label(GTK_BUTTON(uiControlHandle(b.base))));
}

static void setText(uiButton *b, const char *text)
{
	gtk_button_set_label(GTK_BUTTON(uiControlHandle(b.base)), text);
}

static void setOnClicked(uiButton *b, void (*f)(uiControl *, void *), void *data)
{
	struct button *b = (struct button *) (b->base.data);

	b->onClicked = f;
	b->onClickedData = data;
}

uiControl *uiNewButton(const char *text)
{
	uiButton *b;
	struct button *bb;
	GtkWidget *widget;

	b = uiNew(uiButton);

	uiUnixNewControl(&(b.base), GTK_TYPE_BUTTON,
		FALSE, FALSE,
		"label", text,
		NULL);

	widget = GTK_WIDGET(uiControlHandle(&(b.base)));
	g_signal_connect(widget, "clicked", G_CALLBACK(onClicked), b);

	bb = uiNew(struct button);
	g_signal_connect(widget, "destroy", G_CALLBACK(onDestroy), bb);
	bb->onClicked = defaultOnClicked;
	b->priv.data = bb;

	b->Text = getText;
	b->SetText = setText;
	b->OnClicked = setOnClicked;

	return b;
}