PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/cui/container.d

http://github.com/wilkie/djehuty
D | 320 lines | 228 code | 68 blank | 24 comment | 42 complexity | 8518bff37a0494e0cb85e93b184fbca8 MD5 | raw file
  1/*
  2 * container.d
  3 *
  4 * This module implements a widget container for CuiWidget.
  5 *
  6 * Author: Dave Wilkinson
  7 * Originated: August 20th 2009
  8 *
  9 */
 10
 11module cui.container;
 12
 13import cui.widget;
 14
 15import djehuty;
 16
 17private import io.console;
 18
 19class CuiContainer : CuiWidget {
 20	this(uint x, uint y, uint width, uint height) {
 21		_name = "";
 22		super(x,y,width,height);
 23	}
 24
 25	override void onInit() {
 26		_inited = true;
 27		CuiWidget c = _firstControl;
 28
 29		if (c is null) { return; }
 30
 31		do {
 32			c =	c._prevControl;
 33
 34			c.onInit();
 35			//if ()
 36			//c.onDraw();
 37		} while (c !is _firstControl)
 38	}
 39
 40	override void onGotFocus() {
 41		if (_focusedControl is null) {
 42			CuiWidget c = _firstControl;
 43
 44			if (c is null) { return; }
 45
 46			do {
 47				c = c._prevControl;
 48				if (c.isTabStop()) {
 49					_focusedControl = c;
 50					_focusedControl.onGotFocus();
 51					break;
 52				}
 53			} while (c !is _firstControl);
 54		}
 55		else {
 56			_focusedControl.onGotFocus();
 57		}
 58	}
 59
 60	override void onLostFocus() {
 61		if (_focusedControl !is null) {
 62			_focusedControl.onLostFocus();
 63		}
 64	}
 65
 66	override void onDraw() {
 67		// Go through child widget list and draw each one
 68		Console.position(0,0);
 69
 70		CuiWidget c = _firstControl;
 71
 72		io.console.Console.clipSave();
 73
 74		if (c !is null) {
 75			do {
 76				c =	c._prevControl;
 77
 78				io.console.Console.clipSave();
 79				this.widgetClippingContext = c;
 80				c.onDraw();
 81				io.console.Console.clipRestore();
 82				io.console.Console.clipRect(_base_x + this.left + c.left, _base_y + this.top + c.top, _base_x + this.left + c.left + c.width, _base_y + this.top + c.top + c.height);
 83			} while(c !is _firstControl);
 84		}
 85
 86		// Should clear the rest of the space not used by a widget
 87		static string spaces = "                                                                                                                  ";
 88
 89		Console.backcolor = Color.Gray;
 90		for (uint i; i < this.height; i++) {
 91			Console.position(0,i);
 92			io.console.Console.backcolor = Color.Black;
 93			uint numSpaces = this.width;
 94
 95			do {
 96				uint pad = spaces.length;
 97				if (numSpaces < pad) {
 98					pad = numSpaces;
 99				}
100				io.console.Console.put(spaces[0..pad]);
101				numSpaces -= pad;
102			} while (numSpaces > 0)
103		}
104		io.console.Console.clipRestore();
105	}
106
107	override void onKeyDown(Key key) {
108		if (_focusedControl !is null) {
109			_focusedControl.onKeyDown(key);
110		}
111	}
112
113	override void onKeyChar(dchar chr) {
114		if (_focusedControl !is null) {
115			_focusedControl.onKeyChar(chr);
116		}
117	}
118
119	override void onPrimaryMouseDown() {
120		if (_focusedControl !is null) {
121			_focusedControl.onPrimaryMouseDown();
122		}
123	}
124
125	override void onSecondaryMouseDown() {
126		if (_focusedControl !is null) {
127			_focusedControl.onSecondaryMouseDown();
128		}
129	}
130	
131	override void onTertiaryMouseDown() {
132		if (_focusedControl !is null) {
133			_focusedControl.onTertiaryMouseDown();
134		}
135	}
136	
137	override void onPrimaryMouseUp() {
138		if (_focusedControl !is null) {
139			_focusedControl.onPrimaryMouseUp();
140		}
141	}
142
143	override void onSecondaryMouseUp() {
144		if (_focusedControl !is null) {
145			_focusedControl.onSecondaryMouseUp();
146		}
147	}
148
149	override void onTertiaryMouseUp() {
150		if (_focusedControl !is null) {
151			_focusedControl.onTertiaryMouseUp();
152		}
153	}
154	
155	override void onMouseWheelY(int amount) {
156		if (_focusedControl !is null) {
157			_focusedControl.onMouseWheelY(amount);
158		}
159	}
160	
161	override void onMouseWheelX(int amount) {
162		if (_focusedControl !is null) {
163			_focusedControl.onMouseWheelX(amount);
164		}
165	}
166
167	override void onMouseMove() {
168		if (_focusedControl !is null) {
169			_focusedControl.onMouseMove();
170		}
171	}
172
173	override void push(Dispatcher dsp) {
174		if (cast(CuiWidget)dsp) {
175			// do not add a control that is already part of another window
176			CuiWidget control = cast(CuiWidget)dsp;
177
178			if (control._nextControl !is null) {
179				return;
180			}
181
182			// add to the control linked list
183			if (_firstControl is null && _lastControl is null) {
184				// first control
185
186				_firstControl = control;
187				_lastControl = control;
188
189				control._nextControl = control;
190				control._prevControl = control;
191			}
192			else {
193				// next control
194
195				control._nextControl = _firstControl;
196				control._prevControl = _lastControl;
197
198				_firstControl._prevControl = control;
199				_lastControl._nextControl = control;
200
201				_firstControl = control;
202			}
203
204			// increase the number of controls
205			_numControls++;
206
207			Responder.push(dsp);
208
209			control._owner = this;
210			control._window = _window;
211			control._base_x = _base_x + this.left;
212			control._base_y = _base_y + this.top;
213
214			_isTabStop |= control.isTabStop();
215
216			// Call event
217			control.onAdd();
218
219			if (_inited) {
220				control.onInit();
221			}
222			return;
223		}
224		super.push(dsp);
225	}
226
227	override bool isTabStop() {
228		return _isTabStop;
229	}
230	
231	override void move(uint x, uint y) {
232		// Must report to each control
233		_reportMove(x, y);
234
235		super.move(x,y);
236	}
237	
238	void text(string name) {
239		_name = name.dup;
240	}
241
242	string text() {
243		return _name.dup;
244	}
245
246protected:
247
248	// head and tail of the control linked list
249	CuiWidget _firstControl;	//head
250	CuiWidget _lastControl;		//tail
251
252	// currently focused control
253	CuiWidget _focusedControl;
254
255	uint _numControls;
256
257	bool _inited;
258	bool _isTabStop;
259	
260	string _name;
261
262	void _reportMove(uint x, uint y) {
263		CuiWidget c = _firstControl;
264
265		if (c is null) {
266			super.move(x,y);
267			return;
268		}
269
270		do {
271			c = c._prevControl;
272			c._base_x = _base_x + x;
273			c._base_y = _base_y + y;
274			if (cast(CuiContainer)c) {
275				CuiContainer container = cast(CuiContainer)c;
276				container._reportMove(c.left, c.top);
277			}
278		} while (c !is _firstControl);
279	}
280
281	package void _tabForward() {
282		// activate the next control
283		CuiWidget curFocus = _focusedControl;
284
285		if (curFocus is null) { return; }
286
287		_focusedControl.onLostFocus();
288
289		do {
290			_focusedControl = _focusedControl._prevControl;
291			if (_focusedControl is _firstControl && _owner !is null) {
292				_owner._tabForward();
293			}
294			if (_focusedControl.isTabStop()) {
295				_focusedControl.onGotFocus();
296				break;
297			}
298		} while (_focusedControl !is curFocus);
299	}
300
301	package void _tabBackward() {
302		// activate the previous control
303		CuiWidget curFocus = _focusedControl;
304
305		if (curFocus is null) { return; }
306
307		_focusedControl.onLostFocus();
308
309		do {
310			_focusedControl = _focusedControl._nextControl;
311			if (_focusedControl is _firstControl && _owner !is null) {
312				_owner._tabBackward();
313			}
314			if (_focusedControl.isTabStop()) {
315				_focusedControl.onGotFocus();
316				break;
317			}
318		} while (_focusedControl !is curFocus);
319	}
320}