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