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