PageRenderTime 41ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/gui/togglefield.d

http://github.com/wilkie/djehuty
D | 282 lines | 187 code | 79 blank | 16 comment | 25 complexity | 5d3034bcd09fb627b28ff2b64d3d7c45 MD5 | raw file
  1/*
  2 * togglefield.d
  3 *
  4 * This module implements a GUI widget for an optional selection.
  5 *
  6 * Author: Dave Wilkinson
  7 *
  8 */
  9
 10module gui.togglefield;
 11
 12import gui.widget;
 13
 14import core.color;
 15import core.definitions;
 16import core.string;
 17
 18import graphics.graphics;
 19
 20template ControlPrintCSTRList() {
 21	const char[] ControlPrintCSTRList = `
 22	this(int x, int y, int width, int height, String value) {
 23		super(x,y,width,height,value);
 24	}
 25	this(int x, int y, int width, int height, string value) {
 26		super(x,y,width,height,value);
 27	}
 28	`;
 29}
 30
 31// Description: This control provides a standard toggle field.  When grouped, these will act as a exclusive list of options, essentially a 'radio' or 'option' field.  Otherwise they are 'check' fields.
 32class ToggleField : Widget {
 33
 34	enum Signal : uint {
 35		Selected,
 36		Unselected,
 37	}
 38
 39	this(int x, int y, int width, int height, string value) {
 40		super(x,y,width,height);
 41
 42		_value = value.dup;
 43	}
 44
 45	void unselect() {
 46		_btnstate = 0;
 47	}
 48
 49	void select() {
 50		_btnstate = 1;
 51	}
 52
 53	void text(string newTitle) {
 54		_value = newTitle.dup;
 55	}
 56
 57	string text() {
 58		return _value.dup;
 59	}
 60
 61	// handle events
 62	override void onAdd() {
 63		_brsh = new Brush(Color.White);
 64
 65		_clroutline = Color.fromRGB(0x80, 0x80, 0x80);
 66		_clrhighlight = Color.fromRGB(0xdd,0xdd,0xdd);
 67		_clrnormal = Color.fromRGB(0xaa,0xaa,0xaa);
 68		_clrforeground = Color.fromRGB(0,0,0);
 69		_clrbackground = Color.fromRGB(0xff,0xff,0xff);
 70
 71		Graphics grp = _view.lock();
 72
 73		_font = new Font(FontSans, 8, 400, false, false, false);
 74
 75		grp.font = _font;
 76		grp.measureText(_value,_valueBounds);
 77
 78		_view.unlock();
 79
 80		//FIRE_EVENT(id,EventCreated,0,0);
 81	}
 82
 83	override void onDraw(ref Graphics g) {
 84		//Draw Background of Button
 85		Brush brush;
 86		Pen pen;
 87
 88		Rect chkRect;
 89
 90		chkRect.left = this.left + 2;
 91		chkRect.top = this.top + 2;
 92		chkRect.right = (chkRect.left + this.height) - 4;
 93		chkRect.bottom = this.bottom - 2;
 94
 95		if (_is_grouped) {
 96			if (chkRect.right > this.right) {
 97				chkRect.right = this.right;
 98				chkRect.top += (this.height - this.width) / 2;
 99				chkRect.bottom = chkRect.top + this.width;
100			}
101
102			brush = new Brush(_clrbackground);
103			pen = new Pen(_clroutline);
104
105			g.brush = brush;
106			g.pen = pen;
107
108			g.drawRect(chkRect.left, chkRect.top, chkRect.right-chkRect.left, chkRect.bottom-chkRect.top);
109
110			pen.setColor(_clrhighlight);
111
112			g.drawLine(chkRect.left+1, chkRect.top+1, chkRect.right - 1, chkRect.top+1);
113			g.drawLine(chkRect.left+1, chkRect.top+2, chkRect.left + 1, chkRect.bottom-1);
114
115			//Draw Check
116
117			if (_mouseholdstate == 1) {
118				pen.setColor(_clrnormal);
119
120				brush.setColor(_clrbackground);
121
122				g.drawOval(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
123			}
124			else if (_btnstate == 1) {
125				brush.setColor(_clrnormal);
126
127				pen.setColor(_clrnormal);
128
129				g.drawOval(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom - 3);
130
131				if (_hovered) {
132					pen.setColor(_clrhighlight);
133
134					g.drawOval(chkRect.left + 4, chkRect.top + 4, chkRect.right - 4, chkRect.bottom-4);
135				}
136
137			}
138			else if (_hovered) {
139				pen.setColor(_clrhighlight);
140				brush.setColor(_clrbackground);
141
142				g.drawOval(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
143			}
144		}
145		else {
146			//Draw Background of Button
147
148			chkRect.left = this.left + 2;
149			chkRect.top = this.top + 2;
150			chkRect.right = (chkRect.left + this.height) - 4;
151			chkRect.bottom = this.bottom - 2;
152
153			if (chkRect.right > this.right) {
154				chkRect.right = this.right;
155				chkRect.top += (this.height - this.width) / 2;
156				chkRect.bottom = chkRect.top + this.width;
157			}
158
159			pen = new Pen(_clroutline);
160			brush = new Brush(_clrbackground);
161
162			g.brush = brush;
163			g.pen = pen;
164
165			g.drawRect(chkRect.left, chkRect.top, chkRect.right-chkRect.left, chkRect.bottom-chkRect.top);
166
167			pen.setColor(_clrhighlight);
168
169			g.drawLine(chkRect.left+1, chkRect.top+1, chkRect.right - 1, chkRect.top+1);
170			g.drawLine(chkRect.left+1, chkRect.top+2, chkRect.left + 1, chkRect.bottom-1);
171
172			//Draw Check
173
174			if (_mouseholdstate == 1) {
175				pen.setColor(_clrnormal);
176				brush.setColor(_clrbackground);
177
178				g.drawRect(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
179			}
180			else if (_btnstate == 1) {
181				brush.setColor(_clrnormal);
182				pen.setColor(_clrnormal);
183
184				g.drawRect(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom - 3);
185
186				if (_hovered) {
187					pen.setColor(_clrhighlight);
188
189					g.drawRect(chkRect.left + 4, chkRect.top + 4, chkRect.right - 4, chkRect.bottom-4);
190				}
191
192			}
193			else if (_hovered) {
194				pen.setColor(_clrhighlight);
195				brush.setColor(_clrbackground);
196
197				g.drawRect(chkRect.left + 3, chkRect.top + 3, chkRect.right - 3, chkRect.bottom-3);
198			}
199		}
200
201		//Draw the text
202		g.forecolor = _clrforeground;
203		g.setTextModeTransparent();
204		g.font = _font;
205		Rect ctrlrt;
206		ctrlrt.left = chkRect.right;
207		ctrlrt.right = this.right;
208		ctrlrt.top = this.top;
209		ctrlrt.bottom = this.bottom;
210		g.drawClippedText(chkRect.right + 4, (this.bottom + this.top-_valueBounds.y)/2, ctrlrt, _value);
211	}
212
213	override bool onPrimaryMouseDown(ref Mouse mouseProps) {
214		if (!_enabled) { return false; }
215
216		_mouseholdstate = 1;
217
218		requestCapture();
219		return true;
220	}
221
222	override bool onPrimaryMouseUp(ref Mouse mouseProps) {
223		if (!_enabled) { return false; }
224
225		requestRelease();
226
227		_mouseholdstate = 0;
228
229		if (_hovered) {
230			if (!(_is_grouped && _btnstate)) {
231				_btnstate = !_btnstate;
232				raiseSignal(Signal.Selected);
233			}
234		}
235
236		return true;
237	}
238
239	override bool onMouseEnter() {
240		Graphics g = _view.lock();
241
242		_brsh.setColor(Color.White);
243
244		_view.unlock();
245
246		return true;
247	}
248
249	override bool onMouseLeave() {
250		Graphics g = _view.lock();
251
252		Color c;
253		c.fromRGB(0xc8, 0xc8, 0xc8);
254
255		_brsh.setColor(c);
256
257		_view.unlock();
258
259		return true;
260	}
261
262private:
263	string _value;
264
265	package bool _is_grouped = false;
266
267	Brush _brsh;
268	Pen _pen;
269
270	Font _font;
271
272	Color _clroutline;
273	Color _clrhighlight;
274	Color _clrnormal;
275	Color _clrforeground;
276	Color _clrbackground;
277
278	int _btnstate = 0;
279	int _mouseholdstate = 0;
280
281	Size _valueBounds;
282}