]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
722e296edf9cae096b19e2c736f6075d439dc686
[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 void List::set_selected_index(int i)
77 {
78         if(i<0)
79                 sel_index=-1;
80         else if(i<static_cast<int>(items.size()))
81         {
82                 sel_index=i;
83                 signal_item_selected.emit(sel_index, items[sel_index]);
84         }
85         else
86                 throw InvalidParameterValue("Index out of range");
87 }
88
89 const string &List::get_selected() const
90 {
91         if(sel_index<0)
92                 throw InvalidState("No selection");
93
94         return items[sel_index];
95 }
96
97 void List::button_press(int x, int y, unsigned btn)
98 {
99         if(slider->get_geometry().is_inside(x, y))
100         {
101                 const Geometry &sgeom=slider->get_geometry();
102                 slider->button_press(x-sgeom.x, y-sgeom.y, btn);
103                 slider_active=true;
104         }
105         else if(btn==1)
106         {
107                 const GL::Font *const font=style->get_font();
108                 const unsigned row_height=static_cast<unsigned>(font->get_default_size());
109
110                 if(items_part)
111                         y+=items_part->get_margin().top;
112
113                 unsigned i=(geom.h-1-y)/row_height;
114                 if(i<n_visible && first+i<items.size())
115                 {
116                         sel_index=first+i;
117
118                         signal_item_selected.emit(sel_index, items[sel_index]);
119                 }
120         }
121 }
122
123 void List::button_release(int x, int y, unsigned btn)
124 {
125         if(slider_active)
126         {
127                 const Geometry &sgeom=slider->get_geometry();
128                 slider->button_release(x-sgeom.x, y-sgeom.y, btn);
129                 slider_active=false;
130         }
131 }
132
133 void List::pointer_motion(int x, int y)
134 {
135         if(slider_active)
136         {
137                 const Geometry &sgeom=slider->get_geometry();
138                 slider->pointer_motion(x-sgeom.x, y-sgeom.y);
139         }
140 }
141
142 void List::render_special(const Part &part) const
143 {
144         if(part.get_name()=="items")
145         {
146                 const GL::Font *const font=style->get_font();
147                 const float font_size=font->get_default_size();
148                 const unsigned row_height=static_cast<unsigned>(font_size);
149                 const Sides &margin=part.get_margin();
150
151                 Geometry pgeom=geom;
152                 pgeom.h=row_height;
153                 pgeom.w-=margin.left+margin.right;
154
155                 for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
156                 {
157                         Geometry rgeom;
158                         rgeom.w=static_cast<unsigned>(font->get_string_width(items[first+i])*font_size);
159                         rgeom.h=static_cast<unsigned>((font->get_ascent()-font->get_descent())*font_size);
160                         rgeom.y=geom.h-margin.top-(i+1)*row_height-static_cast<int>(font->get_descent()*font_size);
161                         part.get_alignment().apply(rgeom, pgeom);
162
163                         GL::push_matrix();
164                         GL::translate(rgeom.x, rgeom.y, 0);
165                         GL::scale_uniform(font_size);
166                         font->draw_string(items[first+i]);
167                         GL::pop_matrix();
168                 }
169         }
170         else if(part.get_name()=="selection")
171         {
172                 if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
173                 {
174                         const GL::Font *const font=style->get_font();
175                         const float font_size=font->get_default_size();
176                         const unsigned row_height=static_cast<unsigned>(font_size);
177                         const Sides &margin=part.get_margin();
178
179                         Geometry pgeom=geom;
180                         pgeom.h=row_height;
181                         pgeom.w-=margin.left+margin.right;
182
183                         Geometry rgeom=part.get_geometry();
184                         rgeom.y+=geom.h-margin.top-row_height*(sel_index-first+1);
185                         rgeom.x+=margin.left;
186                         part.get_alignment().apply(rgeom, pgeom);
187
188                         GL::push_matrix();
189                         GL::translate(rgeom.x, rgeom.y, 0);
190                         part.get_graphic(state)->render(rgeom.w, rgeom.h);
191                         GL::pop_matrix();
192                 }
193         }
194         else if(part.get_name()=="slider")
195                 slider->render();
196 }
197
198 void List::on_geometry_change()
199 {
200         reposition_slider();
201
202         recalculate_parameters();
203 }
204
205 void List::on_style_change()
206 {
207         reposition_slider();
208
209         items_part=0;
210         for(list<Part>::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
211                 if(i->get_name()=="items")
212                         items_part=&*i;
213
214         recalculate_parameters();
215 }
216
217 void List::reposition_slider()
218 {
219         for(list<Part>::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i)
220                 if(i->get_name()=="slider")
221                 {
222                         Geometry sgeom=i->get_geometry();
223                         i->get_alignment().apply(sgeom, geom, i->get_margin());
224                         slider->set_geometry(sgeom);
225                 }
226 }
227
228 void List::recalculate_parameters()
229 {
230         const GL::Font *font=style->get_font();
231         unsigned row_height=static_cast<unsigned>(font->get_default_size());
232
233         unsigned h=geom.h;
234         if(items_part)
235         {
236                 const Sides &margin=items_part->get_margin();
237                 h-=margin.top+margin.bottom;
238         }
239
240         n_visible=h/row_height;
241
242         if(first+n_visible>items.size())
243         {
244                 if(items.size()>n_visible)
245                         first=items.size()-n_visible;
246                 else
247                         first=0;
248         }
249
250         if(items.size()>n_visible)
251         {
252                 slider->set_range(0, items.size()-n_visible);
253                 slider->set_value(items.size()-n_visible-first);
254         }
255         else
256         {
257                 slider->set_range(0, 0);
258                 slider->set_value(0);
259         }
260 }
261
262 void List::slider_value_changed(double value)
263 {
264         first=items.size()-n_visible-static_cast<unsigned>(value);
265 }
266
267
268 List::Loader::Loader(List &l):
269         Widget::Loader(l)
270 {
271         add("item", &Loader::item);
272 }
273
274 void List::Loader::item(const string &v)
275 {
276         static_cast<List &>(wdg).append(v);
277 }
278
279 } // namespace GLtk
280 } // namespace Msp