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