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 <controls/roadmapcontrol.hxx>
22 #include <controls/roadmapentry.hxx>
23 #include <helper/property.hxx>
24 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <helper/unopropertyarrayhelper.hxx>
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
;
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();
54 // = UnoControlRoadmapModel
57 UnoControlRoadmapModel::UnoControlRoadmapModel( const Reference
< XComponentContext
>& 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
);
79 OUString
UnoControlRoadmapModel::getServiceName()
81 return "stardiv.vcl.controlmodel.Roadmap";
84 OUString
UnoControlRoadmapModel::getImplementationName()
86 return "stardiv.Toolkit.UnoControlRoadmapModel";
89 css::uno::Sequence
<OUString
>
90 UnoControlRoadmapModel::getSupportedServiceNames()
92 auto s(UnoControlRoadmapModel_Base::getSupportedServiceNames());
93 s
.realloc(s
.getLength() + 2);
94 auto ps
= s
.getArray();
95 ps
[s
.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmapModel";
96 ps
[s
.getLength() - 1] = "stardiv.vcl.controlmodel.Roadmap";
100 Any
UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId
) const
105 case BASEPROPERTY_COMPLETE
:
108 case BASEPROPERTY_ACTIVATED
:
111 case BASEPROPERTY_CURRENTITEMID
:
112 aReturn
<<= sal_Int16(-1);
114 case BASEPROPERTY_TEXT
:
116 case BASEPROPERTY_BORDER
:
117 aReturn
<<= sal_Int16(2); // No Border
119 case BASEPROPERTY_DEFAULTCONTROL
:
120 aReturn
<<= OUString( "stardiv.vcl.control.Roadmap" );
122 default : aReturn
= UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId
); break;
129 Reference
< XInterface
> SAL_CALL
UnoControlRoadmapModel::createInstance( )
131 rtl::Reference
<ORoadmapEntry
> pRoadmapItem
= new ORoadmapEntry();
132 Reference
< XInterface
> xNewRoadmapItem
= static_cast<cppu::OWeakObject
*>(pRoadmapItem
.get());
133 return xNewRoadmapItem
;
137 Reference
< XInterface
> SAL_CALL
UnoControlRoadmapModel::createInstanceWithArguments( const Sequence
< Any
>& /*aArguments*/ )
139 // Todo: implementation of the arguments handling
140 rtl::Reference
<ORoadmapEntry
> pRoadmapItem
= new ORoadmapEntry();
141 Reference
< XInterface
> xNewRoadmapItem
= static_cast<cppu::OWeakObject
*>(pRoadmapItem
.get());
142 return xNewRoadmapItem
;
146 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel
, UnoControlRoadmapModel_Base
, UnoControlRoadmapModel_IBase
)
149 css::uno::Any SAL_CALL
UnoControlRoadmapModel::queryAggregation( const css::uno::Type
& rType
)
151 Any aRet
= UnoControlRoadmapModel_Base::queryAggregation( rType
);
152 if ( !aRet
.hasValue() )
153 aRet
= UnoControlRoadmapModel_IBase::queryInterface( rType
);
158 ::cppu::IPropertyArrayHelper
& UnoControlRoadmapModel::getInfoHelper()
160 static UnoPropertyArrayHelper
aHelper( ImplGetPropertyIds() );
165 // beans::XMultiPropertySet
167 Reference
< XPropertySetInfo
> UnoControlRoadmapModel::getPropertySetInfo( )
169 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
174 sal_Int32 SAL_CALL
UnoControlRoadmapModel::getCount()
176 return maRoadmapItems
.size();
179 Any SAL_CALL
UnoControlRoadmapModel::getByIndex( sal_Int32 Index
)
181 if ((Index
< 0) || ( o3tl::make_unsigned(Index
) >= maRoadmapItems
.size()))
182 lcl_throwIndexOutOfBoundsException( );
183 Any
aAny( maRoadmapItems
.at( Index
) );
188 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index
, const Reference
< XInterface
>& xRoadmapItem
)
190 if (( Index
< 0 ) || (o3tl::make_unsigned(Index
) > maRoadmapItems
.size()) )
191 lcl_throwIndexOutOfBoundsException( );
192 if ( !xRoadmapItem
.is() )
193 lcl_throwIllegalArgumentException();
194 Reference
< XServiceInfo
> xServiceInfo( xRoadmapItem
, UNO_QUERY
);
195 bool bIsRoadmapItem
= xServiceInfo
->supportsService("com.sun.star.awt.RoadmapItem");
196 if ( !bIsRoadmapItem
)
197 lcl_throwIllegalArgumentException();
201 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const Reference
< XInterface
>& xRoadmapItem
)
203 Reference
< XPropertySet
> xPropertySet( xRoadmapItem
, UNO_QUERY
);
204 Reference
< XPropertySet
> xProps( xRoadmapItem
, UNO_QUERY
);
208 Any aValue
= xPropertySet
->getPropertyValue("ID");
210 if (LocID
< 0) // index may not be smaller than zero
212 xPropertySet
->setPropertyValue("ID", Any(GetUniqueID()) );
218 // The performance of this method could certainly be improved.
219 // As long as only vectors with up to 10 elements are
220 // involved it should be sufficient
221 sal_Int32
UnoControlRoadmapModel::GetUniqueID()
224 bool bIncrement
= true;
226 sal_Int32 n_CurItemID
= 0;
227 Reference
< XInterface
> CurRoadmapItem
;
231 for ( const auto& rRoadmapItem
: maRoadmapItems
)
233 CurRoadmapItem
= rRoadmapItem
;
234 Reference
< XPropertySet
> xPropertySet( CurRoadmapItem
, UNO_QUERY
);
235 aAny
= xPropertySet
->getPropertyValue("ID");
236 aAny
>>= n_CurItemID
;
237 if (n_CurItemID
== CurID
)
249 ContainerEvent
UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index
, const Reference
< XInterface
>& xRoadmapItem
)
251 ContainerEvent aEvent
;
252 aEvent
.Source
= *this;
253 aEvent
.Element
<<= xRoadmapItem
;
254 aEvent
.Accessor
<<= Index
;
259 sal_Int16
UnoControlRoadmapModel::GetCurrentItemID( const Reference
< XPropertySet
>& xPropertySet
)
261 Any aAny
= xPropertySet
->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
) );
262 sal_Int16 n_CurrentItemID
= 0;
263 aAny
>>= n_CurrentItemID
;
264 return n_CurrentItemID
;
268 void SAL_CALL
UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index
, const Any
& Element
)
270 if ( ( Index
>= ( static_cast<sal_Int32
>(maRoadmapItems
.size()) + 1 ) ) || (Index
< 0))
271 lcl_throwIndexOutOfBoundsException( );
272 Reference
< XInterface
> xRoadmapItem
;
273 Element
>>= xRoadmapItem
;
274 MakeRMItemValidation( Index
, xRoadmapItem
);
275 SetRMItemDefaultProperties( xRoadmapItem
);
276 maRoadmapItems
.insert( maRoadmapItems
.begin() + Index
, xRoadmapItem
);
277 ContainerEvent aEvent
= GetContainerEvent(Index
, xRoadmapItem
);
278 maContainerListeners
.elementInserted( aEvent
);
279 Reference
< XPropertySet
> xPropertySet( this );
280 sal_Int16 n_CurrentItemID
= GetCurrentItemID( xPropertySet
);
281 if ( Index
<= n_CurrentItemID
)
283 Any
aAny(static_cast<sal_Int16
>( n_CurrentItemID
+ 1 ) );
284 xPropertySet
->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
), aAny
);
289 void SAL_CALL
UnoControlRoadmapModel::removeByIndex( sal_Int32 Index
)
291 if ((Index
< 0) || ( o3tl::make_unsigned(Index
) > maRoadmapItems
.size()))
292 lcl_throwIndexOutOfBoundsException( );
293 Reference
< XInterface
> xRoadmapItem
;
294 maRoadmapItems
.erase( maRoadmapItems
.begin() + Index
);
295 ContainerEvent aEvent
= GetContainerEvent(Index
, xRoadmapItem
);
296 maContainerListeners
.elementRemoved( aEvent
);
297 Reference
< XPropertySet
> xPropertySet( this );
298 sal_Int16 n_CurrentItemID
= GetCurrentItemID( xPropertySet
);
300 if ( Index
> n_CurrentItemID
)
303 if ( n_CurrentItemID
>= static_cast<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
<<= static_cast<sal_Int16
>( n_CurrentItemID
- 1 );
315 xPropertySet
->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
), aAny
);
319 void SAL_CALL
UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index
, const Any
& Element
)
321 Reference
< XInterface
> xRoadmapItem
;
322 Element
>>= xRoadmapItem
;
323 MakeRMItemValidation( Index
, xRoadmapItem
);
324 SetRMItemDefaultProperties( xRoadmapItem
);
325 maRoadmapItems
.erase( maRoadmapItems
.begin() + Index
);
326 maRoadmapItems
.insert( maRoadmapItems
.begin() + Index
, xRoadmapItem
); //push_back( xRoadmapItem );
327 ContainerEvent aEvent
= GetContainerEvent(Index
, xRoadmapItem
);
328 maContainerListeners
.elementReplaced( aEvent
);
332 Type SAL_CALL
UnoControlRoadmapModel::getElementType()
334 Type aType
= cppu::UnoType
<XPropertySet
>::get();
339 sal_Bool SAL_CALL
UnoControlRoadmapModel::hasElements()
341 return !maRoadmapItems
.empty();
345 void SAL_CALL
UnoControlRoadmapModel::addContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& xListener
)
347 maContainerListeners
.addInterface( xListener
);
350 void SAL_CALL
UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference
< css::container::XContainerListener
>& xListener
)
352 maContainerListeners
.removeInterface( xListener
);
356 // = UnoRoadmapControl
359 UnoRoadmapControl::UnoRoadmapControl()
360 :maItemListeners( *this )
364 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl
, UnoControlRoadmap_Base
, UnoControlRoadmap_IBase
)
366 css::uno::Any
UnoRoadmapControl::queryAggregation(css::uno::Type
const & aType
) {
367 auto ret
= UnoControlRoadmap_Base::queryAggregation(aType
);
368 if (!ret
.hasValue()) {
369 ret
= UnoControlRoadmap_IBase::queryInterface(aType
);
375 sal_Bool SAL_CALL
UnoRoadmapControl::setModel(const Reference
< XControlModel
>& _rModel
)
377 Reference
< XContainer
> xC( getModel(), UNO_QUERY
);
379 xC
->removeContainerListener( this );
381 bool bReturn
= UnoControlBase::setModel( _rModel
);
383 xC
.set(getModel(), css::uno::UNO_QUERY
);
385 xC
->addContainerListener( this );
391 OUString
UnoRoadmapControl::GetComponentServiceName() const
397 void UnoRoadmapControl::dispose()
400 aEvt
.Source
= static_cast<cppu::OWeakObject
*>(this);
401 maItemListeners
.disposeAndClear( aEvt
);
402 UnoControl::dispose();
406 void UnoRoadmapControl::elementInserted( const ContainerEvent
& rEvent
)
408 Reference
< XInterface
> xRoadmapItem
;
409 rEvent
.Element
>>= xRoadmapItem
;
410 Reference
< XPropertySet
> xRoadmapPropertySet( xRoadmapItem
, UNO_QUERY
);
411 if ( xRoadmapPropertySet
.is() )
412 xRoadmapPropertySet
->addPropertyChangeListener( OUString(), this );
414 Reference
< XContainerListener
> xPeer(getPeer(), UNO_QUERY
);
417 xPeer
->elementInserted( rEvent
);
418 Reference
< XPropertySet
> xPropertySet( xPeer
, UNO_QUERY
);
419 if ( xPropertySet
.is() )
420 xPropertySet
->addPropertyChangeListener( OUString(), this );
425 void UnoRoadmapControl::elementRemoved( const ContainerEvent
& rEvent
)
427 Reference
< XContainerListener
> xPeer(getPeer(), UNO_QUERY
);
429 xPeer
->elementRemoved( rEvent
);
430 Reference
< XInterface
> xRoadmapItem
;
431 rEvent
.Element
>>= xRoadmapItem
;
432 Reference
< XPropertySet
> xPropertySet( xRoadmapItem
, UNO_QUERY
);
433 if ( xPropertySet
.is() )
434 xPropertySet
->removePropertyChangeListener( OUString(), this );
438 void UnoRoadmapControl::elementReplaced( const ContainerEvent
& rEvent
)
440 Reference
< XContainerListener
> xPeer(getPeer(), UNO_QUERY
);
442 xPeer
->elementReplaced( rEvent
);
446 void SAL_CALL
UnoRoadmapControl::itemStateChanged( const ItemEvent
& rEvent
)
448 sal_Int16 CurItemIndex
= sal::static_int_cast
< sal_Int16
>(rEvent
.ItemId
);
449 Reference
< XControlModel
> xModel
= getModel( );
450 Reference
< XPropertySet
> xPropertySet( xModel
, UNO_QUERY
);
451 xPropertySet
->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID
), Any(CurItemIndex
) );
452 if ( maItemListeners
.getLength() )
453 maItemListeners
.itemStateChanged( rEvent
);
457 void SAL_CALL
UnoRoadmapControl::addItemListener( const Reference
< XItemListener
>& l
)
459 maItemListeners
.addInterface( l
);
460 if( getPeer().is() && maItemListeners
.getLength() == 1 )
462 Reference
< XItemEventBroadcaster
> xRoadmap( getPeer(), UNO_QUERY
);
463 xRoadmap
->addItemListener( this );
468 void SAL_CALL
UnoRoadmapControl::removeItemListener( const Reference
< XItemListener
>& l
)
470 if( getPeer().is() && maItemListeners
.getLength() == 1 )
472 Reference
< XItemEventBroadcaster
> xRoadmap( getPeer(), UNO_QUERY
);
473 xRoadmap
->removeItemListener( this );
476 maItemListeners
.removeInterface( l
);
480 void SAL_CALL
UnoRoadmapControl::propertyChange( const PropertyChangeEvent
& evt
)
482 Reference
< XPropertyChangeListener
> xPeer(getPeer(), UNO_QUERY
);
484 xPeer
->propertyChange( evt
);
487 OUString
UnoRoadmapControl::getImplementationName()
489 return "stardiv.Toolkit.UnoRoadmapControl";
492 css::uno::Sequence
<OUString
> UnoRoadmapControl::getSupportedServiceNames()
494 auto s(UnoControlBase::getSupportedServiceNames());
495 s
.realloc(s
.getLength() + 2);
496 auto ps
= s
.getArray();
497 ps
[s
.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
498 ps
[s
.getLength() - 1] = "stardiv.vcl.control.Roadmap";
504 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
505 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
506 css::uno::XComponentContext
*context
,
507 css::uno::Sequence
<css::uno::Any
> const &)
509 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context
));
512 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
513 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
514 css::uno::XComponentContext
*,
515 css::uno::Sequence
<css::uno::Any
> const &)
517 return cppu::acquire(new toolkit::UnoRoadmapControl());
520 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */