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/implbase3.hxx>
26 #include "vbawindow.hxx"
27 #include "vbaglobals.hxx"
29 #include <unordered_map>
31 using namespace ::com::sun::star
;
32 using namespace ::ooo::vba
;
34 typedef std::unordered_map
< OUString
,
35 sal_Int32
, OUStringHash
,
36 std::equal_to
< OUString
> > NameIndexHash
;
38 static uno::Reference
< XHelperInterface
> lcl_createWorkbookHIParent( const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Any
& aApplication
)
40 return new ScVbaWorkbook( uno::Reference
< XHelperInterface
>( aApplication
, uno::UNO_QUERY_THROW
), xContext
, xModel
);
43 uno::Any
ComponentToWindow( const uno::Any
& aSource
, uno::Reference
< uno::XComponentContext
> & xContext
, const uno::Any
& aApplication
)
45 uno::Reference
< frame::XModel
> xModel( aSource
, uno::UNO_QUERY_THROW
);
46 // !! TODO !! iterate over all controllers
47 uno::Reference
< frame::XController
> xController( xModel
->getCurrentController(), uno::UNO_SET_THROW
);
48 uno::Reference
< excel::XWindow
> xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel
, xContext
, aApplication
), xContext
, xModel
, xController
) );
49 return uno::makeAny( xWin
);
52 typedef std::vector
< uno::Reference
< sheet::XSpreadsheetDocument
> > Components
;
53 // #TODO more or less the same as class in workwindows ( code sharing needed )
54 class WindowComponentEnumImpl
: public EnumerationHelper_BASE
57 uno::Reference
< uno::XComponentContext
> m_xContext
;
58 Components m_components
;
59 Components::const_iterator m_it
;
62 WindowComponentEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
, const Components
& components
) throw ( uno::RuntimeException
) : m_xContext( xContext
), m_components( components
)
64 m_it
= m_components
.begin();
67 WindowComponentEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
) throw ( uno::RuntimeException
) : m_xContext( xContext
)
69 uno::Reference
< frame::XDesktop2
> xDesktop
= frame::Desktop::create(m_xContext
);
70 uno::Reference
< container::XEnumeration
> mxComponents
= xDesktop
->getComponents()->createEnumeration();
71 while( mxComponents
->hasMoreElements() )
73 uno::Reference
< sheet::XSpreadsheetDocument
> xNext( mxComponents
->nextElement(), uno::UNO_QUERY
);
75 m_components
.push_back( xNext
);
77 m_it
= m_components
.begin();
80 virtual sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
82 return m_it
!= m_components
.end();
85 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
87 if ( !hasMoreElements() )
89 throw container::NoSuchElementException();
91 return makeAny( *(m_it
++) );
95 class WindowEnumImpl
: public WindowComponentEnumImpl
97 uno::Any m_aApplication
;
99 WindowEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Any
& aApplication
): WindowComponentEnumImpl( xContext
), m_aApplication( aApplication
) {}
100 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
102 return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext
, m_aApplication
);
106 typedef ::cppu::WeakImplHelper3
< container::XEnumerationAccess
107 , com::sun::star::container::XIndexAccess
108 , com::sun::star::container::XNameAccess
109 > WindowsAccessImpl_BASE
;
111 class WindowsAccessImpl
: public WindowsAccessImpl_BASE
113 uno::Reference
< uno::XComponentContext
> m_xContext
;
114 Components m_windows
;
115 NameIndexHash namesToIndices
;
117 WindowsAccessImpl( const uno::Reference
< uno::XComponentContext
>& xContext
):m_xContext( xContext
)
119 uno::Reference
< container::XEnumeration
> xEnum
= new WindowComponentEnumImpl( m_xContext
);
121 while( xEnum
->hasMoreElements() )
123 uno::Reference
< sheet::XSpreadsheetDocument
> xNext( xEnum
->nextElement(), uno::UNO_QUERY
);
126 m_windows
.push_back( xNext
);
127 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 uno::Reference
< frame::XController
> xController( xModel
->getCurrentController(), uno::UNO_SET_THROW
);
130 uno::Reference
< XHelperInterface
> xTemp
; // temporary needed for g++ 3.3.5
131 ScVbaWindow
window( xTemp
, m_xContext
, xModel
, xController
);
133 window
.getCaption() >>= sCaption
;
134 namesToIndices
[ sCaption
] = nIndex
++;
141 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
143 return new WindowComponentEnumImpl( m_xContext
, m_windows
);
146 virtual ::sal_Int32 SAL_CALL
getCount( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
148 return m_windows
.size();
150 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw ( lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
153 || static_cast< Components::size_type
>( Index
) >= m_windows
.size() )
154 throw lang::IndexOutOfBoundsException();
155 return makeAny( m_windows
[ Index
] ); // returns xspreadsheetdoc
159 virtual uno::Type SAL_CALL
getElementType( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
161 return cppu::UnoType
<sheet::XSpreadsheetDocument
>::get();
164 virtual sal_Bool SAL_CALL
hasElements( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
166 return ( !m_windows
.empty() );
170 virtual uno::Any SAL_CALL
getByName( const OUString
& aName
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
172 NameIndexHash::const_iterator it
= namesToIndices
.find( aName
);
173 if ( it
== namesToIndices
.end() )
174 throw container::NoSuchElementException();
175 return makeAny( m_windows
[ it
->second
] );
179 virtual uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
181 uno::Sequence
< OUString
> names( namesToIndices
.size() );
182 OUString
* pString
= names
.getArray();
183 NameIndexHash::const_iterator it
= namesToIndices
.begin();
184 NameIndexHash::const_iterator it_end
= namesToIndices
.end();
185 for ( ; it
!= it_end
; ++it
, ++pString
)
186 *pString
= it
->first
;
190 virtual sal_Bool SAL_CALL
hasByName( const OUString
& aName
) throw (uno::RuntimeException
, std::exception
) SAL_OVERRIDE
192 NameIndexHash::const_iterator it
= namesToIndices
.find( aName
);
193 return (it
!= namesToIndices
.end());
198 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
) ) )
201 uno::Reference
< container::XEnumeration
>
202 ScVbaWindows::createEnumeration() throw (uno::RuntimeException
)
204 return new WindowEnumImpl( mxContext
, Application() );
208 ScVbaWindows::createCollectionObject( const css::uno::Any
& aSource
)
210 return ComponentToWindow( aSource
, mxContext
, Application() );
214 ScVbaWindows::getElementType() throw (uno::RuntimeException
)
216 return cppu::UnoType
<excel::XWindows
>::get();
220 ScVbaWindows::Arrange( ::sal_Int32
/*ArrangeStyle*/, const uno::Any
& /*ActiveWorkbook*/, const uno::Any
& /*SyncHorizontal*/, const uno::Any
& /*SyncVertical*/ ) throw (uno::RuntimeException
, std::exception
)
222 //#TODO #FIXME see what can be done for an implementation here
226 ScVbaWindows::getServiceImplName()
228 return OUString("ScVbaWindows");
231 css::uno::Sequence
<OUString
>
232 ScVbaWindows::getServiceNames()
234 static uno::Sequence
< OUString
> sNames
;
235 if ( sNames
.getLength() == 0 )
238 sNames
[0] = "ooo.vba.excel.Windows";
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */