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