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