libui/unix/checkbox.c

96 lines
2.2 KiB
C
Raw Normal View History

2015-04-09 12:34:27 -05:00
// 7 april 2015
#include "uipriv_unix.h"
struct checkbox {
2015-04-15 21:04:13 -05:00
uiCheckbox c;
void (*onToggled)(uiCheckbox *, void *);
2015-04-09 12:34:27 -05:00
void *onToggledData;
gulong onToggledSignal;
2015-04-09 12:34:27 -05:00
};
static void onToggled(GtkToggleButton *b, gpointer data)
{
2015-04-15 21:04:13 -05:00
struct checkbox *c = (struct checkbox *) data;
(*(c->onToggled))(uiCheckbox(c), c->onToggledData);
2015-04-09 12:34:27 -05:00
}
static void defaultOnToggled(uiCheckbox *c, void *data)
2015-04-09 12:34:27 -05:00
{
// do nothing
}
static void onDestroy(GtkWidget *widget, gpointer data)
{
2015-04-15 21:04:13 -05:00
struct checkbox *c = (struct checkbox *) data;
2015-04-09 12:34:27 -05:00
2015-04-15 21:04:13 -05:00
uiFree(c);
2015-04-09 12:34:27 -05:00
}
2015-04-15 21:04:13 -05:00
#define CHECKBOX(c) GTK_CHECK_BUTTON(uiControlHandle(uiControl(c)))
2015-04-09 12:34:27 -05:00
2015-04-15 21:04:13 -05:00
static char *getText(uiCheckbox *c)
2015-04-09 12:34:27 -05:00
{
2015-04-15 22:14:36 -05:00
return g_strdup(gtk_button_get_label(GTK_BUTTON(CHECKBOX(c))));
2015-04-09 12:34:27 -05:00
}
2015-04-15 21:04:13 -05:00
static void setText(uiCheckbox *c, const char *text)
2015-04-09 12:34:27 -05:00
{
2015-04-15 22:14:36 -05:00
gtk_button_set_label(GTK_BUTTON(CHECKBOX(c)), text);
2015-04-09 12:34:27 -05:00
}
static void setOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
2015-04-09 12:34:27 -05:00
{
struct checkbox *c = (struct checkbox *) cc;
2015-04-09 12:34:27 -05:00
2015-04-15 21:04:13 -05:00
c->onToggled = f;
c->onToggledData = data;
2015-04-09 12:34:27 -05:00
}
2015-04-15 21:04:13 -05:00
static int getChecked(uiCheckbox *c)
2015-04-09 12:34:27 -05:00
{
2015-04-15 21:04:13 -05:00
return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(CHECKBOX(c))) != FALSE;
2015-04-09 12:34:27 -05:00
}
static void setChecked(uiCheckbox *cc, int checked)
2015-04-09 12:34:27 -05:00
{
struct checkbox *c = (struct checkbox *) cc;
GtkToggleButton *button;
2015-04-09 12:34:27 -05:00
gboolean active;
active = FALSE;
if (checked)
active = TRUE;
// we need to inhibit sending of ::toggled because this WILL send a ::toggled otherwise
2015-04-15 21:04:13 -05:00
button = GTK_TOGGLE_BUTTON(CHECKBOX(c));
g_signal_handler_block(button, c->onToggledSignal);
gtk_toggle_button_set_active(button, active);
g_signal_handler_unblock(button, c->onToggledSignal);
2015-04-09 12:34:27 -05:00
}
2015-04-15 21:04:13 -05:00
uiCheckbox *uiNewCheckbox(const char *text)
2015-04-15 21:04:13 -05:00
{
struct checkbox *c;
GtkWidget *widget;
c = uiNew(struct checkbox);
uiUnixNewControl(uiControl(c), GTK_TYPE_CHECK_BUTTON,
FALSE, FALSE,
"label", text,
NULL);
widget = GTK_WIDGET(CHECKBOX(c));
g_signal_connect(widget, "destroy", G_CALLBACK(onDestroy), c);
c->onToggledSignal = g_signal_connect(widget, "toggled", G_CALLBACK(onToggled), c);
c->onToggled = defaultOnToggled;
uiCheckbox(c)->Text = getText;
uiCheckbox(c)->SetText = setText;
uiCheckbox(c)->OnToggled = setOnToggled;
uiCheckbox(c)->Checked = getChecked;
uiCheckbox(c)->SetChecked = setChecked;
return uiCheckbox(c);
}