1 //
2 // Copyright (C) 2009 Nick Gasson
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17
18 #include "gui/Label.hpp"
19 #include "ILogger.hpp"
20
21 #include <cstdarg>
22 #include <cstdio>
23
24 using namespace gui;
25
Label(const AttributeSet & attrs)26 Label::Label(const AttributeSet& attrs)
27 : Widget(attrs),
28 text_(attrs.get<string>("text")),
29 font_name(attrs.get<string>("font", "")),
30 colour_(attrs.get<Colour>("colour", colour::WHITE)),
31 dirty(true)
32 {
33
34 }
35
text(const string & t)36 void Label::text(const string& t)
37 {
38 if (text_ != t) {
39 text_ = t;
40 dirty = true;
41 }
42 }
43
render(RenderContext & rc) const44 void Label::render(RenderContext& rc) const
45 {
46 rc.print(rc.theme().font(font_name), x(), y(), text_, colour_);
47 }
48
adjust_for_theme(const Theme & theme)49 void Label::adjust_for_theme(const Theme& theme)
50 {
51 IFontPtr font = theme.font(font_name);
52
53 if (dirty) {
54 width(font->text_width(text_));
55 height(font->height());
56
57 dirty = false;
58 }
59 }
60
format(const char * fmt,...)61 void Label::format(const char* fmt, ...)
62 {
63 va_list ap;
64 va_start(ap, fmt);
65
66 char* buf;
67 vasprintf(&buf, fmt, ap);
68 text(buf);
69
70 free(buf);
71 va_end(ap);
72 }
73
74