]>
git.ipfire.org Git - thirdparty/newt.git/blob - scale.c
11 unsigned int percentage
;
16 static void scaleDraw(newtComponent co
);
18 static struct componentOps scaleOps
= {
20 newtDefaultEventHandler
,
22 newtDefaultPlaceHandler
,
23 newtDefaultMappedHandler
,
26 newtComponent
newtScale(int left
, int top
, int width
, long long fullValue
) {
30 co
= malloc(sizeof(*co
));
31 sc
= malloc(sizeof(struct scale
));
33 co
->destroyCallback
= NULL
;
43 sc
->fullValue
= fullValue
;
46 sc
->csEmpty
= NEWT_COLORSET_EMPTYSCALE
;
47 sc
->csFull
= NEWT_COLORSET_FULLSCALE
;
52 void newtScaleSet(newtComponent co
, unsigned long long amount
) {
53 struct scale
* sc
= co
->data
;
56 if (amount
>= sc
->fullValue
) {
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);
64 sc
->charsSet
= (amount
* co
->width
) / sc
->fullValue
;
65 newPercentage
= (amount
* 100) / sc
->fullValue
;
68 if (newPercentage
!= sc
->percentage
) {
69 sc
->percentage
= newPercentage
;
74 void newtScaleSetColors(newtComponent co
, int empty
, int full
) {
75 struct scale
* sc
= co
->data
;
82 static void scaleDraw(newtComponent co
) {
83 struct scale
* sc
= co
->data
;
85 int xlabel
= (co
->width
-4) /2;
88 if (co
->top
== -1) return;
90 newtGotorc(co
->top
, co
->left
);
92 sprintf(percent
, "%3d%%", sc
->percentage
);
94 SLsmg_set_color(sc
->csFull
);
96 for (i
= 0; i
< co
->width
; i
++) {
97 if (i
== sc
->charsSet
)
98 SLsmg_set_color(sc
->csEmpty
);
99 if (i
>= xlabel
&& i
< xlabel
+4)
100 SLsmg_write_char(percent
[i
-xlabel
]);
102 SLsmg_write_char(' ');
104 /* put cursor at beginning of text for better accessibility */
105 newtGotorc(co
->top
, co
->left
+ xlabel
);