#include <stdio.h>
#include <stdlib.h>
#include "range.h"
#include "container.h"
#include <ucx/mempool.h>
#include "../common/context.h"
#include "../common/object.h"
static UIWIDGET ui_scrollbar(UiObject *obj, UiOrientation orientation, UiRange *range, ui_callback f,
void *userdata) {
#ifdef UI_GTK3
GtkWidget *scrollbar = gtk_scrollbar_new(orientation ==
UI_HORIZONTAL ?
GTK_ORIENTATION_HORIZONTAL :
GTK_ORIENTATION_VERTICAL,
NULL);
#else
GtkWidget *scrollbar;
if(orientation ==
UI_HORIZONTAL) {
scrollbar = gtk_hscrollbar_new(
NULL);
}
else {
scrollbar = gtk_hscrollbar_new(
NULL);
}
#endif
if(range) {
range->get = ui_scrollbar_get;
range->set = ui_scrollbar_set;
range->setrange = ui_scrollbar_setrange;
range->setextent = ui_scrollbar_setextent;
range->obj = scrollbar;
}
if(f) {
UiEventData *event = malloc(
sizeof(UiEventData));
event->obj = obj;
event->userdata = userdata;
event->callback = f;
event->value =
0;
g_signal_connect(
G_OBJECT(scrollbar),
"value-changed",
G_CALLBACK(ui_scrollbar_value_changed),
event);
g_signal_connect(
scrollbar,
"destroy",
G_CALLBACK(ui_destroy_userdata),
event);
}
UiContainer *ct = uic_get_current_container(obj);
ct->add(ct, scrollbar,
FALSE);
return scrollbar;
}
UIWIDGET ui_hscrollbar(UiObject *obj, UiRange *range, ui_callback f,
void *userdata) {
return ui_scrollbar(obj,
UI_HORIZONTAL, range, f, userdata);
}
UIWIDGET ui_vscrollbar(UiObject *obj, UiRange *range, ui_callback f,
void *userdata) {
return ui_scrollbar(obj,
UI_VERTICAL, range, f, userdata);
}
gboolean ui_scrollbar_value_changed(GtkRange *range, UiEventData *event) {
UiEvent e;
e.obj = event->obj;
e.window = event->obj->window;
e.document = event->obj->ctx->document;
e.eventdata =
NULL;
e.intval = event->value;
event->callback(&e, event->userdata);
return TRUE;
}
double ui_scrollbar_get(UiRange *range) {
double value = gtk_range_get_value(
GTK_RANGE(range->obj));
range->value = value;
return value;
}
void ui_scrollbar_set(UiRange *range,
double value) {
gtk_range_set_value(
GTK_RANGE(range->obj), value);
range->value = value;
}
void ui_scrollbar_setrange(UiRange *range,
double min,
double max) {
gtk_range_set_range(
GTK_RANGE(range->obj), min, max);
range->min = min;
range->max = max;
}
void ui_scrollbar_setextent(UiRange *range,
double extent) {
GtkAdjustment *a = gtk_range_get_adjustment(
GTK_RANGE(range->obj));
#ifdef UI_GTK2LEGACY
a->page_size = extent;
#else
gtk_adjustment_set_page_size(a, extent);
#endif
#if !(
GTK_MAJOR_VERSION >=
3 &&
GTK_MINOR_VERSION >=
18)
gtk_adjustment_changed(a);
#endif
range->extent = extent;
}