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