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 : #include "vbacommandbarhelper.hxx"
20 : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
21 : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
22 : #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
23 : #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
24 : #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
25 : #include <com/sun/star/ui/XUIElement.hpp>
26 : #include <com/sun/star/ui/UIElementType.hpp>
27 : #include <com/sun/star/ui/theWindowStateConfiguration.hpp>
28 : #include <comphelper/processfactory.hxx>
29 : #include <vbahelper/vbahelper.hxx>
30 : #include <rtl/ustrbuf.hxx>
31 : #include <time.h>
32 : #include <map>
33 :
34 : using namespace com::sun::star;
35 : using namespace ooo::vba;
36 :
37 : typedef std::map< OUString, OUString > MSO2OOCommandbarMap;
38 :
39 : class MSO2OOCommandbarHelper
40 : {
41 : private:
42 : static MSO2OOCommandbarHelper* pMSO2OOCommandbarHelper;
43 : MSO2OOCommandbarMap maBuildinToolbarMap;
44 :
45 0 : MSO2OOCommandbarHelper()
46 0 : {
47 : // Buildin toolbars
48 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Standard") , OUString("private:resource/toolbar/standardbar" ) ) );
49 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Formatting"), OUString("private:resource/toolbar/formatobjectbar") ) );
50 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Drawing"), OUString("private:resource/toolbar/drawbar") ) );
51 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Toolbar List"), OUString("private:resource/toolbar/toolbar") ) );
52 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Forms"), OUString("private:resource/toolbar/formcontrols") ) );
53 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Form Controls"),OUString("private:resource/toolbar/formcontrols") ) );
54 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Full Screen"), OUString("private:resource/toolbar/fullscreenbar") ) );
55 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Chart"), OUString("private:resource/toolbar/flowchartshapes") ) );
56 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("Picture"), OUString("private:resource/toolbar/graphicobjectbar") ) );
57 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("WordArt"), OUString("private:resource/toolbar/fontworkobjectbar") ) );
58 0 : maBuildinToolbarMap.insert( std::make_pair( OUString("3-D Settings"), OUString("private:resource/toolbar/extrusionobjectbar") ) );
59 0 : }
60 :
61 : public:
62 0 : virtual ~MSO2OOCommandbarHelper() {};
63 0 : static MSO2OOCommandbarHelper* getMSO2OOCommandbarHelper()
64 : {
65 0 : if( pMSO2OOCommandbarHelper == NULL )
66 : {
67 0 : pMSO2OOCommandbarHelper = new MSO2OOCommandbarHelper();
68 : }
69 0 : return pMSO2OOCommandbarHelper;
70 : }
71 :
72 0 : OUString findBuildinToolbar( const OUString& sToolbarName )
73 : {
74 0 : MSO2OOCommandbarMap::iterator it = maBuildinToolbarMap.begin();
75 0 : for(; it != maBuildinToolbarMap.end(); ++it )
76 : {
77 0 : OUString sName = it->first;
78 0 : if( sName.equalsIgnoreAsciiCase( sToolbarName ) )
79 0 : return it->second;
80 0 : }
81 0 : return OUString();
82 : }
83 : };
84 :
85 : MSO2OOCommandbarHelper* MSO2OOCommandbarHelper::pMSO2OOCommandbarHelper = NULL;
86 :
87 :
88 0 : VbaCommandBarHelper::VbaCommandBarHelper( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::frame::XModel >& xModel ) throw (css::uno::RuntimeException) : mxContext( xContext ), mxModel( xModel )
89 : {
90 0 : Init();
91 0 : }
92 :
93 0 : void VbaCommandBarHelper::Init( ) throw (css::uno::RuntimeException)
94 : {
95 0 : uno::Reference< css::ui::XUIConfigurationManagerSupplier > xUICfgSupplier( mxModel, uno::UNO_QUERY_THROW );
96 0 : m_xDocCfgMgr = xUICfgSupplier->getUIConfigurationManager();
97 :
98 0 : uno::Reference< lang::XServiceInfo > xServiceInfo( mxModel, uno::UNO_QUERY_THROW );
99 0 : if( xServiceInfo->supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
100 : {
101 0 : maModuleId = "com.sun.star.sheet.SpreadsheetDocument";
102 : }
103 0 : else if( xServiceInfo->supportsService( "com.sun.star.text.TextDocument" ) )
104 : {
105 0 : maModuleId = "com.sun.star.text.TextDocument";
106 : }
107 :
108 0 : if( maModuleId.isEmpty() )
109 : {
110 0 : throw uno::RuntimeException( "Not implemented" , uno::Reference< uno::XInterface >() );
111 : }
112 :
113 : css::uno::Reference< css::ui::XModuleUIConfigurationManagerSupplier > xUICfgMgrSupp(
114 0 : css::ui::theModuleUIConfigurationManagerSupplier::get(mxContext) );
115 :
116 0 : m_xAppCfgMgr.set( xUICfgMgrSupp->getUIConfigurationManager( maModuleId ), uno::UNO_QUERY_THROW );
117 :
118 0 : css::uno::Reference< css::container::XNameAccess > xNameAccess = css::ui::theWindowStateConfiguration::get( mxContext );
119 :
120 0 : m_xWindowState.set( xNameAccess->getByName( maModuleId ), uno::UNO_QUERY_THROW );
121 0 : }
122 :
123 0 : css::uno::Reference< css::container::XIndexAccess > VbaCommandBarHelper::getSettings( const OUString& sResourceUrl ) throw (css::uno::RuntimeException)
124 : {
125 0 : if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
126 0 : return m_xDocCfgMgr->getSettings( sResourceUrl, sal_True );
127 0 : else if( m_xAppCfgMgr->hasSettings( sResourceUrl ) )
128 0 : return m_xAppCfgMgr->getSettings( sResourceUrl, sal_True );
129 : else
130 : {
131 0 : css::uno::Reference< css::container::XIndexAccess > xSettings( m_xAppCfgMgr->createSettings( ), uno::UNO_QUERY_THROW );
132 0 : return xSettings;
133 : }
134 : }
135 :
136 0 : void VbaCommandBarHelper::removeSettings( const OUString& sResourceUrl ) throw (css::uno::RuntimeException)
137 : {
138 0 : if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
139 0 : m_xDocCfgMgr->removeSettings( sResourceUrl );
140 0 : else if( m_xAppCfgMgr->hasSettings( sResourceUrl ) )
141 0 : m_xAppCfgMgr->removeSettings( sResourceUrl );
142 :
143 : // persistChanges();
144 0 : }
145 :
146 0 : void VbaCommandBarHelper::ApplyChange( const OUString& sResourceUrl, const css::uno::Reference< css::container::XIndexAccess >& xSettings, sal_Bool bTemporary ) throw (css::uno::RuntimeException)
147 : {
148 0 : if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
149 : {
150 0 : m_xDocCfgMgr->replaceSettings( sResourceUrl, xSettings );
151 : }
152 : else
153 : {
154 0 : m_xDocCfgMgr->insertSettings( sResourceUrl, xSettings );
155 : }
156 0 : if( !bTemporary )
157 : {
158 0 : persistChanges();
159 : }
160 0 : }
161 :
162 0 : sal_Bool VbaCommandBarHelper::persistChanges() throw (css::uno::RuntimeException)
163 : {
164 0 : uno::Reference< css::ui::XUIConfigurationPersistence > xConfigPersistence( m_xDocCfgMgr, uno::UNO_QUERY_THROW );
165 0 : sal_Bool result = sal_False;
166 0 : if( xConfigPersistence->isModified() )
167 : {
168 0 : xConfigPersistence->store();
169 0 : result = sal_True;
170 : }
171 0 : return result;
172 : }
173 :
174 0 : uno::Reference< frame::XLayoutManager > VbaCommandBarHelper::getLayoutManager() throw (uno::RuntimeException)
175 : {
176 0 : uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
177 0 : uno::Reference< beans::XPropertySet > xPropertySet( xFrame, uno::UNO_QUERY_THROW );
178 0 : uno::Reference< frame::XLayoutManager > xLayoutManager( xPropertySet->getPropertyValue( "LayoutManager" ), uno::UNO_QUERY_THROW );
179 0 : return xLayoutManager;
180 : }
181 :
182 0 : sal_Bool VbaCommandBarHelper::hasToolbar( const OUString& sResourceUrl, const OUString& sName ) throw (css::uno::RuntimeException)
183 : {
184 0 : if( m_xDocCfgMgr->hasSettings( sResourceUrl ) )
185 : {
186 0 : OUString sUIName;
187 0 : uno::Reference< beans::XPropertySet > xPropertySet( m_xDocCfgMgr->getSettings( sResourceUrl, sal_False ), uno::UNO_QUERY_THROW );
188 0 : xPropertySet->getPropertyValue( ITEM_DESCRIPTOR_UINAME ) >>= sUIName;
189 0 : if( sName.equalsIgnoreAsciiCase( sUIName ) )
190 0 : return sal_True;
191 : }
192 0 : return sal_False;
193 : }
194 :
195 : // return the resource url if found
196 0 : OUString VbaCommandBarHelper::findToolbarByName( const css::uno::Reference< css::container::XNameAccess >& xNameAccess, const OUString& sName ) throw (css::uno::RuntimeException)
197 : {
198 0 : OUString sResourceUrl;
199 :
200 : // check if it is an buildin toolbar
201 0 : sResourceUrl = MSO2OOCommandbarHelper::getMSO2OOCommandbarHelper()->findBuildinToolbar( sName );
202 0 : if( !sResourceUrl.isEmpty() )
203 0 : return sResourceUrl;
204 :
205 0 : uno::Sequence< OUString > allNames = xNameAccess->getElementNames();
206 0 : for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
207 : {
208 0 : sResourceUrl = allNames[i];
209 0 : if(sResourceUrl.startsWith( ITEM_TOOLBAR_URL ) )
210 : {
211 0 : if( hasToolbar( sResourceUrl, sName ) )
212 0 : return sResourceUrl;
213 : }
214 : }
215 :
216 : // the customize toolbars creating during importing, shoud found there.
217 0 : static OUString sToolbarPrefix( "private:resource/toolbar/custom_" );
218 0 : sResourceUrl = sToolbarPrefix.concat( sName );
219 0 : if( hasToolbar( sResourceUrl, sName ) )
220 0 : return sResourceUrl;
221 :
222 0 : return OUString();
223 : }
224 :
225 : // if found, return the position of the control. if not found, return -1
226 0 : sal_Int32 VbaCommandBarHelper::findControlByName( const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess, const OUString& sName, bool bMenu ) throw (css::uno::RuntimeException)
227 : {
228 0 : sal_Int32 nCount = xIndexAccess->getCount();
229 0 : css::uno::Sequence< css::beans::PropertyValue > aProps;
230 0 : for( sal_Int32 i = 0; i < nCount; i++ )
231 : {
232 0 : OUString sLabel;
233 0 : xIndexAccess->getByIndex( i ) >>= aProps;
234 0 : getPropertyValue( aProps, ITEM_DESCRIPTOR_LABEL ) >>= sLabel;
235 : // handle the hotkey marker '~' (remove in toolbars (?), replace by '&' in menus)
236 0 : OUStringBuffer aBuffer;
237 0 : sal_Int32 index = sLabel.indexOf( '~' );
238 0 : if( index < 0 )
239 : {
240 0 : aBuffer = sLabel;
241 : }
242 : else
243 : {
244 0 : aBuffer.append( sLabel.copy( 0, index ) );
245 0 : if( bMenu )
246 0 : aBuffer.append( '&' );
247 0 : aBuffer.append( sLabel.copy( index + 1 ) );
248 : }
249 0 : OUString sNewLabel = aBuffer.makeStringAndClear();
250 : SAL_INFO("vbahelper", "VbaCommandBarHelper::findControlByName, control name: " << sNewLabel);
251 0 : if( sName.equalsIgnoreAsciiCase( sNewLabel ) )
252 0 : return i;
253 0 : }
254 :
255 : // not found
256 0 : return -1;
257 : }
258 :
259 0 : OUString VbaCommandBarHelper::generateCustomURL()
260 : {
261 0 : OUString url( ITEM_TOOLBAR_URL );
262 0 : url += CUSTOM_TOOLBAR_STR;
263 :
264 : // use a random number to minimize possible clash with existing custom toolbars
265 0 : srand( unsigned( time( NULL ) ));
266 0 : url += OUString::number( rand(), 16 );
267 0 : return url;
268 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|