Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <eventdispatcher.hxx>
21 :
22 : #include <event.hxx>
23 : #include <mutationevent.hxx>
24 : #include <uievent.hxx>
25 : #include <mouseevent.hxx>
26 :
27 : #include "../dom/document.hxx"
28 :
29 :
30 : namespace DOM { namespace events {
31 :
32 0 : void CEventDispatcher::addListener(xmlNodePtr pNode, const OUString& aType, const Reference<XEventListener>& aListener, sal_Bool bCapture)
33 : {
34 : TypeListenerMap *const pTMap = (bCapture)
35 0 : ? (& m_CaptureListeners) : (& m_TargetListeners);
36 :
37 : // get the multimap for the specified type
38 0 : ListenerMap *pMap = 0;
39 0 : TypeListenerMap::const_iterator tIter = pTMap->find(aType);
40 0 : if (tIter == pTMap->end()) {
41 : // the map has to be created
42 0 : pMap = new ListenerMap();
43 0 : pTMap->insert(TypeListenerMap::value_type(aType, pMap));
44 : } else {
45 0 : pMap = tIter->second;
46 : }
47 0 : if (pMap !=0)
48 0 : pMap->insert(ListenerMap::value_type(pNode, aListener));
49 0 : }
50 :
51 0 : void CEventDispatcher::removeListener(xmlNodePtr pNode, const OUString& aType, const Reference<XEventListener>& aListener, sal_Bool bCapture)
52 : {
53 : TypeListenerMap *const pTMap = (bCapture)
54 0 : ? (& m_CaptureListeners) : (& m_TargetListeners);
55 :
56 : // get the multimap for the specified type
57 0 : TypeListenerMap::const_iterator tIter = pTMap->find(aType);
58 0 : if (tIter != pTMap->end()) {
59 0 : ListenerMap *pMap = tIter->second;
60 : // find listeners of specied type for specified node
61 0 : ListenerMap::iterator iter = pMap->find(pNode);
62 0 : while (iter != pMap->end() && iter->first == pNode)
63 : {
64 : // erase all references to specified listener
65 0 : if ((iter->second).is() && iter->second == aListener)
66 : {
67 0 : ListenerMap::iterator tmp_iter = iter;
68 0 : ++iter;
69 0 : pMap->erase(tmp_iter);
70 : }
71 : else
72 0 : ++iter;
73 : }
74 : }
75 0 : }
76 :
77 0 : CEventDispatcher::~CEventDispatcher()
78 : {
79 : // delete the multimaps for the various types
80 0 : for (TypeListenerMap::iterator aI = m_CaptureListeners.begin(); aI != m_CaptureListeners.end(); ++aI)
81 0 : delete aI->second;
82 :
83 0 : for (TypeListenerMap::iterator aI = m_TargetListeners.begin(); aI != m_TargetListeners.end(); ++aI)
84 0 : delete aI->second;
85 0 : }
86 :
87 0 : void CEventDispatcher::callListeners(
88 : TypeListenerMap const& rTMap,
89 : xmlNodePtr const pNode,
90 : const OUString& aType, Reference< XEvent > const& xEvent)
91 : {
92 : // get the multimap for the specified type
93 0 : TypeListenerMap::const_iterator tIter = rTMap.find(aType);
94 0 : if (tIter != rTMap.end()) {
95 0 : ListenerMap *pMap = tIter->second;
96 0 : ListenerMap::const_iterator iter = pMap->lower_bound(pNode);
97 0 : ListenerMap::const_iterator ibound = pMap->upper_bound(pNode);
98 0 : for( ; iter != ibound; ++iter )
99 : {
100 0 : if((iter->second).is())
101 0 : (iter->second)->handleEvent(xEvent);
102 : }
103 : }
104 0 : }
105 :
106 0 : bool CEventDispatcher::dispatchEvent(
107 : DOM::CDocument & rDocument, ::osl::Mutex & rMutex,
108 : xmlNodePtr const pNode, Reference<XNode> const& xNode,
109 : Reference< XEvent > const& i_xEvent) const
110 : {
111 0 : CEvent *pEvent = 0; // pointer to internal event representation
112 :
113 0 : OUString const aType = i_xEvent->getType();
114 0 : if (aType.equalsAscii("DOMSubtreeModified") ||
115 0 : aType.equalsAscii("DOMNodeInserted") ||
116 0 : aType.equalsAscii("DOMNodeRemoved") ||
117 0 : aType.equalsAscii("DOMNodeRemovedFromDocument") ||
118 0 : aType.equalsAscii("DOMNodeInsertedIntoDocument") ||
119 0 : aType.equalsAscii("DOMAttrModified") ||
120 0 : aType.equalsAscii("DOMCharacterDataModified") )
121 : {
122 : Reference< XMutationEvent > const aMEvent(i_xEvent,
123 0 : UNO_QUERY_THROW);
124 : // dispatch a mutation event
125 : // we need to clone the event in order to have complete control
126 : // over the implementation
127 0 : CMutationEvent* pMEvent = new CMutationEvent;
128 : pMEvent->initMutationEvent(
129 0 : aType, aMEvent->getBubbles(), aMEvent->getCancelable(),
130 0 : aMEvent->getRelatedNode(), aMEvent->getPrevValue(),
131 0 : aMEvent->getNewValue(), aMEvent->getAttrName(),
132 0 : aMEvent->getAttrChange());
133 0 : pEvent = pMEvent;
134 0 : } else if ( // UIEvent
135 0 : aType.equalsAscii("DOMFocusIn") ||
136 0 : aType.equalsAscii("DOMFocusOut") ||
137 0 : aType.equalsAscii("DOMActivate") )
138 : {
139 0 : Reference< XUIEvent > const aUIEvent(i_xEvent, UNO_QUERY_THROW);
140 0 : CUIEvent* pUIEvent = new CUIEvent;
141 : pUIEvent->initUIEvent(aType,
142 0 : aUIEvent->getBubbles(), aUIEvent->getCancelable(),
143 0 : aUIEvent->getView(), aUIEvent->getDetail());
144 0 : pEvent = pUIEvent;
145 0 : } else if ( // MouseEvent
146 0 : aType.equalsAscii("click") ||
147 0 : aType.equalsAscii("mousedown") ||
148 0 : aType.equalsAscii("mouseup") ||
149 0 : aType.equalsAscii("mouseover") ||
150 0 : aType.equalsAscii("mousemove") ||
151 0 : aType.equalsAscii("mouseout") )
152 : {
153 : Reference< XMouseEvent > const aMouseEvent(i_xEvent,
154 0 : UNO_QUERY_THROW);
155 0 : CMouseEvent *pMouseEvent = new CMouseEvent;
156 : pMouseEvent->initMouseEvent(aType,
157 0 : aMouseEvent->getBubbles(), aMouseEvent->getCancelable(),
158 0 : aMouseEvent->getView(), aMouseEvent->getDetail(),
159 0 : aMouseEvent->getScreenX(), aMouseEvent->getScreenY(),
160 0 : aMouseEvent->getClientX(), aMouseEvent->getClientY(),
161 0 : aMouseEvent->getCtrlKey(), aMouseEvent->getAltKey(),
162 0 : aMouseEvent->getShiftKey(), aMouseEvent->getMetaKey(),
163 0 : aMouseEvent->getButton(), aMouseEvent->getRelatedTarget());
164 0 : pEvent = pMouseEvent;
165 : }
166 : else // generic event
167 : {
168 0 : pEvent = new CEvent;
169 : pEvent->initEvent(
170 0 : aType, i_xEvent->getBubbles(), i_xEvent->getCancelable());
171 : }
172 0 : pEvent->m_target.set(xNode, UNO_QUERY_THROW);
173 0 : pEvent->m_currentTarget = i_xEvent->getCurrentTarget();
174 0 : pEvent->m_time = i_xEvent->getTimeStamp();
175 :
176 : // create the reference to the provate event implementation
177 : // that will be dispatched to the listeners
178 0 : Reference< XEvent > const xEvent(pEvent);
179 :
180 : // build the path from target node to the root
181 : typedef std::vector< ::std::pair<Reference<XEventTarget>, xmlNodePtr> >
182 : NodeVector_t;
183 0 : NodeVector_t captureVector;
184 0 : TypeListenerMap captureListeners;
185 0 : TypeListenerMap targetListeners;
186 : {
187 0 : ::osl::MutexGuard g(rMutex);
188 :
189 0 : xmlNodePtr cur = pNode;
190 0 : while (cur != NULL)
191 : {
192 : Reference< XEventTarget > const xRef(
193 0 : rDocument.GetCNode(cur).get());
194 0 : captureVector.push_back(::std::make_pair(xRef, cur));
195 0 : cur = cur->parent;
196 0 : }
197 0 : captureListeners = m_CaptureListeners;
198 0 : targetListeners = m_TargetListeners;
199 : }
200 :
201 : // the caputre vector now holds the node path from target to root
202 : // first we must search for capture listernes in order root to
203 : // to target. after that, any target listeners have to be called
204 : // then bubbeling phase listeners are called in target to root
205 : // order
206 : // start at the root
207 : NodeVector_t::const_reverse_iterator rinode =
208 0 : const_cast<NodeVector_t const&>(captureVector).rbegin();
209 0 : if (rinode != const_cast<NodeVector_t const&>(captureVector).rend())
210 : {
211 : // capturing phase:
212 0 : pEvent->m_phase = PhaseType_CAPTURING_PHASE;
213 0 : while (rinode !=
214 : const_cast<NodeVector_t const&>(captureVector).rend())
215 : {
216 0 : pEvent->m_currentTarget = rinode->first;
217 0 : callListeners(captureListeners, rinode->second, aType, xEvent);
218 0 : if (pEvent->m_canceled) return true;
219 0 : ++rinode;
220 : }
221 :
222 0 : NodeVector_t::const_iterator inode = captureVector.begin();
223 :
224 : // target phase
225 0 : pEvent->m_phase = PhaseType_AT_TARGET;
226 0 : pEvent->m_currentTarget = inode->first;
227 0 : callListeners(targetListeners, inode->second, aType, xEvent);
228 0 : if (pEvent->m_canceled) return true;
229 : // bubbeling phase
230 0 : ++inode;
231 0 : if (i_xEvent->getBubbles()) {
232 0 : pEvent->m_phase = PhaseType_BUBBLING_PHASE;
233 0 : while (inode != captureVector.end())
234 : {
235 0 : pEvent->m_currentTarget = inode->first;
236 : callListeners(targetListeners,
237 0 : inode->second, aType, xEvent);
238 0 : if (pEvent->m_canceled) return true;
239 0 : ++inode;
240 : }
241 : }
242 : }
243 0 : return true;
244 : }
245 : }}
246 :
247 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|