Branch libreoffice-5-0-4
[LibreOffice.git] / toolkit / source / controls / roadmapcontrol.cxx
blobb63b72bf68cde17ed21ceacde0c1bfc3ca3c74dc
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( 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, ::com::sun::star::uno::RuntimeException, std::exception)
132 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
133 Reference< XInterface > xNewRoadmapItem = (::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 = (::cppu::OWeakObject*)pRoadmapItem;
143 return xNewRoadmapItem;
147 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
151 ::com::sun::star::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException, std::exception)
153 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
154 if ( !aRet.hasValue() )
155 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
156 return aRet;
161 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
163 static UnoPropertyArrayHelper* pHelper = NULL;
164 if ( !pHelper )
166 Sequence<sal_Int32> aIDs = ImplGetPropertyIds();
167 pHelper = new UnoPropertyArrayHelper( aIDs );
169 return *pHelper;
173 // beans::XMultiPropertySet
175 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( ) throw(RuntimeException, std::exception)
177 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
178 return xInfo;
182 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount() throw(RuntimeException, std::exception)
184 return maRoadmapItems.size();
187 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index ) throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
189 if (( Index >= (sal_Int32)maRoadmapItems.size()) || (Index < 0))
190 lcl_throwIndexOutOfBoundsException( );
191 Any aAny;
192 aAny = makeAny( maRoadmapItems.at( Index ));
193 return aAny;
198 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, Reference< XInterface > xRoadmapItem )
200 if ((Index > (sal_Int32)maRoadmapItems.size()) || ( Index < 0 ) )
201 lcl_throwIndexOutOfBoundsException( );
202 if ( !xRoadmapItem.is() )
203 lcl_throwIllegalArgumentException();
204 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
205 bool bIsRoadmapItem = xServiceInfo->supportsService("com.sun.star.awt.RoadmapItem");
206 if ( !bIsRoadmapItem )
207 lcl_throwIllegalArgumentException();
211 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const sal_Int32 , Reference< XInterface > xRoadmapItem)
213 Any aAny;
214 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
215 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
216 if ( xProps.is() )
218 sal_Int32 LocID = 0;
219 Any aValue = xPropertySet->getPropertyValue("ID");
220 aValue >>= LocID;
221 if (LocID < 0) // index may not be smaller than zero
223 aAny <<= GetUniqueID();
224 xPropertySet->setPropertyValue("ID", aAny );
230 // The performance of this method could certainly be improved.
231 // As long as only vectors with up to 10 elements are
232 // involved it should be sufficient
233 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
235 Any aAny;
236 bool bIncrement = true;
237 sal_Int32 CurID = 0;
238 sal_Int32 n_CurItemID = 0;
239 Reference< XInterface > CurRoadmapItem;
240 while ( bIncrement )
242 bIncrement = false;
243 for ( RoadmapItemHolderList::iterator i = maRoadmapItems.begin(); i < maRoadmapItems.end(); ++i )
245 CurRoadmapItem = *i;
246 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
247 aAny = xPropertySet->getPropertyValue("ID");
248 aAny >>= n_CurItemID;
249 if (n_CurItemID == CurID)
251 bIncrement = true;
252 CurID++;
253 break;
257 return CurID;
261 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, Reference< XInterface > xRoadmapItem)
263 ContainerEvent aEvent;
264 aEvent.Source = *this;
265 aEvent.Element <<= xRoadmapItem;
266 aEvent.Accessor = makeAny(Index);
267 return aEvent;
271 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( Reference< XPropertySet > xPropertySet )
273 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
274 sal_Int16 n_CurrentItemID = 0;
275 aAny >>= n_CurrentItemID;
276 return n_CurrentItemID;
280 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& _Element)
281 throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
283 if ( ( Index >= ( (sal_Int32)maRoadmapItems.size() + 1 ) ) || (Index < 0))
284 lcl_throwIndexOutOfBoundsException( );
285 Reference< XInterface > xRoadmapItem;
286 _Element >>= xRoadmapItem;
287 MakeRMItemValidation( Index, xRoadmapItem);
288 SetRMItemDefaultProperties( Index, xRoadmapItem );
289 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
290 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
291 maContainerListeners.elementInserted( aEvent );
292 Reference< XPropertySet > xPropertySet( (XAggregation*) (::cppu::OWeakAggObject*)this, UNO_QUERY );
293 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
294 if ( Index <= n_CurrentItemID )
296 Any aAny;
297 aAny <<= ( sal_Int16 ) ( n_CurrentItemID + 1 );
298 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
304 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
305 throw (IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
307 if (( Index > (sal_Int32)maRoadmapItems.size()) || (Index < 0))
308 lcl_throwIndexOutOfBoundsException( );
309 Reference< XInterface > xRoadmapItem;
310 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
311 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
312 maContainerListeners.elementRemoved( aEvent );
313 Reference< XPropertySet > xPropertySet( (XAggregation*) (::cppu::OWeakAggObject*)this, UNO_QUERY );
314 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
315 Any aAny;
316 if ( Index <= n_CurrentItemID )
318 if ( n_CurrentItemID >= (sal_Int32)maRoadmapItems.size() )
320 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
321 maRoadmapItems.size()-1);
322 if ( n_CurrentItemID < 0 )
323 return;
324 aAny <<= n_CurrentItemID;
326 else if (Index == n_CurrentItemID)
327 aAny <<= ( sal_Int16 ) -1;
328 else if( Index < n_CurrentItemID)
329 aAny <<= ( sal_Int16 ) ( n_CurrentItemID - 1 );
330 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
335 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& _Element)
336 throw (IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
338 Reference< XInterface > xRoadmapItem;
339 _Element >>= xRoadmapItem;
340 MakeRMItemValidation( Index, xRoadmapItem);
341 SetRMItemDefaultProperties( Index, xRoadmapItem );
342 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
343 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
344 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
345 maContainerListeners.elementReplaced( aEvent );
349 Type SAL_CALL UnoControlRoadmapModel::getElementType() throw(RuntimeException, std::exception)
351 Type aType = cppu::UnoType<XPropertySet>::get();
352 return aType;
356 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements() throw(RuntimeException, std::exception)
358 return !maRoadmapItems.empty();
362 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw (::com::sun::star::uno::RuntimeException, std::exception)
364 maContainerListeners.addInterface( xListener );
367 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) throw (::com::sun::star::uno::RuntimeException, std::exception)
369 maContainerListeners.removeInterface( xListener );
373 // = UnoRoadmapControl
376 UnoRoadmapControl::UnoRoadmapControl()
377 :UnoControlRoadmap_Base()
378 ,maItemListeners( *this )
382 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
383 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
386 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel) throw ( RuntimeException, std::exception )
390 Reference< XContainer > xC( getModel(), UNO_QUERY );
391 if ( xC.is() )
392 xC->removeContainerListener( this );
394 bool bReturn = UnoControlBase::setModel( _rModel );
396 xC.set(getModel(), css::uno::UNO_QUERY);
397 if ( xC.is() )
398 xC->addContainerListener( this );
400 return bReturn;
405 OUString UnoRoadmapControl::GetComponentServiceName()
407 return OUString("Roadmap");
412 void UnoRoadmapControl::dispose() throw(RuntimeException, std::exception)
414 EventObject aEvt;
415 aEvt.Source = (::cppu::OWeakObject*)this;
416 maItemListeners.disposeAndClear( aEvt );
417 UnoControl::dispose();
422 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )throw(RuntimeException, std::exception)
424 Reference< XInterface > xRoadmapItem;
425 rEvent.Element >>= xRoadmapItem;
426 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
427 if ( xRoadmapPropertySet.is() )
428 xRoadmapPropertySet->addPropertyChangeListener( OUString(), this );
430 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
431 if ( xPeer.is() )
433 xPeer->elementInserted( rEvent );
434 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
435 if ( xPropertySet.is() )
436 xPropertySet->addPropertyChangeListener( OUString(), this );
441 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )throw(RuntimeException, std::exception)
443 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
444 if ( xPeer.is() )
445 xPeer->elementRemoved( rEvent );
446 Reference< XInterface > xRoadmapItem;
447 rEvent.Element >>= xRoadmapItem;
448 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
449 if ( xPropertySet.is() )
450 xPropertySet->removePropertyChangeListener( OUString(), this );
454 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )throw(RuntimeException, std::exception)
456 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
457 if ( xPeer.is() )
458 xPeer->elementReplaced( rEvent );
462 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent ) throw (RuntimeException, std::exception)
464 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
465 Any aAny;
466 aAny <<= CurItemIndex;
467 Reference< XControlModel > xModel( getModel( ), UNO_QUERY );
468 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
469 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
470 if ( maItemListeners.getLength() )
471 maItemListeners.itemStateChanged( rEvent );
475 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l ) throw (RuntimeException, std::exception)
477 maItemListeners.addInterface( l );
478 if( getPeer().is() && maItemListeners.getLength() == 1 )
480 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
481 xRoadmap->addItemListener( this );
486 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l ) throw (RuntimeException, std::exception)
488 if( getPeer().is() && maItemListeners.getLength() == 1 )
490 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
491 xRoadmap->removeItemListener( this );
494 maItemListeners.removeInterface( l );
498 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt ) throw (RuntimeException, std::exception)
500 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
501 if ( xPeer.is() )
502 xPeer->propertyChange( evt );
505 OUString UnoRoadmapControl::getImplementationName()
506 throw (css::uno::RuntimeException, std::exception)
508 return OUString("stardiv.Toolkit.UnoRoadmapControl");
511 css::uno::Sequence<OUString> UnoRoadmapControl::getSupportedServiceNames()
512 throw (css::uno::RuntimeException, std::exception)
514 auto s(UnoControlBase::getSupportedServiceNames());
515 s.realloc(s.getLength() + 2);
516 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
517 s[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
518 return s;
523 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
524 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
525 css::uno::XComponentContext *context,
526 css::uno::Sequence<css::uno::Any> const &)
528 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
531 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
532 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
533 css::uno::XComponentContext *,
534 css::uno::Sequence<css::uno::Any> const &)
536 return cppu::acquire(new toolkit::UnoRoadmapControl());
539 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */