1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: acceleratorexecute.cxx,v $
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 //===============================================
38 #ifndef __COM_SUN_STAR_FRAME_XMODULEMANAGER_HPP_
39 #include <com/sun/star/frame/XModuleManager.hpp>
42 #ifndef __COM_SUN_STAR_UI_XUICONFIGURATIONMANAGER_HPP_
43 #include <com/sun/star/ui/XUIConfigurationManager.hpp>
46 #ifndef __COM_SUN_STAR_UI_XMODULEUICONFIGURATIONMANAGERSUPPLIER_HPP_
47 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
50 #ifndef __COM_SUN_STAR_UI_XUICONFIGURATIONMANAGERSUPPLIER_HPP_
51 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
54 #ifndef __COM_SUN_STAR_AWT_KEYMODIFIER_HPP_
55 #include <com/sun/star/awt/KeyModifier.hpp>
58 #ifndef __COM_SUN_STAR_UNO_SEQUENCE_HXX_
59 #include <com/sun/star/uno/Sequence.hxx>
62 #ifndef __COM_SUN_STAR_BEANS_PROPERTYVALUE_HPP_
63 #include <com/sun/star/beans/PropertyValue.hpp>
66 //===============================================
72 namespace css
= ::com::sun::star
;
74 //===============================================
77 //-----------------------------------------------
78 AcceleratorExecute::AcceleratorExecute()
80 , m_aAsyncCallback(LINK(this, AcceleratorExecute
, impl_ts_asyncCallback
))
84 //-----------------------------------------------
85 AcceleratorExecute::AcceleratorExecute(const AcceleratorExecute
& rCopy
)
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()
99 //-----------------------------------------------
100 AcceleratorExecute
* AcceleratorExecute::createAcceleratorHelper()
102 AcceleratorExecute
* pNew
= new AcceleratorExecute();
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
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())
123 // <- SAFE ------------------------------
125 css::uno::Reference
< css::frame::XDispatchProvider
> xDispatcher(
126 xSMGR
->createInstance(SERVICENAME_DESKTOP
),
127 css::uno::UNO_QUERY_THROW
);
129 // SAFE -> ------------------------------
132 m_xDispatcher
= xDispatcher
;
133 bDesktopIsUsed
= sal_True
;
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
;
145 xGlobalCfg
= AcceleratorExecute::impl_st_openGlobalConfig(xSMGR
);
149 xModuleCfg
= AcceleratorExecute::impl_st_openModuleConfig(xSMGR
, xEnv
);
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();
158 xDocCfg
= AcceleratorExecute::impl_st_openDocConfig(xModel
);
161 // SAFE -> ------------------------------
164 m_xGlobalCfg
= xGlobalCfg
;
165 m_xModuleCfg
= xModuleCfg
;
166 m_xDocCfg
= xDocCfg
;
169 // <- SAFE ----------------------------------
172 //-----------------------------------------------
173 void AcceleratorExecute::execute(const KeyCode
& aVCLKey
)
175 css::awt::KeyEvent aAWTKey
= AcceleratorExecute::st_VCLKey2AWTKey(aVCLKey
);
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())
188 // SAFE -> ----------------------------------
189 ::osl::ResettableMutexGuard
aLock(m_aLock
);
191 m_lCommandQueue
.push_back(sCommand
);
192 m_aAsyncCallback
.Post(0);
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
;
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
;
245 // <- SAFE ----------------------------------
247 ::rtl::OUString sCommand
;
251 sCommand
= xDocCfg
->getCommandByKeyEvent(aKey
);
253 catch(const css::container::NoSuchElementException
&)
258 sCommand
= xModuleCfg
->getCommandByKeyEvent(aKey
);
260 catch(const css::container::NoSuchElementException
&)
265 sCommand
= xGlobalCfg
->getCommandByKeyEvent(aKey
);
267 catch(const css::container::NoSuchElementException
&)
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
);
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
)
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
);
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
);
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())
328 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
331 // <- SAFE ----------------------------------
333 css::uno::Reference
< css::util::XURLTransformer
> xParser(
334 xSMGR
->createInstance(SERVICENAME_URLTRANSFORMER
),
335 css::uno::UNO_QUERY_THROW
);
337 // SAFE -> ----------------------------------
339 m_xURLParser
= xParser
;
341 // <- SAFE ----------------------------------
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())
355 ::rtl::OUString sCommand
= *pIt
;
356 m_lCommandQueue
.erase(pIt
);
358 css::uno::Reference
< css::frame::XDispatchProvider
> xProvider
= m_xDispatcher
;
361 // <- SAFE ----------------------------------
363 // convert command in URL structure
364 css::uno::Reference
< css::util::XURLTransformer
> xParser
= impl_ts_getURLParser();
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
;
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
>());