]> git.tdb.fi Git - libs/gltk.git/blob - source/toggle.cpp
Strip copyright messages and id tags from individual files
[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
35 void Toggle::set_text(const string &t)
36 {
37         text = t;
38         signal_autosize_changed.emit();
39 }
40
41 void Toggle::set_exclusive(bool e)
42 {
43         exclusive = e;
44         if(exclusive && value)
45                 exclude_siblings();
46 }
47
48 void Toggle::exclude_siblings()
49 {
50         const list<Widget *> &siblings = parent->get_children();
51         for(list<Widget *>::const_iterator i=siblings.begin(); i!=siblings.end(); ++i)
52                 if(Toggle *tgl = dynamic_cast<Toggle *>(*i))
53                         if(tgl!=this && tgl->get_exclusive() && tgl->get_value())
54                                 tgl->set_value(false);
55 }
56
57 void Toggle::set_value(bool v)
58 {
59         value = v;
60         if(value)
61         {
62                 state |= ACTIVE;
63                 if(exclusive && parent)
64                         exclude_siblings();
65         }
66         else
67                 state &= ~ACTIVE;
68 }
69
70 void Toggle::render_special(const Part &part) const
71 {
72         if(part.get_name()=="text")
73                 text.render(part, geom);
74 }
75
76 void Toggle::button_press(int, int, unsigned btn)
77 {
78         if(btn==1)
79                 pressed = true;
80 }
81
82 void Toggle::button_release(int x, int y, unsigned btn)
83 {
84         if(pressed && btn==1)
85         {
86                 if(geom.is_inside_relative(x, y) && (!value || !exclusive))
87                 {
88                         set_value(!value);
89                         signal_toggled.emit(value);
90                 }
91
92                 pressed = false;
93         }
94 }
95
96 void Toggle::on_style_change()
97 {
98         text.set_style(style);
99 }
100
101
102 Toggle::Loader::Loader(Toggle &t):
103         Widget::Loader(t)
104 {
105         add("exclusive", &Toggle::exclusive);
106         add("text",      &Loader::text);
107         add("value",     &Toggle::value);
108 }
109
110 Toggle &Toggle::Loader::get_object() const
111 {
112         return static_cast<Toggle &>(obj);
113 }
114
115 void Toggle::Loader::finish()
116 {
117         Toggle &tgl = get_object();
118         if(tgl.value)
119                 tgl.state |= ACTIVE;
120         else
121                 tgl.state &= ~ACTIVE;
122 }
123
124 void Toggle::Loader::text(const string &t)
125 {
126         get_object().text = t;
127 }
128
129 } // namespace GLtk
130 } // namespace Msp