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