1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_SVTOOLS_ACCELERATOREXECUTE_HXX
29 #define INCLUDED_SVTOOLS_ACCELERATOREXECUTE_HXX
30 
31 //===============================================
32 // includes
33 
34 #include "svtools/svtdllapi.h"
35 
36 #ifndef INCLUDED_VECTOR
37 #include <vector>
38 #define INCLUDED_VECTOR
39 #endif
40 
41 #ifndef __COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #endif
44 
45 #ifndef __COM_SUN_STAR_FRAME_XFRAME_HPP_
46 #include <com/sun/star/frame/XFrame.hpp>
47 #endif
48 
49 #ifndef __COM_SUN_STAR_FRAME_XDISPATCHPROVIDER_HPP_
50 #include <com/sun/star/frame/XDispatchProvider.hpp>
51 #endif
52 
53 #ifndef __com_SUN_STAR_UI_XACCELERATORCONFIGURATION_HPP_
54 #include <com/sun/star/ui/XAcceleratorConfiguration.hpp>
55 #endif
56 
57 #ifndef __COM_SUN_STAR_UTIL_XURLTRANSFORMER_HPP_
58 #include <com/sun/star/util/XURLTransformer.hpp>
59 #endif
60 #include <com/sun/star/util/URL.hpp>
61 
62 #ifndef __COM_SUN_STAR_AWT_KEYEVENT_HPP_
63 #include <com/sun/star/awt/KeyEvent.hpp>
64 #endif
65 #include <vcl/keycod.hxx>
66 #include <vcl/evntpost.hxx>
67 #include <osl/mutex.h>
68 
69 //===============================================
70 // namespace
71 
72 #ifdef css
73     #error "Conflict on using css as namespace alias!"
74 #else
75     #define css ::com::sun::star
76 #endif
77 
78 namespace svt
79 {
80 
81 //===============================================
82 // definitions
83 
84 struct TMutexInit
85 {
86     ::osl::Mutex m_aLock;
87 };
88 
89 //===============================================
90 /**
91     @descr  implements a helper, which can be used to
92             convert vcl key codes into awt key codes ...
93             and reverse.
94 
95             Further such key code can be triggered.
96             Doing so different accelerator
97             configurations are merged together; a suitable
98             command registered for the given key code is searched
99             and will be dispatched.
100 
101     @attention
102 
103             Because exceution of an accelerator command can be dangerous
104             (in case it force an office shutdown for key "ALT+F4"!)
105             all internal dispatches are done asynchronous.
106             Menas that the trigger call doesnt wait till the dispatch
107             is finished. You can call very often. All requests will be
108             queued internal and dispatched ASAP.
109 
110             Of course this queue will be stopped if the environment
111             will be destructed ...
112  */
113 class SVT_DLLPUBLIC AcceleratorExecute : private TMutexInit
114 {
115     //-------------------------------------------
116     // const, types
117     private:
118 
119         /** @deprecated
120                 replaced by internal class AsyncAccelExec ...
121                 remove this resource here if we go forwards to next major */
122         typedef ::std::vector< ::std::pair< css::util::URL, css::uno::Reference< css::frame::XDispatch > > > TCommandQueue;
123 
124     //-------------------------------------------
125     // member
126     private:
127 
128         /** TODO document me */
129         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
130 
131         /** TODO document me */
132         css::uno::Reference< css::util::XURLTransformer > m_xURLParser;
133 
134         /** TODO document me */
135         css::uno::Reference< css::frame::XDispatchProvider > m_xDispatcher;
136 
137         /** TODO document me */
138         css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xGlobalCfg;
139         css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xModuleCfg;
140         css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xDocCfg;
141 
142         /** @deprecated
143                 replaced by internal class AsyncAccelExec ...
144                 remove this resource here if we go forwards to next major */
145         TCommandQueue m_lCommandQueue;
146 
147         /** @deprecated
148                 replaced by internal class AsyncAccelExec ...
149                 remove this resource here if we go forwards to next major */
150         ::vcl::EventPoster m_aAsyncCallback;
151 
152     //-------------------------------------------
153     // interface
154     public:
155 
156         //---------------------------------------
157         /** @short  factory method to create new accelerator
158                     helper instance.
159 
160             @descr  Such helper instance must be initialized at first.
161                     So it can know its environment (global/module or
162                     document specific).
163 
164                     Afterwards it can be used to execute incoming
165                     accelerator requests.
166 
167                     The "end of life" of such helper can be reached as follow:
168 
169                     - delete the object
170                       => If it stands currently in its execute method, they will
171                          be finished. All further queued requests will be removed
172                          and further not executed!
173 
174                     - "let it stay alone"
175                       => All currently queued events will be finished. The
176                          helper kills itself afterwards. A shutdown of the
177                          environment will be recognized ... The helper stop its
178                          work immediatly then!
179          */
180         static AcceleratorExecute* createAcceleratorHelper();
181 
182         //---------------------------------------
183         /** @short  fight against inlining ... */
184         virtual ~AcceleratorExecute();
185 
186         //---------------------------------------
187         /** @short  init this instance.
188 
189             @descr  It must be called as first method after creation.
190                     And further it can be called more then once ...
191                     but at least its should be used one times only.
192                     Otherwhise nobody can say, which asynchronous
193                     executions will be used inside the old and which one
194                     will be used inside the new environment.
195 
196             @param  xSMGR
197                     reference to an uno service manager.
198 
199             @param  xEnv
200                     if it points to a valid frame it will be used
201                     to execute the dispatch there. Further the frame
202                     is used to locate the right module configuration
203                     and use it merged together with the document and
204                     the global configuration.
205 
206                     If this parameter is set to NULL, the global configuration
207                     is used only. Further the global Desktop instance is
208                     used for dispatch.
209          */
210         virtual void init(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR,
211                           const css::uno::Reference< css::frame::XFrame >&              xEnv );
212 
213         //---------------------------------------
214         /** @short  trigger this accelerator.
215 
216             @descr  The internal configuartions are used to find
217                     as suitable command for this key code.
218                     This command will be queued and executed later
219                     asynchronous.
220 
221             @param  aKey
222                     specify the accelerator for execute.
223 
224             @return [sal_Bool]
225                     sal_True if this key is configured and match to a command.
226                     Attention: This state does not mean the success state
227                     of the corresponding execute. Because its done asynchronous!
228          */
229         virtual sal_Bool execute(const KeyCode&            aKey);
230         virtual sal_Bool execute(const css::awt::KeyEvent& aKey);
231 
232         /** search the command for the given key event.
233         *
234         * @param aKey The key event
235         * @return The command or an empty string if the key event could not be found.
236         */
237         ::rtl::OUString  findCommand(const ::com::sun::star::awt::KeyEvent& aKey);
238         //---------------------------------------
239         /** TODO document me */
240         static css::awt::KeyEvent st_VCLKey2AWTKey(const KeyCode&            aKey);
241         static KeyCode            st_AWTKey2VCLKey(const css::awt::KeyEvent& aKey);
242 
243         //---------------------------------------
244         /** TODO document me */
245         static css::uno::Reference< css::ui::XAcceleratorConfiguration > st_openGlobalConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
246 
247         //---------------------------------------
248         /** TODO document me */
249         static css::uno::Reference< css::ui::XAcceleratorConfiguration > st_openModuleConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
250                                                                                               const css::uno::Reference< css::frame::XFrame >&              xFrame);
251 
252         //---------------------------------------
253         /** TODO document me */
254         static css::uno::Reference< css::ui::XAcceleratorConfiguration > st_openDocConfig(const css::uno::Reference< css::frame::XModel >& xModel);
255 
256     //-------------------------------------------
257     // internal
258     private:
259 
260         //---------------------------------------
261         /** @short  allow creation of instances of this class
262                     by using our factory only!
263          */
264         SVT_DLLPRIVATE AcceleratorExecute();
265 
266         AcceleratorExecute(const AcceleratorExecute& rCopy);
267         void operator=(const AcceleratorExecute&) {};
268         //---------------------------------------
269         /** TODO document me */
270         SVT_DLLPRIVATE ::rtl::OUString impl_ts_findCommand(const css::awt::KeyEvent& aKey);
271 
272         //---------------------------------------
273         /** TODO document me */
274         SVT_DLLPRIVATE css::uno::Reference< css::util::XURLTransformer > impl_ts_getURLParser();
275 
276         //---------------------------------------
277         /** @deprecated
278                 replaced by internal class AsyncAccelExec ...
279                 remove this resource here if we go forwards to next major */
280         DECL_DLLPRIVATE_LINK(impl_ts_asyncCallback, void*);
281 };
282 
283 } // namespace svt
284 
285 #undef css
286 
287 #endif // INCLUDED_SVTOOLS_ACCELERATOREXECUTE_HXX
288