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 <vector>
35 
36 #ifndef __COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #endif
39 
40 #ifndef __COM_SUN_STAR_FRAME_XFRAME_HPP_
41 #include <com/sun/star/frame/XFrame.hpp>
42 #endif
43 
44 #ifndef __COM_SUN_STAR_FRAME_XDISPATCHPROVIDER_HPP_
45 #include <com/sun/star/frame/XDispatchProvider.hpp>
46 #endif
47 
48 #ifndef __COM_SUN_STAR_UI_XACCELERATORCONFIGURATION_HPP_
49 #include <com/sun/star/ui/XAcceleratorConfiguration.hpp>
50 #endif
51 
52 #ifndef __COM_SUN_STAR_UTIL_XURLTRANSFORMER_HPP_
53 #include <com/sun/star/util/XURLTransformer.hpp>
54 #endif
55 
56 #ifndef __COM_SUN_STAR_AWT_KEYEVENT_HPP_
57 #include <com/sun/star/awt/KeyEvent.hpp>
58 #endif
59 #include <vcl/keycod.hxx>
60 #include <vcl/evntpost.hxx>
61 #include <osl/mutex.h>
62 
63 //===============================================
64 // namespace
65 
66 namespace svt
67 {
68 
69 #ifdef css
70     #error "Who define css? I need it as namespace alias."
71 #else
72     #define css ::com::sun::star
73 #endif
74 
75 //===============================================
76 // definitions
77 
78 struct TMutexInit
79 {
80     ::osl::Mutex m_aLock;
81 };
82 
83 //===============================================
84 /**
85     @descr  implements a helper, which can be used to
86             convert vcl key codes into awt key codes ...
87             and reverse.
88 
89             Further such key code can be triggered.
90             Doing so different accelerator
91             configurations are merged together; a suitable
92             command registered for the given key code is searched
93             and will be dispatched.
94 
95     @attention
96 
97             Because exceution of an accelerator command can be dangerous
98             (in case it force an office shutdown for key "ALT+F4"!)
99             all internal dispatches are done asynchronous.
100             Menas that the trigger call doesnt wait till the dispatch
101             is finished. You can call very often. All requests will be
102             queued internal and dispatched ASAP.
103 
104             Of course this queue will be stopped if the environment
105             will be destructed ...
106  */
107 class AcceleratorExecute : private TMutexInit
108 {
109     //-------------------------------------------
110     // const, types
111     private:
112 
113         /** TODO document me */
114         typedef ::std::vector< ::rtl::OUString > TCommandQueue;
115 
116     //-------------------------------------------
117     // member
118     private:
119 
120         /** TODO document me */
121         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
122 
123         /** TODO document me */
124         css::uno::Reference< css::util::XURLTransformer > m_xURLParser;
125 
126         /** TODO document me */
127         css::uno::Reference< css::frame::XDispatchProvider > m_xDispatcher;
128 
129         /** TODO document me */
130         css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xGlobalCfg;
131         css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xModuleCfg;
132         css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xDocCfg;
133 
134         /** TODO document me */
135         TCommandQueue m_lCommandQueue;
136 
137         /** TODO document me */
138         ::vcl::EventPoster m_aAsyncCallback;
139 
140     //-------------------------------------------
141     // interface
142     public:
143 
144         //---------------------------------------
145         /** @short  factory method to create new accelerator
146                     helper instance.
147 
148             @descr  Such helper instance must be initialized at first.
149                     So it can know its environment (global/module or
150                     document specific).
151 
152                     Afterwards it can be used to execute incoming
153                     accelerator requests.
154 
155                     The "end of life" of such helper can be reached as follow:
156 
157                     - delete the object
158                       => If it stands currently in its execute method, they will
159                          be finished. All further queued requests will be removed
160                          and further not executed!
161 
162                     Other modes are possible and will be implemented ASAP :-)
163          */
164         static AcceleratorExecute* createAcceleratorHelper();
165 
166         //---------------------------------------
167         /** @short  fight against inlining ... */
168         virtual ~AcceleratorExecute();
169 
170         //---------------------------------------
171         /** @short  init this instance.
172 
173             @descr  It must be called as first method after creation.
174                     And further it can be called more then once ...
175                     but at least its should be used one times only.
176                     Otherwhise nobody can say, which asynchronous
177                     executions will be used inside the old and which one
178                     will be used inside the new environment.
179 
180             @param  xSMGR
181                     reference to an uno service manager.
182 
183             @param  xEnv
184                     if it points to a valid frame it will be used
185                     to execute the dispatch there. Further the frame
186                     is used to locate the right module configuration
187                     and use it merged together with the document and
188                     the global configuration.
189 
190                     If this parameter is set to NULL, the global configuration
191                     is used only. Further the global Desktop instance is
192                     used for dispatch.
193          */
194         virtual void init(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR,
195                           const css::uno::Reference< css::frame::XFrame >&              xEnv );
196 
197         //---------------------------------------
198         /** @short  trigger this accelerator.
199 
200             @descr  The internal configuartions are used to find
201                     as suitable command for this key code.
202                     This command will be queued and executed later
203                     asynchronous.
204 
205             @param  aKey
206                     specify the accelerator for execute.
207          */
208         virtual void execute(const KeyCode&            aKey);
209         virtual void execute(const css::awt::KeyEvent& aKey);
210 
211         //---------------------------------------
212         /** TODO document me */
213         static css::awt::KeyEvent st_VCLKey2AWTKey(const KeyCode&            aKey);
214         static KeyCode            st_AWTKey2VCLKey(const css::awt::KeyEvent& aKey);
215 
216     //-------------------------------------------
217     // internal
218     private:
219 
220         //---------------------------------------
221         /** @short  allow creation of instances of this class
222                     by using our factory only!
223          */
224         AcceleratorExecute();
225         AcceleratorExecute(const AcceleratorExecute& rCopy);
226         void operator=(const AcceleratorExecute& rCopy) {};
227 
228         //---------------------------------------
229         /** TODO document me */
230         css::uno::Reference< css::ui::XAcceleratorConfiguration > impl_st_openGlobalConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR);
231 
232         css::uno::Reference< css::ui::XAcceleratorConfiguration > impl_st_openModuleConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
233                                                                                             const css::uno::Reference< css::frame::XFrame >&              xFrame);
234 
235         css::uno::Reference< css::ui::XAcceleratorConfiguration > impl_st_openDocConfig(const css::uno::Reference< css::frame::XModel >& xModel);
236 
237         //---------------------------------------
238         /** TODO document me */
239         ::rtl::OUString impl_ts_findCommand(const css::awt::KeyEvent& aKey);
240 
241         //---------------------------------------
242         /** TODO document me */
243         css::uno::Reference< css::util::XURLTransformer > impl_ts_getURLParser();
244 
245         //---------------------------------------
246         /** TODO document me */
247         DECL_LINK(impl_ts_asyncCallback, void*);
248 };
249 
250 #undef  css
251 #undef css
252 
253 } // namespace svt
254 
255 #endif // INCLUDED_SVTOOLS_ACCELERATOREXECUTE_HXX
256