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