This source file includes following definitions.
- gauge_callback
- gauge_new
- gauge_set_value
- gauge_show
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 #include <config.h>
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "lib/global.h"
41
42 #include "lib/tty/tty.h"
43 #include "lib/tty/color.h"
44 #include "lib/skin.h"
45 #include "lib/widget.h"
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 static cb_ret_t
62 gauge_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
63 {
64 WGauge *g = GAUGE (w);
65 const int *colors;
66
67 switch (msg)
68 {
69 case MSG_DRAW:
70 colors = widget_get_colors (w);
71 widget_gotoyx (w, 0, 0);
72 if (!g->shown)
73 {
74 tty_setcolor (colors[DLG_COLOR_NORMAL]);
75 tty_printf ("%*s", w->rect.cols, "");
76 }
77 else
78 {
79 int gauge_len;
80 int percentage, columns;
81 int total = g->max;
82 int done = g->current;
83
84 if (total <= 0 || done < 0)
85 {
86 done = 0;
87 total = 100;
88 }
89 if (done > total)
90 done = total;
91 while (total > 65535)
92 {
93 total /= 256;
94 done /= 256;
95 }
96
97 gauge_len = w->rect.cols - 7;
98
99 percentage = (200 * done / total + 1) / 2;
100 columns = (2 * gauge_len * done / total + 1) / 2;
101 tty_print_char ('[');
102 if (g->from_left_to_right)
103 {
104 tty_setcolor (GAUGE_COLOR);
105 tty_printf ("%*s", columns, "");
106 tty_setcolor (colors[DLG_COLOR_NORMAL]);
107 tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
108 }
109 else
110 {
111 tty_setcolor (colors[DLG_COLOR_NORMAL]);
112 tty_printf ("%*s", gauge_len - columns, "");
113 tty_setcolor (GAUGE_COLOR);
114 tty_printf ("%*s", columns, "");
115 tty_setcolor (colors[DLG_COLOR_NORMAL]);
116 tty_printf ("] %3d%%", percentage);
117 }
118 }
119 return MSG_HANDLED;
120
121 default:
122 return widget_default_callback (w, sender, msg, parm, data);
123 }
124 }
125
126
127
128
129
130 WGauge *
131 gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
132 {
133 WRect r = { y, x, 1, cols };
134 WGauge *g;
135 Widget *w;
136
137 g = g_new (WGauge, 1);
138 w = WIDGET (g);
139 widget_init (w, &r, gauge_callback, NULL);
140
141 g->shown = shown;
142 if (max == 0)
143 max = 1;
144 g->max = max;
145 g->current = current;
146 g->from_left_to_right = TRUE;
147
148 return g;
149 }
150
151
152
153 void
154 gauge_set_value (WGauge *g, int max, int current)
155 {
156 if (g->current == current && g->max == max)
157 return;
158
159 if (max == 0)
160 max = 1;
161 g->current = current;
162 g->max = max;
163 widget_draw (WIDGET (g));
164 }
165
166
167
168 void
169 gauge_show (WGauge *g, gboolean shown)
170 {
171 if (g->shown != shown)
172 {
173 g->shown = shown;
174 widget_draw (WIDGET (g));
175 }
176 }
177
178