libui/unix/spinbox.c

73 lines
1.6 KiB
C
Raw Normal View History

2015-06-11 18:07:06 -05:00
// 11 june 2015
#include "uipriv_unix.h"
2015-08-27 22:00:34 -05:00
struct uiSpinbox {
uiUnixControl c;
2015-06-27 18:46:11 -05:00
GtkWidget *widget;
GtkEntry *entry;
GtkSpinButton *spinButton;
void (*onChanged)(uiSpinbox *, void *);
void *onChangedData;
gulong onChangedSignal;
2015-06-11 18:07:06 -05:00
};
uiUnixControlAllDefaults(uiSpinbox)
2015-06-11 18:07:06 -05:00
static void onChanged(GtkSpinButton *sb, gpointer data)
{
2015-08-27 22:00:34 -05:00
uiSpinbox *s = uiSpinbox(data);
2015-08-27 22:00:34 -05:00
(*(s->onChanged))(s, s->onChangedData);
2015-06-11 18:07:06 -05:00
}
static void defaultOnChanged(uiSpinbox *s, void *data)
{
// do nothing
}
2015-08-27 22:00:34 -05:00
intmax_t uiSpinboxValue(uiSpinbox *s)
2015-06-11 18:07:06 -05:00
{
return (intmax_t) gtk_spin_button_get_value(s->spinButton);
2015-06-11 18:07:06 -05:00
}
2015-08-27 22:00:34 -05:00
void uiSpinboxSetValue(uiSpinbox *s, intmax_t value)
2015-06-11 18:07:06 -05:00
{
// we need to inhibit sending of ::value-changed because this WILL send a ::value-changed otherwise
g_signal_handler_block(s->spinButton, s->onChangedSignal);
// this clamps for us
gtk_spin_button_set_value(s->spinButton, (gdouble) value);
g_signal_handler_unblock(s->spinButton, s->onChangedSignal);
2015-06-11 18:07:06 -05:00
}
2015-08-27 22:00:34 -05:00
void uiSpinboxOnChanged(uiSpinbox *s, void (*f)(uiSpinbox *, void *), void *data)
2015-06-11 18:07:06 -05:00
{
s->onChanged = f;
s->onChangedData = data;
}
uiSpinbox *uiNewSpinbox(intmax_t min, intmax_t max)
{
2015-08-27 22:00:34 -05:00
uiSpinbox *s;
intmax_t temp;
2015-06-11 18:07:06 -05:00
if (min >= max) {
temp = min;
min = max;
max = temp;
}
2015-06-11 18:07:06 -05:00
uiUnixNewControl(uiSpinbox, s);
2015-06-11 18:07:06 -05:00
2015-06-27 18:46:11 -05:00
s->widget = gtk_spin_button_new_with_range(min, max, 1);
s->entry = GTK_ENTRY(s->widget);
s->spinButton = GTK_SPIN_BUTTON(s->widget);
2016-05-22 23:41:56 -05:00
// ensure integers, just to be safe
2015-06-27 18:46:11 -05:00
gtk_spin_button_set_digits(s->spinButton, 0);
2015-06-11 18:07:06 -05:00
s->onChangedSignal = g_signal_connect(s->spinButton, "value-changed", G_CALLBACK(onChanged), s);
2015-08-27 22:00:34 -05:00
uiSpinboxOnChanged(s, defaultOnChanged, NULL);
2015-06-11 18:07:06 -05:00
2015-08-27 22:00:34 -05:00
return s;
2015-06-11 18:07:06 -05:00
}