]> git.ipfire.org Git - thirdparty/newt.git/blob - scale.c
revert a bit
[thirdparty/newt.git] / scale.c
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 };
13
14 static void scaleDraw(newtComponent co);
15
16 static struct componentOps scaleOps = {
17 scaleDraw,
18 newtDefaultEventHandler,
19 NULL,
20 newtDefaultPlaceHandler,
21 newtDefaultMappedHandler,
22 } ;
23
24 newtComponent newtScale(int left, int top, int width, long long fullValue) {
25 newtComponent co;
26 struct scale * sc;
27
28 co = malloc(sizeof(*co));
29 sc = malloc(sizeof(struct scale));
30 co->data = sc;
31
32 co->ops = &scaleOps;
33
34 co->height = 1;
35 co->width = width;
36 co->top = top;
37 co->left = left;
38 co->takesFocus = 0;
39
40 sc->fullValue = fullValue;
41 sc->charsSet = 0;
42 sc->percentage = 0;
43
44 return co;
45 }
46
47 void newtScaleSet(newtComponent co, unsigned long long amount) {
48 struct scale * sc = co->data;
49 int newPercentage;
50
51 sc->charsSet = (amount * co->width) / sc->fullValue;
52 newPercentage = (amount * 100) / sc->fullValue;
53
54 if (newPercentage > 100)
55 newPercentage = 100;
56
57 if (newPercentage != sc->percentage) {
58 sc->percentage = newPercentage;
59 scaleDraw(co);
60 }
61 }
62
63 static void scaleDraw(newtComponent co) {
64 struct scale * sc = co->data;
65 int i;
66 int xlabel = (co->width-4) /2;
67 char percent[10];
68
69 if (co->top == -1) return;
70
71 newtGotorc(co->top, co->left);
72
73 sprintf(percent, "%3d%%", sc->percentage);
74
75 SLsmg_set_color(NEWT_COLORSET_FULLSCALE);
76
77 for (i = 0; i < co->width; i++) {
78 if (i == sc->charsSet)
79 SLsmg_set_color(NEWT_COLORSET_EMPTYSCALE);
80 if (i >= xlabel && i < xlabel+4)
81 SLsmg_write_char(percent[i-xlabel]);
82 else
83 SLsmg_write_char(' ');
84 }
85 }