bump product version to 6.3.0.0.beta1
[LibreOffice.git] / vbahelper / source / msforms / vbacontrol.cxx
blob5d32f2d9af729f0e9ccc2e387432f169bd4fc429
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/XWindow2.hpp>
24 #include <com/sun/star/awt/XActionListener.hpp>
25 #include <com/sun/star/lang/XEventListener.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.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/container/XChild.hpp>
32 #include <com/sun/star/form/binding/XBindableValue.hpp>
33 #include <com/sun/star/form/binding/XListEntrySink.hpp>
34 #include <com/sun/star/table/CellAddress.hpp>
35 #include <com/sun/star/table/CellRangeAddress.hpp>
36 #include <com/sun/star/script/XScriptListener.hpp>
37 #include <com/sun/star/document/XCodeNameQuery.hpp>
38 #include <com/sun/star/form/XChangeListener.hpp>
39 #include <ooo/vba/XControlProvider.hpp>
40 #include <ooo/vba/msforms/fmMousePointer.hpp>
41 #include <svtools/bindablecontrolhelper.hxx>
42 #include "service.hxx"
43 #include "vbacontrol.hxx"
44 #include "vbacombobox.hxx"
45 #include "vbabutton.hxx"
46 #include "vbalabel.hxx"
47 #include "vbatextbox.hxx"
48 #include "vbaradiobutton.hxx"
49 #include "vbalistbox.hxx"
50 #include "vbatogglebutton.hxx"
51 #include "vbacheckbox.hxx"
52 #include "vbaframe.hxx"
53 #include "vbascrollbar.hxx"
54 #include "vbaprogressbar.hxx"
55 #include "vbamultipage.hxx"
56 #include "vbaspinbutton.hxx"
57 #include "vbasystemaxcontrol.hxx"
58 #include "vbaimage.hxx"
59 #include <vbahelper/helperdecl.hxx>
60 #include <toolkit/helper/vclunohelper.hxx>
61 #include <vcl/window.hxx>
62 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
63 #include <com/sun/star/form/XFormsSupplier.hpp>
64 #include <svx/svdobj.hxx>
65 #include <cppuhelper/implbase.hxx>
67 using namespace com::sun::star;
68 using namespace ooo::vba;
70 uno::Reference< css::awt::XWindowPeer >
71 ScVbaControl::getWindowPeer()
73 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
75 uno::Reference< awt::XControlModel > xControlModel;
76 uno::Reference< css::awt::XWindowPeer > xWinPeer;
77 if ( !xControlShape.is() )
79 // would seem to be a Userform control
80 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY_THROW );
81 xWinPeer = xControl->getPeer();
82 return xWinPeer;
84 // form control
85 xControlModel.set( xControlShape->getControl(), uno::UNO_SET_THROW );
87 uno::Reference< view::XControlAccess > xControlAccess( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
88 try
90 uno::Reference< awt::XControl > xControl( xControlAccess->getControl( xControlModel ), uno::UNO_QUERY );
91 xWinPeer = xControl->getPeer();
93 catch(const uno::Exception&)
95 throw uno::RuntimeException( "The Control does not exist" );
97 return xWinPeer;
100 //ScVbaControlListener
101 class ScVbaControlListener: public cppu::WeakImplHelper< lang::XEventListener >
103 private:
104 ScVbaControl *pControl;
105 public:
106 explicit ScVbaControlListener( ScVbaControl *pTmpControl );
108 virtual void SAL_CALL disposing( const lang::EventObject& rEventObject ) override;
111 ScVbaControlListener::ScVbaControlListener( ScVbaControl *pTmpControl ): pControl( pTmpControl )
115 void SAL_CALL
116 ScVbaControlListener::disposing( const lang::EventObject& )
118 if( pControl )
120 pControl->removeResource();
121 pControl = nullptr;
125 //ScVbaControl
127 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 )
128 : ControlImpl_BASE( xParent, xContext ), m_xControl( xControl ), m_xModel( xModel )
130 //add listener
131 m_xEventListener.set( new ScVbaControlListener( this ) );
132 setGeometryHelper( std::move(pGeomHelper) );
133 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
134 xComponent->addEventListener( m_xEventListener );
136 //init m_xProps
137 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
138 uno::Reference< awt::XControl> xUserFormControl( m_xControl, uno::UNO_QUERY ) ;
139 if ( xControlShape.is() ) // form control
141 m_xProps.set( xControlShape->getControl(), uno::UNO_QUERY_THROW );
142 OUString sDefaultControl;
143 m_xProps->getPropertyValue( "DefaultControl" ) >>= sDefaultControl;
144 uno::Reference< lang::XMultiComponentFactory > xMFac( mxContext->getServiceManager(), uno::UNO_SET_THROW );
145 m_xEmptyFormControl.set( xMFac->createInstanceWithContext( sDefaultControl, mxContext ), uno::UNO_QUERY_THROW );
147 else if ( xUserFormControl.is() ) // userform control
149 m_xProps.set( xUserFormControl->getModel(), uno::UNO_QUERY_THROW );
153 ScVbaControl::~ScVbaControl()
155 if( m_xControl.is() )
157 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
158 xComponent->removeEventListener( m_xEventListener );
162 void
163 ScVbaControl::setGeometryHelper( std::unique_ptr<AbstractGeometryAttributes> pHelper )
165 mpGeometryHelper = std::move( pHelper );
168 void ScVbaControl::removeResource()
170 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
171 xComponent->removeEventListener( m_xEventListener );
172 m_xControl= nullptr;
173 m_xProps = nullptr;
176 //In design model has different behavior
177 sal_Bool SAL_CALL ScVbaControl::getEnabled()
179 uno::Any aValue = m_xProps->getPropertyValue ( "Enabled" );
180 bool bRet = false;
181 aValue >>= bRet;
182 return bRet;
185 void SAL_CALL ScVbaControl::setEnabled( sal_Bool bVisible )
187 uno::Any aValue( bVisible );
188 m_xProps->setPropertyValue( "Enabled" , aValue);
192 sal_Bool SAL_CALL ScVbaControl::getVisible()
194 bool bVisible( true );
195 m_xProps->getPropertyValue ( "EnableVisible" ) >>= bVisible;
196 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
197 if ( xControlShape.is() )
199 bool bEnableVisible = bVisible;
200 uno::Reference< beans::XPropertySet > xProps( m_xControl, uno::UNO_QUERY_THROW );
201 xProps->getPropertyValue ( "Visible" ) >>= bVisible;
202 bVisible = bVisible && bEnableVisible;
204 else
205 m_xProps->getPropertyValue ( "EnableVisible" ) >>= bVisible;
206 return bVisible;
209 void SAL_CALL ScVbaControl::setVisible( sal_Bool bVisible )
211 uno::Any aValue( bVisible );
212 m_xProps->setPropertyValue( "EnableVisible" , aValue);
213 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
214 if ( xControlShape.is() )
216 uno::Reference< beans::XPropertySet > xProps( m_xControl, uno::UNO_QUERY_THROW );
217 xProps->setPropertyValue ( "Visible", aValue );
220 double SAL_CALL ScVbaControl::getHeight()
222 return mpGeometryHelper->getHeight();
224 void SAL_CALL ScVbaControl::setHeight( double _height )
226 mpGeometryHelper->setHeight( _height );
229 double SAL_CALL ScVbaControl::getWidth()
231 return mpGeometryHelper->getWidth();
233 void SAL_CALL ScVbaControl::setWidth( double _width )
235 mpGeometryHelper->setWidth( _width );
238 double SAL_CALL
239 ScVbaControl::getLeft()
241 return mpGeometryHelper->getLeft();
244 void SAL_CALL
245 ScVbaControl::setLeft( double _left )
247 mpGeometryHelper->setLeft( _left );
250 double SAL_CALL
251 ScVbaControl::getTop()
253 return mpGeometryHelper->getTop();
256 void SAL_CALL
257 ScVbaControl::setTop( double _top )
259 mpGeometryHelper->setTop( _top );
262 uno::Reference< uno::XInterface > SAL_CALL
263 ScVbaControl::getObject()
265 uno::Reference< msforms::XControl > xRet( this );
266 return xRet;
269 void SAL_CALL ScVbaControl::SetFocus()
271 uno::Reference< awt::XWindow > xWin( m_xControl, uno::UNO_QUERY_THROW );
272 xWin->setFocus();
275 void SAL_CALL ScVbaControl::Move( double Left, double Top, const uno::Any& Width, const uno::Any& Height )
277 double nWidth = 0.0;
278 double nHeight = 0.0;
280 setLeft( Left );
281 setTop( Top );
283 if ( Width >>= nWidth )
284 setWidth( nWidth );
286 if ( Height >>= nHeight )
287 setHeight( nHeight );
290 OUString SAL_CALL
291 ScVbaControl::getControlSource()
293 // #FIXME I *hate* having these upstream differences
294 // but this is necessary until I manage to upstream other
295 // dependent parts
296 OUString sControlSource;
297 uno::Reference< form::binding::XBindableValue > xBindable( m_xProps, uno::UNO_QUERY );
298 if ( xBindable.is() )
302 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
303 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellAddressConversion" ), uno::UNO_QUERY );
304 uno::Reference< beans::XPropertySet > xProps( xBindable->getValueBinding(), uno::UNO_QUERY_THROW );
305 table::CellAddress aAddress;
306 xProps->getPropertyValue( "BoundCell" ) >>= aAddress;
307 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
308 xConvertor->getPropertyValue( "XLA1Representation" ) >>= sControlSource;
310 catch(const uno::Exception&)
314 return sControlSource;
317 void SAL_CALL
318 ScVbaControl::setControlSource( const OUString& _controlsource )
320 // afaik this is only relevant for Excel documents ( and we need to set up a
321 // reference tab in case no Sheet is specified in "_controlsource"
322 // Can't use the active sheet either, code may of course access
323 uno::Reference< drawing::XDrawPagesSupplier > xSupplier( m_xModel, uno::UNO_QUERY_THROW );
324 uno::Reference< container::XIndexAccess > xIndex( xSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
325 sal_Int32 nLen = xIndex->getCount();
326 bool bMatched = false;
327 sal_Int16 nRefTab = 0;
328 for ( sal_Int32 index = 0; index < nLen; ++index )
332 uno::Reference< form::XFormsSupplier > xFormSupplier( xIndex->getByIndex( index ), uno::UNO_QUERY_THROW );
333 uno::Reference< container::XIndexAccess > xFormIndex( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
334 // get the www-standard container
335 uno::Reference< container::XIndexAccess > xFormControls( xFormIndex->getByIndex(0), uno::UNO_QUERY_THROW );
336 sal_Int32 nCntrls = xFormControls->getCount();
337 for( sal_Int32 cIndex = 0; cIndex < nCntrls; ++cIndex )
339 uno::Reference< uno::XInterface > xControl( xFormControls->getByIndex( cIndex ), uno::UNO_QUERY_THROW );
340 bMatched = ( m_xProps == xControl );
341 if ( bMatched )
343 nRefTab = index;
344 break;
348 catch( uno::Exception& ) {}
349 if ( bMatched )
350 break;
353 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, _controlsource, "", sal_uInt16( nRefTab ) );
356 OUString SAL_CALL
357 ScVbaControl::getRowSource()
359 OUString sRowSource;
360 uno::Reference< form::binding::XListEntrySink > xListSink( m_xProps, uno::UNO_QUERY );
361 if ( xListSink.is() )
365 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
366 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellRangeAddressConversion" ), uno::UNO_QUERY );
368 uno::Reference< beans::XPropertySet > xProps( xListSink->getListEntrySource(), uno::UNO_QUERY_THROW );
369 table::CellRangeAddress aAddress;
370 xProps->getPropertyValue( "CellRange" ) >>= aAddress;
371 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
372 xConvertor->getPropertyValue( "XLA1Representation" ) >>= sRowSource;
374 catch(const uno::Exception&)
378 return sRowSource;
381 void SAL_CALL
382 ScVbaControl::setRowSource( const OUString& _rowsource )
384 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, "", _rowsource );
387 OUString SAL_CALL
388 ScVbaControl::getName()
390 OUString sName;
391 m_xProps->getPropertyValue( "Name" ) >>= sName;
392 return sName;
396 void SAL_CALL
397 ScVbaControl::setName( const OUString& _name )
399 m_xProps->setPropertyValue( "Name" , uno::makeAny( _name ) );
402 OUString SAL_CALL
403 ScVbaControl::getControlTipText()
405 OUString sName;
406 m_xProps->getPropertyValue( "HelpText" ) >>= sName;
407 return sName;
410 void SAL_CALL
411 ScVbaControl::setControlTipText( const OUString& rsToolTip )
413 m_xProps->setPropertyValue( "HelpText" , uno::makeAny( rsToolTip ) );
416 OUString SAL_CALL ScVbaControl::getTag()
418 return m_aControlTag;
421 void SAL_CALL ScVbaControl::setTag( const OUString& aTag )
423 m_aControlTag = aTag;
426 ::sal_Int32 SAL_CALL ScVbaControl::getForeColor()
428 Color nForeColor;
429 m_xProps->getPropertyValue( "TextColor" ) >>= nForeColor;
430 return OORGBToXLRGB( nForeColor );
434 struct PointerStyles
436 long const msoPointerStyle;
437 PointerStyle const loPointStyle;
440 // 1 -> 1 map of styles ( some dubious choices in there though )
441 PointerStyles const styles[] = {
442 /// assuming pointer default is Arrow
443 { msforms::fmMousePointer::fmMousePointerDefault, PointerStyle::Arrow },
444 { msforms::fmMousePointer::fmMousePointerArrow, PointerStyle::Arrow },
445 { msforms::fmMousePointer::fmMousePointerCross, PointerStyle::Cross },
446 { msforms::fmMousePointer::fmMousePointerIBeam, PointerStyle::Text },
447 { msforms::fmMousePointer::fmMousePointerSizeNESW, PointerStyle::AutoScrollNSWE }, // #TODO not correct, need to check, need to find the right one
448 { msforms::fmMousePointer::fmMousePointerSizeNS, PointerStyle::AutoScrollNS },
449 { msforms::fmMousePointer::fmMousePointerSizeNWSE, PointerStyle::AutoScrollNSWE }, // #TODO not correct, need to check, need to find the right one
450 { msforms::fmMousePointer::fmMousePointerSizeWE, PointerStyle::AutoScrollWE },
451 { msforms::fmMousePointer::fmMousePointerUpArrow, PointerStyle::WindowNSize },
452 { msforms::fmMousePointer::fmMousePointerHourGlass, PointerStyle::Wait },
453 { msforms::fmMousePointer::fmMousePointerNoDrop, PointerStyle::NotAllowed },
454 { msforms::fmMousePointer::fmMousePointerAppStarting, PointerStyle::Wait },
455 { msforms::fmMousePointer::fmMousePointerHelp, PointerStyle::Help },
456 { msforms::fmMousePointer::fmMousePointerSizeAll, PointerStyle::Cross },
457 { msforms::fmMousePointer::fmMousePointerCustom, PointerStyle::Arrow }, // not supported I guess
461 static long lcl_loPointerToMsoPointer( PointerStyle eType )
463 long nRet = msforms::fmMousePointer::fmMousePointerDefault;
464 for ( int i = 0; i < int(SAL_N_ELEMENTS( styles )); ++i )
466 if ( styles[ i ].loPointStyle == eType )
468 nRet = styles[ i ].msoPointerStyle;
469 break;
472 return nRet;
475 static PointerStyle lcl_msoPointerToLOPointer( long msoPointerStyle )
477 PointerStyle aPointer( PointerStyle::Arrow );
478 for ( int i = 0; i < int(SAL_N_ELEMENTS( styles )); ++i )
480 if ( styles[ i ].msoPointerStyle == msoPointerStyle )
482 aPointer = styles[ i ].loPointStyle;
483 break;
486 return aPointer;
489 ::sal_Int32 SAL_CALL
490 ScVbaControl::getMousePointer()
492 PointerStyle eType = PointerStyle::Arrow; // default ?
493 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( getWindowPeer() );
494 if ( pWindow )
496 eType = pWindow->GetPointer();
498 return lcl_loPointerToMsoPointer( eType );
501 void SAL_CALL
502 ScVbaControl::setMousePointer( ::sal_Int32 _mousepointer )
504 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( getWindowPeer() );
505 if ( pWindow )
507 PointerStyle aPointer = lcl_msoPointerToLOPointer( _mousepointer );
508 pWindow->SetPointer( aPointer );
512 void SAL_CALL ScVbaControl::fireEvent( const script::ScriptEvent& rEvt )
514 script::ScriptEvent evt( rEvt );
515 uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
516 uno::Reference< script::XScriptListener > xScriptListener( xServiceManager->createInstanceWithContext( "ooo.vba.EventListener" , mxContext ), uno::UNO_QUERY_THROW );
518 uno::Reference< beans::XPropertySet > xProps( xScriptListener, uno::UNO_QUERY_THROW );
519 xProps->setPropertyValue( "Model" , uno::makeAny( m_xModel ) );
521 // handling for sheet control
522 uno::Reference< msforms::XControl > xThisControl( this );
525 evt.Arguments.realloc( 1 );
526 lang::EventObject aEvt;
528 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
529 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY ) ;
531 if ( xControlShape.is() )
533 evt.Source = xControlShape;
534 aEvt.Source = m_xEmptyFormControl;
535 // Set up proper scriptcode
536 uno::Reference< lang::XMultiServiceFactory > xDocFac( m_xModel, uno::UNO_QUERY_THROW );
537 uno::Reference< document::XCodeNameQuery > xNameQuery( xDocFac->createInstance( "ooo.vba.VBACodeNameProvider" ), uno::UNO_QUERY_THROW );
538 uno::Reference< uno::XInterface > xIf( xControlShape->getControl(), uno::UNO_QUERY_THROW );
539 evt.ScriptCode = xNameQuery->getCodeNameForObject( xIf );
540 // handle if we passed in our own arguments
541 if ( !rEvt.Arguments.hasElements() )
542 evt.Arguments[ 0 ] <<= aEvt;
543 xScriptListener->firing( evt );
545 else
547 if ( xControl.is() ) // normal control ( from dialog/userform )
549 // #FIXME We should probably store a reference to the
550 // parent dialog/userform here ( other wise the name of
551 // dialog could be changed and we won't be aware of it.
552 // ( OTOH this is probably an unlikely scenario )
553 evt.Source = xThisControl;
554 aEvt.Source = xControl;
555 evt.ScriptCode = m_sLibraryAndCodeName;
556 evt.Arguments[ 0 ] <<= aEvt;
557 xScriptListener->firing( evt );
561 catch(const uno::Exception&)
565 void ScVbaControl::fireChangeEvent()
567 script::ScriptEvent evt;
568 evt.ScriptType = "VBAInterop";
569 evt.ListenerType = cppu::UnoType<form::XChangeListener>::get();
570 evt.MethodName = "changed";
571 fireEvent( evt );
574 void ScVbaControl::fireClickEvent()
576 script::ScriptEvent evt;
577 evt.ScriptType = "VBAInterop";
578 evt.ListenerType = cppu::UnoType<awt::XActionListener>::get();
579 evt.MethodName = "actionPerformed";
580 fireEvent( evt );
583 sal_Int32 SAL_CALL ScVbaControl::getTabIndex()
585 return 1;
588 void SAL_CALL ScVbaControl::setTabIndex( sal_Int32 /*nTabIndex*/ )
592 //ScVbaControlFactory
594 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createShapeControl(
595 const uno::Reference< uno::XComponentContext >& xContext,
596 const uno::Reference< drawing::XControlShape >& xControlShape,
597 const uno::Reference< frame::XModel >& xModel )
599 uno::Reference< beans::XPropertySet > xProps( xControlShape->getControl(), uno::UNO_QUERY_THROW );
600 sal_Int32 nClassId = -1;
601 xProps->getPropertyValue( "ClassId" ) >>= nClassId;
602 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
603 uno::Reference< drawing::XShape > xShape( xControlShape, uno::UNO_QUERY_THROW );
604 ::std::unique_ptr< ConcreteXShapeGeometryAttributes > xGeoHelper( new ConcreteXShapeGeometryAttributes( xShape ) );
605 switch( nClassId )
607 case form::FormComponentType::COMBOBOX:
608 return new ScVbaComboBox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
609 case form::FormComponentType::COMMANDBUTTON:
611 bool bToggle = false;
612 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
613 if ( bToggle )
614 return new ScVbaToggleButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
615 else
616 return new VbaButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
618 case form::FormComponentType::FIXEDTEXT:
619 return new ScVbaLabel( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
620 case form::FormComponentType::TEXTFIELD:
621 return new ScVbaTextBox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
622 case form::FormComponentType::CHECKBOX:
623 return new ScVbaCheckbox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
624 case form::FormComponentType::RADIOBUTTON:
625 return new ScVbaRadioButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
626 case form::FormComponentType::LISTBOX:
627 return new ScVbaListBox( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
628 case form::FormComponentType::SPINBUTTON:
629 return new ScVbaSpinButton( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
630 case form::FormComponentType::IMAGECONTROL:
631 return new ScVbaImage( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
632 case form::FormComponentType::SCROLLBAR:
633 return new ScVbaScrollBar( xVbaParent, xContext, xControlShape, xModel, std::move(xGeoHelper) );
635 throw uno::RuntimeException( "Unsupported control." );
638 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createUserformControl(
639 const uno::Reference< uno::XComponentContext >& xContext,
640 const uno::Reference< awt::XControl >& xControl,
641 const uno::Reference< awt::XControl >& xDialog,
642 const uno::Reference< frame::XModel >& xModel,
643 double fOffsetX, double fOffsetY )
645 uno::Reference< beans::XPropertySet > xProps( xControl->getModel(), uno::UNO_QUERY_THROW );
646 uno::Reference< lang::XServiceInfo > xServiceInfo( xProps, uno::UNO_QUERY_THROW );
647 uno::Reference< msforms::XControl > xVBAControl;
648 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
649 ::std::unique_ptr< UserFormGeometryHelper > xGeoHelper( new UserFormGeometryHelper( xControl, fOffsetX, fOffsetY ) );
651 if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlCheckBoxModel" ) )
652 xVBAControl.set( new ScVbaCheckbox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
653 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlRadioButtonModel" ) )
654 xVBAControl.set( new ScVbaRadioButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
655 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlEditModel" ) )
656 xVBAControl.set( new ScVbaTextBox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper), true ) );
657 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlButtonModel" ) )
659 bool bToggle = false;
660 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
661 if ( bToggle )
662 xVBAControl.set( new ScVbaToggleButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
663 else
664 xVBAControl.set( new VbaButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
666 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlComboBoxModel" ) )
667 xVBAControl.set( new ScVbaComboBox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
668 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlListBoxModel" ) )
669 xVBAControl.set( new ScVbaListBox( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
670 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedTextModel" ) )
671 xVBAControl.set( new ScVbaLabel( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
672 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlImageControlModel" ) )
673 xVBAControl.set( new ScVbaImage( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
674 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlProgressBarModel" ) )
675 xVBAControl.set( new ScVbaProgressBar( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
676 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlGroupBoxModel" ) )
677 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper), xDialog ) );
678 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlScrollBarModel" ) )
679 xVBAControl.set( new ScVbaScrollBar( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
680 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoMultiPageModel" ) )
681 xVBAControl.set( new ScVbaMultiPage( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
682 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlSpinButtonModel" ) )
683 xVBAControl.set( new ScVbaSpinButton( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
684 else if ( xServiceInfo->supportsService( "com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ) )
685 xVBAControl.set( new VbaSystemAXControl( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
686 // #FIXME implement a page control
687 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoPageModel" ) )
688 xVBAControl.set( new ScVbaControl( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper) ) );
689 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoFrameModel" ) )
690 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, std::move(xGeoHelper), xDialog ) );
691 if( xVBAControl.is() )
692 return xVBAControl;
693 throw uno::RuntimeException( "Unsupported control." );
696 OUString
697 ScVbaControl::getServiceImplName()
699 return OUString("ScVbaControl");
702 uno::Sequence< OUString >
703 ScVbaControl::getServiceNames()
705 static uno::Sequence< OUString > const aServiceNames
707 "ooo.vba.excel.Control"
709 return aServiceNames;
712 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 };
714 sal_Int32 ScVbaControl::getBackColor()
716 sal_Int32 nBackColor = 0;
717 m_xProps->getPropertyValue( "BackgroundColor" ) >>= nBackColor;
718 return nBackColor;
721 void ScVbaControl::setBackColor( sal_Int32 nBackColor )
723 if ( ( static_cast<sal_uInt32>(nBackColor) >= sal_uInt32(0x80000000) ) &&
724 ( static_cast<sal_uInt32>(nBackColor) <= sal_uInt32(0x80000000) + SAL_N_ELEMENTS(nSysCols) ) )
726 nBackColor = nSysCols[ nBackColor & 0x0FF];
728 m_xProps->setPropertyValue( "BackgroundColor" , uno::makeAny( XLRGBToOORGB( nBackColor ) ) );
731 bool ScVbaControl::getAutoSize()
733 bool bIsResizeEnabled = false;
734 uno::Reference< uno::XInterface > xIf( m_xControl, uno::UNO_SET_THROW );
735 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xIf );
736 if ( pObj )
737 bIsResizeEnabled = !pObj->IsResizeProtect();
738 return bIsResizeEnabled;
741 // currently no implementation for this
742 void ScVbaControl::setAutoSize( bool bAutoSize )
744 uno::Reference< uno::XInterface > xIf( m_xControl, uno::UNO_SET_THROW );
745 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xIf );
746 if ( pObj )
747 pObj->SetResizeProtect( !bAutoSize );
750 bool ScVbaControl::getLocked()
752 bool bRes( false );
753 m_xProps->getPropertyValue( "ReadOnly" ) >>= bRes;
754 return bRes;
757 void ScVbaControl::setLocked( bool bLocked )
759 m_xProps->setPropertyValue( "ReadOnly" , uno::makeAny( bLocked ) );
762 class ControlProviderImpl : public cppu::WeakImplHelper< XControlProvider >
764 uno::Reference< uno::XComponentContext > m_xCtx;
765 public:
766 explicit ControlProviderImpl( const uno::Reference< uno::XComponentContext >& xCtx ) : m_xCtx( xCtx ) {}
767 virtual uno::Reference< msforms::XControl > SAL_CALL createControl( const uno::Reference< drawing::XControlShape >& xControl, const uno::Reference< frame::XModel >& xDocOwner ) override;
770 uno::Reference< msforms::XControl > SAL_CALL
771 ControlProviderImpl::createControl( const uno::Reference< drawing::XControlShape >& xControlShape, const uno::Reference< frame::XModel >& xDocOwner )
773 uno::Reference< msforms::XControl > xControlToReturn;
774 if ( xControlShape.is() )
775 xControlToReturn = ScVbaControlFactory::createShapeControl( m_xCtx, xControlShape, xDocOwner );
776 return xControlToReturn;
780 namespace controlprovider
782 namespace sdecl = comphelper::service_decl;
783 sdecl::class_<ControlProviderImpl, sdecl::with_args<false> > const serviceImpl;
784 sdecl::ServiceDecl const serviceDecl(
785 serviceImpl,
786 "ControlProviderImpl",
787 "ooo.vba.ControlProvider" );
791 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */