lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / vbahelper / source / msforms / vbauserform.cxx
blob03d803d44207c7820de828c954c83a2e169efdce
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 <vbahelper/helperdecl.hxx>
20 #include "service.hxx"
21 #include "vbauserform.hxx"
22 #include <com/sun/star/awt/XControl.hpp>
23 #include <com/sun/star/awt/XControlContainer.hpp>
24 #include <com/sun/star/awt/XWindow2.hpp>
25 #include <com/sun/star/awt/PosSize.hpp>
26 #include <com/sun/star/beans/PropertyConcept.hpp>
27 #include <com/sun/star/container/XNameContainer.hpp>
28 #include <com/sun/star/util/MeasureUnit.hpp>
29 #include <basic/sbx.hxx>
30 #include <basic/sbstar.hxx>
31 #include <basic/sbmeth.hxx>
32 #include "vbacontrols.hxx"
33 #include <sal/log.hxx>
34 #include <o3tl/make_unique.hxx>
36 using namespace ::ooo::vba;
37 using namespace ::com::sun::star;
39 // some little notes
40 // XDialog implementation has the following interesting bits
41 // a Controls property ( which is an array of the container controls )
42 // each item in the controls array is a XControl, where the model is
43 // basically a property bag
44 // additionally the XDialog instance has itself a model
45 // this model has a ControlModels ( array of models ) property
46 // the models in ControlModels can be accessed by name
47 // also the XDialog is a XControl ( to access the model above
49 ScVbaUserForm::ScVbaUserForm( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& xContext )
50 : ScVbaUserForm_BASE( getXSomethingFromArgs< XHelperInterface >( aArgs, 0 ), xContext, getXSomethingFromArgs< uno::XInterface >( aArgs, 1 ), getXSomethingFromArgs< frame::XModel >( aArgs, 2 ), nullptr ),
51 mbDispose( true )
53 m_xDialog.set( m_xControl, uno::UNO_QUERY_THROW );
54 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
55 m_xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
56 setGeometryHelper( o3tl::make_unique<UserFormGeometryHelper>( xControl, 0.0, 0.0 ) );
57 if ( aArgs.getLength() >= 4 )
58 aArgs[ 3 ] >>= m_sLibName;
61 ScVbaUserForm::~ScVbaUserForm()
65 void SAL_CALL
66 ScVbaUserForm::Show( )
68 SAL_INFO("vbahelper", "ScVbaUserForm::Show( )");
69 short aRet = 0;
70 mbDispose = true;
72 if ( m_xDialog.is() )
74 // try to center dialog on model window
75 if( m_xModel.is() ) try
77 uno::Reference< frame::XController > xController( m_xModel->getCurrentController(), uno::UNO_SET_THROW );
78 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
79 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
80 awt::Rectangle aPosSize = xWindow->getPosSize(); // already in pixel
82 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
83 uno::Reference< awt::XWindow > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
84 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 );
86 catch( uno::Exception& )
90 aRet = m_xDialog->execute();
92 SAL_INFO("vbahelper", "ScVbaUserForm::Show() execute returned " << aRet);
93 if ( mbDispose )
95 try
97 uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY_THROW );
98 m_xDialog = nullptr;
99 xComp->dispose();
100 mbDispose = false;
102 catch( uno::Exception& )
108 OUString SAL_CALL
109 ScVbaUserForm::getCaption()
111 OUString sCaption;
112 m_xProps->getPropertyValue( "Title" ) >>= sCaption;
113 return sCaption;
115 void
116 ScVbaUserForm::setCaption( const OUString& _caption )
118 m_xProps->setPropertyValue( "Title", uno::makeAny( _caption ) );
121 sal_Bool SAL_CALL
122 ScVbaUserForm::getVisible()
124 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
125 uno::Reference< awt::XWindow2 > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
126 return xControlWindow->isVisible();
129 void SAL_CALL
130 ScVbaUserForm::setVisible( sal_Bool bVis )
132 if ( bVis )
133 Show();
134 else
135 Hide();
138 double SAL_CALL ScVbaUserForm::getInnerWidth()
140 return mpGeometryHelper->getInnerWidth();
143 void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth )
145 mpGeometryHelper->setInnerWidth( fInnerWidth );
148 double SAL_CALL ScVbaUserForm::getInnerHeight()
150 return mpGeometryHelper->getInnerHeight();
153 void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight )
155 mpGeometryHelper->setInnerHeight( fInnerHeight );
158 void SAL_CALL
159 ScVbaUserForm::Hide( )
161 mbDispose = false; // hide not dispose
162 if ( m_xDialog.is() )
163 m_xDialog->endExecute();
166 void SAL_CALL
167 ScVbaUserForm::RePaint( )
169 // #STUB
170 // do nothing
173 void SAL_CALL
174 ScVbaUserForm::UnloadObject( )
176 mbDispose = true;
177 if ( m_xDialog.is() )
178 m_xDialog->endExecute();
181 OUString
182 ScVbaUserForm::getServiceImplName()
184 return OUString("ScVbaUserForm");
187 uno::Sequence< OUString >
188 ScVbaUserForm::getServiceNames()
190 static uno::Sequence< OUString > aServiceNames;
191 if ( aServiceNames.getLength() == 0 )
193 aServiceNames.realloc( 1 );
194 aServiceNames[ 0 ] = "ooo.vba.excel.UserForm";
196 return aServiceNames;
199 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
200 ScVbaUserForm::getIntrospection( )
202 return uno::Reference< beans::XIntrospectionAccess >();
205 uno::Any SAL_CALL
206 ScVbaUserForm::invoke( const OUString& /*aFunctionName*/, const uno::Sequence< uno::Any >& /*aParams*/, uno::Sequence< ::sal_Int16 >& /*aOutParamIndex*/, uno::Sequence< uno::Any >& /*aOutParam*/ )
208 throw uno::RuntimeException(); // unsupported operation
211 void SAL_CALL
212 ScVbaUserForm::setValue( const OUString& aPropertyName, const uno::Any& aValue )
214 uno::Any aObject = getValue( aPropertyName );
216 // in case the dialog is already closed the VBA implementation should not throw exceptions
217 if ( aObject.hasValue() )
219 // The Object *must* support XDefaultProperty here because getValue will
220 // only return properties that are Objects ( e.g. controls )
221 // e.g. Userform1.aControl = something
222 // 'aControl' has to support XDefaultProperty to make sense here
223 uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
224 OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
225 uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
226 uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( cppu::UnoType<beans::XPropertySet>::get()), uno::UNO_QUERY_THROW );
227 xPropSet->setPropertyValue( aDfltPropName, aValue );
231 uno::Reference< awt::XControl >
232 ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt::XControlContainer > const & xContainer )
234 uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
235 if ( !xControl.is() )
237 uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
238 const uno::Reference< awt::XControl >* pCtrl = aControls.getConstArray();
239 const uno::Reference< awt::XControl >* pCtrlsEnd = pCtrl + aControls.getLength();
241 for ( ; pCtrl < pCtrlsEnd; ++pCtrl )
243 uno::Reference< awt::XControlContainer > xC( *pCtrl, uno::UNO_QUERY );
244 if ( xC.is() )
246 xControl.set( nestedSearch( aPropertyName, xC ) );
247 if ( xControl.is() )
248 break;
252 return xControl;
255 uno::Any SAL_CALL
256 ScVbaUserForm::getValue( const OUString& aPropertyName )
258 uno::Any aResult;
260 // in case the dialog is already closed the VBA implementation should not throw exceptions
261 if ( m_xDialog.is() )
263 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
264 uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
265 uno::Reference< awt::XControl > xControl = nestedSearch( aPropertyName, xContainer );
266 xContainer->getControl( aPropertyName );
267 if ( xControl.is() )
269 uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
270 ScVbaControl* pControl = dynamic_cast< ScVbaControl* >( xVBAControl.get() );
271 if (pControl && !m_sLibName.isEmpty())
272 pControl->setLibraryAndCodeName( m_sLibName.concat( "." ).concat( getName() ) );
273 aResult <<= xVBAControl;
277 return aResult;
280 sal_Bool SAL_CALL
281 ScVbaUserForm::hasMethod( const OUString& /*aName*/ )
283 return false;
285 uno::Any SAL_CALL
286 ScVbaUserForm::Controls( const uno::Any& index )
288 // if the dialog already closed we should do nothing, but the VBA will call methods of the Controls objects
289 // thus we have to provide a dummy object in this case
290 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
291 uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
292 if ( index.hasValue() )
293 return xControls->Item( index, uno::Any() );
294 return uno::makeAny( xControls );
297 sal_Bool SAL_CALL
298 ScVbaUserForm::hasProperty( const OUString& aName )
300 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
302 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xControl.is() );
303 if ( xControl.is() )
305 uno::Reference< beans::XPropertySet > xDlgProps( xControl->getModel(), uno::UNO_QUERY );
306 if ( xDlgProps.is() )
308 uno::Reference< container::XNameContainer > xAllChildren( xDlgProps->getPropertyValue( "AllDialogChildren" ), uno::UNO_QUERY_THROW );
309 bool bRes = xAllChildren->hasByName( aName );
310 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xAllChildren.is() << " ---> " << bRes );
311 return bRes;
314 return false;
317 namespace userform
319 namespace sdecl = comphelper::service_decl;
320 sdecl::vba_service_class_<ScVbaUserForm, sdecl::with_args<true> > const serviceImpl;
321 sdecl::ServiceDecl const serviceDecl(
322 serviceImpl,
323 "ScVbaUserForm",
324 "ooo.vba.msforms.UserForm" );
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */