]> git.tdb.fi Git - libs/gltk.git/blob - source/toggle.cpp
Improve widget part caching
[libs/gltk.git] / source / toggle.cpp
1 #include "panel.h"
2 #include "part.h"
3 #include "style.h"
4 #include "toggle.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GLtk {
10
11 Toggle::Toggle(const string &t):
12         text(),
13         pressed(false),
14         value(false),
15         exclusive(false)
16 {
17         set_text(t);
18 }
19
20 void Toggle::autosize()
21 {
22         if(!style)
23                 return;
24
25         Widget::autosize();
26
27         if(const Part *text_part = style->get_part("text"))
28         {
29                 const Sides &margin = text_part->get_margin();
30                 geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
31                 geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
32         }
33
34         rebuild();
35 }
36
37 void Toggle::set_text(const string &t)
38 {
39         text = t;
40         signal_autosize_changed.emit();
41         rebuild();
42 }
43
44 void Toggle::set_exclusive(bool e)
45 {
46         exclusive = e;
47         if(exclusive && value)
48                 exclude_siblings();
49 }
50
51 void Toggle::exclude_siblings()
52 {
53         const list<Widget *> &siblings = parent->get_children();
54         for(list<Widget *>::const_iterator i=siblings.begin(); i!=siblings.end(); ++i)
55                 if(Toggle *tgl = dynamic_cast<Toggle *>(*i))
56                         if(tgl!=this && tgl->get_exclusive() && tgl->get_value())
57                                 tgl->set_value(false);
58 }
59
60 void Toggle::set_value(bool v)
61 {
62         value = v;
63         if(value)
64         {
65                 set_state(ACTIVE);
66                 if(exclusive && parent)
67                         exclude_siblings();
68         }
69         else
70                 clear_state(ACTIVE);
71 }
72
73 void Toggle::rebuild_special(const Part &part)
74 {
75         if(part.get_name()=="text")
76                 text.build(part, geom, part_cache);
77 }
78
79 void Toggle::button_press(int, int, unsigned btn)
80 {
81         if(btn==1)
82                 pressed = true;
83 }
84
85 void Toggle::button_release(int x, int y, unsigned btn)
86 {
87         if(pressed && btn==1)
88         {
89                 if(geom.is_inside_relative(x, y) && (!value || !exclusive))
90                 {
91                         set_value(!value);
92                         signal_toggled.emit(value);
93                 }
94
95                 pressed = false;
96         }
97 }
98
99 void Toggle::on_style_change()
100 {
101         text.set_style(style);
102 }
103
104
105 Toggle::Loader::Loader(Toggle &t):
106         DataFile::DerivedObjectLoader<Toggle, Widget::Loader>(t)
107 {
108         add("exclusive", &Toggle::exclusive);
109         add("text",      &Loader::text);
110         add("value",     &Toggle::value);
111 }
112
113 void Toggle::Loader::finish()
114 {
115         obj.set_state(ACTIVE, (obj.value ? ACTIVE : NORMAL));
116 }
117
118 void Toggle::Loader::text(const string &t)
119 {
120         obj.text = t;
121 }
122
123 } // namespace GLtk
124 } // namespace Msp