1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_toolkit.hxx" 26 27 #ifndef _TOOLKIT_ROADMAP_CONTROL_HXX 28 #include <toolkit/controls/roadmapcontrol.hxx> 29 #endif 30 #include <toolkit/helper/unopropertyarrayhelper.hxx> 31 #include <toolkit/helper/property.hxx> 32 #include <com/sun/star/awt/XVclWindowPeer.hpp> 33 #include <comphelper/processfactory.hxx> 34 #include <osl/diagnose.h> 35 36 //........................................................................ 37 namespace toolkit 38 { 39 //........................................................................ 40 41 using namespace ::com::sun::star::uno; 42 using namespace ::com::sun::star::awt; 43 using namespace ::com::sun::star::lang; 44 using namespace ::com::sun::star::beans; 45 using namespace ::com::sun::star::container; 46 47 // ---------------------------------------------------- 48 // helper 49 // ---------------------------------------------------- 50 51 static void lcl_throwIllegalArgumentException( ) 52 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this .... 53 throw IllegalArgumentException(); 54 } 55 56 static void lcl_throwIndexOutOfBoundsException( ) 57 { // throwing is expensive (in terms of code size), thus we hope the compiler does not inline this .... 58 throw IndexOutOfBoundsException(); 59 } 60 61 // =================================================================== 62 // = UnoControlRoadmapModel 63 // =================================================================== 64 // ------------------------------------------------------------------- 65 UnoControlRoadmapModel::UnoControlRoadmapModel( const Reference< XMultiServiceFactory >& i_factory ) 66 :UnoControlRoadmapModel_Base( i_factory ) 67 ,maContainerListeners( *this ) 68 { 69 ImplRegisterProperty( BASEPROPERTY_BACKGROUNDCOLOR ); 70 ImplRegisterProperty( BASEPROPERTY_BORDER ); 71 ImplRegisterProperty( BASEPROPERTY_BORDERCOLOR ); 72 ImplRegisterProperty( BASEPROPERTY_DEFAULTCONTROL ); 73 ImplRegisterProperty( BASEPROPERTY_FONTDESCRIPTOR ); 74 ImplRegisterProperty( BASEPROPERTY_HELPTEXT ); 75 ImplRegisterProperty( BASEPROPERTY_HELPURL ); 76 ImplRegisterProperty( BASEPROPERTY_IMAGEURL ); 77 ImplRegisterProperty( BASEPROPERTY_GRAPHIC ); 78 ImplRegisterProperty( BASEPROPERTY_PRINTABLE ); 79 ImplRegisterProperty( BASEPROPERTY_COMPLETE ); 80 ImplRegisterProperty( BASEPROPERTY_ACTIVATED ); 81 ImplRegisterProperty( BASEPROPERTY_CURRENTITEMID ); 82 ImplRegisterProperty( BASEPROPERTY_TABSTOP ); 83 ImplRegisterProperty( BASEPROPERTY_TEXT ); 84 } 85 86 // ------------------------------------------------------------------- 87 ::rtl::OUString UnoControlRoadmapModel::getServiceName() 88 { 89 return ::rtl::OUString::createFromAscii( szServiceName_UnoControlRoadmapModel ); 90 } 91 92 93 // ------------------------------------------------------------------- 94 Any UnoControlRoadmapModel::ImplGetDefaultValue( sal_uInt16 nPropId ) const 95 { 96 Any aReturn; 97 switch (nPropId) 98 { 99 case BASEPROPERTY_COMPLETE: 100 aReturn <<= (sal_Bool) sal_True; 101 break; 102 case BASEPROPERTY_ACTIVATED: 103 aReturn <<= (sal_Bool) sal_True; 104 break; 105 case BASEPROPERTY_CURRENTITEMID: 106 aReturn <<= (sal_Int16) -1; 107 break; 108 case BASEPROPERTY_TEXT: 109 break; 110 case BASEPROPERTY_BORDER: 111 aReturn <<= (sal_Int16) 2; // No Border 112 break; 113 case BASEPROPERTY_DEFAULTCONTROL: 114 aReturn <<= ::rtl::OUString( ::rtl::OUString::createFromAscii( szServiceName_UnoControlRoadmap ) ); 115 break; 116 default : aReturn = UnoControlRoadmapModel_Base::ImplGetDefaultValue( nPropId ); break; 117 } 118 119 return aReturn; 120 } 121 122 123 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstance( ) 124 { 125 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry(); 126 Reference< XInterface > xNewRoadmapItem = (::cppu::OWeakObject*)pRoadmapItem; 127 return xNewRoadmapItem; 128 } 129 130 131 Reference< XInterface > SAL_CALL UnoControlRoadmapModel::createInstanceWithArguments( const Sequence< Any >& /*aArguments*/ ) 132 { 133 // Todo: implementation of the arguments handling 134 ORoadmapEntry* pRoadmapItem = new ORoadmapEntry(); 135 Reference< XInterface > xNewRoadmapItem = (::cppu::OWeakObject*)pRoadmapItem; 136 return xNewRoadmapItem; 137 } 138 139 140 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoControlRoadmapModel, UnoControlRoadmapModel_Base, UnoControlRoadmapModel_IBase ) 141 142 143 // ------------------------------------------------------------------- 144 ::com::sun::star::uno::Any SAL_CALL UnoControlRoadmapModel::queryAggregation( const ::com::sun::star::uno::Type & rType ) 145 { 146 Any aRet = UnoControlRoadmapModel_Base::queryAggregation( rType ); 147 if ( !aRet.hasValue() ) 148 aRet = UnoControlRoadmapModel_IBase::queryInterface( rType ); 149 return aRet; 150 } 151 152 153 // ------------------------------------------------------------------- 154 ::cppu::IPropertyArrayHelper& UnoControlRoadmapModel::getInfoHelper() 155 { 156 static UnoPropertyArrayHelper* pHelper = NULL; 157 if ( !pHelper ) 158 { 159 Sequence<sal_Int32> aIDs = ImplGetPropertyIds(); 160 pHelper = new UnoPropertyArrayHelper( aIDs ); 161 } 162 return *pHelper; 163 } 164 165 166 // beans::XMultiPropertySet 167 // ------------------------------------------------------------------- 168 Reference< XPropertySetInfo > UnoControlRoadmapModel::getPropertySetInfo( ) 169 { 170 static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) ); 171 return xInfo; 172 } 173 174 175 sal_Int32 SAL_CALL UnoControlRoadmapModel::getCount() 176 { 177 return maRoadmapItems.size(); 178 } 179 180 Any SAL_CALL UnoControlRoadmapModel::getByIndex( sal_Int32 Index ) 181 { 182 if (( Index >= (sal_Int32)maRoadmapItems.size()) || (Index < 0)) 183 lcl_throwIndexOutOfBoundsException( ); 184 Any aAny; 185 aAny = makeAny( maRoadmapItems.at( Index )); 186 return aAny; 187 } 188 189 190 191 void UnoControlRoadmapModel::MakeRMItemValidation( sal_Int32 Index, Reference< XInterface > xRoadmapItem ) 192 { 193 if ((Index > (sal_Int32)maRoadmapItems.size()) || ( Index < 0 ) ) 194 lcl_throwIndexOutOfBoundsException( ); 195 if ( !xRoadmapItem.is() ) 196 lcl_throwIllegalArgumentException(); 197 Reference< XServiceInfo > xServiceInfo( xRoadmapItem, UNO_QUERY ); 198 sal_Bool bIsRoadmapItem = xServiceInfo->supportsService( ::rtl::OUString::createFromAscii( "com.sun.star.awt.RoadmapItem" ) ); 199 if ( !bIsRoadmapItem ) 200 lcl_throwIllegalArgumentException(); 201 } 202 203 204 void UnoControlRoadmapModel::SetRMItemDefaultProperties( const sal_Int32 , Reference< XInterface > xRoadmapItem) 205 { 206 Any aAny; 207 Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY ); 208 Reference< XPropertySet > xProps( xRoadmapItem, UNO_QUERY ); 209 if ( xProps.is() ) 210 { 211 sal_Int32 LocID = 0; 212 Any aValue = xPropertySet->getPropertyValue( ::rtl::OUString::createFromAscii( "ID" ) ); 213 aValue >>= LocID; 214 if (LocID < 0) // index may not be smaller than zero 215 { 216 aAny <<= GetUniqueID(); 217 xPropertySet->setPropertyValue( ::rtl::OUString::createFromAscii( "ID" ), aAny ); 218 } 219 } 220 } 221 222 223 // The performance of this method could certainly be improved. 224 // As long as only vectors with up to 10 elements are 225 // involved it should be sufficient 226 sal_Int32 UnoControlRoadmapModel::GetUniqueID() 227 { 228 Any aAny; 229 sal_Bool bIncrement = sal_True; 230 sal_Int32 CurID = 0; 231 sal_Int32 n_CurItemID = 0; 232 Reference< XInterface > CurRoadmapItem; 233 while ( bIncrement ) 234 { 235 bIncrement = sal_False; 236 for ( RoadmapItemHolderList::iterator i = maRoadmapItems.begin(); i < maRoadmapItems.end(); i++ ) 237 { 238 CurRoadmapItem = *i; 239 Reference< XPropertySet > xPropertySet( CurRoadmapItem, UNO_QUERY ); 240 aAny = xPropertySet->getPropertyValue( ::rtl::OUString::createFromAscii( "ID" ) ); 241 aAny >>= n_CurItemID; 242 if (n_CurItemID == CurID) 243 { 244 bIncrement = sal_True; 245 CurID++; 246 break; 247 } 248 } 249 } 250 return CurID; 251 } 252 253 254 ContainerEvent UnoControlRoadmapModel::GetContainerEvent(sal_Int32 Index, Reference< XInterface > xRoadmapItem) 255 { 256 ContainerEvent aEvent; 257 aEvent.Source = *this; 258 aEvent.Element <<= xRoadmapItem; 259 aEvent.Accessor = makeAny(Index); 260 return aEvent; 261 } 262 263 264 sal_Int16 UnoControlRoadmapModel::GetCurrentItemID( Reference< XPropertySet > xPropertySet ) 265 { 266 Any aAny = xPropertySet->getPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ) ); 267 sal_Int16 n_CurrentItemID = 0; 268 aAny >>= n_CurrentItemID; 269 return n_CurrentItemID; 270 } 271 272 273 void SAL_CALL UnoControlRoadmapModel::insertByIndex( const sal_Int32 Index, const Any& _Element) 274 { 275 if ( ( Index >= ( (sal_Int32)maRoadmapItems.size() + 1 ) ) || (Index < 0)) 276 lcl_throwIndexOutOfBoundsException( ); 277 Reference< XInterface > xRoadmapItem; 278 _Element >>= xRoadmapItem; 279 MakeRMItemValidation( Index, xRoadmapItem); 280 SetRMItemDefaultProperties( Index, xRoadmapItem ); 281 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); 282 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem); 283 maContainerListeners.elementInserted( aEvent ); 284 Reference< XPropertySet > xPropertySet( (XAggregation*) (::cppu::OWeakAggObject*)this, UNO_QUERY ); 285 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet ); 286 if ( Index <= n_CurrentItemID ) 287 { 288 Any aAny; 289 aAny <<= ( sal_Int16 ) ( n_CurrentItemID + 1 ); 290 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny ); 291 } 292 } 293 294 295 296 void SAL_CALL UnoControlRoadmapModel::removeByIndex( sal_Int32 Index) 297 { 298 if (( Index > (sal_Int32)maRoadmapItems.size()) || (Index < 0)) 299 lcl_throwIndexOutOfBoundsException( ); 300 Reference< XInterface > xRoadmapItem; 301 maRoadmapItems.erase( maRoadmapItems.begin() + Index ); 302 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem); 303 maContainerListeners.elementRemoved( aEvent ); 304 Reference< XPropertySet > xPropertySet( (XAggregation*) (::cppu::OWeakAggObject*)this, UNO_QUERY ); 305 sal_Int16 n_CurrentItemID = GetCurrentItemID( xPropertySet ); 306 Any aAny; 307 if ( Index <= n_CurrentItemID ) 308 { 309 if ( n_CurrentItemID >= (sal_Int32)maRoadmapItems.size() ) 310 { 311 n_CurrentItemID = sal::static_int_cast< sal_Int16 >( 312 maRoadmapItems.size()-1); 313 if ( n_CurrentItemID < 0 ) 314 return; 315 aAny <<= n_CurrentItemID; 316 } 317 else if (Index == n_CurrentItemID) 318 aAny <<= ( sal_Int16 ) -1; 319 else if( Index < n_CurrentItemID) 320 aAny <<= ( sal_Int16 ) ( n_CurrentItemID - 1 ); 321 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny ); 322 } 323 } 324 325 326 void SAL_CALL UnoControlRoadmapModel::replaceByIndex( const sal_Int32 Index, const Any& _Element) 327 { 328 Reference< XInterface > xRoadmapItem; 329 _Element >>= xRoadmapItem; 330 MakeRMItemValidation( Index, xRoadmapItem); 331 SetRMItemDefaultProperties( Index, xRoadmapItem ); 332 maRoadmapItems.erase( maRoadmapItems.begin() + Index ); 333 maRoadmapItems.insert( maRoadmapItems.begin() + Index, xRoadmapItem); //push_back( xRoadmapItem ); 334 ContainerEvent aEvent = GetContainerEvent(Index, xRoadmapItem); 335 maContainerListeners.elementReplaced( aEvent ); 336 } 337 338 339 Type SAL_CALL UnoControlRoadmapModel::getElementType() 340 { 341 Type aType = getCppuType( ( Reference< XPropertySet>* ) NULL ); 342 return aType; 343 } 344 345 346 sal_Bool SAL_CALL UnoControlRoadmapModel::hasElements() 347 { 348 return !maRoadmapItems.empty(); 349 } 350 351 352 void SAL_CALL UnoControlRoadmapModel::addContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) 353 { 354 maContainerListeners.addInterface( xListener ); 355 } 356 357 void SAL_CALL UnoControlRoadmapModel::removeContainerListener( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainerListener >& xListener ) 358 { 359 maContainerListeners.removeInterface( xListener ); 360 } 361 362 // =================================================================== 363 // = UnoRoadmapControl 364 // =================================================================== 365 // ------------------------------------------------------------------- 366 UnoRoadmapControl::UnoRoadmapControl( const Reference< XMultiServiceFactory >& i_factory ) 367 :UnoControlRoadmap_Base( i_factory ) 368 ,maItemListeners( *this ) 369 { 370 } 371 372 IMPLEMENT_FORWARD_XTYPEPROVIDER2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase ) 373 IMPLEMENT_FORWARD_XINTERFACE2( UnoRoadmapControl, UnoControlRoadmap_Base, UnoControlRoadmap_IBase ) 374 375 376 sal_Bool SAL_CALL UnoRoadmapControl::setModel(const Reference< XControlModel >& _rModel) 377 { 378 379 380 Reference< XContainer > xC( getModel(), UNO_QUERY ); 381 if ( xC.is() ) 382 xC->removeContainerListener( this ); 383 384 sal_Bool bReturn = UnoControlBase::setModel( _rModel ); 385 386 xC = xC.query( getModel()); 387 if ( xC.is() ) 388 xC->addContainerListener( this ); 389 390 return bReturn; 391 } 392 393 394 // ------------------------------------------------------------------- 395 ::rtl::OUString UnoRoadmapControl::GetComponentServiceName() 396 { 397 return ::rtl::OUString::createFromAscii( "Roadmap" ); 398 } 399 400 401 402 void UnoRoadmapControl::dispose() 403 { 404 EventObject aEvt; 405 aEvt.Source = (::cppu::OWeakObject*)this; 406 maItemListeners.disposeAndClear( aEvt ); 407 UnoControl::dispose(); 408 } 409 410 411 412 void UnoRoadmapControl::elementInserted( const ContainerEvent& rEvent ) 413 { 414 Reference< XInterface > xRoadmapItem; 415 rEvent.Element >>= xRoadmapItem; 416 Reference< XPropertySet > xRoadmapPropertySet( xRoadmapItem, UNO_QUERY ); 417 if ( xRoadmapPropertySet.is() ) 418 xRoadmapPropertySet->addPropertyChangeListener( rtl::OUString(), this ); 419 420 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY); 421 if ( xPeer.is() ) 422 { 423 xPeer->elementInserted( rEvent ); 424 Reference < XPropertySet > xPropertySet( xPeer, UNO_QUERY ); 425 if ( xPropertySet.is() ) 426 xPropertySet->addPropertyChangeListener( rtl::OUString(), this ); 427 } 428 } 429 430 431 void UnoRoadmapControl::elementRemoved( const ContainerEvent& rEvent ) 432 { 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( rtl::OUString(), this ); 441 } 442 443 444 void UnoRoadmapControl::elementReplaced( const ContainerEvent& rEvent ) 445 { 446 Reference< XContainerListener > xPeer(getPeer(), UNO_QUERY); 447 if ( xPeer.is() ) 448 xPeer->elementReplaced( rEvent ); 449 } 450 451 452 void SAL_CALL UnoRoadmapControl::itemStateChanged( const ItemEvent& rEvent ) 453 { 454 sal_Int16 CurItemIndex = sal::static_int_cast< sal_Int16 >(rEvent.ItemId); 455 Any aAny; 456 aAny <<= CurItemIndex; 457 Reference< XControlModel > xModel( getModel( ), UNO_QUERY ); 458 Reference< XPropertySet > xPropertySet( xModel, UNO_QUERY ); 459 xPropertySet->setPropertyValue( GetPropertyName( BASEPROPERTY_CURRENTITEMID ), aAny ); 460 if ( maItemListeners.getLength() ) 461 maItemListeners.itemStateChanged( rEvent ); 462 } 463 464 465 void SAL_CALL UnoRoadmapControl::addItemListener( const Reference< XItemListener >& l ) 466 { 467 maItemListeners.addInterface( l ); 468 if( getPeer().is() && maItemListeners.getLength() == 1 ) 469 { 470 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY ); 471 xRoadmap->addItemListener( this ); 472 } 473 } 474 475 476 void SAL_CALL UnoRoadmapControl::removeItemListener( const Reference< XItemListener >& l ) 477 { 478 if( getPeer().is() && maItemListeners.getLength() == 1 ) 479 { 480 Reference < XItemEventBroadcaster > xRoadmap( getPeer(), UNO_QUERY ); 481 xRoadmap->removeItemListener( this ); 482 } 483 484 maItemListeners.removeInterface( l ); 485 } 486 487 488 void SAL_CALL UnoRoadmapControl::propertyChange( const PropertyChangeEvent& evt ) 489 { 490 Reference< XPropertyChangeListener > xPeer(getPeer(), UNO_QUERY); 491 if ( xPeer.is() ) 492 xPeer->propertyChange( evt ); 493 } 494 495 } 496