/
textfield.d
210 lines (161 loc) · 4.04 KB
/
textfield.d
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
/*
* textfield.d
*
* This module implements a text field widget for TUI applications.
*
* Author: Dave Wilkinson, Lindsey Bieda
*
*/
module cui.textfield;
import djehuty;
import io.console;
import cui.widget;
// Section: Console
// Description: This console control abstracts a simple one line text field.
class CuiTextField : CuiWidget {
// Constructors
this( uint x, uint y, uint width, string value = "") {
super(x,y,width,1);
_max = width-2;
_value = value.dup;
}
// Events
override void onAdd() {
}
override void onInit() {
onDraw();
}
override void onKeyDown(Key key) {
Console.forecolor = _forecolor;
Console.backcolor = _backcolor;
if (key.code == Key.Backspace) {
if (_pos > 0) {
if (_pos == _max) {
Console.put(' ');
_value = _value.substring(0,_value.length-1);
}
else {
Console.put(cast(char)0x8);
Console.put(' ');
_value = _value.substring(0, _pos-1) ~ _value.substring(_pos);
}
Console.put(cast(char)0x8);
_pos--;
}
}
else if (key.code == Key.Return) {
tabForward();
}
}
override void onKeyChar(dchar keyChar) {
Console.forecolor = _forecolor;
Console.backcolor = _backcolor;
if (keyChar != 0x8 && keyChar != '\t' && keyChar != '\n' && keyChar != '\r') {
if (_pos <= _max) {
Console.put(keyChar);
_pos++;
if (_pos >= _max) {
_pos = _max;
_value = _value.substring(0, _value.length-1) ~ Unicode.toUtf8([keyChar]);
Console.put(cast(char)(0x8));
}
else {
_value = _value.substring(0, _pos-1) ~ Unicode.toUtf8([keyChar]) ~ _value.substring(_pos-1);
}
}
}
}
override void onGotFocus() {
Console.showCaret();
positionCursor();
Console.forecolor = _forecolor;
Console.backcolor = _backcolor;
}
// Properties
// Description: This property returns the current text within the field.
// Returns: The value of the field.
string text() {
return _value.dup;
}
// Description: This property sets the current text within the field.
// text: The new value for the field.
void text(string text) {
_value = text.dup;
onDraw();
}
// Description: This property returns the current forecolor of the text in the field
// Returns: forecolor of text
Color forecolor() {
return _forecolor;
}
// Description: This property sets the current forecolor of the text in the field
// value: The new forecolor
void forecolor(Color value) {
_forecolor = value;
onDraw();
}
// Description: This property returns the current backcolor of the text in the field
// Returns: backcolor of text
Color backcolor() {
return _backcolor;
}
// Description: This property sets the current backcolor of the text in the field
// value: The new backcolor
void backcolor(Color value) {
_backcolor = value;
onDraw();
}
// Description: This property returns the current forecolor of the borders of the field
// Returns: forecolor of borders
Color basecolor() {
return _color;
}
// Description: This property sets the current forecolor of the borders of the field
// value: The new forecolor of the borders
void basecolor(Color value) {
_color = value;
}
// Methods
override bool isTabStop() {
return true;
}
override void onDraw() {
if (canDraw) {
Console.position(this.left, this.top);
Console.forecolor = _color;
Console.backcolor = Color.Black;
Console.put("[");
Console.forecolor = _forecolor;
Console.backcolor = _backcolor;
foreach(chr; _value.substring(0,_max)) {
Console.put(chr);
}
_pos = _value.length;
if (_pos > _max) {
_pos = _max;
}
for (int i=_value.length; i<_max; i++) {
Console.put(' ');
}
Console.forecolor = _color;
Console.backcolor = Color.Black;
Console.put("]");
positionCursor();
}
}
protected:
Color _color = Color.Blue;
Color _forecolor = Color.White;
Color _backcolor = Color.Black;
uint _pos = 0;
uint _max = 0;
string _value;
void positionCursor() {
if (_pos == _max) {
Console.position(this.left+_max, this.top);
}
else {
Console.position(this.left+1+_pos, this.top);
}
}
}