nss: upgrade to release 3.73
[LibreOffice.git] / toolkit / source / controls / roadmapcontrol.cxx
blob0a0006014981061123a3905a41493bf59f22a16f
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 <toolkit/helper/property.hxx>
24 #include <helper/servicenames.hxx>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <helper/unopropertyarrayhelper.hxx>
30 namespace toolkit
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::awt;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::container;
41 // helper
44 static void lcl_throwIllegalArgumentException( )
45 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this...
46 throw IllegalArgumentException();
49 static void lcl_throwIndexOutOfBoundsException( )
50 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this...
51 throw IndexOutOfBoundsException();
55 // = UnoControlRoadmapModel
58 UnoControlRoadmapModel::UnoControlRoadmapModel( const Reference< XComponentContext >& i_factory )
59 :UnoControlRoadmapModel_Base( i_factory )
60 ,maContainerListeners( *this )
62 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR );
63 ImplRegisterProperty( BASEPROPERTY_BORDER );
64 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR );
65 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL );
66 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR );
67 ImplRegisterProperty( BASEPROPERTY_HELPTEXT );
68 ImplRegisterProperty( BASEPROPERTY_HELPURL );
69 ImplRegisterProperty( BASEPROPERTY_IMAGEURL );
70 ImplRegisterProperty( BASEPROPERTY_GRAPHIC );
71 ImplRegisterProperty( BASEPROPERTY_PRINTABLE );
72 ImplRegisterProperty( BASEPROPERTY_COMPLETE );
73 ImplRegisterProperty( BASEPROPERTY_ACTIVATED );
74 ImplRegisterProperty( BASEPROPERTY_CURRENTITEMID );
75 ImplRegisterProperty( BASEPROPERTY_TABSTOP );
76 ImplRegisterProperty( BASEPROPERTY_TEXT );
80 OUString UnoControlRoadmapModel::getServiceName()
82 return "stardiv.vcl.controlmodel.Roadmap";
85 OUString UnoControlRoadmapModel::getImplementationName()
87 return "stardiv.Toolkit.UnoControlRoadmapModel";
90 css::uno::Sequence<OUString>
91 UnoControlRoadmapModel::getSupportedServiceNames()
93 auto s(UnoControlRoadmapModel_Base::getSupportedServiceNames());
94 s.realloc(s.getLength() + 2);
95 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmapModel";
96 s[s.getLength() - 1] = "stardiv.vcl.controlmodel.Roadmap";
97 return s;
100 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const
102 Any aReturn;
103 switch (nPropId)
105 case BASEPROPERTY_COMPLETE:
106 aReturn <<= true;
107 break;
108 case BASEPROPERTY_ACTIVATED:
109 aReturn <<= true;
110 break;
111 case BASEPROPERTY_CURRENTITEMID:
112 aReturn <<= sal_Int16(-1);
113 break;
114 case BASEPROPERTY_TEXT:
115 break;
116 case BASEPROPERTY_BORDER:
117 aReturn <<= sal_Int16(2); // No Border
118 break;
119 case BASEPROPERTY_DEFAULTCONTROL:
120 aReturn <<= OUString( "stardiv.vcl.control.Roadmap" );
121 break;
122 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break;
125 return aReturn;
129 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( )
131 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
132 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem);
133 return xNewRoadmapItem;
137 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ )
139 // Todo: implementation of the arguments handling
140 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry();
141 Reference< XInterface > xNewRoadmapItem = static_cast<cppu::OWeakObject*>(pRoadmapItem);
142 return xNewRoadmapItem;
146 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase )
149 css::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const css::uno::Type & rType )
151 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType );
152 if ( !aRet.hasValue() )
153 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType );
154 return aRet;
158 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper()
160 static UnoPropertyArrayHelper aHelper( ImplGetPropertyIds() );
161 return aHelper;
165 // beans::XMultiPropertySet
167 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( )
169 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
170 return xInfo;
174 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount()
176 return maRoadmapItems.size();
179 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index )
181 if (( Index >= static_cast<sal_Int32>(maRoadmapItems.size())) || (Index < 0))
182 lcl_throwIndexOutOfBoundsException( );
183 Any aAny( maRoadmapItems.at( Index ) );
184 return aAny;
188 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, const Reference< XInterface >& xRoadmapItem )
190 if ((Index > static_cast<sal_Int32>(maRoadmapItems.size())) || ( Index < 0 ) )
191 lcl_throwIndexOutOfBoundsException( );
192 if ( !xRoadmapItem.is() )
193 lcl_throwIllegalArgumentException();
194 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY );
195 bool bIsRoadmapItem = xServiceInfo->supportsService("com.sun.star.awt.RoadmapItem");
196 if ( !bIsRoadmapItem )
197 lcl_throwIllegalArgumentException();
201 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const Reference< XInterface >& xRoadmapItem)
203 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
204 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY );
205 if ( xProps.is() )
207 sal_Int32 LocID = 0;
208 Any aValue = xPropertySet->getPropertyValue("ID");
209 aValue >>= LocID;
210 if (LocID < 0) // index may not be smaller than zero
212 xPropertySet->setPropertyValue("ID", Any(GetUniqueID()) );
218 // The performance of this method could certainly be improved.
219 // As long as only vectors with up to 10 elements are
220 // involved it should be sufficient
221 sal_Int32 UnoControlRoadmapModel::GetUniqueID()
223 Any aAny;
224 bool bIncrement = true;
225 sal_Int32 CurID = 0;
226 sal_Int32 n_CurItemID = 0;
227 Reference< XInterface > CurRoadmapItem;
228 while ( bIncrement )
230 bIncrement = false;
231 for ( const auto& rRoadmapItem : maRoadmapItems )
233 CurRoadmapItem = rRoadmapItem;
234 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY );
235 aAny = xPropertySet->getPropertyValue("ID");
236 aAny >>= n_CurItemID;
237 if (n_CurItemID == CurID)
239 bIncrement = true;
240 CurID++;
241 break;
245 return CurID;
249 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, const Reference< XInterface >& xRoadmapItem)
251 ContainerEvent aEvent;
252 aEvent.Source = *this;
253 aEvent.Element <<= xRoadmapItem;
254 aEvent.Accessor <<= Index;
255 return aEvent;
259 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( const Reference< XPropertySet >& xPropertySet )
261 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) );
262 sal_Int16 n_CurrentItemID = 0;
263 aAny >>= n_CurrentItemID;
264 return n_CurrentItemID;
268 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& Element)
270 if ( ( Index >= ( static_cast<sal_Int32>(maRoadmapItems.size()) + 1 ) ) || (Index < 0))
271 lcl_throwIndexOutOfBoundsException( );
272 Reference< XInterface > xRoadmapItem;
273 Element >>= xRoadmapItem;
274 MakeRMItemValidation( Index, xRoadmapItem);
275 SetRMItemDefaultProperties( xRoadmapItem );
276 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem);
277 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
278 maContainerListeners.elementInserted( aEvent );
279 Reference< XPropertySet > xPropertySet( static_cast<XAggregation*>(static_cast<cppu::OWeakAggObject*>(this)), UNO_QUERY );
280 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
281 if ( Index <= n_CurrentItemID )
283 Any aAny(static_cast<sal_Int16>( n_CurrentItemID + 1 ) );
284 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
289 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index)
291 if (( Index > static_cast<sal_Int32>(maRoadmapItems.size())) || (Index < 0))
292 lcl_throwIndexOutOfBoundsException( );
293 Reference< XInterface > xRoadmapItem;
294 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
295 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
296 maContainerListeners.elementRemoved( aEvent );
297 Reference< XPropertySet > xPropertySet( static_cast<XAggregation*>(static_cast<cppu::OWeakAggObject*>(this)), UNO_QUERY );
298 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet );
299 Any aAny;
300 if ( Index > n_CurrentItemID )
301 return;
303 if ( n_CurrentItemID >= static_cast<sal_Int32>(maRoadmapItems.size()) )
305 n_CurrentItemID = sal::static_int_cast< sal_Int16 >(
306 maRoadmapItems.size()-1);
307 if ( n_CurrentItemID < 0 )
308 return;
309 aAny <<= n_CurrentItemID;
311 else if (Index == n_CurrentItemID)
312 aAny <<= sal_Int16(-1);
313 else if( Index < n_CurrentItemID)
314 aAny <<= static_cast<sal_Int16>( n_CurrentItemID - 1 );
315 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny );
319 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& Element)
321 Reference< XInterface > xRoadmapItem;
322 Element >>= xRoadmapItem;
323 MakeRMItemValidation( Index, xRoadmapItem);
324 SetRMItemDefaultProperties( xRoadmapItem );
325 maRoadmapItems.erase( maRoadmapItems.begin() + Index );
326 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem );
327 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem);
328 maContainerListeners.elementReplaced( aEvent );
332 Type SAL_CALL UnoControlRoadmapModel::getElementType()
334 Type aType = cppu::UnoType<XPropertySet>::get();
335 return aType;
339 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements()
341 return !maRoadmapItems.empty();
345 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
347 maContainerListeners.addInterface( xListener );
350 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const css::uno::Reference< css::container::XContainerListener >& xListener )
352 maContainerListeners.removeInterface( xListener );
356 // = UnoRoadmapControl
359 UnoRoadmapControl::UnoRoadmapControl()
360 :UnoControlRoadmap_Base()
361 ,maItemListeners( *this )
365 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
366 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase )
369 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel)
371 Reference< XContainer > xC( getModel(), UNO_QUERY );
372 if ( xC.is() )
373 xC->removeContainerListener( this );
375 bool bReturn = UnoControlBase::setModel( _rModel );
377 xC.set(getModel(), css::uno::UNO_QUERY);
378 if ( xC.is() )
379 xC->addContainerListener( this );
381 return bReturn;
385 OUString UnoRoadmapControl::GetComponentServiceName()
387 return "Roadmap";
391 void UnoRoadmapControl::dispose()
393 EventObject aEvt;
394 aEvt.Source = static_cast<cppu::OWeakObject*>(this);
395 maItemListeners.disposeAndClear( aEvt );
396 UnoControl::dispose();
400 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent )
402 Reference< XInterface > xRoadmapItem;
403 rEvent.Element >>= xRoadmapItem;
404 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY );
405 if ( xRoadmapPropertySet.is() )
406 xRoadmapPropertySet->addPropertyChangeListener( OUString(), this );
408 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
409 if ( xPeer.is() )
411 xPeer->elementInserted( rEvent );
412 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY );
413 if ( xPropertySet.is() )
414 xPropertySet->addPropertyChangeListener( OUString(), this );
419 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent )
421 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
422 if ( xPeer.is() )
423 xPeer->elementRemoved( rEvent );
424 Reference< XInterface > xRoadmapItem;
425 rEvent.Element >>= xRoadmapItem;
426 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY );
427 if ( xPropertySet.is() )
428 xPropertySet->removePropertyChangeListener( OUString(), this );
432 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent )
434 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY);
435 if ( xPeer.is() )
436 xPeer->elementReplaced( rEvent );
440 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent )
442 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId);
443 Reference< XControlModel > xModel = getModel( );
444 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY );
445 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), Any(CurItemIndex) );
446 if ( maItemListeners.getLength() )
447 maItemListeners.itemStateChanged( rEvent );
451 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l )
453 maItemListeners.addInterface( l );
454 if( getPeer().is() && maItemListeners.getLength() == 1 )
456 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
457 xRoadmap->addItemListener( this );
462 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l )
464 if( getPeer().is() && maItemListeners.getLength() == 1 )
466 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY );
467 xRoadmap->removeItemListener( this );
470 maItemListeners.removeInterface( l );
474 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt )
476 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY);
477 if ( xPeer.is() )
478 xPeer->propertyChange( evt );
481 OUString UnoRoadmapControl::getImplementationName()
483 return "stardiv.Toolkit.UnoRoadmapControl";
486 css::uno::Sequence<OUString> UnoRoadmapControl::getSupportedServiceNames()
488 auto s(UnoControlBase::getSupportedServiceNames());
489 s.realloc(s.getLength() + 2);
490 s[s.getLength() - 2] = "com.sun.star.awt.UnoControlRoadmap";
491 s[s.getLength() - 1] = "stardiv.vcl.control.Roadmap";
492 return s;
497 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
498 stardiv_Toolkit_UnoControlRoadmapModel_get_implementation(
499 css::uno::XComponentContext *context,
500 css::uno::Sequence<css::uno::Any> const &)
502 return cppu::acquire(new toolkit::UnoControlRoadmapModel(context));
505 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
506 stardiv_Toolkit_UnoRoadmapControl_get_implementation(
507 css::uno::XComponentContext *,
508 css::uno::Sequence<css::uno::Any> const &)
510 return cppu::acquire(new toolkit::UnoRoadmapControl());
513 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */