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