update dev300-m58
[ooovba.git] / sw / source / ui / vba / vbadocument.cxx
blob1409dbb4450264556a729df142bc9a52b5b2c511
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: vbadocument.cxx,v $
10 * $Revision: 1.7 $
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 "vbadocument.hxx"
31 #include "vbarange.hxx"
32 #include "vbarangehelper.hxx"
33 #include "vbadocumentproperties.hxx"
34 #include "vbabookmarks.hxx"
35 #include "vbavariables.hxx"
36 #include <com/sun/star/text/XBookmarksSupplier.hpp>
37 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
38 #include <com/sun/star/document/XDocumentInfoSupplier.hpp>
39 #include <com/sun/star/document/XDocumentProperties.hpp>
40 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
41 #include <com/sun/star/drawing/XControlShape.hpp>
42 #include <com/sun/star/drawing/XControlShape.hpp>
43 #include <com/sun/star/form/XFormsSupplier.hpp>
44 #include <ooo/vba/XControlProvider.hpp>
46 #include <vbahelper/helperdecl.hxx>
47 #include <wordvbahelper.hxx>
48 #include <docsh.hxx>
49 #include "vbatemplate.hxx"
50 #include "vbaparagraph.hxx"
51 #include "vbastyles.hxx"
52 #include "vbatables.hxx"
53 #include "vbafield.hxx"
54 #include "vbapagesetup.hxx"
55 #include "vbasections.hxx"
56 #include <vbahelper/vbashapes.hxx>
58 using namespace ::ooo::vba;
59 using namespace ::com::sun::star;
61 SwVbaDocument::SwVbaDocument( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< frame::XModel > xModel ): SwVbaDocument_BASE( xParent, xContext, xModel )
63 Initialize();
65 SwVbaDocument::SwVbaDocument( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& xContext ) : SwVbaDocument_BASE( aArgs, xContext )
67 Initialize();
70 SwVbaDocument::~SwVbaDocument()
74 void SwVbaDocument::Initialize()
76 mxTextDocument.set( getModel(), uno::UNO_QUERY_THROW );
79 uno::Reference< word::XRange > SAL_CALL
80 SwVbaDocument::getContent() throw ( uno::RuntimeException )
82 uno::Reference< text::XTextRange > xStart = mxTextDocument->getText()->getStart();
83 uno::Reference< text::XTextRange > xEnd;
84 return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, xStart, xEnd, sal_True ) );
87 uno::Reference< word::XRange > SAL_CALL
88 SwVbaDocument::Range( const uno::Any& rStart, const uno::Any& rEnd ) throw ( uno::RuntimeException )
90 if( !rStart.hasValue() && !rEnd.hasValue() )
91 return getContent();
93 sal_Int32 nStart = 0;
94 sal_Int32 nEnd = 0;
95 rStart >>= nStart;
96 rEnd >>= nEnd;
97 nStart--;
98 nEnd--;
100 uno::Reference< text::XTextRange > xStart;
101 uno::Reference< text::XTextRange > xEnd;
102 if( nStart != -1 || nEnd != -1 )
104 if( nStart == -1 )
105 xStart = mxTextDocument->getText()->getStart();
106 else
107 xStart = SwVbaRangeHelper::getRangeByPosition( mxTextDocument->getText(), nStart );
109 if( nEnd == -1 )
110 xEnd = mxTextDocument->getText()->getEnd();
111 else
112 xEnd = SwVbaRangeHelper::getRangeByPosition( mxTextDocument->getText(), nEnd );
115 if( !xStart.is() && !xEnd.is() )
119 // FIXME
120 xStart = mxTextDocument->getText()->getStart();
121 xEnd = mxTextDocument->getText()->getEnd();
123 catch( uno::Exception )
125 DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
128 return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, xStart, xEnd ) );
131 uno::Any SAL_CALL
132 SwVbaDocument::BuiltInDocumentProperties( const uno::Any& index ) throw (uno::RuntimeException)
134 uno::Reference< XCollection > xCol( new SwVbaBuiltinDocumentProperties( mxParent, mxContext, getModel() ) );
135 if ( index.hasValue() )
136 return xCol->Item( index, uno::Any() );
137 return uno::makeAny( xCol );
140 uno::Any SAL_CALL
141 SwVbaDocument::CustomDocumentProperties( const uno::Any& index ) throw (uno::RuntimeException)
143 uno::Reference< XCollection > xCol( new SwVbaCustomDocumentProperties( mxParent, mxContext, getModel() ) );
144 if ( index.hasValue() )
145 return xCol->Item( index, uno::Any() );
146 return uno::makeAny( xCol );
149 uno::Any SAL_CALL
150 SwVbaDocument::Bookmarks( const uno::Any& rIndex ) throw ( uno::RuntimeException )
152 uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( getModel(),uno::UNO_QUERY_THROW );
153 uno::Reference<container::XIndexAccess > xBookmarks( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
154 uno::Reference< XCollection > xBookmarksVba( new SwVbaBookmarks( this, mxContext, xBookmarks, getModel() ) );
155 if ( rIndex.getValueTypeClass() == uno::TypeClass_VOID )
156 return uno::makeAny( xBookmarksVba );
158 return uno::Any( xBookmarksVba->Item( rIndex, uno::Any() ) );
161 uno::Any SAL_CALL
162 SwVbaDocument::Variables( const uno::Any& rIndex ) throw ( uno::RuntimeException )
164 uno::Reference< document::XDocumentPropertiesSupplier > xDocumentPropertiesSupplier( getModel(),uno::UNO_QUERY_THROW );
165 uno::Reference< document::XDocumentProperties > xDocumentProperties = xDocumentPropertiesSupplier->getDocumentProperties();
166 uno::Reference< beans::XPropertyAccess > xUserDefined( xDocumentProperties->getUserDefinedProperties(), uno::UNO_QUERY_THROW );
168 uno::Reference< XCollection > xVariables( new SwVbaVariables( this, mxContext, xUserDefined ) );
169 if ( rIndex.getValueTypeClass() == uno::TypeClass_VOID )
170 return uno::makeAny( xVariables );
172 return uno::Any( xVariables->Item( rIndex, uno::Any() ) );
175 uno::Any SAL_CALL
176 SwVbaDocument::Paragraphs( const uno::Any& index ) throw (uno::RuntimeException)
178 uno::Reference< XCollection > xCol( new SwVbaParagraphs( mxParent, mxContext, mxTextDocument ) );
179 if ( index.hasValue() )
180 return xCol->Item( index, uno::Any() );
181 return uno::makeAny( xCol );
184 uno::Any SAL_CALL
185 SwVbaDocument::Styles( const uno::Any& index ) throw (uno::RuntimeException)
187 uno::Reference< XCollection > xCol( new SwVbaStyles( mxParent, mxContext, getModel() ) );
188 if ( index.hasValue() )
189 return xCol->Item( index, uno::Any() );
190 return uno::makeAny( xCol );
193 uno::Any SAL_CALL
194 SwVbaDocument::Fields( const uno::Any& index ) throw (uno::RuntimeException)
196 uno::Reference< XCollection > xCol( new SwVbaFields( mxParent, mxContext, getModel() ) );
197 if ( index.hasValue() )
198 return xCol->Item( index, uno::Any() );
199 return uno::makeAny( xCol );
202 uno::Any SAL_CALL
203 SwVbaDocument::Shapes( const uno::Any& index ) throw (uno::RuntimeException)
205 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( getModel(), uno::UNO_QUERY_THROW );
206 //uno::Reference< drawing::XShapes > xShapes( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
207 uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
208 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
209 uno::Reference< XCollection > xCol( new ScVbaShapes( this, mxContext, xIndexAccess, xModel ) );
211 if ( index.hasValue() )
212 return xCol->Item( index, uno::Any() );
213 return uno::makeAny( xCol );
216 uno::Any SAL_CALL
217 SwVbaDocument::Sections( const uno::Any& index ) throw (uno::RuntimeException)
219 uno::Reference< XCollection > xCol( new SwVbaSections( mxParent, mxContext, getModel() ) );
220 if ( index.hasValue() )
221 return xCol->Item( index, uno::Any() );
222 return uno::makeAny( xCol );
225 uno::Any SAL_CALL
226 SwVbaDocument::PageSetup( ) throw (uno::RuntimeException)
228 uno::Reference< beans::XPropertySet > xPageProps( word::getCurrentPageStyle( mxModel ), uno::UNO_QUERY_THROW );
229 return uno::makeAny( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, mxModel, xPageProps ) ) );
232 rtl::OUString&
233 SwVbaDocument::getServiceImplName()
235 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaDocument") );
236 return sImplName;
238 uno::Any SAL_CALL
239 SwVbaDocument::getAttachedTemplate() throw (uno::RuntimeException)
241 uno::Reference< word::XTemplate > xTemplate;
242 uno::Reference< document::XDocumentInfoSupplier > xDocInfoSupp( getModel(), uno::UNO_QUERY_THROW );
243 uno::Reference< document::XDocumentPropertiesSupplier > xDocPropSupp( xDocInfoSupp->getDocumentInfo(), uno::UNO_QUERY_THROW );
244 uno::Reference< document::XDocumentProperties > xDocProps( xDocPropSupp->getDocumentProperties(), uno::UNO_QUERY_THROW );
245 rtl::OUString sTemplateName = xDocProps->getTemplateName();
247 xTemplate = new SwVbaTemplate( this, mxContext, getModel(), sTemplateName );
248 return uno::makeAny( xTemplate );
251 void SAL_CALL
252 SwVbaDocument::setAttachedTemplate( const css::uno::Any& /*_attachedtemplate*/ ) throw (uno::RuntimeException)
254 throw uno::RuntimeException();
257 uno::Any SAL_CALL
258 SwVbaDocument::Tables( const css::uno::Any& aIndex ) throw (uno::RuntimeException)
260 uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
261 uno::Reference< XCollection > xColl( new SwVbaTables( mxParent, mxContext, xModel ) );
263 if ( aIndex.hasValue() )
264 return xColl->Item( aIndex, uno::Any() );
265 return uno::makeAny( xColl );
268 void SAL_CALL SwVbaDocument::Activate() throw (uno::RuntimeException)
270 VbaDocumentBase::Activate();
273 uno::Any
274 SwVbaDocument::getControlShape( const ::rtl::OUString& sName )
276 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
277 uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
279 sal_Int32 nCount = xIndexAccess->getCount();
280 for( int index = 0; index < nCount; index++ )
282 uno::Any aUnoObj = xIndexAccess->getByIndex( index );
283 // It seems there are some drawing objects that can not query into Control shapes?
284 uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY );
285 if( xControlShape.is() )
287 uno::Reference< container::XNamed > xNamed( xControlShape->getControl(), uno::UNO_QUERY_THROW );
288 if( sName.equals( xNamed->getName() ))
290 return aUnoObj;
294 return uno::Any();
297 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
298 SwVbaDocument::getIntrospection( ) throw (uno::RuntimeException)
300 return uno::Reference< beans::XIntrospectionAccess >();
303 uno::Any SAL_CALL
304 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)
306 OSL_TRACE("** SwVbaDocument::invoke( %s ), will barf",
307 rtl::OUStringToOString( aFunctionName, RTL_TEXTENCODING_UTF8 ).getStr() );
309 throw uno::RuntimeException(); // unsupported operation
312 void SAL_CALL
313 SwVbaDocument::setValue( const ::rtl::OUString& /*aPropertyName*/, const uno::Any& /*aValue*/ ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
315 throw uno::RuntimeException(); // unsupported operation
317 uno::Any SAL_CALL
318 SwVbaDocument::getValue( const ::rtl::OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
320 uno::Reference< drawing::XControlShape > xControlShape( getControlShape( aPropertyName ), uno::UNO_QUERY_THROW );
322 uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
323 uno::Reference< XControlProvider > xControlProvider( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.ControlProvider" ) ), mxContext ), uno::UNO_QUERY_THROW );
324 uno::Reference< msforms::XControl > xControl( xControlProvider->createControl( xControlShape, getModel() ) );
325 return uno::makeAny( xControl );
328 ::sal_Bool SAL_CALL
329 SwVbaDocument::hasMethod( const ::rtl::OUString& /*aName*/ ) throw (uno::RuntimeException)
331 return sal_False;
334 ::sal_Bool SAL_CALL
335 SwVbaDocument::hasProperty( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
337 uno::Reference< container::XNameAccess > xFormControls( getFormControls() );
338 if ( xFormControls.is() )
339 return xFormControls->hasByName( aName );
340 return sal_False;
343 uno::Reference< container::XNameAccess >
344 SwVbaDocument::getFormControls()
346 uno::Reference< container::XNameAccess > xFormControls;
349 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
350 uno::Reference< form::XFormsSupplier > xFormSupplier( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
351 uno::Reference< container::XIndexAccess > xIndexAccess( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
352 // get the www-standard container ( maybe we should access the
353 // 'www-standard' by name rather than index, this seems an
354 // implementation detail
355 xFormControls.set( xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW );
357 catch( uno::Exception& )
360 return xFormControls;
363 uno::Sequence< rtl::OUString >
364 SwVbaDocument::getServiceNames()
366 static uno::Sequence< rtl::OUString > aServiceNames;
367 if ( aServiceNames.getLength() == 0 )
369 aServiceNames.realloc( 1 );
370 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Document" ) );
372 return aServiceNames;
375 namespace document
377 namespace sdecl = comphelper::service_decl;
378 sdecl::vba_service_class_<SwVbaDocument, sdecl::with_args<true> > serviceImpl;
379 extern sdecl::ServiceDecl const serviceDecl(
380 serviceImpl,
381 "SwVbaDocument",
382 "ooo.vba.word.Document" );