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 __FRAMEWORK_DISPATCH_WINDOWCOMMANDDISPATCH_HXX_
29 #define __FRAMEWORK_DISPATCH_WINDOWCOMMANDDISPATCH_HXX_
30 
31 //_______________________________________________
32 // my own includes
33 
34 #include <threadhelp/threadhelpbase.hxx>
35 
36 //_______________________________________________
37 // interface includes
38 
39 #include <com/sun/star/awt/XWindow.hpp>
40 #include <com/sun/star/frame/XFrame.hpp>
41 #include <com/sun/star/lang/XEventListener.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 
44 //_______________________________________________
45 // other includes
46 
47 #include <cppuhelper/implbase1.hxx>
48 #include <tools/link.hxx>
49 
50 //_______________________________________________
51 // namespace
52 
53 namespace framework{
54 
55 namespace css = ::com::sun::star;
56 
57 //_______________________________________________
58 // exported const
59 
60 //_______________________________________________
61 // exported definitions
62 
63 /** @short  internal helper to bind e.g. MAC-Menu events to our internal dispatch API.
64 
65     @descr  On e.g. MAC platform system menus are merged together with some fix entries as
66             e.g. "Pereferences" or "About". These menu entries trigger hard coded commands.
67             Here we map these commands to the right URLs and dispatch them.
68 
69             This helper knows a frame and it's container window (where VCL provide the hard coded
70             commands). We hold those objects weak ... so there is no need to react for complex dispose/ing()
71             scenarios. On the other side VCL does not hold us alive (because it doesn't know our UNO reference).
72             So we register us at the XWindow as event listener also to be sure to live as long the XWindow/VCLWindow lives.
73  */
74 class WindowCommandDispatch : private ThreadHelpBase
75                   , public  ::cppu::WeakImplHelper1< css::lang::XEventListener >
76 {
77     //___________________________________________
78     // const
79 
80     private:
81 
82         /// dispatch URL to trigger our "Tools->Options" dialog
83         static const ::rtl::OUString COMMAND_PREFERENCES;
84 
85         /// dispatch URL to trigger our About box
86         static const ::rtl::OUString COMMAND_ABOUTBOX;
87 
88     //___________________________________________
89     // member
90 
91     private:
92 
93         /// can be used to create own needed services on demand.
94         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
95 
96         /// knows the frame, where we dispatch our commands as weak reference
97         css::uno::WeakReference< css::frame::XFrame > m_xFrame;
98 
99         /// knows the VCL window (where the hard coded commands occured) as weak XWindow reference
100         css::uno::WeakReference< css::awt::XWindow > m_xWindow;
101 
102     //___________________________________________
103     // native interface
104 
105     public:
106 
107         //_______________________________________
108 
109         /** @short  creates a new instance and initialize it with all necessary parameters.
110 
111             @descr  Every instance of such MACDispatch can be used for the specified context only.
112                     Means: 1 MACDispatch object is bound to 1 Frame/Window pair in which context
113                     the detected commands will be executed.
114 
115             @param  xSMGR
116                     will be used to create own needed services on demand.
117 
118             @param  xFrame
119                     used as for new detected commands.
120          */
121         WindowCommandDispatch(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR ,
122                               const css::uno::Reference< css::frame::XFrame >&              xFrame);
123 
124         //_______________________________________
125 
126         /** @short  used to free internal resources.
127          */
128         virtual ~WindowCommandDispatch();
129 
130     //___________________________________________
131     // uno interface
132 
133     public:
134 
135         // XEventListener
136          virtual void SAL_CALL disposing(const css::lang::EventObject& aSource)
137             throw (css::uno::RuntimeException);
138 
139     //___________________________________________
140     // implementation
141 
142     private:
143 
144         //_______________________________________
145 
146         /** @short  establish all listener connections we need.
147 
148             @descr  Those listener connections will be created one times only (see ctor).
149                     Afterwards we listen for incoming events till our referred frame/window pair
150                     will be closed. All objects die by refcount automatically. Because we hold
151                     it weak ...
152          */
153         void impl_startListening();
154 
155         //_______________________________________
156 
157         /** @short  callback from VCL to notify new commands
158          */
159         DECL_LINK( impl_notifyCommand, void* );
160 
161         //_______________________________________
162 
163         /** @short  dispatch right command URLs into our frame context.
164 
165             @param  sCommand
166                     the command for dispatch
167          */
168         void impl_dispatchCommand(const ::rtl::OUString& sCommand);
169 
170 }; // class MACDispatch
171 
172 } // namespace framework
173 
174 #endif // #ifndef __FRAMEWORK_DISPATCH_MACDISPATCH_HXX_
175