]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/scout-gfx/lib/loadbar.cc
Inital import
[l4.git] / l4 / pkg / scout-gfx / lib / loadbar.cc
1 #include <l4/scout-gfx/loadbar>
2 #include <l4/mag-gfx/clip_guard>
3
4
5 #include <cstdio>
6
7 #define LOADBAR_RGBA _binary_loadbar_rgba_start
8 #define REDBAR_RGBA _binary_redbar_rgba_start
9 #define WHITEBAR_RGBA    _binary_whitebar_rgba_start
10
11 extern unsigned char LOADBAR_RGBA[];
12 extern unsigned char REDBAR_RGBA[];
13 extern unsigned char WHITEBAR_RGBA[];
14
15 namespace Scout_gfx {
16
17 Loadbar::Loadbar(Factory *f, Loadbar_listener *listener, Mag_gfx::Font *font)
18 : _active(listener ? true : false),
19   _ev_handler(this, listener),
20   _value(0), _max_value(100),
21   _txt(""), _txt_len(0), _txt_sz(Area(0,0)),
22   _font(font)
23 {
24   _size = Area(0,  _LH);
25   _cover = f->create_icon();
26   _cover->rgba(LOADBAR_RGBA, Area(16, 16));
27   _cover->alpha(100);
28   _cover->focus_alpha(150);
29
30   _bar = f->create_icon();
31   _bar->rgba(_active ? REDBAR_RGBA : LOADBAR_RGBA, Area(16, 16));
32   _bar->alpha(_active ? 150 : 255);
33   _bar->default_alpha(150);
34
35   if (_active)
36     {
37       event_handler(&_ev_handler);
38       _bar->event_handler(&_ev_handler);
39       _cover->event_handler(&_ev_handler);
40     }
41
42   append(_cover);
43   append(_bar);
44   _cover->set_geometry(Rect(geometry().area()));
45   _bar->set_geometry(Rect(geometry().area()));
46 }
47
48
49 void
50 Loadbar::_update_bar_geometry(int w)
51 {
52   int max_w = w - _LW;
53   int bar_w = _max_value ? (_value * max_w) / _max_value : max_w;
54   bar_w += _LW;
55   _bar->set_geometry(Rect(_bar->pos(), Area(bar_w, geometry().h())));
56 }
57
58
59 void
60 Loadbar::value(int value)
61 {
62   _value = std::max(std::min(value, _max_value), 0);
63   _update_bar_geometry(_size.w());
64 }
65
66
67 void
68 Loadbar::max_value(int max_value)
69 {
70   _max_value = max_value;
71   _update_bar_geometry(_size.w());
72 }
73
74 void
75 Loadbar::txt(const char *txt)
76 {
77   if (!_font)
78     return;
79
80   _txt     = txt;
81   _txt_sz  = _font->str_sz(_txt);
82   _txt_len = strlen(_txt);
83   invalidate();
84 }
85
86 /**
87  * Element interface
88  */
89 void
90 Loadbar::set_geometry(Rect const &r)
91 {
92   using Mag_gfx::Point;
93   using Mag_gfx::Area;
94   using Mag_gfx::Rect;
95
96   Rect nr = Rect(r.p1(), r.area().max(min_size()));
97
98   Parent_widget::set_geometry(r);
99   _cover->set_geometry(Rect(r.area()));
100   _update_bar_geometry(r.w());
101 }
102
103 void
104 Loadbar::draw(Mag_gfx::Canvas *c, Mag_gfx::Point const &p)
105 {
106   using Mag_gfx::Point;
107   Parent_widget::draw(c, p);
108
109   if (!_font)
110     return;
111
112   Rect pos(geometry().area());
113   Point txt = p + pos.center(_txt_sz).max(pos.p1() + Point(8, 0)) - Point(0, 1);
114
115   /* shrink clipping area to text area (limit too long label) */
116     {
117       Mag_gfx::Clip_guard g(c, c->clip() & (pos + p));
118
119       c->draw_string(txt + Point(0,1), _font, Color(0,0,0,150), _txt);
120       c->draw_string(txt, _font, Color(255,255,255,230), _txt);
121     }
122
123 }
124
125 void
126 Loadbar::mfocus(int flag)
127 {
128   if (!_active)
129     return;
130
131   _bar->mfocus(flag);
132   _cover->mfocus(flag);
133 }
134
135
136
137 void
138 Kbyte_loadbar::_print_kbytes(int kbytes, char *dst, int dst_len)
139 {
140   if (kbytes >= 10*1024)
141     snprintf(dst, dst_len, "%d MByte", kbytes / 1024);
142   else
143     snprintf(dst, dst_len, "%d KByte", kbytes);
144 }
145
146 void
147 Kbyte_loadbar::_update_label()
148 {
149   char value_buf[16];
150   char max_buf[16];
151
152   _print_kbytes(Loadbar::value(), value_buf, sizeof(value_buf));
153   _print_kbytes(Loadbar::max_value(), max_buf, sizeof(max_buf));
154
155   snprintf(_label, sizeof(_label), "%s / %s", value_buf, max_buf);
156
157   Loadbar::txt(_label);
158 }
159
160
161 Kbyte_loadbar::Kbyte_loadbar(Factory *f, Loadbar_listener *listener,
162                              Mag_gfx::Font *font)
163 : Loadbar(f, listener, font)
164 {
165   _label[0] = 0;
166   _update_label();
167 }
168
169 void
170 Kbyte_loadbar::value(int val)
171 {
172   Loadbar::value(val);
173   _update_label();
174 }
175
176 void
177 Kbyte_loadbar::max_value(int max_value)
178 {
179   Loadbar::max_value(max_value);
180   _update_label();
181 }
182
183 }