summaryrefslogtreecommitdiff
path: root/src/ui/scrollwidget.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-07-23 14:25:05 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-07-23 14:25:05 +0300
commita3610ec020882015ae15b3b2808a443f81f651e6 (patch)
tree860412f333cfd3c0c29db0642d6fc547f6d1812d /src/ui/scrollwidget.c
parentce5779323b1fe6aed04f6d9ae3fd6ca77d21a221 (diff)
Added ScrollWidget for scroll bars
Diffstat (limited to 'src/ui/scrollwidget.c')
-rw-r--r--src/ui/scrollwidget.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/ui/scrollwidget.c b/src/ui/scrollwidget.c
new file mode 100644
index 00000000..cbfd2e70
--- /dev/null
+++ b/src/ui/scrollwidget.c
@@ -0,0 +1,79 @@
1#include "scrollwidget.h"
2#include "paint.h"
3
4iDefineObjectConstruction(ScrollWidget)
5
6struct Impl_ScrollWidget {
7 iWidget widget;
8 iRangei range;
9 int thumb;
10 int thumbSize;
11};
12
13void init_ScrollWidget(iScrollWidget *d) {
14 iWidget *w = as_Widget(d);
15 init_Widget(w);
16 setId_Widget(w, "scroll");
17 setFlags_Widget(w,
18 fixedWidth_WidgetFlag | resizeToParentHeight_WidgetFlag |
19 moveToParentRightEdge_WidgetFlag,
20 iTrue);
21 w->rect.size.x = gap_UI * 3;
22}
23
24void deinit_ScrollWidget(iScrollWidget *d) {
25 iUnused(d);
26}
27
28static iRect thumbRect_ScrollWidget_(const iScrollWidget *d) {
29 const iRect bounds = bounds_Widget(constAs_Widget(d));
30 iRect rect = init_Rect(bounds.pos.x, bounds.pos.y, bounds.size.x, 0);
31 const int total = size_Range(&d->range);
32// printf("total: %d thumb: %d thsize: %d\n", total, d->thumb, d->thumbSize); fflush(stdout);
33 if (total > 0) {
34 const int tsize = iMax(gap_UI * 6, d->thumbSize);
35 const int tpos =
36 iClamp((float) d->thumb / (float) total, 0, 1) * (height_Rect(bounds) - tsize);
37 rect.pos.y = tpos;
38 rect.size.y = tsize;
39 }
40 return rect;
41}
42
43static void checkVisible_ScrollWidget_(iScrollWidget *d) {
44 setFlags_Widget(as_Widget(d), hidden_WidgetFlag, height_Rect(thumbRect_ScrollWidget_(d)) == 0);
45}
46
47void setRange_ScrollWidget(iScrollWidget *d, iRangei range) {
48 range.end = iMax(range.start, range.end);
49 d->range = range;
50 checkVisible_ScrollWidget_(d);
51}
52
53void setThumb_ScrollWidget(iScrollWidget *d, int thumb, int thumbSize) {
54 d->thumb = thumb;
55 d->thumbSize = thumbSize;
56 checkVisible_ScrollWidget_(d);
57}
58
59static iBool processEvent_ScrollWidget_(iScrollWidget *d, const SDL_Event *ev) {
60 iWidget *w = as_Widget(d);
61 return processEvent_Widget(w, ev);
62}
63
64static void draw_ScrollWidget_(const iScrollWidget *d) {
65 const iWidget *w = constAs_Widget(d);
66 const iRect bounds = bounds_Widget(w);
67 if (bounds.size.x > 0) {
68 iPaint p;
69 init_Paint(&p);
70 drawRect_Paint(&p, bounds, black_ColorId);
71 iRect tr = thumbRect_ScrollWidget_(d);
72 fillRect_Paint(&p, thumbRect_ScrollWidget_(d), gray50_ColorId);
73 }
74}
75
76iBeginDefineSubclass(ScrollWidget, Widget)
77 .processEvent = (iAny *) processEvent_ScrollWidget_,
78 .draw = (iAny *) draw_ScrollWidget_,
79iEndDefineSubclass(ScrollWidget)