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