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