]> git.tdb.fi Git - libs/gltk.git/blob - source/label.cpp
Add Text class with multiline support
[libs/gltk.git] / source / label.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "label.h"
9 #include "part.h"
10 #include "style.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GLtk {
16
17 Label::Label(const Resources &r, const string &t):
18         Widget(r),
19         text(style)
20 {
21         update_style();
22         set_text(t);
23 }
24
25 void Label::autosize()
26 {
27         const list<Part> &parts=style->get_parts();
28         const Part *text_part=0;
29         for(list<Part>::const_iterator i=parts.begin(); (!text_part && i!=parts.end()); ++i)
30                 if(i->get_name()=="text")
31                         text_part=&*i;
32
33         geom.h=text.get_height();
34         geom.w=text.get_width();
35         if(text_part)
36         {
37                 const Sides &margin=text_part->get_margin();
38                 geom.w+=margin.left+margin.right;
39                 geom.h+=margin.top+margin.bottom;
40         }
41 }
42
43 void Label::set_text(const string &t)
44 {
45         text=t;
46 }
47
48 void Label::render_special(const Part &part) const
49 {
50         if(part.get_name()=="text")
51                 text.render(part, geom);
52 }
53
54
55 Label::Loader::Loader(Label &l):
56         Widget::Loader(l)
57 {
58         add("text", &Loader::text);
59 }
60
61 void Label::Loader::text(const string &t)
62 {
63         static_cast<Label &>(wdg).text=t;
64 }
65
66 } // namespace GLtk
67 } // namespace Msp