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>
36#include <unordered_map>
47class ControlArrayWrapper :
public ::cppu::WeakImplHelper< container::XNameAccess, container::XIndexAccess >
49 uno::Reference< awt::XControlContainer > mxDialog;
50 uno::Sequence< OUString > msNames;
51 std::vector< uno::Reference< awt::XControl > > mControls;
55 void SetArrayElementTo(
const uno::Reference< awt::XControl >& xCtrl, sal_Int32
nIndex )
61 nIndex = msNames.getLength();
63 if (
nIndex >= msNames.getLength() )
66 msNames.getArray()[
nIndex ] = getControlName( xCtrl );
67 mControls.push_back( xCtrl );
72 explicit ControlArrayWrapper(
const uno::Reference< awt::XControl >& xDialog )
76 mxDialog.set( xDialog, uno::UNO_QUERY_THROW );
77 uno::Sequence< uno::Reference< awt::XControl > > sXControls = mxDialog->getControls();
79 msNames.realloc( sXControls.getLength() );
80 for ( sal_Int32
i = 0;
i < sXControls.getLength(); ++
i )
81 SetArrayElementTo( sXControls[
i ],
i );
83 catch (
const uno::Exception&)
90 static OUString getControlName(
const uno::Reference< awt::XControl >& xCtrl )
93 throw uno::RuntimeException();
95 uno::Reference< beans::XPropertySet > xProp( xCtrl->getModel(), uno::UNO_QUERY_THROW );
97 xProp->getPropertyValue(
"Name" ) >>=
sName;
103 virtual uno::Type SAL_CALL getElementType( )
override
108 virtual sal_Bool SAL_CALL hasElements( )
override
110 return ( !mControls.empty() );
114 virtual uno::Any SAL_CALL getByName(
const OUString&
aName )
override
116 if ( !hasByName(
aName ) )
117 throw container::NoSuchElementException();
118 return getByIndex( mIndices[
aName ] );
121 virtual uno::Sequence< OUString > SAL_CALL getElementNames( )
override
126 virtual sal_Bool SAL_CALL hasByName(
const OUString&
aName )
override
128 ControlIndexMap::iterator it = mIndices.find(
aName );
129 return it != mIndices.end();
133 virtual ::sal_Int32 SAL_CALL getCount( )
override
135 return mControls.size();
138 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32
Index )
override
141 throw lang::IndexOutOfBoundsException();
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;
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 ) :
166 m_xIndexAccess(std::move( xIndexAccess )),
167 m_xDlg(std::move( xDlg )),
169 mfOffsetX( fOffsetX ),
170 mfOffsetY( fOffsetY ),
173 virtual sal_Bool SAL_CALL hasMoreElements( )
override
175 return ( nIndex < m_xIndexAccess->getCount() );
178 virtual uno::Any SAL_CALL nextElement( )
override
180 if ( nIndex < m_xIndexAccess->getCount() )
182 uno::Reference< awt::XControl > xControl;
183 m_xIndexAccess->getByIndex(
nIndex++ ) >>= xControl;
185 uno::Reference< msforms::XControl > xVBAControl;
186 if ( xControl.is() && m_xDlg.is() )
190 throw container::NoSuchElementException();
197static uno::Reference<container::XIndexAccess >
200 return new ControlArrayWrapper( xDlg );
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 ) :
212 mfOffsetX( fOffsetX ),
213 mfOffsetY( fOffsetY )
217uno::Reference< container::XEnumeration >
222 throw uno::RuntimeException();
230 uno::Reference< awt::XControl > xControl( aSource, uno::UNO_QUERY_THROW );
239 while ( xEnum->hasMoreElements() )
241 uno::Reference< msforms::XControl > xControl( xEnum->nextElement(), uno::UNO_QUERY_THROW );
242 xControl->setLeft( xControl->getLeft() + cx );
243 xControl->setTop( xControl->getTop() + cy );
250 OUString aComServiceName;
255 throw uno::RuntimeException();
257 uno::Reference< awt::XControl > xNewControl;
258 uno::Reference< lang::XMultiServiceFactory > xModelFactory(
mxDialog->getModel(), uno::UNO_QUERY_THROW );
260 uno::Reference< container::XNameContainer > xDialogContainer( xModelFactory, uno::UNO_QUERY_THROW );
262 Object >>= aComServiceName;
266 StringKey >>= aNewName;
267 if ( aNewName.isEmpty() )
269 aNewName = aComServiceName;
270 if ( aNewName.isEmpty() )
271 aNewName =
"Control";
274 while( xDialogContainer->hasByName( aNewName ) && (nInd <
SAL_MAX_INT32) )
276 aNewName = aComServiceName + OUString::number( nInd++ );
280 double fDefWidth = 72.0, fDefHeight = 18.0;
281 if ( !aComServiceName.isEmpty() )
284 uno::Reference< awt::XControlModel > xNewModel;
285 bool bFontSupport =
false;
286 bool bNativeAX =
false;
287 if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.CommandButton.1" ) )
289 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlButtonModel" ), uno::UNO_QUERY_THROW );
290 fDefWidth = 72.0; fDefHeight = 24.0;
293 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.Label.1" ) )
295 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlFixedTextModel" ), uno::UNO_QUERY_THROW );
296 fDefWidth = 72.0; fDefHeight = 18.0;
299 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.Image.1" ) )
301 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlImageControlModel" ), uno::UNO_QUERY_THROW );
302 fDefWidth = 72.0; fDefHeight = 72.0;
304 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.CheckBox.1" ) )
306 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlCheckBoxModel" ), uno::UNO_QUERY_THROW );
307 fDefWidth = 108.0; fDefHeight = 18.0;
310 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.OptionButton.1" ) )
312 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlRadioButtonModel" ), uno::UNO_QUERY_THROW );
313 fDefWidth = 108.0; fDefHeight = 18.0;
316 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.TextBox.1" ) )
318 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlEditModel" ), uno::UNO_QUERY_THROW );
319 fDefWidth = 72.0; fDefHeight = 18.0;
322 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.ListBox.1" ) )
324 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlListBoxModel" ), uno::UNO_QUERY_THROW );
325 fDefWidth = 72.0; fDefHeight = 18.0;
328 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.ComboBox.1" ) )
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;
336 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.ToggleButton.1" ) )
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;
344 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.Frame.1" ) )
346 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlGroupBoxModel" ), uno::UNO_QUERY_THROW );
347 fDefWidth = 216.0; fDefHeight = 144.0;
350 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.SpinButton.1" ) )
352 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlSpinButtonModel" ), uno::UNO_QUERY_THROW );
353 fDefWidth = 12.75; fDefHeight = 25.5;
355 else if( aComServiceName.equalsIgnoreAsciiCase(
"Forms.ScrollBar.1" ) )
357 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.awt.UnoControlScrollBarModel" ), uno::UNO_QUERY_THROW );
358 fDefWidth = 12.75; fDefHeight = 63.8;
362 xNewModel.set( xModelFactory->createInstance(
"com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ), uno::UNO_QUERY_THROW );
363 fDefWidth = 72.0; fDefHeight = 18.0;
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 ) );
379 xDialogContainer->insertByName( aNewName,
uno::Any( xNewModel ) );
380 uno::Reference< awt::XControlContainer > xControlContainer(
mxDialog, uno::UNO_QUERY_THROW );
381 xNewControl = xControlContainer->getControl( aNewName );
385 uno::Reference< script::XInvocation > xControlInvoke( xNewControl, uno::UNO_QUERY_THROW );
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 );
392 catch (
const uno::Exception&)
394 xDialogContainer->removeByName( aNewName );
399 if ( !xNewControl.is() )
400 throw uno::RuntimeException();
403 aResult <<= xNewControl;
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 );
411 catch (
const uno::RuntimeException&)
415 catch (
const uno::Exception&)
418 throw lang::WrappedTargetRuntimeException(
"Can not create AXControl!",
419 uno::Reference< uno::XInterface >(),
430 OUString aControlName;
433 throw uno::RuntimeException();
435 uno::Reference< lang::XMultiServiceFactory > xModelFactory(
mxDialog->getModel(), uno::UNO_QUERY_THROW );
436 uno::Reference< container::XNameContainer > xDialogContainer( xModelFactory, uno::UNO_QUERY_THROW );
438 if ( StringKeyOrIndex >>= aControlName )
440 if ( aControlName.isEmpty() )
441 throw uno::RuntimeException();
443 else if ( StringKeyOrIndex >>=
nIndex )
446 throw uno::RuntimeException();
449 throw uno::RuntimeException();
451 uno::Reference< awt::XControl > xControl;
452 if ( !aControlName.isEmpty() )
454 uno::Reference< awt::XControlContainer > xControlContainer(
mxDialog, uno::UNO_QUERY_THROW );
455 xControl = xControlContainer->getControl( aControlName );
462 if ( !xControl.is() )
463 throw uno::RuntimeException();
465 if ( aControlName.isEmpty() )
466 aControlName = ControlArrayWrapper::getControlName( xControl );
468 xDialogContainer->removeByName( aControlName );
471 catch (
const uno::RuntimeException&)
478 catch (
const uno::Exception&)
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
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
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
css::uno::Reference< css::awt::XControl > mxDialog
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
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()
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Reference< XModel > xModel
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
std::unordered_map< OUString, sal_Int32 > ControlIndexMap
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....