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