libui/unix/slider.c

69 lines
1.5 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 uiSlider {
uiUnixControl c;
2015-06-11 18:07:06 -05:00
GtkWidget *widget;
2015-06-27 18:46:11 -05:00
GtkRange *range;
GtkScale *scale;
2015-06-11 18:07:06 -05:00
void (*onChanged)(uiSlider *, void *);
void *onChangedData;
gulong onChangedSignal;
2015-06-11 18:07:06 -05:00
};
2015-08-27 22:00:34 -05:00
uiUnixDefineControl(
uiSlider // type name
2015-08-27 22:00:34 -05:00
)
2015-06-11 18:07:06 -05:00
2015-07-01 10:26:14 -05:00
static void onChanged(GtkRange *range, gpointer data)
{
2015-08-27 22:00:34 -05:00
uiSlider *s = uiSlider(data);
2015-07-01 10:26:14 -05:00
2015-08-27 22:00:34 -05:00
(*(s->onChanged))(s, s->onChangedData);
2015-06-11 18:07:06 -05:00
}
static void defaultOnChanged(uiSlider *s, void *data)
{
// do nothing
}
2015-08-27 22:00:34 -05:00
intmax_t uiSliderValue(uiSlider *s)
2015-06-11 18:07:06 -05:00
{
2015-07-01 10:26:14 -05:00
return (intmax_t) gtk_range_get_value(s->range);
2015-06-11 18:07:06 -05:00
}
2015-08-27 22:00:34 -05:00
void uiSliderSetValue(uiSlider *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->range, s->onChangedSignal);
2015-07-01 10:26:14 -05:00
gtk_range_set_value(s->range, value);
g_signal_handler_unblock(s->range, s->onChangedSignal);
2015-06-11 18:07:06 -05:00
}
2015-08-27 22:00:34 -05:00
void uiSliderOnChanged(uiSlider *s, void (*f)(uiSlider *, void *), void *data)
2015-06-11 18:07:06 -05:00
{
s->onChanged = f;
s->onChangedData = data;
}
uiSlider *uiNewSlider(intmax_t min, intmax_t max)
{
2015-08-27 22:00:34 -05:00
uiSlider *s;
2015-06-11 18:07:06 -05:00
s = (uiSlider *) uiNewControl(uiSlider);
2015-06-11 18:07:06 -05:00
2015-06-27 18:46:11 -05:00
s->widget = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, min, max, 1);
s->range = GTK_RANGE(s->widget);
s->scale = GTK_SCALE(s->widget);
// TODO needed?
gtk_scale_set_digits(s->scale, 0);
2015-06-11 18:07:06 -05:00
s->onChangedSignal = g_signal_connect(s->scale, "value-changed", G_CALLBACK(onChanged), s);
2015-08-27 22:00:34 -05:00
uiSliderOnChanged(s, defaultOnChanged, NULL);
2015-06-11 18:07:06 -05:00
2015-08-27 22:00:34 -05:00
uiUnixFinishNewControl(s, uiSlider);
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
}