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_framework.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <uielement/macrosmenucontroller.hxx> 32*cdf0e10cSrcweir #include <uielement/menubarmanager.hxx> 33*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx> 34*cdf0e10cSrcweir #include "services.h" 35*cdf0e10cSrcweir #include <classes/resource.hrc> 36*cdf0e10cSrcweir #include <classes/fwkresid.hxx> 37*cdf0e10cSrcweir #include <framework/imageproducer.hxx> 38*cdf0e10cSrcweir #include <com/sun/star/awt/MenuItemStyle.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/container/XContentEnumerationAccess.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/style/XStyleFamiliesSupplier.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/frame/XModuleManager.hpp> 44*cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 45*cdf0e10cSrcweir #include <vcl/svapp.hxx> 46*cdf0e10cSrcweir #include <vcl/i18nhelp.hxx> 47*cdf0e10cSrcweir #include <tools/urlobj.hxx> 48*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 49*cdf0e10cSrcweir #include <dispatch/uieventloghelper.hxx> 50*cdf0e10cSrcweir #include "helper/mischelper.hxx" 51*cdf0e10cSrcweir #include "helpid.hrc" 52*cdf0e10cSrcweir #include <vos/mutex.hxx> 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir using namespace com::sun::star::uno; 55*cdf0e10cSrcweir using namespace com::sun::star::lang; 56*cdf0e10cSrcweir using namespace com::sun::star::frame; 57*cdf0e10cSrcweir using namespace com::sun::star::beans; 58*cdf0e10cSrcweir using namespace com::sun::star::util; 59*cdf0e10cSrcweir using namespace com::sun::star::style; 60*cdf0e10cSrcweir using namespace com::sun::star::container; 61*cdf0e10cSrcweir using namespace ::com::sun::star::frame; 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir namespace framework 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir class 66*cdf0e10cSrcweir DEFINE_XSERVICEINFO_MULTISERVICE ( MacrosMenuController , 67*cdf0e10cSrcweir OWeakObject , 68*cdf0e10cSrcweir SERVICENAME_POPUPMENUCONTROLLER , 69*cdf0e10cSrcweir IMPLEMENTATIONNAME_MACROSMENUCONTROLLER 70*cdf0e10cSrcweir ) 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir DEFINE_INIT_SERVICE ( MacrosMenuController, {} ) 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir MacrosMenuController::MacrosMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) : 75*cdf0e10cSrcweir svt::PopupMenuControllerBase( xServiceManager ), 76*cdf0e10cSrcweir m_xServiceManager( xServiceManager) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir MacrosMenuController::~MacrosMenuController() 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir OSL_TRACE("calling dtor"); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir // private function 86*cdf0e10cSrcweir void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu ) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir VCLXPopupMenu* pVCLPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu ); 89*cdf0e10cSrcweir PopupMenu* pPopupMenu = 0; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() ); 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir resetPopupMenu( rPopupMenu ); 94*cdf0e10cSrcweir if ( pVCLPopupMenu ) 95*cdf0e10cSrcweir pPopupMenu = (PopupMenu *)pVCLPopupMenu->GetMenu(); 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir if (!pPopupMenu) 98*cdf0e10cSrcweir return; 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir // insert basic 101*cdf0e10cSrcweir String aCommand = String::CreateFromAscii( ".uno:MacroDialog" ); 102*cdf0e10cSrcweir String aDisplayName = RetrieveLabelFromCommand( aCommand ); 103*cdf0e10cSrcweir pPopupMenu->InsertItem( 2, aDisplayName ); 104*cdf0e10cSrcweir pPopupMenu->SetItemCommand( 2, aCommand ); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir // insert providers but not basic or java 107*cdf0e10cSrcweir addScriptItems( pPopupMenu, 4); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // XEventListener 111*cdf0e10cSrcweir void SAL_CALL MacrosMenuController::disposing( const EventObject& ) throw ( RuntimeException ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY ); 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex ); 116*cdf0e10cSrcweir OSL_TRACE("disposing"); 117*cdf0e10cSrcweir m_xFrame.clear(); 118*cdf0e10cSrcweir m_xDispatch.clear(); 119*cdf0e10cSrcweir m_xServiceManager.clear(); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir if ( m_xPopupMenu.is() ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY )); 124*cdf0e10cSrcweir OSL_TRACE("removed listener"); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir m_xPopupMenu.clear(); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir // XStatusListener 130*cdf0e10cSrcweir void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& ) throw ( RuntimeException ) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir osl::MutexGuard aLock( m_aMutex ); 133*cdf0e10cSrcweir if ( m_xPopupMenu.is() ) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir fillPopupMenu( m_xPopupMenu ); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir // XMenuListener 140*cdf0e10cSrcweir void MacrosMenuController::impl_select(const Reference< XDispatch >& /*_xDispatch*/,const ::com::sun::star::util::URL& aTargetURL) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir // need to requery, since we handle more than one type of Command 143*cdf0e10cSrcweir // if we don't do this only .uno:ScriptOrganizer commands are executed 144*cdf0e10cSrcweir Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY ); 145*cdf0e10cSrcweir Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 ); 146*cdf0e10cSrcweir if( xDispatch.is() ) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir ExecuteInfo* pExecuteInfo = new ExecuteInfo; 149*cdf0e10cSrcweir pExecuteInfo->xDispatch = xDispatch; 150*cdf0e10cSrcweir pExecuteInfo->aTargetURL = aTargetURL; 151*cdf0e10cSrcweir //pExecuteInfo->aArgs = aArgs; 152*cdf0e10cSrcweir if(::comphelper::UiEventsLogger::isEnabled()) //#i88653# 153*cdf0e10cSrcweir UiEventLogHelper(::rtl::OUString::createFromAscii("MacrosMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, pExecuteInfo->aArgs); 154*cdf0e10cSrcweir // xDispatch->dispatch( aTargetURL, aArgs ); 155*cdf0e10cSrcweir Application::PostUserEvent( STATIC_LINK(0, MacrosMenuController , ExecuteHdl_Impl), pExecuteInfo ); 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir else 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir IMPL_STATIC_LINK_NOINSTANCE( MacrosMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir try 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir // Asynchronous execution as this can lead to our own destruction! 168*cdf0e10cSrcweir // Framework can recycle our current frame and the layout manager disposes all user interface 169*cdf0e10cSrcweir // elements if a component gets detached from its frame! 170*cdf0e10cSrcweir pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs ); 171*cdf0e10cSrcweir } 172*cdf0e10cSrcweir catch ( Exception& ) 173*cdf0e10cSrcweir { 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir delete pExecuteInfo; 176*cdf0e10cSrcweir return 0; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir String MacrosMenuController::RetrieveLabelFromCommand( const String& aCmdURL ) 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir sal_Bool bModuleIdentified = m_aModuleIdentifier.getLength() != 0; 182*cdf0e10cSrcweir return framework::RetrieveLabelFromCommand(aCmdURL,m_xServiceManager,m_xUICommandLabels,m_xFrame,m_aModuleIdentifier,bModuleIdentified,"Label"); 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir void MacrosMenuController::addScriptItems( PopupMenu* pPopupMenu, sal_uInt16 startItemId ) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir const String aCmdBase = String::CreateFromAscii( ".uno:ScriptOrganizer?ScriptOrganizer.Language:string=" ); 188*cdf0e10cSrcweir const String ellipsis = String::CreateFromAscii( "..." ); 189*cdf0e10cSrcweir const ::rtl::OUString providerKey = 190*cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.script.provider.ScriptProviderFor" ); 191*cdf0e10cSrcweir const ::rtl::OUString languageProviderName = 192*cdf0e10cSrcweir ::rtl::OUString::createFromAscii("com.sun.star.script.provider.LanguageScriptProvider" ); 193*cdf0e10cSrcweir sal_uInt16 itemId = startItemId; 194*cdf0e10cSrcweir Reference< XContentEnumerationAccess > xEnumAccess = Reference< XContentEnumerationAccess >( m_xServiceManager, UNO_QUERY_THROW ); 195*cdf0e10cSrcweir Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName ); 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir while ( xEnum->hasMoreElements() ) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir Reference< XServiceInfo > xServiceInfo; 200*cdf0e10cSrcweir if ( sal_False == ( xEnum->nextElement() >>= xServiceInfo ) ) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir break; 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames(); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir if ( serviceNames.getLength() > 0 ) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir if ( serviceNames[ index ].indexOf( providerKey ) == 0 ) 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir ::rtl::OUString serviceName = serviceNames[ index ]; 213*cdf0e10cSrcweir String aCommand = aCmdBase; 214*cdf0e10cSrcweir String aDisplayName = String( serviceName.copy( providerKey.getLength() ) ); 215*cdf0e10cSrcweir if( aDisplayName.Equals( String::CreateFromAscii( "Java" ) ) || aDisplayName.Equals( String::CreateFromAscii( "Basic" ) ) ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir // no entries for Java & Basic added elsewhere 218*cdf0e10cSrcweir break; 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir aCommand.Append( aDisplayName ); 221*cdf0e10cSrcweir aDisplayName.Append( ellipsis ); 222*cdf0e10cSrcweir pPopupMenu->InsertItem( itemId, aDisplayName ); 223*cdf0e10cSrcweir pPopupMenu->SetItemCommand( itemId, aCommand ); 224*cdf0e10cSrcweir itemId++; 225*cdf0e10cSrcweir break; 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir } 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir } 233