1 #include <slang.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include "newt.h"
6 #include "newt_pr.h"
7 
8 struct scrollbar {
9     int curr;
10     int cs, csThumb;
11     int arrows;
12 } ;
13 
14 static void sbDraw(newtComponent co);
15 static void sbDestroy(newtComponent co);
16 static void sbDrawThumb(newtComponent co, int isOn);
17 
18 static struct componentOps sbOps = {
19     sbDraw,
20     newtDefaultEventHandler,
21     sbDestroy,
22     newtDefaultPlaceHandler,
23     newtDefaultMappedHandler,
24 } ;
25 
newtScrollbarSet(newtComponent co,int where,int total)26 void newtScrollbarSet(newtComponent co, int where, int total) {
27     struct scrollbar * sb = co->data;
28     int new;
29 
30     if (sb->arrows)
31 	new = (where * (co->height - 3)) / (total ? total : 1) + 1;
32     else
33 	new = (where * (co->height - 1)) / (total ? total : 1);
34     if (new != sb->curr) {
35 	sbDrawThumb(co, 0);
36 	sb->curr = new;
37 	sbDrawThumb(co, 1);
38     }
39 }
40 
newtVerticalScrollbar(int left,int top,int height,int normalColorset,int thumbColorset)41 newtComponent newtVerticalScrollbar(int left, int top, int height,
42 				    int normalColorset, int thumbColorset) {
43     newtComponent co;
44     struct scrollbar * sb;
45 
46     co = malloc(sizeof(*co));
47     sb = malloc(sizeof(*sb));
48     co->data = sb;
49     co->destroyCallback = NULL;
50 
51     if (height >= 2) {
52 	sb->arrows = 1;
53 	sb->curr = 1;
54     } else {
55 	sb->arrows = 0;
56 	sb->curr = 0;
57     }
58     sb->cs = normalColorset;
59     sb->csThumb = thumbColorset;
60 
61     co->ops = &sbOps;
62     co->isMapped = 0;
63     co->left = left;
64     co->top = top;
65     co->height = height;
66     co->width = 1;
67     co->takesFocus = 0;
68 
69     return co;
70 }
71 
newtScrollbarSetColors(newtComponent co,int normal,int thumb)72 void newtScrollbarSetColors(newtComponent co, int normal, int thumb) {
73     struct scrollbar * sb = co->data;
74 
75     sb->cs = normal;
76     sb->csThumb = thumb;
77     sbDraw(co);
78 }
79 
sbDraw(newtComponent co)80 static void sbDraw(newtComponent co) {
81     struct scrollbar * sb = co->data;
82     int i;
83 
84     if (!co->isMapped) return;
85 
86     SLsmg_set_color(sb->cs);
87 
88     SLsmg_set_char_set(1);
89     if (sb->arrows) {
90 	newtGotorc(co->top, co->left);
91 	SLsmg_write_char(SLSMG_UARROW_CHAR);
92 	for (i = 1; i < co->height - 1; i++) {
93 	    newtGotorc(i + co->top, co->left);
94 	    SLsmg_write_char(SLSMG_CKBRD_CHAR);
95 	}
96 	newtGotorc(co->top + co->height - 1, co->left);
97 	SLsmg_write_char(SLSMG_DARROW_CHAR);
98     } else {
99 	for (i = 0; i < co->height; i++) {
100 	    newtGotorc(i + co->top, co->left);
101 	    SLsmg_write_char(SLSMG_CKBRD_CHAR);
102 	}
103     }
104 
105     SLsmg_set_char_set(0);
106 
107     sbDrawThumb(co, 1);
108 }
109 
sbDrawThumb(newtComponent co,int isOn)110 static void sbDrawThumb(newtComponent co, int isOn) {
111     struct scrollbar * sb = co->data;
112     SLtt_Char_Type ch = isOn ? SLSMG_BLOCK_CHAR : SLSMG_CKBRD_CHAR;
113 
114     if (!co->isMapped) return;
115 
116     newtGotorc(sb->curr + co->top, co->left);
117     SLsmg_set_char_set(1);
118 
119     /*if (isOn)
120 	SLsmg_set_color(sb->csThumb);
121     else*/
122 	SLsmg_set_color(sb->cs);
123 
124     SLsmg_write_char(ch);
125     SLsmg_set_char_set(0);
126 }
127 
sbDestroy(newtComponent co)128 static void sbDestroy(newtComponent co) {
129     struct scrollbar * sb = co->data;
130 
131     free(sb);
132     free(co);
133 }
134