diff options
Diffstat (limited to 'src/ui/scrollwidget.c')
-rw-r--r-- | src/ui/scrollwidget.c | 79 |
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 | |||
4 | iDefineObjectConstruction(ScrollWidget) | ||
5 | |||
6 | struct Impl_ScrollWidget { | ||
7 | iWidget widget; | ||
8 | iRangei range; | ||
9 | int thumb; | ||
10 | int thumbSize; | ||
11 | }; | ||
12 | |||
13 | void 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 | |||
24 | void deinit_ScrollWidget(iScrollWidget *d) { | ||
25 | iUnused(d); | ||
26 | } | ||
27 | |||
28 | static 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 | |||
43 | static void checkVisible_ScrollWidget_(iScrollWidget *d) { | ||
44 | setFlags_Widget(as_Widget(d), hidden_WidgetFlag, height_Rect(thumbRect_ScrollWidget_(d)) == 0); | ||
45 | } | ||
46 | |||
47 | void setRange_ScrollWidget(iScrollWidget *d, iRangei range) { | ||
48 | range.end = iMax(range.start, range.end); | ||
49 | d->range = range; | ||
50 | checkVisible_ScrollWidget_(d); | ||
51 | } | ||
52 | |||
53 | void setThumb_ScrollWidget(iScrollWidget *d, int thumb, int thumbSize) { | ||
54 | d->thumb = thumb; | ||
55 | d->thumbSize = thumbSize; | ||
56 | checkVisible_ScrollWidget_(d); | ||
57 | } | ||
58 | |||
59 | static iBool processEvent_ScrollWidget_(iScrollWidget *d, const SDL_Event *ev) { | ||
60 | iWidget *w = as_Widget(d); | ||
61 | return processEvent_Widget(w, ev); | ||
62 | } | ||
63 | |||
64 | static 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 | |||
76 | iBeginDefineSubclass(ScrollWidget, Widget) | ||
77 | .processEvent = (iAny *) processEvent_ScrollWidget_, | ||
78 | .draw = (iAny *) draw_ScrollWidget_, | ||
79 | iEndDefineSubclass(ScrollWidget) | ||