Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
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"
31 :
32 : using namespace ::ooo::vba;
33 : using namespace ::com::sun::star;
34 :
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
44 :
45 0 : 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 )
46 : {
47 0 : m_xDialog.set( m_xControl, uno::UNO_QUERY_THROW );
48 0 : uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
49 0 : m_xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
50 0 : setGeometryHelper( new UserFormGeometryHelper( xContext, xControl, 0.0, 0.0 ) );
51 0 : if ( aArgs.getLength() >= 4 )
52 0 : aArgs[ 3 ] >>= m_sLibName;
53 0 : }
54 :
55 0 : ScVbaUserForm::~ScVbaUserForm()
56 : {
57 0 : }
58 :
59 : void SAL_CALL
60 0 : ScVbaUserForm::Show( ) throw (uno::RuntimeException)
61 : {
62 : OSL_TRACE("ScVbaUserForm::Show( )");
63 0 : short aRet = 0;
64 0 : mbDispose = true;
65 :
66 0 : if ( m_xDialog.is() )
67 : {
68 : // try to center dialog on model window
69 0 : if( m_xModel.is() ) try
70 : {
71 0 : uno::Reference< frame::XController > xController( m_xModel->getCurrentController(), uno::UNO_SET_THROW );
72 0 : uno::Reference< frame::XFrame > xFrame( xController->getFrame(), uno::UNO_SET_THROW );
73 0 : uno::Reference< awt::XWindow > xWindow( xFrame->getContainerWindow(), uno::UNO_SET_THROW );
74 0 : awt::Rectangle aPosSize = xWindow->getPosSize(); // already in pixel
75 :
76 0 : uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY_THROW );
77 0 : uno::Reference< awt::XWindow > xControlWindow( xControl->getPeer(), uno::UNO_QUERY_THROW );
78 0 : 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 );
79 : }
80 0 : catch( uno::Exception& )
81 : {
82 : }
83 :
84 0 : aRet = m_xDialog->execute();
85 : }
86 : OSL_TRACE("ScVbaUserForm::Show() execute returned %d", aRet);
87 0 : if ( mbDispose )
88 : {
89 : try
90 : {
91 0 : uno::Reference< lang::XComponent > xComp( m_xDialog, uno::UNO_QUERY_THROW );
92 0 : m_xDialog = NULL;
93 0 : xComp->dispose();
94 0 : mbDispose = false;
95 : }
96 0 : catch( uno::Exception& )
97 : {
98 : }
99 : }
100 0 : }
101 :
102 : OUString SAL_CALL
103 0 : ScVbaUserForm::getCaption() throw (uno::RuntimeException)
104 : {
105 0 : OUString sCaption;
106 0 : m_xProps->getPropertyValue( "Title" ) >>= sCaption;
107 0 : return sCaption;
108 : }
109 : void
110 0 : ScVbaUserForm::setCaption( const OUString& _caption ) throw (uno::RuntimeException)
111 : {
112 0 : m_xProps->setPropertyValue( "Title", uno::makeAny( _caption ) );
113 0 : }
114 :
115 0 : double SAL_CALL ScVbaUserForm::getInnerWidth() throw (uno::RuntimeException)
116 : {
117 0 : return mpGeometryHelper->getInnerWidth();
118 : }
119 :
120 0 : void SAL_CALL ScVbaUserForm::setInnerWidth( double fInnerWidth ) throw (uno::RuntimeException)
121 : {
122 0 : mpGeometryHelper->setInnerWidth( fInnerWidth );
123 0 : }
124 :
125 0 : double SAL_CALL ScVbaUserForm::getInnerHeight() throw (uno::RuntimeException)
126 : {
127 0 : return mpGeometryHelper->getInnerHeight();
128 : }
129 :
130 0 : void SAL_CALL ScVbaUserForm::setInnerHeight( double fInnerHeight ) throw (uno::RuntimeException)
131 : {
132 0 : mpGeometryHelper->setInnerHeight( fInnerHeight );
133 0 : }
134 :
135 : void SAL_CALL
136 0 : ScVbaUserForm::Hide( ) throw (uno::RuntimeException)
137 : {
138 0 : mbDispose = false; // hide not dispose
139 0 : if ( m_xDialog.is() )
140 0 : m_xDialog->endExecute();
141 0 : }
142 :
143 : void SAL_CALL
144 0 : ScVbaUserForm::RePaint( ) throw (uno::RuntimeException)
145 : {
146 : // #STUB
147 : // do nothing
148 0 : }
149 :
150 : void SAL_CALL
151 0 : ScVbaUserForm::UnloadObject( ) throw (uno::RuntimeException)
152 : {
153 0 : mbDispose = true;
154 0 : if ( m_xDialog.is() )
155 0 : m_xDialog->endExecute();
156 0 : }
157 :
158 : OUString
159 0 : ScVbaUserForm::getServiceImplName()
160 : {
161 0 : return OUString("ScVbaUserForm");
162 : }
163 :
164 : uno::Sequence< OUString >
165 0 : ScVbaUserForm::getServiceNames()
166 : {
167 0 : static uno::Sequence< OUString > aServiceNames;
168 0 : if ( aServiceNames.getLength() == 0 )
169 : {
170 0 : aServiceNames.realloc( 1 );
171 0 : aServiceNames[ 0 ] = "ooo.vba.excel.UserForm";
172 : }
173 0 : return aServiceNames;
174 : }
175 :
176 : uno::Reference< beans::XIntrospectionAccess > SAL_CALL
177 0 : ScVbaUserForm::getIntrospection( ) throw (uno::RuntimeException)
178 : {
179 0 : return uno::Reference< beans::XIntrospectionAccess >();
180 : }
181 :
182 : uno::Any SAL_CALL
183 0 : 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)
184 : {
185 0 : throw uno::RuntimeException(); // unsupported operation
186 : }
187 :
188 : void SAL_CALL
189 0 : ScVbaUserForm::setValue( const OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
190 : {
191 0 : uno::Any aObject = getValue( aPropertyName );
192 :
193 : // in case the dialog is already closed the VBA implementation should not throw exceptions
194 0 : if ( aObject.hasValue() )
195 : {
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 0 : uno::Reference< script::XDefaultProperty > xDfltProp( aObject, uno::UNO_QUERY_THROW );
201 0 : OUString aDfltPropName = xDfltProp->getDefaultPropertyName();
202 0 : uno::Reference< beans::XIntrospectionAccess > xUnoAccess( getIntrospectionAccess( aObject ) );
203 0 : uno::Reference< beans::XPropertySet > xPropSet( xUnoAccess->queryAdapter( ::getCppuType( (const uno::Reference< beans::XPropertySet > *)0 ) ), uno::UNO_QUERY_THROW );
204 0 : xPropSet->setPropertyValue( aDfltPropName, aValue );
205 0 : }
206 0 : }
207 :
208 : uno::Reference< awt::XControl >
209 0 : ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt::XControlContainer >& xContainer )
210 : {
211 0 : uno::Reference< awt::XControl > xControl = xContainer->getControl( aPropertyName );
212 0 : if ( !xControl.is() )
213 : {
214 0 : uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
215 0 : const uno::Reference< awt::XControl >* pCtrl = aControls.getConstArray();
216 0 : const uno::Reference< awt::XControl >* pCtrlsEnd = pCtrl + aControls.getLength();
217 :
218 0 : for ( ; pCtrl < pCtrlsEnd; ++pCtrl )
219 : {
220 0 : uno::Reference< awt::XControlContainer > xC( *pCtrl, uno::UNO_QUERY );
221 0 : if ( xC.is() )
222 : {
223 0 : xControl.set( nestedSearch( aPropertyName, xC ) );
224 0 : if ( xControl.is() )
225 : break;
226 : }
227 0 : }
228 : }
229 0 : return xControl;
230 : }
231 :
232 : uno::Any SAL_CALL
233 0 : ScVbaUserForm::getValue( const OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
234 : {
235 0 : uno::Any aResult;
236 :
237 : // in case the dialog is already closed the VBA implementation should not throw exceptions
238 0 : if ( m_xDialog.is() )
239 : {
240 0 : uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY_THROW );
241 0 : uno::Reference< awt::XControlContainer > xContainer( m_xDialog, uno::UNO_QUERY_THROW );
242 0 : uno::Reference< awt::XControl > xControl = nestedSearch( aPropertyName, xContainer );
243 0 : xContainer->getControl( aPropertyName );
244 0 : if ( xControl.is() )
245 : {
246 0 : uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() );
247 0 : ScVbaControl* pControl = dynamic_cast< ScVbaControl* >( xVBAControl.get() );
248 0 : if ( !m_sLibName.isEmpty() )
249 0 : pControl->setLibraryAndCodeName( m_sLibName.concat( "." ).concat( getName() ) );
250 0 : aResult = uno::makeAny( xVBAControl );
251 0 : }
252 : }
253 :
254 0 : return aResult;
255 : }
256 :
257 : ::sal_Bool SAL_CALL
258 0 : ScVbaUserForm::hasMethod( const OUString& /*aName*/ ) throw (uno::RuntimeException)
259 : {
260 0 : return sal_False;
261 : }
262 : uno::Any SAL_CALL
263 0 : ScVbaUserForm::Controls( const uno::Any& index ) throw (uno::RuntimeException)
264 : {
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 0 : uno::Reference< awt::XControl > xDialogControl( m_xDialog, uno::UNO_QUERY );
268 0 : uno::Reference< XCollection > xControls( new ScVbaControls( this, mxContext, xDialogControl, m_xModel, mpGeometryHelper->getOffsetX(), mpGeometryHelper->getOffsetY() ) );
269 0 : if ( index.hasValue() )
270 0 : return uno::makeAny( xControls->Item( index, uno::Any() ) );
271 0 : return uno::makeAny( xControls );
272 : }
273 :
274 : ::sal_Bool SAL_CALL
275 0 : ScVbaUserForm::hasProperty( const OUString& aName ) throw (uno::RuntimeException)
276 : {
277 0 : uno::Reference< awt::XControl > xControl( m_xDialog, uno::UNO_QUERY );
278 :
279 : OSL_TRACE("ScVbaUserForm::hasProperty(%s) %d", OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ).getStr(), xControl.is() );
280 0 : if ( xControl.is() )
281 : {
282 0 : uno::Reference< beans::XPropertySet > xDlgProps( xControl->getModel(), uno::UNO_QUERY );
283 0 : if ( xDlgProps.is() )
284 : {
285 0 : uno::Reference< container::XNameContainer > xAllChildren( xDlgProps->getPropertyValue( "AllDialogChildren" ), uno::UNO_QUERY_THROW );
286 0 : sal_Bool bRes = xAllChildren->hasByName( aName );
287 : OSL_TRACE("ScVbaUserForm::hasProperty(%s) %d ---> %d", OUStringToOString( aName, RTL_TEXTENCODING_UTF8 ).getStr(), xAllChildren.is(), bRes );
288 0 : return bRes;
289 0 : }
290 : }
291 0 : return sal_False;
292 : }
293 :
294 : namespace userform
295 : {
296 : namespace sdecl = comphelper::service_decl;
297 0 : sdecl::vba_service_class_<ScVbaUserForm, sdecl::with_args<true> > serviceImpl;
298 0 : extern sdecl::ServiceDecl const serviceDecl(
299 : serviceImpl,
300 : "ScVbaUserForm",
301 : "ooo.vba.msforms.UserForm" );
302 0 : }
303 :
304 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|