Avoid potential negative array index access to cached text.
[LibreOffice.git] / toolkit / source / controls / roadmapcontrol.cxx
blobe46a607ef3aef773471679d8db91e8ba81ade721
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 .
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>
29 namespace toolkit
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;
40 // helper
42 // = UnoControlRoadmapModel
45 UnoControlRoadmapModel::UnoControlRoadmapModel( const Reference< XComponentContext >& i_factory )
46 :UnoControlRoadmapModel_Base( i_factory )
47 ,maContainerListeners( *this )
49 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
50 ImplRegisterProperty( BASEPROPERTY_BORDER );
51 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
52 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
53 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
54 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
55 ImplRegisterProperty( BASEPROPERTY_HELPURL );
56 ImplRegisterProperty( BASEPROPERTY_IMAGEURL );
57 ImplRegisterProperty( BASEPROPERTY_GRAPHIC );
58 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
59 ImplRegisterProperty( BASEPROPERTY_COMPLETE );
60 ImplRegisterProperty( BASEPROPERTY_ACTIVATED );
61 ImplRegisterProperty( BASEPROPERTY_CURRENTITEMID );
62 ImplRegisterProperty( BASEPROPERTY_TABSTOP );
63 ImplRegisterProperty( BASEPROPERTY_TEXT );
67 OUString UnoControlRoadmapModel::getServiceName()
69 return "stardiv.vcl.controlmodel.Roadmap";
72 OUString UnoControlRoadmapModel::getImplementationName()
74 return "stardiv.Toolkit.UnoControlRoadmapModel";
77 css::uno::Sequence<OUString>
78 UnoControlRoadmapModel::getSupportedServiceNames()
80 auto s(UnoControlRoadmapModel_Base::getSupportedServiceNames());
81 s.realloc(s.getLength() + 2);
82 auto ps = s.getArray();
83 ps[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmapModel";
84 ps[s.getLength() - 1] = "stardiv.vcl.controlmodel.Roadmap";
85 return s;
88 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
90 Any aReturn;
91 switch (nPropId)
93 case BASEPROPERTY_COMPLETE:
94 aReturn <<= true;
95 break;
96 case BASEPROPERTY_ACTIVATED:
97 aReturn <<= true;
98 break;
99 case BASEPROPERTY_CURRENTITEMID:
100 aReturn <<= sal_Int16(-1);
101 break;
102 case BASEPROPERTY_TEXT:
103 break;
104 case BASEPROPERTY_BORDER:
105 aReturn <<= sal_Int16(2); // No Border
106 break;
107 case BASEPROPERTY_DEFAULTCONTROL:
108 aReturn <<= OUString( "stardiv.vcl.control.Roadmap" );
109 break;
110 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break;
113 return aReturn;
117 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( )
119 return cppu::getXWeak(new ORoadmapEntry());
123 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ )
125 // Todo: implementation of the arguments handling
126 return cppu::getXWeak(new ORoadmapEntry());
130 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
133 css::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const css::uno::Type & rType )
135 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
136 if ( !aRet.hasValue() )
137 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
138 return aRet;
142 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
144 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
145 return aHelper;
149 // beans::XMultiPropertySet
151 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( )
153 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
154 return xInfo;
158 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount()
160 return maRoadmapItems.size();
163 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index )
165 if ((Index < 0) || ( o3tl::make_unsigned(Index) >= maRoadmapItems.size()))
166 throw IndexOutOfBoundsException();
167 Any aAny( maRoadmapItems.at( Index ) );
168 return aAny;
172 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, const Reference< XInterface >& xRoadmapItem )
174 if (( Index < 0 ) || (o3tl::make_unsigned(Index) > maRoadmapItems.size()) )
175 throw IndexOutOfBoundsException();
176 if ( !xRoadmapItem.is() )
177 throw IllegalArgumentException();
178 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
179 bool bIsRoadmapItem = xServiceInfo->supportsService("com.sun.star.awt.RoadmapItem");
180 if ( !bIsRoadmapItem )
181 throw IllegalArgumentException();
185 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const Reference< XInterface >& xRoadmapItem)
187 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
188 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
189 if ( xProps.is() )
191 sal_Int32 LocID = 0;
192 Any aValue = xPropertySet->getPropertyValue("ID");
193 aValue >>= LocID;
194 if (LocID < 0) // index may not be smaller than zero
196 xPropertySet->setPropertyValue("ID", Any(GetUniqueID()) );
202 // The performance of this method could certainly be improved.
203 // As long as only vectors with up to 10 elements are
204 // involved it should be sufficient
205 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
207 Any aAny;
208 bool bIncrement = true;
209 sal_Int32 CurID = 0;
210 sal_Int32 n_CurItemID = 0;
211 Reference< XInterface > CurRoadmapItem;
212 while ( bIncrement )
214 bIncrement = false;
215 for ( const auto& rRoadmapItem : maRoadmapItems )
217 CurRoadmapItem = rRoadmapItem;
218 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
219 aAny = xPropertySet->getPropertyValue("ID");
220 aAny >>= n_CurItemID;
221 if (n_CurItemID == CurID)
223 bIncrement = true;
224 CurID++;
225 break;
229 return CurID;
233 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, const Reference< XInterface >& xRoadmapItem)
235 ContainerEvent aEvent;
236 aEvent.Source = *this;
237 aEvent.Element <<= xRoadmapItem;
238 aEvent.Accessor <<= Index;
239 return aEvent;
243 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( const Reference< XPropertySet >& xPropertySet )
245 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
246 sal_Int16 n_CurrentItemID = 0;
247 aAny >>= n_CurrentItemID;
248 return n_CurrentItemID;
252 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& Element)
254 if ( ( Index >= ( static_cast<sal_Int32>(maRoadmapItems.size()) + 1 ) ) || (Index < 0))
255 throw IndexOutOfBoundsException();
256 Reference< XInterface > xRoadmapItem;
257 Element >>= xRoadmapItem;
258 MakeRMItemValidation( Index, xRoadmapItem);
259 SetRMItemDefaultProperties( xRoadmapItem );
260 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
261 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
262 maContainerListeners.elementInserted( aEvent );
263 Reference< XPropertySet > xPropertySet( this );
264 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
265 if ( Index <= n_CurrentItemID )
267 Any aAny(static_cast<sal_Int16>( n_CurrentItemID + 1 ) );
268 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
273 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
275 if ((Index < 0) || ( o3tl::make_unsigned(Index) > maRoadmapItems.size()))
276 throw IndexOutOfBoundsException();
277 Reference< XInterface > xRoadmapItem;
278 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
279 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
280 maContainerListeners.elementRemoved( aEvent );
281 Reference< XPropertySet > xPropertySet( this );
282 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
283 Any aAny;
284 if ( Index > n_CurrentItemID )
285 return;
287 if ( n_CurrentItemID >= static_cast<sal_Int32>(maRoadmapItems.size()) )
289 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
290 maRoadmapItems.size()-1);
291 if ( n_CurrentItemID < 0 )
292 return;
293 aAny <<= n_CurrentItemID;
295 else if (Index == n_CurrentItemID)
296 aAny <<= sal_Int16(-1);
297 else if( Index < n_CurrentItemID)
298 aAny <<= static_cast<sal_Int16>( n_CurrentItemID - 1 );
299 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
303 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& Element)
305 Reference< XInterface > xRoadmapItem;
306 Element >>= xRoadmapItem;
307 MakeRMItemValidation( Index, xRoadmapItem);
308 SetRMItemDefaultProperties( xRoadmapItem );
309 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
310 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
311 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
312 maContainerListeners.elementReplaced( aEvent );
316 Type SAL_CALL UnoControlRoadmapModel::getElementType()
318 Type aType = cppu::UnoType<XPropertySet>::get();
319 return aType;
323 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements()
325 return !maRoadmapItems.empty();
329 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
331 maContainerListeners.addInterface( xListener );
334 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
336 maContainerListeners.removeInterface( xListener );
340 // = UnoRoadmapControl
343 UnoRoadmapControl::UnoRoadmapControl()
344 :maItemListeners( *this )
348 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
350 css::uno::Any UnoRoadmapControl::queryAggregation(css::uno::Type const & aType) {
351 auto ret = UnoControlRoadmap_Base::queryAggregation(aType);
352 if (!ret.hasValue()) {
353 ret = UnoControlRoadmap_IBase::queryInterface(aType);
355 return ret;
359 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel)
361 Reference< XContainer > xC( getModel(), UNO_QUERY );
362 if ( xC.is() )
363 xC->removeContainerListener( this );
365 bool bReturn = UnoControlBase::setModel( _rModel );
367 xC.set(getModel(), css::uno::UNO_QUERY);
368 if ( xC.is() )
369 xC->addContainerListener( this );
371 return bReturn;
375 OUString UnoRoadmapControl::GetComponentServiceName() const
377 return "Roadmap";
381 void UnoRoadmapControl::dispose()
383 EventObject aEvt;
384 aEvt.Source = getXWeak();
385 maItemListeners.disposeAndClear( aEvt );
386 UnoControl::dispose();
390 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )
392 Reference< XInterface > xRoadmapItem;
393 rEvent.Element >>= xRoadmapItem;
394 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
395 if ( xRoadmapPropertySet.is() )
396 xRoadmapPropertySet->addPropertyChangeListener( OUString(), this );
398 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
399 if ( xPeer.is() )
401 xPeer->elementInserted( rEvent );
402 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
403 if ( xPropertySet.is() )
404 xPropertySet->addPropertyChangeListener( OUString(), this );
409 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )
411 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
412 if ( xPeer.is() )
413 xPeer->elementRemoved( rEvent );
414 Reference< XInterface > xRoadmapItem;
415 rEvent.Element >>= xRoadmapItem;
416 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
417 if ( xPropertySet.is() )
418 xPropertySet->removePropertyChangeListener( OUString(), this );
422 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )
424 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
425 if ( xPeer.is() )
426 xPeer->elementReplaced( rEvent );
430 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent )
432 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
433 Reference< XControlModel > xModel = getModel( );
434 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
435 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), Any(CurItemIndex) );
436 if ( maItemListeners.getLength() )
437 maItemListeners.itemStateChanged( rEvent );
441 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l )
443 maItemListeners.addInterface( l );
444 if( getPeer().is() && maItemListeners.getLength() == 1 )
446 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
447 xRoadmap->addItemListener( this );
452 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l )
454 if( getPeer().is() && maItemListeners.getLength() == 1 )
456 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
457 xRoadmap->removeItemListener( this );
460 maItemListeners.removeInterface( l );
464 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt )
466 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
467 if ( xPeer.is() )
468 xPeer->propertyChange( evt );
471 OUString UnoRoadmapControl::getImplementationName()
473 return "stardiv.Toolkit.UnoRoadmapControl";
476 css::uno::Sequence<OUString> UnoRoadmapControl::getSupportedServiceNames()
478 auto s(UnoControlBase::getSupportedServiceNames());
479 s.realloc(s.getLength() + 2);
480 auto ps = s.getArray();
481 ps[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
482 ps[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
483 return s;
488 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
489 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
490 css::uno::XComponentContext *context,
491 css::uno::Sequence<css::uno::Any> const &)
493 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
496 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
497 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
498 css::uno::XComponentContext *,
499 css::uno::Sequence<css::uno::Any> const &)
501 return cppu::acquire(new toolkit::UnoRoadmapControl());
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */