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
lcl_createWorkbookHIParent(const uno::Reference<frame::XModel> & xModel,const uno::Reference<uno::XComponentContext> & xContext,const uno::Any & aApplication)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
ComponentToWindow(const uno::Any & aSource,const uno::Reference<uno::XComponentContext> & xContext,const uno::Any & aApplication)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;
63 Components m_components;
64 Components::const_iterator m_it;
65
66 public:
67 /// @throws uno::RuntimeException
WindowComponentEnumImpl(const uno::Reference<uno::XComponentContext> & xContext,const Components & components)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
73 /// @throws uno::RuntimeException
WindowComponentEnumImpl(const uno::Reference<uno::XComponentContext> & xContext)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
hasMoreElements()87 virtual sal_Bool SAL_CALL hasMoreElements( ) override
88 {
89 return m_it != m_components.end();
90 }
91
nextElement()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:
WindowEnumImpl(const uno::Reference<uno::XComponentContext> & xContext,const uno::Any & aApplication)106 WindowEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication ): WindowComponentEnumImpl( xContext ), m_aApplication( aApplication ) {}
nextElement()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
118 > WindowsAccessImpl_BASE;
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:
WindowsAccessImpl(const uno::Reference<uno::XComponentContext> & xContext)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
createEnumeration()152 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
153 {
154 return new WindowComponentEnumImpl( m_xContext, m_windows );
155 }
156 // XIndexAccess
getCount()157 virtual ::sal_Int32 SAL_CALL getCount( ) override
158 {
159 return m_windows.size();
160 }
getByIndex(::sal_Int32 Index)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
getElementType()170 virtual uno::Type SAL_CALL getElementType( ) override
171 {
172 return cppu::UnoType<sheet::XSpreadsheetDocument>::get();
173 }
174
hasElements()175 virtual sal_Bool SAL_CALL hasElements( ) override
176 {
177 return ( !m_windows.empty() );
178 }
179
180 //XNameAccess
getByName(const OUString & aName)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
getElementNames()190 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
191 {
192 return comphelper::mapKeysToSequence( namesToIndices );
193 }
194
hasByName(const OUString & aName)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
ScVbaWindows(const uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext)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 >
createEnumeration()209 ScVbaWindows::createEnumeration()
210 {
211 return new WindowEnumImpl( mxContext, Application() );
212 }
213
214 uno::Any
createCollectionObject(const css::uno::Any & aSource)215 ScVbaWindows::createCollectionObject( const css::uno::Any& aSource )
216 {
217 return ComponentToWindow( aSource, mxContext, Application() );
218 }
219
220 uno::Type
getElementType()221 ScVbaWindows::getElementType()
222 {
223 return cppu::UnoType<excel::XWindows>::get();
224 }
225
226 void SAL_CALL
Arrange(::sal_Int32,const uno::Any &,const uno::Any &,const uno::Any &)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
getServiceImplName()233 ScVbaWindows::getServiceImplName()
234 {
235 return "ScVbaWindows";
236 }
237
238 css::uno::Sequence<OUString>
getServiceNames()239 ScVbaWindows::getServiceNames()
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: */
249