merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / vba / vbawindows.cxx
blobf65102986a54bb696d2a906989e3ebf66db23853
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbawindows.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbawindows.hxx"
32 #include <hash_map>
34 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
35 #include <com/sun/star/frame/XDesktop.hpp>
36 #include <cppuhelper/implbase3.hxx>
38 #include <tools/urlobj.hxx>
39 #include "vbawindow.hxx"
40 #include "vbaglobals.hxx"
41 //#include "vbaworkbook.hxx"
43 using namespace ::com::sun::star;
44 using namespace ::ooo::vba;
46 typedef std::hash_map< rtl::OUString,
47 sal_Int32, ::rtl::OUStringHash,
48 ::std::equal_to< ::rtl::OUString > > NameIndexHash;
51 uno::Reference< XHelperInterface > lcl_createWorkbookHIParent( const uno::Reference< frame::XModel >& xModel, const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication )
53 return new ScVbaWorkbook( uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext, xModel );
56 uno::Any ComponentToWindow( const uno::Any& aSource, uno::Reference< uno::XComponentContext > & xContext, const uno::Any& aApplication )
58 uno::Reference< frame::XModel > xModel( aSource, uno::UNO_QUERY_THROW );
59 uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext,xModel ) );
60 return uno::makeAny( xWin );
63 typedef std::vector < uno::Reference< sheet::XSpreadsheetDocument > > Components;
64 // #TODO more or less the same as class in workwindows ( code sharing needed )
65 class WindowComponentEnumImpl : public EnumerationHelper_BASE
67 protected:
68 uno::Reference< uno::XComponentContext > m_xContext;
69 Components m_components;
70 Components::const_iterator m_it;
72 public:
73 WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Components& components ) throw ( uno::RuntimeException ) : m_xContext( xContext ), m_components( components )
75 m_it = m_components.begin();
78 WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException ) : m_xContext( xContext )
80 uno::Reference< lang::XMultiComponentFactory > xSMgr(
81 m_xContext->getServiceManager(), uno::UNO_QUERY_THROW );
83 uno::Reference< frame::XDesktop > xDesktop
84 (xSMgr->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"), m_xContext), uno::UNO_QUERY_THROW );
85 uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
86 while( mxComponents->hasMoreElements() )
88 uno::Reference< sheet::XSpreadsheetDocument > xNext( mxComponents->nextElement(), uno::UNO_QUERY );
89 if ( xNext.is() )
90 m_components.push_back( xNext );
92 m_it = m_components.begin();
94 // XEnumeration
95 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
97 return m_it != m_components.end();
100 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
102 if ( !hasMoreElements() )
104 throw container::NoSuchElementException();
106 return makeAny( *(m_it++) );
110 class WindowEnumImpl : public WindowComponentEnumImpl
112 uno::Any m_aApplication;
113 public:
114 WindowEnumImpl(const uno::Reference< uno::XComponentContext >& xContext, const Components& components, const uno::Any& aApplication ):WindowComponentEnumImpl( xContext, components ), m_aApplication( aApplication ){}
115 WindowEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication ): WindowComponentEnumImpl( xContext ), m_aApplication( aApplication ) {}
116 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
118 return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext, m_aApplication );
122 typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
123 , com::sun::star::container::XIndexAccess
124 , com::sun::star::container::XNameAccess
125 > WindowsAccessImpl_BASE;
127 class WindowsAccessImpl : public WindowsAccessImpl_BASE
129 uno::Reference< uno::XComponentContext > m_xContext;
130 Components m_windows;
131 NameIndexHash namesToIndices;
132 public:
133 WindowsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext ):m_xContext( xContext )
135 uno::Reference< container::XEnumeration > xEnum = new WindowComponentEnumImpl( m_xContext );
136 sal_Int32 nIndex=0;
137 while( xEnum->hasMoreElements() )
139 uno::Reference< sheet::XSpreadsheetDocument > xNext( xEnum->nextElement(), uno::UNO_QUERY );
140 if ( xNext.is() )
142 m_windows.push_back( xNext );
143 uno::Reference< frame::XModel > xModel( xNext, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
144 uno::Reference< XHelperInterface > xTemp; // temporary needed for g++ 3.3.5
145 ScVbaWindow window( xTemp, m_xContext, xModel );
146 rtl::OUString sCaption;
147 window.getCaption() >>= sCaption;
148 namesToIndices[ sCaption ] = nIndex++;
154 //XEnumerationAccess
155 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException)
157 return new WindowComponentEnumImpl( m_xContext, m_windows );
159 // XIndexAccess
160 virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException)
162 return m_windows.size();
164 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
166 if ( Index < 0
167 || static_cast< Components::size_type >( Index ) >= m_windows.size() )
168 throw lang::IndexOutOfBoundsException();
169 return makeAny( m_windows[ Index ] ); // returns xspreadsheetdoc
172 //XElementAccess
173 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException)
175 return sheet::XSpreadsheetDocument::static_type(0);
178 virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException)
180 return (m_windows.size() > 0);
183 //XNameAccess
184 virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
186 NameIndexHash::const_iterator it = namesToIndices.find( aName );
187 if ( it == namesToIndices.end() )
188 throw container::NoSuchElementException();
189 return makeAny( m_windows[ it->second ] );
193 virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException)
195 uno::Sequence< ::rtl::OUString > names( namesToIndices.size() );
196 ::rtl::OUString* pString = names.getArray();
197 NameIndexHash::const_iterator it = namesToIndices.begin();
198 NameIndexHash::const_iterator it_end = namesToIndices.end();
199 for ( ; it != it_end; ++it, ++pString )
200 *pString = it->first;
201 return names;
204 virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
206 NameIndexHash::const_iterator it = namesToIndices.find( aName );
207 return (it != namesToIndices.end());
213 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 )
217 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 ) ) )
220 uno::Reference< container::XEnumeration >
221 ScVbaWindows::createEnumeration() throw (uno::RuntimeException)
223 return new WindowEnumImpl( mxContext, Application() );
226 uno::Any
227 ScVbaWindows::createCollectionObject( const css::uno::Any& aSource )
229 return ComponentToWindow( aSource, mxContext, Application() );
232 uno::Type
233 ScVbaWindows::getElementType() throw (uno::RuntimeException)
235 return excel::XWindows::static_type(0);
239 void SAL_CALL
240 ScVbaWindows::Arrange( ::sal_Int32 /*ArrangeStyle*/, const uno::Any& /*ActiveWorkbook*/, const uno::Any& /*SyncHorizontal*/, const uno::Any& /*SyncVertical*/ ) throw (uno::RuntimeException)
242 //#TODO #FIXME see what can be done for an implementation here
246 rtl::OUString&
247 ScVbaWindows::getServiceImplName()
249 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWindows") );
250 return sImplName;
253 css::uno::Sequence<rtl::OUString>
254 ScVbaWindows::getServiceNames()
256 static uno::Sequence< rtl::OUString > sNames;
257 if ( sNames.getLength() == 0 )
259 sNames.realloc( 1 );
260 sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Windows") );
262 return sNames;