1 | // |
---|---|
2 | // EventDispatcher.cpp |
3 | // |
4 | // Library: XML |
5 | // Package: DOM |
6 | // Module: DOMEvents |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/DOM/EventDispatcher.h" |
16 | #include "Poco/DOM/Event.h" |
17 | #include "Poco/DOM/EventListener.h" |
18 | |
19 | |
20 | namespace |
21 | { |
22 | class DispatchGuard |
23 | { |
24 | public: |
25 | DispatchGuard(int& count): |
26 | _count(count) |
27 | { |
28 | ++_count; |
29 | } |
30 | |
31 | ~DispatchGuard() |
32 | { |
33 | --_count; |
34 | } |
35 | |
36 | private: |
37 | int& _count; |
38 | }; |
39 | } |
40 | |
41 | |
42 | namespace Poco { |
43 | namespace XML { |
44 | |
45 | |
46 | EventDispatcher::EventDispatcher(): |
47 | _inDispatch(0) |
48 | { |
49 | } |
50 | |
51 | |
52 | EventDispatcher::~EventDispatcher() |
53 | { |
54 | } |
55 | |
56 | |
57 | void EventDispatcher::addEventListener(const XMLString& type, EventListener* listener, bool useCapture) |
58 | { |
59 | EventListenerItem item; |
60 | item.type = type; |
61 | item.pListener = listener; |
62 | item.useCapture = useCapture; |
63 | _listeners.push_front(item); |
64 | } |
65 | |
66 | |
67 | void EventDispatcher::removeEventListener(const XMLString& type, EventListener* listener, bool useCapture) |
68 | { |
69 | EventListenerList::iterator it = _listeners.begin(); |
70 | while (it != _listeners.end()) |
71 | { |
72 | if (it->type == type && it->pListener == listener && it->useCapture == useCapture) |
73 | { |
74 | it->pListener = 0; |
75 | } |
76 | if (!_inDispatch && !it->pListener) |
77 | { |
78 | EventListenerList::iterator del = it++; |
79 | _listeners.erase(del); |
80 | } |
81 | else ++it; |
82 | } |
83 | } |
84 | |
85 | |
86 | void EventDispatcher::dispatchEvent(Event* evt) |
87 | { |
88 | DispatchGuard guard(_inDispatch); |
89 | EventListenerList::iterator it = _listeners.begin(); |
90 | while (it != _listeners.end()) |
91 | { |
92 | if (it->pListener && it->type == evt->type()) |
93 | { |
94 | it->pListener->handleEvent(evt); |
95 | } |
96 | if (!it->pListener) |
97 | { |
98 | EventListenerList::iterator del = it++; |
99 | _listeners.erase(del); |
100 | } |
101 | else ++it; |
102 | } |
103 | } |
104 | |
105 | |
106 | void EventDispatcher::captureEvent(Event* evt) |
107 | { |
108 | DispatchGuard guard(_inDispatch); |
109 | EventListenerList::iterator it = _listeners.begin(); |
110 | while (it != _listeners.end()) |
111 | { |
112 | if (it->pListener && it->useCapture && it->type == evt->type()) |
113 | { |
114 | it->pListener->handleEvent(evt); |
115 | } |
116 | if (!it->pListener) |
117 | { |
118 | EventListenerList::iterator del = it++; |
119 | _listeners.erase(del); |
120 | } |
121 | else ++it; |
122 | } |
123 | } |
124 | |
125 | |
126 | void EventDispatcher::bubbleEvent(Event* evt) |
127 | { |
128 | DispatchGuard guard(_inDispatch); |
129 | EventListenerList::iterator it = _listeners.begin(); |
130 | while (it != _listeners.end()) |
131 | { |
132 | if (it->pListener && !it->useCapture && it->type == evt->type()) |
133 | { |
134 | it->pListener->handleEvent(evt); |
135 | } |
136 | if (!it->pListener) |
137 | { |
138 | EventListenerList::iterator del = it++; |
139 | _listeners.erase(del); |
140 | } |
141 | else ++it; |
142 | } |
143 | } |
144 | |
145 | |
146 | } } // namespace Poco::XML |
147 |