Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / ui / vba / vbaoleobjects.cxx
blobf0ddf68c82ac0ebaaa16486a1201e01ac6117db9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/drawing/XControlShape.hpp>
21 #include <com/sun/star/container/XNamed.hpp>
22 #include <ooo/vba/excel/XOLEObject.hpp>
24 #include "vbaoleobject.hxx"
25 #include "vbaoleobjects.hxx"
26 #include <cppuhelper/implbase.hxx>
28 using namespace com::sun::star;
29 using namespace ooo::vba;
31 typedef ::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE;
33 namespace {
35 class IndexAccessWrapper : public XIndexAccess_BASE
37 typedef std::vector< uno::Reference< drawing::XControlShape > > OLEObjects;
38 OLEObjects vObjects;
39 public:
40 explicit IndexAccessWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
42 sal_Int32 nLen = xIndexAccess->getCount();
43 for ( sal_Int32 index = 0; index < nLen; ++index )
45 uno::Reference< drawing::XControlShape > xControlShape( xIndexAccess->getByIndex( index), uno::UNO_QUERY);
46 if ( xControlShape.is() )
47 vObjects.push_back( xControlShape );
51 virtual ::sal_Int32 SAL_CALL getCount() override
53 return vObjects.size();
56 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
58 if ( Index < 0 || Index >= getCount() )
59 throw lang::IndexOutOfBoundsException();
60 return uno::makeAny( vObjects[ Index ] );
63 // Methods XElementAccess
64 virtual uno::Type SAL_CALL getElementType() override
66 return cppu::UnoType<drawing::XControlShape>::get();
69 virtual sal_Bool SAL_CALL hasElements() override
71 return ( getCount() > 0 );
76 class EnumWrapper : public EnumerationHelper_BASE
79 uno::Reference<XHelperInterface > m_xParent;
80 uno::Reference<uno::XComponentContext > m_xContext;
81 uno::Reference<container::XIndexAccess > m_xIndexAccess;
82 sal_Int32 nIndex;
83 public:
84 EnumWrapper( const uno::Reference< XHelperInterface >& xParent,
85 const uno::Reference< uno::XComponentContext >& xContext,
86 const uno::Reference< container::XIndexAccess >& xIndexAccess )
87 : m_xParent( xParent ), m_xContext( xContext), m_xIndexAccess( xIndexAccess ), nIndex( 0 ) {}
89 virtual sal_Bool SAL_CALL hasMoreElements( ) override
91 return ( nIndex < m_xIndexAccess->getCount() );
94 virtual uno::Any SAL_CALL nextElement( ) override
96 if ( nIndex < m_xIndexAccess->getCount() )
98 uno::Reference< drawing::XControlShape > xControlShape ( m_xIndexAccess->getByIndex( nIndex++ ), uno::UNO_QUERY_THROW );
99 return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( m_xParent, m_xContext, xControlShape ) ) );
101 throw container::NoSuchElementException();
105 uno::Reference< container::XIndexAccess > oleObjectIndexWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
107 return new IndexAccessWrapper( xIndexAccess );
112 ScVbaOLEObjects::ScVbaOLEObjects( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext,
113 const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess )
114 : OLEObjectsImpl_BASE( xParent, xContext, oleObjectIndexWrapper( xIndexAccess ) )
117 uno::Reference< container::XEnumeration >
118 ScVbaOLEObjects::createEnumeration()
120 return new EnumWrapper( getParent(), mxContext, m_xIndexAccess );
123 uno::Any
124 ScVbaOLEObjects::createCollectionObject( const css::uno::Any& aSource )
126 if( aSource.hasValue() )
128 uno::Reference< drawing::XControlShape > xControlShape( aSource, uno::UNO_QUERY_THROW );
129 // parent of OLEObject is the same parent as the collection ( e.g. the sheet )
130 return uno::makeAny( uno::Reference< ov::excel::XOLEObject >( new ScVbaOLEObject( getParent(), mxContext, xControlShape ) ) );
132 return uno::Any();
135 uno::Any
136 ScVbaOLEObjects::getItemByStringIndex( const OUString& sIndex )
140 return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex );
142 catch (const uno::RuntimeException&)
144 uno::Reference< container::XIndexAccess > xIndexAccess( m_xIndexAccess, uno::UNO_SET_THROW );
145 sal_Int32 nCount = xIndexAccess->getCount();
146 for( int index = 0; index < nCount; index++ )
148 uno::Any aUnoObj = xIndexAccess->getByIndex( index );
149 uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY_THROW );
150 uno::Reference< awt::XControlModel > xControlModel( xControlShape->getControl() );
151 uno::Reference< container::XNamed > xNamed( xControlModel, uno::UNO_QUERY_THROW );
152 if( sIndex == xNamed->getName() )
154 return createCollectionObject( aUnoObj );
158 return uno::Any();
162 uno::Type
163 ScVbaOLEObjects::getElementType()
165 return cppu::UnoType<ooo::vba::excel::XOLEObject>::get();
168 OUString
169 ScVbaOLEObjects::getServiceImplName()
171 return "ScVbaOLEObjects";
174 uno::Sequence< OUString >
175 ScVbaOLEObjects::getServiceNames()
177 static uno::Sequence< OUString > const aServiceNames
179 "ooo.vba.excel.OLEObjects"
181 return aServiceNames;
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */