Mercurial > projects > dynamin
annotate dynamin/core/event.d @ 56:c2566ab82535
Edit comments and fix a typo.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 08 Aug 2009 15:46:39 -0500 |
parents | ccc108b25a0a |
children | c9a4850926d9 |
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.core.event; | |
27 | |
28 import tango.io.Stdout; | |
29 import dynamin.core.global; | |
30 import tango.core.Exception; | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
31 public import tango.core.Traits; |
0 | 32 |
33 /** | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
34 * A struct used to notify handlers of an event. It is similar to .NET's events. |
0 | 35 * Here is an example of its usage: |
36 * ----- | |
37 * class Control { | |
38 * public { | |
39 * protected void whenPainting(PaintingEventArgs e) { | |
40 * /+ painting code goes here +/ | |
41 * } | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
42 * Event!(whenPainting) painting; |
0 | 43 * this() { |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
44 * painting.mainHandler = &whenPainting; |
0 | 45 * } |
46 * } | |
47 * } | |
48 * ----- | |
49 * Then to add a handler to it: | |
50 * ----- | |
51 * Control control = new Control(); | |
52 * control.painting += (PaintingEventArgs e) { | |
53 * /+ painting code goes here +/ | |
54 * }; | |
55 * ----- | |
56 * And fire it in the Control like this: | |
57 * ----- | |
58 * painting(new PaintingEventArgs()); | |
59 * ----- | |
60 * Not as easy to use as I would like, but still much better than Java's | |
61 * event handling. | |
62 * When the event is fired, all the handlers are called first, followed | |
63 * by the delegate passed into the constructor. | |
64 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
65 struct Event(alias mainHandler_) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
66 alias ParameterTupleOf!(mainHandler_)[0] ArgsType; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
67 /// void delegate(ArgsType e) |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
68 public alias void delegate(ArgsType e) Handler; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
69 /// void delegate(ArgsType e) |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
70 public alias void delegate(ArgsType e) Dispatcher; |
0 | 71 |
56
c2566ab82535
Edit comments and fix a typo.
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
72 // TODO: could save memory by storing mainHandler in handlers? |
c2566ab82535
Edit comments and fix a typo.
Jordan Miner <jminer7@gmail.com>
parents:
10
diff
changeset
|
73 // putting 15 events (24 bytes each) in every control takes memory |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
74 // TODO: use a list-like struct? |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
75 Handler[] handlers; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
76 /// |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
77 Handler mainHandler; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
78 /// |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
79 Dispatcher dispatcher; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
80 void defaultDispatch(ArgsType e) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
81 callHandlers(e); |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
82 callMainHandler(e); |
0 | 83 } |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
84 |
0 | 85 /** |
86 * Calls all the handlers added to this event, passing e to them. | |
87 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
88 void opCall(ArgsType e) { |
0 | 89 if(e is null) |
90 Stdout("Warning: EventArgs null").newline; | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
91 if(!dispatcher.funcptr) |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
92 defaultDispatch(e); |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
93 else |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
94 dispatcher(e); |
0 | 95 } |
96 /** | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
97 * Adds the specified handler to this event. The handler will be called |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
98 * when the event is fired. |
0 | 99 */ |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
100 void opAddAssign(Handler handler) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
101 if(!handler.funcptr) throw new Exception("handler cannot be null"); |
0 | 102 handlers.length = handlers.length + 1; |
103 handlers[length-1] = handler; | |
104 // TODO: use a list? | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
105 //handlers.add(handler); |
0 | 106 } |
107 /// ditto | |
108 void opAddAssign(void delegate() handler) { | |
109 struct Foo { | |
110 void delegate() handler; | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
111 void wrapper(ArgsType e) { handler(); } |
0 | 112 } |
113 Foo* f = new Foo; | |
114 f.handler = handler; | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
115 *this += &f.wrapper; |
0 | 116 // I really wish D could do this: |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
117 //this += (ArgsType e) { handler(); }; |
0 | 118 } |
119 /// TODO: implement this method | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
120 void opSubAssign(Handler handler) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
121 throw new Exception("removing handlers not yet implemented"); |
0 | 122 } |
123 /** | |
124 * Calls the handlers (not including the main handler) added to this event. | |
125 * Only use this method from a method that does custom dispatching. | |
126 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
127 void callHandlers(ArgsType e) { |
0 | 128 foreach(handler; handlers) |
129 handler(e); | |
130 } | |
131 /** | |
132 * Calls the main handler unless the StopEventArgs.stopped has been | |
133 * set to true. | |
134 * Only use this method from a method that does custom dispatching. | |
135 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
136 void callMainHandler(ArgsType e) { |
0 | 137 auto stopEventArgs = cast(StopEventArgs)e; |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
138 // if e is an instance of StopEventArgs, then check if it is stopped |
0 | 139 if(stopEventArgs is null || !stopEventArgs.stopped) |
140 mainHandler(e); | |
141 } | |
142 } | |
143 | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
144 // usage: mixin Event!(whenMoved) moved; |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
145 // mixin Event!(whenMoved, dispatchMoved) moved; |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
146 template Event2(alias mainHandler, alias dispatcher) { |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
147 private: |
0 | 148 alias ParameterTupleOf!(mainHandler)[0] ArgsType; |
149 /// void delegate(ArgsType e) | |
150 public alias void delegate(ArgsType e) Handler; | |
151 /// void delegate(ArgsType e) | |
152 public alias void delegate(ArgsType e) Dispatcher; | |
153 | |
154 // TODO: use a list-like struct? | |
155 Handler[] handlers; | |
156 public: | |
157 | |
158 /** | |
159 * Calls all the handlers added to this event, passing e to them. | |
160 */ | |
161 void opCall(ArgsType e) { | |
162 if(e is null) | |
163 Stdout("Warning: EventArgs null").newline; | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
164 dispatcher(e); |
0 | 165 } |
166 /** | |
167 * Adds the specified handler to this event. The handler will be called | |
168 * when the event is fired. | |
169 */ | |
170 void opAddAssign(Handler handler) { | |
171 if(!handler.funcptr) throw new Exception("handler cannot be null"); | |
172 handlers.length = handlers.length + 1; | |
173 handlers[length-1] = handler; | |
174 // TODO: use a list? | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
175 //handlers.add(handler); |
0 | 176 } |
177 /// ditto | |
178 void opAddAssign(void delegate() handler) { | |
179 struct Foo { | |
180 void delegate() handler; | |
181 void wrapper(ArgsType e) { handler(); } | |
182 } | |
183 Foo* f = new Foo; | |
184 f.handler = handler; | |
185 this += &f.wrapper; | |
186 // I really wish D could do this: | |
187 //this += (ArgsType e) { handler(); }; | |
188 } | |
189 /// TODO: implement this method | |
190 void opSubAssign(Handler handler) { | |
191 throw new Exception("removing handlers not yet implemented"); | |
192 } | |
193 /** | |
194 * Calls the handlers (not including the main handler) added to this event. | |
195 * Only use this method from a method that does custom dispatching. | |
196 */ | |
197 void callHandlers(ArgsType e) { | |
198 foreach(handler; handlers) | |
199 handler(e); | |
200 } | |
201 /** | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
202 * Calls the main handler unless the StopEventArgs.stopped has been |
0 | 203 * set to true. |
204 * Only use this method from a method that does custom dispatching. | |
205 */ | |
206 void callMainHandler(ArgsType e) { | |
207 auto stopEventArgs = cast(StopEventArgs)e; | |
208 // if e is an instance of StopEventArgs, then check if it is stopped | |
209 if(stopEventArgs is null || !stopEventArgs.stopped) | |
210 mainHandler(e); | |
211 } | |
212 } | |
213 | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
214 template Event2(alias mainHandler) { |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
215 alias ParameterTupleOf!(mainHandler)[0] ArgsType; |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
216 void defaultDispatch(ArgsType e) { |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
217 callHandlers(e); |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
218 callMainHandler(e); |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
219 } |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
220 mixin Event2!(mainHandler, defaultDispatch); |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
221 } |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
222 |
0 | 223 /// The base class for passing arguments to event handlers. |
224 // TODO: shorter name? | |
225 class EventArgs { | |
226 } | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
227 /// |
0 | 228 class StopEventArgs : EventArgs { |
229 /** | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
230 * If stopped is set to true, then the Control will not respond to |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
231 * the event. For instance, if a key is typed while a text box is focused, |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
232 * but a handler sets stopped to true, the text box will not |
0 | 233 * respond to the key. |
234 */ | |
235 bool stopped = false; | |
236 } |