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