xref: /aoo42x/main/sc/source/ui/vba/vbawindows.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir #include "vbawindows.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <hash_map>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
28cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
29cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <tools/urlobj.hxx>
32cdf0e10cSrcweir #include "vbawindow.hxx"
33cdf0e10cSrcweir #include "vbaglobals.hxx"
34cdf0e10cSrcweir //#include "vbaworkbook.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir using namespace ::ooo::vba;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir typedef  std::hash_map< rtl::OUString,
40cdf0e10cSrcweir sal_Int32, ::rtl::OUStringHash,
41cdf0e10cSrcweir ::std::equal_to< ::rtl::OUString > > NameIndexHash;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 
lcl_createWorkbookHIParent(const uno::Reference<frame::XModel> & xModel,const uno::Reference<uno::XComponentContext> & xContext,const uno::Any & aApplication)44cdf0e10cSrcweir uno::Reference< XHelperInterface > lcl_createWorkbookHIParent( const uno::Reference< frame::XModel >& xModel, const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	return new ScVbaWorkbook( uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext,  xModel );
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
ComponentToWindow(const uno::Any & aSource,uno::Reference<uno::XComponentContext> & xContext,const uno::Any & aApplication)49cdf0e10cSrcweir uno::Any ComponentToWindow( const uno::Any& aSource, uno::Reference< uno::XComponentContext > & xContext, const uno::Any& aApplication )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( aSource, uno::UNO_QUERY_THROW );
52cdf0e10cSrcweir 	// !! TODO !! iterate over all controllers
53cdf0e10cSrcweir 	uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
54cdf0e10cSrcweir 	uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext, xModel, xController ) );
55cdf0e10cSrcweir 	return uno::makeAny( xWin );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir typedef std::vector < uno::Reference< sheet::XSpreadsheetDocument > > Components;
59cdf0e10cSrcweir // #TODO more or less the same as class in workwindows ( code sharing needed )
60cdf0e10cSrcweir class WindowComponentEnumImpl : public EnumerationHelper_BASE
61cdf0e10cSrcweir {
62cdf0e10cSrcweir protected:
63cdf0e10cSrcweir 	uno::Reference< uno::XComponentContext > m_xContext;
64cdf0e10cSrcweir 	Components m_components;
65cdf0e10cSrcweir 	Components::const_iterator m_it;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir public:
WindowComponentEnumImpl(const uno::Reference<uno::XComponentContext> & xContext,const Components & components)68cdf0e10cSrcweir 	WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Components& components ) throw ( uno::RuntimeException ) :  m_xContext( xContext ), m_components( components )
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		m_it = m_components.begin();
71cdf0e10cSrcweir 	}
72cdf0e10cSrcweir 
WindowComponentEnumImpl(const uno::Reference<uno::XComponentContext> & xContext)73cdf0e10cSrcweir 	WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException ) :  m_xContext( xContext )
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 		uno::Reference< lang::XMultiComponentFactory > xSMgr(
76cdf0e10cSrcweir 			m_xContext->getServiceManager(), uno::UNO_QUERY_THROW );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 		uno::Reference< frame::XDesktop > xDesktop
79cdf0e10cSrcweir 			(xSMgr->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"), m_xContext), uno::UNO_QUERY_THROW );
80cdf0e10cSrcweir 		uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
81cdf0e10cSrcweir 		while( mxComponents->hasMoreElements() )
82cdf0e10cSrcweir 		{
83cdf0e10cSrcweir 			uno::Reference< sheet::XSpreadsheetDocument > xNext( mxComponents->nextElement(), uno::UNO_QUERY );
84cdf0e10cSrcweir 			if ( xNext.is() )
85cdf0e10cSrcweir 				m_components.push_back( xNext );
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 		m_it = m_components.begin();
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 	// XEnumeration
hasMoreElements()90cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		return m_it != m_components.end();
93cdf0e10cSrcweir 	}
94cdf0e10cSrcweir 
nextElement()95cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
96cdf0e10cSrcweir 	{
97cdf0e10cSrcweir 		if ( !hasMoreElements() )
98cdf0e10cSrcweir 		{
99cdf0e10cSrcweir 			throw container::NoSuchElementException();
100cdf0e10cSrcweir 		}
101cdf0e10cSrcweir 		return makeAny( *(m_it++) );
102cdf0e10cSrcweir 	}
103cdf0e10cSrcweir };
104cdf0e10cSrcweir 
105cdf0e10cSrcweir class WindowEnumImpl : public  WindowComponentEnumImpl
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	uno::Any m_aApplication;
108cdf0e10cSrcweir public:
WindowEnumImpl(const uno::Reference<uno::XComponentContext> & xContext,const Components & components,const uno::Any & aApplication)109cdf0e10cSrcweir 	WindowEnumImpl(const uno::Reference< uno::XComponentContext >& xContext, const Components& components, const uno::Any& aApplication ):WindowComponentEnumImpl( xContext, components ), m_aApplication( aApplication ){}
WindowEnumImpl(const uno::Reference<uno::XComponentContext> & xContext,const uno::Any & aApplication)110cdf0e10cSrcweir 	WindowEnumImpl( const uno::Reference< uno::XComponentContext >& xContext,  const uno::Any& aApplication ): WindowComponentEnumImpl( xContext ), m_aApplication( aApplication ) {}
nextElement()111cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 		return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext, m_aApplication );
114cdf0e10cSrcweir 	}
115cdf0e10cSrcweir };
116cdf0e10cSrcweir 
117cdf0e10cSrcweir typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
118cdf0e10cSrcweir 	, com::sun::star::container::XIndexAccess
119cdf0e10cSrcweir 	, com::sun::star::container::XNameAccess
120cdf0e10cSrcweir 	> WindowsAccessImpl_BASE;
121cdf0e10cSrcweir 
122cdf0e10cSrcweir class WindowsAccessImpl : public WindowsAccessImpl_BASE
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	uno::Reference< uno::XComponentContext > m_xContext;
125cdf0e10cSrcweir 	Components m_windows;
126cdf0e10cSrcweir 	NameIndexHash namesToIndices;
127cdf0e10cSrcweir public:
WindowsAccessImpl(const uno::Reference<uno::XComponentContext> & xContext)128cdf0e10cSrcweir 	WindowsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext ):m_xContext( xContext )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		uno::Reference< container::XEnumeration > xEnum = new WindowComponentEnumImpl( m_xContext );
131cdf0e10cSrcweir 		sal_Int32 nIndex=0;
132cdf0e10cSrcweir 		while( xEnum->hasMoreElements() )
133cdf0e10cSrcweir 		{
134cdf0e10cSrcweir 			uno::Reference< sheet::XSpreadsheetDocument > xNext( xEnum->nextElement(), uno::UNO_QUERY );
135cdf0e10cSrcweir 			if ( xNext.is() )
136cdf0e10cSrcweir 			{
137cdf0e10cSrcweir 				m_windows.push_back( xNext );
138cdf0e10cSrcweir 				uno::Reference< frame::XModel > xModel( xNext, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
139cdf0e10cSrcweir             	// !! TODO !! iterate over all controllers
140cdf0e10cSrcweir             	uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
141cdf0e10cSrcweir                 uno::Reference< XHelperInterface > xTemp;  // temporary needed for g++ 3.3.5
142cdf0e10cSrcweir 				ScVbaWindow window( xTemp, m_xContext, xModel, xController );
143cdf0e10cSrcweir 				rtl::OUString sCaption;
144cdf0e10cSrcweir 				window.getCaption() >>= sCaption;
145cdf0e10cSrcweir 				namesToIndices[ sCaption ] = nIndex++;
146cdf0e10cSrcweir 			}
147cdf0e10cSrcweir 		}
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	}
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	//XEnumerationAccess
createEnumeration()152cdf0e10cSrcweir 	virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
153cdf0e10cSrcweir 	{
154cdf0e10cSrcweir 		return new WindowComponentEnumImpl( m_xContext, m_windows );
155cdf0e10cSrcweir 	}
156cdf0e10cSrcweir 	// XIndexAccess
getCount()157cdf0e10cSrcweir 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
158cdf0e10cSrcweir 	{
159cdf0e10cSrcweir 		return m_windows.size();
160cdf0e10cSrcweir 	}
getByIndex(::sal_Int32 Index)161cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		if ( Index < 0
164cdf0e10cSrcweir 			|| static_cast< Components::size_type >( Index ) >= m_windows.size() )
165cdf0e10cSrcweir 			throw lang::IndexOutOfBoundsException();
166cdf0e10cSrcweir 		return makeAny( m_windows[ Index ] ); // returns xspreadsheetdoc
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	//XElementAccess
getElementType()170cdf0e10cSrcweir 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
171cdf0e10cSrcweir 	{
172cdf0e10cSrcweir 		return sheet::XSpreadsheetDocument::static_type(0);
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir 
hasElements()175cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		return (m_windows.size() > 0);
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	//XNameAccess
getByName(const::rtl::OUString & aName)181cdf0e10cSrcweir 	virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		NameIndexHash::const_iterator it = namesToIndices.find( aName );
184cdf0e10cSrcweir 		if ( it == namesToIndices.end() )
185cdf0e10cSrcweir 			throw container::NoSuchElementException();
186cdf0e10cSrcweir 		return makeAny( m_windows[ it->second ] );
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 
getElementNames()190cdf0e10cSrcweir 	virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException)
191cdf0e10cSrcweir 	{
192cdf0e10cSrcweir 		uno::Sequence< ::rtl::OUString > names( namesToIndices.size() );
193cdf0e10cSrcweir 		::rtl::OUString* pString = names.getArray();
194cdf0e10cSrcweir 		NameIndexHash::const_iterator it = namesToIndices.begin();
195cdf0e10cSrcweir 		NameIndexHash::const_iterator it_end = namesToIndices.end();
196cdf0e10cSrcweir 		for ( ; it != it_end; ++it, ++pString )
197cdf0e10cSrcweir 			*pString = it->first;
198cdf0e10cSrcweir 		return names;
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 
hasByName(const::rtl::OUString & aName)201cdf0e10cSrcweir 	virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
202cdf0e10cSrcweir 	{
203cdf0e10cSrcweir 		NameIndexHash::const_iterator it = namesToIndices.find( aName );
204cdf0e10cSrcweir 		return (it != namesToIndices.end());
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir 
207cdf0e10cSrcweir };
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 
ScVbaWindows(const uno::Reference<ov::XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XIndexAccess> & xIndexAccess)210cdf0e10cSrcweir ScVbaWindows::ScVbaWindows( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xIndexAccess  ):  ScVbaWindows_BASE( xParent, xContext, xIndexAccess )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
ScVbaWindows(const uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext)214cdf0e10cSrcweir 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 ) ) )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir }
217cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()218cdf0e10cSrcweir ScVbaWindows::createEnumeration() throw (uno::RuntimeException)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir 	return new WindowEnumImpl( mxContext, Application() );
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir uno::Any
createCollectionObject(const css::uno::Any & aSource)224cdf0e10cSrcweir ScVbaWindows::createCollectionObject( const css::uno::Any& aSource )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	return ComponentToWindow( aSource,  mxContext, Application() );
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir uno::Type
getElementType()230cdf0e10cSrcweir ScVbaWindows::getElementType() throw (uno::RuntimeException)
231cdf0e10cSrcweir {
232cdf0e10cSrcweir 	return excel::XWindows::static_type(0);
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 
236cdf0e10cSrcweir void SAL_CALL
Arrange(::sal_Int32,const uno::Any &,const uno::Any &,const uno::Any &)237cdf0e10cSrcweir ScVbaWindows::Arrange( ::sal_Int32 /*ArrangeStyle*/, const uno::Any& /*ActiveWorkbook*/, const uno::Any& /*SyncHorizontal*/, const uno::Any& /*SyncVertical*/ ) throw (uno::RuntimeException)
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	//#TODO #FIXME see what can be done for an implementation here
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 
243cdf0e10cSrcweir rtl::OUString&
getServiceImplName()244cdf0e10cSrcweir ScVbaWindows::getServiceImplName()
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWindows") );
247cdf0e10cSrcweir 	return sImplName;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
getServiceNames()251cdf0e10cSrcweir ScVbaWindows::getServiceNames()
252cdf0e10cSrcweir {
253cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > sNames;
254cdf0e10cSrcweir 	if ( sNames.getLength() == 0 )
255cdf0e10cSrcweir 	{
256cdf0e10cSrcweir 		sNames.realloc( 1 );
257cdf0e10cSrcweir 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Windows") );
258cdf0e10cSrcweir 	}
259cdf0e10cSrcweir 	return sNames;
260cdf0e10cSrcweir }
261