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"
21 #include <boost/unordered_map.hpp>
23 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
24 #include <com/sun/star/frame/Desktop.hpp>
25 #include <cppuhelper/implbase3.hxx>
27 #include "vbawindow.hxx"
28 #include "vbaglobals.hxx"
30 using namespace ::com::sun::star
;
31 using namespace ::ooo::vba
;
33 typedef boost::unordered_map
< OUString
,
34 sal_Int32
, OUStringHash
,
35 ::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
)
82 return m_it
!= m_components
.end();
85 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
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 Components
& components
, const uno::Any
& aApplication
):WindowComponentEnumImpl( xContext
, components
), m_aApplication( aApplication
){}
100 WindowEnumImpl( const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Any
& aApplication
): WindowComponentEnumImpl( xContext
), m_aApplication( aApplication
) {}
101 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
103 return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext
, m_aApplication
);
107 typedef ::cppu::WeakImplHelper3
< container::XEnumerationAccess
108 , com::sun::star::container::XIndexAccess
109 , com::sun::star::container::XNameAccess
110 > WindowsAccessImpl_BASE
;
112 class WindowsAccessImpl
: public WindowsAccessImpl_BASE
114 uno::Reference
< uno::XComponentContext
> m_xContext
;
115 Components m_windows
;
116 NameIndexHash namesToIndices
;
118 WindowsAccessImpl( const uno::Reference
< uno::XComponentContext
>& xContext
):m_xContext( xContext
)
120 uno::Reference
< container::XEnumeration
> xEnum
= new WindowComponentEnumImpl( m_xContext
);
122 while( xEnum
->hasMoreElements() )
124 uno::Reference
< sheet::XSpreadsheetDocument
> xNext( xEnum
->nextElement(), uno::UNO_QUERY
);
127 m_windows
.push_back( xNext
);
128 uno::Reference
< frame::XModel
> xModel( xNext
, uno::UNO_QUERY_THROW
); // that the spreadsheetdocument is a xmodel is a given
129 // !! TODO !! iterate over all controllers
130 uno::Reference
< frame::XController
> xController( xModel
->getCurrentController(), uno::UNO_SET_THROW
);
131 uno::Reference
< XHelperInterface
> xTemp
; // temporary needed for g++ 3.3.5
132 ScVbaWindow
window( xTemp
, m_xContext
, xModel
, xController
);
134 window
.getCaption() >>= sCaption
;
135 namesToIndices
[ sCaption
] = nIndex
++;
142 virtual uno::Reference
< container::XEnumeration
> SAL_CALL
createEnumeration( ) throw (uno::RuntimeException
)
144 return new WindowComponentEnumImpl( m_xContext
, m_windows
);
147 virtual ::sal_Int32 SAL_CALL
getCount( ) throw (uno::RuntimeException
)
149 return m_windows
.size();
151 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw ( lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
154 || static_cast< Components::size_type
>( Index
) >= m_windows
.size() )
155 throw lang::IndexOutOfBoundsException();
156 return makeAny( m_windows
[ Index
] ); // returns xspreadsheetdoc
160 virtual uno::Type SAL_CALL
getElementType( ) throw (uno::RuntimeException
)
162 return sheet::XSpreadsheetDocument::static_type(0);
165 virtual ::sal_Bool SAL_CALL
hasElements( ) throw (uno::RuntimeException
)
167 return ( !m_windows
.empty() );
171 virtual uno::Any SAL_CALL
getByName( const OUString
& aName
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
173 NameIndexHash::const_iterator it
= namesToIndices
.find( aName
);
174 if ( it
== namesToIndices
.end() )
175 throw container::NoSuchElementException();
176 return makeAny( m_windows
[ it
->second
] );
180 virtual uno::Sequence
< OUString
> SAL_CALL
getElementNames( ) throw (uno::RuntimeException
)
182 uno::Sequence
< OUString
> names( namesToIndices
.size() );
183 OUString
* pString
= names
.getArray();
184 NameIndexHash::const_iterator it
= namesToIndices
.begin();
185 NameIndexHash::const_iterator it_end
= namesToIndices
.end();
186 for ( ; it
!= it_end
; ++it
, ++pString
)
187 *pString
= it
->first
;
191 virtual ::sal_Bool SAL_CALL
hasByName( const OUString
& aName
) throw (uno::RuntimeException
)
193 NameIndexHash::const_iterator it
= namesToIndices
.find( aName
);
194 return (it
!= namesToIndices
.end());
199 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
) ) )
202 uno::Reference
< container::XEnumeration
>
203 ScVbaWindows::createEnumeration() throw (uno::RuntimeException
)
205 return new WindowEnumImpl( mxContext
, Application() );
209 ScVbaWindows::createCollectionObject( const css::uno::Any
& aSource
)
211 return ComponentToWindow( aSource
, mxContext
, Application() );
215 ScVbaWindows::getElementType() throw (uno::RuntimeException
)
217 return excel::XWindows::static_type(0);
222 ScVbaWindows::Arrange( ::sal_Int32
/*ArrangeStyle*/, const uno::Any
& /*ActiveWorkbook*/, const uno::Any
& /*SyncHorizontal*/, const uno::Any
& /*SyncVertical*/ ) throw (uno::RuntimeException
)
224 //#TODO #FIXME see what can be done for an implementation here
229 ScVbaWindows::getServiceImplName()
231 return OUString("ScVbaWindows");
234 css::uno::Sequence
<OUString
>
235 ScVbaWindows::getServiceNames()
237 static uno::Sequence
< OUString
> sNames
;
238 if ( sNames
.getLength() == 0 )
241 sNames
[0] = "ooo.vba.excel.Windows";
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */