xref: /aoo41x/main/sc/source/ui/vba/vbaoleobjects.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <com/sun/star/container/XEnumerationAccess.hpp>
29 #include <com/sun/star/drawing/XControlShape.hpp>
30 #include <com/sun/star/container/XNamed.hpp>
31 #include <ooo/vba/excel/XOLEObject.hpp>
32 
33 #include "vbaoleobject.hxx"
34 #include "vbaoleobjects.hxx"
35 
36 using namespace com::sun::star;
37 using namespace ooo::vba;
38 
39 typedef ::cppu::WeakImplHelper1< container::XIndexAccess > XIndexAccess_BASE;
40 
41 class IndexAccessWrapper : public XIndexAccess_BASE
42 {
43 typedef std::vector< uno::Reference< drawing::XControlShape > > OLEObjects;
44 	OLEObjects vObjects;
45 public:
46         IndexAccessWrapper(  const uno::Reference< container::XIndexAccess >& xIndexAccess )
47 	{
48 		sal_Int32 nLen = xIndexAccess->getCount();
49 		for ( sal_Int32 index = 0; index < nLen; ++index )
50 		{
51         		uno::Reference< drawing::XControlShape > xControlShape( xIndexAccess->getByIndex( index), uno::UNO_QUERY);
52 			if ( xControlShape.is() )
53 				vObjects.push_back( xControlShape );
54 		}
55 	}
56 
57 	virtual ::sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException)
58 	{
59 		return vObjects.size();
60 	}
61 
62 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
63 	{
64 		if ( Index < 0 || Index >= getCount() )
65 			throw lang::IndexOutOfBoundsException();
66 		return uno::makeAny( vObjects[ Index ] );
67 	}
68 
69 	    // Methods XElementAcess
70         virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException)
71         {
72             return drawing::XControlShape::static_type(0);
73         }
74 
75         virtual ::sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException)
76         {
77             return ( getCount() > 0 );
78         }
79 
80 };
81 
82 class EnumWrapper : public EnumerationHelper_BASE
83 {
84 
85         uno::Reference<XHelperInterface > m_xParent;
86         uno::Reference<uno::XComponentContext > m_xContext;
87         uno::Reference<container::XIndexAccess > m_xIndexAccess;
88         sal_Int32 nIndex;
89 public:
90         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 ) {}
91 
92         virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
93         {
94                 return ( nIndex < m_xIndexAccess->getCount() );
95         }
96 
97         virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
98         {
99                 if ( nIndex < m_xIndexAccess->getCount() )
100 		{
101 			uno::Reference< drawing::XControlShape > xControlShape (  m_xIndexAccess->getByIndex( nIndex++ ), uno::UNO_QUERY_THROW );
102         		return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( m_xParent, m_xContext, xControlShape ) ) );
103 		}
104                 throw container::NoSuchElementException();
105         }
106 };
107 
108 uno::Reference< container::XIndexAccess > oleObjectIndexWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
109 {
110 	return new IndexAccessWrapper( xIndexAccess );
111 }
112 
113 ScVbaOLEObjects::ScVbaOLEObjects( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext,
114                 const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess )
115             : OLEObjectsImpl_BASE( xParent, xContext, oleObjectIndexWrapper( xIndexAccess  ) )
116 {
117 }
118 uno::Reference< container::XEnumeration >
119 ScVbaOLEObjects::createEnumeration() throw (uno::RuntimeException)
120 {
121     return new EnumWrapper( getParent(), mxContext, m_xIndexAccess );
122 }
123 
124 uno::Any
125 ScVbaOLEObjects::createCollectionObject( const css::uno::Any& aSource )
126 {
127     if( aSource.hasValue() )
128     {
129         uno::Reference< drawing::XControlShape > xControlShape( aSource, uno::UNO_QUERY_THROW );
130 	// parent of OLEObject is the same parent as the collection ( e.g. the sheet )
131         return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( getParent(), mxContext, xControlShape ) ) );
132     }
133     return uno::Any();
134 }
135 
136 uno::Any
137 ScVbaOLEObjects::getItemByStringIndex( const rtl::OUString& sIndex ) throw (uno::RuntimeException)
138 {
139     try
140     {
141         return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex );
142     }
143     catch( uno::RuntimeException )
144     {
145         uno::Reference< container::XIndexAccess > xIndexAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
146         sal_Int32 nCount = xIndexAccess->getCount();
147         for( int index = 0; index < nCount; index++ )
148         {
149             uno::Any aUnoObj =  xIndexAccess->getByIndex( index );
150             uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY_THROW );
151             uno::Reference< awt::XControlModel > xControlModel( xControlShape->getControl() );
152             uno::Reference< container::XNamed > xNamed( xControlModel, uno::UNO_QUERY_THROW );
153             if( sIndex.equals( xNamed->getName() ))
154             {
155                 return createCollectionObject( aUnoObj );
156             }
157 
158         }
159         return uno::Any();
160     }
161 }
162 
163 uno::Type
164 ScVbaOLEObjects::getElementType() throw (uno::RuntimeException)
165 {
166     return ooo::vba::excel::XOLEObject::static_type(0);
167 }
168 rtl::OUString&
169 ScVbaOLEObjects::getServiceImplName()
170 {
171 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaOLEObjects") );
172 	return sImplName;
173 }
174 
175 uno::Sequence< rtl::OUString >
176 ScVbaOLEObjects::getServiceNames()
177 {
178 	static uno::Sequence< rtl::OUString > aServiceNames;
179 	if ( aServiceNames.getLength() == 0 )
180 	{
181 		aServiceNames.realloc( 1 );
182 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.OLEObjects" ) );
183 	}
184 	return aServiceNames;
185 }
186