LibreOffice Module vbahelper (master) 1
vbauserform.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#include "vbauserform.hxx"
20#include <com/sun/star/awt/XControl.hpp>
21#include <com/sun/star/awt/XControlContainer.hpp>
22#include <com/sun/star/awt/XWindow2.hpp>
23#include <com/sun/star/awt/PosSize.hpp>
24#include <com/sun/star/container/XNameContainer.hpp>
25#include <com/sun/star/frame/XController.hpp>
26#include <com/sun/star/frame/XModel.hpp>
27#include <com/sun/star/script/XDefaultProperty.hpp>
28#include "vbacontrols.hxx"
29#include <sal/log.hxx>
30
31using namespace ::ooo::vba;
32using namespace ::com::sun::star;
33
34// some little notes
35// XDialog implementation has the following interesting bits
36// a Controls property ( which is an array of the container controls )
37// each item in the controls array is a XControl, where the model is
38// basically a property bag
39// additionally the XDialog instance has itself a model
40// this model has a ControlModels ( array of models ) property
41// the models in ControlModels can be accessed by name
42// also the XDialog is a XControl ( to access the model above
43
44ScVbaUserForm::ScVbaUserForm( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& xContext )
45 : ScVbaUserForm_BASE( getXSomethingFromArgs< XHelperInterface >( aArgs, 0 ), xContext, getXSomethingFromArgs< uno::XInterface >( aArgs, 1 ), getXSomethingFromArgs< frame::XModel >( aArgs, 2 ), nullptr ),
46 mbDispose( true )
47{
48 m_xDialog.set( m_xControl, uno::UNO_QUERY_THROW );
49 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
50 m_xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
51 setGeometryHelper( std::make_unique<UserFormGeometryHelper>( xControl, 0.0, 0.0 ) );
52 if ( aArgs.getLength() >= 4 )
53 aArgs[ 3 ] >>= m_sLibName;
54}
55
57{
58}
59
60void SAL_CALL
62{
63 SAL_INFO("vbahelper", "ScVbaUserForm::Show( )");
64 short aRet = 0;
65 mbDispose = true;
66
67 if ( m_xDialog.is() )
68 {
69 // try to center dialog on model window
70 if( m_xModel.is() ) try
71 {
72 uno::Reference< frame::XController > xController( m_xModel->getCurrentController(), uno::UNO_SET_THROW );
73 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
74 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
75 awt::Rectangle aPosSize = xWindow->getPosSize(); // already in pixel
76
77 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
78 uno::Reference< awt::XWindow > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
79 xControlWindow->setPosSize(static_cast<sal_Int32>((aPosSize.Width - getWidth()) / 2.0), static_cast<sal_Int32>((aPosSize.Height - getHeight()) / 2.0), 0, 0, awt::PosSize::POS );
80 }
81 catch( uno::Exception& )
82 {
83 }
84
85 aRet = m_xDialog->execute();
86 }
87 SAL_INFO("vbahelper", "ScVbaUserForm::Show() execute returned " << aRet);
88 if ( !mbDispose )
89 return;
90
91 try
92 {
93 uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY_THROW );
94 m_xDialog = nullptr;
95 xComp->dispose();
96 mbDispose = false;
97 }
98 catch( uno::Exception& )
99 {
100 }
101}
102
103OUString SAL_CALL
105{
106 OUString sCaption;
107 m_xProps->getPropertyValue( "Title" ) >>= sCaption;
108 return sCaption;
109}
110void
111ScVbaUserForm::setCaption( const OUString& _caption )
112{
113 m_xProps->setPropertyValue( "Title", uno::Any( _caption ) );
114}
115
116sal_Bool SAL_CALL
118{
119 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
120 uno::Reference< awt::XWindow2 > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
121 return xControlWindow->isVisible();
122}
123
124void SAL_CALL
126{
127 if ( bVis )
128 Show();
129 else
130 Hide();
131}
132
134{
135 return mpGeometryHelper->getInnerWidth();
136}
137
138void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth )
139{
140 mpGeometryHelper->setInnerWidth( fInnerWidth );
141}
142
144{
145 return mpGeometryHelper->getInnerHeight();
146}
147
148void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight )
149{
150 mpGeometryHelper->setInnerHeight( fInnerHeight );
151}
152
153void SAL_CALL
155{
156 mbDispose = false; // hide not dispose
157 if ( m_xDialog.is() )
158 m_xDialog->endExecute();
159}
160
161void SAL_CALL
163{
164 // #STUB
165 // do nothing
166}
167
168void SAL_CALL
170{
171 mbDispose = true;
172 if ( m_xDialog.is() )
173 m_xDialog->endExecute();
174}
175
176OUString
178{
179 return "ScVbaUserForm";
180}
181
182uno::Sequence< OUString >
184{
185 return { "ooo.vba.excel.UserForm" };
186}
187
188uno::Reference< beans::XIntrospectionAccess > SAL_CALL
190{
191 return uno::Reference< beans::XIntrospectionAccess >();
192}
193
194uno::Any SAL_CALL
195ScVbaUserForm::invoke( const OUString& /*aFunctionName*/, const uno::Sequence< uno::Any >& /*aParams*/, uno::Sequence< ::sal_Int16 >& /*aOutParamIndex*/, uno::Sequence< uno::Any >& /*aOutParam*/ )
196{
197 throw uno::RuntimeException(); // unsupported operation
198}
199
200void SAL_CALL
201ScVbaUserForm::setValue( const OUString& aPropertyName, const uno::Any& aValue )
202{
203 uno::Any aObject = getValue( aPropertyName );
204
205 // in case the dialog is already closed the VBA implementation should not throw exceptions
206 if ( aObject.hasValue() )
207 {
208 // The Object *must* support XDefaultProperty here because getValue will
209 // only return properties that are Objects ( e.g. controls )
210 // e.g. Userform1.aControl = something
211 // 'aControl' has to support XDefaultProperty to make sense here
212 uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
213 OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
214 uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
215 uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( cppu::UnoType<beans::XPropertySet>::get()), uno::UNO_QUERY_THROW );
216 xPropSet->setPropertyValue( aDfltPropName, aValue );
217 }
218}
219
220uno::Reference< awt::XControl >
221ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt::XControlContainer > const & xContainer )
222{
223 uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
224 if ( !xControl.is() )
225 {
226 const uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
227
228 for ( const auto& rCtrl : aControls )
229 {
230 uno::Reference< awt::XControlContainer > xC( rCtrl, uno::UNO_QUERY );
231 if ( xC.is() )
232 {
233 xControl.set( nestedSearch( aPropertyName, xC ) );
234 if ( xControl.is() )
235 break;
236 }
237 }
238 }
239 return xControl;
240}
241
242uno::Any SAL_CALL
243ScVbaUserForm::getValue( const OUString& aPropertyName )
244{
245 uno::Any aResult;
246
247 // in case the dialog is already closed the VBA implementation should not throw exceptions
248 if ( m_xDialog.is() )
249 {
250 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
251 uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
252 uno::Reference< awt::XControl > xControl = nestedSearch( aPropertyName, xContainer );
253 xContainer->getControl( aPropertyName );
254 if ( xControl.is() )
255 {
256 uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
257 ScVbaControl* pControl = dynamic_cast< ScVbaControl* >( xVBAControl.get() );
258 if (pControl && !m_sLibName.isEmpty())
259 pControl->setLibraryAndCodeName( m_sLibName + "." + getName() );
260 aResult <<= xVBAControl;
261 }
262 }
263
264 return aResult;
265}
266
267sal_Bool SAL_CALL
268ScVbaUserForm::hasMethod( const OUString& /*aName*/ )
269{
270 return false;
271}
272uno::Any SAL_CALL
274{
275 // if the dialog already closed we should do nothing, but the VBA will call methods of the Controls objects
276 // thus we have to provide a dummy object in this case
277 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
278 uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
279 if ( index.hasValue() )
280 return xControls->Item( index, uno::Any() );
281 return uno::Any( xControls );
282}
283
284sal_Bool SAL_CALL
285ScVbaUserForm::hasProperty( const OUString& aName )
286{
287 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
288
289 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xControl.is() );
290 if ( xControl.is() )
291 {
292 uno::Reference< beans::XPropertySet > xDlgProps( xControl->getModel(), uno::UNO_QUERY );
293 if ( xDlgProps.is() )
294 {
295 uno::Reference< container::XNameContainer > xAllChildren( xDlgProps->getPropertyValue( "AllDialogChildren" ), uno::UNO_QUERY_THROW );
296 bool bRes = xAllChildren->hasByName( aName );
297 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xAllChildren.is() << " ---> " << bRes );
298 return bRes;
299 }
300 }
301 return false;
302}
303
304extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
306 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const& args)
307{
308 return cppu::acquire(new ScVbaUserForm(args, context));
309}
310
311/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XPropertySet > m_xProps
void setLibraryAndCodeName(const OUString &sLibCodeName)
Definition: vbacontrol.hxx:64
virtual css::uno::Any SAL_CALL invoke(const OUString &aFunctionName, const css::uno::Sequence< css::uno::Any > &aParams, css::uno::Sequence< ::sal_Int16 > &aOutParamIndex, css::uno::Sequence< css::uno::Any > &aOutParam) override
virtual sal_Bool SAL_CALL hasProperty(const OUString &aName) override
virtual void SAL_CALL Hide() override
css::uno::Reference< css::awt::XDialog > m_xDialog
Definition: vbauserform.hxx:35
ScVbaUserForm(css::uno::Sequence< css::uno::Any > const &aArgs, css::uno::Reference< css::uno::XComponentContext >const &xContext)
Definition: vbauserform.cxx:44
virtual void SAL_CALL setValue(const OUString &aPropertyName, const css::uno::Any &aValue) override
virtual css::uno::Any SAL_CALL getValue(const OUString &aPropertyName) override
virtual void SAL_CALL Show() override
Definition: vbauserform.cxx:61
virtual css::uno::Reference< css::beans::XIntrospectionAccess > SAL_CALL getIntrospection() override
virtual void SAL_CALL setInnerWidth(double fInnerWidth) override
virtual sal_Bool SAL_CALL hasMethod(const OUString &aName) override
virtual void SAL_CALL UnloadObject() override
OUString m_sLibName
Definition: vbauserform.hxx:37
virtual OUString SAL_CALL getCaption() override
virtual double SAL_CALL getInnerHeight() override
virtual void SAL_CALL setCaption(const OUString &_caption) override
virtual ~ScVbaUserForm() override
Definition: vbauserform.cxx:56
virtual void SAL_CALL setInnerHeight(double fInnerHeight) override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual void SAL_CALL RePaint() override
virtual sal_Bool SAL_CALL getVisible() override
virtual css::uno::Any SAL_CALL Controls(const css::uno::Any &index) override
static css::uno::Reference< css::awt::XControl > nestedSearch(const OUString &aPropertyName, css::uno::Reference< css::awt::XControlContainer > const &xContainer)
virtual double SAL_CALL getInnerWidth() override
virtual OUString getServiceImplName() override
virtual void SAL_CALL setVisible(sal_Bool bVis) override
Reference< frame::XModel > m_xModel
uno::Reference< uno::XComponentContext > mxContext
OUString aName
#define SAL_INFO(area, stream)
css::uno::Reference< ov::msforms::XControl > createUserformControl(const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::awt::XControl > &xControl, const css::uno::Reference< css::awt::XControl > &xDialog, const css::uno::Reference< css::frame::XModel > &xModel, double fOffsetX, double fOffsetY)
index
css::uno::Reference< T > getXSomethingFromArgs(css::uno::Sequence< css::uno::Any > const &args, sal_Int32 nPos, bool bCanBeNull=true)
Definition: vbahelper.hxx:60
uno::Reference< beans::XIntrospectionAccess > getIntrospectionAccess(const uno::Any &aObject)
Definition: vbahelper.cxx:101
args
bool hasValue()
Reference< XController > xController
Reference< XFrame > xFrame
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * ScVbaUserForm_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
cppu::ImplInheritanceHelper< ScVbaControl, ov::msforms::XUserForm > ScVbaUserForm_BASE
Definition: vbauserform.hxx:30
Reference< XControl > m_xControl