xref: /trunk/main/svtools/source/uno/popupmenucontrollerbase.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svtools.hxx"
26 
27 //_________________________________________________________________________________________________________________
28 //  my own includes
29 //_________________________________________________________________________________________________________________
30 #include "svtools/popupmenucontrollerbase.hxx"
31 
32 
33 //_________________________________________________________________________________________________________________
34 //  interface includes
35 //_________________________________________________________________________________________________________________
36 #include <com/sun/star/awt/XDevice.hpp>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/awt/MenuItemStyle.hpp>
39 #include <com/sun/star/frame/XDispatchProvider.hpp>
40 #include <com/sun/star/lang/DisposedException.hpp>
41 
42 //_________________________________________________________________________________________________________________
43 //  includes of other projects
44 //_________________________________________________________________________________________________________________
45 
46 #ifndef _VCL_MENU_HXX_
47 #include <vcl/menu.hxx>
48 #endif
49 #include <vcl/svapp.hxx>
50 #include <rtl/ustrbuf.hxx>
51 #include <rtl/logfile.hxx>
52 #include <vos/mutex.hxx>
53 
54 //_________________________________________________________________________________________________________________
55 //  Defines
56 //_________________________________________________________________________________________________________________
57 //
58 
59 using ::rtl::OUString;
60 
61 using namespace com::sun::star;
62 using namespace com::sun::star::uno;
63 using namespace com::sun::star::lang;
64 using namespace com::sun::star::frame;
65 using namespace com::sun::star::beans;
66 using namespace com::sun::star::util;
67 
68 namespace svt
69 {
70 
71 struct PopupMenuControllerBaseDispatchInfo
72 {
73     Reference< XDispatch > mxDispatch;
74     const URL maURL;
75     const Sequence< PropertyValue > maArgs;
76 
PopupMenuControllerBaseDispatchInfosvt::PopupMenuControllerBaseDispatchInfo77     PopupMenuControllerBaseDispatchInfo( const Reference< XDispatch >& xDispatch, const URL& rURL, const Sequence< PropertyValue >& rArgs )
78         : mxDispatch( xDispatch ), maURL( rURL ), maArgs( rArgs ) {}
79 };
80 
PopupMenuControllerBase(const Reference<XMultiServiceFactory> & xServiceManager)81 PopupMenuControllerBase::PopupMenuControllerBase( const Reference< XMultiServiceFactory >& xServiceManager ) :
82     ::comphelper::OBaseMutex(),
83     PopupMenuControllerBaseType(m_aMutex),
84     m_bInitialized( false ),
85     m_xServiceManager( xServiceManager )
86 {
87     if ( m_xServiceManager.is() )
88         m_xURLTransformer.set( m_xServiceManager->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.util.URLTransformer"))),UNO_QUERY );
89 }
90 
~PopupMenuControllerBase()91 PopupMenuControllerBase::~PopupMenuControllerBase()
92 {
93 }
94 
95 // protected function
throwIfDisposed()96 void PopupMenuControllerBase::throwIfDisposed()
97 {
98     if (rBHelper.bDisposed || rBHelper.bInDispose)
99         throw com::sun::star::lang::DisposedException();
100 }
101 
102 // protected function
resetPopupMenu(com::sun::star::uno::Reference<com::sun::star::awt::XPopupMenu> & rPopupMenu)103 void PopupMenuControllerBase::resetPopupMenu( com::sun::star::uno::Reference< com::sun::star::awt::XPopupMenu >& rPopupMenu )
104 {
105     if ( rPopupMenu.is() && rPopupMenu->getItemCount() > 0 )
106     {
107         rPopupMenu->clear();
108     }
109 }
110 
disposing()111 void SAL_CALL PopupMenuControllerBase::disposing()
112 {
113     // Reset our members and set disposed flag
114     osl::MutexGuard aLock( m_aMutex );
115     m_xFrame.clear();
116     m_xDispatch.clear();
117     m_xPopupMenu.clear();
118     m_xServiceManager.clear();
119 }
120 
121 // XServiceInfo
122 
supportsService(const::rtl::OUString & ServiceName)123 sal_Bool SAL_CALL PopupMenuControllerBase::supportsService( const ::rtl::OUString& ServiceName )
124 {
125     const Sequence< rtl::OUString > aSNL( getSupportedServiceNames() );
126     const rtl::OUString * pArray = aSNL.getConstArray();
127 
128     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
129         if( pArray[i] == ServiceName )
130             return true;
131 
132     return false;
133 }
134 
135 // XEventListener
disposing(const EventObject &)136 void SAL_CALL PopupMenuControllerBase::disposing( const EventObject& )
137 {
138     osl::MutexGuard aLock( m_aMutex );
139     m_xFrame.clear();
140     m_xDispatch.clear();
141     m_xPopupMenu.clear();
142 }
143 
144 // XMenuListener
itemHighlighted(const awt::MenuEvent &)145 void SAL_CALL PopupMenuControllerBase::itemHighlighted( const awt::MenuEvent& )
146 {
147 }
148 
impl_select(const Reference<XDispatch> & _xDispatch,const URL & aURL)149 void PopupMenuControllerBase::impl_select(const Reference< XDispatch >& _xDispatch,const URL& aURL)
150 {
151     Sequence<PropertyValue>      aArgs;
152     OSL_ENSURE(_xDispatch.is(),"PopupMenuControllerBase::impl_select: No dispatch");
153     if ( _xDispatch.is() )
154         _xDispatch->dispatch( aURL, aArgs );
155 }
156 
itemSelected(const awt::MenuEvent & rEvent)157 void SAL_CALL PopupMenuControllerBase::itemSelected( const awt::MenuEvent& rEvent )
158 {
159     throwIfDisposed();
160 
161     osl::MutexGuard aLock( m_aMutex );
162 
163     if( m_xPopupMenu.is() )
164     {
165         Sequence<PropertyValue> aArgs;
166         dispatchCommand( m_xPopupMenu->getCommand( rEvent.MenuId ), aArgs );
167     }
168 }
169 
dispatchCommand(const::rtl::OUString & sCommandURL,const::com::sun::star::uno::Sequence<::com::sun::star::beans::PropertyValue> & rArgs)170 void PopupMenuControllerBase::dispatchCommand( const ::rtl::OUString& sCommandURL, const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rArgs )
171 {
172     osl::MutexGuard aLock( m_aMutex );
173 
174     throwIfDisposed();
175 
176     try
177     {
178         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY_THROW );
179         URL aURL;
180         aURL.Complete = sCommandURL;
181         m_xURLTransformer->parseStrict( aURL );
182 
183         Reference< XDispatch > xDispatch( xDispatchProvider->queryDispatch( aURL, OUString(), 0 ), UNO_QUERY_THROW );
184 
185         Application::PostUserEvent( STATIC_LINK(0, PopupMenuControllerBase, ExecuteHdl_Impl), new PopupMenuControllerBaseDispatchInfo( xDispatch, aURL, rArgs ) );
186 
187     }
188     catch( Exception& )
189     {
190     }
191 
192 }
193 
IMPL_STATIC_LINK_NOINSTANCE(PopupMenuControllerBase,ExecuteHdl_Impl,PopupMenuControllerBaseDispatchInfo *,pDispatchInfo)194 IMPL_STATIC_LINK_NOINSTANCE( PopupMenuControllerBase, ExecuteHdl_Impl, PopupMenuControllerBaseDispatchInfo*, pDispatchInfo )
195 {
196     pDispatchInfo->mxDispatch->dispatch( pDispatchInfo->maURL, pDispatchInfo->maArgs );
197     delete pDispatchInfo;
198     return 0;
199 }
200 
itemActivated(const awt::MenuEvent &)201 void SAL_CALL PopupMenuControllerBase::itemActivated( const awt::MenuEvent& )
202 {
203 }
204 
itemDeactivated(const awt::MenuEvent &)205 void SAL_CALL PopupMenuControllerBase::itemDeactivated( const awt::MenuEvent& )
206 {
207 }
208 
updatePopupMenu()209 void SAL_CALL PopupMenuControllerBase::updatePopupMenu()
210 {
211     osl::ClearableMutexGuard aLock( m_aMutex );
212     throwIfDisposed();
213     aLock.clear();
214 
215     updateCommand( m_aCommandURL );
216 }
217 
updateCommand(const rtl::OUString & rCommandURL)218 void SAL_CALL PopupMenuControllerBase::updateCommand( const rtl::OUString& rCommandURL )
219 {
220     osl::ClearableMutexGuard aLock( m_aMutex );
221     Reference< XStatusListener > xStatusListener( static_cast< OWeakObject* >( this ), UNO_QUERY );
222     Reference< XDispatch > xDispatch( m_xDispatch );
223     URL aTargetURL;
224     aTargetURL.Complete = rCommandURL;
225     m_xURLTransformer->parseStrict( aTargetURL );
226     aLock.clear();
227 
228     // Add/remove status listener to get a status update once
229     if ( xDispatch.is() )
230     {
231         xDispatch->addStatusListener( xStatusListener, aTargetURL );
232         xDispatch->removeStatusListener( xStatusListener, aTargetURL );
233     }
234 }
235 
236 
237 // XDispatchProvider
238 Reference< XDispatch > SAL_CALL
queryDispatch(const URL &,const rtl::OUString &,sal_Int32)239 PopupMenuControllerBase::queryDispatch(
240     const URL& /*aURL*/,
241     const rtl::OUString& /*sTarget*/,
242     sal_Int32 /*nFlags*/ )
243 {
244     // must be implemented by subclass
245     osl::MutexGuard aLock( m_aMutex );
246     throwIfDisposed();
247 
248     return Reference< XDispatch >();
249 }
250 
queryDispatches(const Sequence<DispatchDescriptor> & lDescriptor)251 Sequence< Reference< XDispatch > > SAL_CALL PopupMenuControllerBase::queryDispatches( const Sequence< DispatchDescriptor >& lDescriptor )
252 {
253     // Create return list - which must have same size then the given descriptor
254     // It's not allowed to pack it!
255     osl::ClearableMutexGuard aLock( m_aMutex );
256     throwIfDisposed();
257     aLock.clear();
258 
259     sal_Int32                                                          nCount = lDescriptor.getLength();
260     uno::Sequence< uno::Reference< frame::XDispatch > > lDispatcher( nCount );
261 
262     // Step over all descriptors and try to get any dispatcher for it.
263     for( sal_Int32 i=0; i<nCount; ++i )
264     {
265         lDispatcher[i] = queryDispatch( lDescriptor[i].FeatureURL  ,
266                                         lDescriptor[i].FrameName   ,
267                                         lDescriptor[i].SearchFlags );
268     }
269 
270     return lDispatcher;
271 }
272 
273 // XDispatch
274 void SAL_CALL
dispatch(const URL &,const Sequence<PropertyValue> &)275 PopupMenuControllerBase::dispatch(
276     const URL& /*aURL*/,
277     const Sequence< PropertyValue >& /*seqProperties*/ )
278 {
279     // must be implemented by subclass
280     osl::MutexGuard aLock( m_aMutex );
281     throwIfDisposed();
282 }
283 
284 void SAL_CALL
addStatusListener(const Reference<XStatusListener> & xControl,const URL & aURL)285 PopupMenuControllerBase::addStatusListener(
286     const Reference< XStatusListener >& xControl,
287     const URL& aURL )
288 {
289     osl::ResettableMutexGuard aLock( m_aMutex );
290     throwIfDisposed();
291     aLock.clear();
292 
293     bool bStatusUpdate( false );
294     rBHelper.addListener( ::getCppuType( &xControl ), xControl );
295 
296     aLock.reset();
297     if ( aURL.Complete.indexOf( m_aBaseURL ) == 0 )
298         bStatusUpdate = true;
299     aLock.clear();
300 
301     if ( bStatusUpdate )
302     {
303         // Dummy update for popup menu controllers
304         FeatureStateEvent aEvent;
305         aEvent.FeatureURL = aURL;
306         aEvent.IsEnabled  = sal_True;
307         aEvent.Requery    = sal_False;
308         aEvent.State      = Any();
309         xControl->statusChanged( aEvent );
310     }
311 }
312 
removeStatusListener(const Reference<XStatusListener> & xControl,const URL &)313 void SAL_CALL PopupMenuControllerBase::removeStatusListener(
314     const Reference< XStatusListener >& xControl,
315     const URL& /*aURL*/ )
316 {
317     rBHelper.removeListener( ::getCppuType( &xControl ), xControl );
318 }
319 
determineBaseURL(const::rtl::OUString & aURL)320 ::rtl::OUString PopupMenuControllerBase::determineBaseURL( const ::rtl::OUString& aURL )
321 {
322     // Just use the main part of the URL for popup menu controllers
323     sal_Int32     nQueryPart( 0 );
324     sal_Int32     nSchemePart( 0 );
325     rtl::OUString aMainURL( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.popup:" ));
326 
327     nSchemePart = aURL.indexOf( ':' );
328     if (( nSchemePart > 0 ) &&
329         ( aURL.getLength() > ( nSchemePart+1 )))
330     {
331         nQueryPart  = aURL.indexOf( '?', nSchemePart );
332         if ( nQueryPart > 0 )
333             aMainURL += aURL.copy( nSchemePart, nQueryPart-nSchemePart );
334         else if ( nQueryPart == -1 )
335             aMainURL += aURL.copy( nSchemePart+1 );
336     }
337 
338     return aMainURL;
339 }
340 
341 // XInitialization
initialize(const Sequence<Any> & aArguments)342 void SAL_CALL PopupMenuControllerBase::initialize( const Sequence< Any >& aArguments )
343 {
344     osl::MutexGuard aLock( m_aMutex );
345 
346     sal_Bool bInitialized( m_bInitialized );
347     if ( !bInitialized )
348     {
349         PropertyValue       aPropValue;
350         rtl::OUString       aCommandURL;
351         Reference< XFrame > xFrame;
352 
353         for ( int i = 0; i < aArguments.getLength(); i++ )
354         {
355             if ( aArguments[i] >>= aPropValue )
356             {
357                 if ( aPropValue.Name.equalsAscii( "Frame" ))
358                     aPropValue.Value >>= xFrame;
359                 else if ( aPropValue.Name.equalsAscii( "CommandURL" ))
360                     aPropValue.Value >>= aCommandURL;
361             }
362         }
363 
364         if ( xFrame.is() && aCommandURL.getLength() )
365         {
366             m_xFrame        = xFrame;
367             m_aCommandURL   = aCommandURL;
368             m_aBaseURL      = determineBaseURL( aCommandURL );
369             m_bInitialized  = true;
370         }
371     }
372 }
373 // XPopupMenuController
setPopupMenu(const Reference<awt::XPopupMenu> & xPopupMenu)374 void SAL_CALL PopupMenuControllerBase::setPopupMenu( const Reference< awt::XPopupMenu >& xPopupMenu )
375 {
376     osl::MutexGuard aLock( m_aMutex );
377     throwIfDisposed();
378 
379     if ( m_xFrame.is() && !m_xPopupMenu.is() )
380     {
381         // Create popup menu on demand
382         vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
383 
384         m_xPopupMenu = xPopupMenu;
385         m_xPopupMenu->addMenuListener( Reference< awt::XMenuListener >( (OWeakObject*)this, UNO_QUERY ));
386 
387         Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
388 
389         URL aTargetURL;
390         aTargetURL.Complete = m_aCommandURL;
391         m_xURLTransformer->parseStrict( aTargetURL );
392         m_xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
393 
394         impl_setPopupMenu();
395 
396         updatePopupMenu();
397     }
398 }
impl_setPopupMenu()399 void PopupMenuControllerBase::impl_setPopupMenu()
400 {
401 }
402 }
403