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