LibreOffice Module sc (master) 1
vbaoleobjects.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/drawing/XControlShape.hpp>
21#include <com/sun/star/container/XNamed.hpp>
22#include <ooo/vba/excel/XOLEObject.hpp>
23
24#include "vbaoleobject.hxx"
25#include "vbaoleobjects.hxx"
27#include <utility>
28
29using namespace com::sun::star;
30using namespace ooo::vba;
31
32typedef ::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE;
33
34namespace {
35
36class IndexAccessWrapper : public XIndexAccess_BASE
37{
38typedef std::vector< uno::Reference< drawing::XControlShape > > OLEObjects;
39 OLEObjects vObjects;
40public:
41 explicit IndexAccessWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
42 {
43 sal_Int32 nLen = xIndexAccess->getCount();
44 for ( sal_Int32 index = 0; index < nLen; ++index )
45 {
46 uno::Reference< drawing::XControlShape > xControlShape( xIndexAccess->getByIndex( index), uno::UNO_QUERY);
47 if ( xControlShape.is() )
48 vObjects.push_back( xControlShape );
49 }
50 }
51
52 virtual ::sal_Int32 SAL_CALL getCount() override
53 {
54 return vObjects.size();
55 }
56
57 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
58 {
59 if ( Index < 0 || Index >= getCount() )
60 throw lang::IndexOutOfBoundsException();
61 return uno::Any( vObjects[ Index ] );
62 }
63
64 // Methods XElementAccess
65 virtual uno::Type SAL_CALL getElementType() override
66 {
68 }
69
70 virtual sal_Bool SAL_CALL hasElements() override
71 {
72 return ( getCount() > 0 );
73 }
74
75};
76
77class EnumWrapper : public EnumerationHelper_BASE
78{
79
80 uno::Reference<XHelperInterface > m_xParent;
81 uno::Reference<uno::XComponentContext > m_xContext;
82 uno::Reference<container::XIndexAccess > m_xIndexAccess;
83 sal_Int32 nIndex;
84public:
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 ) {}
89
90 virtual sal_Bool SAL_CALL hasMoreElements( ) override
91 {
92 return ( nIndex < m_xIndexAccess->getCount() );
93 }
94
95 virtual uno::Any SAL_CALL nextElement( ) override
96 {
97 if ( nIndex < m_xIndexAccess->getCount() )
98 {
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 ) ) );
101 }
102 throw container::NoSuchElementException();
103 }
104};
105
106uno::Reference< container::XIndexAccess > oleObjectIndexWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
107{
108 return new IndexAccessWrapper( xIndexAccess );
109}
110
111}
112
113ScVbaOLEObjects::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 ) )
116{
117}
118uno::Reference< container::XEnumeration >
120{
121 return new EnumWrapper( getParent(), mxContext, m_xIndexAccess );
122}
123
125ScVbaOLEObjects::createCollectionObject( const css::uno::Any& aSource )
126{
127 if( aSource.hasValue() )
128 {
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 ) ) );
132 }
133 return uno::Any();
134}
135
138{
139 try
140 {
142 }
143 catch (const uno::RuntimeException&)
144 {
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++ )
148 {
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() )
154 {
155 return createCollectionObject( aUnoObj );
156 }
157
158 }
159 return uno::Any();
160 }
161}
162
165{
167}
168
169OUString
171{
172 return "ScVbaOLEObjects";
173}
174
175uno::Sequence< OUString >
177{
178 static uno::Sequence< OUString > const aServiceNames
179 {
180 "ooo.vba.excel.OLEObjects"
181 };
182 return aServiceNames;
183}
184
185/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
Reference< XComponentContext > m_xContext
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex)
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex) override
virtual OUString getServiceImplName() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
ScVbaOLEObjects(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess)
css::uno::Type const & get()
int nCount
Sequence< OUString > aServiceNames
sal_Int32 nIndex
index
unsigned char sal_Bool
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE
::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE