1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbawindows.hxx"
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>
29 #include "vbawindow.hxx"
30 #include "vbaworkbook.hxx"
32 #include <unordered_map>
34 using namespace ::com::sun::star
;
35 using namespace ::ooo::vba
;
37 typedef std::unordered_map
< OUString
,
38 sal_Int32
> NameIndexHash
;
40 static uno::Reference
< XHelperInterface
> lcl_createWorkbookHIParent( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Any
& aApplication
)
42 return new ScVbaWorkbook( uno::Reference
< XHelperInterface
>( aApplication
, uno::UNO_QUERY_THROW
), xContext
, xModel
);
45 static uno::Any
ComponentToWindow( const uno::Any
& aSource
, const uno::Reference
< uno::XComponentContext
> & xContext
, const uno::Any
& aApplication
)
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
);
54 typedef std::vector
< uno::Reference
< sheet::XSpreadsheetDocument
> > Components
;
58 // #TODO more or less the same as class in workwindows ( code sharing needed )
59 class WindowComponentEnumImpl
: public EnumerationHelper_BASE
62 uno::Reference
< uno::XComponentContext
> m_xContext
;
63 Components m_components
;
64 Components::const_iterator m_it
;
67 /// @throws uno::RuntimeException
68 WindowComponentEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
, const Components
& components
) : m_xContext( xContext
), m_components( components
)
70 m_it
= m_components
.begin();
73 /// @throws uno::RuntimeException
74 explicit WindowComponentEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
) : m_xContext( xContext
)
76 uno::Reference
< frame::XDesktop2
> xDesktop
= frame::Desktop::create(m_xContext
);
77 uno::Reference
< container::XEnumeration
> xComponents
= xDesktop
->getComponents()->createEnumeration();
78 while( xComponents
->hasMoreElements() )
80 uno::Reference
< sheet::XSpreadsheetDocument
> xNext( xComponents
->nextElement(), uno::UNO_QUERY
);
82 m_components
.push_back( xNext
);
84 m_it
= m_components
.begin();
87 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
89 return m_it
!= m_components
.end();
92 virtual uno::Any SAL_CALL
nextElement( ) override
94 if ( !hasMoreElements() )
96 throw container::NoSuchElementException();
98 return makeAny( *(m_it
++) );
102 class WindowEnumImpl
: public WindowComponentEnumImpl
104 uno::Any m_aApplication
;
106 WindowEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Any
& aApplication
): WindowComponentEnumImpl( xContext
), m_aApplication( aApplication
) {}
107 virtual uno::Any SAL_CALL
nextElement( ) override
109 return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext
, m_aApplication
);
115 typedef ::cppu::WeakImplHelper
< container::XEnumerationAccess
116 , css::container::XIndexAccess
117 , css::container::XNameAccess
118 > WindowsAccessImpl_BASE
;
122 class WindowsAccessImpl
: public WindowsAccessImpl_BASE
124 uno::Reference
< uno::XComponentContext
> m_xContext
;
125 Components m_windows
;
126 NameIndexHash namesToIndices
;
128 explicit WindowsAccessImpl( const uno::Reference
< uno::XComponentContext
>& xContext
):m_xContext( xContext
)
130 uno::Reference
< container::XEnumeration
> xEnum
= new WindowComponentEnumImpl( m_xContext
);
132 while( xEnum
->hasMoreElements() )
134 uno::Reference
< sheet::XSpreadsheetDocument
> xNext( xEnum
->nextElement(), uno::UNO_QUERY
);
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
) );
144 window
->getCaption() >>= sCaption
;
145 namesToIndices
[ sCaption
] = nIndex
++;
152 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) override
154 return new WindowComponentEnumImpl( m_xContext
, m_windows
);
157 virtual ::sal_Int32 SAL_CALL
getCount( ) override
159 return m_windows
.size();
161 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) override
164 || o3tl::make_unsigned( Index
) >= m_windows
.size() )
165 throw lang::IndexOutOfBoundsException();
166 return makeAny( m_windows
[ Index
] ); // returns xspreadsheetdoc
170 virtual uno::Type SAL_CALL
getElementType( ) override
172 return cppu::UnoType
<sheet::XSpreadsheetDocument
>::get();
175 virtual sal_Bool SAL_CALL
hasElements( ) override
177 return ( !m_windows
.empty() );
181 virtual uno::Any SAL_CALL
getByName( const OUString
& aName
) override
183 NameIndexHash::const_iterator it
= namesToIndices
.find( aName
);
184 if ( it
== namesToIndices
.end() )
185 throw container::NoSuchElementException();
186 return makeAny( m_windows
[ it
->second
] );
190 virtual uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) override
192 return comphelper::mapKeysToSequence( namesToIndices
);
195 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) override
197 NameIndexHash::const_iterator it
= namesToIndices
.find( aName
);
198 return (it
!= namesToIndices
.end());
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
) ) )
208 uno::Reference
< container::XEnumeration
>
209 ScVbaWindows::createEnumeration()
211 return new WindowEnumImpl( mxContext
, Application() );
215 ScVbaWindows::createCollectionObject( const css::uno::Any
& aSource
)
217 return ComponentToWindow( aSource
, mxContext
, Application() );
221 ScVbaWindows::getElementType()
223 return cppu::UnoType
<excel::XWindows
>::get();
227 ScVbaWindows::Arrange( ::sal_Int32
/*ArrangeStyle*/, const uno::Any
& /*ActiveWorkbook*/, const uno::Any
& /*SyncHorizontal*/, const uno::Any
& /*SyncVertical*/ )
229 //#TODO #FIXME see what can be done for an implementation here
233 ScVbaWindows::getServiceImplName()
235 return "ScVbaWindows";
238 css::uno::Sequence
<OUString
>
239 ScVbaWindows::getServiceNames()
241 static uno::Sequence
< OUString
> const sNames
243 "ooo.vba.excel.Windows"
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */