merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / vba / vbaoleobjects.cxx
blobd22149eb4fb0e8ec5b09ee189b420bd669a48ea6
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: vbaoleobjects.cxx,v $
10 * $Revision: 1.3 $
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 #include <com/sun/star/container/XEnumerationAccess.hpp>
32 #include <com/sun/star/drawing/XControlShape.hpp>
33 #include <com/sun/star/container/XNamed.hpp>
34 #include <ooo/vba/excel/XOLEObject.hpp>
36 #include "vbaoleobject.hxx"
37 #include "vbaoleobjects.hxx"
39 using namespace com::sun::star;
40 using namespace ooo::vba;
42 typedef ::cppu::WeakImplHelper1< container::XIndexAccess > XIndexAccess_BASE;
44 class IndexAccessWrapper : public XIndexAccess_BASE
46 typedef std::vector< uno::Reference< drawing::XControlShape > > OLEObjects;
47 OLEObjects vObjects;
48 public:
49 IndexAccessWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
51 sal_Int32 nLen = xIndexAccess->getCount();
52 for ( sal_Int32 index = 0; index < nLen; ++index )
54 uno::Reference< drawing::XControlShape > xControlShape( xIndexAccess->getByIndex( index), uno::UNO_QUERY);
55 if ( xControlShape.is() )
56 vObjects.push_back( xControlShape );
60 virtual ::sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException)
62 return vObjects.size();
65 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
67 if ( Index < 0 || Index >= getCount() )
68 throw lang::IndexOutOfBoundsException();
69 return uno::makeAny( vObjects[ Index ] );
72 // Methods XElementAcess
73 virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException)
75 return drawing::XControlShape::static_type(0);
78 virtual ::sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException)
80 return ( getCount() > 0 );
85 class EnumWrapper : public EnumerationHelper_BASE
88 uno::Reference<XHelperInterface > m_xParent;
89 uno::Reference<uno::XComponentContext > m_xContext;
90 uno::Reference<container::XIndexAccess > m_xIndexAccess;
91 sal_Int32 nIndex;
92 public:
93 EnumWrapper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< container::XIndexAccess >& xIndexAccess ) : m_xParent( xParent ), m_xContext( xContext), m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
95 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
97 return ( nIndex < m_xIndexAccess->getCount() );
100 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
102 if ( nIndex < m_xIndexAccess->getCount() )
104 uno::Reference< drawing::XControlShape > xControlShape ( m_xIndexAccess->getByIndex( nIndex++ ), uno::UNO_QUERY_THROW );
105 return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( m_xParent, m_xContext, xControlShape ) ) );
107 throw container::NoSuchElementException();
111 uno::Reference< container::XIndexAccess > oleObjectIndexWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
113 return new IndexAccessWrapper( xIndexAccess );
116 ScVbaOLEObjects::ScVbaOLEObjects( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext,
117 const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess )
118 : OLEObjectsImpl_BASE( xParent, xContext, oleObjectIndexWrapper( xIndexAccess ) )
121 uno::Reference< container::XEnumeration >
122 ScVbaOLEObjects::createEnumeration() throw (uno::RuntimeException)
124 return new EnumWrapper( getParent(), mxContext, m_xIndexAccess );
127 uno::Any
128 ScVbaOLEObjects::createCollectionObject( const css::uno::Any& aSource )
130 if( aSource.hasValue() )
132 uno::Reference< drawing::XControlShape > xControlShape( aSource, uno::UNO_QUERY_THROW );
133 // parent of OLEObject is the same parent as the collection ( e.g. the sheet )
134 return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( getParent(), mxContext, xControlShape ) ) );
136 return uno::Any();
139 uno::Any
140 ScVbaOLEObjects::getItemByStringIndex( const rtl::OUString& sIndex ) throw (uno::RuntimeException)
144 return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex );
146 catch( uno::RuntimeException )
148 uno::Reference< container::XIndexAccess > xIndexAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
149 sal_Int32 nCount = xIndexAccess->getCount();
150 for( int index = 0; index < nCount; index++ )
152 uno::Any aUnoObj = xIndexAccess->getByIndex( index );
153 uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY_THROW );
154 uno::Reference< awt::XControlModel > xControlModel( xControlShape->getControl() );
155 uno::Reference< container::XNamed > xNamed( xControlModel, uno::UNO_QUERY_THROW );
156 if( sIndex.equals( xNamed->getName() ))
158 return createCollectionObject( aUnoObj );
162 return uno::Any();
166 uno::Type
167 ScVbaOLEObjects::getElementType() throw (uno::RuntimeException)
169 return ooo::vba::excel::XOLEObject::static_type(0);
171 rtl::OUString&
172 ScVbaOLEObjects::getServiceImplName()
174 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaOLEObjects") );
175 return sImplName;
178 uno::Sequence< rtl::OUString >
179 ScVbaOLEObjects::getServiceNames()
181 static uno::Sequence< rtl::OUString > aServiceNames;
182 if ( aServiceNames.getLength() == 0 )
184 aServiceNames.realloc( 1 );
185 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.OLEObjects" ) );
187 return aServiceNames;