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 scale {
9     long long fullValue;
10     int charsSet;
11     unsigned int percentage;
12     int csEmpty;
13     int csFull;
14 };
15 
16 static void scaleDraw(newtComponent co);
17 
18 static struct componentOps scaleOps = {
19     scaleDraw,
20     newtDefaultEventHandler,
21     NULL,
22     newtDefaultPlaceHandler,
23     newtDefaultMappedHandler,
24 } ;
25 
newtScale(int left,int top,int width,long long fullValue)26 newtComponent newtScale(int left, int top, int width, long long fullValue) {
27     newtComponent co;
28     struct scale * sc;
29 
30     co = malloc(sizeof(*co));
31     sc = malloc(sizeof(struct scale));
32     co->data = sc;
33     co->destroyCallback = NULL;
34 
35     co->ops = &scaleOps;
36 
37     co->height = 1;
38     co->width = width;
39     co->top = top;
40     co->left = left;
41     co->takesFocus = 0;
42     co->isMapped = 0;
43 
44     sc->fullValue = fullValue;
45     sc->charsSet = 0;
46     sc->percentage = 0;
47     sc->csEmpty = NEWT_COLORSET_EMPTYSCALE;
48     sc->csFull = NEWT_COLORSET_FULLSCALE;
49 
50     return co;
51 }
52 
newtScaleSet(newtComponent co,unsigned long long amount)53 void newtScaleSet(newtComponent co, unsigned long long amount) {
54     struct scale * sc = co->data;
55     int newPercentage;
56 
57     if (amount >= sc->fullValue) {
58 	newPercentage = 100;
59 	sc->charsSet = co->width;
60     } else if (sc->fullValue >= -1ULL / (100 > co->width ? 100 : co->width)) {
61 	/* avoid overflow on large numbers */
62 	sc->charsSet = amount / (sc->fullValue / co->width);
63 	newPercentage = amount / (sc->fullValue / 100);
64     } else {
65 	sc->charsSet = (amount * co->width) / sc->fullValue;
66 	newPercentage = (amount * 100) / sc->fullValue;
67     }
68 
69     if (newPercentage != sc->percentage) {
70 	sc->percentage = newPercentage;
71 	scaleDraw(co);
72     }
73 }
74 
newtScaleSetColors(newtComponent co,int empty,int full)75 void newtScaleSetColors(newtComponent co, int empty, int full) {
76     struct scale * sc = co->data;
77 
78     sc->csEmpty = empty;
79     sc->csFull = full;
80     scaleDraw(co);
81 }
82 
scaleDraw(newtComponent co)83 static void scaleDraw(newtComponent co) {
84     struct scale * sc = co->data;
85     int i;
86     int xlabel = (co->width-4) /2;
87     char percent[10];
88 
89     if (!co->isMapped) return;
90 
91     newtGotorc(co->top, co->left);
92 
93     sprintf(percent, "%3d%%", sc->percentage);
94 
95     SLsmg_set_color(sc->csFull);
96 
97     for (i = 0; i < co->width; i++) {
98         if (i == sc->charsSet)
99             SLsmg_set_color(sc->csEmpty);
100         if (i >= xlabel && i < xlabel+4)
101             SLsmg_write_char(percent[i-xlabel]);
102         else
103             SLsmg_write_char(' ');
104     }
105     /* put cursor at beginning of text for better accessibility */
106     newtGotorc(co->top, co->left + xlabel);
107 }
108