Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / vbahelper / source / msforms / vbauserform.cxx
blob988e837482233655e8f383a47a8b4b70160c8e9a
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/PosSize.hpp>
24 #include <com/sun/star/beans/PropertyConcept.hpp>
25 #include <com/sun/star/container/XNameContainer.hpp>
26 #include <com/sun/star/util/MeasureUnit.hpp>
27 #include <basic/sbx.hxx>
28 #include <basic/sbstar.hxx>
29 #include <basic/sbmeth.hxx>
30 #include "vbacontrols.hxx"
32 using namespace ::ooo::vba;
33 using namespace ::com::sun::star;
35 // some little notes
36 // XDialog implementation has the following interesting bits
37 // a Controls property ( which is an array of the container controls )
38 // each item in the controls array is a XControl, where the model is
39 // basically a property bag
40 // additionally the XDialog instance has itself a model
41 // this model has a ControlModels ( array of models ) property
42 // the models in ControlModels can be accessed by name
43 // also the XDialog is a XControl ( to access the model above
45 ScVbaUserForm::ScVbaUserForm( uno::Sequence< uno::Any > const& aArgs, uno::Reference< uno::XComponentContext >const& xContext ) throw ( lang::IllegalArgumentException ) : 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 )
47 m_xDialog.set( m_xControl, uno::UNO_QUERY_THROW );
48 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
49 m_xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
50 setGeometryHelper( new UserFormGeometryHelper( xContext, xControl, 0.0, 0.0 ) );
51 if ( aArgs.getLength() >= 4 )
52 aArgs[ 3 ] >>= m_sLibName;
55 ScVbaUserForm::~ScVbaUserForm()
59 void SAL_CALL
60 ScVbaUserForm::Show( ) throw (uno::RuntimeException)
62 OSL_TRACE("ScVbaUserForm::Show( )");
63 short aRet = 0;
64 mbDispose = true;
66 if ( m_xDialog.is() )
68 // try to center dialog on model window
69 if( m_xModel.is() ) try
71 uno::Reference< frame::XController > xController( m_xModel->getCurrentController(), uno::UNO_SET_THROW );
72 uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
73 uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
74 awt::Rectangle aPosSize = xWindow->getPosSize(); // already in pixel
76 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
77 uno::Reference< awt::XWindow > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
78 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 catch( uno::Exception& )
84 aRet = m_xDialog->execute();
86 OSL_TRACE("ScVbaUserForm::Show() execute returned %d", aRet);
87 if ( mbDispose )
89 try
91 uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY_THROW );
92 m_xDialog = NULL;
93 xComp->dispose();
94 mbDispose = false;
96 catch( uno::Exception& )
102 OUString SAL_CALL
103 ScVbaUserForm::getCaption() throw (uno::RuntimeException)
105 OUString sCaption;
106 m_xProps->getPropertyValue( "Title" ) >>= sCaption;
107 return sCaption;
109 void
110 ScVbaUserForm::setCaption( const OUString& _caption ) throw (uno::RuntimeException)
112 m_xProps->setPropertyValue( "Title", uno::makeAny( _caption ) );
115 double SAL_CALL ScVbaUserForm::getInnerWidth() throw (uno::RuntimeException)
117 return mpGeometryHelper->getInnerWidth();
120 void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth ) throw (uno::RuntimeException)
122 mpGeometryHelper->setInnerWidth( fInnerWidth );
125 double SAL_CALL ScVbaUserForm::getInnerHeight() throw (uno::RuntimeException)
127 return mpGeometryHelper->getInnerHeight();
130 void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight ) throw (uno::RuntimeException)
132 mpGeometryHelper->setInnerHeight( fInnerHeight );
135 void SAL_CALL
136 ScVbaUserForm::Hide( ) throw (uno::RuntimeException)
138 mbDispose = false; // hide not dispose
139 if ( m_xDialog.is() )
140 m_xDialog->endExecute();
143 void SAL_CALL
144 ScVbaUserForm::RePaint( ) throw (uno::RuntimeException)
146 // #STUB
147 // do nothing
150 void SAL_CALL
151 ScVbaUserForm::UnloadObject( ) throw (uno::RuntimeException)
153 mbDispose = true;
154 if ( m_xDialog.is() )
155 m_xDialog->endExecute();
158 OUString
159 ScVbaUserForm::getServiceImplName()
161 return OUString("ScVbaUserForm");
164 uno::Sequence< OUString >
165 ScVbaUserForm::getServiceNames()
167 static uno::Sequence< OUString > aServiceNames;
168 if ( aServiceNames.getLength() == 0 )
170 aServiceNames.realloc( 1 );
171 aServiceNames[ 0 ] = "ooo.vba.excel.UserForm";
173 return aServiceNames;
176 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
177 ScVbaUserForm::getIntrospection( ) throw (uno::RuntimeException)
179 return uno::Reference< beans::XIntrospectionAccess >();
182 uno::Any SAL_CALL
183 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)
185 throw uno::RuntimeException(); // unsupported operation
188 void SAL_CALL
189 ScVbaUserForm::setValue( const OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
191 uno::Any aObject = getValue( aPropertyName );
193 // in case the dialog is already closed the VBA implementation should not throw exceptions
194 if ( aObject.hasValue() )
196 // The Object *must* support XDefaultProperty here because getValue will
197 // only return properties that are Objects ( e.g. controls )
198 // e.g. Userform1.aControl = something
199 // 'aControl' has to support XDefaultProperty to make sense here
200 uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
201 OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
202 uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
203 uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( ::getCppuType( (const uno::Reference< beans::XPropertySet > *)0 ) ), uno::UNO_QUERY_THROW );
204 xPropSet->setPropertyValue( aDfltPropName, aValue );
208 uno::Reference< awt::XControl >
209 ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt::XControlContainer >& xContainer )
211 uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
212 if ( !xControl.is() )
214 uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
215 const uno::Reference< awt::XControl >* pCtrl = aControls.getConstArray();
216 const uno::Reference< awt::XControl >* pCtrlsEnd = pCtrl + aControls.getLength();
218 for ( ; pCtrl < pCtrlsEnd; ++pCtrl )
220 uno::Reference< awt::XControlContainer > xC( *pCtrl, uno::UNO_QUERY );
221 if ( xC.is() )
223 xControl.set( nestedSearch( aPropertyName, xC ) );
224 if ( xControl.is() )
225 break;
229 return xControl;
232 uno::Any SAL_CALL
233 ScVbaUserForm::getValue( const OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
235 uno::Any aResult;
237 // in case the dialog is already closed the VBA implementation should not throw exceptions
238 if ( m_xDialog.is() )
240 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
241 uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
242 uno::Reference< awt::XControl > xControl = nestedSearch( aPropertyName, xContainer );
243 xContainer->getControl( aPropertyName );
244 if ( xControl.is() )
246 uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
247 ScVbaControl* pControl = dynamic_cast< ScVbaControl* >( xVBAControl.get() );
248 if ( !m_sLibName.isEmpty() )
249 pControl->setLibraryAndCodeName( m_sLibName.concat( "." ).concat( getName() ) );
250 aResult = uno::makeAny( xVBAControl );
254 return aResult;
257 ::sal_Bool SAL_CALL
258 ScVbaUserForm::hasMethod( const OUString& /*aName*/ ) throw (uno::RuntimeException)
260 return sal_False;
262 uno::Any SAL_CALL
263 ScVbaUserForm::Controls( const uno::Any& index ) throw (uno::RuntimeException)
265 // if the dialog already closed we should do nothing, but the VBA will call methods of the Controls objects
266 // thus we have to provide a dummy object in this case
267 uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
268 uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
269 if ( index.hasValue() )
270 return uno::makeAny( xControls->Item( index, uno::Any() ) );
271 return uno::makeAny( xControls );
274 ::sal_Bool SAL_CALL
275 ScVbaUserForm::hasProperty( const OUString& aName ) throw (uno::RuntimeException)
277 uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
279 OSL_TRACE("ScVbaUserForm::hasProperty(%s) %d", OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ).getStr(), xControl.is() );
280 if ( xControl.is() )
282 uno::Reference< beans::XPropertySet > xDlgProps( xControl->getModel(), uno::UNO_QUERY );
283 if ( xDlgProps.is() )
285 uno::Reference< container::XNameContainer > xAllChildren( xDlgProps->getPropertyValue( "AllDialogChildren" ), uno::UNO_QUERY_THROW );
286 sal_Bool bRes = xAllChildren->hasByName( aName );
287 OSL_TRACE("ScVbaUserForm::hasProperty(%s) %d ---> %d", OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ).getStr(), xAllChildren.is(), bRes );
288 return bRes;
291 return sal_False;
294 namespace userform
296 namespace sdecl = comphelper::service_decl;
297 sdecl::vba_service_class_<ScVbaUserForm, sdecl::with_args<true> > serviceImpl;
298 extern sdecl::ServiceDecl const serviceDecl(
299 serviceImpl,
300 "ScVbaUserForm",
301 "ooo.vba.msforms.UserForm" );
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */