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