]>
Commit | Line | Data |
---|---|---|
139f06bc | 1 | #include <slang.h> |
eaf8dbd3 | 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; | |
5505c898 | 11 | unsigned int percentage; |
099b22e5 ML |
12 | int csEmpty; |
13 | int csFull; | |
eaf8dbd3 | 14 | }; |
15 | ||
16 | static void scaleDraw(newtComponent co); | |
17 | ||
18 | static struct componentOps scaleOps = { | |
19 | scaleDraw, | |
20 | newtDefaultEventHandler, | |
21 | NULL, | |
8f52cd47 | 22 | newtDefaultPlaceHandler, |
23 | newtDefaultMappedHandler, | |
eaf8dbd3 | 24 | } ; |
25 | ||
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; | |
c101e99e | 33 | co->destroyCallback = NULL; |
eaf8dbd3 | 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 | ||
43 | sc->fullValue = fullValue; | |
44 | sc->charsSet = 0; | |
5505c898 | 45 | sc->percentage = 0; |
099b22e5 ML |
46 | sc->csEmpty = NEWT_COLORSET_EMPTYSCALE; |
47 | sc->csFull = NEWT_COLORSET_FULLSCALE; | |
eaf8dbd3 | 48 | |
49 | return co; | |
50 | } | |
51 | ||
649a0152 | 52 | void newtScaleSet(newtComponent co, unsigned long long amount) { |
eaf8dbd3 | 53 | struct scale * sc = co->data; |
5505c898 | 54 | int newPercentage; |
53179ae3 | 55 | |
c3db22aa | 56 | if (amount >= sc->fullValue) { |
6f481af2 | 57 | newPercentage = 100; |
c3db22aa | 58 | sc->charsSet = co->width; |
59 | } else if (sc->fullValue >= -1ULL / (100 > co->width ? 100 : co->width)) { | |
60 | /* avoid overflow on large numbers */ | |
61 | sc->charsSet = amount / (sc->fullValue / co->width); | |
62 | newPercentage = amount / (sc->fullValue / 100); | |
63 | } else { | |
64 | sc->charsSet = (amount * co->width) / sc->fullValue; | |
65 | newPercentage = (amount * 100) / sc->fullValue; | |
66 | } | |
53179ae3 | 67 | |
5505c898 | 68 | if (newPercentage != sc->percentage) { |
6f481af2 | 69 | sc->percentage = newPercentage; |
70 | scaleDraw(co); | |
53179ae3 | 71 | } |
eaf8dbd3 | 72 | } |
73 | ||
099b22e5 ML |
74 | void newtScaleSetColors(newtComponent co, int empty, int full) { |
75 | struct scale * sc = co->data; | |
76 | ||
77 | sc->csEmpty = empty; | |
78 | sc->csFull = full; | |
79 | scaleDraw(co); | |
80 | } | |
81 | ||
eaf8dbd3 | 82 | static void scaleDraw(newtComponent co) { |
83 | struct scale * sc = co->data; | |
84 | int i; | |
5505c898 | 85 | int xlabel = (co->width-4) /2; |
86 | char percent[10]; | |
87 | ||
eaf8dbd3 | 88 | if (co->top == -1) return; |
89 | ||
90 | newtGotorc(co->top, co->left); | |
91 | ||
5505c898 | 92 | sprintf(percent, "%3d%%", sc->percentage); |
eaf8dbd3 | 93 | |
099b22e5 | 94 | SLsmg_set_color(sc->csFull); |
5505c898 | 95 | |
96 | for (i = 0; i < co->width; i++) { | |
97 | if (i == sc->charsSet) | |
099b22e5 | 98 | SLsmg_set_color(sc->csEmpty); |
5505c898 | 99 | if (i >= xlabel && i < xlabel+4) |
100 | SLsmg_write_char(percent[i-xlabel]); | |
101 | else | |
102 | SLsmg_write_char(' '); | |
103 | } | |
4e667865 | 104 | /* put cursor at beginning of text for better accessibility */ |
105 | newtGotorc(co->top, co->left + xlabel); | |
eaf8dbd3 | 106 | } |