update dev300-m58
[ooovba.git] / framework / source / accelerators / acceleratorexecute.cxx
bloba6b0bd7bce1c1d9f38edb1dac8f7b78ffe6fcf0c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: acceleratorexecute.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
33 #include "acceleratorexecute.hxx"
35 //===============================================
36 // includes
38 #ifndef __COM_SUN_STAR_FRAME_XMODULEMANAGER_HPP_
39 #include <com/sun/star/frame/XModuleManager.hpp>
40 #endif
42 #ifndef __COM_SUN_STAR_UI_XUICONFIGURATIONMANAGER_HPP_
43 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
44 #endif
46 #ifndef __COM_SUN_STAR_UI_XMODULEUICONFIGURATIONMANAGERSUPPLIER_HPP_
47 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
48 #endif
50 #ifndef __COM_SUN_STAR_UI_XUICONFIGURATIONMANAGERSUPPLIER_HPP_
51 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
52 #endif
54 #ifndef __COM_SUN_STAR_AWT_KEYMODIFIER_HPP_
55 #include <com/sun/star/awt/KeyModifier.hpp>
56 #endif
58 #ifndef __COM_SUN_STAR_UNO_SEQUENCE_HXX_
59 #include <com/sun/star/uno/Sequence.hxx>
60 #endif
62 #ifndef __COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
63 #include <com/sun/star/beans/PropertyValue.hpp>
64 #endif
66 //===============================================
67 // namespace
69 namespace svt
72 namespace css = ::com::sun::star;
74 //===============================================
75 // definitions
77 //-----------------------------------------------
78 AcceleratorExecute::AcceleratorExecute()
79 : TMutexInit ( )
80 , m_aAsyncCallback(LINK(this, AcceleratorExecute, impl_ts_asyncCallback))
84 //-----------------------------------------------
85 AcceleratorExecute::AcceleratorExecute(const AcceleratorExecute& rCopy)
86 : TMutexInit ( )
87 , m_aAsyncCallback(LINK(this, AcceleratorExecute, impl_ts_asyncCallback))
89 // copy construction sint supported in real ...
90 // but we need this ctor to init our async callback ...
93 //-----------------------------------------------
94 AcceleratorExecute::~AcceleratorExecute()
96 // does nothing real
99 //-----------------------------------------------
100 AcceleratorExecute* AcceleratorExecute::createAcceleratorHelper()
102 AcceleratorExecute* pNew = new AcceleratorExecute();
103 return pNew;
106 //-----------------------------------------------
107 void AcceleratorExecute::init(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR,
108 const css::uno::Reference< css::frame::XFrame >& xEnv )
110 // SAFE -> ----------------------------------
111 ::osl::ResettableMutexGuard aLock(m_aLock);
113 // take over the uno service manager
114 m_xSMGR = xSMGR;
116 // specify our internal dispatch provider
117 // frame or desktop?! => document or global config.
118 sal_Bool bDesktopIsUsed = sal_False;
119 m_xDispatcher = css::uno::Reference< css::frame::XDispatchProvider >(xEnv, css::uno::UNO_QUERY);
120 if (!m_xDispatcher.is())
122 aLock.clear();
123 // <- SAFE ------------------------------
125 css::uno::Reference< css::frame::XDispatchProvider > xDispatcher(
126 xSMGR->createInstance(SERVICENAME_DESKTOP),
127 css::uno::UNO_QUERY_THROW);
129 // SAFE -> ------------------------------
130 aLock.reset();
132 m_xDispatcher = xDispatcher;
133 bDesktopIsUsed = sal_True;
136 aLock.clear();
137 // <- SAFE ----------------------------------
139 // open all needed configuration objects
140 css::uno::Reference< css::ui::XAcceleratorConfiguration > xGlobalCfg;
141 css::uno::Reference< css::ui::XAcceleratorConfiguration > xModuleCfg;
142 css::uno::Reference< css::ui::XAcceleratorConfiguration > xDocCfg ;
144 // global cfg
145 xGlobalCfg = AcceleratorExecute::impl_st_openGlobalConfig(xSMGR);
146 if (!bDesktopIsUsed)
148 // module cfg
149 xModuleCfg = AcceleratorExecute::impl_st_openModuleConfig(xSMGR, xEnv);
151 // doc cfg
152 css::uno::Reference< css::frame::XController > xController;
153 css::uno::Reference< css::frame::XModel > xModel;
154 xController = xEnv->getController();
155 if (xController.is())
156 xModel = xController->getModel();
157 if (xModel.is())
158 xDocCfg = AcceleratorExecute::impl_st_openDocConfig(xModel);
161 // SAFE -> ------------------------------
162 aLock.reset();
164 m_xGlobalCfg = xGlobalCfg;
165 m_xModuleCfg = xModuleCfg;
166 m_xDocCfg = xDocCfg ;
168 aLock.clear();
169 // <- SAFE ----------------------------------
172 //-----------------------------------------------
173 void AcceleratorExecute::execute(const KeyCode& aVCLKey)
175 css::awt::KeyEvent aAWTKey = AcceleratorExecute::st_VCLKey2AWTKey(aVCLKey);
176 execute(aAWTKey);
179 //-----------------------------------------------
180 void AcceleratorExecute::execute(const css::awt::KeyEvent& aAWTKey)
182 ::rtl::OUString sCommand = impl_ts_findCommand(aAWTKey);
184 // No Command found? Do nothing! User isnt interested on any error handling .-)
185 if (!sCommand.getLength())
186 return;
188 // SAFE -> ----------------------------------
189 ::osl::ResettableMutexGuard aLock(m_aLock);
191 m_lCommandQueue.push_back(sCommand);
192 m_aAsyncCallback.Post(0);
194 aLock.clear();
195 // <- SAFE ----------------------------------
198 //-----------------------------------------------
199 css::awt::KeyEvent AcceleratorExecute::st_VCLKey2AWTKey(const KeyCode& aVCLKey)
201 css::awt::KeyEvent aAWTKey;
202 aAWTKey.Modifiers = 0;
203 aAWTKey.KeyCode = (sal_Int16)aVCLKey.GetCode();
205 if (aVCLKey.IsShift())
206 aAWTKey.Modifiers |= css::awt::KeyModifier::SHIFT;
207 if (aVCLKey.IsMod1())
208 aAWTKey.Modifiers |= css::awt::KeyModifier::MOD1;
209 if (aVCLKey.IsMod2())
210 aAWTKey.Modifiers |= css::awt::KeyModifier::MOD2;
211 if (aVCLKey.IsMod3())
212 aAWTKey.Modifiers |= css::awt::KeyModifier::MOD3;
214 return aAWTKey;
218 ViewFrame->GetObjectShell
219 ObjectShell->GetStyleSheetPool
222 //-----------------------------------------------
223 KeyCode AcceleratorExecute::st_AWTKey2VCLKey(const css::awt::KeyEvent& aAWTKey)
225 sal_Bool bShift = ((aAWTKey.Modifiers & css::awt::KeyModifier::SHIFT) == css::awt::KeyModifier::SHIFT );
226 sal_Bool bMod1 = ((aAWTKey.Modifiers & css::awt::KeyModifier::MOD1 ) == css::awt::KeyModifier::MOD1 );
227 sal_Bool bMod2 = ((aAWTKey.Modifiers & css::awt::KeyModifier::MOD2 ) == css::awt::KeyModifier::MOD2 );
228 sal_Bool bMod3 = ((aAWTKey.Modifiers & css::awt::KeyModifier::MOD3 ) == css::awt::KeyModifier::MOD3 );
229 USHORT nKey = (USHORT)aAWTKey.KeyCode;
231 return KeyCode(nKey, bShift, bMod1, bMod2, bMod3);
234 //-----------------------------------------------
235 ::rtl::OUString AcceleratorExecute::impl_ts_findCommand(const css::awt::KeyEvent& aKey)
237 // SAFE -> ----------------------------------
238 ::osl::ResettableMutexGuard aLock(m_aLock);
240 css::uno::Reference< css::ui::XAcceleratorConfiguration > xGlobalCfg = m_xGlobalCfg;
241 css::uno::Reference< css::ui::XAcceleratorConfiguration > xModuleCfg = m_xModuleCfg;
242 css::uno::Reference< css::ui::XAcceleratorConfiguration > xDocCfg = m_xDocCfg ;
244 aLock.clear();
245 // <- SAFE ----------------------------------
247 ::rtl::OUString sCommand;
250 if (xDocCfg.is())
251 sCommand = xDocCfg->getCommandByKeyEvent(aKey);
253 catch(const css::container::NoSuchElementException&)
257 if (xModuleCfg.is())
258 sCommand = xModuleCfg->getCommandByKeyEvent(aKey);
260 catch(const css::container::NoSuchElementException&)
264 if (xGlobalCfg.is())
265 sCommand = xGlobalCfg->getCommandByKeyEvent(aKey);
267 catch(const css::container::NoSuchElementException&)
272 return sCommand;
275 //-----------------------------------------------
276 css::uno::Reference< css::ui::XAcceleratorConfiguration > AcceleratorExecute::impl_st_openGlobalConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
278 css::uno::Reference< css::ui::XAcceleratorConfiguration > xAccCfg(
279 xSMGR->createInstance(SERVICENAME_GLOBALACCELERATORCONFIGURATION),
280 css::uno::UNO_QUERY_THROW);
281 return xAccCfg;
284 //-----------------------------------------------
285 css::uno::Reference< css::ui::XAcceleratorConfiguration > AcceleratorExecute::impl_st_openModuleConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
286 const css::uno::Reference< css::frame::XFrame >& xFrame)
288 css::uno::Reference< css::frame::XModuleManager > xModuleDetection(
289 xSMGR->createInstance(SERVICENAME_MODULEMANAGER),
290 css::uno::UNO_QUERY_THROW);
292 ::rtl::OUString sModule;
295 sModule = xModuleDetection->identify(xFrame);
297 catch(const css::uno::RuntimeException& exRuntime)
298 { throw exRuntime; }
299 catch(const css::uno::Exception&)
300 { return css::uno::Reference< css::ui::XAcceleratorConfiguration >(); }
302 css::uno::Reference< css::ui::XModuleUIConfigurationManagerSupplier > xUISupplier(
303 xSMGR->createInstance(SERVICENAME_MODULEUICONFIGURATIONMANAGERSUPPLIER),
304 css::uno::UNO_QUERY_THROW);
306 css::uno::Reference< css::ui::XUIConfigurationManager > xUIManager = xUISupplier->getUIConfigurationManager(sModule);
307 css::uno::Reference< css::ui::XAcceleratorConfiguration > xAccCfg (xUIManager->getShortCutManager(), css::uno::UNO_QUERY_THROW);
308 return xAccCfg;
311 //-----------------------------------------------
312 css::uno::Reference< css::ui::XAcceleratorConfiguration > AcceleratorExecute::impl_st_openDocConfig(const css::uno::Reference< css::frame::XModel >& xModel)
314 css::uno::Reference< css::ui::XUIConfigurationManagerSupplier > xUISupplier(xModel, css::uno::UNO_QUERY_THROW);
315 css::uno::Reference< css::ui::XUIConfigurationManager > xUIManager = xUISupplier->getUIConfigurationManager();
316 css::uno::Reference< css::ui::XAcceleratorConfiguration > xAccCfg (xUIManager->getShortCutManager(), css::uno::UNO_QUERY_THROW);
317 return xAccCfg;
320 //-----------------------------------------------
321 css::uno::Reference< css::util::XURLTransformer > AcceleratorExecute::impl_ts_getURLParser()
323 // SAFE -> ----------------------------------
324 ::osl::ResettableMutexGuard aLock(m_aLock);
326 if (m_xURLParser.is())
327 return m_xURLParser;
328 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
330 aLock.clear();
331 // <- SAFE ----------------------------------
333 css::uno::Reference< css::util::XURLTransformer > xParser(
334 xSMGR->createInstance(SERVICENAME_URLTRANSFORMER),
335 css::uno::UNO_QUERY_THROW);
337 // SAFE -> ----------------------------------
338 aLock.reset();
339 m_xURLParser = xParser;
340 aLock.clear();
341 // <- SAFE ----------------------------------
343 return xParser;
346 //-----------------------------------------------
347 IMPL_LINK(AcceleratorExecute, impl_ts_asyncCallback, void*, pVoid)
349 // SAFE -> ----------------------------------
350 ::osl::ResettableMutexGuard aLock(m_aLock);
352 TCommandQueue::iterator pIt = m_lCommandQueue.begin();
353 if (pIt == m_lCommandQueue.end())
354 return 0;
355 ::rtl::OUString sCommand = *pIt;
356 m_lCommandQueue.erase(pIt);
358 css::uno::Reference< css::frame::XDispatchProvider > xProvider = m_xDispatcher;
360 aLock.clear();
361 // <- SAFE ----------------------------------
363 // convert command in URL structure
364 css::uno::Reference< css::util::XURLTransformer > xParser = impl_ts_getURLParser();
365 css::util::URL aURL;
366 aURL.Complete = sCommand;
367 xParser->parseStrict(aURL);
369 // ask for dispatch object
370 css::uno::Reference< css::frame::XDispatch > xDispatch = xProvider->queryDispatch(aURL, ::rtl::OUString(), 0);
371 css::uno::Sequence< css::beans::PropertyValue> aArgs;
372 if (xDispatch.is())
374 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
376 Sequence<css::beans::PropertyValue> source;
377 ::comphelper::UiEventsLogger::appendDispatchOrigin(OUString::createFromAscii("AcceleratorExecute"));
378 ::comphelper::UiEventsLogger::logDispatch(aURL, source);
380 xDispatch->dispatch(aURL, css::uno::Sequence< css::beans::PropertyValue >());
382 return 0;
385 } // namespace svt