]> git.tdb.fi Git - libs/gltk.git/blob - source/dropdown.cpp
Use size_t to represent counts and indices
[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         input_type = INPUT_NAVIGATION;
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(size_t 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                         close_list();
87         }
88         else if(btn==1)
89                 open_list();
90 }
91
92 bool Dropdown::navigate(Navigation nav)
93 {
94         if(dropped)
95         {
96                 if(nav==NAV_CANCEL)
97                         close_list();
98                 else
99                         list.navigate(nav);
100         }
101         else if(nav==NAV_ACTIVATE)
102                 open_list();
103         else
104                 return false;
105
106         return true;
107 }
108
109 void Dropdown::on_size_change()
110 {
111         if(dropped)
112                 resize_list();
113 }
114
115 void Dropdown::on_style_change()
116 {
117         text.set_style(style);
118         if(dropped)
119                 resize_list();
120 }
121
122 void Dropdown::open_list()
123 {
124         dropped = true;
125         list.set_visible(true);
126         resize_list();
127         set_state(ACTIVE);
128         set_input_focus(&list);
129         signal_grab_pointer.emit();
130 }
131
132 void Dropdown::close_list()
133 {
134         dropped = false;
135         list.set_visible(false);
136         clear_state(ACTIVE);
137         signal_ungrab_pointer.emit();
138 }
139
140 void Dropdown::list_autosize_changed()
141 {
142         if(dropped)
143                 resize_list();
144         signal_autosize_changed.emit();
145 }
146
147 void Dropdown::resize_list()
148 {
149         Geometry lgeom;
150         list.autosize(lgeom);
151         lgeom.x = 0;
152         lgeom.y = -lgeom.h;
153         lgeom.w = max(geom.w, lgeom.w);
154         int root_x = geom.x;
155         int root_y = geom.y;
156         for(Widget *p=parent; p; p=p->get_parent())
157         {
158                 root_x += p->get_geometry().x;
159                 root_y += p->get_geometry().y;
160                 if(Root *root = dynamic_cast<Root *>(p))
161                 {
162                         const Geometry &rgeom = root->get_geometry();
163                         if(lgeom.h*2>rgeom.h)
164                         {
165                                 lgeom.h = rgeom.h/2;
166                                 lgeom.y = -lgeom.h;
167                         }
168                         if(root_y+lgeom.y<0)
169                                 lgeom.y = -root_y;
170                         if(root_y+lgeom.y+lgeom.h>rgeom.h)
171                                 lgeom.y = rgeom.h-lgeom.h-root_y;
172                         if(root_x+lgeom.x+lgeom.w>rgeom.w)
173                                 lgeom.x = rgeom.w-lgeom.w-root_x;
174                         break;
175                 }
176         }
177         list.set_geometry(lgeom);
178 }
179
180 void Dropdown::list_item_selected(size_t index)
181 {
182         if(dropped)
183         {
184                 dropped = false;
185                 clear_state(ACTIVE);
186                 signal_ungrab_pointer.emit();
187         }
188
189         text.set(list.get_data().get_string(index));
190
191         signal_item_selected.emit(index);
192         mark_rebuild();
193 }
194
195 void Dropdown::list_selection_cleared()
196 {
197         text.set(string());
198 }
199
200
201 Dropdown::Loader::Loader(Dropdown &d):
202         DataFile::DerivedObjectLoader<Dropdown, Widget::Loader>(d)
203 {
204         add("item", &Loader::item);
205 }
206
207 void Dropdown::Loader::item(const string &v)
208 {
209         dynamic_cast<BasicListData<string> &>(obj.list.get_data()).append(v);
210 }
211
212 } // namespace GLtk
213 } // namespace Msp