]> git.ipfire.org Git - thirdparty/newt.git/blame - scrollbar.c
1) got scrollbars working w/ gridded text widgets
[thirdparty/newt.git] / scrollbar.c
CommitLineData
139f06bc 1#include <slang.h>
ab3827f8 2#include <stdlib.h>
3#include <string.h>
4
5#include "newt.h"
6#include "newt_pr.h"
7
8struct scrollbar {
9 int curr;
10 int cs, csThumb;
11} ;
12
13static void sbDraw(newtComponent co);
14static void sbDestroy(newtComponent co);
15static void sbDrawThumb(newtComponent co, int isOn);
16
17static struct componentOps sbOps = {
18 sbDraw,
19 newtDefaultEventHandler,
20 sbDestroy,
8f52cd47 21 newtDefaultPlaceHandler,
22 newtDefaultMappedHandler,
ab3827f8 23} ;
24
25void newtScrollbarSet(newtComponent co, int where, int total) {
26 struct scrollbar * sb = co->data;
27 int new;
28
e528434e 29 new = (where * (co->height - 1)) / (total ? total : 1);
ab3827f8 30 if (new != sb->curr) {
31 sbDrawThumb(co, 0);
32 sb->curr = new;
33 sbDrawThumb(co, 1);
34 }
35}
36
37newtComponent newtVerticalScrollbar(int left, int top, int height,
38 int normalColorset, int thumbColorset) {
39 newtComponent co;
40 struct scrollbar * sb;
41
42 co = malloc(sizeof(*co));
43 sb = malloc(sizeof(*sb));
44 co->data = sb;
45
46 sb->curr = 0;
47 sb->cs = normalColorset;
48 sb->csThumb = thumbColorset;
49
50 co->ops = &sbOps;
11e3bf5e 51 co->isMapped = 0;
ab3827f8 52 co->left = left;
53 co->top = top;
54 co->height = height;
55 co->width = 1;
56 co->takesFocus = 0;
57
58 return co;
59}
60
61static void sbDraw(newtComponent co) {
62 struct scrollbar * sb = co->data;
63 int i;
64
11e3bf5e 65 if (!co->isMapped) return;
66
ab3827f8 67 SLsmg_set_color(sb->cs);
68
69 SLsmg_set_char_set(1);
70 for (i = 0; i < co->height; i++) {
71 newtGotorc(i + co->top, co->left);
28dfd928 72 SLsmg_write_char('\x61');
ab3827f8 73 }
74
75 SLsmg_set_char_set(0);
76
77 sbDrawThumb(co, 1);
78}
79
80static void sbDrawThumb(newtComponent co, int isOn) {
81 struct scrollbar * sb = co->data;
28dfd928 82 char ch = isOn ? '#' : '\x61';
ab3827f8 83
11e3bf5e 84 if (!co->isMapped) return;
85
ab3827f8 86 newtGotorc(sb->curr + co->top, co->left);
87 SLsmg_set_char_set(1);
88
28dfd928 89 /*if (isOn)
ab3827f8 90 SLsmg_set_color(sb->csThumb);
28dfd928 91 else*/
ab3827f8 92 SLsmg_set_color(sb->cs);
93
28dfd928 94 SLsmg_write_char(ch);
ab3827f8 95 SLsmg_set_char_set(0);
96}
97
98static void sbDestroy(newtComponent co) {
99 struct scrollbar * sb = co->data;
100
101 free(sb);
102 free(co);
103}