]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
34ca1781153f946fddaaf69f82e993643b2864f1
[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_visible(false);
32         list.set_view_all();
33         list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected));
34         list.signal_selection_cleared.connect(sigc::mem_fun(this, &Dropdown::list_selection_cleared));
35         list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed));
36 }
37
38 void Dropdown::autosize_special(const Part &part, Geometry &ageom) const
39 {
40         if(part.get_name()=="list")
41         {
42                 Geometry lgeom;
43                 list.autosize(lgeom);
44                 ageom.w = max(ageom.w, list.get_geometry().w);
45         }
46         else if(part.get_name()=="text")
47         {
48                 const Sides &margin = part.get_margin();
49                 const GL::Font &font = style->get_font();
50                 float font_size = style->get_font_size();
51
52                 unsigned max_w = 0;
53                 const ListData &data = list.get_data();
54                 for(unsigned i=0; i<data.size(); ++i)
55                 {
56                         unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
57                         max_w = max(max_w, w);
58                 }
59                 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
60
61                 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font_size);
62                 ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
63         }
64 }
65
66 void Dropdown::rebuild_special(const Part &part)
67 {
68         if(part.get_name()=="text")
69                 text.build(part, state, geom, part_cache);
70         else
71                 Widget::rebuild_special(part);
72 }
73
74 void Dropdown::render_special(const Part &part, GL::Renderer &renderer) const
75 {
76         if(part.get_name()=="list" && dropped)
77                 list.render(renderer);
78 }
79
80 void Dropdown::button_press(int x, int y, unsigned btn)
81 {
82         if(dropped)
83         {
84                 Container::button_press(x, y, btn);
85                 if(!click_focus)
86                 {
87                         dropped = false;
88                         list.set_visible(false);
89                         clear_state(ACTIVE);
90                         signal_ungrab_pointer.emit();
91                 }
92         }
93         else if(btn==1)
94         {
95                 dropped = true;
96                 list.set_visible(true);
97                 resize_list();
98                 set_state(ACTIVE);
99                 signal_grab_pointer.emit();
100         }
101 }
102
103 void Dropdown::on_geometry_change()
104 {
105         if(dropped)
106                 resize_list();
107 }
108
109 void Dropdown::on_style_change()
110 {
111         text.set_style(style);
112         if(dropped)
113                 resize_list();
114 }
115
116 void Dropdown::list_autosize_changed()
117 {
118         if(dropped)
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 void Dropdown::list_selection_cleared()
172 {
173         text.set(string());
174 }
175
176
177 Dropdown::Loader::Loader(Dropdown &d):
178         DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
179 {
180         add("item", &Loader::item);
181 }
182
183 void Dropdown::Loader::item(const string &v)
184 {
185         dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);
186 }
187
188 } // namespace GLtk
189 } // namespace Msp