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