merge the formfield patch from ooo-build
[ooovba.git] / framework / source / uielement / macrosmenucontroller.cxx
blob65385b780cf106520ac586bc7a842e5132916b98
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macrosmenucontroller.cxx,v $
10 * $Revision: 1.12.40.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 #include <uielement/macrosmenucontroller.hxx>
35 #include <uielement/menubarmanager.hxx>
36 #include <threadhelp/resetableguard.hxx>
37 #include "services.h"
38 #include <classes/resource.hrc>
39 #include <classes/fwkresid.hxx>
40 #include <helper/imageproducer.hxx>
41 #include <com/sun/star/awt/MenuItemStyle.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
45 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
46 #include <com/sun/star/frame/XModuleManager.hpp>
47 #include <comphelper/processfactory.hxx>
48 #include <vcl/svapp.hxx>
49 #include <vcl/i18nhelp.hxx>
50 #include <tools/urlobj.hxx>
51 #include <rtl/ustrbuf.hxx>
52 #include <dispatch/uieventloghelper.hxx>
53 #include "helper/mischelper.hxx"
55 using namespace com::sun::star::uno;
56 using namespace com::sun::star::lang;
57 using namespace com::sun::star::frame;
58 using namespace com::sun::star::beans;
59 using namespace com::sun::star::util;
60 using namespace com::sun::star::style;
61 using namespace com::sun::star::container;
62 using namespace ::com::sun::star::frame;
64 namespace framework
66 class
67 DEFINE_XSERVICEINFO_MULTISERVICE ( MacrosMenuController ,
68 OWeakObject ,
69 SERVICENAME_POPUPMENUCONTROLLER ,
70 IMPLEMENTATIONNAME_MACROSMENUCONTROLLER
73 DEFINE_INIT_SERVICE ( MacrosMenuController, {} )
75 MacrosMenuController::MacrosMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
76 PopupMenuControllerBase( xServiceManager ),
77 m_xServiceManager( xServiceManager)
81 MacrosMenuController::~MacrosMenuController()
83 OSL_TRACE("calling dtor");
86 // private function
87 void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
89 VCLXPopupMenu* pVCLPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
90 PopupMenu* pPopupMenu = 0;
92 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
94 resetPopupMenu( rPopupMenu );
95 if ( pVCLPopupMenu )
96 pPopupMenu = (PopupMenu *)pVCLPopupMenu->GetMenu();
98 // insert basic
99 String aCommand = String::CreateFromAscii( ".uno:MacroDialog" );
100 String aDisplayName = RetrieveLabelFromCommand( aCommand );
101 pPopupMenu->InsertItem( 2, aDisplayName );
102 pPopupMenu->SetItemCommand( 2, aCommand );
103 //pPopupMenu->SetHelpId( 2, HID_SVX_BASIC_MACRO_ORGANIZER );
104 pPopupMenu->SetHelpId( 2, 40012 );
106 // insert providers but not basic or java
107 addScriptItems( pPopupMenu, 4);
110 // XEventListener
111 void SAL_CALL MacrosMenuController::disposing( const EventObject& ) throw ( RuntimeException )
113 Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
115 ResetableGuard aLock( m_aLock );
116 OSL_TRACE("disposing");
117 m_xFrame.clear();
118 m_xDispatch.clear();
119 m_xServiceManager.clear();
121 if ( m_xPopupMenu.is() )
123 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
124 OSL_TRACE("removed listener");
126 m_xPopupMenu.clear();
129 // XStatusListener
130 void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& ) throw ( RuntimeException )
132 ResetableGuard aLock( m_aLock );
133 if ( m_xPopupMenu.is() )
135 fillPopupMenu( m_xPopupMenu );
139 // XMenuListener
140 void MacrosMenuController::impl_select(const Reference< XDispatch >& /*_xDispatch*/,const ::com::sun::star::util::URL& aTargetURL)
142 // need to requery, since we handle more than one type of Command
143 // if we don't do this only .uno:ScriptOrganizer commands are executed
144 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
145 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
146 if( xDispatch.is() )
148 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
149 pExecuteInfo->xDispatch = xDispatch;
150 pExecuteInfo->aTargetURL = aTargetURL;
151 //pExecuteInfo->aArgs = aArgs;
152 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
153 UiEventLogHelper(::rtl::OUString::createFromAscii("MacrosMenuController")).log(m_xServiceManager, m_xFrame, aTargetURL, pExecuteInfo->aArgs);
154 // xDispatch->dispatch( aTargetURL, aArgs );
155 Application::PostUserEvent( STATIC_LINK(0, MacrosMenuController , ExecuteHdl_Impl), pExecuteInfo );
157 else
163 IMPL_STATIC_LINK_NOINSTANCE( MacrosMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
167 // Asynchronous execution as this can lead to our own destruction!
168 // Framework can recycle our current frame and the layout manager disposes all user interface
169 // elements if a component gets detached from its frame!
170 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
172 catch ( Exception& )
175 delete pExecuteInfo;
176 return 0;
179 String MacrosMenuController::RetrieveLabelFromCommand( const String& aCmdURL )
181 sal_Bool bModuleIdentified = m_aModuleIdentifier.getLength() != 0;
182 return framework::RetrieveLabelFromCommand(aCmdURL,m_xServiceManager,m_xUICommandLabels,m_xFrame,m_aModuleIdentifier,bModuleIdentified,"Label");
185 void MacrosMenuController::addScriptItems( PopupMenu* pPopupMenu, USHORT startItemId )
187 const String aCmdBase = String::CreateFromAscii( ".uno:ScriptOrganizer?ScriptOrganizer.Language:string=" );
188 const String ellipsis = String::CreateFromAscii( "..." );
189 const ::rtl::OUString providerKey =
190 ::rtl::OUString::createFromAscii("com.sun.star.script.provider.ScriptProviderFor" );
191 const ::rtl::OUString languageProviderName =
192 ::rtl::OUString::createFromAscii("com.sun.star.script.provider.LanguageScriptProvider" );
193 USHORT itemId = startItemId;
194 Reference< XContentEnumerationAccess > xEnumAccess = Reference< XContentEnumerationAccess >( m_xServiceManager, UNO_QUERY_THROW );
195 Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
197 while ( xEnum->hasMoreElements() )
199 Reference< XServiceInfo > xServiceInfo;
200 if ( sal_False == ( xEnum->nextElement() >>= xServiceInfo ) )
202 break;
204 Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
206 if ( serviceNames.getLength() > 0 )
208 for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
210 if ( serviceNames[ index ].indexOf( providerKey ) == 0 )
212 ::rtl::OUString serviceName = serviceNames[ index ];
213 String aCommand = aCmdBase;
214 String aDisplayName = String( serviceName.copy( providerKey.getLength() ) );
215 if( aDisplayName.Equals( String::CreateFromAscii( "Java" ) ) || aDisplayName.Equals( String::CreateFromAscii( "Basic" ) ) )
217 // no entries for Java & Basic added elsewhere
218 break;
220 aCommand.Append( aDisplayName );
221 aDisplayName.Append( ellipsis );
222 pPopupMenu->InsertItem( itemId, aDisplayName );
223 pPopupMenu->SetItemCommand( itemId, aCommand );
224 //pPopupMenu->SetHelpId( itemId, HID_SVX_COMMON_MACRO_ORGANIZER );
225 pPopupMenu->SetHelpId( itemId, 40014 );
226 itemId++;
227 break;