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
;
35 class IndexAccessWrapper
: public XIndexAccess_BASE
37 typedef std::vector
< uno::Reference
< drawing::XControlShape
> > OLEObjects
;
40 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() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
53 return vObjects
.size();
56 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw (lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
58 if ( Index
< 0 || Index
>= getCount() )
59 throw lang::IndexOutOfBoundsException();
60 return uno::makeAny( vObjects
[ Index
] );
63 // Methods XElementAcess
64 virtual uno::Type SAL_CALL
getElementType() throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
66 return cppu::UnoType
<drawing::XControlShape
>::get();
69 virtual sal_Bool SAL_CALL
hasElements() throw (uno::RuntimeException
, std::exception
) SAL_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
;
84 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 ) {}
86 virtual sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
88 return ( nIndex
< m_xIndexAccess
->getCount() );
91 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
93 if ( nIndex
< m_xIndexAccess
->getCount() )
95 uno::Reference
< drawing::XControlShape
> xControlShape ( m_xIndexAccess
->getByIndex( nIndex
++ ), uno::UNO_QUERY_THROW
);
96 return uno::makeAny( uno::Reference
< ov::excel::XOLEObject
>( new ScVbaOLEObject( m_xParent
, m_xContext
, xControlShape
) ) );
98 throw container::NoSuchElementException();
102 uno::Reference
< container::XIndexAccess
> oleObjectIndexWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
)
104 return new IndexAccessWrapper( 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
) )
114 uno::Reference
< container::XEnumeration
>
115 ScVbaOLEObjects::createEnumeration() throw (uno::RuntimeException
)
117 return new EnumWrapper( getParent(), mxContext
, m_xIndexAccess
);
121 ScVbaOLEObjects::createCollectionObject( const css::uno::Any
& aSource
)
123 if( aSource
.hasValue() )
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
) ) );
133 ScVbaOLEObjects::getItemByStringIndex( const OUString
& sIndex
) throw (uno::RuntimeException
)
137 return OLEObjectsImpl_BASE::getItemByStringIndex( sIndex
);
139 catch (const uno::RuntimeException
&)
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
++ )
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() ))
151 return createCollectionObject( aUnoObj
);
160 ScVbaOLEObjects::getElementType() throw (uno::RuntimeException
)
162 return cppu::UnoType
<ooo::vba::excel::XOLEObject
>::get();
166 ScVbaOLEObjects::getServiceImplName()
168 return OUString("ScVbaOLEObjects");
171 uno::Sequence
< OUString
>
172 ScVbaOLEObjects::getServiceNames()
174 static uno::Sequence
< OUString
> aServiceNames
;
175 if ( aServiceNames
.getLength() == 0 )
177 aServiceNames
.realloc( 1 );
178 aServiceNames
[ 0 ] = "ooo.vba.excel.OLEObjects";
180 return aServiceNames
;
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */