Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / vbahelper / source / msforms / vbacontrol.cxx
blobf97ee77cf8241668a5db1952b6ff2f7442f8eac2
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 using namespace com::sun::star;
60 using namespace ooo::vba;
62 uno::Reference< css::awt::XWindowPeer >
63 ScVbaControl::getWindowPeer() throw (uno::RuntimeException)
65 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY );
67 uno::Reference< awt::XControlModel > xControlModel;
68 uno::Reference< css::awt::XWindowPeer > xWinPeer;
69 if ( !xControlShape.is() )
71 // would seem to be a Userform control
72 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY_THROW );
73 xWinPeer = xControl->getPeer();
74 return xWinPeer;
76 // form control
77 xControlModel.set( xControlShape->getControl(), uno::UNO_QUERY_THROW );
79 uno::Reference< view::XControlAccess > xControlAccess( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
80 try
82 uno::Reference< awt::XControl > xControl( xControlAccess->getControl( xControlModel ), uno::UNO_QUERY );
83 xWinPeer = xControl->getPeer();
85 catch(const uno::Exception&)
87 throw uno::RuntimeException( "The Control does not exsit" , uno::Reference< uno::XInterface >() );
89 return xWinPeer;
92 //ScVbaControlListener
93 class ScVbaControlListener: public cppu::WeakImplHelper1< lang::XEventListener >
95 private:
96 ScVbaControl *pControl;
97 public:
98 ScVbaControlListener( ScVbaControl *pTmpControl );
99 virtual ~ScVbaControlListener();
100 virtual void SAL_CALL disposing( const lang::EventObject& rEventObject ) throw( uno::RuntimeException );
103 ScVbaControlListener::ScVbaControlListener( ScVbaControl *pTmpControl ): pControl( pTmpControl )
107 ScVbaControlListener::~ScVbaControlListener()
111 void SAL_CALL
112 ScVbaControlListener::disposing( const lang::EventObject& ) throw( uno::RuntimeException )
114 if( pControl )
116 pControl->removeResouce();
117 pControl = NULL;
121 //ScVbaControl
123 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 )
125 //add listener
126 m_xEventListener.set( new ScVbaControlListener( this ) );
127 setGeometryHelper( pGeomHelper );
128 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
129 xComponent->addEventListener( m_xEventListener );
131 //init m_xProps
132 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
133 uno::Reference< awt::XControl> xUserFormControl( m_xControl, uno::UNO_QUERY ) ;
134 if ( xControlShape.is() ) // form control
136 m_xProps.set( xControlShape->getControl(), uno::UNO_QUERY_THROW );
137 OUString sDefaultControl;
138 m_xProps->getPropertyValue( "DefaultControl" ) >>= sDefaultControl;
139 uno::Reference< lang::XMultiComponentFactory > xMFac( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
140 m_xEmptyFormControl.set( xMFac->createInstanceWithContext( sDefaultControl, mxContext ), uno::UNO_QUERY_THROW );
142 else if ( xUserFormControl.is() ) // userform control
144 m_xProps.set( xUserFormControl->getModel(), uno::UNO_QUERY_THROW );
145 bIsDialog = true;
149 ScVbaControl::~ScVbaControl()
151 if( m_xControl.is() )
153 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
154 xComponent->removeEventListener( m_xEventListener );
158 void
159 ScVbaControl::setGeometryHelper( AbstractGeometryAttributes* pHelper )
161 mpGeometryHelper.reset( pHelper );
164 void ScVbaControl::removeResouce() throw( uno::RuntimeException )
166 uno::Reference< lang::XComponent > xComponent( m_xControl, uno::UNO_QUERY_THROW );
167 xComponent->removeEventListener( m_xEventListener );
168 m_xControl= NULL;
169 m_xProps = NULL;
172 //In design model has different behavior
173 sal_Bool SAL_CALL ScVbaControl::getEnabled() throw (uno::RuntimeException)
175 uno::Any aValue = m_xProps->getPropertyValue ( "Enabled" );
176 sal_Bool bRet = false;
177 aValue >>= bRet;
178 return bRet;
181 void SAL_CALL ScVbaControl::setEnabled( sal_Bool bVisible ) throw (uno::RuntimeException)
183 uno::Any aValue( bVisible );
184 m_xProps->setPropertyValue( "Enabled" , aValue);
188 sal_Bool SAL_CALL ScVbaControl::getVisible() throw (uno::RuntimeException)
190 sal_Bool bVisible( sal_True );
191 m_xProps->getPropertyValue( "EnableVisible" ) >>= bVisible;
192 return bVisible;
195 void SAL_CALL ScVbaControl::setVisible( sal_Bool bVisible ) throw (uno::RuntimeException)
197 uno::Any aValue( bVisible );
198 m_xProps->setPropertyValue( "EnableVisible" , aValue);
200 double SAL_CALL ScVbaControl::getHeight() throw (uno::RuntimeException)
202 return mpGeometryHelper->getHeight();
204 void SAL_CALL ScVbaControl::setHeight( double _height ) throw (uno::RuntimeException)
206 mpGeometryHelper->setHeight( _height );
209 double SAL_CALL ScVbaControl::getWidth() throw (uno::RuntimeException)
211 return mpGeometryHelper->getWidth();
213 void SAL_CALL ScVbaControl::setWidth( double _width ) throw (uno::RuntimeException)
215 mpGeometryHelper->setWidth( _width );
218 double SAL_CALL
219 ScVbaControl::getLeft() throw (uno::RuntimeException)
221 return mpGeometryHelper->getLeft();
224 void SAL_CALL
225 ScVbaControl::setLeft( double _left ) throw (uno::RuntimeException)
227 mpGeometryHelper->setLeft( _left );
230 double SAL_CALL
231 ScVbaControl::getTop() throw (uno::RuntimeException)
233 return mpGeometryHelper->getTop();
236 void SAL_CALL
237 ScVbaControl::setTop( double _top ) throw (uno::RuntimeException)
239 mpGeometryHelper->setTop( _top );
242 uno::Reference< uno::XInterface > SAL_CALL
243 ScVbaControl::getObject() throw (uno::RuntimeException)
245 uno::Reference< msforms::XControl > xRet( this );
246 return xRet;
249 void SAL_CALL ScVbaControl::SetFocus() throw (uno::RuntimeException)
251 uno::Reference< awt::XWindow > xWin( m_xControl, uno::UNO_QUERY_THROW );
252 xWin->setFocus();
255 void SAL_CALL ScVbaControl::Move( double Left, double Top, const uno::Any& Width, const uno::Any& Height )
256 throw ( uno::RuntimeException )
258 double nWidth = 0.0;
259 double nHeight = 0.0;
261 setLeft( Left );
262 setTop( Top );
264 if ( Width >>= nWidth )
265 setWidth( nWidth );
267 if ( Height >>= nHeight )
268 setHeight( nHeight );
271 OUString SAL_CALL
272 ScVbaControl::getControlSource() throw (uno::RuntimeException)
274 // #FIXME I *hate* having these upstream differences
275 // but this is necessary until I manage to upstream other
276 // dependant parts
277 OUString sControlSource;
278 uno::Reference< form::binding::XBindableValue > xBindable( m_xProps, uno::UNO_QUERY );
279 if ( xBindable.is() )
283 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
284 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellAddressConversion" ), uno::UNO_QUERY );
285 uno::Reference< beans::XPropertySet > xProps( xBindable->getValueBinding(), uno::UNO_QUERY_THROW );
286 table::CellAddress aAddress;
287 xProps->getPropertyValue( "BoundCell" ) >>= aAddress;
288 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
289 xConvertor->getPropertyValue( "XL_A1_Representation" ) >>= sControlSource;
291 catch(const uno::Exception&)
295 return sControlSource;
298 void SAL_CALL
299 ScVbaControl::setControlSource( const OUString& _controlsource ) throw (uno::RuntimeException)
301 OUString sEmpty;
302 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, _controlsource, sEmpty );
305 OUString SAL_CALL
306 ScVbaControl::getRowSource() throw (uno::RuntimeException)
308 OUString sRowSource;
309 uno::Reference< form::binding::XListEntrySink > xListSink( m_xProps, uno::UNO_QUERY );
310 if ( xListSink.is() )
314 uno::Reference< lang::XMultiServiceFactory > xFac( m_xModel, uno::UNO_QUERY_THROW );
315 uno::Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellRangeAddressConversion" ), uno::UNO_QUERY );
317 uno::Reference< beans::XPropertySet > xProps( xListSink->getListEntrySource(), uno::UNO_QUERY_THROW );
318 table::CellRangeAddress aAddress;
319 xProps->getPropertyValue( "CellRange" ) >>= aAddress;
320 xConvertor->setPropertyValue( "Address" , uno::makeAny( aAddress ) );
321 xConvertor->getPropertyValue( "XL_A1_Representation" ) >>= sRowSource;
323 catch(const uno::Exception&)
327 return sRowSource;
330 void SAL_CALL
331 ScVbaControl::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException)
333 OUString sEmpty;
334 svt::BindableControlHelper::ApplyListSourceAndBindableData( m_xModel, m_xProps, sEmpty, _rowsource );
337 OUString SAL_CALL
338 ScVbaControl::getName() throw (uno::RuntimeException)
340 OUString sName;
341 m_xProps->getPropertyValue( "Name" ) >>= sName;
342 return sName;
346 void SAL_CALL
347 ScVbaControl::setName( const OUString& _name ) throw (uno::RuntimeException)
349 m_xProps->setPropertyValue( "Name" , uno::makeAny( _name ) );
352 OUString SAL_CALL
353 ScVbaControl::getControlTipText() throw (css::uno::RuntimeException)
355 OUString sName;
356 m_xProps->getPropertyValue( "HelpText" ) >>= sName;
357 return sName;
360 void SAL_CALL
361 ScVbaControl::setControlTipText( const OUString& rsToolTip ) throw (css::uno::RuntimeException)
363 m_xProps->setPropertyValue( "HelpText" , uno::makeAny( rsToolTip ) );
366 OUString SAL_CALL ScVbaControl::getTag()
367 throw (css::uno::RuntimeException)
369 return m_aControlTag;
372 void SAL_CALL ScVbaControl::setTag( const OUString& aTag )
373 throw (css::uno::RuntimeException)
375 m_aControlTag = aTag;
378 ::sal_Int32 SAL_CALL ScVbaControl::getForeColor() throw (::com::sun::star::uno::RuntimeException)
380 sal_Int32 nForeColor = -1;
381 m_xProps->getPropertyValue( "TextColor" ) >>= nForeColor;
382 return OORGBToXLRGB( nForeColor );
385 void SAL_CALL ScVbaControl::setForeColor( ::sal_Int32 _forecolor ) throw (::com::sun::star::uno::RuntimeException)
387 m_xProps->setPropertyValue( "TextColor" , uno::makeAny( XLRGBToOORGB( _forecolor ) ) );
390 struct PointerStyles
392 long msoPointerStyle;
393 PointerStyle loPointStyle;
396 // 1 -> 1 map of styles ( some dubious choices in there though )
397 PointerStyles styles[] = {
398 /// assuming pointer default is Arrow
399 { msforms::fmMousePointer::fmMousePointerDefault, POINTER_ARROW },
400 { msforms::fmMousePointer::fmMousePointerArrow, POINTER_ARROW },
401 { msforms::fmMousePointer::fmMousePointerCross, POINTER_CROSS },
402 { msforms::fmMousePointer::fmMousePointerIBeam, POINTER_TEXT },
403 { msforms::fmMousePointer::fmMousePointerSizeNESW, POINTER_AUTOSCROLL_NSWE }, // #TODO not correct, need to check, need to find the right one
404 { msforms::fmMousePointer::fmMousePointerSizeNS, POINTER_AUTOSCROLL_NS },
405 { msforms::fmMousePointer::fmMousePointerSizeNWSE, POINTER_AUTOSCROLL_NSWE }, // #TODO not correct, need to check, need to find the right one
406 { msforms::fmMousePointer::fmMousePointerSizeWE, POINTER_AUTOSCROLL_WE },
407 { msforms::fmMousePointer::fmMousePointerUpArrow, POINTER_WINDOW_NSIZE },
408 { msforms::fmMousePointer::fmMousePointerHourGlass, POINTER_WAIT },
409 { msforms::fmMousePointer::fmMousePointerNoDrop, POINTER_NOTALLOWED },
410 { msforms::fmMousePointer::fmMousePointerAppStarting, POINTER_WAIT },
411 { msforms::fmMousePointer::fmMousePointerHelp, POINTER_HELP },
412 { msforms::fmMousePointer::fmMousePointerSizeAll, POINTER_CROSS },
413 { msforms::fmMousePointer::fmMousePointerCustom, POINTER_ARROW }, // not supported I guess
417 static long lcl_loPointerToMsoPointer( PointerStyle eType )
419 long nRet = msforms::fmMousePointer::fmMousePointerDefault;
420 for ( int i = 0, nElems = SAL_N_ELEMENTS( styles ); i < nElems; ++i )
422 if ( styles[ i ].loPointStyle == eType )
424 nRet = styles[ i ].msoPointerStyle;
425 break;
428 return nRet;
431 static Pointer lcl_msoPointerToLOPointer( long msoPointerStyle )
433 Pointer aPointer( POINTER_ARROW );
434 for ( int i = 0, nElems = SAL_N_ELEMENTS( styles ); i < nElems; ++i )
436 if ( styles[ i ].msoPointerStyle == msoPointerStyle )
438 aPointer = Pointer( styles[ i ].loPointStyle );
439 break;
442 return aPointer;
445 ::sal_Int32 SAL_CALL
446 ScVbaControl::getMousePointer() throw (::com::sun::star::uno::RuntimeException)
448 PointerStyle eType = POINTER_ARROW; // default ?
449 Window* pWindow = VCLUnoHelper::GetWindow( getWindowPeer() );
450 if ( pWindow )
452 eType = pWindow->GetPointer().GetStyle();
454 return lcl_loPointerToMsoPointer( eType );
457 void SAL_CALL
458 ScVbaControl::setMousePointer( ::sal_Int32 _mousepointer ) throw (::com::sun::star::uno::RuntimeException)
460 Window* pWindow = VCLUnoHelper::GetWindow( getWindowPeer() );
461 if ( pWindow )
463 Pointer aPointer( POINTER_ARROW );
464 aPointer = lcl_msoPointerToLOPointer( _mousepointer );
465 pWindow->SetPointer( aPointer );
469 void ScVbaControl::fireEvent( script::ScriptEvent& evt )
471 uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
472 uno::Reference< script::XScriptListener > xScriptListener( xServiceManager->createInstanceWithContext( "ooo.vba.EventListener" , mxContext ), uno::UNO_QUERY_THROW );
474 uno::Reference< beans::XPropertySet > xProps( xScriptListener, uno::UNO_QUERY_THROW );
475 xProps->setPropertyValue( "Model" , uno::makeAny( m_xModel ) );
477 // handling for sheet control
478 uno::Reference< msforms::XControl > xThisControl( this );
481 evt.Arguments.realloc( 1 );
482 lang::EventObject aEvt;
484 uno::Reference< drawing::XControlShape > xControlShape( m_xControl, uno::UNO_QUERY ) ;
485 uno::Reference< awt::XControl > xControl( m_xControl, uno::UNO_QUERY ) ;
487 if ( xControlShape.is() )
489 evt.Source = xControlShape;
490 aEvt.Source = m_xEmptyFormControl;
491 // Set up proper scriptcode
492 uno::Reference< lang::XMultiServiceFactory > xDocFac( m_xModel, uno::UNO_QUERY_THROW );
493 uno::Reference< document::XCodeNameQuery > xNameQuery( xDocFac->createInstance( "ooo.vba.VBACodeNameProvider" ), uno::UNO_QUERY_THROW );
494 uno::Reference< uno::XInterface > xIf( xControlShape->getControl(), uno::UNO_QUERY_THROW );
495 evt.ScriptCode = xNameQuery->getCodeNameForObject( xIf );
496 evt.Arguments[ 0 ] = uno::makeAny( aEvt );
497 xScriptListener->firing( evt );
499 else
501 if ( xControl.is() ) // normal control ( from dialog/userform )
503 // #FIXME We should probably store a reference to the
504 // parent dialog/userform here ( other wise the name of
505 // dialog could be changed and we won't be aware of it.
506 // ( OTOH this is probably an unlikely scenario )
507 evt.Source = xThisControl;
508 aEvt.Source = xControl;
509 evt.ScriptCode = m_sLibraryAndCodeName;
510 evt.Arguments[ 0 ] = uno::makeAny( aEvt );
511 xScriptListener->firing( evt );
515 catch(const uno::Exception&)
519 void ScVbaControl::fireChangeEvent()
521 script::ScriptEvent evt;
522 evt.ScriptType = "VBAInterop";
523 evt.ListenerType = form::XChangeListener::static_type(0);
524 evt.MethodName = "changed";
525 fireEvent( evt );
528 void ScVbaControl::fireClickEvent()
530 script::ScriptEvent evt;
531 evt.ScriptType = "VBAInterop";
532 evt.ListenerType = awt::XActionListener::static_type(0);
533 evt.MethodName = "actionPerformed";
534 fireEvent( evt );
537 sal_Int32 SAL_CALL ScVbaControl::getTabIndex() throw (uno::RuntimeException)
539 return 1;
542 void SAL_CALL ScVbaControl::setTabIndex( sal_Int32 /*nTabIndex*/ ) throw (uno::RuntimeException)
546 //ScVbaControlFactory
548 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createShapeControl(
549 const uno::Reference< uno::XComponentContext >& xContext,
550 const uno::Reference< drawing::XControlShape >& xControlShape,
551 const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException)
553 uno::Reference< beans::XPropertySet > xProps( xControlShape->getControl(), uno::UNO_QUERY_THROW );
554 sal_Int32 nClassId = -1;
555 const static OUString sClassId( "ClassId" );
556 xProps->getPropertyValue( sClassId ) >>= nClassId;
557 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
558 uno::Reference< drawing::XShape > xShape( xControlShape, uno::UNO_QUERY_THROW );
559 ::std::auto_ptr< ConcreteXShapeGeometryAttributes > xGeoHelper( new ConcreteXShapeGeometryAttributes( xContext, xShape ) );
560 switch( nClassId )
562 case form::FormComponentType::COMBOBOX:
563 return new ScVbaComboBox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
564 case form::FormComponentType::COMMANDBUTTON:
565 return new ScVbaButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
566 case form::FormComponentType::FIXEDTEXT:
567 return new ScVbaLabel( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
568 case form::FormComponentType::TEXTFIELD:
569 return new ScVbaTextBox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
570 case form::FormComponentType::CHECKBOX:
571 return new ScVbaCheckbox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
572 case form::FormComponentType::RADIOBUTTON:
573 return new ScVbaRadioButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
574 case form::FormComponentType::LISTBOX:
575 return new ScVbaListBox( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
576 case form::FormComponentType::SPINBUTTON:
577 return new ScVbaSpinButton( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
578 case form::FormComponentType::IMAGECONTROL:
579 return new ScVbaImage( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
580 case form::FormComponentType::SCROLLBAR:
581 return new ScVbaScrollBar( xVbaParent, xContext, xControlShape, xModel, xGeoHelper.release() );
583 throw uno::RuntimeException( "Unsupported control." , uno::Reference< uno::XInterface >() );
586 /*static*/ uno::Reference< msforms::XControl > ScVbaControlFactory::createUserformControl(
587 const uno::Reference< uno::XComponentContext >& xContext,
588 const uno::Reference< awt::XControl >& xControl,
589 const uno::Reference< awt::XControl >& xDialog,
590 const uno::Reference< frame::XModel >& xModel,
591 double fOffsetX, double fOffsetY ) throw (uno::RuntimeException)
593 uno::Reference< beans::XPropertySet > xProps( xControl->getModel(), uno::UNO_QUERY_THROW );
594 uno::Reference< lang::XServiceInfo > xServiceInfo( xProps, uno::UNO_QUERY_THROW );
595 uno::Reference< msforms::XControl > xVBAControl;
596 uno::Reference< XHelperInterface > xVbaParent; // #FIXME - should be worksheet I guess
597 ::std::auto_ptr< UserFormGeometryHelper > xGeoHelper( new UserFormGeometryHelper( xContext, xControl, fOffsetX, fOffsetY ) );
599 if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlCheckBoxModel" ) )
600 xVBAControl.set( new ScVbaCheckbox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
601 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlRadioButtonModel" ) )
602 xVBAControl.set( new ScVbaRadioButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
603 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlEditModel" ) )
604 xVBAControl.set( new ScVbaTextBox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release(), true ) );
605 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlButtonModel" ) )
607 sal_Bool bToggle = sal_False;
608 xProps->getPropertyValue( "Toggle" ) >>= bToggle;
609 if ( bToggle )
610 xVBAControl.set( new ScVbaToggleButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
611 else
612 xVBAControl.set( new ScVbaButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
614 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlComboBoxModel" ) )
615 xVBAControl.set( new ScVbaComboBox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
616 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlListBoxModel" ) )
617 xVBAControl.set( new ScVbaListBox( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
618 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlFixedTextModel" ) )
619 xVBAControl.set( new ScVbaLabel( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
620 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlImageControlModel" ) )
621 xVBAControl.set( new ScVbaImage( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
622 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlProgressBarModel" ) )
623 xVBAControl.set( new ScVbaProgressBar( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
624 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlGroupBoxModel" ) )
625 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, xGeoHelper.release(), xDialog ) );
626 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlScrollBarModel" ) )
627 xVBAControl.set( new ScVbaScrollBar( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
628 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoMultiPageModel" ) )
629 xVBAControl.set( new ScVbaMultiPage( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
630 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlSpinButtonModel" ) )
631 xVBAControl.set( new ScVbaSpinButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
632 else if ( xServiceInfo->supportsService( "com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ) )
633 xVBAControl.set( new VbaSystemAXControl( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
634 // #FIXME implement a page control
635 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoPageModel" ) )
636 xVBAControl.set( new ScVbaControl( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
637 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoFrameModel" ) )
638 xVBAControl.set( new ScVbaFrame( xVbaParent, xContext, xControl, xModel, xGeoHelper.release(), xDialog ) );
639 else if ( xServiceInfo->supportsService( "com.sun.star.awt.UnoControlSpinButtonModel" ) )
640 xVBAControl.set( new ScVbaSpinButton( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
641 else if ( xServiceInfo->supportsService( "com.sun.star.custom.awt.UnoControlSystemAXContainerModel" ) )
642 xVBAControl.set( new VbaSystemAXControl( xVbaParent, xContext, xControl, xModel, xGeoHelper.release() ) );
643 if( xVBAControl.is() )
644 return xVBAControl;
645 throw uno::RuntimeException( "Unsupported control." , uno::Reference< uno::XInterface >() );
648 OUString
649 ScVbaControl::getServiceImplName()
651 return OUString("ScVbaControl");
654 uno::Sequence< OUString >
655 ScVbaControl::getServiceNames()
657 static uno::Sequence< OUString > aServiceNames;
658 if ( aServiceNames.getLength() == 0 )
660 aServiceNames.realloc( 1 );
661 aServiceNames[ 0 ] = "ooo.vba.excel.Control";
663 return aServiceNames;
666 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 };
668 sal_Int32 ScVbaControl::getBackColor() throw (uno::RuntimeException)
670 sal_Int32 nBackColor = 0;
671 m_xProps->getPropertyValue( "BackgroundColor" ) >>= nBackColor;
672 return nBackColor;
675 void ScVbaControl::setBackColor( sal_Int32 nBackColor ) throw (uno::RuntimeException)
677 if ( ( nBackColor >= (sal_Int32)0x80000000 ) && ( nBackColor <= (sal_Int32)0x80000018 ) )
679 nBackColor = nSysCols[ nBackColor - 0x80000000 ];
681 m_xProps->setPropertyValue( "BackgroundColor" , uno::makeAny( XLRGBToOORGB( nBackColor ) ) );
684 sal_Bool ScVbaControl::getAutoSize() throw (uno::RuntimeException)
686 return sal_False;
689 // currently no implementation for this
690 void ScVbaControl::setAutoSize( sal_Bool /*bAutoSize*/ ) throw (uno::RuntimeException)
694 sal_Bool ScVbaControl::getLocked() throw (uno::RuntimeException)
696 sal_Bool bRes( sal_False );
697 m_xProps->getPropertyValue( "ReadOnly" ) >>= bRes;
698 return bRes;
701 void ScVbaControl::setLocked( sal_Bool bLocked ) throw (uno::RuntimeException)
703 m_xProps->setPropertyValue( "ReadOnly" , uno::makeAny( bLocked ) );
706 typedef cppu::WeakImplHelper1< XControlProvider > ControlProvider_BASE;
707 class ControlProviderImpl : public ControlProvider_BASE
709 uno::Reference< uno::XComponentContext > m_xCtx;
710 public:
711 ControlProviderImpl( const uno::Reference< uno::XComponentContext >& xCtx ) : m_xCtx( xCtx ) {}
712 virtual uno::Reference< msforms::XControl > SAL_CALL createControl( const uno::Reference< drawing::XControlShape >& xControl, const uno::Reference< frame::XModel >& xDocOwner ) throw (uno::RuntimeException);
715 uno::Reference< msforms::XControl > SAL_CALL
716 ControlProviderImpl::createControl( const uno::Reference< drawing::XControlShape >& xControlShape, const uno::Reference< frame::XModel >& xDocOwner ) throw (uno::RuntimeException)
718 uno::Reference< msforms::XControl > xControlToReturn;
719 if ( xControlShape.is() )
720 xControlToReturn = ScVbaControlFactory::createShapeControl( m_xCtx, xControlShape, xDocOwner );
721 return xControlToReturn;
725 namespace controlprovider
727 namespace sdecl = comphelper::service_decl;
728 sdecl::class_<ControlProviderImpl, sdecl::with_args<false> > serviceImpl;
729 extern sdecl::ServiceDecl const serviceDecl(
730 serviceImpl,
731 "ControlProviderImpl",
732 "ooo.vba.ControlProvider" );
736 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */