cURL: follow redirects
[LibreOffice.git] / toolkit / source / controls / roadmapcontrol.cxx
blobc6359a1082724f40e1ff69892e7da25b590fb976
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 <toolkit/controls/roadmapcontrol.hxx>
22 #include <toolkit/helper/property.hxx>
23 #include <com/sun/star/awt/XVclWindowPeer.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <osl/diagnose.h>
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
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() throw(RuntimeException, std::exception)
81 return OUString::createFromAscii( szServiceName_UnoControlRoadmapModel );
84 OUString UnoControlRoadmapModel::getImplementationName()
85 throw (css::uno::RuntimeException, std::exception)
87 return OUString("stardiv.Toolkit.UnoControlRoadmapModel");
90 css::uno::Sequence<OUString>
91 UnoControlRoadmapModel::getSupportedServiceNames()
92 throw (css::uno::RuntimeException, std::exception)
94 auto s(UnoControlRoadmapModel_Base::getSupportedServiceNames());
95 s.realloc(s.getLength() + 2);
96 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmapModel";
97 s[s.getLength() - 1] = "stardiv.vcl.controlmodel.Roadmap";
98 return s;
101 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
103 Any aReturn;
104 switch (nPropId)
106 case BASEPROPERTY_COMPLETE:
107 aReturn <<= true;
108 break;
109 case BASEPROPERTY_ACTIVATED:
110 aReturn <<= true;
111 break;
112 case BASEPROPERTY_CURRENTITEMID:
113 aReturn <<= (sal_Int16) -1;
114 break;
115 case BASEPROPERTY_TEXT:
116 break;
117 case BASEPROPERTY_BORDER:
118 aReturn <<= (sal_Int16) 2; // No Border
119 break;
120 case BASEPROPERTY_DEFAULTCONTROL:
121 aReturn <<= OUString::createFromAscii( szServiceName_UnoControlRoadmap );
122 break;
123 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break;
126 return aReturn;
130 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( ) throw (Exception, css::uno::RuntimeException, std::exception)
132 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
133 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem);
134 return xNewRoadmapItem;
138 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ ) throw (Exception, RuntimeException, std::exception)
140 // Todo: implementation of the arguments handling
141 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
142 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem);
143 return xNewRoadmapItem;
147 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
150 css::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const css::uno::Type & rType ) throw(css::uno::RuntimeException, std::exception)
152 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
153 if ( !aRet.hasValue() )
154 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
155 return aRet;
159 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
161 static UnoPropertyArrayHelper* pHelper = nullptr;
162 if ( !pHelper )
164 Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
165 pHelper = new UnoPropertyArrayHelper( aIDs );
167 return *pHelper;
171 // beans::XMultiPropertySet
173 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( ) throw(RuntimeException, std::exception)
175 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
176 return xInfo;
180 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount() throw(RuntimeException, std::exception)
182 return maRoadmapItems.size();
185 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index ) throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
187 if (( Index >= (sal_Int32)maRoadmapItems.size()) || (Index < 0))
188 lcl_throwIndexOutOfBoundsException( );
189 Any aAny;
190 aAny = makeAny( maRoadmapItems.at( Index ));
191 return aAny;
195 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, const Reference< XInterface >& xRoadmapItem )
197 if ((Index > (sal_Int32)maRoadmapItems.size()) || ( Index < 0 ) )
198 lcl_throwIndexOutOfBoundsException( );
199 if ( !xRoadmapItem.is() )
200 lcl_throwIllegalArgumentException();
201 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
202 bool bIsRoadmapItem = xServiceInfo->supportsService("com.sun.star.awt.RoadmapItem");
203 if ( !bIsRoadmapItem )
204 lcl_throwIllegalArgumentException();
208 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const sal_Int32 , const Reference< XInterface >& xRoadmapItem)
210 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
211 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
212 if ( xProps.is() )
214 sal_Int32 LocID = 0;
215 Any aValue = xPropertySet->getPropertyValue("ID");
216 aValue >>= LocID;
217 if (LocID < 0) // index may not be smaller than zero
219 xPropertySet->setPropertyValue("ID", Any(GetUniqueID()) );
225 // The performance of this method could certainly be improved.
226 // As long as only vectors with up to 10 elements are
227 // involved it should be sufficient
228 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
230 Any aAny;
231 bool bIncrement = true;
232 sal_Int32 CurID = 0;
233 sal_Int32 n_CurItemID = 0;
234 Reference< XInterface > CurRoadmapItem;
235 while ( bIncrement )
237 bIncrement = false;
238 for ( RoadmapItemHolderList::iterator i = maRoadmapItems.begin(); i < maRoadmapItems.end(); ++i )
240 CurRoadmapItem = *i;
241 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
242 aAny = xPropertySet->getPropertyValue("ID");
243 aAny >>= n_CurItemID;
244 if (n_CurItemID == CurID)
246 bIncrement = true;
247 CurID++;
248 break;
252 return CurID;
256 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, const Reference< XInterface >& xRoadmapItem)
258 ContainerEvent aEvent;
259 aEvent.Source = *this;
260 aEvent.Element <<= xRoadmapItem;
261 aEvent.Accessor = makeAny(Index);
262 return aEvent;
266 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( const Reference< XPropertySet >& xPropertySet )
268 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
269 sal_Int16 n_CurrentItemID = 0;
270 aAny >>= n_CurrentItemID;
271 return n_CurrentItemID;
275 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& Element)
276 throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
278 if ( ( Index >= ( (sal_Int32)maRoadmapItems.size() + 1 ) ) || (Index < 0))
279 lcl_throwIndexOutOfBoundsException( );
280 Reference< XInterface > xRoadmapItem;
281 Element >>= xRoadmapItem;
282 MakeRMItemValidation( Index, xRoadmapItem);
283 SetRMItemDefaultProperties( Index, xRoadmapItem );
284 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
285 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
286 maContainerListeners.elementInserted( aEvent );
287 Reference< XPropertySet > xPropertySet( static_cast<XAggregation*>(static_cast<cppu::OWeakAggObject*>(this)), UNO_QUERY );
288 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
289 if ( Index <= n_CurrentItemID )
291 Any aAny(( sal_Int16 ) ( n_CurrentItemID + 1 ) );
292 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
297 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
298 throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
300 if (( Index > (sal_Int32)maRoadmapItems.size()) || (Index < 0))
301 lcl_throwIndexOutOfBoundsException( );
302 Reference< XInterface > xRoadmapItem;
303 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
304 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
305 maContainerListeners.elementRemoved( aEvent );
306 Reference< XPropertySet > xPropertySet( static_cast<XAggregation*>(static_cast<cppu::OWeakAggObject*>(this)), UNO_QUERY );
307 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
308 Any aAny;
309 if ( Index <= n_CurrentItemID )
311 if ( n_CurrentItemID >= (sal_Int32)maRoadmapItems.size() )
313 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
314 maRoadmapItems.size()-1);
315 if ( n_CurrentItemID < 0 )
316 return;
317 aAny <<= n_CurrentItemID;
319 else if (Index == n_CurrentItemID)
320 aAny <<= ( sal_Int16 ) -1;
321 else if( Index < n_CurrentItemID)
322 aAny <<= ( sal_Int16 ) ( n_CurrentItemID - 1 );
323 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
328 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& Element)
329 throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
331 Reference< XInterface > xRoadmapItem;
332 Element >>= xRoadmapItem;
333 MakeRMItemValidation( Index, xRoadmapItem);
334 SetRMItemDefaultProperties( Index, xRoadmapItem );
335 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
336 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
337 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
338 maContainerListeners.elementReplaced( aEvent );
342 Type SAL_CALL UnoControlRoadmapModel::getElementType() throw(RuntimeException, std::exception)
344 Type aType = cppu::UnoType<XPropertySet>::get();
345 return aType;
349 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements() throw(RuntimeException, std::exception)
351 return !maRoadmapItems.empty();
355 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener ) throw (css::uno::RuntimeException, std::exception)
357 maContainerListeners.addInterface( xListener );
360 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener ) throw (css::uno::RuntimeException, std::exception)
362 maContainerListeners.removeInterface( xListener );
366 // = UnoRoadmapControl
369 UnoRoadmapControl::UnoRoadmapControl()
370 :UnoControlRoadmap_Base()
371 ,maItemListeners( *this )
375 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
376 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
379 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel) throw ( RuntimeException, std::exception )
383 Reference< XContainer > xC( getModel(), UNO_QUERY );
384 if ( xC.is() )
385 xC->removeContainerListener( this );
387 bool bReturn = UnoControlBase::setModel( _rModel );
389 xC.set(getModel(), css::uno::UNO_QUERY);
390 if ( xC.is() )
391 xC->addContainerListener( this );
393 return bReturn;
397 OUString UnoRoadmapControl::GetComponentServiceName()
399 return OUString("Roadmap");
403 void UnoRoadmapControl::dispose() throw(RuntimeException, std::exception)
405 EventObject aEvt;
406 aEvt.Source = static_cast<cppu::OWeakObject*>(this);
407 maItemListeners.disposeAndClear( aEvt );
408 UnoControl::dispose();
412 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )throw(RuntimeException, std::exception)
414 Reference< XInterface > xRoadmapItem;
415 rEvent.Element >>= xRoadmapItem;
416 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
417 if ( xRoadmapPropertySet.is() )
418 xRoadmapPropertySet->addPropertyChangeListener( OUString(), this );
420 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
421 if ( xPeer.is() )
423 xPeer->elementInserted( rEvent );
424 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
425 if ( xPropertySet.is() )
426 xPropertySet->addPropertyChangeListener( OUString(), this );
431 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )throw(RuntimeException, std::exception)
433 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
434 if ( xPeer.is() )
435 xPeer->elementRemoved( rEvent );
436 Reference< XInterface > xRoadmapItem;
437 rEvent.Element >>= xRoadmapItem;
438 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
439 if ( xPropertySet.is() )
440 xPropertySet->removePropertyChangeListener( OUString(), this );
444 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )throw(RuntimeException, std::exception)
446 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
447 if ( xPeer.is() )
448 xPeer->elementReplaced( rEvent );
452 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent ) throw (RuntimeException, std::exception)
454 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
455 Reference< XControlModel > xModel( getModel( ), UNO_QUERY );
456 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
457 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), Any(CurItemIndex) );
458 if ( maItemListeners.getLength() )
459 maItemListeners.itemStateChanged( rEvent );
463 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l ) throw (RuntimeException, std::exception)
465 maItemListeners.addInterface( l );
466 if( getPeer().is() && maItemListeners.getLength() == 1 )
468 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
469 xRoadmap->addItemListener( this );
474 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l ) throw (RuntimeException, std::exception)
476 if( getPeer().is() && maItemListeners.getLength() == 1 )
478 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
479 xRoadmap->removeItemListener( this );
482 maItemListeners.removeInterface( l );
486 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException, std::exception)
488 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
489 if ( xPeer.is() )
490 xPeer->propertyChange( evt );
493 OUString UnoRoadmapControl::getImplementationName()
494 throw (css::uno::RuntimeException, std::exception)
496 return OUString("stardiv.Toolkit.UnoRoadmapControl");
499 css::uno::Sequence<OUString> UnoRoadmapControl::getSupportedServiceNames()
500 throw (css::uno::RuntimeException, std::exception)
502 auto s(UnoControlBase::getSupportedServiceNames());
503 s.realloc(s.getLength() + 2);
504 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
505 s[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
506 return s;
511 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
512 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
513 css::uno::XComponentContext *context,
514 css::uno::Sequence<css::uno::Any> const &)
516 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
519 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
520 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
521 css::uno::XComponentContext *,
522 css::uno::Sequence<css::uno::Any> const &)
524 return cppu::acquire(new toolkit::UnoRoadmapControl());
527 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */