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"
26 #include <cppuhelper/implbase.hxx>
27 
28 using namespace com::sun::star;
29 using namespace ooo::vba;
30 
31 typedef ::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE;
32 
33 namespace {
34 
35 class IndexAccessWrapper : public XIndexAccess_BASE
36 {
37 typedef std::vector< uno::Reference< drawing::XControlShape > > OLEObjects;
38  OLEObjects vObjects;
39 public:
40  explicit IndexAccessWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
41  {
42  sal_Int32 nLen = xIndexAccess->getCount();
43  for ( sal_Int32 index = 0; index < nLen; ++index )
44  {
45  uno::Reference< drawing::XControlShape > xControlShape( xIndexAccess->getByIndex( index), uno::UNO_QUERY);
46  if ( xControlShape.is() )
47  vObjects.push_back( xControlShape );
48  }
49  }
50 
51  virtual ::sal_Int32 SAL_CALL getCount() override
52  {
53  return vObjects.size();
54  }
55 
56  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
57  {
58  if ( Index < 0 || Index >= getCount() )
59  throw lang::IndexOutOfBoundsException();
60  return uno::makeAny( vObjects[ Index ] );
61  }
62 
63  // Methods XElementAccess
64  virtual uno::Type SAL_CALL getElementType() override
65  {
67  }
68 
69  virtual sal_Bool SAL_CALL hasElements() override
70  {
71  return ( getCount() > 0 );
72  }
73 
74 };
75 
76 class EnumWrapper : public EnumerationHelper_BASE
77 {
78 
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 ) {}
88 
89  virtual sal_Bool SAL_CALL hasMoreElements( ) override
90  {
91  return ( nIndex < m_xIndexAccess->getCount() );
92  }
93 
94  virtual uno::Any SAL_CALL nextElement( ) override
95  {
96  if ( nIndex < m_xIndexAccess->getCount() )
97  {
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 ) ) );
100  }
101  throw container::NoSuchElementException();
102  }
103 };
104 
105 uno::Reference< container::XIndexAccess > oleObjectIndexWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess )
106 {
107  return new IndexAccessWrapper( xIndexAccess );
108 }
109 
110 }
111 
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 ) )
115 {
116 }
117 uno::Reference< container::XEnumeration >
119 {
120  return new EnumWrapper( getParent(), mxContext, m_xIndexAccess );
121 }
122 
123 uno::Any
124 ScVbaOLEObjects::createCollectionObject( const css::uno::Any& aSource )
125 {
126  if( aSource.hasValue() )
127  {
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 ) ) );
131  }
132  return uno::Any();
133 }
134 
135 uno::Any
136 ScVbaOLEObjects::getItemByStringIndex( const OUString& sIndex )
137 {
138  try
139  {
141  }
142  catch (const uno::RuntimeException&)
143  {
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++ )
147  {
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() )
153  {
154  return createCollectionObject( aUnoObj );
155  }
156 
157  }
158  return uno::Any();
159  }
160 }
161 
162 uno::Type
164 {
166 }
167 
168 OUString
170 {
171  return "ScVbaOLEObjects";
172 }
173 
174 uno::Sequence< OUString >
176 {
177  static uno::Sequence< OUString > const aServiceNames
178  {
179  "ooo.vba.excel.OLEObjects"
180  };
181  return aServiceNames;
182 }
183 
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
virtual css::uno::Sequence< OUString > getServiceNames() override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
Sequence< OUString > aServiceNames
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex)
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
int nCount
::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE
virtual OUString getServiceImplName() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
unsigned char sal_Bool
css::uno::Type const & get()
tuple index
css::uno::Reference< css::uno::XComponentContext > mxContext
Any makeAny(Color const &value)
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
::cppu::WeakImplHelper< container::XIndexAccess > XIndexAccess_BASE
virtual css::uno::Type SAL_CALL getElementType() 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)
Reference< XComponentContext > m_xContext
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex) override
struct _ADOIndex Index