]> git.tdb.fi Git - libs/gltk.git/blob - source/entry.cpp
Add an input method subsystem
[libs/gltk.git] / source / entry.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/texture.h>
4 #include <msp/input/keys.h>
5 #include "entry.h"
6 #include "graphic.h"
7 #include "part.h"
8 #include "style.h"
9 #include "vslider.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GLtk {
15
16 Entry::Entry(const string &t):
17         text(),
18         multiline(false),
19         edit_width(10),
20         edit_height(1),
21         edit_pos(0),
22         first_row(0),
23         visible_rows(1),
24         text_part(0),
25         slider(0),
26         got_key_press(false)
27 {
28         input_type = INPUT_TEXT;
29         set_text(t);
30 }
31
32 void Entry::autosize_special(const Part &part, Geometry &ageom) const
33 {
34         if(part.get_name()=="text")
35         {
36                 const Sides &margin = part.get_margin();
37                 const GL::Font &font = style->get_font();
38                 unsigned en_width = static_cast<unsigned>(font.get_string_width("n")*style->get_font_size());
39                 ageom.w = max(ageom.w, edit_width*en_width+margin.left+margin.right);
40
41                 unsigned line_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
42                 if(multiline)
43                 {
44                         unsigned line_spacing = style->get_font_size()*6/5;
45                         ageom.h = max(ageom.h, line_height+line_spacing*(edit_height-1)+margin.top+margin.bottom);
46                 }
47                 else
48                         ageom.h = max(ageom.h, line_height+margin.top+margin.bottom);
49         }
50         else if(part.get_name()=="slider" && multiline)
51                 autosize_child(*slider, part, ageom);
52 }
53
54 void Entry::set_text(const string &t)
55 {
56         text = t;
57         edit_pos = text.size();
58
59         if(multiline)
60                 check_view_range();
61
62         rebuild();
63 }
64
65 void Entry::set_edit_size(unsigned w, unsigned h)
66 {
67         edit_width = w;
68         edit_height = h;
69         signal_autosize_changed.emit();
70 }
71
72 void Entry::set_multiline(bool m)
73 {
74         multiline = m;
75         if(multiline)
76         {
77                 if(!slider)
78                 {
79                         slider = new VSlider;
80                         add(*slider);
81                         slider->set_step(1);
82                         slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed));
83                         rebuild();
84                 }
85                 check_view_range();
86         }
87 }
88
89 void Entry::rebuild_special(const Part &part)
90 {
91         if(part.get_name()=="text")
92                 text.build(part, state, geom, first_row, part_cache);
93         else if(part.get_name()=="cursor")
94         {
95                 const Graphic *graphic = part.get_graphic(state);
96                 if(!text_part || !graphic || !graphic->get_texture())
97                         return;
98
99                 unsigned row, col;
100                 text.offset_to_coords(edit_pos, row, col);
101
102                 if(row<first_row || row>=first_row+visible_rows)
103                         return;
104
105                 Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col);
106
107                 GL::MeshBuilder bld(part_cache.create_mesh(part, *graphic->get_texture()));
108                 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
109                 graphic->build(part.get_geometry().w, part.get_geometry().h, bld);
110         }
111         else if(part.get_name()=="slider")
112         {
113                 if(multiline)
114                 {
115                         reposition_child(*slider, part);
116                         Widget::rebuild_special(part);
117                 }
118         }
119         else
120                 Widget::rebuild_special(part);
121 }
122
123 void Entry::render_special(const Part &part, GL::Renderer &renderer) const
124 {
125         if(part.get_name()=="slider" && multiline)
126                 slider->render(renderer);
127 }
128
129 bool Entry::key_press(unsigned key, unsigned)
130 {
131         got_key_press = true;
132         if(key==Input::KEY_LEFT)
133         {
134                 if(edit_pos>0)
135                         set_edit_position(edit_pos-1);
136         }
137         else if(key==Input::KEY_RIGHT)
138         {
139                 if(edit_pos<text.size())
140                         set_edit_position(edit_pos+1);
141         }
142         else if(key==Input::KEY_DOWN && multiline)
143         {
144                 unsigned row, col;
145                 text.offset_to_coords(edit_pos, row, col);
146                 set_edit_position(text.coords_to_offset(row+1, col));
147         }
148         else if(key==Input::KEY_UP && multiline)
149         {
150                 unsigned row, col;
151                 text.offset_to_coords(edit_pos, row, col);
152                 set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
153         }
154         else if(key==Input::KEY_BACKSPACE)
155         {
156                 if(edit_pos>0)
157                 {
158                         text.erase(--edit_pos, 1);
159                         check_view_range();
160                         rebuild();
161                 }
162         }
163         else if(key==Input::KEY_ENTER)
164         {
165                 if(multiline)
166                 {
167                         text.insert(edit_pos++, "\n");
168                         check_view_range();
169                         rebuild();
170                 }
171                 else
172                         signal_enter.emit();
173         }
174         else
175                 return false;
176
177         return true;
178 }
179
180 bool Entry::character(wchar_t ch)
181 {
182         if(got_key_press && ch>=' ')
183         {
184                 text.insert(edit_pos, StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
185                 ++edit_pos;
186                 rebuild();
187                 return true;
188         }
189
190         return false;
191 }
192
193 void Entry::focus_out()
194 {
195         Widget::focus_out();
196         got_key_press = false;
197 }
198
199 void Entry::on_geometry_change()
200 {
201         if(multiline)
202                 check_view_range();
203 }
204
205 void Entry::on_style_change()
206 {
207         text.set_style(style);
208
209         if(!style)
210         {
211                 text_part = 0;
212                 return;
213         }
214
215         text_part = style->get_part("text");
216
217         if(multiline)
218                 check_view_range();
219 }
220
221 void Entry::set_edit_position(unsigned ep)
222 {
223         edit_pos = ep;
224         check_view_range();
225         rebuild();
226 }
227
228 void Entry::check_view_range()
229 {
230         if(!multiline || !text_part)
231                 return;
232
233         float font_size = style->get_font_size();
234         unsigned line_spacing = static_cast<unsigned>(font_size*6/5);
235
236         const Sides &margin = text_part->get_margin();
237         visible_rows = max((geom.h-margin.top-margin.bottom)/line_spacing, 1U);
238
239         unsigned row, col;
240         text.offset_to_coords(edit_pos, row, col);
241
242         if(first_row>row)
243                 first_row = row;
244         else if(row>=first_row+visible_rows)
245                 first_row = row+1-visible_rows;
246
247         unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows;
248         slider->set_range(0, scroll);
249         slider->set_value(scroll-first_row);
250 }
251
252 void Entry::slider_value_changed(double value)
253 {
254         if(text.get_n_lines()>visible_rows)
255                 first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
256 }
257
258
259 Entry::Loader::Loader(Entry &e):
260         DataFile::DerivedObjectLoader<Entry, Widget::Loader>(e)
261 {
262         add("edit_size", &Entry::edit_width, &Entry::edit_height);
263         add("multiline", &Loader::multiline);
264 }
265
266 void Entry::Loader::multiline(bool m)
267 {
268         obj.set_multiline(m);
269 }
270
271 } // namespace GLtk
272 } // namespace Msp