libui/redo/unix/slider.c

81 lines
1.7 KiB
C
Raw Normal View History

2015-06-11 18:07:06 -05:00
// 11 june 2015
#include "uipriv_unix.h"
struct slider {
uiSlider s;
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;
};
uiDefineControlType(uiSlider, uiTypeSlider, struct slider)
2015-07-01 10:26:14 -05:00
static void onChanged(GtkRange *range, gpointer data)
{
struct slider *s = (struct slider *) data;
(*(s->onChanged))(uiSlider(s), s->onChangedData);
}
2015-06-11 18:07:06 -05:00
static uintptr_t sliderHandle(uiControl *c)
{
struct slider *s = (struct slider *) c;
return (uintptr_t) (s->widget);
}
static void defaultOnChanged(uiSlider *s, void *data)
{
// do nothing
}
static intmax_t sliderValue(uiSlider *ss)
{
struct slider *s = (struct slider *) ss;
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
}
static void sliderSetValue(uiSlider *ss, intmax_t value)
{
struct slider *s = (struct slider *) ss;
2015-07-01 10:26:14 -05:00
gtk_range_set_value(s->range, value);
2015-06-11 18:07:06 -05:00
}
static void sliderOnChanged(uiSlider *ss, void (*f)(uiSlider *, void *), void *data)
{
struct slider *s = (struct slider *) ss;
s->onChanged = f;
s->onChangedData = data;
}
uiSlider *uiNewSlider(intmax_t min, intmax_t max)
{
struct slider *s;
s = (struct slider *) uiNewControl(uiTypeSlider());
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);
uiUnixMakeSingleWidgetControl(uiControl(s), s->widget);
// TODO needed?
gtk_scale_set_digits(s->scale, 0);
2015-06-11 18:07:06 -05:00
2015-07-01 10:26:14 -05:00
g_signal_connect(s->scale, "value-changed", G_CALLBACK(onChanged), s);
2015-06-11 18:07:06 -05:00
s->onChanged = defaultOnChanged;
uiControl(s)->Handle = sliderHandle;
uiSlider(s)->Value = sliderValue;
uiSlider(s)->SetValue = sliderSetValue;
uiSlider(s)->OnChanged = sliderOnChanged;
return uiSlider(s);
}