libui/unix/button.c

72 lines
1.4 KiB
C
Raw Normal View History

// 7 april 2015
#include "uipriv_unix.h"
struct button {
2015-04-15 20:57:59 -05:00
uiButton b;
2015-04-15 22:14:36 -05:00
void (*onClicked)(uiButton *, void *);
void *onClickedData;
};
static void onClicked(GtkButton *button, gpointer data)
{
2015-04-15 20:57:59 -05:00
struct button *b = (struct button *) data;
2015-04-15 20:57:59 -05:00
(*(b->onClicked))(uiButton(b), b->onClickedData);
}
2015-04-15 20:57:59 -05:00
static void defaultOnClicked(uiButton *b, void *data)
{
// do nothing
}
static void onDestroy(GtkWidget *widget, gpointer data)
{
struct button *b = (struct button *) data;
uiFree(b);
}
2015-04-15 20:57:59 -05:00
#define BUTTON(b) GTK_BUTTON(uiControlHandle(uiControl(b)))
static char *getText(uiButton *b)
{
2015-04-15 20:57:59 -05:00
return g_strdup(gtk_button_get_label(BUTTON(b)));
}
static void setText(uiButton *b, const char *text)
{
2015-04-15 20:57:59 -05:00
gtk_button_set_label(BUTTON(b), text);
}
2015-04-15 22:14:36 -05:00
static void setOnClicked(uiButton *bb, void (*f)(uiButton *, void *), void *data)
{
struct button *b = (struct button *) bb;
b->onClicked = f;
b->onClickedData = data;
}
uiButton *uiNewButton(const char *text)
{
2015-04-15 20:57:59 -05:00
struct button *b;
GtkWidget *widget;
b = uiNew(struct button);
2015-04-15 20:57:59 -05:00
uiUnixNewControl(uiControl(b), GTK_TYPE_BUTTON,
FALSE, FALSE,
"label", text,
NULL);
2015-04-15 20:57:59 -05:00
widget = GTK_WIDGET(BUTTON(b));
g_signal_connect(widget, "clicked", G_CALLBACK(onClicked), b);
2015-04-15 20:57:59 -05:00
g_signal_connect(widget, "destroy", G_CALLBACK(onDestroy), b);
b->onClicked = defaultOnClicked;
2015-04-15 20:57:59 -05:00
uiButton(b)->Text = getText;
uiButton(b)->SetText = setText;
uiButton(b)->OnClicked = setOnClicked;
return uiButton(b);
}