sc: factor out some more code
[LibreOffice.git] / sw / source / ui / vba / vbaapplication.cxx
blob3310f8688a05cefd3a10c8dd8b5a4723818403f4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column:100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/task/XStatusIndicatorSupplier.hpp>
21 #include <com/sun/star/task/XStatusIndicator.hpp>
22 #include <com/sun/star/util/thePathSettings.hpp>
23 #include <com/sun/star/awt/XDevice.hpp>
25 #include "vbaapplication.hxx"
26 #include "vbadocument.hxx"
27 #include "vbafilterpropsfromformat.hxx"
28 #include <sal/log.hxx>
29 #include <osl/file.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vbahelper/vbahelper.hxx>
32 #include "vbawindow.hxx"
33 #include "vbasystem.hxx"
34 #include "vbaoptions.hxx"
35 #include "vbaselection.hxx"
36 #include "vbadocuments.hxx"
37 #include "vbaaddins.hxx"
38 #include "vbamailmerge.hxx"
39 #include "vbadialogs.hxx"
40 #include "vbawordbasic.hxx"
41 #include <ooo/vba/XConnectionPoint.hpp>
42 #include <ooo/vba/word/WdEnableCancelKey.hpp>
43 #include <ooo/vba/word/WdWindowState.hpp>
44 #include <ooo/vba/word/XApplicationOutgoing.hpp>
45 #include <ooo/vba/word/XBookmarks.hpp>
46 #include <comphelper/processfactory.hxx>
47 #include <comphelper/propertyvalue.hxx>
48 #include <cppu/unotype.hxx>
49 #include <editeng/acorrcfg.hxx>
50 #include <swdll.hxx>
51 #include <swmodule.hxx>
52 #include <unotxdoc.hxx>
53 #include "vbalistgalleries.hxx"
54 #include <tools/urlobj.hxx>
56 using namespace ::ooo;
57 using namespace ::ooo::vba;
58 using namespace ::ooo::vba::word;
59 using namespace ::com::sun::star;
61 namespace {
63 class SwVbaApplicationOutgoingConnectionPoint : public cppu::WeakImplHelper<XConnectionPoint>
65 private:
66 SwVbaApplication* mpApp;
68 public:
69 SwVbaApplicationOutgoingConnectionPoint( SwVbaApplication* pApp );
71 // XConnectionPoint
72 sal_uInt32 SAL_CALL Advise(const uno::Reference< XSink >& Sink ) override;
73 void SAL_CALL Unadvise( sal_uInt32 Cookie ) override;
78 SwVbaApplication::SwVbaApplication( uno::Reference<uno::XComponentContext >& xContext ):
79 SwVbaApplication_BASE( xContext )
83 SwVbaApplication::~SwVbaApplication()
87 sal_uInt32
88 SwVbaApplication::AddSink( const uno::Reference< XSink >& xSink )
91 SolarMutexGuard aGuard;
92 SwGlobals::ensure();
94 // No harm in potentially calling this several times
95 SwModule::get()->RegisterAutomationApplicationEventsCaller(uno::Reference<XSinkCaller>(this));
96 mvSinks.push_back(xSink);
97 return mvSinks.size();
100 void
101 SwVbaApplication::RemoveSink( sal_uInt32 nNumber )
103 if (nNumber < 1 || nNumber > mvSinks.size())
104 return;
106 mvSinks[nNumber-1] = uno::Reference< XSink >();
109 OUString SAL_CALL
110 SwVbaApplication::getName()
112 return u"Microsoft Word"_ustr;
115 uno::Reference< word::XDocument > SAL_CALL
116 SwVbaApplication::getActiveDocument()
118 return new SwVbaDocument( this, mxContext, getCurrentSwDocument() );
121 rtl::Reference<SwVbaWindow>
122 SwVbaApplication::getActiveSwVbaWindow()
124 // #FIXME so far can't determine Parent
125 rtl::Reference< SwXTextDocument > xModel( getCurrentSwDocument() );
126 uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
127 return new SwVbaWindow( uno::Reference< XHelperInterface >(), mxContext, xModel, xController );
130 uno::Reference< css::uno::XComponentContext > const &
131 SwVbaApplication::getContext() const
133 return mxContext;
136 uno::Reference< word::XWindow > SAL_CALL
137 SwVbaApplication::getActiveWindow()
139 return getActiveSwVbaWindow();
142 uno::Reference<word::XSystem > SAL_CALL
143 SwVbaApplication::getSystem()
145 return uno::Reference< word::XSystem >( new SwVbaSystem( mxContext ) );
148 uno::Reference<word::XOptions > SAL_CALL
149 SwVbaApplication::getOptions()
151 return uno::Reference< word::XOptions >( new SwVbaOptions( mxContext ) );
154 uno::Any SAL_CALL
155 SwVbaApplication::CommandBars( const uno::Any& aIndex )
159 return VbaApplicationBase::CommandBars( aIndex );
161 catch (const uno::RuntimeException&)
163 return uno::Any();
167 uno::Reference< word::XSelection > SAL_CALL
168 SwVbaApplication::getSelection()
170 return new SwVbaSelection( this, mxContext, getCurrentSwDocument() );
173 uno::Reference< word::XWordBasic > SAL_CALL
174 SwVbaApplication::getWordBasic()
176 uno::Reference< word::XWordBasic > xWB( new SwWordBasic( this ) );
177 return xWB;
180 uno::Any SAL_CALL
181 SwVbaApplication::Documents( const uno::Any& index )
183 uno::Reference< XCollection > xCol( new SwVbaDocuments( this, mxContext ) );
184 if ( index.hasValue() )
185 return xCol->Item( index, uno::Any() );
186 return uno::Any( xCol );
189 uno::Any SAL_CALL
190 SwVbaApplication::Addins( const uno::Any& index )
192 static uno::Reference< XCollection > xCol( new SwVbaAddins( this, mxContext ) );
193 if ( index.hasValue() )
194 return xCol->Item( index, uno::Any() );
195 return uno::Any( xCol );
198 uno::Any SAL_CALL
199 SwVbaApplication::Dialogs( const uno::Any& index )
201 uno::Reference< word::XDialogs > xCol( new SwVbaDialogs( this, mxContext, getCurrentSwDocument() ));
202 if ( index.hasValue() )
203 return xCol->Item( index );
204 return uno::Any( xCol );
207 uno::Any SAL_CALL
208 SwVbaApplication::ListGalleries( const uno::Any& index )
210 rtl::Reference< SwXTextDocument > xTextDoc( getCurrentSwDocument() );
211 uno::Reference< XCollection > xCol( new SwVbaListGalleries( this, mxContext, xTextDoc ) );
212 if ( index.hasValue() )
213 return xCol->Item( index, uno::Any() );
214 return uno::Any( xCol );
217 sal_Bool SAL_CALL SwVbaApplication::getDisplayAutoCompleteTips()
219 return SvxAutoCorrCfg::Get().IsAutoTextTip();
222 void SAL_CALL SwVbaApplication::setDisplayAutoCompleteTips( sal_Bool _displayAutoCompleteTips )
224 SvxAutoCorrCfg::Get().SetAutoTextTip( _displayAutoCompleteTips );
227 sal_Int32 SAL_CALL SwVbaApplication::getEnableCancelKey()
229 // the default value is wdCancelInterrupt in Word
230 return word::WdEnableCancelKey::wdCancelInterrupt;
233 void SAL_CALL SwVbaApplication::setEnableCancelKey( sal_Int32/* _enableCancelKey */)
235 // seems not supported in Writer
238 sal_Int32 SAL_CALL SwVbaApplication::getWindowState()
240 auto xWindow = getActiveWindow();
241 if (xWindow.is())
243 uno::Any aState = xWindow->getWindowState();
244 sal_Int32 nState;
245 if (aState >>= nState)
246 return nState;
249 return word::WdWindowState::wdWindowStateNormal; // ?
252 void SAL_CALL SwVbaApplication::setWindowState( sal_Int32 _windowstate )
256 auto xWindow = getActiveWindow();
257 if (xWindow.is())
259 uno::Any aState;
260 aState <<= _windowstate;
261 xWindow->setWindowState( aState );
264 catch (const uno::RuntimeException&)
269 sal_Int32 SAL_CALL SwVbaApplication::getWidth()
271 auto pWindow = getActiveSwVbaWindow();
272 return pWindow->getWidth();
275 void SAL_CALL SwVbaApplication::setWidth( sal_Int32 _width )
277 auto pWindow = getActiveSwVbaWindow();
278 pWindow->setWidth( _width );
281 sal_Int32 SAL_CALL SwVbaApplication::getHeight()
283 auto pWindow = getActiveSwVbaWindow();
284 return pWindow->getHeight();
287 void SAL_CALL SwVbaApplication::setHeight( sal_Int32 _height )
289 auto pWindow = getActiveSwVbaWindow();
290 pWindow->setHeight( _height );
293 sal_Int32 SAL_CALL SwVbaApplication::getLeft()
295 auto pWindow = getActiveSwVbaWindow();
296 return pWindow->getLeft();
299 void SAL_CALL SwVbaApplication::setLeft( sal_Int32 _left )
301 auto pWindow = getActiveSwVbaWindow();
302 pWindow->setLeft( _left );
305 sal_Int32 SAL_CALL SwVbaApplication::getTop()
307 auto pWindow = getActiveSwVbaWindow();
308 return pWindow->getTop();
311 void SAL_CALL SwVbaApplication::setTop( sal_Int32 _top )
313 auto pWindow = getActiveSwVbaWindow();
314 pWindow->setTop( _top );
317 OUString SAL_CALL SwVbaApplication::getStatusBar()
319 return u""_ustr;
322 uno::Any SAL_CALL SwVbaApplication::getCustomizationContext()
324 return uno::Any(); // ???
327 void SAL_CALL SwVbaApplication::setCustomizationContext(const uno::Any& /*_customizationcontext*/)
329 // ???
332 void SAL_CALL SwVbaApplication::setStatusBar( const OUString& _statusbar )
334 // ScVbaAppSettings::setStatusBar() also uses the XStatusIndicator to show this, so maybe that is OK?
335 rtl::Reference< SwXTextDocument > xModel = getCurrentSwDocument();
336 if (xModel.is())
338 uno::Reference< task::XStatusIndicatorSupplier > xStatusIndicatorSupplier( xModel->getCurrentController(), uno::UNO_QUERY );
339 if (xStatusIndicatorSupplier.is())
341 uno::Reference< task::XStatusIndicator > xStatusIndicator = xStatusIndicatorSupplier->getStatusIndicator();
342 if (xStatusIndicator.is())
343 xStatusIndicator->start( _statusbar, 100 );
347 // Yes, we intentionally use the "extensions.olebridge" tag here even if this is sw. We
348 // interpret setting the StatusBar property as a request from an Automation client to display
349 // the string in LibreOffice's debug output, and all other generic Automation support debug
350 // output (in extensions/source/ole) uses that tag. If the check for "cross-module" or mixed log
351 // areas in compilerplugins/clang/sallogareas.cxx is re-activated, this will have to be added as
352 // a special case.
354 SAL_INFO("extensions.olebridge", "Client debug output: " << _statusbar);
357 float SAL_CALL SwVbaApplication::CentimetersToPoints( float Centimeters )
359 return o3tl::convert(Centimeters, o3tl::Length::cm, o3tl::Length::pt);
362 float SAL_CALL SwVbaApplication::PointsToCentimeters( float Points )
364 return o3tl::convert(Points, o3tl::Length::pt, o3tl::Length::cm);
367 float SAL_CALL SwVbaApplication::PixelsToPoints( float Pixels, ::sal_Bool fVertical )
369 //Set up xDevice
370 rtl::Reference< SwXTextDocument > xModel( getCurrentSwDocument() );
371 uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
372 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
373 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
374 css::uno::Reference< css::awt::XDevice > xDevice( xWindow, css::uno::UNO_QUERY );
376 return ooo::vba::PixelsToPoints(xDevice, Pixels, fVertical);
379 float SAL_CALL SwVbaApplication::PointsToPixels( float Pixels, ::sal_Bool fVertical )
381 rtl::Reference< SwXTextDocument > xModel( getCurrentSwDocument() );
382 uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
383 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
384 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
385 css::uno::Reference< css::awt::XDevice > xDevice( xWindow, css::uno::UNO_QUERY );
387 return ooo::vba::PointsToPixels(xDevice, Pixels, fVertical);
390 float SAL_CALL SwVbaApplication::InchesToPoints( float Inches )
392 return o3tl::convert(Inches, o3tl::Length::in, o3tl::Length::pt);
395 float SAL_CALL SwVbaApplication::PointsToInches( float Points )
397 return o3tl::convert(Points, o3tl::Length::pt, o3tl::Length::in);
400 float SAL_CALL SwVbaApplication::MillimetersToPoints( float Millimeters )
402 return o3tl::convert(Millimeters, o3tl::Length::mm, o3tl::Length::pt);
405 float SAL_CALL SwVbaApplication::PointsToMillimeters( float Points )
407 return o3tl::convert(Points, o3tl::Length::pt, o3tl::Length::mm);
410 float SAL_CALL SwVbaApplication::PicasToPoints( float Picas )
412 return o3tl::convert(Picas, o3tl::Length::pc, o3tl::Length::pt);
415 float SAL_CALL SwVbaApplication::PointsToPicas( float Points )
417 return o3tl::convert(Points, o3tl::Length::pt, o3tl::Length::pc);
420 void SAL_CALL SwVbaApplication::ShowMe()
422 // Method no longer supported in word - deprecated
425 void SAL_CALL SwVbaApplication::Resize( sal_Int32 Width, sal_Int32 Height )
427 // Have to do it like this as the Width and Height are hidden away in the ooo::vba::XWindowBase
428 // which ooo::vba::word::XApplication does not inherit from. SwVbaWindow, however, does inherit
429 // from XWindowBase. Ugh.
430 auto pWindow = getActiveSwVbaWindow();
431 pWindow->setWidth( Width );
432 pWindow->setHeight( Height );
435 void SAL_CALL SwVbaApplication::Move( sal_Int32 Left, sal_Int32 Top )
437 // See comment in Resize().
438 auto pWindow = getActiveSwVbaWindow();
439 pWindow->setLeft( Left );
440 pWindow->setTop( Top );
443 // XInterfaceWithIID
445 OUString SAL_CALL
446 SwVbaApplication::getIID()
448 return u"{82154421-0FBF-11d4-8313-005004526AB4}"_ustr;
451 // XConnectable
453 OUString SAL_CALL
454 SwVbaApplication::GetIIDForClassItselfNotCoclass()
456 return u"{82154423-0FBF-11D4-8313-005004526AB4}"_ustr;
459 TypeAndIID SAL_CALL
460 SwVbaApplication::GetConnectionPoint()
462 TypeAndIID aResult =
463 { cppu::UnoType<word::XApplicationOutgoing>::get(),
464 u"{82154422-0FBF-11D4-8313-005004526AB4}"_ustr
467 return aResult;
470 uno::Reference<XConnectionPoint> SAL_CALL
471 SwVbaApplication::FindConnectionPoint()
473 uno::Reference<XConnectionPoint> xCP(new SwVbaApplicationOutgoingConnectionPoint(this));
474 return xCP;
477 OUString
478 SwVbaApplication::getServiceImplName()
480 return u"SwVbaApplication"_ustr;
483 uno::Sequence< OUString >
484 SwVbaApplication::getServiceNames()
486 static uno::Sequence< OUString > const aServiceNames
488 u"ooo.vba.word.Application"_ustr
490 return aServiceNames;
493 uno::Reference< frame::XModel >
494 SwVbaApplication::getCurrentDocument()
496 return static_cast<SfxBaseModel*>(getCurrentWordDoc( mxContext ).get());
499 rtl::Reference< SwXTextDocument >
500 SwVbaApplication::getCurrentSwDocument()
502 return getCurrentWordDoc( mxContext );
505 // XSinkCaller
507 void SAL_CALL
508 SwVbaApplication::CallSinks( const OUString& Method, uno::Sequence< uno::Any >& Arguments )
510 for (auto& i : mvSinks)
512 if (i.is())
513 i->Call(Method, Arguments);
517 // SwVbaApplicationOutgoingConnectionPoint
519 SwVbaApplicationOutgoingConnectionPoint::SwVbaApplicationOutgoingConnectionPoint( SwVbaApplication* pApp ) :
520 mpApp(pApp)
524 // XConnectionPoint
525 sal_uInt32 SAL_CALL
526 SwVbaApplicationOutgoingConnectionPoint::Advise( const uno::Reference< XSink >& Sink )
528 return mpApp->AddSink(Sink);
531 void SAL_CALL
532 SwVbaApplicationOutgoingConnectionPoint::Unadvise( sal_uInt32 Cookie )
534 mpApp->RemoveSink( Cookie );
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */