annotate dynamin/core/event.d @ 10:ccc108b25a0a

Convert to using a struct for events. Fix some comments too.
author Jordan Miner <jminer7@gmail.com>
date Wed, 15 Jul 2009 21:22:20 -0500
parents 1311fae1ca9b
children c2566ab82535
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
1 // Written in the D programming language
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
2 // www.digitalmars.com/d/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
3
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
4 /*
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
5 * The contents of this file are subject to the Mozilla Public License Version
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
6 * 1.1 (the "License"); you may not use this file except in compliance with
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
7 * the License. You may obtain a copy of the License at
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
8 * http://www.mozilla.org/MPL/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
9 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
10 * Software distributed under the License is distributed on an "AS IS" basis,
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
12 * for the specific language governing rights and limitations under the
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
13 * License.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
14 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
15 * The Original Code is the Dynamin library.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
16 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
17 * The Initial Developer of the Original Code is Jordan Miner.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
19 * the Initial Developer. All Rights Reserved.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
20 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
21 * Contributor(s):
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
22 * Jordan Miner <jminer7@gmail.com>
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
23 *
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
24 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
25
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
26 module dynamin.core.event;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
27
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
28 import tango.io.Stdout;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
29 import dynamin.core.global;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
32
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
35 * Here is an example of its usage:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
36 * -----
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
37 * class Control {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
38 * public {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
39 * protected void whenPainting(PaintingEventArgs e) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
40 * /+ painting code goes here +/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
45 * }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
46 * }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
47 * }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
48 * -----
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
49 * Then to add a handler to it:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
50 * -----
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
51 * Control control = new Control();
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
52 * control.painting += (PaintingEventArgs e) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
53 * /+ painting code goes here +/
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
54 * };
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
55 * -----
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
56 * And fire it in the Control like this:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
57 * -----
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
58 * painting(new PaintingEventArgs());
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
59 * -----
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
60 * Not as easy to use as I would like, but still much better than Java's
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
61 * event handling.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
62 * When the event is fired, all the handlers are called first, followed
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
63 * by the delegate passed into the constructor.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
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
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
71
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
72 // 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
73 Handler[] handlers;
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
74 ///
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
75 Handler mainHandler;
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 Dispatcher dispatcher;
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
78 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
79 callHandlers(e);
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
80 callMainHandler(e);
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
81 }
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
82
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
83 /**
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
84 * Calls all the handlers added to this event, passing e to them.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
85 */
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
86 void opCall(ArgsType e) {
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
87 if(e is null)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
88 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
89 if(!dispatcher.funcptr)
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
90 defaultDispatch(e);
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
91 else
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
92 dispatcher(e);
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
93 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
94 /**
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
95 * 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
96 * when the event is fired.
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
97 */
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
98 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
99 if(!handler.funcptr) throw new Exception("handler cannot be null");
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
100 handlers.length = handlers.length + 1;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
101 handlers[length-1] = handler;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
102 // TODO: use a list?
7
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
103 //handlers.add(handler);
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
104 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
105 /// ditto
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
106 void opAddAssign(void delegate() handler) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
107 struct Foo {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
108 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
109 void wrapper(ArgsType e) { handler(); }
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
110 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
111 Foo* f = new Foo;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
112 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
113 *this += &f.wrapper;
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
114 // 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
115 //this += (ArgsType e) { handler(); };
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
116 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
117 /// 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
118 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
119 throw new Exception("removing handlers not yet implemented");
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
120 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
121 /**
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
122 * Calls the handlers (not including the main handler) added to this event.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
123 * Only use this method from a method that does custom dispatching.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
124 */
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
125 void callHandlers(ArgsType e) {
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
126 foreach(handler; handlers)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
127 handler(e);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
128 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
129 /**
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
130 * Calls the main handler unless the StopEventArgs.stopped has been
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
131 * set to true.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
132 * Only use this method from a method that does custom dispatching.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
133 */
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
134 void callMainHandler(ArgsType e) {
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
135 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
136 // if e is an instance of StopEventArgs, then check if it is stopped
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
137 if(stopEventArgs is null || !stopEventArgs.stopped)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
138 mainHandler(e);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
139 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
140 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
141
7
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
142 // usage: mixin Event!(whenMoved) moved;
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
143 // mixin Event!(whenMoved, dispatchMoved) moved;
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
144 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
145 private:
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
146 alias ParameterTupleOf!(mainHandler)[0] ArgsType;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
147 /// void delegate(ArgsType e)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
148 public alias void delegate(ArgsType e) Handler;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
149 /// void delegate(ArgsType e)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
150 public alias void delegate(ArgsType e) Dispatcher;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
151
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
152 // TODO: use a list-like struct?
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
153 Handler[] handlers;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
154 public:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
155
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
156 /**
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
157 * Calls all the handlers added to this event, passing e to them.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
158 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
159 void opCall(ArgsType e) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
160 if(e is null)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
161 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
162 dispatcher(e);
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
163 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
164 /**
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
165 * Adds the specified handler to this event. The handler will be called
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
166 * when the event is fired.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
167 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
168 void opAddAssign(Handler handler) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
169 if(!handler.funcptr) throw new Exception("handler cannot be null");
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
170 handlers.length = handlers.length + 1;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
171 handlers[length-1] = handler;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
172 // TODO: use a list?
7
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
173 //handlers.add(handler);
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
174 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
175 /// ditto
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
176 void opAddAssign(void delegate() handler) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
177 struct Foo {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
178 void delegate() handler;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
179 void wrapper(ArgsType e) { handler(); }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
180 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
181 Foo* f = new Foo;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
182 f.handler = handler;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
183 this += &f.wrapper;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
184 // I really wish D could do this:
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
185 //this += (ArgsType e) { handler(); };
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
186 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
187 /// TODO: implement this method
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
188 void opSubAssign(Handler handler) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
189 throw new Exception("removing handlers not yet implemented");
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
190 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
191 /**
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
192 * Calls the handlers (not including the main handler) added to this event.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
193 * Only use this method from a method that does custom dispatching.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
194 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
195 void callHandlers(ArgsType e) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
196 foreach(handler; handlers)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
197 handler(e);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
198 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
199 /**
7
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
200 * Calls the main handler unless the StopEventArgs.stopped has been
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
201 * set to true.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
202 * Only use this method from a method that does custom dispatching.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
203 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
204 void callMainHandler(ArgsType e) {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
205 auto stopEventArgs = cast(StopEventArgs)e;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
206 // if e is an instance of StopEventArgs, then check if it is stopped
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
207 if(stopEventArgs is null || !stopEventArgs.stopped)
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
208 mainHandler(e);
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
209 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
210 }
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
211
7
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
212 template Event2(alias mainHandler) {
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
213 alias ParameterTupleOf!(mainHandler)[0] ArgsType;
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
214 void defaultDispatch(ArgsType e) {
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
215 callHandlers(e);
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
216 callMainHandler(e);
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
217 }
1311fae1ca9b Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents: 0
diff changeset
218 mixin Event2!(mainHandler, defaultDispatch);
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
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
221 /// The base class for passing arguments to event handlers.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
222 // TODO: shorter name?
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
223 class EventArgs {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
224 }
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
225 ///
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
226 class StopEventArgs : EventArgs {
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
227 /**
10
ccc108b25a0a Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents: 7
diff changeset
228 * 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
229 * 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
230 * but a handler sets stopped to true, the text box will not
0
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
231 * respond to the key.
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
232 */
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
233 bool stopped = false;
aa4efef0f0b1 Initial commit of code.
Jordan Miner <jminer7@gmail.com>
parents:
diff changeset
234 }