2015-04-07 12:58:50 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#include "uipriv_unix.h"
|
|
|
|
|
|
|
|
struct button {
|
2015-04-15 20:57:59 -05:00
|
|
|
uiButton b;
|
2015-04-07 12:58:50 -05:00
|
|
|
void (*onClicked)(uiControl *, void *);
|
|
|
|
void *onClickedData;
|
|
|
|
};
|
|
|
|
|
2015-04-09 17:17:04 -05:00
|
|
|
static void onClicked(GtkButton *button, gpointer data)
|
2015-04-07 12:58:50 -05:00
|
|
|
{
|
2015-04-15 20:57:59 -05:00
|
|
|
struct button *b = (struct button *) data;
|
2015-04-09 17:17:04 -05:00
|
|
|
|
2015-04-15 20:57:59 -05:00
|
|
|
(*(b->onClicked))(uiButton(b), b->onClickedData);
|
2015-04-07 12:58:50 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 20:57:59 -05:00
|
|
|
static void defaultOnClicked(uiButton *b, void *data)
|
2015-04-07 12:58:50 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:16:22 -05:00
|
|
|
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)))
|
|
|
|
|
2015-04-15 18:46:24 -05:00
|
|
|
static char *getText(uiButton *b)
|
2015-04-07 12:58:50 -05:00
|
|
|
{
|
2015-04-15 20:57:59 -05:00
|
|
|
return g_strdup(gtk_button_get_label(BUTTON(b)));
|
2015-04-07 12:58:50 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 18:46:24 -05:00
|
|
|
static void setText(uiButton *b, const char *text)
|
2015-04-09 01:56:51 -05:00
|
|
|
{
|
2015-04-15 20:57:59 -05:00
|
|
|
gtk_button_set_label(BUTTON(b), text);
|
2015-04-09 01:56:51 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 18:46:24 -05:00
|
|
|
static void setOnClicked(uiButton *b, void (*f)(uiControl *, void *), void *data)
|
2015-04-09 01:56:51 -05:00
|
|
|
{
|
2015-04-15 20:57:59 -05:00
|
|
|
struct button *b = (struct button *) b;
|
2015-04-15 18:46:24 -05:00
|
|
|
|
|
|
|
b->onClicked = f;
|
|
|
|
b->onClickedData = data;
|
2015-04-09 01:56:51 -05:00
|
|
|
}
|
2015-04-07 12:58:50 -05:00
|
|
|
|
2015-04-15 18:46:24 -05:00
|
|
|
uiControl *uiNewButton(const char *text)
|
2015-04-07 12:58:50 -05:00
|
|
|
{
|
2015-04-15 20:57:59 -05:00
|
|
|
struct button *b;
|
2015-04-15 18:46:24 -05:00
|
|
|
GtkWidget *widget;
|
2015-04-07 12:58:50 -05:00
|
|
|
|
2015-04-15 20:57:59 -05:00
|
|
|
b = uiNew(b);
|
2015-04-15 18:46:24 -05:00
|
|
|
|
2015-04-15 20:57:59 -05:00
|
|
|
uiUnixNewControl(uiControl(b), GTK_TYPE_BUTTON,
|
2015-04-15 18:46:24 -05:00
|
|
|
FALSE, FALSE,
|
|
|
|
"label", text,
|
|
|
|
NULL);
|
|
|
|
|
2015-04-15 20:57:59 -05:00
|
|
|
widget = GTK_WIDGET(BUTTON(b));
|
2015-04-15 18:46:24 -05:00
|
|
|
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 18:46:24 -05:00
|
|
|
|
2015-04-15 20:57:59 -05:00
|
|
|
uiButton(b)->Text = getText;
|
|
|
|
uiButton(b)->SetText = setText;
|
|
|
|
uiButton(b)->OnClicked = setOnClicked;
|
2015-04-15 18:46:24 -05:00
|
|
|
|
|
|
|
return b;
|
2015-04-07 12:58:50 -05:00
|
|
|
}
|