Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <uielement/macrosmenucontroller.hxx>
22 : #include <uielement/menubarmanager.hxx>
23 : #include <threadhelp/resetableguard.hxx>
24 : #include "services.h"
25 : #include <classes/resource.hrc>
26 : #include <classes/fwkresid.hxx>
27 : #include <framework/imageproducer.hxx>
28 : #include <com/sun/star/awt/MenuItemStyle.hpp>
29 : #include <com/sun/star/beans/PropertyValue.hpp>
30 : #include <com/sun/star/beans/XPropertySet.hpp>
31 : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
32 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
33 : #include <com/sun/star/frame/XModuleManager.hpp>
34 : #include <comphelper/processfactory.hxx>
35 : #include <vcl/svapp.hxx>
36 : #include <vcl/i18nhelp.hxx>
37 : #include <rtl/ustrbuf.hxx>
38 : #include "helper/mischelper.hxx"
39 : #include "helpid.hrc"
40 : #include <osl/mutex.hxx>
41 :
42 : using namespace com::sun::star::uno;
43 : using namespace com::sun::star::lang;
44 : using namespace com::sun::star::frame;
45 : using namespace com::sun::star::beans;
46 : using namespace com::sun::star::util;
47 : using namespace com::sun::star::style;
48 : using namespace com::sun::star::container;
49 : using namespace ::com::sun::star::frame;
50 :
51 : namespace framework
52 : {
53 : class
54 0 : DEFINE_XSERVICEINFO_MULTISERVICE ( MacrosMenuController ,
55 : OWeakObject ,
56 : SERVICENAME_POPUPMENUCONTROLLER ,
57 : IMPLEMENTATIONNAME_MACROSMENUCONTROLLER
58 : )
59 :
60 0 : DEFINE_INIT_SERVICE ( MacrosMenuController, {} )
61 :
62 0 : MacrosMenuController::MacrosMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceManager ) :
63 : svt::PopupMenuControllerBase( xServiceManager ),
64 0 : m_xServiceManager( xServiceManager)
65 : {
66 0 : }
67 :
68 0 : MacrosMenuController::~MacrosMenuController()
69 : {
70 : OSL_TRACE("calling dtor");
71 0 : }
72 :
73 : // private function
74 0 : void MacrosMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
75 : {
76 0 : VCLXPopupMenu* pVCLPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( rPopupMenu );
77 0 : PopupMenu* pPopupMenu = 0;
78 :
79 0 : SolarMutexGuard aSolarMutexGuard;
80 :
81 0 : resetPopupMenu( rPopupMenu );
82 0 : if ( pVCLPopupMenu )
83 0 : pPopupMenu = (PopupMenu *)pVCLPopupMenu->GetMenu();
84 :
85 0 : if (!pPopupMenu)
86 0 : return;
87 :
88 : // insert basic
89 0 : rtl::OUString aCommand(".uno:MacroDialog");
90 0 : String aDisplayName = RetrieveLabelFromCommand( aCommand );
91 0 : pPopupMenu->InsertItem( 2, aDisplayName );
92 0 : pPopupMenu->SetItemCommand( 2, aCommand );
93 :
94 : // insert providers but not basic or java
95 0 : addScriptItems( pPopupMenu, 4);
96 : }
97 :
98 : // XEventListener
99 0 : void SAL_CALL MacrosMenuController::disposing( const EventObject& ) throw ( RuntimeException )
100 : {
101 0 : Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
102 :
103 0 : osl::MutexGuard aLock( m_aMutex );
104 : OSL_TRACE("disposing");
105 0 : m_xFrame.clear();
106 0 : m_xDispatch.clear();
107 0 : m_xServiceManager.clear();
108 :
109 0 : if ( m_xPopupMenu.is() )
110 : {
111 0 : m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
112 : OSL_TRACE("removed listener");
113 : }
114 0 : m_xPopupMenu.clear();
115 0 : }
116 :
117 : // XStatusListener
118 0 : void SAL_CALL MacrosMenuController::statusChanged( const FeatureStateEvent& ) throw ( RuntimeException )
119 : {
120 0 : osl::MutexGuard aLock( m_aMutex );
121 0 : if ( m_xPopupMenu.is() )
122 : {
123 0 : fillPopupMenu( m_xPopupMenu );
124 0 : }
125 0 : }
126 :
127 : // XMenuListener
128 0 : void MacrosMenuController::impl_select(const Reference< XDispatch >& /*_xDispatch*/,const ::com::sun::star::util::URL& aTargetURL)
129 : {
130 : // need to requery, since we handle more than one type of Command
131 : // if we don't do this only .uno:ScriptOrganizer commands are executed
132 0 : Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
133 0 : Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
134 0 : if( xDispatch.is() )
135 : {
136 0 : ExecuteInfo* pExecuteInfo = new ExecuteInfo;
137 0 : pExecuteInfo->xDispatch = xDispatch;
138 0 : pExecuteInfo->aTargetURL = aTargetURL;
139 0 : Application::PostUserEvent( STATIC_LINK(0, MacrosMenuController , ExecuteHdl_Impl), pExecuteInfo );
140 0 : }
141 0 : }
142 :
143 :
144 0 : IMPL_STATIC_LINK_NOINSTANCE( MacrosMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
145 : {
146 : try
147 : {
148 : // Asynchronous execution as this can lead to our own destruction!
149 : // Framework can recycle our current frame and the layout manager disposes all user interface
150 : // elements if a component gets detached from its frame!
151 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
152 : }
153 0 : catch ( const Exception& )
154 : {
155 : }
156 0 : delete pExecuteInfo;
157 0 : return 0;
158 : }
159 :
160 0 : String MacrosMenuController::RetrieveLabelFromCommand( const String& aCmdURL )
161 : {
162 0 : sal_Bool bModuleIdentified = !m_aModuleIdentifier.isEmpty();
163 0 : return framework::RetrieveLabelFromCommand(aCmdURL, comphelper::getComponentContext(m_xServiceManager),m_xUICommandLabels,m_xFrame,m_aModuleIdentifier,bModuleIdentified,"Label");
164 : }
165 :
166 0 : void MacrosMenuController::addScriptItems( PopupMenu* pPopupMenu, sal_uInt16 startItemId )
167 : {
168 0 : const rtl::OUString aCmdBase(".uno:ScriptOrganizer?ScriptOrganizer.Language:string=");
169 0 : const rtl::OUString ellipsis( "..." );
170 0 : const rtl::OUString providerKey("com.sun.star.script.provider.ScriptProviderFor");
171 0 : const rtl::OUString languageProviderName("com.sun.star.script.provider.LanguageScriptProvider");
172 0 : sal_uInt16 itemId = startItemId;
173 0 : Reference< XContentEnumerationAccess > xEnumAccess = Reference< XContentEnumerationAccess >( m_xServiceManager, UNO_QUERY_THROW );
174 0 : Reference< XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName );
175 :
176 0 : while ( xEnum->hasMoreElements() )
177 : {
178 0 : Reference< XServiceInfo > xServiceInfo;
179 0 : if ( sal_False == ( xEnum->nextElement() >>= xServiceInfo ) )
180 : {
181 : break;
182 : }
183 0 : Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames();
184 :
185 0 : if ( serviceNames.getLength() > 0 )
186 : {
187 0 : for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ )
188 : {
189 0 : if ( serviceNames[ index ].indexOf( providerKey ) == 0 )
190 : {
191 0 : ::rtl::OUString serviceName = serviceNames[ index ];
192 0 : String aCommand = aCmdBase;
193 0 : String aDisplayName = String( serviceName.copy( providerKey.getLength() ) );
194 0 : if( aDisplayName.Equals( rtl::OUString("Java") ) || aDisplayName.Equals( rtl::OUString("Basic") ) )
195 : {
196 : // no entries for Java & Basic added elsewhere
197 : break;
198 : }
199 0 : aCommand.Append( aDisplayName );
200 0 : aDisplayName.Append( ellipsis );
201 0 : pPopupMenu->InsertItem( itemId, aDisplayName );
202 0 : pPopupMenu->SetItemCommand( itemId, aCommand );
203 0 : itemId++;
204 0 : break;
205 : }
206 : }
207 : }
208 0 : }
209 0 : }
210 :
211 : }
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|