Use correct object
[LibreOffice.git] / vbahelper / source / msforms / vbauserform.cxx
blob8f99b6f39bec5c2061ed06678f6b2c773d244620
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
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
44 ScVbaUserForm::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 )
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;
56 ScVbaUserForm::~ScVbaUserForm()
60 void SAL_CALL
61 ScVbaUserForm::Show( )
63 SAL_INFO("vbahelper", "ScVbaUserForm::Show( )");
64 short aRet = 0;
65 mbDispose = true;
67 if ( m_xDialog.is() )
69 // try to center dialog on model window
70 if( m_xModel.is() ) try
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
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 );
81 catch( uno::Exception& )
85 aRet = m_xDialog->execute();
87 SAL_INFO("vbahelper", "ScVbaUserForm::Show() execute returned " << aRet);
88 if ( !mbDispose )
89 return;
91 try
93 uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY );
94 if (xComp)
96 m_xDialog = nullptr;
97 xComp->dispose();
98 mbDispose = false;
101 catch( uno::Exception& )
106 OUString SAL_CALL
107 ScVbaUserForm::getCaption()
109 OUString sCaption;
110 m_xProps->getPropertyValue( u"Title"_ustr ) >>= sCaption;
111 return sCaption;
113 void
114 ScVbaUserForm::setCaption( const OUString& _caption )
116 m_xProps->setPropertyValue( u"Title"_ustr, uno::Any( _caption ) );
119 sal_Bool SAL_CALL
120 ScVbaUserForm::getVisible()
122 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
123 uno::Reference< awt::XWindow2 > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
124 return xControlWindow->isVisible();
127 void SAL_CALL
128 ScVbaUserForm::setVisible( sal_Bool bVis )
130 if ( bVis )
131 Show();
132 else
133 Hide();
136 double SAL_CALL ScVbaUserForm::getInnerWidth()
138 return mpGeometryHelper->getInnerWidth();
141 void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth )
143 mpGeometryHelper->setInnerWidth( fInnerWidth );
146 double SAL_CALL ScVbaUserForm::getInnerHeight()
148 return mpGeometryHelper->getInnerHeight();
151 void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight )
153 mpGeometryHelper->setInnerHeight( fInnerHeight );
156 void SAL_CALL
157 ScVbaUserForm::Hide( )
159 mbDispose = false; // hide not dispose
160 if ( m_xDialog.is() )
161 m_xDialog->endExecute();
164 void SAL_CALL
165 ScVbaUserForm::RePaint( )
167 // #STUB
168 // do nothing
171 void SAL_CALL
172 ScVbaUserForm::UnloadObject( )
174 mbDispose = true;
175 if ( m_xDialog.is() )
176 m_xDialog->endExecute();
179 OUString
180 ScVbaUserForm::getServiceImplName()
182 return u"ScVbaUserForm"_ustr;
185 uno::Sequence< OUString >
186 ScVbaUserForm::getServiceNames()
188 return { u"ooo.vba.excel.UserForm"_ustr };
191 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
192 ScVbaUserForm::getIntrospection( )
194 return uno::Reference< beans::XIntrospectionAccess >();
197 uno::Any SAL_CALL
198 ScVbaUserForm::invoke( const OUString& /*aFunctionName*/, const uno::Sequence< uno::Any >& /*aParams*/, uno::Sequence< ::sal_Int16 >& /*aOutParamIndex*/, uno::Sequence< uno::Any >& /*aOutParam*/ )
200 throw uno::RuntimeException(); // unsupported operation
203 void SAL_CALL
204 ScVbaUserForm::setValue( const OUString& aPropertyName, const uno::Any& aValue )
206 uno::Any aObject = getValue( aPropertyName );
208 // in case the dialog is already closed the VBA implementation should not throw exceptions
209 if ( aObject.hasValue() )
211 // The Object *must* support XDefaultProperty here because getValue will
212 // only return properties that are Objects ( e.g. controls )
213 // e.g. Userform1.aControl = something
214 // 'aControl' has to support XDefaultProperty to make sense here
215 uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
216 OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
217 uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
218 uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( cppu::UnoType<beans::XPropertySet>::get()), uno::UNO_QUERY_THROW );
219 xPropSet->setPropertyValue( aDfltPropName, aValue );
223 uno::Reference< awt::XControl >
224 ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt::XControlContainer > const & xContainer )
226 uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
227 if ( !xControl.is() )
229 const uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
231 for ( const auto& rCtrl : aControls )
233 uno::Reference< awt::XControlContainer > xC( rCtrl, uno::UNO_QUERY );
234 if ( xC.is() )
236 xControl.set( nestedSearch( aPropertyName, xC ) );
237 if ( xControl.is() )
238 break;
242 return xControl;
245 uno::Any SAL_CALL
246 ScVbaUserForm::getValue( const OUString& aPropertyName )
248 uno::Any aResult;
250 // in case the dialog is already closed the VBA implementation should not throw exceptions
251 if ( m_xDialog.is() )
253 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
254 uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
255 uno::Reference< awt::XControl > xControl = nestedSearch( aPropertyName, xContainer );
256 xContainer->getControl( aPropertyName );
257 if ( xControl.is() )
259 uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
260 ScVbaControl* pControl = dynamic_cast< ScVbaControl* >( xVBAControl.get() );
261 if (pControl && !m_sLibName.isEmpty())
262 pControl->setLibraryAndCodeName( m_sLibName + "." + getName() );
263 aResult <<= xVBAControl;
267 return aResult;
270 sal_Bool SAL_CALL
271 ScVbaUserForm::hasMethod( const OUString& /*aName*/ )
273 return false;
275 uno::Any SAL_CALL
276 ScVbaUserForm::Controls( const uno::Any& index )
278 // if the dialog already closed we should do nothing, but the VBA will call methods of the Controls objects
279 // thus we have to provide a dummy object in this case
280 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
281 uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
282 if ( index.hasValue() )
283 return xControls->Item( index, uno::Any() );
284 return uno::Any( xControls );
287 sal_Bool SAL_CALL
288 ScVbaUserForm::hasProperty( const OUString& aName )
290 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
292 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xControl.is() );
293 if ( xControl.is() )
295 uno::Reference< beans::XPropertySet > xDlgProps( xControl->getModel(), uno::UNO_QUERY );
296 if ( xDlgProps.is() )
298 uno::Reference< container::XNameContainer > xAllChildren( xDlgProps->getPropertyValue( u"AllDialogChildren"_ustr ), uno::UNO_QUERY_THROW );
299 bool bRes = xAllChildren->hasByName( aName );
300 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xAllChildren.is() << " ---> " << bRes );
301 return bRes;
304 return false;
307 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
308 ScVbaUserForm_get_implementation(
309 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const& args)
311 return cppu::acquire(new ScVbaUserForm(args, context));
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */