Repository navigation
Expand file tree
/
Copy pathTextEntry.vala
More file actions
424 lines (387 loc) · 15.2 KB
/
Copy pathTextEntry.vala
File metadata and controls
424 lines (387 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* ev3devKit - ev3dev toolkit for LEGO MINDSTORMS EV3
*
* Copyright 2014 David Lechner <david@lechnology.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/* TextEntry.vala - Widget for getting text from user */
using Grx;
namespace Ev3devKit.Ui {
/**
* A wiget for getting user input.
*/
public class TextEntry : Ev3devKit.Ui.Widget {
const string CONTINUE_RIGHT = "\xbb";
const string CONTINUE_LEFT = "\xab";
/**
* String containing all numeric characters (``0-9``).
*/
public const string NUMERIC = "0123456789";
/**
* String containing numeric characters (``0-9``) and a decimal point (``.``).
*/
public const string DECIMAL = NUMERIC + ".";
/**
* String containing hexadecimal numeric characters (``0-9`` and ``A-F``).
*/
public const string HEXIDECIMAL = NUMERIC + "ABCDEF";
/**
* String containing uppercase alphabetic characters (``A-Z``).
*/
public const string UPPER_ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* String containing lowercase alphabetic characters (``a-z``).
*/
public const string LOWER_ALPHA = "abcdefghijklmnopqrstuvwxyz";
/**
* String containing all alphabetic characters (``a-z`` and ``A-Z``).
*/
public const string ALPHA = LOWER_ALPHA + UPPER_ALPHA;
/**
* String containing alphanumeric characters (``a-z``, ``A-Z``, ``0-9`` and ``.-_<space>``).
*/
public const string ALPHA_NUM = ALPHA + DECIMAL + "-_ ";
/**
* String containing symbol characters (``!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~``).
*/
public const string SYMBOL = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
/**
* String containing all 95 printable ASCII characters.
*/
public const string ALPHA_NUM_SYMBOL = ALPHA + NUMERIC + SYMBOL + " ";
Label label;
int text_offset; // chars
internal int cursor_offset; // chars
int cursor_x; // pixels
bool should_inc_text_offset_with_cursor;
string save_text;
OnScreenKeyboard on_screen_keyboard;
/**
* Gets and sets the test displayed in the text entry.
*/
public string text { get; set; }
/**
* Gets and sets the font used in the text entry.
*/
public unowned Font font {
get {return label.font; }
set { label.font = value; }
}
/**
* Gets and sets the minimum width for the text entry.
*/
public int min_width { get; set; default = 10; }
/**
* Gets and sets the edit-ability of the text entry.
*/
public bool can_edit { get; set; default = true; }
/**
* Gets and sets whether the text entry should use an on screen keyboard.
*/
public bool use_on_screen_keyboard { get; set; default = true; }
/**
* Gets the current editing state.
*/
public bool editing { get; internal set; default = false; }
/**
* Gets and sets a string containing valid characters for the text entry.
*
* This limits the number of characters that have to be scrolled when
* not using the on screen keyboard.
*/
public string valid_chars { get; set; default = ALPHA_NUM_SYMBOL; }
/**
* Gets and sets whether the text entry inserts or replaces text.
*/
public bool insert { get; set; default = false; }
construct {
label = new Label () {
text_horizontal_align = TextHAlign.LEFT,
text_vertical_align = TextVAlign.TOP,
font = Fonts.get_default ()
};
can_focus = true;
border = 1;
padding = 2;
vertical_align = WidgetAlign.CENTER;
notify["text"].connect (redraw);
notify["min-width"].connect (redraw);
notify["editing"].connect (redraw);
notify["parent"].connect (() => label.parent = this.parent);
}
/**
* Creates a new text entry.
*
* @param text The initial text.
*/
public TextEntry (string text = "") {
this.text = text;
}
/**
* Starts an editing session.
*
* If the text entry uses an on screen keyboard, it will be displayed.
* Otherwise, a cursor will be displayed. The left and right arrow keys
* move the cursor and the up and down arrow keys change the character
* above the cursor using the list of {@link valid_chars}. Either
* {@link commit_editing} or {@link cancel_editing} must be called to
* end the editing session.
*/
public void start_editing () {
if (_editing)
return;
save_text = text;
editing = true;
if (_use_on_screen_keyboard) {
on_screen_keyboard = new OnScreenKeyboard ();
on_screen_keyboard.text = text;
on_screen_keyboard.accepted.connect (commit_editing);
on_screen_keyboard.canceled.connect (cancel_editing);
on_screen_keyboard.show ();
}
}
/**
* Commits the changes made in an editing session.
*
* An editing session is started by calling {@link start_editing}.
*/
public void commit_editing () {
if (!_editing)
return;
editing = false;
if (_use_on_screen_keyboard) {
text = on_screen_keyboard.text;
on_screen_keyboard = null;
}
}
/**
* Cancels the changes made in an editing session.
*
* An editing session is started by calling {@link start_editing}.
* {@link text} will be reverted to the value saved when
* {@link start_editing} was called.
*/
public void cancel_editing () {
if (!_editing)
return;
editing = false;
text = save_text;
if (_use_on_screen_keyboard)
on_screen_keyboard = null;
}
/**
* {@inheritDoc}
*/
protected override int get_preferred_width () {
return int.max (min_width, font.get_text_width (text ?? "")
+ get_margin_border_padding_width ());
}
/**
* {@inheritDoc}
*/
protected override int get_preferred_height () {
return label.get_preferred_height () + get_margin_border_padding_height ();
}
/**
* Sets the character at the current cursor position.
*
* @param c The new character.
* @param move_next If true, the cursor will be moved to the next character.
*/
public void set_char (char c, bool move_next = false) {
if (!_editing)
return;
if (!(c.to_string() in valid_chars))
return;
var builder = new StringBuilder (text);
if (_insert)
builder.insert (cursor_offset, c.to_string ());
else
builder.overwrite (cursor_offset, c.to_string ());
text = builder.str;
if (move_next)
cursor_offset++;
redraw ();
}
/**
* Removes the character at the current cursor position.
*
* @param backspace If true deletes the character before the cursor instead.
*/
public void delete_char (bool backspace = false) {
if (!_editing)
return;
if (text.length == 0 || (backspace && cursor_offset == 0)
|| (!backspace && cursor_offset == text.length - 1))
return;
var offset = cursor_offset - (backspace ? 1 : 0);
var builder = new StringBuilder (text);
builder.erase (offset, 1);
text = builder.str;
cursor_offset = offset;
redraw ();
}
/**
* Increments or decrements the character at the current cursor position.
*
* Uses {@link valid_chars} to determine the next character.
*
* @param dec If true, decrements instead of increments.
*/
void inc_char (bool dec = false) {
var new_char = text.get (cursor_offset);
var next_index = _valid_chars.index_of_char (new_char) + (dec ? -1 : 1);
// wraparound
next_index = (_valid_chars.length + next_index) % _valid_chars.length;
new_char = _valid_chars.get (next_index);
set_char (new_char);
}
/**
* Default handler for the key_pressed signal.
*/
internal override bool key_pressed (uint key_code) {
if (_editing) {
if (key_code == Key.UP)
inc_char ();
else if (key_code == Key.DOWN)
inc_char (true);
else if (key_code == Key.RIGHT) {
cursor_offset ++;
if (should_inc_text_offset_with_cursor)
text_offset++;
} else if (key_code == Key.LEFT)
cursor_offset--;
else if (key_code == Key.RETURN)
commit_editing ();
else if (key_code == Key.BACK_SPACE)
cancel_editing ();
else if (key_code >= 32 && key_code < 127)
set_char ((char)key_code, true);
else
return false;
redraw ();
Signal.stop_emission_by_name (this, "key-pressed");
return true;
} else {
if (key_code == Key.RIGHT)
text_offset ++;
else if (key_code == Key.LEFT)
text_offset--;
else if (_can_edit && key_code == Key.RETURN)
start_editing ();
else
return base.key_pressed (key_code);
redraw ();
Signal.stop_emission_by_name (this, "key-pressed");
return true;
}
}
void set_label_text () {
var builder = new StringBuilder ();
var continuation_offset = 0;
should_inc_text_offset_with_cursor = false;
// don't need to do fancy calculations if we are not out of bounds.
if (font.get_text_width (text ?? "") <= content_bounds.width) {
label.text = text;
text_offset = 0;
} else {
// FIXME: draw elipsis (...) or arrows instead of using text for contrinue left/right
if (!editing && text_offset > CONTINUE_LEFT.length)
cursor_offset = text_offset;
if (editing && cursor_offset < text_offset)
text_offset = cursor_offset;
text_offset = int.max (CONTINUE_LEFT.length, text_offset);
builder.append (CONTINUE_LEFT);
var max_text_offset = text.length - 1;
while (max_text_offset > 0
&& font.get_text_width (builder.str) < content_bounds.width)
builder.append_c (text[max_text_offset--]);
max_text_offset += CONTINUE_LEFT.length + 1;
text_offset = int.min (text_offset, max_text_offset);
var cursor_out_of_range = true;
while (cursor_out_of_range) {
cursor_out_of_range = false;
builder.erase ();
if (text_offset > CONTINUE_LEFT.length)
builder.append (CONTINUE_LEFT);
else
builder.append (text[0:CONTINUE_LEFT.length]);
continuation_offset = CONTINUE_LEFT.length;
var index = text_offset;
while (index < text.length
&& font.get_text_width (builder.str) < content_bounds.width)
builder.append_c (text[index++]);
if (index < text.length
|| font.get_text_width (builder.str) > content_bounds.width)
{
while (index > 0
&& font.get_text_width (builder.str + CONTINUE_RIGHT)
> content_bounds.width)
{
builder.truncate (builder.len - 1);
}
if (cursor_offset - text_offset > builder.len - 2) {
text_offset++;
cursor_out_of_range = true;
continue;
} else if (cursor_offset - text_offset == builder.len - 2)
should_inc_text_offset_with_cursor = true;
builder.append (CONTINUE_RIGHT);
}
}
label.text = builder.str;
}
cursor_offset = int.max (0, cursor_offset);
cursor_offset = int.min (cursor_offset, text.length - 1);
cursor_x = content_bounds.x1;
if (text.length > 0)
cursor_x += font.get_text_width (
label.text[0:continuation_offset + cursor_offset - text_offset]);
}
/**
* {@inheritDoc}
*/
protected override void redraw () {
label.text = null;
base.redraw ();
}
/**
* {@inheritDoc}
*/
protected override void do_layout () {
label.set_bounds (content_bounds.x1, content_bounds.y1,
content_bounds.x2, content_bounds.y2);
if (text != null && label.text == null)
set_label_text ();
}
/**
* {@inheritDoc}
*/
protected override void draw_content () {
var color = has_focus ? window.screen.mid_color : window.screen.fg_color;
label.draw ();
if (editing) {
draw_hline (cursor_x,
cursor_x + font.width, content_bounds.y2 + 1, color);
}
if (has_focus && !editing) {
draw_box (border_bounds.x1 + 1, border_bounds.y1 + 1,
border_bounds.x2 - 1, border_bounds.y2 -1, color);
}
}
}
}