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/container/XEnumerationAccess.hpp>
21 #include <com/sun/star/drawing/XControlShape.hpp>
22 #include <com/sun/star/container/XNamed.hpp>
23 #include <ooo/vba/excel/XOLEObject.hpp>
25 #include "vbaoleobject.hxx"
26 #include "vbaoleobjects.hxx"
28 using namespace com::sun::star
;
29 using namespace ooo::vba
;
31 typedef ::cppu::WeakImplHelper1
< container::XIndexAccess
> XIndexAccess_BASE
;
33 class IndexAccessWrapper
: public XIndexAccess_BASE
35 typedef std::vector
< uno::Reference
< drawing::XControlShape
> > OLEObjects
;
38 IndexAccessWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
)
40 sal_Int32 nLen
= xIndexAccess
->getCount();
41 for ( sal_Int32 index
= 0; index
< nLen
; ++index
)
43 uno::Reference
< drawing::XControlShape
> xControlShape( xIndexAccess
->getByIndex( index
), uno::UNO_QUERY
);
44 if ( xControlShape
.is() )
45 vObjects
.push_back( xControlShape
);
49 virtual ::sal_Int32 SAL_CALL
getCount() throw (uno::RuntimeException
)
51 return vObjects
.size();
54 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw (lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
56 if ( Index
< 0 || Index
>= getCount() )
57 throw lang::IndexOutOfBoundsException();
58 return uno::makeAny( vObjects
[ Index
] );
61 // Methods XElementAcess
62 virtual uno::Type SAL_CALL
getElementType() throw (uno::RuntimeException
)
64 return drawing::XControlShape::static_type(0);
67 virtual ::sal_Bool SAL_CALL
hasElements() throw (uno::RuntimeException
)
69 return ( getCount() > 0 );
74 class EnumWrapper
: public EnumerationHelper_BASE
77 uno::Reference
<XHelperInterface
> m_xParent
;
78 uno::Reference
<uno::XComponentContext
> m_xContext
;
79 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
82 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 ) {}
84 virtual ::sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
)
86 return ( nIndex
< m_xIndexAccess
->getCount() );
89 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
91 if ( nIndex
< m_xIndexAccess
->getCount() )
93 uno::Reference
< drawing::XControlShape
> xControlShape ( m_xIndexAccess
->getByIndex( nIndex
++ ), uno::UNO_QUERY_THROW
);
94 return uno::makeAny( uno::Reference
< ov::excel::XOLEObject
>( new ScVbaOLEObject( m_xParent
, m_xContext
, xControlShape
) ) );
96 throw container::NoSuchElementException();
100 uno::Reference
< container::XIndexAccess
> oleObjectIndexWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
)
102 return new IndexAccessWrapper( xIndexAccess
);
105 ScVbaOLEObjects::ScVbaOLEObjects( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
,
106 const css::uno::Reference
< css::container::XIndexAccess
>& xIndexAccess
)
107 : OLEObjectsImpl_BASE( xParent
, xContext
, oleObjectIndexWrapper( xIndexAccess
) )
110 uno::Reference
< container::XEnumeration
>
111 ScVbaOLEObjects::createEnumeration() throw (uno::RuntimeException
)
113 return new EnumWrapper( getParent(), mxContext
, m_xIndexAccess
);
117 ScVbaOLEObjects::createCollectionObject( const css::uno::Any
& aSource
)
119 if( aSource
.hasValue() )
121 uno::Reference
< drawing::XControlShape
> xControlShape( aSource
, uno::UNO_QUERY_THROW
);
122 // parent of OLEObject is the same parent as the collection ( e.g. the sheet )
123 return uno::makeAny( uno::Reference
< ov::excel::XOLEObject
>( new ScVbaOLEObject( getParent(), mxContext
, xControlShape
) ) );
129 ScVbaOLEObjects::getItemByStringIndex( const OUString
& sIndex
) throw (uno::RuntimeException
)
133 return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex
);
135 catch (const uno::RuntimeException
&)
137 uno::Reference
< container::XIndexAccess
> xIndexAccess( m_xIndexAccess
, uno::UNO_QUERY_THROW
);
138 sal_Int32 nCount
= xIndexAccess
->getCount();
139 for( int index
= 0; index
< nCount
; index
++ )
141 uno::Any aUnoObj
= xIndexAccess
->getByIndex( index
);
142 uno::Reference
< drawing::XControlShape
> xControlShape( aUnoObj
, uno::UNO_QUERY_THROW
);
143 uno::Reference
< awt::XControlModel
> xControlModel( xControlShape
->getControl() );
144 uno::Reference
< container::XNamed
> xNamed( xControlModel
, uno::UNO_QUERY_THROW
);
145 if( sIndex
.equals( xNamed
->getName() ))
147 return createCollectionObject( aUnoObj
);
156 ScVbaOLEObjects::getElementType() throw (uno::RuntimeException
)
158 return ooo::vba::excel::XOLEObject::static_type(0);
162 ScVbaOLEObjects::getServiceImplName()
164 return OUString("ScVbaOLEObjects");
167 uno::Sequence
< OUString
>
168 ScVbaOLEObjects::getServiceNames()
170 static uno::Sequence
< OUString
> aServiceNames
;
171 if ( aServiceNames
.getLength() == 0 )
173 aServiceNames
.realloc( 1 );
174 aServiceNames
[ 0 ] = OUString("ooo.vba.excel.OLEObjects" );
176 return aServiceNames
;
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */