xref: /aoo42x/main/sw/source/ui/vba/vbadocument.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbadocument.hxx"
28 #include "vbarange.hxx"
29 #include "vbarangehelper.hxx"
30 #include "vbadocumentproperties.hxx"
31 #include "vbabookmarks.hxx"
32 #include "vbavariables.hxx"
33 #include <com/sun/star/text/XBookmarksSupplier.hpp>
34 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
35 #include <com/sun/star/document/XDocumentInfoSupplier.hpp>
36 #include <com/sun/star/document/XDocumentProperties.hpp>
37 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
38 #include <com/sun/star/drawing/XControlShape.hpp>
39 #include <com/sun/star/drawing/XControlShape.hpp>
40 #include <com/sun/star/form/XFormsSupplier.hpp>
41 #include <ooo/vba/XControlProvider.hpp>
42 
43 #include <vbahelper/helperdecl.hxx>
44 #include <wordvbahelper.hxx>
45 #include <docsh.hxx>
46 #include "vbatemplate.hxx"
47 #include "vbaparagraph.hxx"
48 #include "vbastyles.hxx"
49 #include "vbatables.hxx"
50 #include "vbafield.hxx"
51 #include "vbapagesetup.hxx"
52 #include "vbasections.hxx"
53 #include <vbahelper/vbashapes.hxx>
54 
55 using namespace ::ooo::vba;
56 using namespace ::com::sun::star;
57 
58 SwVbaDocument::SwVbaDocument( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< frame::XModel > xModel ): SwVbaDocument_BASE( xParent, xContext, xModel )
59 {
60     Initialize();
61 }
62 SwVbaDocument::SwVbaDocument( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& xContext ) : SwVbaDocument_BASE( aArgs, xContext )
63 {
64     Initialize();
65 }
66 
67 SwVbaDocument::~SwVbaDocument()
68 {
69 }
70 
71 void SwVbaDocument::Initialize()
72 {
73     mxTextDocument.set( getModel(), uno::UNO_QUERY_THROW );
74 }
75 
76 uno::Reference< word::XRange > SAL_CALL
77 SwVbaDocument::getContent() throw ( uno::RuntimeException )
78 {
79     uno::Reference< text::XTextRange > xStart = mxTextDocument->getText()->getStart();
80     uno::Reference< text::XTextRange > xEnd;
81     return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, xStart, xEnd, sal_True ) );
82 }
83 
84 uno::Reference< word::XRange > SAL_CALL
85 SwVbaDocument::Range( const uno::Any& rStart, const uno::Any& rEnd ) throw ( uno::RuntimeException )
86 {
87     if( !rStart.hasValue() && !rEnd.hasValue() )
88         return getContent();
89 
90     sal_Int32 nStart = 0;
91     sal_Int32 nEnd = 0;
92     rStart >>= nStart;
93     rEnd >>= nEnd;
94     nStart--;
95     nEnd--;
96 
97     uno::Reference< text::XTextRange > xStart;
98     uno::Reference< text::XTextRange > xEnd;
99     if( nStart != -1 || nEnd != -1 )
100     {
101         if( nStart == -1 )
102             xStart = mxTextDocument->getText()->getStart();
103         else
104             xStart = SwVbaRangeHelper::getRangeByPosition( mxTextDocument->getText(), nStart );
105 
106         if( nEnd == -1 )
107             xEnd = mxTextDocument->getText()->getEnd();
108         else
109             xEnd = SwVbaRangeHelper::getRangeByPosition( mxTextDocument->getText(), nEnd );
110     }
111 
112     if( !xStart.is() && !xEnd.is() )
113     {
114         try
115         {
116             // FIXME
117             xStart = mxTextDocument->getText()->getStart();
118             xEnd = mxTextDocument->getText()->getEnd();
119         }
120         catch( uno::Exception )
121         {
122             DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
123         }
124     }
125     return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, xStart, xEnd ) );
126 }
127 
128 uno::Any SAL_CALL
129 SwVbaDocument::BuiltInDocumentProperties( const uno::Any& index ) throw (uno::RuntimeException)
130 {
131     uno::Reference< XCollection > xCol( new SwVbaBuiltinDocumentProperties( mxParent, mxContext, getModel() ) );
132     if ( index.hasValue() )
133         return xCol->Item( index, uno::Any() );
134     return uno::makeAny( xCol );
135 }
136 
137 uno::Any SAL_CALL
138 SwVbaDocument::CustomDocumentProperties( const uno::Any& index ) throw (uno::RuntimeException)
139 {
140     uno::Reference< XCollection > xCol( new SwVbaCustomDocumentProperties( mxParent, mxContext, getModel() ) );
141     if ( index.hasValue() )
142         return xCol->Item( index, uno::Any() );
143     return uno::makeAny( xCol );
144 }
145 
146 uno::Any SAL_CALL
147 SwVbaDocument::Bookmarks( const uno::Any& rIndex ) throw ( uno::RuntimeException )
148 {
149     uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( getModel(),uno::UNO_QUERY_THROW );
150     uno::Reference<container::XIndexAccess > xBookmarks( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
151     uno::Reference< XCollection > xBookmarksVba( new SwVbaBookmarks( this, mxContext, xBookmarks, getModel() ) );
152     if (  rIndex.getValueTypeClass() == uno::TypeClass_VOID )
153         return uno::makeAny( xBookmarksVba );
154 
155     return uno::Any( xBookmarksVba->Item( rIndex, uno::Any() ) );
156 }
157 
158 uno::Any SAL_CALL
159 SwVbaDocument::Variables( const uno::Any& rIndex ) throw ( uno::RuntimeException )
160 {
161     uno::Reference< document::XDocumentPropertiesSupplier > xDocumentPropertiesSupplier( getModel(),uno::UNO_QUERY_THROW );
162     uno::Reference< document::XDocumentProperties > xDocumentProperties =  xDocumentPropertiesSupplier->getDocumentProperties();
163     uno::Reference< beans::XPropertyAccess > xUserDefined( xDocumentProperties->getUserDefinedProperties(), uno::UNO_QUERY_THROW );
164 
165     uno::Reference< XCollection > xVariables( new SwVbaVariables( this, mxContext, xUserDefined ) );
166     if (  rIndex.getValueTypeClass() == uno::TypeClass_VOID )
167         return uno::makeAny( xVariables );
168 
169     return uno::Any( xVariables->Item( rIndex, uno::Any() ) );
170 }
171 
172 uno::Any SAL_CALL
173 SwVbaDocument::Paragraphs( const uno::Any& index ) throw (uno::RuntimeException)
174 {
175     uno::Reference< XCollection > xCol( new SwVbaParagraphs( mxParent, mxContext, mxTextDocument ) );
176     if ( index.hasValue() )
177         return xCol->Item( index, uno::Any() );
178     return uno::makeAny( xCol );
179 }
180 
181 uno::Any SAL_CALL
182 SwVbaDocument::Styles( const uno::Any& index ) throw (uno::RuntimeException)
183 {
184     uno::Reference< XCollection > xCol( new SwVbaStyles( mxParent, mxContext, getModel() ) );
185     if ( index.hasValue() )
186         return xCol->Item( index, uno::Any() );
187     return uno::makeAny( xCol );
188 }
189 
190 uno::Any SAL_CALL
191 SwVbaDocument::Fields( const uno::Any& index ) throw (uno::RuntimeException)
192 {
193     uno::Reference< XCollection > xCol( new SwVbaFields( mxParent, mxContext, getModel() ) );
194     if ( index.hasValue() )
195         return xCol->Item( index, uno::Any() );
196     return uno::makeAny( xCol );
197 }
198 
199 uno::Any SAL_CALL
200 SwVbaDocument::Shapes( const uno::Any& index ) throw (uno::RuntimeException)
201 {
202     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( getModel(), uno::UNO_QUERY_THROW );
203     //uno::Reference< drawing::XShapes > xShapes( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
204     uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
205     uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
206     uno::Reference< XCollection > xCol( new ScVbaShapes( this, mxContext, xIndexAccess, xModel ) );
207 
208     if ( index.hasValue() )
209         return xCol->Item( index, uno::Any() );
210     return uno::makeAny( xCol );
211 }
212 
213 uno::Any SAL_CALL
214 SwVbaDocument::Sections( const uno::Any& index ) throw (uno::RuntimeException)
215 {
216     uno::Reference< XCollection > xCol( new SwVbaSections( mxParent, mxContext, getModel() ) );
217     if ( index.hasValue() )
218         return xCol->Item( index, uno::Any() );
219     return uno::makeAny( xCol );
220 }
221 
222 uno::Any SAL_CALL
223 SwVbaDocument::PageSetup( ) throw (uno::RuntimeException)
224 {
225     uno::Reference< beans::XPropertySet > xPageProps( word::getCurrentPageStyle( mxModel ), uno::UNO_QUERY_THROW );
226     return uno::makeAny( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, mxModel, xPageProps ) ) );
227 }
228 
229 rtl::OUString&
230 SwVbaDocument::getServiceImplName()
231 {
232 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaDocument") );
233 	return sImplName;
234 }
235 uno::Any SAL_CALL
236 SwVbaDocument::getAttachedTemplate() throw (uno::RuntimeException)
237 {
238     uno::Reference< word::XTemplate > xTemplate;
239     uno::Reference< document::XDocumentInfoSupplier > xDocInfoSupp( getModel(), uno::UNO_QUERY_THROW );
240     uno::Reference< document::XDocumentPropertiesSupplier > xDocPropSupp( xDocInfoSupp->getDocumentInfo(), uno::UNO_QUERY_THROW );
241     uno::Reference< document::XDocumentProperties > xDocProps( xDocPropSupp->getDocumentProperties(), uno::UNO_QUERY_THROW );
242     rtl::OUString sTemplateName = xDocProps->getTemplateName();
243 
244     xTemplate = new SwVbaTemplate( this, mxContext, getModel(), sTemplateName );
245     return uno::makeAny( xTemplate );
246 }
247 
248 void SAL_CALL
249 SwVbaDocument::setAttachedTemplate( const css::uno::Any& /*_attachedtemplate*/ ) throw (uno::RuntimeException)
250 {
251     throw uno::RuntimeException();
252 }
253 
254 uno::Any SAL_CALL
255 SwVbaDocument::Tables( const css::uno::Any& aIndex ) throw (uno::RuntimeException)
256 {
257     uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
258     uno::Reference< XCollection > xColl( new SwVbaTables( mxParent, mxContext, xModel ) );
259 
260     if ( aIndex.hasValue() )
261         return xColl->Item( aIndex, uno::Any() );
262     return uno::makeAny( xColl );
263 }
264 
265 void SAL_CALL SwVbaDocument::Activate() throw (uno::RuntimeException)
266 {
267     VbaDocumentBase::Activate();
268 }
269 
270 uno::Any
271 SwVbaDocument::getControlShape( const ::rtl::OUString& sName )
272 {
273     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
274     uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
275 
276     sal_Int32 nCount = xIndexAccess->getCount();
277     for( int index = 0; index < nCount; index++ )
278     {
279         uno::Any aUnoObj =  xIndexAccess->getByIndex( index );
280         // It seems there are some drawing objects that can not query into Control shapes?
281         uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY );
282         if( xControlShape.is() )
283         {
284      	    uno::Reference< container::XNamed > xNamed( xControlShape->getControl(), uno::UNO_QUERY_THROW );
285             if( sName.equals( xNamed->getName() ))
286             {
287                 return aUnoObj;
288             }
289         }
290     }
291     return uno::Any();
292 }
293 
294 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
295 SwVbaDocument::getIntrospection(  ) throw (uno::RuntimeException)
296 {
297 	return uno::Reference< beans::XIntrospectionAccess >();
298 }
299 
300 uno::Any SAL_CALL
301 SwVbaDocument::invoke( const ::rtl::OUString& aFunctionName, const uno::Sequence< uno::Any >& /*aParams*/, uno::Sequence< ::sal_Int16 >& /*aOutParamIndex*/, uno::Sequence< uno::Any >& /*aOutParam*/ ) throw (lang::IllegalArgumentException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
302 {
303 	OSL_TRACE("** SwVbaDocument::invoke( %s ), will barf",
304 		rtl::OUStringToOString( aFunctionName, RTL_TEXTENCODING_UTF8 ).getStr() );
305 
306 	throw uno::RuntimeException(); // unsupported operation
307 }
308 
309 void SAL_CALL
310 SwVbaDocument::setValue( const ::rtl::OUString& /*aPropertyName*/, const uno::Any& /*aValue*/ ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
311 {
312 	throw uno::RuntimeException(); // unsupported operation
313 }
314 uno::Any SAL_CALL
315 SwVbaDocument::getValue( const ::rtl::OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
316 {
317     uno::Reference< drawing::XControlShape > xControlShape( getControlShape( aPropertyName ), uno::UNO_QUERY_THROW );
318 
319     uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
320     uno::Reference< XControlProvider > xControlProvider( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.ControlProvider" ) ), mxContext ), uno::UNO_QUERY_THROW );
321     uno::Reference< msforms::XControl > xControl( xControlProvider->createControl(  xControlShape, getModel() ) );
322     return uno::makeAny( xControl );
323 }
324 
325 ::sal_Bool SAL_CALL
326 SwVbaDocument::hasMethod( const ::rtl::OUString& /*aName*/ ) throw (uno::RuntimeException)
327 {
328 	return sal_False;
329 }
330 
331 ::sal_Bool SAL_CALL
332 SwVbaDocument::hasProperty( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
333 {
334 	uno::Reference< container::XNameAccess > xFormControls( getFormControls() );
335 	if ( xFormControls.is() )
336 		return xFormControls->hasByName( aName );
337 	return sal_False;
338 }
339 
340 uno::Reference< container::XNameAccess >
341 SwVbaDocument::getFormControls()
342 {
343 	uno::Reference< container::XNameAccess > xFormControls;
344 	try
345 	{
346 		uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
347 		uno::Reference< form::XFormsSupplier >  xFormSupplier( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
348     		uno::Reference< container::XIndexAccess > xIndexAccess( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
349 		// get the www-standard container ( maybe we should access the
350 		// 'www-standard' by name rather than index, this seems an
351 		// implementation detail
352 		xFormControls.set( xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW );
353 	}
354 	catch( uno::Exception& )
355 	{
356 	}
357 	return xFormControls;
358 }
359 
360 uno::Sequence< rtl::OUString >
361 SwVbaDocument::getServiceNames()
362 {
363 	static uno::Sequence< rtl::OUString > aServiceNames;
364 	if ( aServiceNames.getLength() == 0 )
365 	{
366 		aServiceNames.realloc( 1 );
367 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Document" ) );
368 	}
369 	return aServiceNames;
370 }
371 
372 namespace document
373 {
374 namespace sdecl = comphelper::service_decl;
375 sdecl::vba_service_class_<SwVbaDocument, sdecl::with_args<true> > serviceImpl;
376 extern sdecl::ServiceDecl const serviceDecl(
377     serviceImpl,
378     "SwVbaDocument",
379     "ooo.vba.word.Document" );
380 }
381 
382