andlabs-ui/new/button_unix.c

68 lines
1.3 KiB
C

// 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;
}