LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / vbahelper / source / msforms / vbacontrol.cxx
blobb0b33543e059acd5d3ed3fe2bffb980f20073fec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <memory>
20 #include <com/sun/star/form/FormComponentType.hpp>
21 #include <com/sun/star/awt/XControlModel.hpp>
22 #include <com/sun/star/awt/XControl.hpp>
23 #include <com/sun/star/awt/XActionListener.hpp>
24 #include <com/sun/star/lang/XEventListener.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/drawing/XShape.hpp>
28 #include <com/sun/star/drawing/XControlShape.hpp>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/view/XControlAccess.hpp>
31 #include <com/sun/star/form/binding/XBindableValue.hpp>
32 #include <com/sun/star/form/binding/XListEntrySink.hpp>
33 #include <com/sun/star/table/CellAddress.hpp>
34 #include <com/sun/star/table/CellRangeAddress.hpp>
35 #include <com/sun/star/script/XScriptListener.hpp>
36 #include <com/sun/star/document/XCodeNameQuery.hpp>
37 #include <com/sun/star/form/XChangeListener.hpp>
38 #include <ooo/vba/XControlProvider.hpp>
39 #include <ooo/vba/msforms/fmMousePointer.hpp>
40 #include <svtools/bindablecontrolhelper.hxx>
41 #include "vbacontrol.hxx"
42 #include "vbacombobox.hxx"
43 #include "vbabutton.hxx"
44 #include "vbalabel.hxx"
45 #include "vbatextbox.hxx"
46 #include "vbaradiobutton.hxx"
47 #include "vbalistbox.hxx"
48 #include "vbatogglebutton.hxx"
49 #include "vbacheckbox.hxx"
50 #include "vbaframe.hxx"
51 #include "vbascrollbar.hxx"
52 #include "vbaprogressbar.hxx"
53 #include "vbamultipage.hxx"
54 #include "vbaspinbutton.hxx"
55 #include "vbasystemaxcontrol.hxx"
56 #include "vbaimage.hxx"
57 #include <toolkit/helper/vclunohelper.hxx>
58 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
59 #include <com/sun/star/form/XFormsSupplier.hpp>
60 #include <svx/svdobj.hxx>
61 #include <cppuhelper/implbase.hxx>
62 #include <cppuhelper/supportsservice.hxx>
64 using namespace com::sun::star;
65 using namespace ooo::vba;
67 uno::Reference< css::awt::XWindowPeer >
68 ScVbaControl::getWindowPeer()
70 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
72 uno::Reference< awt::XControlModel > xControlModel;
73 uno::Reference< css::awt::XWindowPeer > xWinPeer;
74 if ( !xControlShape.is() )
76 // would seem to be a Userform control
77 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY_THROW );
78 xWinPeer = xControl->getPeer();
79 return xWinPeer;
81 // form control
82 xControlModel.set( xControlShape->getControl(), uno::UNO_SET_THROW );
84 uno::Reference< view::XControlAccess > xControlAccess( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
85 try
87 uno::Reference< awt::XControl > xControl = xControlAccess->getControl( xControlModel );
88 xWinPeer = xControl->getPeer();
90 catch(const uno::Exception&)
92 throw uno::RuntimeException( "The Control does not exist" );
94 return xWinPeer;
97 namespace {
99 //ScVbaControlListener
100 class ScVbaControlListener: public cppu::WeakImplHelper< lang::XEventListener >
102 private:
103 ScVbaControl *pControl;
104 public:
105 explicit ScVbaControlListener( ScVbaControl *pTmpControl );
107 virtual void SAL_CALL disposing( const lang::EventObject& rEventObject ) override;
112 ScVbaControlListener::ScVbaControlListener( ScVbaControl *pTmpControl ): pControl( pTmpControl )
116 void SAL_CALL
117 ScVbaControlListener::disposing( const lang::EventObject& )
119 if( pControl )
121 pControl->removeResource();
122 pControl = nullptr;
126 //ScVbaControl
128 ScVbaControl::ScVbaControl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< ::uno::XInterface >& xControl, const css::uno::Reference< css::frame::XModel >& xModel, std::unique_ptr<ov::AbstractGeometryAttributes> pGeomHelper )
129 : ControlImpl_BASE( xParent, xContext ), m_xControl( xControl ), m_xModel( xModel )
131 //add listener
132 m_xEventListener.set( new ScVbaControlListener( this ) );
133 setGeometryHelper( std::move(pGeomHelper) );
134 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
135 xComponent->addEventListener( m_xEventListener );
137 //init m_xProps
138 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
139 uno::Reference< awt::XControl> xUserFormControl( m_xControl, uno::UNO_QUERY ) ;
140 if ( xControlShape.is() ) // form control
142 m_xProps.set( xControlShape->getControl(), uno::UNO_QUERY_THROW );
143 OUString sDefaultControl;
144 m_xProps->getPropertyValue( "DefaultControl" ) >>= sDefaultControl;
145 uno::Reference< lang::XMultiComponentFactory > xMFac( mxContext->getServiceManager(), uno::UNO_SET_THROW );
146 m_xEmptyFormControl.set( xMFac->createInstanceWithContext( sDefaultControl, mxContext ), uno::UNO_QUERY_THROW );
148 else if ( xUserFormControl.is() ) // userform control
150 m_xProps.set( xUserFormControl->getModel(), uno::UNO_QUERY_THROW );
154 ScVbaControl::~ScVbaControl()
156 if( m_xControl.is() )
158 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
159 xComponent->removeEventListener( m_xEventListener );
163 void
164 ScVbaControl::setGeometryHelper( std::unique_ptr<AbstractGeometryAttributes> pHelper )
166 mpGeometryHelper = std::move( pHelper );
169 void ScVbaControl::removeResource()
171 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
172 xComponent->removeEventListener( m_xEventListener );
173 m_xControl= nullptr;
174 m_xProps = nullptr;
177 //In design model has different behavior
178 sal_Bool SAL_CALL ScVbaControl::getEnabled()
180 uno::Any aValue = m_xProps->getPropertyValue ( "Enabled" );
181 bool bRet = false;
182 aValue >>= bRet;
183 return bRet;
186 void SAL_CALL ScVbaControl::setEnabled( sal_Bool bVisible )
188 uno::Any aValue( bVisible );
189 m_xProps->setPropertyValue( "Enabled" , aValue);
193 sal_Bool SAL_CALL ScVbaControl::getVisible()
195 bool bVisible( true );
196 m_xProps->getPropertyValue ( "EnableVisible" ) >>= bVisible;
197 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
198 if ( xControlShape.is() )
200 bool bEnableVisible = bVisible;
201 uno::Reference< beans::XPropertySet > xProps( m_xControl, uno::UNO_QUERY_THROW );
202 xProps->getPropertyValue ( "Visible" ) >>= bVisible;
203 bVisible = bVisible && bEnableVisible;
205 else
206 m_xProps->getPropertyValue ( "EnableVisible" ) >>= bVisible;
207 return bVisible;
210 void SAL_CALL ScVbaControl::setVisible( sal_Bool bVisible )
212 uno::Any aValue( bVisible );
213 m_xProps->setPropertyValue( "EnableVisible" , aValue);
214 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
215 if ( xControlShape.is() )
217 uno::Reference< beans::XPropertySet > xProps( m_xControl, uno::UNO_QUERY_THROW );
218 xProps->setPropertyValue ( "Visible", aValue );
221 double SAL_CALL ScVbaControl::getHeight()
223 return mpGeometryHelper->getHeight();
225 void SAL_CALL ScVbaControl::setHeight( double _height )
227 mpGeometryHelper->setHeight( _height );
230 double SAL_CALL ScVbaControl::getWidth()
232 return mpGeometryHelper->getWidth();
234 void SAL_CALL ScVbaControl::setWidth( double _width )
236 mpGeometryHelper->setWidth( _width );
239 double SAL_CALL
240 ScVbaControl::getLeft()
242 return mpGeometryHelper->getLeft();
245 void SAL_CALL
246 ScVbaControl::setLeft( double _left )
248 mpGeometryHelper->setLeft( _left );
251 double SAL_CALL
252 ScVbaControl::getTop()
254 return mpGeometryHelper->getTop();
257 void SAL_CALL
258 ScVbaControl::setTop( double _top )
260 mpGeometryHelper->setTop( _top );
263 uno::Reference< uno::XInterface > SAL_CALL
264 ScVbaControl::getObject()
266 uno::Reference< msforms::XControl > xRet( this );
267 return xRet;
270 void SAL_CALL ScVbaControl::SetFocus()
272 uno::Reference< awt::XWindow > xWin( m_xControl, uno::UNO_QUERY_THROW );
273 xWin->setFocus();
276 void SAL_CALL ScVbaControl::Move( double Left, double Top, const uno::Any& Width, const uno::Any& Height )
278 double nWidth = 0.0;
279 double nHeight = 0.0;
281 setLeft( Left );
282 setTop( Top );
284 if ( Width >>= nWidth )
285 setWidth( nWidth );
287 if ( Height >>= nHeight )
288 setHeight( nHeight );
291 OUString SAL_CALL
292 ScVbaControl::getControlSource()
294 // #FIXME I *hate* having these upstream differences
295 // but this is necessary until I manage to upstream other
296 // dependent parts
297 OUString sControlSource;
298 uno::Reference< form::binding::XBindableValue > xBindable( m_xProps, uno::UNO_QUERY );
299 if ( xBindable.is() )
303 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
304 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellAddressConversion" ), uno::UNO_QUERY );
305 uno::Reference< beans::XPropertySet > xProps( xBindable->getValueBinding(), uno::UNO_QUERY_THROW );
306 table::CellAddress aAddress;
307 xProps->getPropertyValue( "BoundCell" ) >>= aAddress;
308 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
309 xConvertor->getPropertyValue( "XLA1Representation" ) >>= sControlSource;
311 catch(const uno::Exception&)
315 return sControlSource;
318 void SAL_CALL
319 ScVbaControl::setControlSource( const OUString& _controlsource )
321 // afaik this is only relevant for Excel documents ( and we need to set up a
322 // reference tab in case no Sheet is specified in "_controlsource"
323 // Can't use the active sheet either, code may of course access
324 uno::Reference< drawing::XDrawPagesSupplier > xSupplier( m_xModel, uno::UNO_QUERY_THROW );
325 uno::Reference< container::XIndexAccess > xIndex( xSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
326 sal_Int32 nLen = xIndex->getCount();
327 bool bMatched = false;
328 sal_Int16 nRefTab = 0;
329 for ( sal_Int32 index = 0; index < nLen; ++index )
333 uno::Reference< form::XFormsSupplier > xFormSupplier( xIndex->getByIndex( index ), uno::UNO_QUERY_THROW );
334 uno::Reference< container::XIndexAccess > xFormIndex( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
335 // get the www-standard container
336 uno::Reference< container::XIndexAccess > xFormControls( xFormIndex->getByIndex(0), uno::UNO_QUERY_THROW );
337 sal_Int32 nCntrls = xFormControls->getCount();
338 for( sal_Int32 cIndex = 0; cIndex < nCntrls; ++cIndex )
340 uno::Reference< uno::XInterface > xControl( xFormControls->getByIndex( cIndex ), uno::UNO_QUERY_THROW );
341 bMatched = ( m_xProps == xControl );
342 if ( bMatched )
344 nRefTab = index;
345 break;
349 catch( uno::Exception& ) {}
350 if ( bMatched )
351 break;
354 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, _controlsource, "", sal_uInt16( nRefTab ) );
357 OUString SAL_CALL
358 ScVbaControl::getRowSource()
360 OUString sRowSource;
361 uno::Reference< form::binding::XListEntrySink > xListSink( m_xProps, uno::UNO_QUERY );
362 if ( xListSink.is() )
366 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
367 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellRangeAddressConversion" ), uno::UNO_QUERY );
369 uno::Reference< beans::XPropertySet > xProps( xListSink->getListEntrySource(), uno::UNO_QUERY_THROW );
370 table::CellRangeAddress aAddress;
371 xProps->getPropertyValue( "CellRange" ) >>= aAddress;
372 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
373 xConvertor->getPropertyValue( "XLA1Representation" ) >>= sRowSource;
375 catch(const uno::Exception&)
379 return sRowSource;
382 void SAL_CALL
383 ScVbaControl::setRowSource( const OUString& _rowsource )
385 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, "", _rowsource );
388 OUString SAL_CALL
389 ScVbaControl::getName()
391 OUString sName;
392 m_xProps->getPropertyValue( "Name" ) >>= sName;
393 return sName;
397 void SAL_CALL
398 ScVbaControl::setName( const OUString& _name )
400 m_xProps->setPropertyValue( "Name" , uno::makeAny( _name ) );
403 OUString SAL_CALL
404 ScVbaControl::getControlTipText()
406 OUString sName;
407 m_xProps->getPropertyValue( "HelpText" ) >>= sName;
408 return sName;
411 void SAL_CALL
412 ScVbaControl::setControlTipText( const OUString& rsToolTip )
414 m_xProps->setPropertyValue( "HelpText" , uno::makeAny( rsToolTip ) );
417 OUString SAL_CALL ScVbaControl::getTag()
419 return m_aControlTag;
422 void SAL_CALL ScVbaControl::setTag( const OUString& aTag )
424 m_aControlTag = aTag;
427 ::sal_Int32 SAL_CALL ScVbaControl::getForeColor()
429 Color nForeColor;
430 m_xProps->getPropertyValue( "TextColor" ) >>= nForeColor;
431 return OORGBToXLRGB( nForeColor );
434 namespace {
436 struct PointerStyles
438 tools::Long msoPointerStyle;
439 PointerStyle loPointStyle;
444 // 1 -> 1 map of styles ( some dubious choices in there though )
445 PointerStyles const styles[] = {
446 /// assuming pointer default is Arrow
447 { msforms::fmMousePointer::fmMousePointerDefault, PointerStyle::Arrow },
448 { msforms::fmMousePointer::fmMousePointerArrow, PointerStyle::Arrow },
449 { msforms::fmMousePointer::fmMousePointerCross, PointerStyle::Cross },
450 { msforms::fmMousePointer::fmMousePointerIBeam, PointerStyle::Text },
451 { msforms::fmMousePointer::fmMousePointerSizeNESW, PointerStyle::AutoScrollNSWE }, // #TODO not correct, need to check, need to find the right one
452 { msforms::fmMousePointer::fmMousePointerSizeNS, PointerStyle::AutoScrollNS },
453 { msforms::fmMousePointer::fmMousePointerSizeNWSE, PointerStyle::AutoScrollNSWE }, // #TODO not correct, need to check, need to find the right one
454 { msforms::fmMousePointer::fmMousePointerSizeWE, PointerStyle::AutoScrollWE },
455 { msforms::fmMousePointer::fmMousePointerUpArrow, PointerStyle::WindowNSize },
456 { msforms::fmMousePointer::fmMousePointerHourGlass, PointerStyle::Wait },
457 { msforms::fmMousePointer::fmMousePointerNoDrop, PointerStyle::NotAllowed },
458 { msforms::fmMousePointer::fmMousePointerAppStarting, PointerStyle::Wait },
459 { msforms::fmMousePointer::fmMousePointerHelp, PointerStyle::Help },
460 { msforms::fmMousePointer::fmMousePointerSizeAll, PointerStyle::Cross },
461 { msforms::fmMousePointer::fmMousePointerCustom, PointerStyle::Arrow }, // not supported I guess
465 static tools::Long lcl_loPointerToMsoPointer( PointerStyle eType )
467 tools::Long nRet = msforms::fmMousePointer::fmMousePointerDefault;
468 for ( int i = 0; i < int(SAL_N_ELEMENTS( styles )); ++i )
470 if ( styles[ i ].loPointStyle == eType )
472 nRet = styles[ i ].msoPointerStyle;
473 break;
476 return nRet;
479 static PointerStyle lcl_msoPointerToLOPointer( tools::Long msoPointerStyle )
481 PointerStyle aPointer( PointerStyle::Arrow );
482 for ( int i = 0; i < int(SAL_N_ELEMENTS( styles )); ++i )
484 if ( styles[ i ].msoPointerStyle == msoPointerStyle )
486 aPointer = styles[ i ].loPointStyle;
487 break;
490 return aPointer;
493 ::sal_Int32 SAL_CALL
494 ScVbaControl::getMousePointer()
496 return lcl_loPointerToMsoPointer(VCLUnoHelper::getMousePointer(getWindowPeer()));
499 void SAL_CALL
500 ScVbaControl::setMousePointer( ::sal_Int32 _mousepointer )
502 VCLUnoHelper::setMousePointer(getWindowPeer(), lcl_msoPointerToLOPointer(_mousepointer));
505 void SAL_CALL ScVbaControl::fireEvent( const script::ScriptEvent& rEvt )
507 script::ScriptEvent evt( rEvt );
508 uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
509 uno::Reference< script::XScriptListener > xScriptListener( xServiceManager->createInstanceWithContext( "ooo.vba.EventListener" , mxContext ), uno::UNO_QUERY_THROW );
511 uno::Reference< beans::XPropertySet > xProps( xScriptListener, uno::UNO_QUERY_THROW );
512 xProps->setPropertyValue( "Model" , uno::makeAny( m_xModel ) );
514 // handling for sheet control
515 uno::Reference< msforms::XControl > xThisControl( this );
518 evt.Arguments.realloc( 1 );
519 lang::EventObject aEvt;
521 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
522 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY ) ;
524 if ( xControlShape.is() )
526 evt.Source = xControlShape;
527 aEvt.Source = m_xEmptyFormControl;
528 // Set up proper scriptcode
529 uno::Reference< lang::XMultiServiceFactory > xDocFac( m_xModel, uno::UNO_QUERY_THROW );
530 uno::Reference< document::XCodeNameQuery > xNameQuery( xDocFac->createInstance( "ooo.vba.VBACodeNameProvider" ), uno::UNO_QUERY_THROW );
531 uno::Reference< uno::XInterface > xIf( xControlShape->getControl(), uno::UNO_QUERY_THROW );
532 evt.ScriptCode = xNameQuery->getCodeNameForObject( xIf );
533 // handle if we passed in our own arguments
534 if ( !rEvt.Arguments.hasElements() )
535 evt.Arguments.getArray()[ 0 ] <<= aEvt;
536 xScriptListener->firing( evt );
538 else
540 if ( xControl.is() ) // normal control ( from dialog/userform )
542 // #FIXME We should probably store a reference to the
543 // parent dialog/userform here (otherwise the name of
544 // dialog could be changed and we won't be aware of it.
545 // (OTOH this is probably an unlikely scenario)
546 evt.Source = xThisControl;
547 aEvt.Source = xControl;
548 evt.ScriptCode = m_sLibraryAndCodeName;
549 evt.Arguments.getArray()[ 0 ] <<= aEvt;
550 xScriptListener->firing( evt );
554 catch(const uno::Exception&)
558 void ScVbaControl::fireChangeEvent()
560 script::ScriptEvent evt;
561 evt.ScriptType = "VBAInterop";
562 evt.ListenerType = cppu::UnoType<form::XChangeListener>::get();
563 evt.MethodName = "changed";
564 fireEvent( evt );
567 void ScVbaControl::fireClickEvent()
569 script::ScriptEvent evt;
570 evt.ScriptType = "VBAInterop";
571 evt.ListenerType = cppu::UnoType<awt::XActionListener>::get();
572 evt.MethodName = "actionPerformed";
573 fireEvent( evt );
576 sal_Int32 SAL_CALL ScVbaControl::getTabIndex()
578 return 1;
581 void SAL_CALL ScVbaControl::setTabIndex( sal_Int32 /*nTabIndex*/ )
585 //ScVbaControlFactory
587 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createShapeControl(
588 const uno::Reference< uno::XComponentContext >& xContext,
589 const uno::Reference< drawing::XControlShape >& xControlShape,
590 const uno::Reference< frame::XModel >& xModel )
592 uno::Reference< beans::XPropertySet > xProps( xControlShape->getControl(), uno::UNO_QUERY_THROW );
593 sal_Int32 nClassId = -1;
594 xProps->getPropertyValue( "ClassId" ) >>= nClassId;
595 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
596 uno::Reference< drawing::XShape > xShape( xControlShape, uno::UNO_QUERY_THROW );
597 ::std::unique_ptr< ConcreteXShapeGeometryAttributes > xGeoHelper( new ConcreteXShapeGeometryAttributes( xShape ) );
598 switch( nClassId )
600 case form::FormComponentType::COMBOBOX:
601 return new ScVbaComboBox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
602 case form::FormComponentType::COMMANDBUTTON:
604 bool bToggle = false;
605 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
606 if ( bToggle )
607 return new ScVbaToggleButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
608 else
609 return new VbaButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
611 case form::FormComponentType::FIXEDTEXT:
612 return new ScVbaLabel( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
613 case form::FormComponentType::TEXTFIELD:
614 return new ScVbaTextBox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
615 case form::FormComponentType::CHECKBOX:
616 return new ScVbaCheckbox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
617 case form::FormComponentType::RADIOBUTTON:
618 return new ScVbaRadioButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
619 case form::FormComponentType::LISTBOX:
620 return new ScVbaListBox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
621 case form::FormComponentType::SPINBUTTON:
622 return new ScVbaSpinButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
623 case form::FormComponentType::IMAGECONTROL:
624 return new ScVbaImage( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
625 case form::FormComponentType::SCROLLBAR:
626 return new ScVbaScrollBar( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
628 throw uno::RuntimeException( "Unsupported control." );
631 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createUserformControl(
632 const uno::Reference< uno::XComponentContext >& xContext,
633 const uno::Reference< awt::XControl >& xControl,
634 const uno::Reference< awt::XControl >& xDialog,
635 const uno::Reference< frame::XModel >& xModel,
636 double fOffsetX, double fOffsetY )
638 uno::Reference< beans::XPropertySet > xProps( xControl->getModel(), uno::UNO_QUERY_THROW );
639 uno::Reference< lang::XServiceInfo > xServiceInfo( xProps, uno::UNO_QUERY_THROW );
640 uno::Reference< msforms::XControl > xVBAControl;
641 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
642 ::std::unique_ptr< UserFormGeometryHelper > xGeoHelper( new UserFormGeometryHelper( xControl, fOffsetX, fOffsetY ) );
644 if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlCheckBoxModel" ) )
645 xVBAControl.set( new ScVbaCheckbox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
646 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlRadioButtonModel" ) )
647 xVBAControl.set( new ScVbaRadioButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
648 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlEditModel" ) )
649 xVBAControl.set( new ScVbaTextBox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper), true ) );
650 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlButtonModel" ) )
652 bool bToggle = false;
653 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
654 if ( bToggle )
655 xVBAControl.set( new ScVbaToggleButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
656 else
657 xVBAControl.set( new VbaButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
659 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlComboBoxModel" ) )
660 xVBAControl.set( new ScVbaComboBox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
661 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlListBoxModel" ) )
662 xVBAControl.set( new ScVbaListBox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
663 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedTextModel" ) )
664 xVBAControl.set( new ScVbaLabel( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
665 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlImageControlModel" ) )
666 xVBAControl.set( new ScVbaImage( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
667 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlProgressBarModel" ) )
668 xVBAControl.set( new ScVbaProgressBar( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
669 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlGroupBoxModel" ) )
670 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper), xDialog ) );
671 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlScrollBarModel" ) )
672 xVBAControl.set( new ScVbaScrollBar( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
673 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoMultiPageModel" ) )
674 xVBAControl.set( new ScVbaMultiPage( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
675 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlSpinButtonModel" ) )
676 xVBAControl.set( new ScVbaSpinButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
677 else if ( xServiceInfo->supportsService( "com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ) )
678 xVBAControl.set( new VbaSystemAXControl( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
679 // #FIXME implement a page control
680 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoPageModel" ) )
681 xVBAControl.set( new ScVbaControl( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
682 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoFrameModel" ) )
683 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper), xDialog ) );
684 if( xVBAControl.is() )
685 return xVBAControl;
686 throw uno::RuntimeException( "Unsupported control." );
689 OUString
690 ScVbaControl::getServiceImplName()
692 return "ScVbaControl";
695 uno::Sequence< OUString >
696 ScVbaControl::getServiceNames()
698 return { "ooo.vba.excel.Control" };
701 sal_Int32 const nSysCols[] = { 0xC8D0D4, 0x0, 0x6A240A, 0x808080, 0xE4E4E4, 0xFFFFFF, 0x0, 0x0, 0x0, 0xFFFFFF, 0xE4E4E4, 0xE4E4E4, 0x808080, 0x6A240A, 0xFFFFFF, 0xE4E4E4, 0x808080, 0x808080, 0x0, 0xC8D0D4, 0xFFFFFF, 0x404040, 0xE4E4E4, 0x0, 0xE1FFFF };
703 sal_Int32 ScVbaControl::getBackColor()
705 sal_Int32 nBackColor = 0;
706 m_xProps->getPropertyValue( "BackgroundColor" ) >>= nBackColor;
707 return nBackColor;
710 void ScVbaControl::setBackColor( sal_Int32 nBackColor )
712 auto const col = static_cast<sal_uInt32>(nBackColor);
713 if ( ( col >= sal_uInt32(0x80000000) ) &&
714 ( col <= sal_uInt32(0x80000000) + SAL_N_ELEMENTS(nSysCols) ) )
716 nBackColor = nSysCols[ col & 0x0FF];
718 m_xProps->setPropertyValue( "BackgroundColor" , uno::makeAny( XLRGBToOORGB( nBackColor ) ) );
721 bool ScVbaControl::getAutoSize() const
723 bool bIsResizeEnabled = false;
724 uno::Reference< uno::XInterface > xIf( m_xControl, uno::UNO_SET_THROW );
725 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xIf );
726 if ( pObj )
727 bIsResizeEnabled = !pObj->IsResizeProtect();
728 return bIsResizeEnabled;
731 // currently no implementation for this
732 void ScVbaControl::setAutoSize( bool bAutoSize )
734 uno::Reference< uno::XInterface > xIf( m_xControl, uno::UNO_SET_THROW );
735 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xIf );
736 if ( pObj )
737 pObj->SetResizeProtect( !bAutoSize );
740 bool ScVbaControl::getLocked()
742 bool bRes( false );
743 m_xProps->getPropertyValue( "ReadOnly" ) >>= bRes;
744 return bRes;
747 void ScVbaControl::setLocked( bool bLocked )
749 m_xProps->setPropertyValue( "ReadOnly" , uno::makeAny( bLocked ) );
752 namespace {
754 class ControlProviderImpl : public cppu::WeakImplHelper< XControlProvider, css::lang::XServiceInfo >
756 uno::Reference< uno::XComponentContext > m_xCtx;
757 public:
758 explicit ControlProviderImpl( const uno::Reference< uno::XComponentContext >& xCtx ) : m_xCtx( xCtx ) {}
759 virtual uno::Reference< msforms::XControl > SAL_CALL createControl( const uno::Reference< drawing::XControlShape >& xControl, const uno::Reference< frame::XModel >& xDocOwner ) override;
761 // XServiceInfo
762 virtual sal_Bool SAL_CALL supportsService(const OUString& sServiceName) override;
763 virtual OUString SAL_CALL getImplementationName() override;
764 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
769 // XServiceInfo
770 sal_Bool ControlProviderImpl::supportsService(const OUString& sServiceName)
772 return cppu::supportsService(this, sServiceName);
774 OUString ControlProviderImpl::getImplementationName()
776 return "ControlProviderImpl";
778 css::uno::Sequence< OUString > ControlProviderImpl::getSupportedServiceNames()
780 return { "ooo.vba.ControlProvider" };
783 uno::Reference< msforms::XControl > SAL_CALL
784 ControlProviderImpl::createControl( const uno::Reference< drawing::XControlShape >& xControlShape, const uno::Reference< frame::XModel >& xDocOwner )
786 uno::Reference< msforms::XControl > xControlToReturn;
787 if ( xControlShape.is() )
788 xControlToReturn = ScVbaControlFactory::createShapeControl( m_xCtx, xControlShape, xDocOwner );
789 return xControlToReturn;
793 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
794 ControlProviderImpl_get_implementation(
795 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
797 return cppu::acquire(new ControlProviderImpl(context));
800 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */