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