1 #include <l4/scout-gfx/loadbar>
2 #include <l4/mag-gfx/clip_guard>
7 #define LOADBAR_RGBA _binary_loadbar_rgba_start
8 #define REDBAR_RGBA _binary_redbar_rgba_start
9 #define WHITEBAR_RGBA _binary_whitebar_rgba_start
11 extern unsigned char LOADBAR_RGBA[];
12 extern unsigned char REDBAR_RGBA[];
13 extern unsigned char WHITEBAR_RGBA[];
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)),
25 _cover = f->create_icon();
26 _cover->rgba(LOADBAR_RGBA, Area(16, 16));
28 _cover->focus_alpha(150);
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);
37 event_handler(&_ev_handler);
38 _bar->event_handler(&_ev_handler);
39 _cover->event_handler(&_ev_handler);
44 _cover->set_geometry(Rect(geometry().area()));
45 _bar->set_geometry(Rect(geometry().area()));
50 Loadbar::_update_bar_geometry(int w)
53 int bar_w = _max_value ? (_value * max_w) / _max_value : max_w;
55 _bar->set_geometry(Rect(_bar->pos(), Area(bar_w, geometry().h())));
60 Loadbar::value(int value)
62 _value = std::max(std::min(value, _max_value), 0);
63 _update_bar_geometry(_size.w());
68 Loadbar::max_value(int max_value)
70 _max_value = max_value;
71 _update_bar_geometry(_size.w());
75 Loadbar::txt(const char *txt)
81 _txt_sz = _font->str_sz(_txt);
82 _txt_len = strlen(_txt);
90 Loadbar::set_geometry(Rect const &r)
96 Rect nr = Rect(r.p1(), r.area().max(min_size()));
98 Parent_widget::set_geometry(r);
99 _cover->set_geometry(Rect(r.area()));
100 _update_bar_geometry(r.w());
104 Loadbar::draw(Mag_gfx::Canvas *c, Mag_gfx::Point const &p)
106 using Mag_gfx::Point;
107 Parent_widget::draw(c, p);
112 Rect pos(geometry().area());
113 Point txt = p + pos.center(_txt_sz).max(pos.p1() + Point(8, 0)) - Point(0, 1);
115 /* shrink clipping area to text area (limit too long label) */
117 Mag_gfx::Clip_guard g(c, c->clip() & (pos + p));
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);
126 Loadbar::mfocus(int flag)
132 _cover->mfocus(flag);
138 Kbyte_loadbar::_print_kbytes(int kbytes, char *dst, int dst_len)
140 if (kbytes >= 10*1024)
141 snprintf(dst, dst_len, "%d MByte", kbytes / 1024);
143 snprintf(dst, dst_len, "%d KByte", kbytes);
147 Kbyte_loadbar::_update_label()
152 _print_kbytes(Loadbar::value(), value_buf, sizeof(value_buf));
153 _print_kbytes(Loadbar::max_value(), max_buf, sizeof(max_buf));
155 snprintf(_label, sizeof(_label), "%s / %s", value_buf, max_buf);
157 Loadbar::txt(_label);
161 Kbyte_loadbar::Kbyte_loadbar(Factory *f, Loadbar_listener *listener,
163 : Loadbar(f, listener, font)
170 Kbyte_loadbar::value(int val)
177 Kbyte_loadbar::max_value(int max_value)
179 Loadbar::max_value(max_value);