1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <toolkit/controls/roadmapcontrol.hxx>
22 #include <toolkit/helper/unopropertyarrayhelper.hxx>
23 #include <toolkit/helper/property.hxx>
24 #include <com/sun/star/awt/XVclWindowPeer.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <osl/diagnose.h>
28 //........................................................................
31 //........................................................................
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::awt
;
35 using namespace ::com::sun::star::lang
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::container
;
39 // ----------------------------------------------------
41 // ----------------------------------------------------
43 static void lcl_throwIllegalArgumentException( )
44 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this ....
45 throw IllegalArgumentException();
48 static void lcl_throwIndexOutOfBoundsException( )
49 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this ....
50 throw IndexOutOfBoundsException();
53 // ===================================================================
54 // = UnoControlRoadmapModel
55 // ===================================================================
56 // -------------------------------------------------------------------
57 UnoControlRoadmapModel::UnoControlRoadmapModel( const Reference
< XMultiServiceFactory
>& i_factory
)
58 :UnoControlRoadmapModel_Base( i_factory
)
59 ,maContainerListeners( *this )
61 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR
);
62 ImplRegisterProperty( BASEPROPERTY_BORDER
);
63 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR
);
64 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL
);
65 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR
);
66 ImplRegisterProperty( BASEPROPERTY_HELPTEXT
);
67 ImplRegisterProperty( BASEPROPERTY_HELPURL
);
68 ImplRegisterProperty( BASEPROPERTY_IMAGEURL
);
69 ImplRegisterProperty( BASEPROPERTY_GRAPHIC
);
70 ImplRegisterProperty( BASEPROPERTY_PRINTABLE
);
71 ImplRegisterProperty( BASEPROPERTY_COMPLETE
);
72 ImplRegisterProperty( BASEPROPERTY_ACTIVATED
);
73 ImplRegisterProperty( BASEPROPERTY_CURRENTITEMID
);
74 ImplRegisterProperty( BASEPROPERTY_TABSTOP
);
75 ImplRegisterProperty( BASEPROPERTY_TEXT
);
78 // -------------------------------------------------------------------
79 ::rtl::OUString
UnoControlRoadmapModel::getServiceName() throw(RuntimeException
)
81 return ::rtl::OUString::createFromAscii( szServiceName_UnoControlRoadmapModel
);
85 // -------------------------------------------------------------------
86 Any
UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId
) const
91 case BASEPROPERTY_COMPLETE
:
92 aReturn
<<= (sal_Bool
) sal_True
;
94 case BASEPROPERTY_ACTIVATED
:
95 aReturn
<<= (sal_Bool
) sal_True
;
97 case BASEPROPERTY_CURRENTITEMID
:
98 aReturn
<<= (sal_Int16
) -1;
100 case BASEPROPERTY_TEXT
:
102 case BASEPROPERTY_BORDER
:
103 aReturn
<<= (sal_Int16
) 2; // No Border
105 case BASEPROPERTY_DEFAULTCONTROL
:
106 aReturn
<<= ::rtl::OUString( ::rtl::OUString::createFromAscii( szServiceName_UnoControlRoadmap
) );
108 default : aReturn
= UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId
); break;
115 Reference
< XInterface
> SAL_CALL
UnoControlRoadmapModel::createInstance( ) throw (Exception
, ::com::sun::star::uno::RuntimeException
)
117 ORoadmapEntry
* pRoadmapItem
= new ORoadmapEntry();
118 Reference
< XInterface
> xNewRoadmapItem
= (::cppu::OWeakObject
*)pRoadmapItem
;
119 return xNewRoadmapItem
;
123 Reference
< XInterface
> SAL_CALL
UnoControlRoadmapModel::createInstanceWithArguments( const Sequence
< Any
>& /*aArguments*/ ) throw (Exception
, RuntimeException
)
125 // Todo: implementation of the arguments handling
126 ORoadmapEntry
* pRoadmapItem
= new ORoadmapEntry();
127 Reference
< XInterface
> xNewRoadmapItem
= (::cppu::OWeakObject
*)pRoadmapItem
;
128 return xNewRoadmapItem
;
132 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel
, UnoControlRoadmapModel_Base
, UnoControlRoadmapModel_IBase
)
135 // -------------------------------------------------------------------
136 ::com::sun::star::uno::Any SAL_CALL
UnoControlRoadmapModel::queryAggregation( const ::com::sun::star::uno::Type
& rType
) throw(::com::sun::star::uno::RuntimeException
)
138 Any aRet
= UnoControlRoadmapModel_Base::queryAggregation( rType
);
139 if ( !aRet
.hasValue() )
140 aRet
= UnoControlRoadmapModel_IBase::queryInterface( rType
);
145 // -------------------------------------------------------------------
146 ::cppu::IPropertyArrayHelper
& UnoControlRoadmapModel::getInfoHelper()
148 static UnoPropertyArrayHelper
* pHelper
= NULL
;
151 Sequence
<sal_Int32
> aIDs
= ImplGetPropertyIds();
152 pHelper
= new UnoPropertyArrayHelper( aIDs
);
158 // beans::XMultiPropertySet
159 // -------------------------------------------------------------------
160 Reference
< XPropertySetInfo
> UnoControlRoadmapModel::getPropertySetInfo( ) throw(RuntimeException
)
162 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
167 sal_Int32 SAL_CALL
UnoControlRoadmapModel::getCount() throw(RuntimeException
)
169 return maRoadmapItems
.size();
172 Any SAL_CALL
UnoControlRoadmapModel::getByIndex( sal_Int32 Index
) throw (IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
174 if (( Index
>= (sal_Int32
)maRoadmapItems
.size()) || (Index
< 0))
175 lcl_throwIndexOutOfBoundsException( );
177 aAny
= makeAny( maRoadmapItems
.at( Index
));
183 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index
, Reference
< XInterface
> xRoadmapItem
)
185 if ((Index
> (sal_Int32
)maRoadmapItems
.size()) || ( Index
< 0 ) )
186 lcl_throwIndexOutOfBoundsException( );
187 if ( !xRoadmapItem
.is() )
188 lcl_throwIllegalArgumentException();
189 Reference
< XServiceInfo
> xServiceInfo( xRoadmapItem
, UNO_QUERY
);
190 sal_Bool bIsRoadmapItem
= xServiceInfo
->supportsService( ::rtl::OUString("com.sun.star.awt.RoadmapItem") );
191 if ( !bIsRoadmapItem
)
192 lcl_throwIllegalArgumentException();
196 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const sal_Int32
, Reference
< XInterface
> xRoadmapItem
)
199 Reference
< XPropertySet
> xPropertySet( xRoadmapItem
, UNO_QUERY
);
200 Reference
< XPropertySet
> xProps( xRoadmapItem
, UNO_QUERY
);
204 Any aValue
= xPropertySet
->getPropertyValue( ::rtl::OUString("ID") );
206 if (LocID
< 0) // index may not be smaller than zero
208 aAny
<<= GetUniqueID();
209 xPropertySet
->setPropertyValue( ::rtl::OUString("ID"), aAny
);
215 // The performance of this method could certainly be improved.
216 // As long as only vectors with up to 10 elements are
217 // involved it should be sufficient
218 sal_Int32
UnoControlRoadmapModel::GetUniqueID()
221 sal_Bool bIncrement
= sal_True
;
223 sal_Int32 n_CurItemID
= 0;
224 Reference
< XInterface
> CurRoadmapItem
;
227 bIncrement
= sal_False
;
228 for ( RoadmapItemHolderList::iterator i
= maRoadmapItems
.begin(); i
< maRoadmapItems
.end(); ++i
)
231 Reference
< XPropertySet
> xPropertySet( CurRoadmapItem
, UNO_QUERY
);
232 aAny
= xPropertySet
->getPropertyValue( ::rtl::OUString("ID") );
233 aAny
>>= n_CurItemID
;
234 if (n_CurItemID
== CurID
)
236 bIncrement
= sal_True
;
246 ContainerEvent
UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index
, Reference
< XInterface
> xRoadmapItem
)
248 ContainerEvent aEvent
;
249 aEvent
.Source
= *this;
250 aEvent
.Element
<<= xRoadmapItem
;
251 aEvent
.Accessor
= makeAny(Index
);
256 sal_Int16
UnoControlRoadmapModel::GetCurrentItemID( Reference
< XPropertySet
> xPropertySet
)
258 Any aAny
= xPropertySet
->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
) );
259 sal_Int16 n_CurrentItemID
= 0;
260 aAny
>>= n_CurrentItemID
;
261 return n_CurrentItemID
;
265 void SAL_CALL
UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index
, const Any
& _Element
)
266 throw (IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
268 if ( ( Index
>= ( (sal_Int32
)maRoadmapItems
.size() + 1 ) ) || (Index
< 0))
269 lcl_throwIndexOutOfBoundsException( );
270 Reference
< XInterface
> xRoadmapItem
;
271 _Element
>>= xRoadmapItem
;
272 MakeRMItemValidation( Index
, xRoadmapItem
);
273 SetRMItemDefaultProperties( Index
, xRoadmapItem
);
274 maRoadmapItems
.insert( maRoadmapItems
.begin() + Index
, xRoadmapItem
);
275 ContainerEvent aEvent
= GetContainerEvent(Index
, xRoadmapItem
);
276 maContainerListeners
.elementInserted( aEvent
);
277 Reference
< XPropertySet
> xPropertySet( (XAggregation
*) (::cppu::OWeakAggObject
*)this, UNO_QUERY
);
278 sal_Int16 n_CurrentItemID
= GetCurrentItemID( xPropertySet
);
279 if ( Index
<= n_CurrentItemID
)
282 aAny
<<= ( sal_Int16
) ( n_CurrentItemID
+ 1 );
283 xPropertySet
->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
), aAny
);
289 void SAL_CALL
UnoControlRoadmapModel::removeByIndex( sal_Int32 Index
)
290 throw (IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
292 if (( Index
> (sal_Int32
)maRoadmapItems
.size()) || (Index
< 0))
293 lcl_throwIndexOutOfBoundsException( );
294 Reference
< XInterface
> xRoadmapItem
;
295 maRoadmapItems
.erase( maRoadmapItems
.begin() + Index
);
296 ContainerEvent aEvent
= GetContainerEvent(Index
, xRoadmapItem
);
297 maContainerListeners
.elementRemoved( aEvent
);
298 Reference
< XPropertySet
> xPropertySet( (XAggregation
*) (::cppu::OWeakAggObject
*)this, UNO_QUERY
);
299 sal_Int16 n_CurrentItemID
= GetCurrentItemID( xPropertySet
);
301 if ( Index
<= n_CurrentItemID
)
303 if ( n_CurrentItemID
>= (sal_Int32
)maRoadmapItems
.size() )
305 n_CurrentItemID
= sal::static_int_cast
< sal_Int16
>(
306 maRoadmapItems
.size()-1);
307 if ( n_CurrentItemID
< 0 )
309 aAny
<<= n_CurrentItemID
;
311 else if (Index
== n_CurrentItemID
)
312 aAny
<<= ( sal_Int16
) -1;
313 else if( Index
< n_CurrentItemID
)
314 aAny
<<= ( sal_Int16
) ( n_CurrentItemID
- 1 );
315 xPropertySet
->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
), aAny
);
320 void SAL_CALL
UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index
, const Any
& _Element
)
321 throw (IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
323 Reference
< XInterface
> xRoadmapItem
;
324 _Element
>>= xRoadmapItem
;
325 MakeRMItemValidation( Index
, xRoadmapItem
);
326 SetRMItemDefaultProperties( Index
, xRoadmapItem
);
327 maRoadmapItems
.erase( maRoadmapItems
.begin() + Index
);
328 maRoadmapItems
.insert( maRoadmapItems
.begin() + Index
, xRoadmapItem
); //push_back( xRoadmapItem );
329 ContainerEvent aEvent
= GetContainerEvent(Index
, xRoadmapItem
);
330 maContainerListeners
.elementReplaced( aEvent
);
334 Type SAL_CALL
UnoControlRoadmapModel::getElementType() throw(RuntimeException
)
336 Type aType
= getCppuType( ( Reference
< XPropertySet
>* ) NULL
);
341 sal_Bool SAL_CALL
UnoControlRoadmapModel::hasElements() throw(RuntimeException
)
343 return !maRoadmapItems
.empty();
347 void SAL_CALL
UnoControlRoadmapModel::addContainerListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
)
349 maContainerListeners
.addInterface( xListener
);
352 void SAL_CALL
UnoControlRoadmapModel::removeContainerListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XContainerListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
)
354 maContainerListeners
.removeInterface( xListener
);
357 // ===================================================================
358 // = UnoRoadmapControl
359 // ===================================================================
360 // -------------------------------------------------------------------
361 UnoRoadmapControl::UnoRoadmapControl( const Reference
< XMultiServiceFactory
>& i_factory
)
362 :UnoControlRoadmap_Base( i_factory
)
363 ,maItemListeners( *this )
367 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl
, UnoControlRoadmap_Base
, UnoControlRoadmap_IBase
)
368 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl
, UnoControlRoadmap_Base
, UnoControlRoadmap_IBase
)
371 sal_Bool SAL_CALL
UnoRoadmapControl::setModel(const Reference
< XControlModel
>& _rModel
) throw ( RuntimeException
)
375 Reference
< XContainer
> xC( getModel(), UNO_QUERY
);
377 xC
->removeContainerListener( this );
379 sal_Bool bReturn
= UnoControlBase::setModel( _rModel
);
381 xC
= xC
.query( getModel());
383 xC
->addContainerListener( this );
389 // -------------------------------------------------------------------
390 ::rtl::OUString
UnoRoadmapControl::GetComponentServiceName()
392 return ::rtl::OUString("Roadmap");
397 void UnoRoadmapControl::dispose() throw(RuntimeException
)
400 aEvt
.Source
= (::cppu::OWeakObject
*)this;
401 maItemListeners
.disposeAndClear( aEvt
);
402 UnoControl::dispose();
407 void UnoRoadmapControl::elementInserted( const ContainerEvent
& rEvent
)throw(RuntimeException
)
409 Reference
< XInterface
> xRoadmapItem
;
410 rEvent
.Element
>>= xRoadmapItem
;
411 Reference
< XPropertySet
> xRoadmapPropertySet( xRoadmapItem
, UNO_QUERY
);
412 if ( xRoadmapPropertySet
.is() )
413 xRoadmapPropertySet
->addPropertyChangeListener( rtl::OUString(), this );
415 Reference
< XContainerListener
> xPeer(getPeer(), UNO_QUERY
);
418 xPeer
->elementInserted( rEvent
);
419 Reference
< XPropertySet
> xPropertySet( xPeer
, UNO_QUERY
);
420 if ( xPropertySet
.is() )
421 xPropertySet
->addPropertyChangeListener( rtl::OUString(), this );
426 void UnoRoadmapControl::elementRemoved( const ContainerEvent
& rEvent
)throw(RuntimeException
)
428 Reference
< XContainerListener
> xPeer(getPeer(), UNO_QUERY
);
430 xPeer
->elementRemoved( rEvent
);
431 Reference
< XInterface
> xRoadmapItem
;
432 rEvent
.Element
>>= xRoadmapItem
;
433 Reference
< XPropertySet
> xPropertySet( xRoadmapItem
, UNO_QUERY
);
434 if ( xPropertySet
.is() )
435 xPropertySet
->removePropertyChangeListener( rtl::OUString(), this );
439 void UnoRoadmapControl::elementReplaced( const ContainerEvent
& rEvent
)throw(RuntimeException
)
441 Reference
< XContainerListener
> xPeer(getPeer(), UNO_QUERY
);
443 xPeer
->elementReplaced( rEvent
);
447 void SAL_CALL
UnoRoadmapControl::itemStateChanged( const ItemEvent
& rEvent
) throw (RuntimeException
)
449 sal_Int16 CurItemIndex
= sal::static_int_cast
< sal_Int16
>(rEvent
.ItemId
);
451 aAny
<<= CurItemIndex
;
452 Reference
< XControlModel
> xModel( getModel( ), UNO_QUERY
);
453 Reference
< XPropertySet
> xPropertySet( xModel
, UNO_QUERY
);
454 xPropertySet
->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
), aAny
);
455 if ( maItemListeners
.getLength() )
456 maItemListeners
.itemStateChanged( rEvent
);
460 void SAL_CALL
UnoRoadmapControl::addItemListener( const Reference
< XItemListener
>& l
) throw (RuntimeException
)
462 maItemListeners
.addInterface( l
);
463 if( getPeer().is() && maItemListeners
.getLength() == 1 )
465 Reference
< XItemEventBroadcaster
> xRoadmap( getPeer(), UNO_QUERY
);
466 xRoadmap
->addItemListener( this );
471 void SAL_CALL
UnoRoadmapControl::removeItemListener( const Reference
< XItemListener
>& l
) throw (RuntimeException
)
473 if( getPeer().is() && maItemListeners
.getLength() == 1 )
475 Reference
< XItemEventBroadcaster
> xRoadmap( getPeer(), UNO_QUERY
);
476 xRoadmap
->removeItemListener( this );
479 maItemListeners
.removeInterface( l
);
483 void SAL_CALL
UnoRoadmapControl::propertyChange( const PropertyChangeEvent
& evt
) throw (RuntimeException
)
485 Reference
< XPropertyChangeListener
> xPeer(getPeer(), UNO_QUERY
);
487 xPeer
->propertyChange( evt
);
492 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */