PageRenderTime 49ms CodeModel.GetById 21ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/cui/textfield.d

http://github.com/wilkie/djehuty
D | 210 lines | 131 code | 49 blank | 30 comment | 26 complexity | d62712edcbad227e4fe1f1c9f75a7269 MD5 | raw file
  1/*
  2 * textfield.d
  3 *
  4 * This module implements a text field widget for TUI applications.
  5 *
  6 * Author: Dave Wilkinson, Lindsey Bieda
  7 *
  8 */
  9
 10module cui.textfield;
 11
 12import djehuty;
 13
 14import io.console;
 15
 16import cui.widget;
 17
 18// Section: Console
 19
 20// Description: This console control abstracts a simple one line text field.
 21class CuiTextField : CuiWidget {
 22
 23	// Constructors
 24
 25	this( uint x, uint y, uint width, string value = "") {
 26		super(x,y,width,1);
 27
 28		_max = width-2;
 29
 30		_value = value.dup;
 31	}
 32
 33	// Events
 34
 35	override void onAdd() {
 36	}
 37
 38	override void onInit() {
 39		onDraw();
 40	}
 41
 42
 43	override void onKeyDown(Key key) {
 44
 45		Console.forecolor = _forecolor;
 46		Console.backcolor = _backcolor;
 47		if (key.code == Key.Backspace) {
 48			if (_pos > 0) {
 49				if (_pos == _max) {
 50					Console.put(' ');
 51					_value = _value.substring(0,_value.length-1);
 52				}
 53				else {
 54					Console.put(cast(char)0x8);
 55					Console.put(' ');
 56					_value = _value.substring(0, _pos-1) ~ _value.substring(_pos);
 57				}
 58
 59				Console.put(cast(char)0x8);
 60
 61				_pos--;
 62			}
 63		}
 64		else if (key.code == Key.Return) {
 65			tabForward();
 66		}
 67	}
 68
 69	override void onKeyChar(dchar keyChar) {
 70
 71		Console.forecolor = _forecolor;
 72		Console.backcolor = _backcolor;
 73		if (keyChar != 0x8 && keyChar != '\t' && keyChar != '\n' && keyChar != '\r') {
 74			if (_pos <= _max) {
 75				Console.put(keyChar);
 76
 77				_pos++;
 78
 79				if (_pos >= _max) {
 80					_pos = _max;
 81					_value = _value.substring(0, _value.length-1) ~ Unicode.toUtf8([keyChar]);
 82					Console.put(cast(char)(0x8));
 83				}
 84				else {
 85					_value = _value.substring(0, _pos-1) ~ Unicode.toUtf8([keyChar]) ~ _value.substring(_pos-1);
 86				}
 87			}
 88		}
 89	}
 90
 91	override void onGotFocus() {
 92		Console.showCaret();
 93
 94		positionCursor();
 95
 96		Console.forecolor = _forecolor;
 97		Console.backcolor = _backcolor;
 98	}
 99
100	// Properties
101
102	// Description: This property returns the current text within the field.
103	// Returns: The value of the field.
104	string text() {
105		return _value.dup;
106	}
107
108	// Description: This property sets the current text within the field.
109	// text: The new value for the field.
110	void text(string text) {
111		_value = text.dup;
112		onDraw();
113	}
114	
115	// Description: This property returns the current forecolor of the text in the field
116	// Returns: forecolor of text
117	Color forecolor() {
118		return _forecolor;
119	}
120	
121	// Description: This property sets the current forecolor of the text in the field
122	// value: The new forecolor
123	void forecolor(Color value) {
124		_forecolor = value;
125		onDraw();
126	}
127	
128	// Description: This property returns the current backcolor of the text in the field
129	// Returns: backcolor of text
130	Color backcolor() {
131		return _backcolor;
132	}
133	
134	// Description: This property sets the current backcolor of the text in the field
135	// value: The new backcolor
136	void backcolor(Color value) {
137		_backcolor = value;
138		onDraw();
139	}
140	
141	// Description: This property returns the current forecolor of the borders of the field
142	// Returns: forecolor of borders
143	Color basecolor() {
144		return _color;
145	}
146	
147	// Description: This property sets the current forecolor of the borders of the field
148	// value: The new forecolor of the borders
149	void basecolor(Color value) {
150		_color = value;
151	}
152
153	// Methods
154
155	override bool isTabStop() {
156		return true;
157	}
158
159	override void onDraw() {
160		if (canDraw) {
161			Console.position(this.left, this.top);
162			Console.forecolor = _color;
163			Console.backcolor = Color.Black;
164			Console.put("[");
165
166			Console.forecolor = _forecolor;
167			Console.backcolor = _backcolor;
168
169			foreach(chr; _value.substring(0,_max)) {
170				Console.put(chr);
171			}
172
173			_pos = _value.length;
174			if (_pos > _max) {
175				_pos = _max;
176			}
177
178			for (int i=_value.length; i<_max; i++) {
179				Console.put(' ');
180			}
181
182			Console.forecolor = _color;
183			Console.backcolor = Color.Black;
184
185			Console.put("]");
186
187			positionCursor();
188		}
189	}
190
191protected:
192
193	Color _color = Color.Blue;
194	Color _forecolor = Color.White;
195	Color _backcolor = Color.Black;
196
197	uint _pos = 0;
198	uint _max = 0;
199
200	string _value;
201
202	void positionCursor() {
203		if (_pos == _max) {
204			Console.position(this.left+_max, this.top);
205		}
206		else {
207			Console.position(this.left+1+_pos, this.top);
208		}
209	}
210}