Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / toolkit / source / controls / roadmapcontrol.cxx
blob48e7902b2a932fb05303e57208e798e1b0959900
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/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
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 OUString::createFromAscii( szServiceName_UnoControlRoadmapModel );
84 OUString UnoControlRoadmapModel::getImplementationName()
86 return OUString("stardiv.Toolkit.UnoControlRoadmapModel");
89 css::uno::Sequence<OUString>
90 UnoControlRoadmapModel::getSupportedServiceNames()
92 auto s(UnoControlRoadmapModel_Base::getSupportedServiceNames());
93 s.realloc(s.getLength() + 2);
94 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmapModel";
95 s[s.getLength() - 1] = "stardiv.vcl.controlmodel.Roadmap";
96 return s;
99 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
101 Any aReturn;
102 switch (nPropId)
104 case BASEPROPERTY_COMPLETE:
105 aReturn <<= true;
106 break;
107 case BASEPROPERTY_ACTIVATED:
108 aReturn <<= true;
109 break;
110 case BASEPROPERTY_CURRENTITEMID:
111 aReturn <<= sal_Int16(-1);
112 break;
113 case BASEPROPERTY_TEXT:
114 break;
115 case BASEPROPERTY_BORDER:
116 aReturn <<= sal_Int16(2); // No Border
117 break;
118 case BASEPROPERTY_DEFAULTCONTROL:
119 aReturn <<= OUString::createFromAscii( szServiceName_UnoControlRoadmap );
120 break;
121 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break;
124 return aReturn;
128 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( )
130 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
131 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem);
132 return xNewRoadmapItem;
136 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ )
138 // Todo: implementation of the arguments handling
139 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
140 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem);
141 return xNewRoadmapItem;
145 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
148 css::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const css::uno::Type & rType )
150 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
151 if ( !aRet.hasValue() )
152 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
153 return aRet;
157 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
159 static UnoPropertyArrayHelper* pHelper = nullptr;
160 if ( !pHelper )
162 Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
163 pHelper = new UnoPropertyArrayHelper( aIDs );
165 return *pHelper;
169 // beans::XMultiPropertySet
171 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( )
173 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
174 return xInfo;
178 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount()
180 return maRoadmapItems.size();
183 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index )
185 if (( Index >= static_cast<sal_Int32>(maRoadmapItems.size())) || (Index < 0))
186 lcl_throwIndexOutOfBoundsException( );
187 Any aAny( maRoadmapItems.at( Index ) );
188 return aAny;
192 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, const Reference< XInterface >& xRoadmapItem )
194 if ((Index > static_cast<sal_Int32>(maRoadmapItems.size())) || ( Index < 0 ) )
195 lcl_throwIndexOutOfBoundsException( );
196 if ( !xRoadmapItem.is() )
197 lcl_throwIllegalArgumentException();
198 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
199 bool bIsRoadmapItem = xServiceInfo->supportsService("com.sun.star.awt.RoadmapItem");
200 if ( !bIsRoadmapItem )
201 lcl_throwIllegalArgumentException();
205 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const Reference< XInterface >& xRoadmapItem)
207 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
208 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
209 if ( xProps.is() )
211 sal_Int32 LocID = 0;
212 Any aValue = xPropertySet->getPropertyValue("ID");
213 aValue >>= LocID;
214 if (LocID < 0) // index may not be smaller than zero
216 xPropertySet->setPropertyValue("ID", Any(GetUniqueID()) );
222 // The performance of this method could certainly be improved.
223 // As long as only vectors with up to 10 elements are
224 // involved it should be sufficient
225 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
227 Any aAny;
228 bool bIncrement = true;
229 sal_Int32 CurID = 0;
230 sal_Int32 n_CurItemID = 0;
231 Reference< XInterface > CurRoadmapItem;
232 while ( bIncrement )
234 bIncrement = false;
235 for ( RoadmapItemHolderList::iterator i = maRoadmapItems.begin(); i < maRoadmapItems.end(); ++i )
237 CurRoadmapItem = *i;
238 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
239 aAny = xPropertySet->getPropertyValue("ID");
240 aAny >>= n_CurItemID;
241 if (n_CurItemID == CurID)
243 bIncrement = true;
244 CurID++;
245 break;
249 return CurID;
253 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, const Reference< XInterface >& xRoadmapItem)
255 ContainerEvent aEvent;
256 aEvent.Source = *this;
257 aEvent.Element <<= xRoadmapItem;
258 aEvent.Accessor <<= Index;
259 return aEvent;
263 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( const Reference< XPropertySet >& xPropertySet )
265 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
266 sal_Int16 n_CurrentItemID = 0;
267 aAny >>= n_CurrentItemID;
268 return n_CurrentItemID;
272 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& Element)
274 if ( ( Index >= ( static_cast<sal_Int32>(maRoadmapItems.size()) + 1 ) ) || (Index < 0))
275 lcl_throwIndexOutOfBoundsException( );
276 Reference< XInterface > xRoadmapItem;
277 Element >>= xRoadmapItem;
278 MakeRMItemValidation( Index, xRoadmapItem);
279 SetRMItemDefaultProperties( xRoadmapItem );
280 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
281 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
282 maContainerListeners.elementInserted( aEvent );
283 Reference< XPropertySet > xPropertySet( static_cast<XAggregation*>(static_cast<cppu::OWeakAggObject*>(this)), UNO_QUERY );
284 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
285 if ( Index <= n_CurrentItemID )
287 Any aAny(static_cast<sal_Int16>( n_CurrentItemID + 1 ) );
288 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
293 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
295 if (( Index > static_cast<sal_Int32>(maRoadmapItems.size())) || (Index < 0))
296 lcl_throwIndexOutOfBoundsException( );
297 Reference< XInterface > xRoadmapItem;
298 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
299 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
300 maContainerListeners.elementRemoved( aEvent );
301 Reference< XPropertySet > xPropertySet( static_cast<XAggregation*>(static_cast<cppu::OWeakAggObject*>(this)), UNO_QUERY );
302 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
303 Any aAny;
304 if ( Index <= n_CurrentItemID )
306 if ( n_CurrentItemID >= static_cast<sal_Int32>(maRoadmapItems.size()) )
308 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
309 maRoadmapItems.size()-1);
310 if ( n_CurrentItemID < 0 )
311 return;
312 aAny <<= n_CurrentItemID;
314 else if (Index == n_CurrentItemID)
315 aAny <<= sal_Int16(-1);
316 else if( Index < n_CurrentItemID)
317 aAny <<= static_cast<sal_Int16>( n_CurrentItemID - 1 );
318 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
323 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& Element)
325 Reference< XInterface > xRoadmapItem;
326 Element >>= xRoadmapItem;
327 MakeRMItemValidation( Index, xRoadmapItem);
328 SetRMItemDefaultProperties( xRoadmapItem );
329 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
330 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
331 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
332 maContainerListeners.elementReplaced( aEvent );
336 Type SAL_CALL UnoControlRoadmapModel::getElementType()
338 Type aType = cppu::UnoType<XPropertySet>::get();
339 return aType;
343 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements()
345 return !maRoadmapItems.empty();
349 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
351 maContainerListeners.addInterface( xListener );
354 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
356 maContainerListeners.removeInterface( xListener );
360 // = UnoRoadmapControl
363 UnoRoadmapControl::UnoRoadmapControl()
364 :UnoControlRoadmap_Base()
365 ,maItemListeners( *this )
369 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
370 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
373 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel)
377 Reference< XContainer > xC( getModel(), UNO_QUERY );
378 if ( xC.is() )
379 xC->removeContainerListener( this );
381 bool bReturn = UnoControlBase::setModel( _rModel );
383 xC.set(getModel(), css::uno::UNO_QUERY);
384 if ( xC.is() )
385 xC->addContainerListener( this );
387 return bReturn;
391 OUString UnoRoadmapControl::GetComponentServiceName()
393 return OUString("Roadmap");
397 void UnoRoadmapControl::dispose()
399 EventObject aEvt;
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);
415 if ( xPeer.is() )
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);
428 if ( xPeer.is() )
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);
441 if ( xPeer.is() )
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( ), UNO_QUERY );
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);
483 if ( xPeer.is() )
484 xPeer->propertyChange( evt );
487 OUString UnoRoadmapControl::getImplementationName()
489 return OUString("stardiv.Toolkit.UnoRoadmapControl");
492 css::uno::Sequence<OUString> UnoRoadmapControl::getSupportedServiceNames()
494 auto s(UnoControlBase::getSupportedServiceNames());
495 s.realloc(s.getLength() + 2);
496 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
497 s[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
498 return s;
503 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
504 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
505 css::uno::XComponentContext *context,
506 css::uno::Sequence<css::uno::Any> const &)
508 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
511 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
512 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
513 css::uno::XComponentContext *,
514 css::uno::Sequence<css::uno::Any> const &)
516 return cppu::acquire(new toolkit::UnoRoadmapControl());
519 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */