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