]> git.tdb.fi Git - libs/gltk.git/blob - source/label.cpp
Make the Text class interface more obvious by not using a reference to pointer
[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()
20 {
21         focusable=false;
22         update_style();
23         set_text(t);
24 }
25
26 void Label::autosize()
27 {
28         const list<Part> &parts=style->get_parts();
29         const Part *text_part=0;
30         for(list<Part>::const_iterator i=parts.begin(); (!text_part && i!=parts.end()); ++i)
31                 if(i->get_name()=="text")
32                         text_part=&*i;
33
34         geom.h=text.get_height();
35         geom.w=text.get_width();
36         if(text_part)
37         {
38                 const Sides &margin=text_part->get_margin();
39                 geom.w+=margin.left+margin.right;
40                 geom.h+=margin.top+margin.bottom;
41         }
42 }
43
44 void Label::set_text(const string &t)
45 {
46         text=t;
47 }
48
49 void Label::render_special(const Part &part) const
50 {
51         if(part.get_name()=="text")
52                 text.render(part, geom);
53 }
54
55 void Label::on_style_change()
56 {
57         text.set_style(style);
58 }
59
60
61 Label::Loader::Loader(Label &l):
62         Widget::Loader(l)
63 {
64         add("text", &Loader::text);
65 }
66
67 void Label::Loader::text(const string &t)
68 {
69         static_cast<Label &>(wdg).text=t;
70 }
71
72 } // namespace GLtk
73 } // namespace Msp