1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 using namespace com::sun::star
;
30 using namespace ooo::vba
;
32 typedef ::cppu::WeakImplHelper
< container::XIndexAccess
> XIndexAccess_BASE
;
36 class IndexAccessWrapper
: public XIndexAccess_BASE
38 typedef std::vector
< uno::Reference
< drawing::XControlShape
> > OLEObjects
;
41 explicit IndexAccessWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
)
43 sal_Int32 nLen
= xIndexAccess
->getCount();
44 for ( sal_Int32 index
= 0; index
< nLen
; ++index
)
46 uno::Reference
< drawing::XControlShape
> xControlShape( xIndexAccess
->getByIndex( index
), uno::UNO_QUERY
);
47 if ( xControlShape
.is() )
48 vObjects
.push_back( xControlShape
);
52 virtual ::sal_Int32 SAL_CALL
getCount() override
54 return vObjects
.size();
57 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) override
59 if ( Index
< 0 || Index
>= getCount() )
60 throw lang::IndexOutOfBoundsException();
61 return uno::Any( vObjects
[ Index
] );
64 // Methods XElementAccess
65 virtual uno::Type SAL_CALL
getElementType() override
67 return cppu::UnoType
<drawing::XControlShape
>::get();
70 virtual sal_Bool SAL_CALL
hasElements() override
72 return ( getCount() > 0 );
77 class EnumWrapper
: public EnumerationHelper_BASE
80 uno::Reference
<XHelperInterface
> m_xParent
;
81 uno::Reference
<uno::XComponentContext
> m_xContext
;
82 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
85 EnumWrapper( uno::Reference
< XHelperInterface
> xParent
,
86 uno::Reference
< uno::XComponentContext
> xContext
,
87 uno::Reference
< container::XIndexAccess
> xIndexAccess
)
88 : m_xParent(std::move( xParent
)), m_xContext(std::move( xContext
)), m_xIndexAccess(std::move( xIndexAccess
)), nIndex( 0 ) {}
90 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
92 return ( nIndex
< m_xIndexAccess
->getCount() );
95 virtual uno::Any SAL_CALL
nextElement( ) override
97 if ( nIndex
< m_xIndexAccess
->getCount() )
99 uno::Reference
< drawing::XControlShape
> xControlShape ( m_xIndexAccess
->getByIndex( nIndex
++ ), uno::UNO_QUERY_THROW
);
100 return uno::Any( uno::Reference
< ov::excel::XOLEObject
>( new ScVbaOLEObject( m_xParent
, m_xContext
, xControlShape
) ) );
102 throw container::NoSuchElementException();
106 uno::Reference
< container::XIndexAccess
> oleObjectIndexWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
)
108 return new IndexAccessWrapper( xIndexAccess
);
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
) )
118 uno::Reference
< container::XEnumeration
>
119 ScVbaOLEObjects::createEnumeration()
121 return new EnumWrapper( getParent(), mxContext
, m_xIndexAccess
);
125 ScVbaOLEObjects::createCollectionObject( const css::uno::Any
& aSource
)
127 if( aSource
.hasValue() )
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::Any( uno::Reference
< ov::excel::XOLEObject
>( new ScVbaOLEObject( getParent(), mxContext
, xControlShape
) ) );
137 ScVbaOLEObjects::getItemByStringIndex( const OUString
& sIndex
)
141 return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex
);
143 catch (const uno::RuntimeException
&)
145 uno::Reference
< container::XIndexAccess
> xIndexAccess( m_xIndexAccess
, uno::UNO_SET_THROW
);
146 sal_Int32 nCount
= xIndexAccess
->getCount();
147 for( int index
= 0; index
< nCount
; index
++ )
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
== xNamed
->getName() )
155 return createCollectionObject( aUnoObj
);
164 ScVbaOLEObjects::getElementType()
166 return cppu::UnoType
<ooo::vba::excel::XOLEObject
>::get();
170 ScVbaOLEObjects::getServiceImplName()
172 return u
"ScVbaOLEObjects"_ustr
;
175 uno::Sequence
< OUString
>
176 ScVbaOLEObjects::getServiceNames()
178 static uno::Sequence
< OUString
> const aServiceNames
180 u
"ooo.vba.excel.OLEObjects"_ustr
182 return aServiceNames
;
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */