LibreOffice Module sc (master)  1
vbawindows.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 #include "vbawindows.hxx"
20 
21 
22 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
23 #include <com/sun/star/frame/Desktop.hpp>
24 #include <cppuhelper/implbase.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <rtl/ref.hxx>
28 
29 #include "vbawindow.hxx"
30 #include "vbaworkbook.hxx"
31 
32 #include <unordered_map>
33 
34 using namespace ::com::sun::star;
35 using namespace ::ooo::vba;
36 
37 typedef std::unordered_map< OUString,
38 sal_Int32 > NameIndexHash;
39 
40 static uno::Reference< XHelperInterface > lcl_createWorkbookHIParent( const uno::Reference< frame::XModel >& xModel, const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication )
41 {
42  return new ScVbaWorkbook( uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext, xModel );
43 }
44 
45 static uno::Any ComponentToWindow( const uno::Any& aSource, const uno::Reference< uno::XComponentContext > & xContext, const uno::Any& aApplication )
46 {
47  uno::Reference< frame::XModel > xModel( aSource, uno::UNO_QUERY_THROW );
48  // !! TODO !! iterate over all controllers
49  uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
50  uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext, xModel, xController ) );
51  return uno::makeAny( xWin );
52 }
53 
54 typedef std::vector < uno::Reference< sheet::XSpreadsheetDocument > > Components;
55 
56 namespace {
57 
58 // #TODO more or less the same as class in workwindows ( code sharing needed )
59 class WindowComponentEnumImpl : public EnumerationHelper_BASE
60 {
61 protected:
62  uno::Reference< uno::XComponentContext > m_xContext;
64  Components::const_iterator m_it;
65 
66 public:
68  WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Components& components ) : m_xContext( xContext ), m_components( components )
69  {
70  m_it = m_components.begin();
71  }
72 
74  explicit WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) : m_xContext( xContext )
75  {
76  uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(m_xContext);
77  uno::Reference< container::XEnumeration > xComponents = xDesktop->getComponents()->createEnumeration();
78  while( xComponents->hasMoreElements() )
79  {
80  uno::Reference< sheet::XSpreadsheetDocument > xNext( xComponents->nextElement(), uno::UNO_QUERY );
81  if ( xNext.is() )
82  m_components.push_back( xNext );
83  }
84  m_it = m_components.begin();
85  }
86  // XEnumeration
87  virtual sal_Bool SAL_CALL hasMoreElements( ) override
88  {
89  return m_it != m_components.end();
90  }
91 
92  virtual uno::Any SAL_CALL nextElement( ) override
93  {
94  if ( !hasMoreElements() )
95  {
96  throw container::NoSuchElementException();
97  }
98  return makeAny( *(m_it++) );
99  }
100 };
101 
102 class WindowEnumImpl : public WindowComponentEnumImpl
103 {
104  uno::Any m_aApplication;
105 public:
106  WindowEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication ): WindowComponentEnumImpl( xContext ), m_aApplication( aApplication ) {}
107  virtual uno::Any SAL_CALL nextElement( ) override
108  {
109  return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext, m_aApplication );
110  }
111 };
112 
113 }
114 
115 typedef ::cppu::WeakImplHelper< container::XEnumerationAccess
116  , css::container::XIndexAccess
117  , css::container::XNameAccess
119 
120 namespace {
121 
122 class WindowsAccessImpl : public WindowsAccessImpl_BASE
123 {
124  uno::Reference< uno::XComponentContext > m_xContext;
125  Components m_windows;
126  NameIndexHash namesToIndices;
127 public:
128  explicit WindowsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext ):m_xContext( xContext )
129  {
130  uno::Reference< container::XEnumeration > xEnum = new WindowComponentEnumImpl( m_xContext );
131  sal_Int32 nIndex=0;
132  while( xEnum->hasMoreElements() )
133  {
134  uno::Reference< sheet::XSpreadsheetDocument > xNext( xEnum->nextElement(), uno::UNO_QUERY );
135  if ( xNext.is() )
136  {
137  m_windows.push_back( xNext );
138  uno::Reference< frame::XModel > xModel( xNext, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
139  // !! TODO !! iterate over all controllers
140  uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
141  uno::Reference< XHelperInterface > xTemp; // temporary needed for g++ 3.3.5
142  rtl::Reference< ScVbaWindow > window( new ScVbaWindow( xTemp, m_xContext, xModel, xController ) );
143  OUString sCaption;
144  window->getCaption() >>= sCaption;
145  namesToIndices[ sCaption ] = nIndex++;
146  }
147  }
148 
149  }
150 
151  //XEnumerationAccess
152  virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
153  {
154  return new WindowComponentEnumImpl( m_xContext, m_windows );
155  }
156  // XIndexAccess
157  virtual ::sal_Int32 SAL_CALL getCount( ) override
158  {
159  return m_windows.size();
160  }
161  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
162  {
163  if ( Index < 0
164  || o3tl::make_unsigned( Index ) >= m_windows.size() )
165  throw lang::IndexOutOfBoundsException();
166  return makeAny( m_windows[ Index ] ); // returns xspreadsheetdoc
167  }
168 
169  //XElementAccess
170  virtual uno::Type SAL_CALL getElementType( ) override
171  {
173  }
174 
175  virtual sal_Bool SAL_CALL hasElements( ) override
176  {
177  return ( !m_windows.empty() );
178  }
179 
180  //XNameAccess
181  virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
182  {
183  NameIndexHash::const_iterator it = namesToIndices.find( aName );
184  if ( it == namesToIndices.end() )
185  throw container::NoSuchElementException();
186  return makeAny( m_windows[ it->second ] );
187 
188  }
189 
190  virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
191  {
192  return comphelper::mapKeysToSequence( namesToIndices );
193  }
194 
195  virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
196  {
197  NameIndexHash::const_iterator it = namesToIndices.find( aName );
198  return (it != namesToIndices.end());
199  }
200 
201 };
202 
203 }
204 
205 ScVbaWindows::ScVbaWindows( const uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWindows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess > ( new WindowsAccessImpl( xContext ) ) )
206 {
207 }
208 uno::Reference< container::XEnumeration >
210 {
211  return new WindowEnumImpl( mxContext, Application() );
212 }
213 
214 uno::Any
215 ScVbaWindows::createCollectionObject( const css::uno::Any& aSource )
216 {
217  return ComponentToWindow( aSource, mxContext, Application() );
218 }
219 
220 uno::Type
222 {
224 }
225 
226 void SAL_CALL
227 ScVbaWindows::Arrange( ::sal_Int32 /*ArrangeStyle*/, const uno::Any& /*ActiveWorkbook*/, const uno::Any& /*SyncHorizontal*/, const uno::Any& /*SyncVertical*/ )
228 {
229  //#TODO #FIXME see what can be done for an implementation here
230 }
231 
232 OUString
234 {
235  return "ScVbaWindows";
236 }
237 
238 css::uno::Sequence<OUString>
240 {
241  static uno::Sequence< OUString > const sNames
242  {
243  "ooo.vba.excel.Windows"
244  };
245  return sNames;
246 }
247 
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Any SAL_CALL Application() override
sal_Int32 nIndex
virtual OUString getServiceImplName() override
Definition: vbawindows.cxx:233
static uno::Any ComponentToWindow(const uno::Any &aSource, const uno::Reference< uno::XComponentContext > &xContext, const uno::Any &aApplication)
Definition: vbawindows.cxx:45
std::deque< OUString > m_components
Reference
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbawindows.cxx:221
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
exports com.sun.star. container
Reference< XController > xController
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbawindows.cxx:239
virtual void SAL_CALL Arrange(::sal_Int32 ArrangeStyle, const css::uno::Any &ActiveWorkbook, const css::uno::Any &SyncHorizontal, const css::uno::Any &SyncVertical) override
Definition: vbawindows.cxx:227
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbawindows.cxx:209
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
unsigned char sal_Bool
ScVbaWindows(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext)
Definition: vbawindows.cxx:205
css::uno::Type const & get()
std::unordered_map< OUString, sal_Int32 > NameIndexHash
::cppu::WeakImplHelper< container::XEnumerationAccess, css::container::XIndexAccess, css::container::XNameAccess > WindowsAccessImpl_BASE
Definition: vbawindows.cxx:118
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbawindows.cxx:215
std::vector< uno::Reference< sheet::XSpreadsheetDocument > > Components
Definition: vbawindows.cxx:54
css::uno::Reference< css::uno::XComponentContext > mxContext
OUString aName
std::unordered_map< OUString, sal_Int32 > NameIndexHash
Definition: vbawindows.cxx:38
static uno::Reference< XHelperInterface > lcl_createWorkbookHIParent(const uno::Reference< frame::XModel > &xModel, const uno::Reference< uno::XComponentContext > &xContext, const uno::Any &aApplication)
Definition: vbawindows.cxx:40
Reference< XModel > xModel
Reference< XComponentContext > m_xContext
struct _ADOIndex Index
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)