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 :
21 : #include "TaskPaneFocusManager.hxx"
22 :
23 : #include <vcl/window.hxx>
24 : #include <vcl/svapp.hxx>
25 : #include <vcl/event.hxx>
26 : #include <rtl/instance.hxx>
27 : #include <boost/unordered_map.hpp>
28 :
29 : namespace {
30 :
31 : class WindowHash
32 : {
33 : public:
34 0 : size_t operator()(const ::Window* argument) const
35 0 : { return reinterpret_cast<uintptr_t>(argument); }
36 : };
37 :
38 : class EventDescriptor
39 : {
40 : public:
41 0 : EventDescriptor (const KeyCode& rKey, ::Window* pWindow)
42 0 : : maKeyCode(rKey), mpTargetWindow(pWindow) {}
43 : KeyCode maKeyCode;
44 : ::Window* mpTargetWindow;
45 : };
46 :
47 : } // end of anonymous namespace
48 :
49 :
50 :
51 :
52 : namespace sd { namespace toolpanel {
53 :
54 :
55 :
56 0 : class FocusManager::LinkMap
57 : : public ::boost::unordered_multimap< ::Window*, EventDescriptor, WindowHash>
58 : {
59 : };
60 :
61 0 : struct FocusManagerCreator
62 : {
63 : FocusManager m_aFocusManager;
64 : };
65 :
66 : namespace
67 : {
68 : class theFocusManagerInstance : public rtl::Static< FocusManagerCreator, theFocusManagerInstance> {};
69 : }
70 :
71 0 : FocusManager& FocusManager::Instance (void)
72 : {
73 0 : return theFocusManagerInstance::get().m_aFocusManager;
74 : }
75 :
76 :
77 :
78 :
79 0 : FocusManager::FocusManager (void)
80 0 : : mpLinks(new LinkMap())
81 : {
82 0 : }
83 :
84 :
85 :
86 :
87 0 : FocusManager::~FocusManager (void)
88 : {
89 0 : Clear();
90 0 : }
91 :
92 :
93 :
94 :
95 0 : void FocusManager::Clear (void)
96 : {
97 0 : if (mpLinks.get() != NULL)
98 : {
99 0 : while ( ! mpLinks->empty())
100 : {
101 0 : ::Window* pWindow = mpLinks->begin()->first;
102 0 : if (pWindow == NULL)
103 : {
104 0 : mpLinks->erase(mpLinks->begin());
105 : }
106 : else
107 : {
108 0 : RemoveLinks(pWindow);
109 : }
110 : }
111 : }
112 0 : }
113 :
114 :
115 :
116 :
117 0 : void FocusManager::RegisterUpLink (::Window* pSource, ::Window* pTarget)
118 : {
119 0 : RegisterLink(pSource, pTarget, KEY_ESCAPE);
120 0 : }
121 :
122 :
123 :
124 :
125 0 : void FocusManager::RegisterDownLink (::Window* pSource, ::Window* pTarget)
126 : {
127 0 : RegisterLink(pSource, pTarget, KEY_RETURN);
128 0 : }
129 :
130 :
131 :
132 :
133 0 : void FocusManager::RegisterLink (
134 : ::Window* pSource,
135 : ::Window* pTarget,
136 : const KeyCode& rKey)
137 : {
138 : OSL_ASSERT(pSource!=NULL);
139 : OSL_ASSERT(pTarget!=NULL);
140 :
141 0 : if (pSource==NULL || pTarget==NULL)
142 0 : return;
143 :
144 : // Register this focus manager as event listener at the source window.
145 0 : if (mpLinks->equal_range(pSource).first == mpLinks->end())
146 0 : pSource->AddEventListener (LINK (this, FocusManager, WindowEventListener));
147 0 : mpLinks->insert(LinkMap::value_type(pSource, EventDescriptor(rKey,pTarget)));
148 : }
149 :
150 :
151 :
152 :
153 0 : void FocusManager::RemoveLinks (
154 : ::Window* pSourceWindow,
155 : ::Window* pTargetWindow)
156 : {
157 : OSL_ASSERT(pSourceWindow!=NULL);
158 : OSL_ASSERT(pTargetWindow!=NULL);
159 :
160 0 : if (pSourceWindow==NULL || pTargetWindow==NULL)
161 : {
162 : // This method was called with invalid arguments. To avoid
163 : // referencing windows that will soon be deleted we clear *all*
164 : // links as an emergency fallback.
165 0 : Clear();
166 0 : return;
167 : }
168 :
169 0 : ::std::pair<LinkMap::iterator,LinkMap::iterator> aCandidates;
170 0 : LinkMap::iterator iCandidate;
171 0 : bool bLoop (mpLinks->size() > 0);
172 0 : while (bLoop)
173 : {
174 0 : aCandidates = mpLinks->equal_range(pSourceWindow);
175 0 : if (aCandidates.first == mpLinks->end())
176 : {
177 : // No links for the source window found -> nothing more to do.
178 0 : bLoop = false;
179 : }
180 : else
181 : {
182 : // Set the loop control to false so that when no candidate for
183 : // deletion is found the loop is left.
184 0 : bLoop = false;
185 0 : for (iCandidate=aCandidates.first; iCandidate!=aCandidates.second; ++iCandidate)
186 0 : if (iCandidate->second.mpTargetWindow == pTargetWindow)
187 : {
188 0 : mpLinks->erase(iCandidate);
189 : // One link erased. The iterators have become invalid so
190 : // start the search for links to delete anew.
191 0 : bLoop = true;
192 0 : break;
193 : }
194 : }
195 : }
196 :
197 0 : RemoveUnusedEventListener(pSourceWindow);
198 : }
199 :
200 :
201 :
202 :
203 0 : void FocusManager::RemoveLinks (::Window* pWindow)
204 : {
205 : OSL_ASSERT(pWindow!=NULL);
206 :
207 0 : if (pWindow == NULL)
208 : {
209 : // This method was called with invalid arguments. To avoid
210 : // referencing windows that will soon be deleted we clear *all*
211 : // links as an emergency fallback.
212 0 : Clear();
213 0 : return;
214 : }
215 :
216 : // Make sure that we are not called back for the window.
217 0 : pWindow->RemoveEventListener (LINK (this, FocusManager, WindowEventListener));
218 :
219 : // Remove the links from the given window.
220 0 : mpLinks->erase(pWindow);
221 :
222 : // Remove links to the given window.
223 : bool bLinkRemoved;
224 0 : do
225 : {
226 0 : bLinkRemoved = false;
227 0 : LinkMap::iterator iLink;
228 0 : for (iLink=mpLinks->begin(); iLink!=mpLinks->end(); ++iLink)
229 : {
230 0 : if (iLink->second.mpTargetWindow == pWindow)
231 : {
232 0 : ::Window* const pSourceWindow(iLink->first);
233 0 : mpLinks->erase(iLink);
234 0 : RemoveUnusedEventListener(pSourceWindow);
235 0 : bLinkRemoved = true;
236 0 : break;
237 : }
238 : }
239 : }
240 : while (bLinkRemoved);
241 : }
242 :
243 :
244 :
245 :
246 0 : void FocusManager::RemoveUnusedEventListener (::Window* pWindow)
247 : {
248 : OSL_ASSERT(pWindow!=NULL);
249 :
250 0 : if (pWindow == NULL)
251 0 : return;
252 :
253 : // When there are no more links from the window to another window
254 : // then remove the event listener from the window.
255 0 : if (mpLinks->find(pWindow) == mpLinks->end())
256 0 : pWindow->RemoveEventListener (LINK (this, FocusManager, WindowEventListener));
257 : }
258 :
259 :
260 :
261 :
262 0 : bool FocusManager::TransferFocus (
263 : ::Window* pSourceWindow,
264 : const KeyCode& rKeyCode)
265 : {
266 0 : bool bSuccess (false);
267 :
268 : OSL_ASSERT(pSourceWindow!=NULL);
269 0 : if (pSourceWindow == NULL)
270 0 : return bSuccess;
271 :
272 : ::std::pair<LinkMap::iterator,LinkMap::iterator> aCandidates (
273 0 : mpLinks->equal_range(pSourceWindow));
274 0 : LinkMap::const_iterator iCandidate;
275 0 : for (iCandidate=aCandidates.first; iCandidate!=aCandidates.second; ++iCandidate)
276 0 : if (iCandidate->second.maKeyCode == rKeyCode)
277 : {
278 : OSL_ASSERT(iCandidate->second.mpTargetWindow != NULL);
279 0 : iCandidate->second.mpTargetWindow->GrabFocus();
280 0 : bSuccess = true;
281 0 : break;
282 : }
283 :
284 0 : return bSuccess;
285 : }
286 :
287 :
288 :
289 :
290 0 : IMPL_LINK(FocusManager, WindowEventListener, VclSimpleEvent*, pEvent)
291 : {
292 0 : if (pEvent!=NULL && pEvent->ISA(VclWindowEvent))
293 : {
294 0 : VclWindowEvent* pWindowEvent = static_cast<VclWindowEvent*>(pEvent);
295 0 : switch (pWindowEvent->GetId())
296 : {
297 : case VCLEVENT_WINDOW_KEYINPUT:
298 : {
299 0 : ::Window* pSource = pWindowEvent->GetWindow();
300 0 : KeyEvent* pKeyEvent = static_cast<KeyEvent*>(pWindowEvent->GetData());
301 0 : TransferFocus(pSource, pKeyEvent->GetKeyCode());
302 : }
303 0 : break;
304 :
305 : case VCLEVENT_OBJECT_DYING:
306 0 : RemoveLinks(pWindowEvent->GetWindow());
307 0 : break;
308 : }
309 : }
310 0 : return 1;
311 : }
312 :
313 :
314 : } } // end of namespace ::sd::toolpanel
315 :
316 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|