bump product version to 5.0.4.1
[LibreOffice.git] / vbahelper / source / msforms / vbauserform.cxx
blob8b6fe02e666f6f6e5e0e87a60215231ff9e15a4e
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 "vbauserform.hxx"
21 #include <com/sun/star/awt/XControl.hpp>
22 #include <com/sun/star/awt/XControlContainer.hpp>
23 #include <com/sun/star/awt/XWindow2.hpp>
24 #include <com/sun/star/awt/PosSize.hpp>
25 #include <com/sun/star/beans/PropertyConcept.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/util/MeasureUnit.hpp>
28 #include <basic/sbx.hxx>
29 #include <basic/sbstar.hxx>
30 #include <basic/sbmeth.hxx>
31 #include "vbacontrols.hxx"
33 using namespace ::ooo::vba;
34 using namespace ::com::sun::star;
36 // some little notes
37 // XDialog implementation has the following interesting bits
38 // a Controls property ( which is an array of the container controls )
39 // each item in the controls array is a XControl, where the model is
40 // basically a property bag
41 // additionally the XDialog instance has itself a model
42 // this model has a ControlModels ( array of models ) property
43 // the models in ControlModels can be accessed by name
44 // also the XDialog is a XControl ( to access the model above
46 ScVbaUserForm::ScVbaUserForm( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& xContext ) throw ( lang::IllegalArgumentException, uno::RuntimeException ) : ScVbaUserForm_BASE( getXSomethingFromArgs< XHelperInterface >( aArgs, 0 ), xContext, getXSomethingFromArgs< uno::XInterface >( aArgs, 1 ), getXSomethingFromArgs< frame::XModel >( aArgs, 2 ), static_cast< ooo::vba::AbstractGeometryAttributes* >(0) ), 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( new UserFormGeometryHelper( xContext, 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( ) throw (uno::RuntimeException, std::exception)
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 )
90 try
92 uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY_THROW );
93 m_xDialog = NULL;
94 xComp->dispose();
95 mbDispose = false;
97 catch( uno::Exception& )
103 OUString SAL_CALL
104 ScVbaUserForm::getCaption() throw (uno::RuntimeException, std::exception)
106 OUString sCaption;
107 m_xProps->getPropertyValue( "Title" ) >>= sCaption;
108 return sCaption;
110 void
111 ScVbaUserForm::setCaption( const OUString& _caption ) throw (uno::RuntimeException, std::exception)
113 m_xProps->setPropertyValue( "Title", uno::makeAny( _caption ) );
116 sal_Bool SAL_CALL
117 ScVbaUserForm::getVisible() throw (uno::RuntimeException, std::exception)
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();
124 void SAL_CALL
125 ScVbaUserForm::setVisible( sal_Bool bVis ) throw (uno::RuntimeException, std::exception)
127 if ( bVis )
128 Show();
129 else
130 Hide();
133 double SAL_CALL ScVbaUserForm::getInnerWidth() throw (uno::RuntimeException, std::exception)
135 return mpGeometryHelper->getInnerWidth();
138 void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth ) throw (uno::RuntimeException, std::exception)
140 mpGeometryHelper->setInnerWidth( fInnerWidth );
143 double SAL_CALL ScVbaUserForm::getInnerHeight() throw (uno::RuntimeException, std::exception)
145 return mpGeometryHelper->getInnerHeight();
148 void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight ) throw (uno::RuntimeException, std::exception)
150 mpGeometryHelper->setInnerHeight( fInnerHeight );
153 void SAL_CALL
154 ScVbaUserForm::Hide( ) throw (uno::RuntimeException, std::exception)
156 mbDispose = false; // hide not dispose
157 if ( m_xDialog.is() )
158 m_xDialog->endExecute();
161 void SAL_CALL
162 ScVbaUserForm::RePaint( ) throw (uno::RuntimeException, std::exception)
164 // #STUB
165 // do nothing
168 void SAL_CALL
169 ScVbaUserForm::UnloadObject( ) throw (uno::RuntimeException, std::exception)
171 mbDispose = true;
172 if ( m_xDialog.is() )
173 m_xDialog->endExecute();
176 OUString
177 ScVbaUserForm::getServiceImplName()
179 return OUString("ScVbaUserForm");
182 uno::Sequence< OUString >
183 ScVbaUserForm::getServiceNames()
185 static uno::Sequence< OUString > aServiceNames;
186 if ( aServiceNames.getLength() == 0 )
188 aServiceNames.realloc( 1 );
189 aServiceNames[ 0 ] = "ooo.vba.excel.UserForm";
191 return aServiceNames;
194 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
195 ScVbaUserForm::getIntrospection( ) throw (uno::RuntimeException, std::exception)
197 return uno::Reference< beans::XIntrospectionAccess >();
200 uno::Any SAL_CALL
201 ScVbaUserForm::invoke( const 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, std::exception)
203 throw uno::RuntimeException(); // unsupported operation
206 void SAL_CALL
207 ScVbaUserForm::setValue( const OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException, std::exception)
209 uno::Any aObject = getValue( aPropertyName );
211 // in case the dialog is already closed the VBA implementation should not throw exceptions
212 if ( aObject.hasValue() )
214 // The Object *must* support XDefaultProperty here because getValue will
215 // only return properties that are Objects ( e.g. controls )
216 // e.g. Userform1.aControl = something
217 // 'aControl' has to support XDefaultProperty to make sense here
218 uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
219 OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
220 uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
221 uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( cppu::UnoType<beans::XPropertySet>::get()), uno::UNO_QUERY_THROW );
222 xPropSet->setPropertyValue( aDfltPropName, aValue );
226 uno::Reference< awt::XControl >
227 ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt::XControlContainer >& xContainer )
229 uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
230 if ( !xControl.is() )
232 uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
233 const uno::Reference< awt::XControl >* pCtrl = aControls.getConstArray();
234 const uno::Reference< awt::XControl >* pCtrlsEnd = pCtrl + aControls.getLength();
236 for ( ; pCtrl < pCtrlsEnd; ++pCtrl )
238 uno::Reference< awt::XControlContainer > xC( *pCtrl, uno::UNO_QUERY );
239 if ( xC.is() )
241 xControl.set( nestedSearch( aPropertyName, xC ) );
242 if ( xControl.is() )
243 break;
247 return xControl;
250 uno::Any SAL_CALL
251 ScVbaUserForm::getValue( const OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException, std::exception)
253 uno::Any aResult;
255 // in case the dialog is already closed the VBA implementation should not throw exceptions
256 if ( m_xDialog.is() )
258 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
259 uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
260 uno::Reference< awt::XControl > xControl = nestedSearch( aPropertyName, xContainer );
261 xContainer->getControl( aPropertyName );
262 if ( xControl.is() )
264 uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
265 ScVbaControl* pControl = dynamic_cast< ScVbaControl* >( xVBAControl.get() );
266 if (pControl && !m_sLibName.isEmpty())
267 pControl->setLibraryAndCodeName( m_sLibName.concat( "." ).concat( getName() ) );
268 aResult = uno::makeAny( xVBAControl );
272 return aResult;
275 sal_Bool SAL_CALL
276 ScVbaUserForm::hasMethod( const OUString& /*aName*/ ) throw (uno::RuntimeException, std::exception)
278 return sal_False;
280 uno::Any SAL_CALL
281 ScVbaUserForm::Controls( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
283 // if the dialog already closed we should do nothing, but the VBA will call methods of the Controls objects
284 // thus we have to provide a dummy object in this case
285 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
286 uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
287 if ( index.hasValue() )
288 return xControls->Item( index, uno::Any() );
289 return uno::makeAny( xControls );
292 sal_Bool SAL_CALL
293 ScVbaUserForm::hasProperty( const OUString& aName ) throw (uno::RuntimeException, std::exception)
295 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
297 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xControl.is() );
298 if ( xControl.is() )
300 uno::Reference< beans::XPropertySet > xDlgProps( xControl->getModel(), uno::UNO_QUERY );
301 if ( xDlgProps.is() )
303 uno::Reference< container::XNameContainer > xAllChildren( xDlgProps->getPropertyValue( "AllDialogChildren" ), uno::UNO_QUERY_THROW );
304 bool bRes = xAllChildren->hasByName( aName );
305 SAL_INFO("vbahelper", "ScVbaUserForm::hasProperty(" << aName << ") " << xAllChildren.is() << " ---> " << bRes );
306 return bRes;
309 return sal_False;
312 namespace userform
314 namespace sdecl = comphelper::service_decl;
315 sdecl::vba_service_class_<ScVbaUserForm, sdecl::with_args<true> > serviceImpl;
316 extern sdecl::ServiceDecl const serviceDecl(
317 serviceImpl,
318 "ScVbaUserForm",
319 "ooo.vba.msforms.UserForm" );
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */