]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Add Dropdown widget
[libs/gltk.git] / source / list.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/transform.h>
10 #include "graphic.h"
11 #include "list.h"
12 #include "part.h"
13 #include "style.h"
14 #include "vslider.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GLtk {
20
21 List::List(const Resources &r):
22         Widget(r),
23         sel_index(-1),
24         first(0),
25         n_visible(1),
26         items_part(0),
27         slider(new VSlider(res)),
28         slider_active(false)
29 {
30         slider->set_step(1);
31         slider->signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
32
33         update_style();
34 }
35
36 List::~List()
37 {
38         delete slider;
39 }
40
41 void List::append(const string &v)
42 {
43         items.push_back(v);
44         recalculate_parameters();
45 }
46
47 void List::insert(unsigned i, const string &v)
48 {
49         if(i>items.size())
50                 throw InvalidParameterValue("Index out of range");
51
52         items.insert(items.begin()+i, v);
53         recalculate_parameters();
54 }
55
56 void List::remove(unsigned i)
57 {
58         if(i>items.size())
59                 throw InvalidParameterValue("Index out of range");
60
61         items.erase(items.begin()+i);
62         if(sel_index>static_cast<int>(i))
63                 --sel_index;
64         else if(sel_index==static_cast<int>(i))
65                 sel_index=-1;
66
67         recalculate_parameters();
68 }
69
70 void List::clear()
71 {
72         items.clear();
73         sel_index=-1;
74 }
75
76 const string &List::get_selected() const
77 {
78         if(sel_index<0)
79                 throw InvalidState("No selection");
80
81         return items[sel_index];
82 }
83
84 void List::button_press(int x, int y, unsigned btn)
85 {
86         if(slider->get_geometry().is_inside(x, y))
87         {
88                 const Geometry &sgeom=slider->get_geometry();
89                 slider->button_press(x-sgeom.x, y-sgeom.y, btn);
90                 slider_active=true;
91         }
92         else if(btn==1)
93         {
94                 const GL::Font *const font=style->get_font();
95                 const unsigned row_height=static_cast<unsigned>(font->get_default_size());
96
97                 if(items_part)
98                         y+=items_part->get_margin().top;
99
100                 unsigned i=(geom.h-1-y)/row_height;
101                 if(i<n_visible)
102                 {
103                         sel_index=first+i;
104
105                         signal_item_selected.emit(sel_index, items[sel_index]);
106                 }
107         }
108 }
109
110 void List::button_release(int x, int y, unsigned btn)
111 {
112         if(slider_active)
113         {
114                 const Geometry &sgeom=slider->get_geometry();
115                 slider->button_release(x-sgeom.x, y-sgeom.y, btn);
116                 slider_active=false;
117         }
118 }
119
120 void List::pointer_motion(int x, int y)
121 {
122         if(slider_active)
123         {
124                 const Geometry &sgeom=slider->get_geometry();
125                 slider->pointer_motion(x-sgeom.x, y-sgeom.y);
126         }
127 }
128
129 void List::render_special(const Part &part) const
130 {
131         if(part.get_name()=="items")
132         {
133                 const GL::Font *const font=style->get_font();
134                 const float font_size=font->get_default_size();
135                 const unsigned row_height=static_cast<unsigned>(font_size);
136                 const Sides &margin=part.get_margin();
137
138                 Geometry pgeom=geom;
139                 pgeom.h=row_height;
140
141                 for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
142                 {
143                         Geometry rgeom;
144                         rgeom.w=static_cast<unsigned>(font->get_string_width(items[first+i])*font_size);
145                         rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
146                         rgeom.y=geom.h-margin.top-(i+1)*row_height-static_cast<int>(font->get_descent()*font_size);
147                         part.get_alignment().apply(rgeom, pgeom);
148
149                         GL::push_matrix();
150                         GL::translate(rgeom.x, rgeom.y, 0);
151                         GL::scale_uniform(font_size);
152                         font->draw_string(items[first+i]);
153                         GL::pop_matrix();
154                 }
155         }
156         else if(part.get_name()=="selection")
157         {
158                 if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
159                 {
160                         const GL::Font *const font=style->get_font();
161                         const float font_size=font->get_default_size();
162                         const unsigned row_height=static_cast<unsigned>(font_size);
163                         const Sides &margin=part.get_margin();
164
165                         Geometry pgeom=geom;
166                         pgeom.h=row_height;
167                         pgeom.w-=margin.left+margin.right;
168
169                         Geometry rgeom=part.get_geometry();
170                         rgeom.y+=geom.h-margin.top-row_height*(sel_index-first+1);
171                         rgeom.x+=margin.left;
172                         part.get_alignment().apply(rgeom, pgeom);
173
174                         GL::push_matrix();
175                         GL::translate(rgeom.x, rgeom.y, 0);
176                         part.get_graphic(state)->render(rgeom.w, rgeom.h);
177                         GL::pop_matrix();
178                 }
179         }
180         else if(part.get_name()=="slider")
181                 slider->render();
182 }
183
184 void List::on_geometry_change()
185 {
186         reposition_slider();
187
188         recalculate_parameters();
189 }
190
191 void List::on_style_change()
192 {
193         reposition_slider();
194
195         items_part=0;
196         for(list<Part>::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
197                 if(i->get_name()=="items")
198                         items_part=&*i;
199
200         recalculate_parameters();
201 }
202
203 void List::reposition_slider()
204 {
205         for(list<Part>::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
206                 if(i->get_name()=="slider")
207                 {
208                         Geometry sgeom=i->get_geometry();
209                         i->get_alignment().apply(sgeom, geom, i->get_margin());
210                         slider->set_geometry(sgeom);
211                 }
212 }
213
214 void List::recalculate_parameters()
215 {
216         const GL::Font *font=style->get_font();
217         unsigned row_height=static_cast<unsigned>(font->get_default_size());
218
219         unsigned h=geom.h;
220         if(items_part)
221         {
222                 const Sides &margin=items_part->get_margin();
223                 h-=margin.top+margin.bottom;
224         }
225
226         n_visible=h/row_height;
227
228         if(first+n_visible>items.size())
229         {
230                 if(items.size()>n_visible)
231                         first=items.size()-n_visible;
232                 else
233                         first=0;
234         }
235
236         if(items.size()>n_visible)
237         {
238                 slider->set_range(0, items.size()-n_visible);
239                 slider->set_value(items.size()-n_visible-first);
240         }
241         else
242         {
243                 slider->set_range(0, 0);
244                 slider->set_value(0);
245         }
246 }
247
248 void List::slider_value_changed(double value)
249 {
250         first=items.size()-n_visible-static_cast<unsigned>(value);
251 }
252
253
254 List::Loader::Loader(List &l):
255         Widget::Loader(l)
256 {
257         add("item", &Loader::item);
258 }
259
260 void List::Loader::item(const string &v)
261 {
262         static_cast<List &>(wdg).append(v);
263 }
264
265 } // namespace GLtk
266 } // namespace Msp