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