1 | // |
---|
2 | // EventDispatcher.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/XML/src/EventDispatcher.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: XML |
---|
7 | // Package: DOM |
---|
8 | // Module: DOMEvents |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/DOM/EventDispatcher.h> |
---|
38 | #include <Poco/DOM/Event.h> |
---|
39 | #include <Poco/DOM/EventListener.h> |
---|
40 | |
---|
41 | |
---|
42 | namespace |
---|
43 | { |
---|
44 | class DispatchGuard |
---|
45 | { |
---|
46 | public: |
---|
47 | DispatchGuard(int& count): |
---|
48 | _count(count) |
---|
49 | { |
---|
50 | ++_count; |
---|
51 | } |
---|
52 | |
---|
53 | ~DispatchGuard() |
---|
54 | { |
---|
55 | --_count; |
---|
56 | } |
---|
57 | |
---|
58 | private: |
---|
59 | int& _count; |
---|
60 | }; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | namespace Poco { |
---|
65 | namespace XML { |
---|
66 | |
---|
67 | |
---|
68 | EventDispatcher::EventDispatcher(): |
---|
69 | _inDispatch(0) |
---|
70 | { |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | EventDispatcher::~EventDispatcher() |
---|
75 | { |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void EventDispatcher::addEventListener(const XMLString& type, EventListener* listener, bool useCapture) |
---|
80 | { |
---|
81 | EventListenerItem item; |
---|
82 | item.type = type; |
---|
83 | item.pListener = listener; |
---|
84 | item.useCapture = useCapture; |
---|
85 | _listeners.push_front(item); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void EventDispatcher::removeEventListener(const XMLString& type, EventListener* listener, bool useCapture) |
---|
90 | { |
---|
91 | EventListenerList::iterator it = _listeners.begin(); |
---|
92 | while (it != _listeners.end()) |
---|
93 | { |
---|
94 | if (it->type == type && it->pListener == listener && it->useCapture == useCapture) |
---|
95 | { |
---|
96 | it->pListener = 0; |
---|
97 | } |
---|
98 | if (!_inDispatch && !it->pListener) |
---|
99 | { |
---|
100 | EventListenerList::iterator del = it++; |
---|
101 | _listeners.erase(del); |
---|
102 | } |
---|
103 | else ++it; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | void EventDispatcher::dispatchEvent(Event* evt) |
---|
109 | { |
---|
110 | DispatchGuard guard(_inDispatch); |
---|
111 | EventListenerList::iterator it = _listeners.begin(); |
---|
112 | while (it != _listeners.end()) |
---|
113 | { |
---|
114 | if (it->pListener && it->type == evt->type()) |
---|
115 | { |
---|
116 | it->pListener->handleEvent(evt); |
---|
117 | } |
---|
118 | if (!it->pListener) |
---|
119 | { |
---|
120 | EventListenerList::iterator del = it++; |
---|
121 | _listeners.erase(del); |
---|
122 | } |
---|
123 | else ++it; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void EventDispatcher::captureEvent(Event* evt) |
---|
129 | { |
---|
130 | DispatchGuard guard(_inDispatch); |
---|
131 | EventListenerList::iterator it = _listeners.begin(); |
---|
132 | while (it != _listeners.end()) |
---|
133 | { |
---|
134 | if (it->pListener && it->useCapture && it->type == evt->type()) |
---|
135 | { |
---|
136 | it->pListener->handleEvent(evt); |
---|
137 | } |
---|
138 | if (!it->pListener) |
---|
139 | { |
---|
140 | EventListenerList::iterator del = it++; |
---|
141 | _listeners.erase(del); |
---|
142 | } |
---|
143 | else ++it; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | void EventDispatcher::bubbleEvent(Event* evt) |
---|
149 | { |
---|
150 | DispatchGuard guard(_inDispatch); |
---|
151 | EventListenerList::iterator it = _listeners.begin(); |
---|
152 | while (it != _listeners.end()) |
---|
153 | { |
---|
154 | if (it->pListener && !it->useCapture && it->type == evt->type()) |
---|
155 | { |
---|
156 | it->pListener->handleEvent(evt); |
---|
157 | } |
---|
158 | if (!it->pListener) |
---|
159 | { |
---|
160 | EventListenerList::iterator del = it++; |
---|
161 | _listeners.erase(del); |
---|
162 | } |
---|
163 | else ++it; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | } } // namespace Poco::XML |
---|