15900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
35900e8ecSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
45900e8ecSAndrew Rist * or more contributor license agreements. See the NOTICE file
55900e8ecSAndrew Rist * distributed with this work for additional information
65900e8ecSAndrew Rist * regarding copyright ownership. The ASF licenses this file
75900e8ecSAndrew Rist * to you under the Apache License, Version 2.0 (the
85900e8ecSAndrew Rist * "License"); you may not use this file except in compliance
95900e8ecSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
115900e8ecSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
135900e8ecSAndrew Rist * Unless required by applicable law or agreed to in writing,
145900e8ecSAndrew Rist * software distributed under the License is distributed on an
155900e8ecSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165900e8ecSAndrew Rist * KIND, either express or implied. See the License for the
175900e8ecSAndrew Rist * specific language governing permissions and limitations
185900e8ecSAndrew Rist * under the License.
19cdf0e10cSrcweir *
205900e8ecSAndrew Rist *************************************************************/
215900e8ecSAndrew Rist
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_svtools.hxx"
24cdf0e10cSrcweir
25cdf0e10cSrcweir #include <svtools/contextmenuhelper.hxx>
26cdf0e10cSrcweir #include <svtools/menuoptions.hxx>
27cdf0e10cSrcweir #include <svtools/miscopt.hxx>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <com/sun/star/frame/XDispatch.hpp>
30cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/XModuleManager.hpp>
32cdf0e10cSrcweir #include <com/sun/star/frame/XStatusListener.hpp>
33cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationManager.hpp>
36cdf0e10cSrcweir #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
37cdf0e10cSrcweir #include <com/sun/star/ui/ImageType.hpp>
38cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
39cdf0e10cSrcweir
40cdf0e10cSrcweir #include <osl/conditn.hxx>
41cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
42cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
43cdf0e10cSrcweir #include <vos/mutex.hxx>
44cdf0e10cSrcweir #include <vcl/svapp.hxx>
45cdf0e10cSrcweir #include <vcl/image.hxx>
46b63233d8Sdamjan #include <toolkit/helper/vclunohelper.hxx>
47cdf0e10cSrcweir #include <toolkit/awt/vclxwindow.hxx>
48cdf0e10cSrcweir #include <toolkit/awt/vclxmenu.hxx>
49cdf0e10cSrcweir
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir
52cdf0e10cSrcweir namespace svt
53cdf0e10cSrcweir {
54cdf0e10cSrcweir
55cdf0e10cSrcweir // internal helper class to retrieve status updates
56cdf0e10cSrcweir class StateEventHelper : public ::com::sun::star::frame::XStatusListener,
57cdf0e10cSrcweir public ::cppu::OWeakObject
58cdf0e10cSrcweir {
59cdf0e10cSrcweir public:
60cdf0e10cSrcweir StateEventHelper( const uno::Reference< frame::XDispatchProvider >& xDispatchProvider,
61cdf0e10cSrcweir const uno::Reference< util::XURLTransformer >& xURLTransformer,
62cdf0e10cSrcweir const rtl::OUString& aCommandURL );
63cdf0e10cSrcweir virtual ~StateEventHelper();
64cdf0e10cSrcweir
65cdf0e10cSrcweir bool isCommandEnabled();
66cdf0e10cSrcweir
67cdf0e10cSrcweir // XInterface
68cdf0e10cSrcweir virtual uno::Any SAL_CALL queryInterface( const uno::Type& aType ) throw ( uno::RuntimeException);
69cdf0e10cSrcweir virtual void SAL_CALL acquire() throw ();
70cdf0e10cSrcweir virtual void SAL_CALL release() throw ();
71cdf0e10cSrcweir
72cdf0e10cSrcweir // XEventListener
73cdf0e10cSrcweir virtual void SAL_CALL disposing(const lang::EventObject& Source) throw( uno::RuntimeException );
74cdf0e10cSrcweir
75cdf0e10cSrcweir // XStatusListener
76cdf0e10cSrcweir virtual void SAL_CALL statusChanged(const frame::FeatureStateEvent& Event) throw( uno::RuntimeException );
77cdf0e10cSrcweir
78cdf0e10cSrcweir private:
79cdf0e10cSrcweir StateEventHelper();
80cdf0e10cSrcweir StateEventHelper( const StateEventHelper& );
81cdf0e10cSrcweir StateEventHelper& operator=( const StateEventHelper& );
82cdf0e10cSrcweir
83cdf0e10cSrcweir bool m_bCurrentCommandEnabled;
84cdf0e10cSrcweir ::rtl::OUString m_aCommandURL;
85cdf0e10cSrcweir uno::Reference< frame::XDispatchProvider > m_xDispatchProvider;
86cdf0e10cSrcweir uno::Reference< util::XURLTransformer > m_xURLTransformer;
87cdf0e10cSrcweir osl::Condition m_aCondition;
88cdf0e10cSrcweir };
89cdf0e10cSrcweir
StateEventHelper(const uno::Reference<frame::XDispatchProvider> & xDispatchProvider,const uno::Reference<util::XURLTransformer> & xURLTransformer,const rtl::OUString & rCommandURL)90cdf0e10cSrcweir StateEventHelper::StateEventHelper(
91cdf0e10cSrcweir const uno::Reference< frame::XDispatchProvider >& xDispatchProvider,
92cdf0e10cSrcweir const uno::Reference< util::XURLTransformer >& xURLTransformer,
93cdf0e10cSrcweir const rtl::OUString& rCommandURL ) :
94cdf0e10cSrcweir m_bCurrentCommandEnabled( true ),
95cdf0e10cSrcweir m_aCommandURL( rCommandURL ),
96cdf0e10cSrcweir m_xDispatchProvider( xDispatchProvider ),
97cdf0e10cSrcweir m_xURLTransformer( xURLTransformer )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir m_aCondition.reset();
100cdf0e10cSrcweir }
101cdf0e10cSrcweir
~StateEventHelper()102cdf0e10cSrcweir StateEventHelper::~StateEventHelper()
103cdf0e10cSrcweir {}
104cdf0e10cSrcweir
queryInterface(const uno::Type & aType)105cdf0e10cSrcweir uno::Any SAL_CALL StateEventHelper::queryInterface(
106cdf0e10cSrcweir const uno::Type& aType )
107cdf0e10cSrcweir throw ( uno::RuntimeException )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir uno::Any a = ::cppu::queryInterface(
110cdf0e10cSrcweir aType,
111cdf0e10cSrcweir SAL_STATIC_CAST( XStatusListener*, this ));
112cdf0e10cSrcweir
113cdf0e10cSrcweir if( a.hasValue() )
114cdf0e10cSrcweir return a;
115cdf0e10cSrcweir
116cdf0e10cSrcweir return ::cppu::OWeakObject::queryInterface( aType );
117cdf0e10cSrcweir }
118cdf0e10cSrcweir
acquire()119cdf0e10cSrcweir void SAL_CALL StateEventHelper::acquire()
120cdf0e10cSrcweir throw ()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir ::cppu::OWeakObject::acquire();
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
release()125cdf0e10cSrcweir void SAL_CALL StateEventHelper::release()
126cdf0e10cSrcweir throw ()
127cdf0e10cSrcweir {
128cdf0e10cSrcweir ::cppu::OWeakObject::release();
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
disposing(const lang::EventObject &)131cdf0e10cSrcweir void SAL_CALL StateEventHelper::disposing(
132cdf0e10cSrcweir const lang::EventObject& )
133cdf0e10cSrcweir throw ( uno::RuntimeException )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
136cdf0e10cSrcweir m_xDispatchProvider.clear();
137cdf0e10cSrcweir m_xURLTransformer.clear();
138cdf0e10cSrcweir m_aCondition.set();
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
statusChanged(const frame::FeatureStateEvent & Event)141cdf0e10cSrcweir void SAL_CALL StateEventHelper::statusChanged(
142cdf0e10cSrcweir const frame::FeatureStateEvent& Event )
143cdf0e10cSrcweir throw ( uno::RuntimeException )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
146cdf0e10cSrcweir m_bCurrentCommandEnabled = Event.IsEnabled;
147cdf0e10cSrcweir m_aCondition.set();
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
isCommandEnabled()150cdf0e10cSrcweir bool StateEventHelper::isCommandEnabled()
151cdf0e10cSrcweir {
152cdf0e10cSrcweir // Be sure that we cannot die during condition wait
153cdf0e10cSrcweir uno::Reference< frame::XStatusListener > xSelf(
154cdf0e10cSrcweir SAL_STATIC_CAST( frame::XStatusListener*, this ));
155cdf0e10cSrcweir
156cdf0e10cSrcweir uno::Reference< frame::XDispatch > xDispatch;
157cdf0e10cSrcweir util::URL aTargetURL;
158cdf0e10cSrcweir {
159cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
160cdf0e10cSrcweir if ( m_xDispatchProvider.is() && m_xURLTransformer.is() )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir ::rtl::OUString aSelf( RTL_CONSTASCII_USTRINGPARAM( "_self" ));
163cdf0e10cSrcweir
164cdf0e10cSrcweir aTargetURL.Complete = m_aCommandURL;
165cdf0e10cSrcweir m_xURLTransformer->parseStrict( aTargetURL );
166cdf0e10cSrcweir
167cdf0e10cSrcweir try
168cdf0e10cSrcweir {
169cdf0e10cSrcweir xDispatch = m_xDispatchProvider->queryDispatch( aTargetURL, aSelf, 0 );
170cdf0e10cSrcweir }
171cdf0e10cSrcweir catch ( uno::RuntimeException& )
172cdf0e10cSrcweir {
173cdf0e10cSrcweir throw;
174cdf0e10cSrcweir }
175cdf0e10cSrcweir catch ( uno::Exception& )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir }
178cdf0e10cSrcweir }
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
181cdf0e10cSrcweir bool bResult( false );
182cdf0e10cSrcweir if ( xDispatch.is() )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir try
185cdf0e10cSrcweir {
186cdf0e10cSrcweir // add/remove ourself to retrieve status by callback
187cdf0e10cSrcweir xDispatch->addStatusListener( xSelf, aTargetURL );
188cdf0e10cSrcweir xDispatch->removeStatusListener( xSelf, aTargetURL );
189cdf0e10cSrcweir
190*2ca93d58Smseidel // wait for answer
191cdf0e10cSrcweir m_aCondition.wait();
192cdf0e10cSrcweir }
193cdf0e10cSrcweir catch ( uno::RuntimeException& )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir throw;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir catch ( uno::Exception& )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
201cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
202cdf0e10cSrcweir bResult = m_bCurrentCommandEnabled;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir return bResult;
206cdf0e10cSrcweir }
207cdf0e10cSrcweir
208cdf0e10cSrcweir /*************************************************************************/
209cdf0e10cSrcweir
210cdf0e10cSrcweir struct ExecuteInfo
211cdf0e10cSrcweir {
212cdf0e10cSrcweir uno::Reference< frame::XDispatch > xDispatch;
213cdf0e10cSrcweir util::URL aTargetURL;
214cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aArgs;
215cdf0e10cSrcweir };
216cdf0e10cSrcweir
lcl_FindPopupFromItemId(const PopupMenu * pPopupMenu,sal_uInt16 nItemId)217cdf0e10cSrcweir static const PopupMenu* lcl_FindPopupFromItemId( const PopupMenu* pPopupMenu, sal_uInt16 nItemId )
218cdf0e10cSrcweir {
219cdf0e10cSrcweir if ( pPopupMenu )
220cdf0e10cSrcweir {
221cdf0e10cSrcweir sal_uInt16 nCount = pPopupMenu->GetItemCount();
222cdf0e10cSrcweir for ( sal_uInt16 i = 0; i < nCount; i++ )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir sal_uInt16 nId = pPopupMenu->GetItemId( i );
225cdf0e10cSrcweir if ( nId == nItemId )
226cdf0e10cSrcweir return pPopupMenu;
227cdf0e10cSrcweir else
228cdf0e10cSrcweir {
229cdf0e10cSrcweir const PopupMenu* pResult( 0 );
230cdf0e10cSrcweir
231cdf0e10cSrcweir const PopupMenu* pSubPopup = pPopupMenu->GetPopupMenu( i );
232cdf0e10cSrcweir if ( pPopupMenu )
233cdf0e10cSrcweir pResult = lcl_FindPopupFromItemId( pSubPopup, nItemId );
234cdf0e10cSrcweir if ( pResult != 0 )
235cdf0e10cSrcweir return pResult;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir }
238cdf0e10cSrcweir }
239cdf0e10cSrcweir
240cdf0e10cSrcweir return NULL;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
lcl_GetItemCommandRecursive(const PopupMenu * pPopupMenu,sal_uInt16 nItemId)243cdf0e10cSrcweir static ::rtl::OUString lcl_GetItemCommandRecursive( const PopupMenu* pPopupMenu, sal_uInt16 nItemId )
244cdf0e10cSrcweir {
245cdf0e10cSrcweir const PopupMenu* pPopup = lcl_FindPopupFromItemId( pPopupMenu, nItemId );
246cdf0e10cSrcweir if ( pPopup )
247cdf0e10cSrcweir return pPopup->GetItemCommand( nItemId );
248cdf0e10cSrcweir else
249cdf0e10cSrcweir return ::rtl::OUString();
250cdf0e10cSrcweir }
251cdf0e10cSrcweir
252cdf0e10cSrcweir /*************************************************************************/
253cdf0e10cSrcweir
ContextMenuHelper(const uno::Reference<frame::XFrame> & xFrame,bool bAutoRefresh)254cdf0e10cSrcweir ContextMenuHelper::ContextMenuHelper(
255cdf0e10cSrcweir const uno::Reference< frame::XFrame >& xFrame,
256cdf0e10cSrcweir bool bAutoRefresh ) :
257cdf0e10cSrcweir m_xWeakFrame( xFrame ),
258cdf0e10cSrcweir m_aSelf( RTL_CONSTASCII_USTRINGPARAM( "_self" )),
259cdf0e10cSrcweir m_bAutoRefresh( bAutoRefresh ),
260cdf0e10cSrcweir m_bUICfgMgrAssociated( false )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir }
263cdf0e10cSrcweir
~ContextMenuHelper()264cdf0e10cSrcweir ContextMenuHelper::~ContextMenuHelper()
265cdf0e10cSrcweir {
266cdf0e10cSrcweir }
267cdf0e10cSrcweir
268cdf0e10cSrcweir void
completeAndExecute(const Point & aPos,PopupMenu & rPopupMenu)269cdf0e10cSrcweir ContextMenuHelper::completeAndExecute(
270cdf0e10cSrcweir const Point& aPos,
271cdf0e10cSrcweir PopupMenu& rPopupMenu )
272cdf0e10cSrcweir {
273cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
274cdf0e10cSrcweir
275cdf0e10cSrcweir associateUIConfigurationManagers();
276cdf0e10cSrcweir completeMenuProperties( &rPopupMenu );
277cdf0e10cSrcweir executePopupMenu( aPos, &rPopupMenu );
278cdf0e10cSrcweir resetAssociations();
279cdf0e10cSrcweir }
280cdf0e10cSrcweir
281cdf0e10cSrcweir void
completeAndExecute(const Point & aPos,const uno::Reference<awt::XPopupMenu> & xPopupMenu)282cdf0e10cSrcweir ContextMenuHelper::completeAndExecute(
283cdf0e10cSrcweir const Point& aPos,
284cdf0e10cSrcweir const uno::Reference< awt::XPopupMenu >& xPopupMenu )
285cdf0e10cSrcweir {
286cdf0e10cSrcweir vos::OGuard aSolarGuard( Application::GetSolarMutex() );
287cdf0e10cSrcweir
288cdf0e10cSrcweir VCLXMenu* pXMenu = VCLXMenu::GetImplementation( xPopupMenu );
289cdf0e10cSrcweir if ( pXMenu )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir PopupMenu* pPopupMenu = dynamic_cast< PopupMenu* >( pXMenu->GetMenu() );
292cdf0e10cSrcweir // as dynamic_cast can return zero check pointer
293cdf0e10cSrcweir if ( pPopupMenu )
294cdf0e10cSrcweir {
295cdf0e10cSrcweir associateUIConfigurationManagers();
296cdf0e10cSrcweir completeMenuProperties( pPopupMenu );
297cdf0e10cSrcweir executePopupMenu( aPos, pPopupMenu );
298cdf0e10cSrcweir resetAssociations();
299cdf0e10cSrcweir }
300cdf0e10cSrcweir }
301cdf0e10cSrcweir }
302cdf0e10cSrcweir
303cdf0e10cSrcweir uno::Reference< awt::XPopupMenu >
create(const::rtl::OUString &)304cdf0e10cSrcweir ContextMenuHelper::create(
305cdf0e10cSrcweir const ::rtl::OUString& /*aPopupMenuResourceId*/ )
306cdf0e10cSrcweir {
307cdf0e10cSrcweir // NOT IMPLEMENTED YET!
308cdf0e10cSrcweir return uno::Reference< awt::XPopupMenu >();
309cdf0e10cSrcweir }
310cdf0e10cSrcweir
311cdf0e10cSrcweir bool
createAndExecute(const Point &,const::rtl::OUString &)312cdf0e10cSrcweir ContextMenuHelper::createAndExecute(
313cdf0e10cSrcweir const Point& /*aPos*/,
314cdf0e10cSrcweir const ::rtl::OUString& /*aPopupMenuResourceId*/ )
315cdf0e10cSrcweir {
316cdf0e10cSrcweir // NOT IMPLEMENTED YET!
317cdf0e10cSrcweir return false;
318cdf0e10cSrcweir }
319cdf0e10cSrcweir
320cdf0e10cSrcweir // private member
321cdf0e10cSrcweir
322cdf0e10cSrcweir void
executePopupMenu(const Point & rPos,PopupMenu * pMenu)323cdf0e10cSrcweir ContextMenuHelper::executePopupMenu(
324cdf0e10cSrcweir const Point& rPos,
325cdf0e10cSrcweir PopupMenu* pMenu )
326cdf0e10cSrcweir {
327cdf0e10cSrcweir if ( pMenu )
328cdf0e10cSrcweir {
329cdf0e10cSrcweir uno::Reference< frame::XFrame > xFrame( m_xWeakFrame );
330cdf0e10cSrcweir if ( xFrame.is() )
331cdf0e10cSrcweir {
332cdf0e10cSrcweir uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow() );
333cdf0e10cSrcweir if ( xWindow.is() )
334cdf0e10cSrcweir {
335cdf0e10cSrcweir Window* pParent = VCLUnoHelper::GetWindow( xWindow );
336cdf0e10cSrcweir sal_uInt16 nResult = pMenu->Execute( pParent, rPos );
337cdf0e10cSrcweir
338cdf0e10cSrcweir if ( nResult > 0 )
339cdf0e10cSrcweir {
340cdf0e10cSrcweir ::rtl::OUString aCommand = lcl_GetItemCommandRecursive( pMenu, nResult );
341cdf0e10cSrcweir if ( aCommand.getLength() > 0 )
342cdf0e10cSrcweir dispatchCommand( xFrame, aCommand );
343cdf0e10cSrcweir }
344cdf0e10cSrcweir }
345cdf0e10cSrcweir }
346cdf0e10cSrcweir }
347cdf0e10cSrcweir }
348cdf0e10cSrcweir
349cdf0e10cSrcweir bool
dispatchCommand(const uno::Reference<::frame::XFrame> & rFrame,const::rtl::OUString & aCommandURL)350cdf0e10cSrcweir ContextMenuHelper::dispatchCommand(
351cdf0e10cSrcweir const uno::Reference< ::frame::XFrame >& rFrame,
352cdf0e10cSrcweir const ::rtl::OUString& aCommandURL )
353cdf0e10cSrcweir {
354cdf0e10cSrcweir if ( !m_xURLTransformer.is() )
355cdf0e10cSrcweir {
356cdf0e10cSrcweir m_xURLTransformer = uno::Reference< util::XURLTransformer >(
357cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance(
358cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
359cdf0e10cSrcweir "com.sun.star.util.URLTransformer" ))),
360cdf0e10cSrcweir uno::UNO_QUERY );
361cdf0e10cSrcweir }
362cdf0e10cSrcweir
363cdf0e10cSrcweir util::URL aTargetURL;
364cdf0e10cSrcweir uno::Reference< frame::XDispatch > xDispatch;
365cdf0e10cSrcweir if ( m_xURLTransformer.is() )
366cdf0e10cSrcweir {
367cdf0e10cSrcweir aTargetURL.Complete = aCommandURL;
368cdf0e10cSrcweir m_xURLTransformer->parseStrict( aTargetURL );
369cdf0e10cSrcweir
370cdf0e10cSrcweir uno::Reference< frame::XDispatchProvider > xDispatchProvider(
371cdf0e10cSrcweir rFrame, uno::UNO_QUERY );
372cdf0e10cSrcweir if ( xDispatchProvider.is() )
373cdf0e10cSrcweir {
374cdf0e10cSrcweir try
375cdf0e10cSrcweir {
376cdf0e10cSrcweir xDispatch = xDispatchProvider->queryDispatch( aTargetURL, m_aSelf, 0 );
377cdf0e10cSrcweir }
378cdf0e10cSrcweir catch ( uno::RuntimeException& )
379cdf0e10cSrcweir {
380cdf0e10cSrcweir throw;
381cdf0e10cSrcweir }
382cdf0e10cSrcweir catch ( uno::Exception& )
383cdf0e10cSrcweir {
384cdf0e10cSrcweir }
385cdf0e10cSrcweir }
386cdf0e10cSrcweir }
387cdf0e10cSrcweir
388cdf0e10cSrcweir if ( xDispatch.is() )
389cdf0e10cSrcweir {
390cdf0e10cSrcweir ExecuteInfo* pExecuteInfo = new ExecuteInfo;
391cdf0e10cSrcweir pExecuteInfo->xDispatch = xDispatch;
392cdf0e10cSrcweir pExecuteInfo->aTargetURL = aTargetURL;
393cdf0e10cSrcweir pExecuteInfo->aArgs = m_aDefaultArgs;
394cdf0e10cSrcweir
395cdf0e10cSrcweir Application::PostUserEvent( STATIC_LINK(0, ContextMenuHelper , ExecuteHdl_Impl), pExecuteInfo );
396cdf0e10cSrcweir return true;
397cdf0e10cSrcweir }
398cdf0e10cSrcweir
399cdf0e10cSrcweir return false;
400cdf0e10cSrcweir }
401cdf0e10cSrcweir
402cdf0e10cSrcweir // retrieves and stores references to our user-interface
403*2ca93d58Smseidel // configuration managers, like image manager, UI command
404cdf0e10cSrcweir // description manager.
405cdf0e10cSrcweir bool
associateUIConfigurationManagers()406cdf0e10cSrcweir ContextMenuHelper::associateUIConfigurationManagers()
407cdf0e10cSrcweir {
408cdf0e10cSrcweir uno::Reference< frame::XFrame > xFrame( m_xWeakFrame );
409cdf0e10cSrcweir if ( !m_bUICfgMgrAssociated && xFrame.is() )
410cdf0e10cSrcweir {
411cdf0e10cSrcweir // clear current state
412cdf0e10cSrcweir m_xDocImageMgr.clear();
413cdf0e10cSrcweir m_xModuleImageMgr.clear();
414cdf0e10cSrcweir m_xUICommandLabels.clear();
415cdf0e10cSrcweir
416cdf0e10cSrcweir try
417cdf0e10cSrcweir {
418cdf0e10cSrcweir uno::Reference < frame::XController > xController;
419cdf0e10cSrcweir uno::Reference < frame::XModel > xModel;
420cdf0e10cSrcweir xController = xFrame->getController();
421cdf0e10cSrcweir if ( xController.is() )
422cdf0e10cSrcweir xModel = xController->getModel();
423cdf0e10cSrcweir
424cdf0e10cSrcweir if ( xModel.is() )
425cdf0e10cSrcweir {
426cdf0e10cSrcweir // retrieve document image manager form model
427cdf0e10cSrcweir uno::Reference< ui::XUIConfigurationManagerSupplier > xSupplier( xModel, uno::UNO_QUERY );
428cdf0e10cSrcweir if ( xSupplier.is() )
429cdf0e10cSrcweir {
430cdf0e10cSrcweir uno::Reference< ui::XUIConfigurationManager > xDocUICfgMgr(
431cdf0e10cSrcweir xSupplier->getUIConfigurationManager(), uno::UNO_QUERY );
432cdf0e10cSrcweir m_xDocImageMgr = uno::Reference< ui::XImageManager >(
433cdf0e10cSrcweir xDocUICfgMgr->getImageManager(), uno::UNO_QUERY );
434cdf0e10cSrcweir }
435cdf0e10cSrcweir }
436cdf0e10cSrcweir
437cdf0e10cSrcweir uno::Reference< frame::XModuleManager > xModuleManager(
438cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance(
439cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
440cdf0e10cSrcweir "com.sun.star.frame.ModuleManager" ))),
441cdf0e10cSrcweir uno::UNO_QUERY );
442cdf0e10cSrcweir
443cdf0e10cSrcweir uno::Reference< ui::XImageManager > xModuleImageManager;
444cdf0e10cSrcweir rtl::OUString aModuleId;
445cdf0e10cSrcweir if ( xModuleManager.is() )
446cdf0e10cSrcweir {
447cdf0e10cSrcweir // retrieve module image manager
448cdf0e10cSrcweir aModuleId = xModuleManager->identify( xFrame );
449cdf0e10cSrcweir
450cdf0e10cSrcweir uno::Reference< ui::XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier(
451cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance(
452cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
453cdf0e10cSrcweir "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
454cdf0e10cSrcweir uno::UNO_QUERY );
455cdf0e10cSrcweir if ( xModuleCfgMgrSupplier.is() )
456cdf0e10cSrcweir {
457cdf0e10cSrcweir uno::Reference< ui::XUIConfigurationManager > xUICfgMgr(
458cdf0e10cSrcweir xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId ));
459cdf0e10cSrcweir if ( xUICfgMgr.is() )
460cdf0e10cSrcweir {
461cdf0e10cSrcweir m_xModuleImageMgr = uno::Reference< ui::XImageManager >(
462cdf0e10cSrcweir xUICfgMgr->getImageManager(), uno::UNO_QUERY );
463cdf0e10cSrcweir }
464cdf0e10cSrcweir }
465cdf0e10cSrcweir }
466cdf0e10cSrcweir
467cdf0e10cSrcweir uno::Reference< container::XNameAccess > xNameAccess(
468cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance(
469cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
470cdf0e10cSrcweir "com.sun.star.frame.UICommandDescription" ))),
471cdf0e10cSrcweir uno::UNO_QUERY );
472cdf0e10cSrcweir if ( xNameAccess.is() )
473cdf0e10cSrcweir {
474cdf0e10cSrcweir try
475cdf0e10cSrcweir {
476cdf0e10cSrcweir uno::Any a = xNameAccess->getByName( aModuleId );
477cdf0e10cSrcweir a >>= m_xUICommandLabels;
478cdf0e10cSrcweir }
479cdf0e10cSrcweir catch ( container::NoSuchElementException& )
480cdf0e10cSrcweir {
481cdf0e10cSrcweir }
482cdf0e10cSrcweir }
483cdf0e10cSrcweir }
484cdf0e10cSrcweir catch ( uno::RuntimeException& )
485cdf0e10cSrcweir {
486cdf0e10cSrcweir throw;
487cdf0e10cSrcweir }
488cdf0e10cSrcweir catch ( uno::Exception& )
489cdf0e10cSrcweir {
490cdf0e10cSrcweir m_bUICfgMgrAssociated = true;
491cdf0e10cSrcweir return false;
492cdf0e10cSrcweir }
493cdf0e10cSrcweir m_bUICfgMgrAssociated = true;
494cdf0e10cSrcweir }
495cdf0e10cSrcweir
496cdf0e10cSrcweir return true;
497cdf0e10cSrcweir }
498cdf0e10cSrcweir
499cdf0e10cSrcweir Image
getImageFromCommandURL(const::rtl::OUString & aCmdURL,bool bHiContrast) const500cdf0e10cSrcweir ContextMenuHelper::getImageFromCommandURL(
501cdf0e10cSrcweir const ::rtl::OUString& aCmdURL,
502cdf0e10cSrcweir bool bHiContrast ) const
503cdf0e10cSrcweir {
504cdf0e10cSrcweir Image aImage;
505cdf0e10cSrcweir sal_Int16 nImageType( ui::ImageType::COLOR_NORMAL|
506cdf0e10cSrcweir ui::ImageType::SIZE_DEFAULT );
507cdf0e10cSrcweir if ( bHiContrast )
508cdf0e10cSrcweir nImageType |= ui::ImageType::COLOR_HIGHCONTRAST;
509cdf0e10cSrcweir
510cdf0e10cSrcweir uno::Sequence< uno::Reference< graphic::XGraphic > > aGraphicSeq;
511cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aImageCmdSeq( 1 );
512cdf0e10cSrcweir aImageCmdSeq[0] = aCmdURL;
513cdf0e10cSrcweir
514cdf0e10cSrcweir if ( m_xDocImageMgr.is() )
515cdf0e10cSrcweir {
516cdf0e10cSrcweir try
517cdf0e10cSrcweir {
518cdf0e10cSrcweir aGraphicSeq = m_xDocImageMgr->getImages( nImageType, aImageCmdSeq );
519cdf0e10cSrcweir uno::Reference< graphic::XGraphic > xGraphic = aGraphicSeq[0];
520cdf0e10cSrcweir aImage = Image( xGraphic );
521cdf0e10cSrcweir
522cdf0e10cSrcweir if ( !!aImage )
523cdf0e10cSrcweir return aImage;
524cdf0e10cSrcweir }
525cdf0e10cSrcweir catch ( uno::RuntimeException& )
526cdf0e10cSrcweir {
527cdf0e10cSrcweir throw;
528cdf0e10cSrcweir }
529cdf0e10cSrcweir catch ( uno::Exception& )
530cdf0e10cSrcweir {
531cdf0e10cSrcweir }
532cdf0e10cSrcweir }
533cdf0e10cSrcweir
534cdf0e10cSrcweir if ( m_xModuleImageMgr.is() )
535cdf0e10cSrcweir {
536cdf0e10cSrcweir try
537cdf0e10cSrcweir {
538cdf0e10cSrcweir aGraphicSeq = m_xModuleImageMgr->getImages( nImageType, aImageCmdSeq );
539cdf0e10cSrcweir uno::Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
540cdf0e10cSrcweir aImage = Image( xGraphic );
541cdf0e10cSrcweir
542cdf0e10cSrcweir if ( !!aImage )
543cdf0e10cSrcweir return aImage;
544cdf0e10cSrcweir }
545cdf0e10cSrcweir catch ( uno::RuntimeException& )
546cdf0e10cSrcweir {
547cdf0e10cSrcweir throw;
548cdf0e10cSrcweir }
549cdf0e10cSrcweir catch ( uno::Exception& )
550cdf0e10cSrcweir {
551cdf0e10cSrcweir }
552cdf0e10cSrcweir }
553cdf0e10cSrcweir
554cdf0e10cSrcweir return aImage;
555cdf0e10cSrcweir }
556cdf0e10cSrcweir
557cdf0e10cSrcweir rtl::OUString
getLabelFromCommandURL(const::rtl::OUString & aCmdURL) const558cdf0e10cSrcweir ContextMenuHelper::getLabelFromCommandURL(
559cdf0e10cSrcweir const ::rtl::OUString& aCmdURL ) const
560cdf0e10cSrcweir {
561cdf0e10cSrcweir ::rtl::OUString aLabel;
562cdf0e10cSrcweir
563cdf0e10cSrcweir if ( m_xUICommandLabels.is() )
564cdf0e10cSrcweir {
565cdf0e10cSrcweir try
566cdf0e10cSrcweir {
567cdf0e10cSrcweir if ( aCmdURL.getLength() > 0 )
568cdf0e10cSrcweir {
569cdf0e10cSrcweir rtl::OUString aStr;
570cdf0e10cSrcweir uno::Sequence< beans::PropertyValue > aPropSeq;
571cdf0e10cSrcweir uno::Any a( m_xUICommandLabels->getByName( aCmdURL ));
572cdf0e10cSrcweir if ( a >>= aPropSeq )
573cdf0e10cSrcweir {
574cdf0e10cSrcweir for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
575cdf0e10cSrcweir {
576cdf0e10cSrcweir if ( aPropSeq[i].Name.equalsAscii( "Label" ))
577cdf0e10cSrcweir {
578cdf0e10cSrcweir aPropSeq[i].Value >>= aStr;
579cdf0e10cSrcweir break;
580cdf0e10cSrcweir }
581cdf0e10cSrcweir }
582cdf0e10cSrcweir }
583cdf0e10cSrcweir aLabel = aStr;
584cdf0e10cSrcweir }
585cdf0e10cSrcweir }
586cdf0e10cSrcweir catch ( uno::RuntimeException& )
587cdf0e10cSrcweir {
588cdf0e10cSrcweir }
589cdf0e10cSrcweir catch ( uno::Exception& )
590cdf0e10cSrcweir {
591cdf0e10cSrcweir }
592cdf0e10cSrcweir }
593cdf0e10cSrcweir
594cdf0e10cSrcweir return aLabel;
595cdf0e10cSrcweir }
596cdf0e10cSrcweir
597cdf0e10cSrcweir void
completeMenuProperties(Menu * pMenu)598cdf0e10cSrcweir ContextMenuHelper::completeMenuProperties(
599cdf0e10cSrcweir Menu* pMenu )
600cdf0e10cSrcweir {
601cdf0e10cSrcweir // Retrieve some settings necessary to display complete context
602cdf0e10cSrcweir // menu correctly.
603cdf0e10cSrcweir const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
604cdf0e10cSrcweir bool bShowMenuImages( rSettings.GetUseImagesInMenus() );
605cdf0e10cSrcweir bool bIsHiContrast( rSettings.GetHighContrastMode() );
606cdf0e10cSrcweir
607cdf0e10cSrcweir if ( pMenu )
608cdf0e10cSrcweir {
609cdf0e10cSrcweir uno::Reference< frame::XFrame > xFrame( m_xWeakFrame );
610cdf0e10cSrcweir uno::Reference< frame::XDispatchProvider > xDispatchProvider( xFrame, uno::UNO_QUERY );
611cdf0e10cSrcweir
612cdf0e10cSrcweir if ( !m_xURLTransformer.is() )
613cdf0e10cSrcweir {
614cdf0e10cSrcweir m_xURLTransformer = uno::Reference< util::XURLTransformer >(
615cdf0e10cSrcweir ::comphelper::getProcessServiceFactory()->createInstance(
616cdf0e10cSrcweir rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
617cdf0e10cSrcweir "com.sun.star.util.URLTransformer" ))),
618cdf0e10cSrcweir uno::UNO_QUERY );
619cdf0e10cSrcweir }
620cdf0e10cSrcweir
621cdf0e10cSrcweir for ( sal_uInt16 nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
622cdf0e10cSrcweir {
623cdf0e10cSrcweir sal_uInt16 nId = pMenu->GetItemId( nPos );
624cdf0e10cSrcweir PopupMenu* pPopupMenu = pMenu->GetPopupMenu( nId );
625cdf0e10cSrcweir if ( pPopupMenu )
626cdf0e10cSrcweir completeMenuProperties( pPopupMenu );
627cdf0e10cSrcweir if ( pMenu->GetItemType( nPos ) != MENUITEM_SEPARATOR )
628cdf0e10cSrcweir {
629cdf0e10cSrcweir ::rtl::OUString aCmdURL( pMenu->GetItemCommand( nId ));
630cdf0e10cSrcweir
631cdf0e10cSrcweir if ( bShowMenuImages )
632cdf0e10cSrcweir {
633cdf0e10cSrcweir Image aImage;
634cdf0e10cSrcweir if ( aCmdURL.getLength() > 0 )
635cdf0e10cSrcweir aImage = getImageFromCommandURL( aCmdURL, bIsHiContrast );
636cdf0e10cSrcweir pMenu->SetItemImage( nId, aImage );
637cdf0e10cSrcweir }
638cdf0e10cSrcweir else
639cdf0e10cSrcweir pMenu->SetItemImage( nId, Image() );
640cdf0e10cSrcweir
641cdf0e10cSrcweir if ( pMenu->GetItemText( nId ).Len() == 0 )
642cdf0e10cSrcweir {
643cdf0e10cSrcweir ::rtl::OUString aLabel( getLabelFromCommandURL( aCmdURL ));
644cdf0e10cSrcweir pMenu->SetItemText( nId, aLabel );
645cdf0e10cSrcweir }
646cdf0e10cSrcweir
647cdf0e10cSrcweir // Use helper to retrieve state of the command URL
648cdf0e10cSrcweir StateEventHelper* pHelper = new StateEventHelper(
649cdf0e10cSrcweir xDispatchProvider,
650cdf0e10cSrcweir m_xURLTransformer,
651cdf0e10cSrcweir aCmdURL );
652cdf0e10cSrcweir
653cdf0e10cSrcweir uno::Reference< frame::XStatusListener > xHelper( pHelper );
654cdf0e10cSrcweir pMenu->EnableItem( nId, pHelper->isCommandEnabled() );
655cdf0e10cSrcweir }
656cdf0e10cSrcweir }
657cdf0e10cSrcweir }
658cdf0e10cSrcweir }
659cdf0e10cSrcweir
IMPL_STATIC_LINK_NOINSTANCE(ContextMenuHelper,ExecuteHdl_Impl,ExecuteInfo *,pExecuteInfo)660cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( ContextMenuHelper, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
661cdf0e10cSrcweir {
662cdf0e10cSrcweir // Release solar mutex to prevent deadlocks with clipboard thread
663cdf0e10cSrcweir const sal_uInt32 nRef = Application::ReleaseSolarMutex();
664cdf0e10cSrcweir try
665cdf0e10cSrcweir {
666cdf0e10cSrcweir // Asynchronous execution as this can lead to our own destruction while we are
667cdf0e10cSrcweir // on the stack. Stack unwinding would access the destroyed context menu.
668cdf0e10cSrcweir pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
669cdf0e10cSrcweir }
670cdf0e10cSrcweir catch ( uno::Exception& )
671cdf0e10cSrcweir {
672cdf0e10cSrcweir }
673cdf0e10cSrcweir
674cdf0e10cSrcweir // Acquire solar mutex again
675cdf0e10cSrcweir Application::AcquireSolarMutex( nRef );
676cdf0e10cSrcweir delete pExecuteInfo;
677cdf0e10cSrcweir return 0;
678cdf0e10cSrcweir }
679cdf0e10cSrcweir
680cdf0e10cSrcweir } // namespace svt
681*2ca93d58Smseidel
682*2ca93d58Smseidel /* vim: set noet sw=4 ts=4: */
683