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

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

#define B(x) ((struct button *) (x))

static void onClicked(GtkButton *b, gpointer data)
{
	(*(B(data)->onClicked))(B(data)->c, B(data)->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);
}

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

	b = uiNew(struct button);

	b->c = uiUnixNewControl(GTK_TYPE_BUTTON,
		FALSE, FALSE, b,
		"label", text,
		NULL);

	widget = GTK_WIDGET(uiControlHandle(b->c));
	g_signal_connect(widget, "destroy", G_CALLBACK(onDestroy), b);
	g_signal_connect(widget, "clicked", G_CALLBACK(onClicked), b);

	b->onClicked = defaultOnClicked;

	return b->c;
}

char *uiButtonText(uiControl *c)
{
	return g_strdup(gtk_button_get_label(GTK_BUTTON(uiControlHandle(c))));
}

void uiButtonSetText(uiControl *c, const char *text)
{
	gtk_button_set_label(GTK_BUTTON(uiControlHandle(c)), text);
}

void uiButtonOnClicked(uiControl *c, void (*f)(uiControl *, void *), void *data)
{
	struct button *b;

	b = (struct button *) uiUnixControlData(c);
	b->onClicked = f;
	b->onClickedData = data;
}