1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <com/sun/star/awt/XControl.hpp>
25 #include <com/sun/star/awt/XControlContainer.hpp>
26 #include <com/sun/star/awt/FontWeight.hpp>
27 #include <com/sun/star/awt/FontSlant.hpp>
28 #include <com/sun/star/awt/FontStrikeout.hpp>
29 #include <com/sun/star/awt/FontUnderline.hpp>
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/script/XInvocation.hpp>
32 #include <com/sun/star/lang/WrappedTargetException.hpp>
33 
34 #include "vbacontrols.hxx"
35 #include "vbacontrol.hxx"
36 #include <cppuhelper/implbase2.hxx>
37 #include <ooo/vba/XControlProvider.hpp>
38 #include <hash_map>
39 
40 using namespace com::sun::star;
41 using namespace ooo::vba;
42 
43 
44 typedef ::cppu::WeakImplHelper2< container::XNameAccess, container::XIndexAccess > ArrayWrapImpl;
45 
46 typedef  std::hash_map< rtl::OUString, sal_Int32, ::rtl::OUStringHash,
47     ::std::equal_to< ::rtl::OUString >  > ControlIndexMap;
48 typedef  std::vector< uno::Reference< awt::XControl > > ControlVec;
49 
50 class ControlArrayWrapper : public ArrayWrapImpl
51 {
52     uno::Reference< awt::XControlContainer > mxDialog;
53     uno::Sequence< ::rtl::OUString > msNames;
54     ControlVec mControls;
55     ControlIndexMap mIndices;
56 
57 private:
SetArrayElementTo(const uno::Reference<awt::XControl> & xCtrl,sal_Int32 nIndex=-1)58     void SetArrayElementTo( const uno::Reference< awt::XControl >& xCtrl, sal_Int32 nIndex = -1 )
59     {
60         // initialize the element with specified index to the control
61         if ( xCtrl.is() )
62         {
63             if ( nIndex == -1 )
64                 nIndex = msNames.getLength();
65 
66             if ( nIndex >= msNames.getLength() )
67                 msNames.realloc( nIndex );
68 
69             msNames[ nIndex ] = getControlName( xCtrl );
70             mControls.push_back( xCtrl );
71             mIndices[ msNames[ nIndex ] ] = nIndex;
72         }
73     }
74 
75 public:
ControlArrayWrapper(const uno::Reference<awt::XControl> & xDialog)76     ControlArrayWrapper( const uno::Reference< awt::XControl >& xDialog )
77     {
78         try
79         {
80             mxDialog.set( xDialog, uno::UNO_QUERY_THROW );
81             uno::Sequence< uno::Reference< awt::XControl > > sXControls = mxDialog->getControls();
82 
83             msNames.realloc( sXControls.getLength() );
84             for ( sal_Int32 i = 0; i < sXControls.getLength(); ++i )
85                 SetArrayElementTo( sXControls[ i ], i );
86         }
87         catch( uno::Exception& )
88         {
89             // accept the case when the dialog already does not exist
90             // in this case the wrapper should work in dummy mode
91         }
92     }
93 
getControlName(const uno::Reference<awt::XControl> & xCtrl)94     static rtl::OUString getControlName( const uno::Reference< awt::XControl >& xCtrl )
95     {
96         if ( !xCtrl.is() )
97             throw uno::RuntimeException();
98 
99         uno::Reference< beans::XPropertySet > xProp( xCtrl->getModel(), uno::UNO_QUERY_THROW );
100         rtl::OUString sName;
101         xProp->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ) ) >>= sName;
102         return sName;
103     }
104 
105 
106     // XElementAccess
getElementType()107     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
108     {
109         return awt::XControl::static_type(0);
110     }
111 
hasElements()112     virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException)
113     {
114         return ( mControls.size() > 0 );
115     }
116 
117     // XNameAcess
getByName(const::rtl::OUString & aName)118     virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
119     {
120         if ( !hasByName( aName ) )
121             throw container::NoSuchElementException();
122         return getByIndex( mIndices[ aName ] );
123     }
124 
getElementNames()125     virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException)
126     {
127         return msNames;
128     }
129 
hasByName(const::rtl::OUString & aName)130     virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (css::uno::RuntimeException)
131     {
132         ControlIndexMap::iterator it = mIndices.find( aName );
133         return it != mIndices.end();
134     }
135 
136     // XElementAccess
getCount()137     virtual ::sal_Int32 SAL_CALL getCount(  ) throw (css::uno::RuntimeException)
138     {
139         return mControls.size();
140     }
141 
getByIndex(::sal_Int32 Index)142     virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
143     {
144         if ( Index < 0 || Index >= static_cast< sal_Int32 >( mControls.size() ) )
145             throw lang::IndexOutOfBoundsException();
146         return uno::makeAny( mControls[ Index ] );
147     }
148 };
149 
150 
151 class ControlsEnumWrapper : public EnumerationHelper_BASE
152 {
153     uno::Reference<XHelperInterface > m_xParent;
154     uno::Reference<uno::XComponentContext > m_xContext;
155     uno::Reference<container::XIndexAccess > m_xIndexAccess;
156     uno::Reference<awt::XControl > m_xDlg;
157     uno::Reference< frame::XModel > m_xModel;
158     double mfOffsetX;
159     double mfOffsetY;
160     sal_Int32 nIndex;
161 
162 public:
163 
ControlsEnumWrapper(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XIndexAccess> & xIndexAccess,const uno::Reference<awt::XControl> & xDlg,const uno::Reference<frame::XModel> & xModel,double fOffsetX,double fOffsetY)164     ControlsEnumWrapper(
165         const uno::Reference< XHelperInterface >& xParent,
166         const uno::Reference< uno::XComponentContext >& xContext,
167         const uno::Reference< container::XIndexAccess >& xIndexAccess,
168         const uno::Reference< awt::XControl >& xDlg,
169         const uno::Reference< frame::XModel >& xModel,
170         double fOffsetX, double fOffsetY ) :
171     m_xParent( xParent ),
172     m_xContext( xContext),
173     m_xIndexAccess( xIndexAccess ),
174     m_xDlg( xDlg ),
175     m_xModel( xModel ),
176     mfOffsetX( fOffsetX ),
177     mfOffsetY( fOffsetY ),
178     nIndex( 0 ) {}
179 
hasMoreElements()180     virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
181     {
182         return ( nIndex < m_xIndexAccess->getCount() );
183     }
184 
nextElement()185     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
186     {
187         if ( nIndex < m_xIndexAccess->getCount() )
188         {
189             uno::Reference< awt::XControl > xControl;
190             m_xIndexAccess->getByIndex( nIndex++ ) >>= xControl;
191 
192             uno::Reference< msforms::XControl > xVBAControl;
193             if ( xControl.is() && m_xDlg.is() )
194             	xVBAControl = ScVbaControlFactory::createUserformControl( m_xContext, xControl, m_xDlg, m_xModel, mfOffsetX, mfOffsetY );
195             return uno::makeAny( xVBAControl );
196         }
197         throw container::NoSuchElementException();
198     }
199 
200 };
201 
202 
203 uno::Reference<container::XIndexAccess >
lcl_controlsWrapper(const uno::Reference<awt::XControl> & xDlg)204 lcl_controlsWrapper( const uno::Reference< awt::XControl >& xDlg )
205 {
206     return new ControlArrayWrapper( xDlg );
207 }
208 
ScVbaControls(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const css::uno::Reference<awt::XControl> & xDialog,const uno::Reference<frame::XModel> & xModel,double fOffsetX,double fOffsetY)209 ScVbaControls::ScVbaControls(
210         const uno::Reference< XHelperInterface >& xParent,
211         const uno::Reference< uno::XComponentContext >& xContext,
212         const css::uno::Reference< awt::XControl >& xDialog,
213         const uno::Reference< frame::XModel >& xModel,
214         double fOffsetX, double fOffsetY ) :
215     ControlsImpl_BASE( xParent, xContext, lcl_controlsWrapper( xDialog  ) ),
216     mxDialog( xDialog ),
217     mxModel( xModel ),
218     mfOffsetX( fOffsetX ),
219     mfOffsetY( fOffsetY )
220 {
221 }
222 
223 uno::Reference< container::XEnumeration >
createEnumeration()224 ScVbaControls::createEnumeration() throw (uno::RuntimeException)
225 {
226     uno::Reference< container::XEnumeration > xEnum( new ControlsEnumWrapper( mxParent, mxContext, m_xIndexAccess, mxDialog, mxModel, mfOffsetX, mfOffsetY ) );
227     if ( !xEnum.is() )
228         throw uno::RuntimeException();
229     return xEnum;
230 }
231 
232 uno::Any
createCollectionObject(const css::uno::Any & aSource)233 ScVbaControls::createCollectionObject( const css::uno::Any& aSource )
234 {
235     // Create control from awt::XControl
236     uno::Reference< awt::XControl > xControl( aSource, uno::UNO_QUERY_THROW );
237 	uno::Reference< msforms::XControl > xVBAControl = ScVbaControlFactory::createUserformControl( mxContext, xControl, mxDialog, mxModel, mfOffsetX, mfOffsetY );
238     return uno::Any( xVBAControl );
239 }
240 
241 void SAL_CALL
Move(double cx,double cy)242 ScVbaControls::Move( double cx, double cy ) throw (uno::RuntimeException)
243 {
244     uno::Reference< container::XEnumeration > xEnum( createEnumeration() );
245     while ( xEnum->hasMoreElements() )
246     {
247         uno::Reference< msforms::XControl > xControl( xEnum->nextElement(), uno::UNO_QUERY_THROW );
248         xControl->setLeft( xControl->getLeft() + cx );
249         xControl->setTop( xControl->getTop() + cy );
250     }
251 }
252 
Add(const uno::Any & Object,const uno::Any & StringKey,const uno::Any &,const uno::Any &)253 uno::Any SAL_CALL ScVbaControls::Add( const uno::Any& Object, const uno::Any& StringKey, const uno::Any& /*Before*/, const uno::Any& /*After*/ )
254     throw (uno::RuntimeException)
255 {
256     uno::Any aResult;
257     ::rtl::OUString aComServiceName;
258 
259     try
260     {
261         if ( !mxDialog.is() )
262             throw uno::RuntimeException();
263 
264         uno::Reference< awt::XControl > xNewControl;
265         uno::Reference< lang::XMultiServiceFactory > xModelFactory( mxDialog->getModel(), uno::UNO_QUERY_THROW );
266 
267         uno::Reference< container::XNameContainer > xDialogContainer( xModelFactory, uno::UNO_QUERY_THROW );
268 
269         Object >>= aComServiceName;
270 
271         // TODO: Support Before and After?
272         ::rtl::OUString aNewName;
273         StringKey >>= aNewName;
274         if ( !aNewName.getLength() )
275         {
276             aNewName = aComServiceName;
277             if ( !aNewName.getLength() )
278                 aNewName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Control" ) );
279 
280             sal_Int32 nInd = 0;
281             while( xDialogContainer->hasByName( aNewName ) && (nInd < SAL_MAX_INT32) )
282             {
283                 aNewName = aComServiceName;
284                 aNewName += ::rtl::OUString::valueOf( nInd++ );
285             }
286         }
287 
288         double fDefWidth = 72.0, fDefHeight = 18.0;
289         if ( aComServiceName.getLength() )
290         {
291             // create a UNO control model based on the passed control type
292             uno::Reference< awt::XControlModel > xNewModel;
293             bool bFontSupport = false;
294             bool bNativeAX = false;
295             if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.CommandButton.1" ) ) )
296             {
297                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ) ), uno::UNO_QUERY_THROW );
298                 fDefWidth = 72.0; fDefHeight = 24.0;
299                 bFontSupport = true;
300             }
301             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.Label.1" ) ) )
302             {
303                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFixedTextModel" ) ) ), uno::UNO_QUERY_THROW );
304                 fDefWidth = 72.0; fDefHeight = 18.0;
305                 bFontSupport = true;
306             }
307             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.Image.1" ) ) )
308             {
309                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlImageControlModel" ) ) ), uno::UNO_QUERY_THROW );
310                 fDefWidth = 72.0; fDefHeight = 72.0;
311             }
312             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.CheckBox.1" ) ) )
313             {
314                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlCheckBoxModel" ) ) ), uno::UNO_QUERY_THROW );
315                 fDefWidth = 108.0; fDefHeight = 18.0;
316                 bFontSupport = true;
317             }
318             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.OptionButton.1" ) ) )
319             {
320                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlRadioButtonModel" ) ) ), uno::UNO_QUERY_THROW );
321                 fDefWidth = 108.0; fDefHeight = 18.0;
322                 bFontSupport = true;
323             }
324             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.TextBox.1" ) ) )
325             {
326                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlEditModel" ) ) ), uno::UNO_QUERY_THROW );
327                 fDefWidth = 72.0; fDefHeight = 18.0;
328                 bFontSupport = true;
329             }
330             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.ListBox.1" ) ) )
331             {
332                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlListBoxModel" ) ) ), uno::UNO_QUERY_THROW );
333                 fDefWidth = 72.0; fDefHeight = 18.0;
334                 bFontSupport = true;
335             }
336             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.ComboBox.1" ) ) )
337             {
338                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlComboBoxModel" ) ) ), uno::UNO_QUERY_THROW );
339                 uno::Reference< beans::XPropertySet > xProps( xNewModel, uno::UNO_QUERY_THROW );
340                 xProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Dropdown" ) ), uno::Any( true ) );
341                 fDefWidth = 72.0; fDefHeight = 18.0;
342                 bFontSupport = true;
343             }
344             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.ToggleButton.1" ) ) )
345             {
346                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ) ), uno::UNO_QUERY_THROW );
347                 uno::Reference< beans::XPropertySet > xProps( xNewModel, uno::UNO_QUERY_THROW );
348                 xProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Toggle" ) ), uno::Any( true ) );
349                 fDefWidth = 72.0; fDefHeight = 18.0;
350                 bFontSupport = true;
351             }
352             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.Frame.1" ) ) )
353             {
354                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlGroupBoxModel" ) ) ), uno::UNO_QUERY_THROW );
355                 fDefWidth = 216.0; fDefHeight = 144.0;
356                 bFontSupport = true;
357             }
358             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.SpinButton.1" ) ) )
359             {
360                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlSpinButtonModel" ) ) ), uno::UNO_QUERY_THROW );
361                 fDefWidth = 12.75; fDefHeight = 25.5;
362             }
363             else if( aComServiceName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "Forms.ScrollBar.1" ) ) )
364             {
365                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlScrollBarModel" ) ) ), uno::UNO_QUERY_THROW );
366                 fDefWidth = 12.75; fDefHeight = 63.8;
367             }
368             else
369             {
370                 xNewModel.set( xModelFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ) ) ), uno::UNO_QUERY_THROW );
371                 fDefWidth = 72.0; fDefHeight = 18.0;
372                 bNativeAX = true;
373             }
374 
375             // need to set a few font properties to get rid of the default DONT_KNOW values
376             if( bFontSupport )
377             {
378                 uno::Reference< beans::XPropertySet > xModelProps( xNewModel, uno::UNO_QUERY_THROW );
379                 xModelProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontName" ) ), uno::Any( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Tahoma" ) ) ) );
380                 xModelProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontHeight" ) ), uno::Any( float( 8.0 ) ) );
381                 xModelProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontWeight" ) ), uno::Any( awt::FontWeight::NORMAL ) );
382                 xModelProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontSlant" ) ), uno::Any( awt::FontSlant_NONE ) );
383                 xModelProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontUnderline" ) ), uno::Any( awt::FontUnderline::NONE ) );
384                 xModelProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontStrikeout" ) ), uno::Any( awt::FontStrikeout::NONE ) );
385             }
386 
387             xDialogContainer->insertByName( aNewName, uno::makeAny( xNewModel ) );
388             uno::Reference< awt::XControlContainer > xControlContainer( mxDialog, uno::UNO_QUERY_THROW );
389             xNewControl = xControlContainer->getControl( aNewName );
390 
391             if( bNativeAX ) try
392             {
393                 uno::Reference< script::XInvocation > xControlInvoke( xNewControl, uno::UNO_QUERY_THROW );
394 
395                 uno::Sequence< uno::Any > aArgs( 1 );
396                 aArgs[0] <<= aComServiceName;
397                 uno::Sequence< sal_Int16 > aOutIDDummy;
398                 uno::Sequence< uno::Any > aOutDummy;
399                 xControlInvoke->invoke( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SOAddAXControl" ) ), aArgs, aOutIDDummy, aOutDummy );
400             }
401             catch( uno::Exception& )
402             {
403                 xDialogContainer->removeByName( aNewName );
404                 throw;
405             }
406         }
407 
408         if ( xNewControl.is() )
409         {
410             UpdateCollectionIndex( lcl_controlsWrapper( mxDialog  ) );
411             aResult <<= xNewControl;
412             aResult = createCollectionObject( aResult );
413             uno::Reference< msforms::XControl > xVBAControl( aResult, uno::UNO_QUERY_THROW );
414             if( fDefWidth > 0.0 )
415                 xVBAControl->setWidth( fDefWidth );
416             if( fDefHeight > 0.0 )
417                 xVBAControl->setHeight( fDefHeight );
418         }
419         else
420             throw uno::RuntimeException();
421     }
422     catch( uno::RuntimeException& )
423     {
424         throw;
425     }
426     catch( uno::Exception& e )
427     {
428         throw lang::WrappedTargetException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can not create AXControl!" ) ),
429                 uno::Reference< uno::XInterface >(),
430                 uno::makeAny( e ) );
431     }
432 
433     return aResult;
434 }
435 
Remove(const uno::Any & StringKeyOrIndex)436 void SAL_CALL ScVbaControls::Remove( const uno::Any& StringKeyOrIndex )
437     throw (uno::RuntimeException)
438 {
439     ::rtl::OUString aControlName;
440     sal_Int32 nIndex = -1;
441 
442     try
443     {
444         if ( !mxDialog.is() )
445             throw uno::RuntimeException();
446 
447         uno::Reference< lang::XMultiServiceFactory > xModelFactory( mxDialog->getModel(), uno::UNO_QUERY_THROW );
448         uno::Reference< container::XNameContainer > xDialogContainer( xModelFactory, uno::UNO_QUERY_THROW );
449 
450         if ( !( ( StringKeyOrIndex >>= aControlName ) && aControlName.getLength() )
451           && !( ( StringKeyOrIndex >>= nIndex ) && nIndex >= 0 && nIndex < m_xIndexAccess->getCount() ) )
452             throw uno::RuntimeException();
453 
454         uno::Reference< awt::XControl > xControl;
455         if ( aControlName.getLength() )
456         {
457             uno::Reference< awt::XControlContainer > xControlContainer( mxDialog, uno::UNO_QUERY_THROW );
458             xControl = xControlContainer->getControl( aControlName );
459         }
460         else
461         {
462             m_xIndexAccess->getByIndex( nIndex ) >>= xControl;
463         }
464 
465         if ( !xControl.is() )
466             throw uno::RuntimeException();
467 
468         if ( !aControlName.getLength() )
469             aControlName = ControlArrayWrapper::getControlName( xControl );
470 
471         xDialogContainer->removeByName( aControlName );
472         xControl->dispose();
473     }
474     catch( uno::RuntimeException& )
475     {
476         // the exceptions are not rethrown, impossibility to find or remove the control is currently not reported
477         // since in most cases it means just that the controls is already not there, the VBA seems to do it in the same way
478 
479         // throw;
480     }
481     catch( uno::Exception& e )
482     {
483         // throw lang::WrappedTargetException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can not create AXControl!" ) ),
484         //         uno::Reference< uno::XInterface >(),
485         //         uno::makeAny( e ) );
486     }
487 }
488 
489 
490 uno::Type
getElementType()491 ScVbaControls::getElementType() throw (uno::RuntimeException)
492 {
493     return ooo::vba::msforms::XControl::static_type(0);
494 }
495 
496 VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaControls, "ooo.vba.msforms.Controls" )
497