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;
|
2015-04-16 21:14:54 -05:00
|
|
|
GtkWidget *widget;
|
|
|
|
GtkButton *button;
|
|
|
|
GtkToggleButton *toggleButton;
|
|
|
|
GtkCheckButton *checkButton;
|
2015-04-15 22:07:43 -05:00
|
|
|
void (*onToggled)(uiCheckbox *, void *);
|
2015-04-09 12:34:27 -05:00
|
|
|
void *onToggledData;
|
2015-04-09 22:47:03 -05:00
|
|
|
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;
|
2015-04-09 17:17:04 -05:00
|
|
|
|
2015-04-15 22:07:43 -05:00
|
|
|
(*(c->onToggled))(uiCheckbox(c), c->onToggledData);
|
2015-04-09 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2015-04-15 22:07:43 -05:00
|
|
|
static void defaultOnToggled(uiCheckbox *c, void *data)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:14:19 -05:00
|
|
|
static void onDestroy(void *data)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
2015-04-18 16:14:19 -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-16 21:14:54 -05:00
|
|
|
static char *checkboxText(uiCheckbox *cc)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
2015-04-16 21:14:54 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
2015-04-30 21:55:06 -05:00
|
|
|
return uiUnixStrdupText(gtk_button_get_label(c->button));
|
2015-04-09 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 21:14:54 -05:00
|
|
|
static void checkboxSetText(uiCheckbox *cc, const char *text)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
2015-04-16 21:14:54 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
|
|
|
gtk_button_set_label(GTK_BUTTON(c->button), text);
|
2015-04-09 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 21:14:54 -05:00
|
|
|
static void checkboxOnToggled(uiCheckbox *cc, void (*f)(uiCheckbox *, void *), void *data)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
2015-04-15 22:07:43 -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-16 21:14:54 -05:00
|
|
|
static int checkboxChecked(uiCheckbox *cc)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
2015-04-16 21:14:54 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
|
|
|
|
|
|
|
return gtk_toggle_button_get_active(c->toggleButton) != FALSE;
|
2015-04-09 12:34:27 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 21:14:54 -05:00
|
|
|
static void checkboxSetChecked(uiCheckbox *cc, int checked)
|
2015-04-09 12:34:27 -05:00
|
|
|
{
|
2015-04-15 22:07:43 -05:00
|
|
|
struct checkbox *c = (struct checkbox *) cc;
|
2015-04-09 12:34:27 -05:00
|
|
|
gboolean active;
|
|
|
|
|
|
|
|
active = FALSE;
|
|
|
|
if (checked)
|
|
|
|
active = TRUE;
|
2015-04-09 22:47:03 -05:00
|
|
|
// we need to inhibit sending of ::toggled because this WILL send a ::toggled otherwise
|
2015-04-16 21:14:54 -05:00
|
|
|
g_signal_handler_block(c->toggleButton, c->onToggledSignal);
|
|
|
|
gtk_toggle_button_set_active(c->toggleButton, active);
|
|
|
|
g_signal_handler_unblock(c->toggleButton, c->onToggledSignal);
|
2015-04-09 12:34:27 -05:00
|
|
|
}
|
2015-04-15 21:04:13 -05:00
|
|
|
|
2015-04-15 22:07:43 -05:00
|
|
|
uiCheckbox *uiNewCheckbox(const char *text)
|
2015-04-15 21:04:13 -05:00
|
|
|
{
|
|
|
|
struct checkbox *c;
|
|
|
|
|
|
|
|
c = uiNew(struct checkbox);
|
|
|
|
|
2015-05-02 19:51:00 -05:00
|
|
|
uiUnixMakeControl(uiControl(c), GTK_TYPE_CHECK_BUTTON,
|
2015-04-18 16:14:19 -05:00
|
|
|
FALSE, FALSE, onDestroy, c,
|
2015-04-15 21:04:13 -05:00
|
|
|
"label", text,
|
|
|
|
NULL);
|
|
|
|
|
2015-04-29 09:36:31 -05:00
|
|
|
c->widget = GTK_WIDGET(uiControlHandle(uiControl(c)));
|
2015-04-16 21:14:54 -05:00
|
|
|
c->button = GTK_BUTTON(c->widget);
|
|
|
|
c->toggleButton = GTK_TOGGLE_BUTTON(c->widget);
|
|
|
|
c->checkButton = GTK_CHECK_BUTTON(c->widget);
|
|
|
|
|
|
|
|
c->onToggledSignal = g_signal_connect(c->widget, "toggled", G_CALLBACK(onToggled), c);
|
2015-04-15 21:04:13 -05:00
|
|
|
c->onToggled = defaultOnToggled;
|
|
|
|
|
2015-04-16 21:14:54 -05:00
|
|
|
uiCheckbox(c)->Text = checkboxText;
|
|
|
|
uiCheckbox(c)->SetText = checkboxSetText;
|
|
|
|
uiCheckbox(c)->OnToggled = checkboxOnToggled;
|
|
|
|
uiCheckbox(c)->Checked = checkboxChecked;
|
|
|
|
uiCheckbox(c)->SetChecked = checkboxSetChecked;
|
2015-04-15 21:04:13 -05:00
|
|
|
|
|
|
|
return uiCheckbox(c);
|
|
|
|
}
|