]> git.tdb.fi Git - libs/gltk.git/blob - source/label.cpp
Implement autosize() method for most widgets
[libs/gltk.git] / source / label.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007, 2009-2011  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 string &t)
18 {
19         focusable = false;
20         set_text(t);
21 }
22
23 void Label::autosize()
24 {
25         if(!style)
26                 return;
27
28         Widget::autosize();
29
30         if(const Part *text_part = style->get_part("text"))
31         {
32                 const Sides &margin = text_part->get_margin();
33                 geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
34                 geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
35         }
36 }
37
38 void Label::set_text(const string &t)
39 {
40         text = t;
41 }
42
43 void Label::render_special(const Part &part) const
44 {
45         if(part.get_name()=="text")
46                 text.render(part, geom);
47 }
48
49 void Label::on_style_change()
50 {
51         text.set_style(style);
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 &>(obj).text = t;
64 }
65
66 } // namespace GLtk
67 } // namespace Msp