merge the formfield patch from ooo-build
[ooovba.git] / vbahelper / source / vbahelper / vbacommandbars.cxx
blobe87e4a32f5ce00a9c7f12a1fb84613dcd40e9870
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile$
7 * $Revision$
9 * last change: $Author$ $Date$
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
34 ************************************************************************/
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/frame/XDesktop.hpp>
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
39 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
40 #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
41 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
43 #include "vbacommandbars.hxx"
44 #include "vbacommandbar.hxx"
46 using namespace com::sun::star;
47 using namespace ooo::vba;
50 typedef ::cppu::WeakImplHelper1< container::XEnumeration > CommandBarEnumeration_BASE;
52 class CommandBarEnumeration : public CommandBarEnumeration_BASE
54 uno::Reference< XHelperInterface > m_xParent;
55 uno::Reference< uno::XComponentContext > m_xContext;
56 VbaCommandBarHelperRef pCBarHelper;
57 uno::Sequence< rtl::OUString > m_sNames;
58 sal_Int32 m_nCurrentPosition;
59 public:
60 CommandBarEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, VbaCommandBarHelperRef pHelper) throw ( uno::RuntimeException ) : m_xParent( xParent ), m_xContext( xContext ), pCBarHelper( pHelper ) , m_nCurrentPosition( 0 )
62 uno::Reference< container::XNameAccess > xNameAccess = pCBarHelper->getPersistentWindowState();
63 m_sNames = xNameAccess->getElementNames();
65 virtual sal_Bool SAL_CALL hasMoreElements() throw ( uno::RuntimeException )
67 if( m_nCurrentPosition < m_sNames.getLength() )
68 return sal_True;
69 return sal_False;
71 virtual uno::Any SAL_CALL nextElement() throw ( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
73 // FIXME: should be add menubar
74 if( hasMoreElements() )
76 rtl::OUString sResourceUrl( m_sNames[ m_nCurrentPosition++ ] );
77 if( sResourceUrl.indexOf( rtl::OUString::createFromAscii("private:resource/toolbar/") ) != -1 )
79 uno::Reference< container::XIndexAccess > xCBarSetting = pCBarHelper->getSettings( sResourceUrl );
80 uno::Reference< XCommandBar > xCommandBar( new ScVbaCommandBar( m_xParent, m_xContext, pCBarHelper, xCBarSetting, sResourceUrl, sal_False, sal_False ) );
82 else
83 return nextElement();
85 else
86 throw container::NoSuchElementException();
87 return uno::Any();
91 ScVbaCommandBars::ScVbaCommandBars( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : CommandBars_BASE( xParent, xContext, xIndexAccess )
93 pCBarHelper.reset( new VbaCommandBarHelper( mxContext, xModel ) );
94 m_xNameAccess = pCBarHelper->getPersistentWindowState();
97 ScVbaCommandBars::~ScVbaCommandBars()
101 // XEnumerationAccess
102 uno::Type SAL_CALL
103 ScVbaCommandBars::getElementType() throw ( uno::RuntimeException )
105 return XCommandBar::static_type( 0 );
108 uno::Reference< container::XEnumeration >
109 ScVbaCommandBars::createEnumeration() throw ( uno::RuntimeException )
111 return uno::Reference< container::XEnumeration >( new CommandBarEnumeration( this, mxContext, pCBarHelper ) );
114 uno::Any
115 ScVbaCommandBars::createCollectionObject( const uno::Any& aSource )
117 // aSource should be a name at this time, because of the class is API wrapper.
118 rtl::OUString sResourceUrl;
119 uno::Reference< container::XIndexAccess > xBarSettings;
120 rtl::OUString sBarName;
121 sal_Bool bMenu = sal_False;
122 if( aSource >>= sBarName )
124 if( sBarName.equalsIgnoreAsciiCase( rtl::OUString::createFromAscii("Worksheet Menu Bar") )
125 || sBarName.equalsIgnoreAsciiCase( rtl::OUString::createFromAscii("Menu Bar") ) )
127 // menu bar
128 sResourceUrl = rtl::OUString::createFromAscii( ITEM_MENUBAR_URL );
129 bMenu = sal_True;
131 else
133 sResourceUrl = pCBarHelper->findToolbarByName( m_xNameAccess, sBarName );
134 bMenu = sal_False;
138 if( sResourceUrl.getLength() )
139 xBarSettings = pCBarHelper->getSettings( sResourceUrl );
140 else
141 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Toolbar do not exist") ), uno::Reference< uno::XInterface >() );
143 return uno::Any( uno::Reference< XCommandBar >( new ScVbaCommandBar( this, mxContext, pCBarHelper, xBarSettings, sResourceUrl, bMenu, sal_False ) ) );
146 // XCommandBars
147 uno::Reference< XCommandBar > SAL_CALL
148 ScVbaCommandBars::Add( const css::uno::Any& Name, const css::uno::Any& /*Position*/, const css::uno::Any& /*MenuBar*/, const css::uno::Any& Temporary ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
150 // FIXME: only support to add Toolbar
151 // Position - MsoBar MenuBar - sal_Bool
152 // Currently only the Name is supported.
153 rtl::OUString sName;
154 if( Name.hasValue() )
155 Name >>= sName;
157 rtl::OUString sResourceUrl;
158 if( sName.getLength() )
160 sResourceUrl = pCBarHelper->findToolbarByName( m_xNameAccess, sName );
161 if( sResourceUrl.getLength() )
162 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Toolbar exists") ), uno::Reference< uno::XInterface >() );
164 else
166 sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Custom1") );
169 sal_Bool bTemporary = sal_False;
170 if( Temporary.hasValue() )
171 Temporary >>= bTemporary;
173 sResourceUrl = VbaCommandBarHelper::generateCustomURL();
174 uno::Reference< container::XIndexAccess > xBarSettings( pCBarHelper->getSettings( sResourceUrl ), uno::UNO_QUERY_THROW );
175 uno::Reference< XCommandBar > xCBar( new ScVbaCommandBar( this, mxContext, pCBarHelper, xBarSettings, sResourceUrl, sal_False, bTemporary ) );
176 xCBar->setName( sName );
177 return xCBar;
179 sal_Int32 SAL_CALL
180 ScVbaCommandBars::getCount() throw(css::uno::RuntimeException)
182 // Filter out all toolbars from the window collection
183 sal_Int32 nCount = 1; // there is a Menubar in OOo
184 uno::Sequence< ::rtl::OUString > allNames = m_xNameAccess->getElementNames();
185 for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
187 if(allNames[i].indexOf( rtl::OUString::createFromAscii("private:resource/toolbar/") ) != -1 )
189 nCount++;
192 return nCount;
195 // ScVbaCollectionBaseImpl
196 uno::Any SAL_CALL
197 ScVbaCommandBars::Item( const uno::Any& aIndex, const uno::Any& /*aIndex2*/ ) throw( uno::RuntimeException )
199 if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
201 return createCollectionObject( aIndex );
204 // hardcode if "aIndex = 1" that would return "main menu".
205 sal_Int16 nIndex = 0;
206 aIndex >>= nIndex;
207 if( nIndex == 1 )
209 uno::Any aSource;
210 if( pCBarHelper->getModuleId().equalsAscii( "com.sun.star.sheet.SpreadsheetDocument" ) )
211 aSource <<= rtl::OUString::createFromAscii( "Worksheet Menu Bar" );
212 else if( pCBarHelper->getModuleId().equalsAscii("com.sun.star.text.TextDocument") )
213 aSource <<= rtl::OUString::createFromAscii( "Menu Bar" );
214 if( aSource.hasValue() )
215 return createCollectionObject( aSource );
217 return uno::Any();
220 // XHelperInterface
221 rtl::OUString&
222 ScVbaCommandBars::getServiceImplName()
224 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBars") );
225 return sImplName;
227 uno::Sequence<rtl::OUString>
228 ScVbaCommandBars::getServiceNames()
230 static uno::Sequence< rtl::OUString > aServiceNames;
231 if ( aServiceNames.getLength() == 0 )
233 aServiceNames.realloc( 1 );
234 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBars" ) );
236 return aServiceNames;