]> git.tdb.fi Git - libs/gltk.git/blob - dropdown.cpp
41e42fa72fa4b45b399ede99167d3a45d0bbe869
[libs/gltk.git] / dropdown.cpp
1 #include <msp/gl/font.h>
2 #include "dropdown.h"
3 #include "list.h"
4 #include "panel.h"
5 #include "part.h"
6 #include "root.h"
7 #include "style.h"
8 #include "text.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GLtk {
14
15 Dropdown::Dropdown():
16         dropped(false)
17 {
18         add(list);
19         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
20         list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
21 }
22
23 void Dropdown::autosize()
24 {
25         if(!style)
26                 return;
27
28         Widget::autosize();
29         geom.w = max(geom.w, list.get_geometry().w);
30
31         if(const Part *text_part = style->get_part("text"))
32         {
33                 const Sides &margin = text_part->get_margin();
34                 const GL::Font &font = style->get_font();
35                 float font_size = style->get_font_size();
36                 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
37                 geom.h = max(geom.h, line_height+margin.top+margin.bottom);
38         }
39
40         rebuild();
41 }
42
43 void Dropdown::append(const string &item)
44 {
45         list.append(item);
46 }
47
48 void Dropdown::insert(unsigned i, const string &v)
49 {
50         list.insert(i, v);
51 }
52
53 void Dropdown::remove(unsigned i)
54 {
55         list.remove(i);
56 }
57
58 void Dropdown::clear()
59 {
60         list.clear();
61 }
62
63 unsigned Dropdown::get_n_items() const
64 {
65         return list.get_n_items();
66 }
67
68 void Dropdown::set_selected_index(int i)
69 {
70         list.set_selected_index(i);
71 }
72
73 const string &Dropdown::get_selected() const
74 {
75         return list.get_selected();
76 }
77
78 int Dropdown::get_selected_index() const
79 {
80         return list.get_selected_index();
81 }
82
83 void Dropdown::rebuild_special(const Part &part, CachedPart &cache)
84 {
85         if(part.get_name()=="text")
86         {
87                 if(list.get_selected_index()>=0)
88                         Text(*style, list.get_selected()).build(part, geom, cache);
89                 else
90                         cache.texture = 0;
91         }
92 }
93
94 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
95 {
96         if(part.get_name()=="list" && dropped)
97                 list.render(renderer);
98 }
99
100 void Dropdown::button_press(int x, int y, unsigned btn)
101 {
102         if(dropped)
103         {
104                 Container::button_press(x, y, btn);
105                 if(!click_focus)
106                 {
107                         dropped = false;
108                         clear_state(ACTIVE);
109                         signal_ungrab_pointer.emit();
110                 }
111         }
112         else if(btn==1)
113         {
114                 dropped = true;
115                 set_state(ACTIVE);
116                 signal_grab_pointer.emit();
117         }
118 }
119
120 void Dropdown::on_geometry_change()
121 {
122         resize_list();
123 }
124
125 void Dropdown::on_style_change()
126 {
127         resize_list();
128 }
129
130 void Dropdown::list_autosize_changed()
131 {
132         resize_list();
133         signal_autosize_changed.emit();
134 }
135
136 void Dropdown::resize_list()
137 {
138         list.autosize_all();
139         Geometry lgeom = list.get_geometry();
140         lgeom.x = 0;
141         lgeom.y = -lgeom.h;
142         lgeom.w = max(geom.w, lgeom.w);
143         int root_x = geom.x;
144         int root_y = geom.y;
145         for(Widget *p=parent; p; p=p->get_parent())
146         {
147                 root_x += p->get_geometry().x;
148                 root_y += p->get_geometry().y;
149                 if(Root *root = dynamic_cast<Root *>(p))
150                 {
151                         const Geometry &rgeom = root->get_geometry();
152                         if(lgeom.h*2>rgeom.h)
153                                 lgeom.h = rgeom.h/2;
154                         if(root_y+lgeom.y<0)
155                                 lgeom.y = -root_y;
156                         if(root_y+lgeom.y+lgeom.h>rgeom.h)
157                                 lgeom.y = rgeom.h-lgeom.h-root_y;
158                         if(root_x+lgeom.x+lgeom.w>rgeom.w)
159                                 lgeom.x = rgeom.w-lgeom.w-root_x;
160                         break;
161                 }
162         }
163         list.set_geometry(lgeom);
164 }
165
166 void Dropdown::list_item_selected(unsigned index, const std::string &item)
167 {
168         if(dropped)
169         {
170                 dropped = false;
171                 clear_state(ACTIVE);
172                 signal_ungrab_pointer.emit();
173         }
174
175         signal_item_selected.emit(index, item);
176 }
177
178
179 Dropdown::Loader::Loader(Dropdown &d):
180         Widget::Loader(d)
181 {
182         add("item", &Loader::item);
183 }
184
185 void Dropdown::Loader::item(const string &str)
186 {
187         dynamic_cast<Dropdown &>(obj).append(str);
188 }
189
190 } // namespace GLtk
191 } // namespace Msp