1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Xml;
|
---|
5 |
|
---|
6 | using SharpVectors.Dom.Events;
|
---|
7 | using SharpVectors.Dom.Svg;
|
---|
8 |
|
---|
9 | namespace SharpVectors.Dom.Events
|
---|
10 | {
|
---|
11 | /// <summary>
|
---|
12 | /// Summary description for EventTarget.
|
---|
13 | /// </summary>
|
---|
14 | public class EventTarget : IEventTargetSupport
|
---|
15 | {
|
---|
16 | #region Private Fields
|
---|
17 |
|
---|
18 | private IEventTargetSupport eventTarget;
|
---|
19 |
|
---|
20 | private EventListenerMap captureMap;
|
---|
21 | private EventListenerMap bubbleMap;
|
---|
22 | private ArrayList _ancestors;
|
---|
23 |
|
---|
24 | #endregion
|
---|
25 |
|
---|
26 | #region Constructors
|
---|
27 |
|
---|
28 | public EventTarget(IEventTargetSupport eventTarget)
|
---|
29 | {
|
---|
30 | this.eventTarget = eventTarget;
|
---|
31 | this.captureMap = new EventListenerMap();
|
---|
32 | this.bubbleMap = new EventListenerMap();
|
---|
33 | this._ancestors = new ArrayList();
|
---|
34 | }
|
---|
35 |
|
---|
36 | #endregion
|
---|
37 |
|
---|
38 | #region IEventTarget interface
|
---|
39 |
|
---|
40 | #region Methods
|
---|
41 |
|
---|
42 | #region DOM Level 2
|
---|
43 |
|
---|
44 | public void AddEventListener(string type, EventListener listener,
|
---|
45 | bool useCapture)
|
---|
46 | {
|
---|
47 | if (useCapture)
|
---|
48 | {
|
---|
49 | captureMap.AddEventListener(null, type, null, listener);
|
---|
50 | }
|
---|
51 | else
|
---|
52 | {
|
---|
53 | bubbleMap.AddEventListener(null, type, null, listener);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void RemoveEventListener(string type, EventListener listener,
|
---|
58 | bool useCapture)
|
---|
59 | {
|
---|
60 | if (useCapture)
|
---|
61 | {
|
---|
62 | captureMap.RemoveEventListener(null, type, listener);
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | bubbleMap.RemoveEventListener(null, type, listener);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public bool DispatchEvent(IEvent eventInfo)
|
---|
71 | {
|
---|
72 | if (eventInfo.Type == null || eventInfo.Type == "")
|
---|
73 | {
|
---|
74 | throw new EventException(EventExceptionCode.UnspecifiedEventTypeErr);
|
---|
75 | }
|
---|
76 | try
|
---|
77 | {
|
---|
78 | // The actual target may be an SvgElement or an SvgElementInstance from
|
---|
79 | // a conceptual tree <see href="http://www.w3.org/TR/SVG/struct.html#UseElement"/>
|
---|
80 | XmlNode currNode = null;
|
---|
81 | ISvgElementInstance currInstance = null;
|
---|
82 |
|
---|
83 | if (this.eventTarget is ISvgElementInstance)
|
---|
84 | currInstance = (ISvgElementInstance)this.eventTarget;
|
---|
85 | else
|
---|
86 | currNode = (XmlNode)this.eventTarget;
|
---|
87 |
|
---|
88 | // We can't use an XPath ancestor axe because we must account for
|
---|
89 | // conceptual nodes
|
---|
90 | _ancestors.Clear();
|
---|
91 |
|
---|
92 | // Build the ancestors from the conceptual tree
|
---|
93 | if (currInstance != null)
|
---|
94 | {
|
---|
95 | while (currInstance.ParentNode != null)
|
---|
96 | {
|
---|
97 | currInstance = currInstance.ParentNode;
|
---|
98 | _ancestors.Add(currInstance);
|
---|
99 | }
|
---|
100 | currNode = (XmlNode)currInstance.CorrespondingUseElement;
|
---|
101 | _ancestors.Add(currNode);
|
---|
102 | }
|
---|
103 |
|
---|
104 | // Build actual ancestors
|
---|
105 | while (currNode != null && currNode.ParentNode != null)
|
---|
106 | {
|
---|
107 | currNode = currNode.ParentNode;
|
---|
108 | _ancestors.Add(currNode);
|
---|
109 | }
|
---|
110 |
|
---|
111 | Event realEvent = (Event)eventInfo;
|
---|
112 | realEvent.eventTarget = this.eventTarget;
|
---|
113 |
|
---|
114 | if (!realEvent.stopped)
|
---|
115 | {
|
---|
116 | realEvent.eventPhase = EventPhase.CapturingPhase;
|
---|
117 |
|
---|
118 | for (int i = _ancestors.Count - 1; i >= 0; i--)
|
---|
119 | {
|
---|
120 | if (realEvent.stopped)
|
---|
121 | {
|
---|
122 | break;
|
---|
123 | }
|
---|
124 |
|
---|
125 | IEventTarget ancestor = _ancestors[i] as IEventTarget;
|
---|
126 |
|
---|
127 | if (ancestor != null)
|
---|
128 | {
|
---|
129 | realEvent.currentTarget = ancestor;
|
---|
130 |
|
---|
131 | if (ancestor is IEventTargetSupport)
|
---|
132 | {
|
---|
133 | ((IEventTargetSupport)ancestor).FireEvent(realEvent);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (!realEvent.stopped)
|
---|
140 | {
|
---|
141 | realEvent.eventPhase = EventPhase.AtTarget;
|
---|
142 | realEvent.currentTarget = this.eventTarget;
|
---|
143 | this.eventTarget.FireEvent(realEvent);
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (!realEvent.stopped)
|
---|
147 | {
|
---|
148 | realEvent.eventPhase = EventPhase.BubblingPhase;
|
---|
149 |
|
---|
150 | for (int i = 0; i < _ancestors.Count; i++)
|
---|
151 | {
|
---|
152 | if (realEvent.stopped)
|
---|
153 | {
|
---|
154 | break;
|
---|
155 | }
|
---|
156 |
|
---|
157 | IEventTarget ancestor = _ancestors[i] as IEventTarget;
|
---|
158 |
|
---|
159 | if (ancestor != null)
|
---|
160 | {
|
---|
161 | realEvent.currentTarget = ancestor;
|
---|
162 | ((IEventTargetSupport)ancestor).FireEvent(realEvent);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | return realEvent.stopped;
|
---|
168 | }
|
---|
169 | catch (InvalidCastException)
|
---|
170 | {
|
---|
171 | throw new DomException(DomExceptionType.WrongDocumentErr);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | #endregion
|
---|
176 |
|
---|
177 | #region DOM Level 3 Experimental
|
---|
178 |
|
---|
179 | public void AddEventListenerNs(string namespaceUri, string type,
|
---|
180 | EventListener listener, bool useCapture, object evtGroup)
|
---|
181 | {
|
---|
182 | if (useCapture)
|
---|
183 | {
|
---|
184 | captureMap.AddEventListener(namespaceUri, type, evtGroup, listener);
|
---|
185 | }
|
---|
186 | else
|
---|
187 | {
|
---|
188 | bubbleMap.AddEventListener(namespaceUri, type, evtGroup, listener);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | public void RemoveEventListenerNs(string namespaceUri, string type,
|
---|
193 | EventListener listener, bool useCapture)
|
---|
194 | {
|
---|
195 | if (useCapture)
|
---|
196 | {
|
---|
197 | captureMap.RemoveEventListener(namespaceUri, type, listener);
|
---|
198 | }
|
---|
199 | else
|
---|
200 | {
|
---|
201 | bubbleMap.RemoveEventListener(namespaceUri, type, listener);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | public bool WillTriggerNs(string namespaceUri, string type)
|
---|
206 | {
|
---|
207 | XmlNode node = (XmlNode)this.eventTarget;
|
---|
208 | XmlNodeList ancestors = node.SelectNodes("ancestor::node()");
|
---|
209 |
|
---|
210 | for (int i = 0; i < ancestors.Count; i++)
|
---|
211 | {
|
---|
212 | IEventTarget ancestor = ancestors[i] as IEventTarget;
|
---|
213 |
|
---|
214 | if (ancestor.HasEventListenerNs(namespaceUri, type))
|
---|
215 | {
|
---|
216 | return true;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | return false;
|
---|
221 | }
|
---|
222 |
|
---|
223 | public bool HasEventListenerNs(string namespaceUri, string eventType)
|
---|
224 | {
|
---|
225 | return captureMap.HasEventListenerNs(namespaceUri, eventType) ||
|
---|
226 | bubbleMap.HasEventListenerNs(namespaceUri, eventType);
|
---|
227 | }
|
---|
228 |
|
---|
229 | #endregion
|
---|
230 |
|
---|
231 | #endregion
|
---|
232 |
|
---|
233 | #region NON-DOM
|
---|
234 |
|
---|
235 | public void FireEvent(IEvent eventInfo)
|
---|
236 | {
|
---|
237 | switch (eventInfo.EventPhase)
|
---|
238 | {
|
---|
239 | case EventPhase.AtTarget:
|
---|
240 | case EventPhase.BubblingPhase:
|
---|
241 | bubbleMap.Lock();
|
---|
242 | bubbleMap.FireEvent(eventInfo);
|
---|
243 | bubbleMap.Unlock();
|
---|
244 | break;
|
---|
245 | case EventPhase.CapturingPhase:
|
---|
246 | captureMap.Lock();
|
---|
247 | captureMap.FireEvent(eventInfo);
|
---|
248 | captureMap.Unlock();
|
---|
249 | break;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | public event EventListener OnMouseMove
|
---|
254 | {
|
---|
255 | add
|
---|
256 | {
|
---|
257 | throw new NotImplementedException();
|
---|
258 | }
|
---|
259 | remove
|
---|
260 | {
|
---|
261 | throw new NotImplementedException();
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | #endregion
|
---|
266 |
|
---|
267 | #endregion
|
---|
268 | }
|
---|
269 | }
|
---|