libui/unix/button.c

77 lines
1.5 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;
GtkWidget *widget;
GtkButton *button;
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-16 15:38:33 -05:00
static char *buttonText(uiButton *bb)
{
struct button *b = (struct button *) bb;
return g_strdup(gtk_button_get_label(b->button));
}
2015-04-16 15:38:33 -05:00
static void buttonSetText(uiButton *bb, const char *text)
{
struct button *b = (struct button *) bb;
gtk_button_set_label(b->button, text);
}
2015-04-16 15:38:33 -05:00
static void buttonOnClicked(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;
b = uiNew(struct button);
2015-04-15 20:57:59 -05:00
uiUnixNewControl(uiControl(b), GTK_TYPE_BUTTON,
FALSE, FALSE,
"label", text,
NULL);
b->widget = WIDGET(b);
b->button = GTK_BUTTON(b->widget);
g_signal_connect(b->widget, "clicked", G_CALLBACK(onClicked), b);
g_signal_connect(b->widget, "destroy", G_CALLBACK(onDestroy), b);
2015-04-15 20:57:59 -05:00
b->onClicked = defaultOnClicked;
2015-04-16 15:38:33 -05:00
uiButton(b)->Text = buttonText;
uiButton(b)->SetText = buttonSetText;
uiButton(b)->OnClicked = buttonOnClicked;
return uiButton(b);
}