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