xref: /AOO41X/main/vbahelper/source/msforms/vbauserform.cxx (revision e6ed5fbc51cf474df369618b58e945b84a21a167)
1*e6ed5fbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e6ed5fbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e6ed5fbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e6ed5fbcSAndrew Rist  * distributed with this work for additional information
6*e6ed5fbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e6ed5fbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e6ed5fbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*e6ed5fbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*e6ed5fbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*e6ed5fbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e6ed5fbcSAndrew Rist  * software distributed under the License is distributed on an
15*e6ed5fbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e6ed5fbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*e6ed5fbcSAndrew Rist  * specific language governing permissions and limitations
18*e6ed5fbcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*e6ed5fbcSAndrew Rist  *************************************************************/
21*e6ed5fbcSAndrew Rist 
22*e6ed5fbcSAndrew Rist 
23cdf0e10cSrcweir #include <vbahelper/helperdecl.hxx>
24cdf0e10cSrcweir #include "vbauserform.hxx"
25cdf0e10cSrcweir #include <com/sun/star/awt/XControl.hpp>
26cdf0e10cSrcweir #include <com/sun/star/awt/XControlContainer.hpp>
27cdf0e10cSrcweir #include <com/sun/star/awt/PosSize.hpp>
28cdf0e10cSrcweir #include <com/sun/star/beans/PropertyConcept.hpp>
29cdf0e10cSrcweir #include <com/sun/star/util/MeasureUnit.hpp>
30cdf0e10cSrcweir #include <basic/sbx.hxx>
31cdf0e10cSrcweir #include <basic/sbstar.hxx>
32cdf0e10cSrcweir #include <basic/sbmeth.hxx>
33cdf0e10cSrcweir #include "vbacontrols.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::ooo::vba;
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir // some little notes
39cdf0e10cSrcweir // XDialog implementation has the following interesting bits
40cdf0e10cSrcweir // a Controls property ( which is an array of the container controls )
41cdf0e10cSrcweir //   each item in the controls array is a XControl, where the model is
42cdf0e10cSrcweir //   basically a property bag
43cdf0e10cSrcweir // additionally the XDialog instance has itself a model
44cdf0e10cSrcweir //     this model has a ControlModels ( array of models ) property
45cdf0e10cSrcweir //     the models in ControlModels can be accessed by name
46cdf0e10cSrcweir // also the XDialog is a XControl ( to access the model above
47cdf0e10cSrcweir 
ScVbaUserForm(uno::Sequence<uno::Any> const & aArgs,uno::Reference<uno::XComponentContext> const & xContext)48cdf0e10cSrcweir 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 )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     m_xDialog.set( m_xControl, uno::UNO_QUERY_THROW );
51cdf0e10cSrcweir     uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
52cdf0e10cSrcweir     m_xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
53cdf0e10cSrcweir     setGeometryHelper( new UserFormGeometryHelper( xContext, xControl, 0.0, 0.0 ) );
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
~ScVbaUserForm()56cdf0e10cSrcweir ScVbaUserForm::~ScVbaUserForm()
57cdf0e10cSrcweir {
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir void SAL_CALL
Show()61cdf0e10cSrcweir ScVbaUserForm::Show(  ) throw (uno::RuntimeException)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	OSL_TRACE("ScVbaUserForm::Show(  )");
64cdf0e10cSrcweir 	short aRet = 0;
65cdf0e10cSrcweir     mbDispose = true;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	if ( m_xDialog.is() )
68cdf0e10cSrcweir 	{
69cdf0e10cSrcweir         // try to center dialog on model window
70cdf0e10cSrcweir         if( m_xModel.is() ) try
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             uno::Reference< frame::XController > xController( m_xModel->getCurrentController(), uno::UNO_SET_THROW );
73cdf0e10cSrcweir             uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
74cdf0e10cSrcweir             uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
75cdf0e10cSrcweir             awt::Rectangle aPosSize = xWindow->getPosSize();    // already in pixel
76cdf0e10cSrcweir 
77cdf0e10cSrcweir             uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
78cdf0e10cSrcweir             uno::Reference< awt::XWindow > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
79cdf0e10cSrcweir             xControlWindow->setPosSize( (aPosSize.Width - getWidth()) / 2.0, (aPosSize.Height - getHeight()) / 2.0, 0, 0, awt::PosSize::POS );
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir         catch( uno::Exception& )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 		aRet = m_xDialog->execute();
86cdf0e10cSrcweir 	}
87cdf0e10cSrcweir 	OSL_TRACE("ScVbaUserForm::Show() execute returned %d", aRet);
88cdf0e10cSrcweir 	if ( mbDispose )
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		try
91cdf0e10cSrcweir 		{
92cdf0e10cSrcweir 			uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY_THROW );
93cdf0e10cSrcweir 			m_xDialog = NULL;
94cdf0e10cSrcweir 			xComp->dispose();
95cdf0e10cSrcweir 			mbDispose = false;
96cdf0e10cSrcweir 		}
97cdf0e10cSrcweir 		catch( uno::Exception& )
98cdf0e10cSrcweir 		{
99cdf0e10cSrcweir 		}
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir rtl::OUString SAL_CALL
getCaption()104cdf0e10cSrcweir ScVbaUserForm::getCaption() throw (uno::RuntimeException)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     rtl::OUString sCaption;
107cdf0e10cSrcweir     m_xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Title") ) ) >>= sCaption;
108cdf0e10cSrcweir     return sCaption;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir void
setCaption(const::rtl::OUString & _caption)111cdf0e10cSrcweir ScVbaUserForm::setCaption( const ::rtl::OUString& _caption ) throw (uno::RuntimeException)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     m_xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Title") ), uno::makeAny( _caption ) );
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
getInnerWidth()116cdf0e10cSrcweir double SAL_CALL ScVbaUserForm::getInnerWidth() throw (uno::RuntimeException)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     return mpGeometryHelper->getInnerWidth();
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
setInnerWidth(double fInnerWidth)121cdf0e10cSrcweir void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth ) throw (uno::RuntimeException)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     mpGeometryHelper->setInnerWidth( fInnerWidth );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
getInnerHeight()126cdf0e10cSrcweir double SAL_CALL ScVbaUserForm::getInnerHeight() throw (uno::RuntimeException)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     return mpGeometryHelper->getInnerHeight();
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
setInnerHeight(double fInnerHeight)131cdf0e10cSrcweir void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight ) throw (uno::RuntimeException)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     mpGeometryHelper->setInnerHeight( fInnerHeight );
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir void SAL_CALL
Hide()137cdf0e10cSrcweir ScVbaUserForm::Hide(  ) throw (uno::RuntimeException)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	mbDispose = false;  // hide not dispose
140cdf0e10cSrcweir 	if ( m_xDialog.is() )
141cdf0e10cSrcweir 		m_xDialog->endExecute();
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir void SAL_CALL
RePaint()145cdf0e10cSrcweir ScVbaUserForm::RePaint(  ) throw (uno::RuntimeException)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir 	// do nothing
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir void SAL_CALL
UnloadObject()151cdf0e10cSrcweir ScVbaUserForm::UnloadObject(  ) throw (uno::RuntimeException)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir 	mbDispose = true;
154cdf0e10cSrcweir 	if ( m_xDialog.is() )
155cdf0e10cSrcweir 		m_xDialog->endExecute();
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir rtl::OUString&
getServiceImplName()159cdf0e10cSrcweir ScVbaUserForm::getServiceImplName()
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaUserForm") );
162cdf0e10cSrcweir 	return sImplName;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()166cdf0e10cSrcweir ScVbaUserForm::getServiceNames()
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
169cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
170cdf0e10cSrcweir 	{
171cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
172cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.UserForm" ) );
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir 	return aServiceNames;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir uno::Reference< beans::XIntrospectionAccess > SAL_CALL
getIntrospection()178cdf0e10cSrcweir ScVbaUserForm::getIntrospection(  ) throw (uno::RuntimeException)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	return uno::Reference< beans::XIntrospectionAccess >();
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir uno::Any SAL_CALL
invoke(const::rtl::OUString &,const uno::Sequence<uno::Any> &,uno::Sequence<::sal_Int16> &,uno::Sequence<uno::Any> &)184cdf0e10cSrcweir ScVbaUserForm::invoke( const ::rtl::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)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	throw uno::RuntimeException(); // unsupported operation
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
189cdf0e10cSrcweir void SAL_CALL
setValue(const::rtl::OUString & aPropertyName,const uno::Any & aValue)190cdf0e10cSrcweir ScVbaUserForm::setValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir 	uno::Any aObject = getValue( aPropertyName );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir     // in case the dialog is already closed the VBA implementation should not throw exceptions
195cdf0e10cSrcweir     if ( aObject.hasValue() )
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         // The Object *must* support XDefaultProperty here because getValue will
198cdf0e10cSrcweir         // only return properties that are Objects ( e.g. controls )
199cdf0e10cSrcweir         // e.g. Userform1.aControl = something
200cdf0e10cSrcweir         // 'aControl' has to support XDefaultProperty to make sense here
201cdf0e10cSrcweir         uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
202cdf0e10cSrcweir         rtl::OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
203cdf0e10cSrcweir         uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
204cdf0e10cSrcweir         uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( ::getCppuType( (const uno::Reference< beans::XPropertySet > *)0 ) ), uno::UNO_QUERY_THROW );
205cdf0e10cSrcweir         xPropSet->setPropertyValue( aDfltPropName, aValue );
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir uno::Any SAL_CALL
getValue(const::rtl::OUString & aPropertyName)210cdf0e10cSrcweir ScVbaUserForm::getValue( const ::rtl::OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
211cdf0e10cSrcweir {
212cdf0e10cSrcweir     uno::Any aResult;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     // in case the dialog is already closed the VBA implementation should not throw exceptions
215cdf0e10cSrcweir     if ( m_xDialog.is() )
216cdf0e10cSrcweir     {
217cdf0e10cSrcweir         uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
218cdf0e10cSrcweir         uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
219cdf0e10cSrcweir         uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
220cdf0e10cSrcweir         if ( xControl.is() )
221cdf0e10cSrcweir             aResult <<= ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir     return aResult;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir ::sal_Bool SAL_CALL
hasMethod(const::rtl::OUString &)228cdf0e10cSrcweir ScVbaUserForm::hasMethod( const ::rtl::OUString& /*aName*/ ) throw (uno::RuntimeException)
229cdf0e10cSrcweir {
230cdf0e10cSrcweir 	return sal_False;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir uno::Any SAL_CALL
Controls(const uno::Any & index)233cdf0e10cSrcweir ScVbaUserForm::Controls( const uno::Any& index ) throw (uno::RuntimeException)
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     // if the dialog already closed we should do nothing, but the VBA will call methods of the Controls objects
236cdf0e10cSrcweir     // thus we have to provide a dummy object in this case
237cdf0e10cSrcweir 	uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
238cdf0e10cSrcweir 	uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
239cdf0e10cSrcweir 	if ( index.hasValue() )
240cdf0e10cSrcweir 		return uno::makeAny( xControls->Item( index, uno::Any() ) );
241cdf0e10cSrcweir 	return uno::makeAny( xControls );
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir ::sal_Bool SAL_CALL
hasProperty(const::rtl::OUString & aName)245cdf0e10cSrcweir ScVbaUserForm::hasProperty( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
246cdf0e10cSrcweir {
247cdf0e10cSrcweir 	uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
248cdf0e10cSrcweir 	OSL_TRACE("ScVbaUserForm::hasProperty(%s) %d", rtl::OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ).getStr(), xControl.is() );
249cdf0e10cSrcweir 	if ( xControl.is() )
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir 		uno::Reference< container::XNameAccess > xNameAccess( xControl->getModel(), uno::UNO_QUERY_THROW );
252cdf0e10cSrcweir 		sal_Bool bRes =  xNameAccess->hasByName( aName );
253cdf0e10cSrcweir 	OSL_TRACE("ScVbaUserForm::hasProperty(%s) %d ---> %d", rtl::OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ).getStr(), xControl.is(), bRes );
254cdf0e10cSrcweir 		return bRes;
255cdf0e10cSrcweir 	}
256cdf0e10cSrcweir 	return sal_False;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir namespace userform
260cdf0e10cSrcweir {
261cdf0e10cSrcweir namespace sdecl = comphelper::service_decl;
262cdf0e10cSrcweir sdecl::vba_service_class_<ScVbaUserForm, sdecl::with_args<true> > serviceImpl;
263cdf0e10cSrcweir extern sdecl::ServiceDecl const serviceDecl(
264cdf0e10cSrcweir     serviceImpl,
265cdf0e10cSrcweir     "ScVbaUserForm",
266cdf0e10cSrcweir     "ooo.vba.msforms.UserForm" );
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
269