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