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