Mercurial > projects > dynamin
annotate dynamin/gui/control.d @ 10:ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Wed, 15 Jul 2009 21:22:20 -0500 |
parents | b621b528823d |
children | 96b29f2efa4d |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.gui.control; | |
27 | |
28 import dynamin.all_core; | |
29 import dynamin.all_painting; | |
30 import dynamin.gui.container; | |
31 import dynamin.gui.events; | |
32 import dynamin.gui.window; | |
33 import dynamin.gui.cursor; | |
34 import tango.io.Stdout; | |
35 | |
36 Control hotControl; | |
37 // the hot control is the one the mouse is over | |
38 package void setHotControl(Control c) { | |
39 if(c !is hotControl) { | |
40 if(hotControl) | |
41 hotControl.mouseLeft(new EventArgs); | |
42 hotControl = c; | |
43 if(c) | |
44 c.mouseEntered(new EventArgs); | |
45 } | |
46 } | |
47 package Control getHotControl() { return hotControl; } | |
48 Control captorControl; | |
49 package void setCaptorControl(Control c) { | |
50 captorControl = c; | |
51 } | |
52 package Control getCaptorControl() { return captorControl; } | |
53 | |
54 /** | |
55 * The painting event is an exception to the rule that added handlers are called | |
56 * before whenPainting. The painting event is far more useful since | |
57 * added handles are called after the control's whenPainting has finished. | |
58 * Otherwise, anything handlers painted would likely be painted over when the | |
59 * control painted. | |
60 */ | |
61 class Control { | |
62 protected: | |
63 bool _visible; | |
64 bool _focusable; | |
65 bool _focused; | |
66 package Point _location; | |
67 package Size _size; | |
68 string _text; | |
69 Color _backColor; | |
70 Color _foreColor; | |
71 Font _font; | |
72 Cursor _cursor; | |
73 Container _parent; | |
74 bool _elasticX, _elasticY; | |
75 public: | |
76 protected void dispatchMouseEntered(EventArgs e) { | |
77 Cursor.setCurrent(this, _cursor); | |
78 mouseEntered.callHandlers(e); | |
79 mouseEntered.callMainHandler(e); | |
80 } | |
81 protected void dispatchMouseDown(MouseEventArgs e) { | |
82 setCaptorControl(this); | |
83 mouseDown.callHandlers(e); | |
84 mouseDown.callMainHandler(e); | |
85 } | |
86 protected void dispatchMouseUp(MouseEventArgs e) { | |
87 setCaptorControl(null); | |
88 mouseUp.callHandlers(e); | |
89 mouseUp.callMainHandler(e); | |
90 } | |
91 protected void dispatchMouseMoved(MouseEventArgs e) { | |
92 setHotControl(this); | |
93 mouseMoved.callHandlers(e); | |
94 mouseMoved.callMainHandler(e); | |
95 } | |
96 protected void dispatchMouseDragged(MouseEventArgs e) { | |
97 setHotControl(this); | |
98 mouseDragged.callHandlers(e); | |
99 mouseDragged.callMainHandler(e); | |
100 } | |
101 protected void dispatchMouseTurned(MouseTurnedEventArgs e) { | |
102 mouseTurned.callHandlers(e); | |
103 mouseTurned.callMainHandler(e); | |
104 if(!e.stopped && _parent) | |
105 _parent.mouseTurned(e); | |
106 } | |
107 protected void dispatchPainting(PaintingEventArgs e) { | |
108 e.graphics.save(); | |
109 painting.callMainHandler(e); | |
110 e.graphics.restore(); | |
111 e.graphics.save(); | |
112 // TODO: every call to a handler should be wrapped in a save/restore | |
113 painting.callHandlers(e); | |
114 e.graphics.restore(); | |
115 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
116 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
117 /// Override this method in a subclass to handle the moved event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
118 protected void whenMoved(EventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
119 /// This event occurs after the control has been moved. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
120 Event!(whenMoved) moved; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
121 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
122 /// Override this method in a subclass to handle the resized event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
123 protected void whenResized(EventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
124 /// This event occurs after the control has been resized. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
125 Event!(whenResized) resized; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
126 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
127 /// Override this method in a subclass to handle the mouseEntered event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
128 protected void whenMouseEntered(EventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
129 /// This event occurs after the mouse has entered the control. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
130 Event!(whenMouseEntered) mouseEntered; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
131 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
132 /// Override this method in a subclass to handle the mouseLeft event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
133 protected void whenMouseLeft(EventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
134 /// This event occurs after the mouse has left the control. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
135 Event!(whenMouseLeft) mouseLeft; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
136 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
137 /// Override this method in a subclass to handle the mouseDown event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
138 protected void whenMouseDown(MouseEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
139 /// This event occurs after a mouse button is pressed. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
140 Event!(whenMouseDown) mouseDown; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
141 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
142 /// Override this method in a subclass to handle the mouseUp event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
143 protected void whenMouseUp(MouseEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
144 /// This event occurs after a mouse button is released. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
145 Event!(whenMouseUp) mouseUp; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
146 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
147 /// Override this method in a subclass to handle the mouseMoved event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
148 protected void whenMouseMoved(MouseEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
149 /// This event occurs after the mouse has been moved. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
150 Event!(whenMouseMoved) mouseMoved; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
151 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
152 /// Override this method in a subclass to handle the mouseMoved event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
153 protected void whenMouseDragged(MouseEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
154 /// This event occurs after the mouse has been moved. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
155 Event!(whenMouseDragged) mouseDragged; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
156 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
157 /// Override this method in a subclass to handle the mouseTurned event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
158 protected void whenMouseTurned(MouseTurnedEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
159 /// This event occurs after the mouse wheel has been turned. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
160 Event!(whenMouseTurned) mouseTurned; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
161 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
162 /// Override this method in a subclass to handle the keyDown event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
163 protected void whenKeyDown(KeyEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
164 /// This event occurs after a key is pressed. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
165 Event!(whenKeyDown) keyDown; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
166 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
167 /// Override this method in a subclass to handle the keyTyped event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
168 protected void whenKeyTyped(KeyTypedEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
169 /// This event occurs after a character key is pressed. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
170 Event!(whenKeyTyped) keyTyped; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
171 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
172 /// Override this method in a subclass to handle the keyUp event. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
173 protected void whenKeyUp(KeyEventArgs e) { } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
174 /// This event occurs after a key is released. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
175 Event!(whenKeyUp) keyUp; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
176 |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
177 /// Override this method in a subclass to handle the painting event. |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
178 protected void whenPainting(PaintingEventArgs e) { |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
179 e.graphics.source = backColor; |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
180 e.graphics.paint(); |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
181 } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
182 /// This event occurs when the control needs to be painted. |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
183 Event!(whenPainting) painting; |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
184 |
0 | 185 this() { |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
186 moved.mainHandler = &whenMoved; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
187 resized.mainHandler = &whenResized; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
188 mouseEntered.mainHandler = &whenMouseEntered; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
189 mouseEntered.dispatcher = &dispatchMouseEntered; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
190 mouseLeft.mainHandler = &whenMouseLeft; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
191 mouseDown.mainHandler = &whenMouseDown; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
192 mouseDown.dispatcher = &dispatchMouseDown; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
193 mouseUp.mainHandler = &whenMouseUp; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
194 mouseUp.dispatcher = &dispatchMouseUp; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
195 mouseMoved.mainHandler = &whenMouseMoved; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
196 mouseMoved.dispatcher = &dispatchMouseMoved; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
197 mouseDragged.mainHandler = &whenMouseDragged; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
198 mouseDragged.dispatcher = &dispatchMouseDragged; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
199 mouseTurned.mainHandler = &whenMouseTurned; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
200 mouseTurned.dispatcher = &dispatchMouseTurned; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
201 keyDown.mainHandler = &whenKeyDown; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
202 keyTyped.mainHandler = &whenKeyTyped; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
203 keyUp.mainHandler = &whenKeyUp; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
204 painting.mainHandler = &whenPainting; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
205 painting.dispatcher = &dispatchPainting; |
0 | 206 |
207 _location = Point(0, 0); | |
208 _size = Size(100, 100); | |
209 _text = ""; | |
210 _focusable = false; | |
211 _focused = false; | |
212 _visible = true; | |
213 _cursor = Cursor.Arrow; | |
214 _elasticX = false; | |
215 _elasticY = false; | |
216 | |
217 // TODO: remove these when themes mature | |
218 _foreColor = Color.Black; | |
219 _font = new Font("Tahoma", 11); | |
220 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
221 |
0 | 222 protected Graphics quickCreateGraphics() { |
223 if(_parent is null) | |
224 return null; | |
225 auto g = _parent.quickCreateGraphics(); | |
226 g.translate(location); | |
227 return g; | |
228 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
229 |
0 | 230 /** |
231 * Sets the specified Graphics' font and source to this control's font | |
232 * and foreground color. Also, clips to this control's rectangle. | |
233 */ | |
234 void setupGraphics(Graphics g) { | |
235 g.rectangle(0, 0, width, height); | |
236 g.clip(); | |
237 g.font = font; | |
238 g.source = foreColor; | |
239 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
240 |
0 | 241 /** |
242 * Creates a Graphics, calls the specified delegate with it, and deletes | |
243 * it to release resources. | |
244 */ | |
245 void withGraphics(void delegate(Graphics g) dg) { | |
246 auto g = quickCreateGraphics(); | |
247 setupGraphics(g); | |
248 dg(g); | |
249 delete g; | |
250 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
251 |
0 | 252 /** |
253 * | |
254 */ | |
255 bool focused() { | |
256 return _focused; | |
257 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
258 |
0 | 259 /** |
260 * | |
261 */ | |
262 void focus() { | |
263 if(!_focusable) | |
264 return; | |
265 Control c = this; | |
266 while(c.parent) | |
267 c = c.parent; | |
268 if(auto win = cast(Window)c) { | |
269 if(win.focusedControl) { | |
270 win.focusedControl._focused = false; | |
271 win.focusedControl.repaint(); | |
272 } | |
273 win.focusedControl = this; | |
274 _focused = true; | |
275 repaint(); | |
276 } | |
277 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
278 |
0 | 279 /** |
280 * Returns whether this control is on the screen. A control is | |
281 * on screen if one of its ancestors is a top level window. Whether or | |
282 * not the control is visible has no bearing on this value. If a control | |
283 * has no parent, then it is clearly not on the screen. | |
284 */ | |
285 bool onScreen() { | |
286 return _parent && _parent.onScreen; | |
287 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
288 |
0 | 289 /** |
290 * Gets the location of this control in screen coordinates. An exception is | |
291 * thrown if this control is not on the screen. | |
292 */ | |
293 Point screenLocation() { | |
294 if(!_parent) | |
295 throw new Exception("control is not on screen"); | |
296 return _parent.screenLocation + location; | |
297 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
298 |
0 | 299 /** |
300 * Converts the specified point in content coordinates into screen | |
301 * coordinates. An exception is thrown if this control is not on the screen. | |
302 */ | |
303 Point contentToScreen(Point pt) { // TODO: content?? even on Window?? | |
304 if(!_parent) | |
305 throw new Exception("control is not on screen"); | |
306 return _parent.contentToScreen(pt + location); | |
307 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
308 |
0 | 309 /** |
310 * Converts the specified point in screen coordinates into content | |
311 * coordinates. An exception is thrown if this control is not on the screen. | |
312 */ | |
313 Point screenToContent(Point pt) { | |
314 if(!_parent) | |
315 throw new Exception("control is not on screen"); | |
316 return _parent.screenToContent(pt) - location; // TODO: borders | |
317 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
318 |
0 | 319 /** |
320 * Converts the specified point in this control's content coordinates | |
321 * into the specified control's content coordinates. An exception is | |
322 * thrown if this control is not on the screen. | |
323 */ | |
324 Point contentToContent(Point pt, Control c) { | |
325 return c.screenToContent(contentToScreen(pt)); | |
326 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
327 |
0 | 328 /** |
329 * Returns whether the specified point is inside this control. | |
330 */ | |
331 bool contains(Point pt) { | |
332 return pt.x >= 0 && pt.y >= 0 && pt.x < width && pt.y < height; | |
333 } | |
334 /// ditto | |
335 bool contains(real x, real y) { | |
336 return contains(Point(x, y)); | |
337 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
338 |
0 | 339 bool topLevel() { return false; } |
340 // TODO: return NativeControl/Window? | |
341 Control getTopLevel() { | |
342 Control c = this; | |
343 while(c.parent) | |
344 c = c.parent; | |
345 return c.topLevel ? c : null; | |
346 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
347 |
0 | 348 /** |
349 * Gets this control's parent. | |
350 */ | |
351 Container parent() { return _parent; } | |
352 package void parent(Container c) { | |
353 _parent = c; | |
354 //ParentChanged(new EventArgs); // TODO: add event | |
355 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
356 |
0 | 357 /** |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
358 * Gets or sets whether is control is visible. The default is true, except |
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
359 * on top-level windows. |
0 | 360 */ |
361 //void visible(bool b) { visible = b; } | |
362 bool visible() { return _visible; } | |
363 | |
364 /** | |
365 * Gets or sets the location of this control in its parent's content | |
366 * coordinates. | |
367 * Examples: | |
368 * ----- | |
369 * control.location = [5, 35]; | |
370 * control.location = Point(50, 50); | |
371 * ----- | |
372 */ | |
373 Point location() { return _location; } | |
374 /// ditto | |
375 void location(Point pt) { | |
376 if((cast(Window)_parent) !is null) | |
377 throw new Exception("cannot set location of a window's content"); | |
378 pt.x = round(pt.x); | |
379 pt.y = round(pt.y); | |
380 repaint(); | |
381 _location = pt; | |
382 repaint(); | |
383 moved(new EventArgs); | |
384 } | |
385 /// ditto | |
386 void location(real[] pt) { | |
387 assert(pt.length == 2, "pt must be just an x and y"); | |
388 location = Point(pt[0], pt[1]); | |
389 } | |
390 | |
391 /** | |
392 * Gets or sets the size of this control. | |
393 * Examples: | |
394 * ----- | |
395 * control.size = [75, 23]; | |
396 * control.size = Size(80, 20); | |
397 * ----- | |
398 */ | |
399 Size size() { return _size; } | |
400 /// ditto | |
401 void size(Size newSize) { | |
402 if(newSize.width < 0) | |
403 newSize.width = 0; | |
404 if(newSize.height < 0) | |
405 newSize.height = 0; | |
406 newSize.width = round(newSize.width); | |
407 newSize.height = round(newSize.height); | |
408 repaint(); | |
409 _size = newSize; | |
410 repaint(); | |
411 resized(new EventArgs); | |
412 } | |
413 /// ditto | |
414 void size(real[] newSize) { | |
415 assert(newSize.length == 2, "size must be just a width and height"); | |
416 size = Size(newSize[0], newSize[1]); | |
417 } | |
418 | |
419 /** | |
420 * Gets the size at which this control looks the best. It is intended that | |
421 * the control not be made smaller than this size, and only be made larger | |
422 * if it is elastic, or if it needs to be aligned with other controls. | |
423 * | |
424 * This property should be overridden in subclasses to return a best size. | |
425 */ | |
426 Size bestSize() { return Size(100, 100); } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
427 |
0 | 428 /** |
429 * Gets the distance from the top of this control to the baseline of | |
430 * the first line of this control's text. If this control does not have | |
431 * text, 0 may be returned. | |
432 */ | |
433 int baseline() { return 0; } | |
434 | |
435 /// Same as location.x | |
436 real x() { return location.x; } | |
437 /// Same as location.y | |
438 real y() { return location.y; } | |
439 /// Same as size.width | |
440 real width() { return size.width; } | |
441 /// Same as size.height | |
442 real height() { return size.height; } | |
443 | |
444 /** | |
445 * Gets or sets whether this control is elastic horizontally or vertically. | |
446 * If a control is elastic, then it is intended to be made larger than its | |
447 * best size. | |
448 */ | |
449 bool elasticX() { return _elasticX; } | |
450 /// ditto | |
451 void elasticX(bool b) { _elasticX = b; } | |
452 /// ditto | |
453 bool elasticY() { return _elasticY; } | |
454 /// ditto | |
455 void elasticY(bool b) { _elasticY = b; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
456 |
0 | 457 /** |
458 * Gets or sets the text that this control shows. | |
459 */ | |
460 string text() { return _text.dup; } | |
461 /// ditto | |
462 void text(string str) { | |
463 _text = str.dup; | |
464 repaint(); | |
465 //TextChanged(EventArgs e) // TODO: add event | |
466 } | |
467 | |
468 /** | |
469 * Gets or sets the background color of this control. | |
470 */ | |
471 Color backColor() { return _backColor; } | |
472 /// ditto | |
473 void backColor(Color c) { | |
474 _backColor = c; | |
475 repaint(); | |
476 } | |
477 | |
478 /** | |
479 * Gets or sets the foreground color of this control. | |
480 */ | |
481 Color foreColor() { return _foreColor; } | |
482 /// ditto | |
483 void foreColor(Color c) { | |
484 _foreColor = c; | |
485 repaint(); | |
486 } | |
487 | |
488 /** | |
489 * Gets or sets the font of this control uses to display text. A value of | |
490 * null means that the font is unset. When the font is null, the | |
491 * current theme's font is used. The default is null. | |
492 */ | |
493 Font font() { | |
494 // TODO: if font is null (unset), return from theme | |
495 //if(font is null) | |
496 // return Theme.Current.Control_Font(this); | |
497 //else | |
498 return _font; | |
499 } | |
500 /// ditto | |
501 void font(Font f) { | |
502 _font = f; | |
503 repaint(); | |
504 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
505 |
0 | 506 Cursor cursor() { |
507 return _cursor; | |
508 } | |
509 void cursor(Cursor cur) { | |
510 if(_cursor is cur) | |
511 return; | |
512 _cursor = cur; | |
513 if(getHotControl() is this) | |
514 Cursor.setCurrent(this, _cursor); | |
515 } | |
516 | |
517 /** | |
518 * Causes the part of the control inside the specified | |
519 * rectangle to be repainted. The rectangle is in content coordinates. | |
520 * | |
521 * The control will not be repainted before this method returns; rather, | |
522 * the area is just marked as needing to be repainted. The next time there | |
523 * are no other system events to be processed, a painting event will called. | |
524 */ | |
525 void repaint(Rect rect) { | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
526 // TODO: make sure that parts clipped off by the parent are |
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
527 // not invalidated |
0 | 528 if(_parent) |
529 _parent.repaint(rect + location); | |
530 } | |
531 /// ditto | |
532 void repaint(real x, real y, real width, real height) { | |
533 repaint(Rect(x, y, width, height)); | |
534 } | |
535 /** | |
536 * Causes the entire control to be repainted. | |
537 */ | |
538 void repaint() { | |
539 repaint(Rect(0, 0, width, height)); | |
540 } | |
541 } | |
542 | |
543 |