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