sync master with lastest vba changes
[ooovba.git] / toolkit / source / controls / roadmapcontrol.cxx
blobc24abdb0aeddf2fe3398ddfe0743265d0f38315f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: roadmapcontrol.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
34 #ifndef _TOOLKIT_ROADMAP_CONTROL_HXX
35 #include <toolkit/controls/roadmapcontrol.hxx>
36 #endif
37 #include <toolkit/helper/unopropertyarrayhelper.hxx>
38 #include <toolkit/helper/property.hxx>
39 #include <com/sun/star/awt/XVclWindowPeer.hpp>
40 #include <comphelper/processfactory.hxx>
41 #include <osl/diagnose.h>
43 //........................................................................
44 namespace toolkit
46 //........................................................................
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::awt;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::container;
54 // ----------------------------------------------------
55 // helper
56 // ----------------------------------------------------
58 static void lcl_knitImageComponents( const Reference< XControlModel >& _rxModel,
59 const Reference< XWindowPeer >& _rxPeer,
60 bool _bAdd )
62 Reference< XImageProducer > xProducer( _rxModel, UNO_QUERY );
63 if ( xProducer.is() )
65 Reference< XImageConsumer > xConsumer( _rxPeer, UNO_QUERY );
66 if ( xConsumer.is() )
68 if ( _bAdd )
70 xProducer->addConsumer( xConsumer );
71 xProducer->startProduction();
73 else
74 xProducer->removeConsumer( xConsumer );
79 static void lcl_throwIllegalArgumentException( )
80 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this ....
81 throw IllegalArgumentException();
84 static void lcl_throwIndexOutOfBoundsException( )
85 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this ....
86 throw IndexOutOfBoundsException();
89 // ===================================================================
90 // = UnoControlRoadmapModel
91 // ===================================================================
92 // -------------------------------------------------------------------
93 UnoControlRoadmapModel::UnoControlRoadmapModel() : maContainerListeners( *this )
95 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
96 ImplRegisterProperty( BASEPROPERTY_BORDER );
97 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
98 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
99 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
100 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
101 ImplRegisterProperty( BASEPROPERTY_HELPURL );
102 ImplRegisterProperty( BASEPROPERTY_IMAGEURL );
103 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
104 ImplRegisterProperty( BASEPROPERTY_COMPLETE );
105 ImplRegisterProperty( BASEPROPERTY_ACTIVATED );
106 ImplRegisterProperty( BASEPROPERTY_CURRENTITEMID );
107 ImplRegisterProperty( BASEPROPERTY_TEXT );
110 // -------------------------------------------------------------------
111 ::rtl::OUString UnoControlRoadmapModel::getServiceName() throw(RuntimeException)
113 return ::rtl::OUString::createFromAscii( szServiceName_UnoControlRoadmapModel );
117 // -------------------------------------------------------------------
118 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
120 Any aReturn;
121 switch (nPropId)
123 case BASEPROPERTY_COMPLETE:
124 aReturn <<= (sal_Bool) sal_True;
125 break;
126 case BASEPROPERTY_ACTIVATED:
127 aReturn <<= (sal_Bool) sal_True;
128 break;
129 case BASEPROPERTY_CURRENTITEMID:
130 aReturn <<= (sal_Int16) -1;
131 break;
132 case BASEPROPERTY_TEXT:
133 break;
134 case BASEPROPERTY_BORDER:
135 aReturn <<= (sal_Int16) 2; // No Border
136 break;
137 case BASEPROPERTY_DEFAULTCONTROL:
138 aReturn <<= ::rtl::OUString( ::rtl::OUString::createFromAscii( szServiceName_UnoControlRoadmap ) );
139 break;
140 default : aReturn = UnoControlModel::ImplGetDefaultValue( nPropId ); break;
143 return aReturn;
147 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( ) throw (Exception, ::com::sun::star::uno::RuntimeException)
149 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
150 Reference< XInterface > xNewRoadmapItem = (::cppu::OWeakObject*)pRoadmapItem;
151 return xNewRoadmapItem;
155 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ ) throw (Exception, RuntimeException)
157 // Todo: implementation of the arguments handling
158 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
159 Reference< XInterface > xNewRoadmapItem = (::cppu::OWeakObject*)pRoadmapItem;
160 return xNewRoadmapItem;
164 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
167 // -------------------------------------------------------------------
168 ::com::sun::star::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException)
170 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
171 if ( !aRet.hasValue() )
172 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
173 return aRet;
177 // -------------------------------------------------------------------
178 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
180 static UnoPropertyArrayHelper* pHelper = NULL;
181 if ( !pHelper )
183 Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
184 pHelper = new UnoPropertyArrayHelper( aIDs );
186 return *pHelper;
190 // beans::XMultiPropertySet
191 // -------------------------------------------------------------------
192 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( ) throw(RuntimeException)
194 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
195 return xInfo;
199 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount() throw(RuntimeException)
201 return maRoadmapItems.size();
204 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index ) throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
206 if (( Index >= (sal_Int32)maRoadmapItems.size()) || (Index < 0))
207 lcl_throwIndexOutOfBoundsException( );
208 Any aAny;
209 aAny = makeAny( maRoadmapItems.at( Index ));
210 return aAny;
215 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, Reference< XInterface > xRoadmapItem )
217 if ((Index > (sal_Int32)maRoadmapItems.size()) || ( Index < 0 ) )
218 lcl_throwIndexOutOfBoundsException( );
219 if ( !xRoadmapItem.is() )
220 lcl_throwIllegalArgumentException();
221 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
222 sal_Bool bIsRoadmapItem = xServiceInfo->supportsService( ::rtl::OUString::createFromAscii( "com.sun.star.awt.RoadmapItem" ) );
223 if ( !bIsRoadmapItem )
224 lcl_throwIllegalArgumentException();
228 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const sal_Int32 , Reference< XInterface > xRoadmapItem)
230 Any aAny;
231 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
232 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
233 if ( xProps.is() )
235 sal_Int32 LocID = 0;
236 Any aValue = xPropertySet->getPropertyValue( ::rtl::OUString::createFromAscii( "ID" ) );
237 aValue >>= LocID;
238 if (LocID < 0) // index may not be smaller than zero
240 aAny <<= GetUniqueID();
241 xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( "ID" ), aAny );
247 // The performance of this method could certainly be improved.
248 // As long as only vectors with up to 10 elements are
249 // involved it should be sufficient
250 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
252 Any aAny;
253 sal_Bool bIncrement = sal_True;
254 sal_Int32 CurID = 0;
255 sal_Int32 n_CurItemID = 0;
256 Reference< XInterface > CurRoadmapItem;
257 while ( bIncrement )
259 bIncrement = sal_False;
260 for ( RoadmapItemHolderList::iterator i = maRoadmapItems.begin(); i < maRoadmapItems.end(); i++ )
262 CurRoadmapItem = *i;
263 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
264 aAny = xPropertySet->getPropertyValue( ::rtl::OUString::createFromAscii( "ID" ) );
265 aAny >>= n_CurItemID;
266 if (n_CurItemID == CurID)
268 bIncrement = sal_True;
269 CurID++;
270 break;
274 return CurID;
278 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, Reference< XInterface > xRoadmapItem)
280 ContainerEvent aEvent;
281 aEvent.Source = *this;
282 aEvent.Element <<= xRoadmapItem;
283 aEvent.Accessor = makeAny(Index);
284 return aEvent;
288 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( Reference< XPropertySet > xPropertySet )
290 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
291 sal_Int16 n_CurrentItemID = 0;
292 aAny >>= n_CurrentItemID;
293 return n_CurrentItemID;
297 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& _Element)
298 throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
300 if ( ( Index >= ( (sal_Int32)maRoadmapItems.size() + 1 ) ) || (Index < 0))
301 lcl_throwIndexOutOfBoundsException( );
302 Reference< XInterface > xRoadmapItem;
303 _Element >>= xRoadmapItem;
304 MakeRMItemValidation( Index, xRoadmapItem);
305 SetRMItemDefaultProperties( Index, xRoadmapItem );
306 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
307 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
308 maContainerListeners.elementInserted( aEvent );
309 Reference< XPropertySet > xPropertySet( (XAggregation*) (::cppu::OWeakAggObject*)this, UNO_QUERY );
310 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
311 if ( Index <= n_CurrentItemID )
313 Any aAny;
314 aAny <<= ( sal_Int16 ) ( n_CurrentItemID + 1 );
315 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
321 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
322 throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
324 if (( Index > (sal_Int32)maRoadmapItems.size()) || (Index < 0))
325 lcl_throwIndexOutOfBoundsException( );
326 Reference< XInterface > xRoadmapItem;
327 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
328 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
329 maContainerListeners.elementRemoved( aEvent );
330 Reference< XPropertySet > xPropertySet( (XAggregation*) (::cppu::OWeakAggObject*)this, UNO_QUERY );
331 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
332 Any aAny;
333 if ( Index <= n_CurrentItemID )
335 if ( n_CurrentItemID >= (sal_Int32)maRoadmapItems.size() )
337 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
338 maRoadmapItems.size()-1);
339 if ( n_CurrentItemID < 0 )
340 return;
341 aAny <<= n_CurrentItemID;
343 else if (Index == n_CurrentItemID)
344 aAny <<= ( sal_Int16 ) -1;
345 else if( Index < n_CurrentItemID)
346 aAny <<= ( sal_Int16 ) ( n_CurrentItemID - 1 );
347 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
352 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& _Element)
353 throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
355 Reference< XInterface > xRoadmapItem;
356 _Element >>= xRoadmapItem;
357 MakeRMItemValidation( Index, xRoadmapItem);
358 SetRMItemDefaultProperties( Index, xRoadmapItem );
359 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
360 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
361 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
362 maContainerListeners.elementReplaced( aEvent );
366 Type SAL_CALL UnoControlRoadmapModel::getElementType() throw(RuntimeException)
368 Type aType = getCppuType( ( Reference< XPropertySet>* ) NULL );
369 return aType;
373 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements() throw(RuntimeException)
375 return !maRoadmapItems.empty();
379 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw (::com::sun::star::uno::RuntimeException)
381 maContainerListeners.addInterface( xListener );
384 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw (::com::sun::star::uno::RuntimeException)
386 maContainerListeners.removeInterface( xListener );
390 void UnoControlRoadmapModel::addConsumer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer >& xConsumer ) throw (::com::sun::star::uno::RuntimeException)
392 maImageListeners.push_back( xConsumer );
396 void UnoControlRoadmapModel::removeConsumer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer >& xConsumer ) throw (::com::sun::star::uno::RuntimeException)
398 maImageListeners.remove( xConsumer );
402 void UnoControlRoadmapModel::startProduction( ) throw (::com::sun::star::uno::RuntimeException)
404 Sequence<Any> aArgs(1);
405 aArgs.getArray()[0] = getPropertyValue( GetPropertyName( BASEPROPERTY_IMAGEURL ) );
406 Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
407 Reference< XImageProducer > xImageProducer( xMSF->createInstanceWithArguments( ::rtl::OUString::createFromAscii( "com.sun.star.awt.ImageProducer" ), aArgs ), UNO_QUERY );
408 if ( xImageProducer.is() )
410 std::list< Reference< XImageConsumer > >::iterator aIter( maImageListeners.begin() );
411 while ( aIter != maImageListeners.end() )
413 xImageProducer->addConsumer( *aIter );
414 aIter++;
416 xImageProducer->startProduction();
423 // ===================================================================
424 // = UnoRoadmapControl
425 // ===================================================================
426 // -------------------------------------------------------------------
427 UnoRoadmapControl::UnoRoadmapControl(): maItemListeners( *this )
431 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
432 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
435 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel) throw ( RuntimeException )
439 // remove the peer as image consumer from the model
440 lcl_knitImageComponents( getModel(), getPeer(), false );
442 Reference< XContainer > xC( getModel(), UNO_QUERY );
443 if ( xC.is() )
444 xC->removeContainerListener( this );
446 sal_Bool bReturn = UnoControlBase::setModel( _rModel );
448 xC = xC.query( getModel());
449 if ( xC.is() )
450 xC->addContainerListener( this );
452 // add the peer as image consumer to the model
453 lcl_knitImageComponents( getModel(), getPeer(), true );
455 return bReturn;
459 // -------------------------------------------------------------------
460 ::rtl::OUString UnoRoadmapControl::GetComponentServiceName()
462 return ::rtl::OUString::createFromAscii( "Roadmap" );
467 void SAL_CALL UnoRoadmapControl::createPeer( const Reference<XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer ) throw(RuntimeException)
469 // remove the peer as image consumer from the model
470 lcl_knitImageComponents( getModel(), getPeer(), false );
472 UnoControl::createPeer( rxToolkit, rParentPeer );
474 lcl_knitImageComponents( getModel(), getPeer(), true );
479 void UnoRoadmapControl::dispose() throw(RuntimeException)
481 EventObject aEvt;
482 aEvt.Source = (::cppu::OWeakObject*)this;
483 maItemListeners.disposeAndClear( aEvt );
484 UnoControl::dispose();
489 void UnoRoadmapControl::ImplSetPeerProperty( const ::rtl::OUString& rPropName, const Any& rVal )
491 sal_uInt16 nType = GetPropertyId( rPropName );
492 if ( getPeer().is() && ( nType == BASEPROPERTY_IMAGEURL ) )
494 Reference < XImageProducer > xImgProd( getModel(), UNO_QUERY );
495 Reference < XImageConsumer > xImgCons( getPeer(), UNO_QUERY );
497 if ( xImgProd.is() && xImgCons.is() )
499 xImgProd->startProduction();
502 else
503 UnoControlBase::ImplSetPeerProperty( rPropName, rVal );
507 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )throw(RuntimeException)
509 Reference< XInterface > xRoadmapItem;
510 rEvent.Element >>= xRoadmapItem;
511 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
512 if ( xRoadmapPropertySet.is() )
513 xRoadmapPropertySet->addPropertyChangeListener( rtl::OUString(), this );
515 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
516 if ( xPeer.is() )
518 xPeer->elementInserted( rEvent );
519 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
520 if ( xPropertySet.is() )
521 xPropertySet->addPropertyChangeListener( rtl::OUString(), this );
526 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )throw(RuntimeException)
528 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
529 if ( xPeer.is() )
530 xPeer->elementRemoved( rEvent );
531 Reference< XInterface > xRoadmapItem;
532 rEvent.Element >>= xRoadmapItem;
533 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
534 if ( xPropertySet.is() )
535 xPropertySet->removePropertyChangeListener( rtl::OUString(), this );
539 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )throw(RuntimeException)
541 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
542 if ( xPeer.is() )
543 xPeer->elementReplaced( rEvent );
547 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent ) throw (RuntimeException)
549 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
550 Any aAny;
551 aAny <<= CurItemIndex;
552 Reference< XControlModel > xModel( getModel( ), UNO_QUERY );
553 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
554 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
555 if ( maItemListeners.getLength() )
556 maItemListeners.itemStateChanged( rEvent );
560 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l ) throw (RuntimeException)
562 maItemListeners.addInterface( l );
563 if( getPeer().is() && maItemListeners.getLength() == 1 )
565 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
566 xRoadmap->addItemListener( this );
571 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l ) throw (RuntimeException)
573 if( getPeer().is() && maItemListeners.getLength() == 1 )
575 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
576 xRoadmap->removeItemListener( this );
579 maItemListeners.removeInterface( l );
583 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException)
585 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
586 if ( xPeer.is() )
587 xPeer->propertyChange( evt );