xref: /aoo41x/main/sc/source/ui/vba/vbaoleobjects.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
25cdf0e10cSrcweir #include <com/sun/star/drawing/XControlShape.hpp>
26cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp>
27cdf0e10cSrcweir #include <ooo/vba/excel/XOLEObject.hpp>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "vbaoleobject.hxx"
30cdf0e10cSrcweir #include "vbaoleobjects.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace com::sun::star;
33cdf0e10cSrcweir using namespace ooo::vba;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< container::XIndexAccess > XIndexAccess_BASE;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir class IndexAccessWrapper : public XIndexAccess_BASE
38cdf0e10cSrcweir {
39cdf0e10cSrcweir typedef std::vector< uno::Reference< drawing::XControlShape > > OLEObjects;
40cdf0e10cSrcweir 	OLEObjects vObjects;
41cdf0e10cSrcweir public:
IndexAccessWrapper(const uno::Reference<container::XIndexAccess> & xIndexAccess)42cdf0e10cSrcweir         IndexAccessWrapper(  const uno::Reference< container::XIndexAccess >& xIndexAccess )
43cdf0e10cSrcweir 	{
44cdf0e10cSrcweir 		sal_Int32 nLen = xIndexAccess->getCount();
45cdf0e10cSrcweir 		for ( sal_Int32 index = 0; index < nLen; ++index )
46cdf0e10cSrcweir 		{
47cdf0e10cSrcweir         		uno::Reference< drawing::XControlShape > xControlShape( xIndexAccess->getByIndex( index), uno::UNO_QUERY);
48cdf0e10cSrcweir 			if ( xControlShape.is() )
49cdf0e10cSrcweir 				vObjects.push_back( xControlShape );
50cdf0e10cSrcweir 		}
51cdf0e10cSrcweir 	}
52cdf0e10cSrcweir 
getCount()53cdf0e10cSrcweir 	virtual ::sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException)
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		return vObjects.size();
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir 
getByIndex(::sal_Int32 Index)58cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		if ( Index < 0 || Index >= getCount() )
61cdf0e10cSrcweir 			throw lang::IndexOutOfBoundsException();
62cdf0e10cSrcweir 		return uno::makeAny( vObjects[ Index ] );
63cdf0e10cSrcweir 	}
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	    // Methods XElementAcess
getElementType()66cdf0e10cSrcweir         virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException)
67cdf0e10cSrcweir         {
68cdf0e10cSrcweir             return drawing::XControlShape::static_type(0);
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir 
hasElements()71cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException)
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             return ( getCount() > 0 );
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir };
77cdf0e10cSrcweir 
78cdf0e10cSrcweir class EnumWrapper : public EnumerationHelper_BASE
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         uno::Reference<XHelperInterface > m_xParent;
82cdf0e10cSrcweir         uno::Reference<uno::XComponentContext > m_xContext;
83cdf0e10cSrcweir         uno::Reference<container::XIndexAccess > m_xIndexAccess;
84cdf0e10cSrcweir         sal_Int32 nIndex;
85cdf0e10cSrcweir public:
EnumWrapper(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,uno::Reference<container::XIndexAccess> & xIndexAccess)86cdf0e10cSrcweir         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 ) {}
87cdf0e10cSrcweir 
hasMoreElements()88cdf0e10cSrcweir         virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir                 return ( nIndex < m_xIndexAccess->getCount() );
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir 
nextElement()93cdf0e10cSrcweir         virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir                 if ( nIndex < m_xIndexAccess->getCount() )
96cdf0e10cSrcweir 		{
97cdf0e10cSrcweir 			uno::Reference< drawing::XControlShape > xControlShape (  m_xIndexAccess->getByIndex( nIndex++ ), uno::UNO_QUERY_THROW );
98cdf0e10cSrcweir         		return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( m_xParent, m_xContext, xControlShape ) ) );
99cdf0e10cSrcweir 		}
100cdf0e10cSrcweir                 throw container::NoSuchElementException();
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir };
103cdf0e10cSrcweir 
oleObjectIndexWrapper(const uno::Reference<container::XIndexAccess> & xIndexAccess)104cdf0e10cSrcweir uno::Reference< container::XIndexAccess > oleObjectIndexWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	return new IndexAccessWrapper( xIndexAccess );
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
ScVbaOLEObjects(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const css::uno::Reference<css::container::XIndexAccess> & xIndexAccess)109cdf0e10cSrcweir ScVbaOLEObjects::ScVbaOLEObjects( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext,
110cdf0e10cSrcweir                 const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess )
111cdf0e10cSrcweir             : OLEObjectsImpl_BASE( xParent, xContext, oleObjectIndexWrapper( xIndexAccess  ) )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir }
114cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()115cdf0e10cSrcweir ScVbaOLEObjects::createEnumeration() throw (uno::RuntimeException)
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     return new EnumWrapper( getParent(), mxContext, m_xIndexAccess );
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir uno::Any
createCollectionObject(const css::uno::Any & aSource)121cdf0e10cSrcweir ScVbaOLEObjects::createCollectionObject( const css::uno::Any& aSource )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     if( aSource.hasValue() )
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         uno::Reference< drawing::XControlShape > xControlShape( aSource, uno::UNO_QUERY_THROW );
126cdf0e10cSrcweir 	// parent of OLEObject is the same parent as the collection ( e.g. the sheet )
127cdf0e10cSrcweir         return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( getParent(), mxContext, xControlShape ) ) );
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir     return uno::Any();
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir uno::Any
getItemByStringIndex(const rtl::OUString & sIndex)133cdf0e10cSrcweir ScVbaOLEObjects::getItemByStringIndex( const rtl::OUString& sIndex ) throw (uno::RuntimeException)
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     try
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex );
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir     catch( uno::RuntimeException )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         uno::Reference< container::XIndexAccess > xIndexAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
142cdf0e10cSrcweir         sal_Int32 nCount = xIndexAccess->getCount();
143cdf0e10cSrcweir         for( int index = 0; index < nCount; index++ )
144cdf0e10cSrcweir         {
145cdf0e10cSrcweir             uno::Any aUnoObj =  xIndexAccess->getByIndex( index );
146cdf0e10cSrcweir             uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY_THROW );
147cdf0e10cSrcweir             uno::Reference< awt::XControlModel > xControlModel( xControlShape->getControl() );
148cdf0e10cSrcweir             uno::Reference< container::XNamed > xNamed( xControlModel, uno::UNO_QUERY_THROW );
149cdf0e10cSrcweir             if( sIndex.equals( xNamed->getName() ))
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 return createCollectionObject( aUnoObj );
152cdf0e10cSrcweir             }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir         return uno::Any();
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir uno::Type
getElementType()160cdf0e10cSrcweir ScVbaOLEObjects::getElementType() throw (uno::RuntimeException)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     return ooo::vba::excel::XOLEObject::static_type(0);
163cdf0e10cSrcweir }
164cdf0e10cSrcweir rtl::OUString&
getServiceImplName()165cdf0e10cSrcweir ScVbaOLEObjects::getServiceImplName()
166cdf0e10cSrcweir {
167cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaOLEObjects") );
168cdf0e10cSrcweir 	return sImplName;
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()172cdf0e10cSrcweir ScVbaOLEObjects::getServiceNames()
173cdf0e10cSrcweir {
174cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
175cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
178cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.OLEObjects" ) );
179cdf0e10cSrcweir 	}
180cdf0e10cSrcweir 	return aServiceNames;
181cdf0e10cSrcweir }
182