bump product version to 5.0.4.1
[LibreOffice.git] / vbahelper / source / msforms / vbacontrol.cxx
blob56bc102c6a1589883360ea60c4180b662b61bb22
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 <com/sun/star/form/FormComponentType.hpp>
20 #include <com/sun/star/awt/XControlModel.hpp>
21 #include <com/sun/star/awt/XControl.hpp>
22 #include <com/sun/star/awt/XWindow2.hpp>
23 #include <com/sun/star/awt/XActionListener.hpp>
24 #include <com/sun/star/lang/XEventListener.hpp>
25 #include <com/sun/star/drawing/XShape.hpp>
26 #include <com/sun/star/drawing/XControlShape.hpp>
27 #include <com/sun/star/frame/XModel.hpp>
28 #include <com/sun/star/view/XControlAccess.hpp>
29 #include <com/sun/star/container/XChild.hpp>
30 #include <com/sun/star/form/binding/XBindableValue.hpp>
31 #include <com/sun/star/form/binding/XListEntrySink.hpp>
32 #include <com/sun/star/table/CellAddress.hpp>
33 #include <com/sun/star/table/CellRangeAddress.hpp>
34 #include <com/sun/star/script/XScriptListener.hpp>
35 #include <com/sun/star/document/XCodeNameQuery.hpp>
36 #include <com/sun/star/form/XChangeListener.hpp>
37 #include <ooo/vba/XControlProvider.hpp>
38 #include <ooo/vba/msforms/fmMousePointer.hpp>
39 #include <svtools/bindablecontrolhelper.hxx>
40 #include "vbacontrol.hxx"
41 #include "vbacombobox.hxx"
42 #include "vbabutton.hxx"
43 #include "vbalabel.hxx"
44 #include "vbatextbox.hxx"
45 #include "vbaradiobutton.hxx"
46 #include "vbalistbox.hxx"
47 #include "vbatogglebutton.hxx"
48 #include "vbacheckbox.hxx"
49 #include "vbaframe.hxx"
50 #include "vbascrollbar.hxx"
51 #include "vbaprogressbar.hxx"
52 #include "vbamultipage.hxx"
53 #include "vbaspinbutton.hxx"
54 #include "vbasystemaxcontrol.hxx"
55 #include "vbaimage.hxx"
56 #include <vbahelper/helperdecl.hxx>
57 #include <toolkit/helper/vclunohelper.hxx>
58 #include <vcl/window.hxx>
59 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
60 #include <com/sun/star/form/XFormsSupplier.hpp>
61 #include <svx/svdobj.hxx>
63 using namespace com::sun::star;
64 using namespace ooo::vba;
66 uno::Reference< css::awt::XWindowPeer >
67 ScVbaControl::getWindowPeer() throw (uno::RuntimeException)
69 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
71 uno::Reference< awt::XControlModel > xControlModel;
72 uno::Reference< css::awt::XWindowPeer > xWinPeer;
73 if ( !xControlShape.is() )
75 // would seem to be a Userform control
76 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY_THROW );
77 xWinPeer = xControl->getPeer();
78 return xWinPeer;
80 // form control
81 xControlModel.set( xControlShape->getControl(), uno::UNO_QUERY_THROW );
83 uno::Reference< view::XControlAccess > xControlAccess( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
84 try
86 uno::Reference< awt::XControl > xControl( xControlAccess->getControl( xControlModel ), uno::UNO_QUERY );
87 xWinPeer = xControl->getPeer();
89 catch(const uno::Exception&)
91 throw uno::RuntimeException( "The Control does not exist" );
93 return xWinPeer;
96 //ScVbaControlListener
97 class ScVbaControlListener: public cppu::WeakImplHelper1< lang::XEventListener >
99 private:
100 ScVbaControl *pControl;
101 public:
102 ScVbaControlListener( ScVbaControl *pTmpControl );
103 virtual ~ScVbaControlListener();
104 virtual void SAL_CALL disposing( const lang::EventObject& rEventObject ) throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
107 ScVbaControlListener::ScVbaControlListener( ScVbaControl *pTmpControl ): pControl( pTmpControl )
111 ScVbaControlListener::~ScVbaControlListener()
115 void SAL_CALL
116 ScVbaControlListener::disposing( const lang::EventObject& ) throw( uno::RuntimeException, std::exception )
118 if( pControl )
120 pControl->removeResource();
121 pControl = NULL;
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, AbstractGeometryAttributes* pGeomHelper ) : ControlImpl_BASE( xParent, xContext ), bIsDialog(false), m_xControl( xControl ), m_xModel( xModel )
129 //add listener
130 m_xEventListener.set( new ScVbaControlListener( this ) );
131 setGeometryHelper( pGeomHelper );
132 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
133 xComponent->addEventListener( m_xEventListener );
135 //init m_xProps
136 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
137 uno::Reference< awt::XControl> xUserFormControl( m_xControl, uno::UNO_QUERY ) ;
138 if ( xControlShape.is() ) // form control
140 m_xProps.set( xControlShape->getControl(), uno::UNO_QUERY_THROW );
141 OUString sDefaultControl;
142 m_xProps->getPropertyValue( "DefaultControl" ) >>= sDefaultControl;
143 uno::Reference< lang::XMultiComponentFactory > xMFac( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
144 m_xEmptyFormControl.set( xMFac->createInstanceWithContext( sDefaultControl, mxContext ), uno::UNO_QUERY_THROW );
146 else if ( xUserFormControl.is() ) // userform control
148 m_xProps.set( xUserFormControl->getModel(), uno::UNO_QUERY_THROW );
149 bIsDialog = true;
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( AbstractGeometryAttributes* pHelper )
165 mpGeometryHelper.reset( pHelper );
168 void ScVbaControl::removeResource() throw( uno::RuntimeException )
170 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
171 xComponent->removeEventListener( m_xEventListener );
172 m_xControl= NULL;
173 m_xProps = NULL;
176 //In design model has different behavior
177 sal_Bool SAL_CALL ScVbaControl::getEnabled() throw (uno::RuntimeException, std::exception)
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 ) throw (uno::RuntimeException, std::exception)
187 uno::Any aValue( bVisible );
188 m_xProps->setPropertyValue( "Enabled" , aValue);
192 sal_Bool SAL_CALL ScVbaControl::getVisible() throw (uno::RuntimeException, std::exception)
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 ) throw (uno::RuntimeException, std::exception)
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() throw (uno::RuntimeException, std::exception)
222 return mpGeometryHelper->getHeight();
224 void SAL_CALL ScVbaControl::setHeight( double _height ) throw (uno::RuntimeException, std::exception)
226 mpGeometryHelper->setHeight( _height );
229 double SAL_CALL ScVbaControl::getWidth() throw (uno::RuntimeException, std::exception)
231 return mpGeometryHelper->getWidth();
233 void SAL_CALL ScVbaControl::setWidth( double _width ) throw (uno::RuntimeException, std::exception)
235 mpGeometryHelper->setWidth( _width );
238 double SAL_CALL
239 ScVbaControl::getLeft() throw (uno::RuntimeException, std::exception)
241 return mpGeometryHelper->getLeft();
244 void SAL_CALL
245 ScVbaControl::setLeft( double _left ) throw (uno::RuntimeException, std::exception)
247 mpGeometryHelper->setLeft( _left );
250 double SAL_CALL
251 ScVbaControl::getTop() throw (uno::RuntimeException, std::exception)
253 return mpGeometryHelper->getTop();
256 void SAL_CALL
257 ScVbaControl::setTop( double _top ) throw (uno::RuntimeException, std::exception)
259 mpGeometryHelper->setTop( _top );
262 uno::Reference< uno::XInterface > SAL_CALL
263 ScVbaControl::getObject() throw (uno::RuntimeException, std::exception)
265 uno::Reference< msforms::XControl > xRet( this );
266 return xRet;
269 void SAL_CALL ScVbaControl::SetFocus() throw (uno::RuntimeException, std::exception)
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 )
276 throw ( uno::RuntimeException, std::exception )
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() throw (uno::RuntimeException, std::exception)
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 ) throw (uno::RuntimeException, std::exception)
321 OUString sEmpty;
322 // afaik this is only relevant for Excel documents ( and we need to set up a
323 // reference tab in case no Sheet is specified in "_controlsource"
324 // Can't use the active sheet either, code may of course access
325 uno::Reference< drawing::XDrawPagesSupplier > xSupplier( m_xModel, uno::UNO_QUERY_THROW );
326 uno::Reference< container::XIndexAccess > xIndex( xSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
327 sal_Int32 nLen = xIndex->getCount();
328 bool bMatched = false;
329 sal_Int16 nRefTab = 0;
330 for ( sal_Int32 index = 0; index < nLen; ++index )
334 uno::Reference< form::XFormsSupplier > xFormSupplier( xIndex->getByIndex( index ), uno::UNO_QUERY_THROW );
335 uno::Reference< container::XIndexAccess > xFormIndex( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
336 // get the www-standard container
337 uno::Reference< container::XIndexAccess > xFormControls( xFormIndex->getByIndex(0), uno::UNO_QUERY_THROW );
338 sal_Int32 nCntrls = xFormControls->getCount();
339 for( sal_Int32 cIndex = 0; cIndex < nCntrls; ++cIndex )
341 uno::Reference< uno::XInterface > xControl( xFormControls->getByIndex( cIndex ), uno::UNO_QUERY_THROW );
342 bMatched = ( m_xProps == xControl );
343 if ( bMatched )
345 nRefTab = index;
346 break;
350 catch( uno::Exception& ) {}
351 if ( bMatched )
352 break;
355 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, _controlsource, sEmpty, sal_uInt16( nRefTab ) );
358 OUString SAL_CALL
359 ScVbaControl::getRowSource() throw (uno::RuntimeException, std::exception)
361 OUString sRowSource;
362 uno::Reference< form::binding::XListEntrySink > xListSink( m_xProps, uno::UNO_QUERY );
363 if ( xListSink.is() )
367 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
368 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellRangeAddressConversion" ), uno::UNO_QUERY );
370 uno::Reference< beans::XPropertySet > xProps( xListSink->getListEntrySource(), uno::UNO_QUERY_THROW );
371 table::CellRangeAddress aAddress;
372 xProps->getPropertyValue( "CellRange" ) >>= aAddress;
373 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
374 xConvertor->getPropertyValue( "XLA1Representation" ) >>= sRowSource;
376 catch(const uno::Exception&)
380 return sRowSource;
383 void SAL_CALL
384 ScVbaControl::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException, std::exception)
386 OUString sEmpty;
387 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, sEmpty, _rowsource );
390 OUString SAL_CALL
391 ScVbaControl::getName() throw (uno::RuntimeException, std::exception)
393 OUString sName;
394 m_xProps->getPropertyValue( "Name" ) >>= sName;
395 return sName;
399 void SAL_CALL
400 ScVbaControl::setName( const OUString& _name ) throw (uno::RuntimeException, std::exception)
402 m_xProps->setPropertyValue( "Name" , uno::makeAny( _name ) );
405 OUString SAL_CALL
406 ScVbaControl::getControlTipText() throw (css::uno::RuntimeException, std::exception)
408 OUString sName;
409 m_xProps->getPropertyValue( "HelpText" ) >>= sName;
410 return sName;
413 void SAL_CALL
414 ScVbaControl::setControlTipText( const OUString& rsToolTip ) throw (css::uno::RuntimeException, std::exception)
416 m_xProps->setPropertyValue( "HelpText" , uno::makeAny( rsToolTip ) );
419 OUString SAL_CALL ScVbaControl::getTag()
420 throw (css::uno::RuntimeException, std::exception)
422 return m_aControlTag;
425 void SAL_CALL ScVbaControl::setTag( const OUString& aTag )
426 throw (css::uno::RuntimeException, std::exception)
428 m_aControlTag = aTag;
431 ::sal_Int32 SAL_CALL ScVbaControl::getForeColor() throw (::com::sun::star::uno::RuntimeException)
433 sal_Int32 nForeColor = -1;
434 m_xProps->getPropertyValue( "TextColor" ) >>= nForeColor;
435 return OORGBToXLRGB( nForeColor );
438 void SAL_CALL ScVbaControl::setForeColor( ::sal_Int32 _forecolor ) throw (::com::sun::star::uno::RuntimeException)
440 m_xProps->setPropertyValue( "TextColor" , uno::makeAny( XLRGBToOORGB( _forecolor ) ) );
443 struct PointerStyles
445 long msoPointerStyle;
446 PointerStyle loPointStyle;
449 // 1 -> 1 map of styles ( some dubious choices in there though )
450 PointerStyles styles[] = {
451 /// assuming pointer default is Arrow
452 { msforms::fmMousePointer::fmMousePointerDefault, PointerStyle::Arrow },
453 { msforms::fmMousePointer::fmMousePointerArrow, PointerStyle::Arrow },
454 { msforms::fmMousePointer::fmMousePointerCross, PointerStyle::Cross },
455 { msforms::fmMousePointer::fmMousePointerIBeam, PointerStyle::Text },
456 { msforms::fmMousePointer::fmMousePointerSizeNESW, PointerStyle::AutoScrollNSWE }, // #TODO not correct, need to check, need to find the right one
457 { msforms::fmMousePointer::fmMousePointerSizeNS, PointerStyle::AutoScrollNS },
458 { msforms::fmMousePointer::fmMousePointerSizeNWSE, PointerStyle::AutoScrollNSWE }, // #TODO not correct, need to check, need to find the right one
459 { msforms::fmMousePointer::fmMousePointerSizeWE, PointerStyle::AutoScrollWE },
460 { msforms::fmMousePointer::fmMousePointerUpArrow, PointerStyle::WindowNSize },
461 { msforms::fmMousePointer::fmMousePointerHourGlass, PointerStyle::Wait },
462 { msforms::fmMousePointer::fmMousePointerNoDrop, PointerStyle::NotAllowed },
463 { msforms::fmMousePointer::fmMousePointerAppStarting, PointerStyle::Wait },
464 { msforms::fmMousePointer::fmMousePointerHelp, PointerStyle::Help },
465 { msforms::fmMousePointer::fmMousePointerSizeAll, PointerStyle::Cross },
466 { msforms::fmMousePointer::fmMousePointerCustom, PointerStyle::Arrow }, // not supported I guess
470 static long lcl_loPointerToMsoPointer( PointerStyle eType )
472 long nRet = msforms::fmMousePointer::fmMousePointerDefault;
473 for ( int i = 0, nElems = SAL_N_ELEMENTS( styles ); i < nElems; ++i )
475 if ( styles[ i ].loPointStyle == eType )
477 nRet = styles[ i ].msoPointerStyle;
478 break;
481 return nRet;
484 static Pointer lcl_msoPointerToLOPointer( long msoPointerStyle )
486 Pointer aPointer( PointerStyle::Arrow );
487 for ( int i = 0, nElems = SAL_N_ELEMENTS( styles ); i < nElems; ++i )
489 if ( styles[ i ].msoPointerStyle == msoPointerStyle )
491 aPointer = Pointer( styles[ i ].loPointStyle );
492 break;
495 return aPointer;
498 ::sal_Int32 SAL_CALL
499 ScVbaControl::getMousePointer() throw (::com::sun::star::uno::RuntimeException, std::exception)
501 PointerStyle eType = PointerStyle::Arrow; // default ?
502 vcl::Window* pWindow = VCLUnoHelper::GetWindow( getWindowPeer() );
503 if ( pWindow )
505 eType = pWindow->GetPointer().GetStyle();
507 return lcl_loPointerToMsoPointer( eType );
510 void SAL_CALL
511 ScVbaControl::setMousePointer( ::sal_Int32 _mousepointer ) throw (::com::sun::star::uno::RuntimeException, std::exception)
513 vcl::Window* pWindow = VCLUnoHelper::GetWindow( getWindowPeer() );
514 if ( pWindow )
516 Pointer aPointer( PointerStyle::Arrow );
517 aPointer = lcl_msoPointerToLOPointer( _mousepointer );
518 pWindow->SetPointer( aPointer );
522 void SAL_CALL ScVbaControl::fireEvent( const script::ScriptEvent& rEvt ) throw (uno::RuntimeException, std::exception)
524 script::ScriptEvent evt( rEvt );
525 uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
526 uno::Reference< script::XScriptListener > xScriptListener( xServiceManager->createInstanceWithContext( "ooo.vba.EventListener" , mxContext ), uno::UNO_QUERY_THROW );
528 uno::Reference< beans::XPropertySet > xProps( xScriptListener, uno::UNO_QUERY_THROW );
529 xProps->setPropertyValue( "Model" , uno::makeAny( m_xModel ) );
531 // handling for sheet control
532 uno::Reference< msforms::XControl > xThisControl( this );
535 evt.Arguments.realloc( 1 );
536 lang::EventObject aEvt;
538 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
539 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY ) ;
541 if ( xControlShape.is() )
543 evt.Source = xControlShape;
544 aEvt.Source = m_xEmptyFormControl;
545 // Set up proper scriptcode
546 uno::Reference< lang::XMultiServiceFactory > xDocFac( m_xModel, uno::UNO_QUERY_THROW );
547 uno::Reference< document::XCodeNameQuery > xNameQuery( xDocFac->createInstance( "ooo.vba.VBACodeNameProvider" ), uno::UNO_QUERY_THROW );
548 uno::Reference< uno::XInterface > xIf( xControlShape->getControl(), uno::UNO_QUERY_THROW );
549 evt.ScriptCode = xNameQuery->getCodeNameForObject( xIf );
550 // handle if we passed in our own arguments
551 if ( !rEvt.Arguments.getLength() )
552 evt.Arguments[ 0 ] = uno::makeAny( aEvt );
553 xScriptListener->firing( evt );
555 else
557 if ( xControl.is() ) // normal control ( from dialog/userform )
559 // #FIXME We should probably store a reference to the
560 // parent dialog/userform here ( other wise the name of
561 // dialog could be changed and we won't be aware of it.
562 // ( OTOH this is probably an unlikely scenario )
563 evt.Source = xThisControl;
564 aEvt.Source = xControl;
565 evt.ScriptCode = m_sLibraryAndCodeName;
566 evt.Arguments[ 0 ] = uno::makeAny( aEvt );
567 xScriptListener->firing( evt );
571 catch(const uno::Exception&)
575 void ScVbaControl::fireChangeEvent()
577 script::ScriptEvent evt;
578 evt.ScriptType = "VBAInterop";
579 evt.ListenerType = cppu::UnoType<form::XChangeListener>::get();
580 evt.MethodName = "changed";
581 fireEvent( evt );
584 void ScVbaControl::fireClickEvent()
586 script::ScriptEvent evt;
587 evt.ScriptType = "VBAInterop";
588 evt.ListenerType = cppu::UnoType<awt::XActionListener>::get();
589 evt.MethodName = "actionPerformed";
590 fireEvent( evt );
593 sal_Int32 SAL_CALL ScVbaControl::getTabIndex() throw (uno::RuntimeException, std::exception)
595 return 1;
598 void SAL_CALL ScVbaControl::setTabIndex( sal_Int32 /*nTabIndex*/ ) throw (uno::RuntimeException, std::exception)
602 //ScVbaControlFactory
604 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createShapeControl(
605 const uno::Reference< uno::XComponentContext >& xContext,
606 const uno::Reference< drawing::XControlShape >& xControlShape,
607 const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException)
609 uno::Reference< beans::XPropertySet > xProps( xControlShape->getControl(), uno::UNO_QUERY_THROW );
610 sal_Int32 nClassId = -1;
611 xProps->getPropertyValue( "ClassId" ) >>= nClassId;
612 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
613 uno::Reference< drawing::XShape > xShape( xControlShape, uno::UNO_QUERY_THROW );
614 ::std::unique_ptr< ConcreteXShapeGeometryAttributes > xGeoHelper( new ConcreteXShapeGeometryAttributes( xContext, xShape ) );
615 switch( nClassId )
617 case form::FormComponentType::COMBOBOX:
618 return new ScVbaComboBox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
619 case form::FormComponentType::COMMANDBUTTON:
621 bool bToggle = false;
622 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
623 if ( bToggle )
624 return new ScVbaToggleButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
625 else
626 return new VbaButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
628 case form::FormComponentType::FIXEDTEXT:
629 return new ScVbaLabel( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
630 case form::FormComponentType::TEXTFIELD:
631 return new ScVbaTextBox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
632 case form::FormComponentType::CHECKBOX:
633 return new ScVbaCheckbox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
634 case form::FormComponentType::RADIOBUTTON:
635 return new ScVbaRadioButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
636 case form::FormComponentType::LISTBOX:
637 return new ScVbaListBox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
638 case form::FormComponentType::SPINBUTTON:
639 return new ScVbaSpinButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
640 case form::FormComponentType::IMAGECONTROL:
641 return new ScVbaImage( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
642 case form::FormComponentType::SCROLLBAR:
643 return new ScVbaScrollBar( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
645 throw uno::RuntimeException( "Unsupported control." );
648 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createUserformControl(
649 const uno::Reference< uno::XComponentContext >& xContext,
650 const uno::Reference< awt::XControl >& xControl,
651 const uno::Reference< awt::XControl >& xDialog,
652 const uno::Reference< frame::XModel >& xModel,
653 double fOffsetX, double fOffsetY ) throw (uno::RuntimeException)
655 uno::Reference< beans::XPropertySet > xProps( xControl->getModel(), uno::UNO_QUERY_THROW );
656 uno::Reference< lang::XServiceInfo > xServiceInfo( xProps, uno::UNO_QUERY_THROW );
657 uno::Reference< msforms::XControl > xVBAControl;
658 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
659 ::std::unique_ptr< UserFormGeometryHelper > xGeoHelper( new UserFormGeometryHelper( xContext, xControl, fOffsetX, fOffsetY ) );
661 if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlCheckBoxModel" ) )
662 xVBAControl.set( new ScVbaCheckbox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
663 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlRadioButtonModel" ) )
664 xVBAControl.set( new ScVbaRadioButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
665 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlEditModel" ) )
666 xVBAControl.set( new ScVbaTextBox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release(), true ) );
667 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlButtonModel" ) )
669 bool bToggle = false;
670 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
671 if ( bToggle )
672 xVBAControl.set( new ScVbaToggleButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
673 else
674 xVBAControl.set( new VbaButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
676 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlComboBoxModel" ) )
677 xVBAControl.set( new ScVbaComboBox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
678 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlListBoxModel" ) )
679 xVBAControl.set( new ScVbaListBox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
680 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedTextModel" ) )
681 xVBAControl.set( new ScVbaLabel( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
682 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlImageControlModel" ) )
683 xVBAControl.set( new ScVbaImage( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
684 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlProgressBarModel" ) )
685 xVBAControl.set( new ScVbaProgressBar( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
686 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlGroupBoxModel" ) )
687 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, xGeoHelper.release(), xDialog ) );
688 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlScrollBarModel" ) )
689 xVBAControl.set( new ScVbaScrollBar( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
690 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoMultiPageModel" ) )
691 xVBAControl.set( new ScVbaMultiPage( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
692 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlSpinButtonModel" ) )
693 xVBAControl.set( new ScVbaSpinButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
694 else if ( xServiceInfo->supportsService( "com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ) )
695 xVBAControl.set( new VbaSystemAXControl( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
696 // #FIXME implement a page control
697 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoPageModel" ) )
698 xVBAControl.set( new ScVbaControl( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
699 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoFrameModel" ) )
700 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, xGeoHelper.release(), xDialog ) );
701 if( xVBAControl.is() )
702 return xVBAControl;
703 throw uno::RuntimeException( "Unsupported control." );
706 OUString
707 ScVbaControl::getServiceImplName()
709 return OUString("ScVbaControl");
712 uno::Sequence< OUString >
713 ScVbaControl::getServiceNames()
715 static uno::Sequence< OUString > aServiceNames;
716 if ( aServiceNames.getLength() == 0 )
718 aServiceNames.realloc( 1 );
719 aServiceNames[ 0 ] = "ooo.vba.excel.Control";
721 return aServiceNames;
724 sal_Int32 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 };
726 sal_Int32 ScVbaControl::getBackColor() throw (uno::RuntimeException)
728 sal_Int32 nBackColor = 0;
729 m_xProps->getPropertyValue( "BackgroundColor" ) >>= nBackColor;
730 return nBackColor;
733 void ScVbaControl::setBackColor( sal_Int32 nBackColor ) throw (uno::RuntimeException)
735 if ( ( (sal_uInt32)nBackColor >= (sal_uInt32)0x80000000 ) &&
736 ( (sal_uInt32)nBackColor <= (sal_uInt32)0x80000000 + SAL_N_ELEMENTS(nSysCols) ) )
738 nBackColor = nSysCols[ nBackColor & 0x0FF];
740 m_xProps->setPropertyValue( "BackgroundColor" , uno::makeAny( XLRGBToOORGB( nBackColor ) ) );
743 bool ScVbaControl::getAutoSize() throw (uno::RuntimeException)
745 bool bIsResizeEnabled = false;
746 uno::Reference< uno::XInterface > xIf( m_xControl, uno::UNO_QUERY_THROW );
747 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xIf );
748 if ( pObj )
749 bIsResizeEnabled = !pObj->IsResizeProtect();
750 return bIsResizeEnabled;
753 // currently no implementation for this
754 void ScVbaControl::setAutoSize( bool bAutoSize ) throw (uno::RuntimeException)
756 uno::Reference< uno::XInterface > xIf( m_xControl, uno::UNO_QUERY_THROW );
757 SdrObject* pObj = SdrObject::getSdrObjectFromXShape( xIf );
758 if ( pObj )
759 pObj->SetResizeProtect( !bAutoSize );
762 bool ScVbaControl::getLocked() throw (uno::RuntimeException)
764 bool bRes( false );
765 m_xProps->getPropertyValue( "ReadOnly" ) >>= bRes;
766 return bRes;
769 void ScVbaControl::setLocked( bool bLocked ) throw (uno::RuntimeException)
771 m_xProps->setPropertyValue( "ReadOnly" , uno::makeAny( bLocked ) );
774 typedef cppu::WeakImplHelper1< XControlProvider > ControlProvider_BASE;
775 class ControlProviderImpl : public ControlProvider_BASE
777 uno::Reference< uno::XComponentContext > m_xCtx;
778 public:
779 ControlProviderImpl( const uno::Reference< uno::XComponentContext >& xCtx ) : m_xCtx( xCtx ) {}
780 virtual uno::Reference< msforms::XControl > SAL_CALL createControl( const uno::Reference< drawing::XControlShape >& xControl, const uno::Reference< frame::XModel >& xDocOwner ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
783 uno::Reference< msforms::XControl > SAL_CALL
784 ControlProviderImpl::createControl( const uno::Reference< drawing::XControlShape >& xControlShape, const uno::Reference< frame::XModel >& xDocOwner ) throw (uno::RuntimeException, std::exception)
786 uno::Reference< msforms::XControl > xControlToReturn;
787 if ( xControlShape.is() )
788 xControlToReturn = ScVbaControlFactory::createShapeControl( m_xCtx, xControlShape, xDocOwner );
789 return xControlToReturn;
793 namespace controlprovider
795 namespace sdecl = comphelper::service_decl;
796 sdecl::class_<ControlProviderImpl, sdecl::with_args<false> > serviceImpl;
797 extern sdecl::ServiceDecl const serviceDecl(
798 serviceImpl,
799 "ControlProviderImpl",
800 "ooo.vba.ControlProvider" );
804 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */