libui/redo/unix/checkbox.c

86 lines
2.0 KiB
C
Raw Normal View History

2015-06-11 18:07:06 -05:00
// 10 june 2015
#include "uipriv_unix.h"
2015-08-27 14:30:55 -05:00
struct uiCheckbox {
uiUnixControl c;
2015-06-11 18:07:06 -05:00
GtkWidget *widget;
2015-06-27 18:46:11 -05:00
GtkButton *button;
GtkToggleButton *toggleButton;
GtkCheckButton *checkButton;
2015-06-11 18:07:06 -05:00
void (*onToggled)(uiCheckbox *, void *);
void *onToggledData;
2015-06-30 10:20:14 -05:00
gulong onToggledSignal;
2015-06-11 18:07:06 -05:00
};
2015-08-27 14:30:55 -05:00
uiUnixDefineControl(
uiCheckbox, // type name
uiCheckboxType // type function
)
2015-06-11 18:07:06 -05:00
2015-06-30 10:20:14 -05:00
static void onToggled(GtkToggleButton *b, gpointer data)
{
2015-08-27 22:00:34 -05:00
uiCheckbox *c = uiCheckbox(data);
2015-06-30 10:20:14 -05:00
2015-08-27 22:00:34 -05:00
(*(c->onToggled))(c, c->onToggledData);
2015-06-30 10:20:14 -05:00
}
2015-06-11 18:07:06 -05:00
static void defaultOnToggled(uiCheckbox *c, void *data)
{
// do nothing
}
2015-08-27 14:30:55 -05:00
char *uiCheckboxText(uiCheckbox *c)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:20:14 -05:00
return uiUnixStrdupText(gtk_button_get_label(c->button));
2015-06-11 18:07:06 -05:00
}
2015-08-27 14:30:55 -05:00
void uiCheckboxSetText(uiCheckbox *c, const char *text)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:20:14 -05:00
gtk_button_set_label(GTK_BUTTON(c->button), text);
2015-06-11 18:07:06 -05:00
// changing the text might necessitate a change in the checkbox's size
uiControlQueueResize(uiControl(c));
}
2015-08-27 14:30:55 -05:00
void uiCheckboxOnToggled(uiCheckbox *c, void (*f)(uiCheckbox *, void *), void *data)
2015-06-11 18:07:06 -05:00
{
c->onToggled = f;
c->onToggledData = data;
}
2015-08-27 14:30:55 -05:00
int uiCheckboxChecked(uiCheckbox *c)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:20:14 -05:00
return gtk_toggle_button_get_active(c->toggleButton) != FALSE;
2015-06-11 18:07:06 -05:00
}
2015-08-27 14:30:55 -05:00
void uiCheckboxSetChecked(uiCheckbox *c, int checked)
2015-06-11 18:07:06 -05:00
{
2015-06-30 10:20:14 -05:00
gboolean active;
active = FALSE;
if (checked)
active = TRUE;
// we need to inhibit sending of ::toggled because this WILL send a ::toggled otherwise
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-06-11 18:07:06 -05:00
}
uiCheckbox *uiNewCheckbox(const char *text)
{
2015-08-27 14:30:55 -05:00
uiCheckbox *c;
2015-06-11 18:07:06 -05:00
c = (uiCheckbox *) uiNewControl(uiCheckboxType());
2015-06-11 18:07:06 -05:00
2015-06-27 18:46:11 -05:00
c->widget = gtk_check_button_new_with_label(text);
c->button = GTK_BUTTON(c->widget);
c->toggleButton = GTK_TOGGLE_BUTTON(c->widget);
c->checkButton = GTK_CHECK_BUTTON(c->widget);
2015-06-11 18:07:06 -05:00
2015-06-30 10:20:14 -05:00
c->onToggledSignal = g_signal_connect(c->widget, "toggled", G_CALLBACK(onToggled), c);
2015-08-27 14:30:55 -05:00
uiCheckboxOnToggled(c, defaultOnToggled, NULL);
2015-06-11 18:07:06 -05:00
2015-08-27 14:30:55 -05:00
uiUnixFinishNewControl(c, uiCheckbox);
2015-06-11 18:07:06 -05:00
2015-08-27 14:30:55 -05:00
return c;
2015-06-11 18:07:06 -05:00
}