Mercurial > projects > dynamin
annotate dynamin/gui/control.d @ 78:651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 06 Feb 2010 15:12:52 -0600 |
parents | 76b081749316 |
children | 73060bc3f004 |
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 | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
36 //{{{ hotControl |
0 | 37 Control hotControl; |
38 // the hot control is the one the mouse is over | |
39 package void setHotControl(Control c) { | |
40 if(c !is hotControl) { | |
41 if(hotControl) | |
42 hotControl.mouseLeft(new EventArgs); | |
43 hotControl = c; | |
44 if(c) | |
45 c.mouseEntered(new EventArgs); | |
46 } | |
47 } | |
48 package Control getHotControl() { return hotControl; } | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
49 //}}} |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
50 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
51 //{{{ captorControl |
0 | 52 Control captorControl; |
53 package void setCaptorControl(Control c) { | |
54 captorControl = c; | |
55 } | |
56 package Control getCaptorControl() { return captorControl; } | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
57 //}}} |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
58 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
59 //{{{ focusedControl |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
60 Control focusedControl; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
61 package void setFocusedControl(Control c) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
62 if(focusedControl is c) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
63 return; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
64 scope e = new EventArgs; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
65 if(focusedControl) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
66 focusedControl.focusLost(e); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
67 focusedControl = c; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
68 if(focusedControl) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
69 focusedControl.focusGained(e); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
70 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
71 package Control getFocusedControl() { return focusedControl; } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
72 //}}} |
0 | 73 |
74 /** | |
75 * The painting event is an exception to the rule that added handlers are called | |
76 * before whenPainting. The painting event is far more useful since | |
77 * added handles are called after the control's whenPainting has finished. | |
78 * Otherwise, anything handlers painted would likely be painted over when the | |
79 * control painted. | |
80 */ | |
81 class Control { | |
82 protected: | |
83 bool _focusable; | |
84 bool _focused; | |
49 | 85 int _tabIndex; |
0 | 86 package Point _location; |
87 package Size _size; | |
88 string _text; | |
89 Color _backColor; | |
90 Color _foreColor; | |
91 Font _font; | |
92 Cursor _cursor; | |
93 Container _parent; | |
94 bool _elasticX, _elasticY; | |
95 public: | |
96 protected void dispatchMouseEntered(EventArgs e) { | |
23
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
97 setCurrentCursor(_cursor); |
0 | 98 mouseEntered.callHandlers(e); |
99 mouseEntered.callMainHandler(e); | |
100 } | |
101 protected void dispatchMouseDown(MouseEventArgs e) { | |
102 setCaptorControl(this); | |
103 mouseDown.callHandlers(e); | |
104 mouseDown.callMainHandler(e); | |
105 } | |
106 protected void dispatchMouseUp(MouseEventArgs e) { | |
107 setCaptorControl(null); | |
108 mouseUp.callHandlers(e); | |
109 mouseUp.callMainHandler(e); | |
110 } | |
111 protected void dispatchMouseMoved(MouseEventArgs e) { | |
112 setHotControl(this); | |
113 mouseMoved.callHandlers(e); | |
114 mouseMoved.callMainHandler(e); | |
115 } | |
116 protected void dispatchMouseDragged(MouseEventArgs e) { | |
117 setHotControl(this); | |
118 mouseDragged.callHandlers(e); | |
119 mouseDragged.callMainHandler(e); | |
120 } | |
121 protected void dispatchMouseTurned(MouseTurnedEventArgs e) { | |
122 mouseTurned.callHandlers(e); | |
123 mouseTurned.callMainHandler(e); | |
124 if(!e.stopped && _parent) | |
125 _parent.mouseTurned(e); | |
126 } | |
47
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
127 protected void dispatchKeyDown(KeyEventArgs e) { |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
128 keyDown.callHandlers(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
129 keyDown.callMainHandler(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
130 if(!e.stopped && _parent) |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
131 _parent.keyDown(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
132 } |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
133 protected void dispatchKeyUp(KeyEventArgs e) { |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
134 keyUp.callHandlers(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
135 keyUp.callMainHandler(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
136 if(!e.stopped && _parent) |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
137 _parent.keyUp(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
138 } |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
139 protected void dispatchKeyTyped(KeyTypedEventArgs e) { |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
140 keyTyped.callHandlers(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
141 keyTyped.callMainHandler(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
142 if(!e.stopped && _parent) |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
143 _parent.keyTyped(e); |
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
144 } |
0 | 145 protected void dispatchPainting(PaintingEventArgs e) { |
146 e.graphics.save(); | |
147 painting.callMainHandler(e); | |
148 e.graphics.restore(); | |
149 e.graphics.save(); | |
150 // TODO: every call to a handler should be wrapped in a save/restore | |
151 painting.callHandlers(e); | |
152 e.graphics.restore(); | |
153 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
154 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
155 /// 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
|
156 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
|
157 /// 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
|
158 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
|
159 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
160 /// 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
|
161 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
|
162 /// 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
|
163 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
|
164 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
165 /// 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
|
166 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
|
167 /// 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
|
168 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
|
169 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
170 /// 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
|
171 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
|
172 /// 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
|
173 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
|
174 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
175 /// 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
|
176 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
|
177 /// 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
|
178 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
|
179 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
180 /// 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
|
181 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
|
182 /// 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
|
183 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
|
184 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
185 /// 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
|
186 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
|
187 /// 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
|
188 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
|
189 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
190 /// 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
|
191 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
|
192 /// 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
|
193 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
|
194 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
195 /// Override this method in a subclass to handle the mouseTurned event. |
47
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
196 /// If this event is not stopped, it will be sent to the control's parent. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
197 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
|
198 /// 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
|
199 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
|
200 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
201 /// Override this method in a subclass to handle the keyDown event. |
47
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
202 /// If this event is not stopped, it will be sent to the control's parent. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
203 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
|
204 /// 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
|
205 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
|
206 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
207 /// Override this method in a subclass to handle the keyTyped event. |
47
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
208 /// If this event is not stopped, it will be sent to the control's parent. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
209 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
|
210 /// 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
|
211 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
|
212 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
8
diff
changeset
|
213 /// Override this method in a subclass to handle the keyUp event. |
47
41b76c218851
Change keyUp, keyDown, and keyTyped to bubble to a control's parent if not stopped.
Jordan Miner <jminer7@gmail.com>
parents:
23
diff
changeset
|
214 /// If this event is not stopped, it will be sent to the control's parent. |
8
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
215 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
|
216 /// 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
|
217 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
|
218 |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
219 /// 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
|
220 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
|
221 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
|
222 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
|
223 } |
b621b528823d
whenXX methods have to come before the event if switched to template mixins.
Jordan Miner <jminer7@gmail.com>
parents:
5
diff
changeset
|
224 /// 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
|
225 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
|
226 |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
227 /// Override this method in a subclass to handle the focusGained event. |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
228 protected void whenFocusGained(EventArgs e) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
229 repaint(); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
230 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
231 /// This event occurs after this control is focused. |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
232 Event!(whenFocusGained) focusGained; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
233 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
234 /// Override this method in a subclass to handle the focusLost event. |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
235 protected void whenFocusLost(EventArgs e) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
236 repaint(); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
237 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
238 /// This event occurs after this control loses focus. |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
239 Event!(whenFocusLost) focusLost; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
240 |
0 | 241 this() { |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
242 moved .setUp(&whenMoved); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
243 resized .setUp(&whenResized); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
244 mouseEntered.setUp(&whenMouseEntered, &dispatchMouseEntered); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
245 mouseLeft .setUp(&whenMouseLeft); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
246 mouseDown .setUp(&whenMouseDown, &dispatchMouseDown); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
247 mouseUp .setUp(&whenMouseUp, &dispatchMouseUp); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
248 mouseMoved .setUp(&whenMouseMoved, &dispatchMouseMoved); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
249 mouseDragged.setUp(&whenMouseDragged, &dispatchMouseDragged); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
250 mouseTurned .setUp(&whenMouseTurned, &dispatchMouseTurned); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
251 keyDown .setUp(&whenKeyDown, &dispatchKeyDown); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
252 keyTyped .setUp(&whenKeyTyped, &dispatchKeyTyped); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
253 keyUp .setUp(&whenKeyUp, &dispatchKeyUp); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
254 painting .setUp(&whenPainting, &dispatchPainting); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
255 focusGained .setUp(&whenFocusGained); |
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
76
diff
changeset
|
256 focusLost .setUp(&whenFocusLost); |
0 | 257 |
258 _location = Point(0, 0); | |
259 _size = Size(100, 100); | |
260 _text = ""; | |
261 _focusable = false; | |
262 _focused = false; | |
49 | 263 _tabIndex = 0; |
0 | 264 _cursor = Cursor.Arrow; |
265 _elasticX = false; | |
266 _elasticY = false; | |
267 | |
268 // TODO: remove these when themes mature | |
269 _foreColor = Color.Black; | |
270 _font = new Font("Tahoma", 11); | |
271 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
272 |
0 | 273 protected Graphics quickCreateGraphics() { |
274 if(_parent is null) | |
275 return null; | |
276 auto g = _parent.quickCreateGraphics(); | |
60
cf7c5f968306
Fix crash in withGraphics() if there wasn't a Window ancestor with a handle.
Jordan Miner <jminer7@gmail.com>
parents:
59
diff
changeset
|
277 if(g) |
cf7c5f968306
Fix crash in withGraphics() if there wasn't a Window ancestor with a handle.
Jordan Miner <jminer7@gmail.com>
parents:
59
diff
changeset
|
278 g.translate(location); |
0 | 279 return g; |
280 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
281 |
0 | 282 /** |
283 * Sets the specified Graphics' font and source to this control's font | |
284 * and foreground color. Also, clips to this control's rectangle. | |
285 */ | |
286 void setupGraphics(Graphics g) { | |
287 g.rectangle(0, 0, width, height); | |
288 g.clip(); | |
289 g.font = font; | |
290 g.source = foreColor; | |
291 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
292 |
0 | 293 /** |
294 * Creates a Graphics, calls the specified delegate with it, and deletes | |
295 * it to release resources. | |
296 */ | |
297 void withGraphics(void delegate(Graphics g) dg) { | |
298 auto g = quickCreateGraphics(); | |
60
cf7c5f968306
Fix crash in withGraphics() if there wasn't a Window ancestor with a handle.
Jordan Miner <jminer7@gmail.com>
parents:
59
diff
changeset
|
299 if(!g) |
cf7c5f968306
Fix crash in withGraphics() if there wasn't a Window ancestor with a handle.
Jordan Miner <jminer7@gmail.com>
parents:
59
diff
changeset
|
300 return; |
0 | 301 setupGraphics(g); |
302 dg(g); | |
303 delete g; | |
304 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
305 |
0 | 306 /** |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
307 * Returns whether or not this control can receive focus. |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
308 */ |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
309 bool focusable() { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
310 return _focusable; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
311 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
312 void focusable(bool f) { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
313 _focusable = f; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
314 // TODO: |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
315 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
316 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
317 /** |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
318 * Returns whether this control currently has focus. A control with focus |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
319 * receives keyboard events. |
0 | 320 */ |
321 bool focused() { | |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
322 return getFocusedControl() is this; |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
323 } |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
324 |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
325 /** |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
326 * Returns true if this control should visually show when it has focus |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
327 * and returns false if not. Focus is usually hidden until the |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
328 * user uses the keyboard to navigate. |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
329 * |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
330 * A text box is one control that shows when it is |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
331 * focused (by its caret), regardless of this value. (Because showing |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
332 * focus isn't the sole purpose of a caret.) |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
333 */ |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
334 bool showFocus() { |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
335 auto top = getTopLevel(); |
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
336 return top && (cast(Window)top).showFocus; |
0 | 337 } |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
338 |
0 | 339 /** |
49 | 340 * Gets or sets this control's tab index. The tab index of controls |
341 * decides in what order they are focused when the tab key is pressed. | |
342 * If multiple controls in a window have the same tab index, focus is | |
343 * changed based on the order they were added to the window. | |
344 * The default is 0. | |
345 */ | |
346 int tabIndex() { | |
347 return _tabIndex; | |
348 } | |
349 /// ditto | |
350 void tabIndex(int i) { | |
351 _tabIndex = i; | |
352 } | |
353 | |
354 /** | |
76
76b081749316
Add comment to Control.focus()
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
355 * Sets this control as the focused control in its window. The focused |
76b081749316
Add comment to Control.focus()
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
356 * control is the one keyboard input is sent to. If the window this control |
76b081749316
Add comment to Control.focus()
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
357 * is in is not active, then this control will be focused only when the |
76b081749316
Add comment to Control.focus()
Jordan Miner <jminer7@gmail.com>
parents:
68
diff
changeset
|
358 * window is activated. |
0 | 359 */ |
360 void focus() { | |
361 if(!_focusable) | |
362 return; | |
56
c2566ab82535
Edit comments and fix a typo.
Jordan Miner <jminer7@gmail.com>
parents:
55
diff
changeset
|
363 |
50
a18c2fd9fe36
Fix a crash if Control.focus was called on a Control not in a Window.
Jordan Miner <jminer7@gmail.com>
parents:
49
diff
changeset
|
364 auto top = getTopLevel(); |
a18c2fd9fe36
Fix a crash if Control.focus was called on a Control not in a Window.
Jordan Miner <jminer7@gmail.com>
parents:
49
diff
changeset
|
365 if(!top) |
a18c2fd9fe36
Fix a crash if Control.focus was called on a Control not in a Window.
Jordan Miner <jminer7@gmail.com>
parents:
49
diff
changeset
|
366 return; |
55
c138461bf845
Add focusing and other changes that are related
Jordan Miner <jminer7@gmail.com>
parents:
53
diff
changeset
|
367 if(auto win = cast(Window)top) |
0 | 368 win.focusedControl = this; |
369 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
370 |
0 | 371 /** |
372 * Returns whether this control is on the screen. A control is | |
373 * on screen if one of its ancestors is a top level window. Whether or | |
374 * not the control is visible has no bearing on this value. If a control | |
375 * has no parent, then it is clearly not on the screen. | |
376 */ | |
377 bool onScreen() { | |
378 return _parent && _parent.onScreen; | |
379 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
380 |
0 | 381 /** |
382 * Gets the location of this control in screen coordinates. An exception is | |
383 * thrown if this control is not on the screen. | |
384 */ | |
385 Point screenLocation() { | |
386 if(!_parent) | |
387 throw new Exception("control is not on screen"); | |
388 return _parent.screenLocation + location; | |
389 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
390 |
0 | 391 /** |
392 * Converts the specified point in content coordinates into screen | |
393 * coordinates. An exception is thrown if this control is not on the screen. | |
394 */ | |
395 Point contentToScreen(Point pt) { // TODO: content?? even on Window?? | |
396 if(!_parent) | |
397 throw new Exception("control is not on screen"); | |
398 return _parent.contentToScreen(pt + location); | |
399 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
400 |
0 | 401 /** |
402 * Converts the specified point in screen coordinates into content | |
403 * coordinates. An exception is thrown if this control is not on the screen. | |
404 */ | |
405 Point screenToContent(Point pt) { | |
406 if(!_parent) | |
407 throw new Exception("control is not on screen"); | |
408 return _parent.screenToContent(pt) - location; // TODO: borders | |
409 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
410 |
0 | 411 /** |
412 * Converts the specified point in this control's content coordinates | |
413 * into the specified control's content coordinates. An exception is | |
414 * thrown if this control is not on the screen. | |
415 */ | |
416 Point contentToContent(Point pt, Control c) { | |
417 return c.screenToContent(contentToScreen(pt)); | |
418 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
419 |
0 | 420 /** |
421 * Returns whether the specified point is inside this control. | |
422 */ | |
423 bool contains(Point pt) { | |
424 return pt.x >= 0 && pt.y >= 0 && pt.x < width && pt.y < height; | |
425 } | |
426 /// ditto | |
427 bool contains(real x, real y) { | |
428 return contains(Point(x, y)); | |
429 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
430 |
51 | 431 /** |
432 * Returns true if this control is a top-level control and false otherwise. | |
433 * Top-level controls do not have parents. Non-top-level controls can only | |
434 * be shown on the screen by adding them as children to a top-level control. | |
435 * Currently, the only top-level control is Window. | |
436 */ | |
0 | 437 bool topLevel() { return false; } |
438 // TODO: return NativeControl/Window? | |
51 | 439 /** |
440 * Loops over this control's ancestors, and if a top-level control is found, | |
441 * it is returned. If this control does not have a top-level ancestor, | |
442 * null is returned. | |
443 */ | |
0 | 444 Control getTopLevel() { |
445 Control c = this; | |
446 while(c.parent) | |
447 c = c.parent; | |
448 return c.topLevel ? c : null; | |
449 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
450 |
0 | 451 /** |
452 * Gets this control's parent. | |
453 */ | |
454 Container parent() { return _parent; } | |
455 package void parent(Container c) { | |
456 _parent = c; | |
53
6e33b00595e9
Fix capitalization in a couple comments.
Jordan Miner <jminer7@gmail.com>
parents:
51
diff
changeset
|
457 //parentChanged(new EventArgs); // TODO: add event |
0 | 458 } |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
459 |
0 | 460 /** |
59 | 461 * Calls the specified delegate with each of this control's ancestors, |
462 * starting with its parent and moving up. Returns the first ancestor | |
463 * that the delgate returns true for. If none are found, returns null. | |
464 */ | |
465 Container findAncestor(bool delegate(Container) dg) { | |
466 Container anc = parent; | |
467 while(anc) { | |
468 if(dg(anc)) | |
469 return anc; | |
470 anc = anc.parent; | |
471 } | |
472 return null; | |
473 } | |
474 | |
475 /** | |
0 | 476 * Gets or sets the location of this control in its parent's content |
477 * coordinates. | |
478 * Examples: | |
479 * ----- | |
480 * control.location = [5, 35]; | |
481 * control.location = Point(50, 50); | |
482 * ----- | |
483 */ | |
484 Point location() { return _location; } | |
485 /// ditto | |
486 void location(Point pt) { | |
487 if((cast(Window)_parent) !is null) | |
488 throw new Exception("cannot set location of a window's content"); | |
489 pt.x = round(pt.x); | |
490 pt.y = round(pt.y); | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
491 if(_location == pt) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
492 return; |
0 | 493 repaint(); |
494 _location = pt; | |
495 repaint(); | |
496 moved(new EventArgs); | |
497 } | |
498 /// ditto | |
499 void location(real[] pt) { | |
500 assert(pt.length == 2, "pt must be just an x and y"); | |
501 location = Point(pt[0], pt[1]); | |
502 } | |
503 | |
504 /** | |
505 * Gets or sets the size of this control. | |
506 * Examples: | |
507 * ----- | |
508 * control.size = [75, 23]; | |
509 * control.size = Size(80, 20); | |
510 * ----- | |
511 */ | |
512 Size size() { return _size; } | |
513 /// ditto | |
514 void size(Size newSize) { | |
515 if(newSize.width < 0) | |
516 newSize.width = 0; | |
517 if(newSize.height < 0) | |
518 newSize.height = 0; | |
519 newSize.width = round(newSize.width); | |
520 newSize.height = round(newSize.height); | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
521 if(_size == newSize) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
522 return; |
0 | 523 repaint(); |
524 _size = newSize; | |
525 repaint(); | |
526 resized(new EventArgs); | |
527 } | |
528 /// ditto | |
529 void size(real[] newSize) { | |
530 assert(newSize.length == 2, "size must be just a width and height"); | |
531 size = Size(newSize[0], newSize[1]); | |
532 } | |
533 | |
534 /** | |
535 * Gets the size at which this control looks the best. It is intended that | |
536 * the control not be made smaller than this size, and only be made larger | |
537 * if it is elastic, or if it needs to be aligned with other controls. | |
538 * | |
539 * This property should be overridden in subclasses to return a best size. | |
540 */ | |
541 Size bestSize() { return Size(100, 100); } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
542 |
0 | 543 /** |
544 * Gets the distance from the top of this control to the baseline of | |
545 * the first line of this control's text. If this control does not have | |
546 * text, 0 may be returned. | |
547 */ | |
548 int baseline() { return 0; } | |
549 | |
550 /// Same as location.x | |
551 real x() { return location.x; } | |
552 /// Same as location.y | |
553 real y() { return location.y; } | |
554 /// Same as size.width | |
555 real width() { return size.width; } | |
556 /// Same as size.height | |
557 real height() { return size.height; } | |
558 | |
559 /** | |
560 * Gets or sets whether this control is elastic horizontally or vertically. | |
561 * If a control is elastic, then it is intended to be made larger than its | |
562 * best size. | |
563 */ | |
564 bool elasticX() { return _elasticX; } | |
565 /// ditto | |
566 void elasticX(bool b) { _elasticX = b; } | |
567 /// ditto | |
568 bool elasticY() { return _elasticY; } | |
569 /// ditto | |
570 void elasticY(bool b) { _elasticY = b; } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
571 |
0 | 572 /** |
573 * Gets or sets the text that this control shows. | |
574 */ | |
575 string text() { return _text.dup; } | |
576 /// ditto | |
577 void text(string str) { | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
578 if(_text == str) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
579 return; |
0 | 580 _text = str.dup; |
581 repaint(); | |
582 //TextChanged(EventArgs e) // TODO: add event | |
583 } | |
584 | |
585 /** | |
586 * Gets or sets the background color of this control. | |
587 */ | |
588 Color backColor() { return _backColor; } | |
589 /// ditto | |
590 void backColor(Color c) { | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
591 if(_backColor == c) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
592 return; |
0 | 593 _backColor = c; |
594 repaint(); | |
595 } | |
596 | |
597 /** | |
598 * Gets or sets the foreground color of this control. | |
599 */ | |
600 Color foreColor() { return _foreColor; } | |
601 /// ditto | |
602 void foreColor(Color c) { | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
603 if(_foreColor == c) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
604 return; |
0 | 605 _foreColor = c; |
606 repaint(); | |
607 } | |
608 | |
609 /** | |
610 * Gets or sets the font of this control uses to display text. A value of | |
611 * null means that the font is unset. When the font is null, the | |
612 * current theme's font is used. The default is null. | |
613 */ | |
614 Font font() { | |
615 // TODO: if font is null (unset), return from theme | |
616 //if(font is null) | |
617 // return Theme.Current.Control_Font(this); | |
618 //else | |
619 return _font; | |
620 } | |
621 /// ditto | |
622 void font(Font f) { | |
68
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
623 if(_font is f) |
6580fabb7dce
Setting a property to the value it already is shouldn't do anything
Jordan Miner <jminer7@gmail.com>
parents:
67
diff
changeset
|
624 return; |
0 | 625 _font = f; |
626 repaint(); | |
627 } | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
628 |
23
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
629 void setCurrentCursor(Cursor cur) { |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
630 if(parent) |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
631 parent.setCurrentCursor(cur); |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
632 } |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
633 /** |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
634 * Gets or sets the mouse cursor that will be shown when the mouse |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
635 * is over this control. |
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
636 */ |
0 | 637 Cursor cursor() { |
638 return _cursor; | |
639 } | |
23
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
640 /// ditto |
0 | 641 void cursor(Cursor cur) { |
642 if(_cursor is cur) | |
643 return; | |
644 _cursor = cur; | |
645 if(getHotControl() is this) | |
23
d55b5b998412
Implement built-in mouse cursors with X.
Jordan Miner <jminer7@gmail.com>
parents:
15
diff
changeset
|
646 setCurrentCursor(_cursor); |
0 | 647 } |
648 | |
649 /** | |
650 * Causes the part of the control inside the specified | |
651 * rectangle to be repainted. The rectangle is in content coordinates. | |
652 * | |
653 * The control will not be repainted before this method returns; rather, | |
654 * the area is just marked as needing to be repainted. The next time there | |
15 | 655 * are no other system events to be processed, a painting event will |
656 * be called. | |
0 | 657 */ |
658 void repaint(Rect rect) { | |
5
4029d5af7542
Add blank lines and rewrap some comments.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
659 // 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
|
660 // not invalidated |
0 | 661 if(_parent) |
662 _parent.repaint(rect + location); | |
663 } | |
664 /// ditto | |
665 void repaint(real x, real y, real width, real height) { | |
666 repaint(Rect(x, y, width, height)); | |
667 } | |
668 /** | |
669 * Causes the entire control to be repainted. | |
670 */ | |
671 void repaint() { | |
672 repaint(Rect(0, 0, width, height)); | |
673 } | |
674 } | |
675 | |
64 | 676 /** |
677 * Sets the tabIndex of each control to be subsequent numbers. The first control | |
678 * will have an index of startIndex. | |
679 */ | |
680 void setTabOrder(Control[] controls...) { | |
681 setTabOrder(0, controls); | |
682 } | |
683 /// ditto | |
684 void setTabOrder(int startIndex, Control[] controls...) { | |
685 foreach(c; controls) | |
686 c.tabIndex = startIndex++; | |
60
cf7c5f968306
Fix crash in withGraphics() if there wasn't a Window ancestor with a handle.
Jordan Miner <jminer7@gmail.com>
parents:
59
diff
changeset
|
687 } |
0 | 688 |
64 | 689 unittest { |
690 auto c1 = new Control; | |
691 auto c2 = new Control; | |
692 auto c3 = new Control; | |
693 | |
694 // test setTabOrder() | |
695 setTabOrder(c1, c2, c3); | |
696 assert(c1.tabIndex == 0); | |
697 assert(c2.tabIndex == 1); | |
698 assert(c3.tabIndex == 2); | |
699 | |
700 c1.withGraphics((Graphics g) { g.source = Color.Blue; }); | |
701 } | |
702 |