Fix GNU C++ version check
[LibreOffice.git] / toolkit / source / controls / roadmapcontrol.cxx
blob0c6538bd9c3d06e7e7dea39b4691d98978f50bdd
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 u"stardiv.vcl.controlmodel.Roadmap"_ustr;
72 OUString UnoControlRoadmapModel::getImplementationName()
74 return u"stardiv.Toolkit.UnoControlRoadmapModel"_ustr;
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 case BASEPROPERTY_ACTIVATED:
95 aReturn <<= true;
96 break;
97 case BASEPROPERTY_CURRENTITEMID:
98 aReturn <<= sal_Int16(-1);
99 break;
100 case BASEPROPERTY_TEXT:
101 break;
102 case BASEPROPERTY_BORDER:
103 aReturn <<= sal_Int16(2); // No Border
104 break;
105 case BASEPROPERTY_DEFAULTCONTROL:
106 aReturn <<= u"stardiv.vcl.control.Roadmap"_ustr;
107 break;
108 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break;
111 return aReturn;
115 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( )
117 return cppu::getXWeak(new ORoadmapEntry());
121 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ )
123 // Todo: implementation of the arguments handling
124 return cppu::getXWeak(new ORoadmapEntry());
128 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
131 css::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const css::uno::Type & rType )
133 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
134 if ( !aRet.hasValue() )
135 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
136 return aRet;
140 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
142 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
143 return aHelper;
147 // beans::XMultiPropertySet
149 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( )
151 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
152 return xInfo;
156 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount()
158 return maRoadmapItems.size();
161 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index )
163 if ((Index < 0) || ( o3tl::make_unsigned(Index) >= maRoadmapItems.size()))
164 throw IndexOutOfBoundsException();
165 Any aAny( maRoadmapItems.at( Index ) );
166 return aAny;
170 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, const Reference< XInterface >& xRoadmapItem )
172 if (( Index < 0 ) || (o3tl::make_unsigned(Index) > maRoadmapItems.size()) )
173 throw IndexOutOfBoundsException();
174 if ( !xRoadmapItem.is() )
175 throw IllegalArgumentException();
176 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
177 bool bIsRoadmapItem = xServiceInfo->supportsService(u"com.sun.star.awt.RoadmapItem"_ustr);
178 if ( !bIsRoadmapItem )
179 throw IllegalArgumentException();
183 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const Reference< XInterface >& xRoadmapItem)
185 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
186 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
187 if ( xProps.is() )
189 sal_Int32 LocID = 0;
190 Any aValue = xPropertySet->getPropertyValue(u"ID"_ustr);
191 aValue >>= LocID;
192 if (LocID < 0) // index may not be smaller than zero
194 xPropertySet->setPropertyValue(u"ID"_ustr, Any(GetUniqueID()) );
200 // The performance of this method could certainly be improved.
201 // As long as only vectors with up to 10 elements are
202 // involved it should be sufficient
203 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
205 Any aAny;
206 bool bIncrement = true;
207 sal_Int32 CurID = 0;
208 sal_Int32 n_CurItemID = 0;
209 Reference< XInterface > CurRoadmapItem;
210 while ( bIncrement )
212 bIncrement = false;
213 for ( const auto& rRoadmapItem : maRoadmapItems )
215 CurRoadmapItem = rRoadmapItem;
216 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
217 aAny = xPropertySet->getPropertyValue(u"ID"_ustr);
218 aAny >>= n_CurItemID;
219 if (n_CurItemID == CurID)
221 bIncrement = true;
222 CurID++;
223 break;
227 return CurID;
231 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, const Reference< XInterface >& xRoadmapItem)
233 ContainerEvent aEvent;
234 aEvent.Source = *this;
235 aEvent.Element <<= xRoadmapItem;
236 aEvent.Accessor <<= Index;
237 return aEvent;
241 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( const Reference< XPropertySet >& xPropertySet )
243 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
244 sal_Int16 n_CurrentItemID = 0;
245 aAny >>= n_CurrentItemID;
246 return n_CurrentItemID;
250 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& Element)
252 if ( ( Index >= ( static_cast<sal_Int32>(maRoadmapItems.size()) + 1 ) ) || (Index < 0))
253 throw IndexOutOfBoundsException();
254 Reference< XInterface > xRoadmapItem;
255 Element >>= xRoadmapItem;
256 MakeRMItemValidation( Index, xRoadmapItem);
257 SetRMItemDefaultProperties( xRoadmapItem );
258 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
259 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
260 maContainerListeners.elementInserted( aEvent );
261 Reference< XPropertySet > xPropertySet( this );
262 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
263 if ( Index <= n_CurrentItemID )
265 Any aAny(static_cast<sal_Int16>( n_CurrentItemID + 1 ) );
266 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
271 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
273 if ((Index < 0) || ( o3tl::make_unsigned(Index) > maRoadmapItems.size()))
274 throw IndexOutOfBoundsException();
275 Reference< XInterface > xRoadmapItem;
276 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
277 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
278 maContainerListeners.elementRemoved( aEvent );
279 Reference< XPropertySet > xPropertySet( this );
280 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
281 Any aAny;
282 if ( Index > n_CurrentItemID )
283 return;
285 if ( n_CurrentItemID >= static_cast<sal_Int32>(maRoadmapItems.size()) )
287 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
288 maRoadmapItems.size()-1);
289 if ( n_CurrentItemID < 0 )
290 return;
291 aAny <<= n_CurrentItemID;
293 else if (Index == n_CurrentItemID)
294 aAny <<= sal_Int16(-1);
295 else if( Index < n_CurrentItemID)
296 aAny <<= static_cast<sal_Int16>( n_CurrentItemID - 1 );
297 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
301 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& Element)
303 Reference< XInterface > xRoadmapItem;
304 Element >>= xRoadmapItem;
305 MakeRMItemValidation( Index, xRoadmapItem);
306 SetRMItemDefaultProperties( xRoadmapItem );
307 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
308 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
309 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
310 maContainerListeners.elementReplaced( aEvent );
314 Type SAL_CALL UnoControlRoadmapModel::getElementType()
316 Type aType = cppu::UnoType<XPropertySet>::get();
317 return aType;
321 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements()
323 return !maRoadmapItems.empty();
327 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
329 maContainerListeners.addInterface( xListener );
332 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
334 maContainerListeners.removeInterface( xListener );
338 // = UnoRoadmapControl
341 UnoRoadmapControl::UnoRoadmapControl()
342 :maItemListeners( *this )
346 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
348 css::uno::Any UnoRoadmapControl::queryAggregation(css::uno::Type const & aType) {
349 auto ret = UnoControlRoadmap_Base::queryAggregation(aType);
350 if (!ret.hasValue()) {
351 ret = UnoControlRoadmap_IBase::queryInterface(aType);
353 return ret;
357 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel)
359 Reference< XContainer > xC( getModel(), UNO_QUERY );
360 if ( xC.is() )
361 xC->removeContainerListener( this );
363 bool bReturn = UnoControlBase::setModel( _rModel );
365 xC.set(getModel(), css::uno::UNO_QUERY);
366 if ( xC.is() )
367 xC->addContainerListener( this );
369 return bReturn;
373 OUString UnoRoadmapControl::GetComponentServiceName() const
375 return u"Roadmap"_ustr;
379 void UnoRoadmapControl::dispose()
381 EventObject aEvt;
382 aEvt.Source = getXWeak();
383 maItemListeners.disposeAndClear( aEvt );
384 UnoControl::dispose();
388 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )
390 Reference< XInterface > xRoadmapItem;
391 rEvent.Element >>= xRoadmapItem;
392 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
393 if ( xRoadmapPropertySet.is() )
394 xRoadmapPropertySet->addPropertyChangeListener( OUString(), this );
396 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
397 if ( xPeer.is() )
399 xPeer->elementInserted( rEvent );
400 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
401 if ( xPropertySet.is() )
402 xPropertySet->addPropertyChangeListener( OUString(), this );
407 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )
409 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
410 if ( xPeer.is() )
411 xPeer->elementRemoved( rEvent );
412 Reference< XInterface > xRoadmapItem;
413 rEvent.Element >>= xRoadmapItem;
414 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
415 if ( xPropertySet.is() )
416 xPropertySet->removePropertyChangeListener( OUString(), this );
420 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )
422 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
423 if ( xPeer.is() )
424 xPeer->elementReplaced( rEvent );
428 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent )
430 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
431 Reference< XControlModel > xModel = getModel( );
432 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
433 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), Any(CurItemIndex) );
434 if ( maItemListeners.getLength() )
435 maItemListeners.itemStateChanged( rEvent );
439 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l )
441 maItemListeners.addInterface( l );
442 if( getPeer().is() && maItemListeners.getLength() == 1 )
444 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
445 xRoadmap->addItemListener( this );
450 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l )
452 if( getPeer().is() && maItemListeners.getLength() == 1 )
454 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
455 xRoadmap->removeItemListener( this );
458 maItemListeners.removeInterface( l );
462 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt )
464 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
465 if ( xPeer.is() )
466 xPeer->propertyChange( evt );
469 OUString UnoRoadmapControl::getImplementationName()
471 return u"stardiv.Toolkit.UnoRoadmapControl"_ustr;
474 css::uno::Sequence<OUString> UnoRoadmapControl::getSupportedServiceNames()
476 auto s(UnoControlBase::getSupportedServiceNames());
477 s.realloc(s.getLength() + 2);
478 auto ps = s.getArray();
479 ps[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
480 ps[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
481 return s;
486 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
487 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
488 css::uno::XComponentContext *context,
489 css::uno::Sequence<css::uno::Any> const &)
491 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
494 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
495 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
496 css::uno::XComponentContext *,
497 css::uno::Sequence<css::uno::Any> const &)
499 return cppu::acquire(new toolkit::UnoRoadmapControl());
502 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */