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