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_sfx2.hxx" 26 #include <sfx2/itemconnect.hxx> 27 28 #include <boost/shared_ptr.hpp> 29 #include <list> 30 #include <svl/itempool.hxx> 31 32 // ============================================================================ 33 34 namespace sfx { 35 36 // ============================================================================ 37 // Helpers 38 // ============================================================================ 39 40 namespace { 41 42 TriState lclConvertToTriState( bool bKnown, bool bIsKnownFlag, bool bIsUnknownFlag ) 43 { 44 return (bKnown && bIsKnownFlag) ? STATE_CHECK : ((!bKnown && bIsUnknownFlag) ? STATE_NOCHECK : STATE_DONTKNOW); 45 } 46 47 } // namespace 48 49 // ---------------------------------------------------------------------------- 50 51 sal_uInt16 ItemWrapperHelper::GetWhichId( const SfxItemSet& rItemSet, sal_uInt16 nSlot ) 52 { 53 return rItemSet.GetPool()->GetWhich( nSlot ); 54 } 55 56 bool ItemWrapperHelper::IsKnownItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot ) 57 { 58 return rItemSet.GetItemState( GetWhichId( rItemSet, nSlot ), sal_True ) != SFX_ITEM_UNKNOWN; 59 } 60 61 const SfxPoolItem* ItemWrapperHelper::GetUniqueItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot ) 62 { 63 sal_uInt16 nWhich = GetWhichId( rItemSet, nSlot ); 64 return (rItemSet.GetItemState( nWhich, sal_True ) >= SFX_ITEM_DEFAULT) ? rItemSet.GetItem( nWhich, sal_True ) : 0; 65 } 66 67 const SfxPoolItem& ItemWrapperHelper::GetDefaultItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot ) 68 { 69 return rItemSet.GetPool()->GetDefaultItem( GetWhichId( rItemSet, nSlot ) ); 70 } 71 72 void ItemWrapperHelper::RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, sal_uInt16 nSlot ) 73 { 74 sal_uInt16 nWhich = GetWhichId( rDestSet, nSlot ); 75 if( rOldSet.GetItemState( nWhich, sal_False ) == SFX_ITEM_DEFAULT ) 76 rDestSet.ClearItem( nWhich ); 77 } 78 79 // ============================================================================ 80 // Base control wrapper classes 81 // ============================================================================ 82 83 ControlWrapperBase::~ControlWrapperBase() 84 { 85 } 86 87 // ============================================================================ 88 // Single control wrappers 89 // ============================================================================ 90 91 DummyWindowWrapper::DummyWindowWrapper( Window& rWindow ) : 92 SingleControlWrapperType( rWindow ) 93 { 94 } 95 96 bool DummyWindowWrapper::IsControlDontKnow() const 97 { 98 return false; 99 } 100 101 void DummyWindowWrapper::SetControlDontKnow( bool ) 102 { 103 } 104 105 void* DummyWindowWrapper::GetControlValue() const 106 { 107 return 0; 108 } 109 110 void DummyWindowWrapper::SetControlValue( void* ) 111 { 112 } 113 114 // ---------------------------------------------------------------------------- 115 116 CheckBoxWrapper::CheckBoxWrapper( CheckBox& rCheckBox ) : 117 SingleControlWrapperType( rCheckBox ) 118 { 119 } 120 121 bool CheckBoxWrapper::IsControlDontKnow() const 122 { 123 return GetControl().GetState() == STATE_DONTKNOW; 124 } 125 126 void CheckBoxWrapper::SetControlDontKnow( bool bSet ) 127 { 128 GetControl().EnableTriState( bSet ); 129 GetControl().SetState( bSet ? STATE_DONTKNOW : STATE_NOCHECK ); 130 } 131 132 sal_Bool CheckBoxWrapper::GetControlValue() const 133 { 134 return GetControl().IsChecked(); 135 } 136 137 void CheckBoxWrapper::SetControlValue( sal_Bool bValue ) 138 { 139 GetControl().Check( bValue ); 140 } 141 142 // ---------------------------------------------------------------------------- 143 144 EditWrapper::EditWrapper( Edit& rEdit ) : 145 SingleControlWrapperType( rEdit ) 146 { 147 } 148 149 bool EditWrapper::IsControlDontKnow() const 150 { 151 // no "don't know" state - empty string is a valid value of an Edit 152 return false; 153 } 154 155 void EditWrapper::SetControlDontKnow( bool bSet ) 156 { 157 if( bSet ) 158 GetControl().SetText( String() ); 159 } 160 161 String EditWrapper::GetControlValue() const 162 { 163 return GetControl().GetText(); 164 } 165 166 void EditWrapper::SetControlValue( String aValue ) 167 { 168 GetControl().SetText( aValue ); 169 } 170 171 // ---------------------------------------------------------------------------- 172 173 ColorListBoxWrapper::ColorListBoxWrapper(ColorListBox & rListBox): 174 SingleControlWrapper< ColorListBox, Color >(rListBox) 175 {} 176 177 ColorListBoxWrapper::~ColorListBoxWrapper() 178 {} 179 180 bool ColorListBoxWrapper::IsControlDontKnow() const 181 { 182 return GetControl().GetSelectEntryCount() == 0; 183 } 184 185 void ColorListBoxWrapper::SetControlDontKnow( bool bSet ) 186 { 187 if( bSet ) GetControl().SetNoSelection(); 188 } 189 190 Color ColorListBoxWrapper::GetControlValue() const 191 { 192 return GetControl().GetSelectEntryColor(); 193 } 194 195 void ColorListBoxWrapper::SetControlValue( Color aColor ) 196 { 197 GetControl().SelectEntry( aColor ); 198 } 199 200 // ============================================================================ 201 // Multi control wrappers 202 // ============================================================================ 203 204 typedef std::vector< ControlWrapperBase* > ControlWrpVec; 205 typedef ControlWrpVec::iterator ControlWrpVecI; 206 typedef ControlWrpVec::const_iterator ControlWrpVecCI; 207 208 struct MultiControlWrapperHelper_Impl 209 { 210 ControlWrpVec maVec; 211 }; 212 213 MultiControlWrapperHelper::MultiControlWrapperHelper() : 214 mxImpl( new MultiControlWrapperHelper_Impl ) 215 { 216 } 217 218 MultiControlWrapperHelper::~MultiControlWrapperHelper() 219 { 220 } 221 222 void MultiControlWrapperHelper::RegisterControlWrapper( ControlWrapperBase& rWrapper ) 223 { 224 mxImpl->maVec.push_back( &rWrapper ); 225 } 226 227 void MultiControlWrapperHelper::ModifyControl( TriState eEnable, TriState eShow ) 228 { 229 for( ControlWrpVecI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); aIt != aEnd; ++aIt ) 230 (*aIt)->ModifyControl( eEnable, eShow ); 231 } 232 233 bool MultiControlWrapperHelper::IsControlDontKnow() const 234 { 235 bool bIs = !mxImpl->maVec.empty(); 236 for( ControlWrpVecCI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); bIs && (aIt != aEnd); ++aIt ) 237 bIs &= (*aIt)->IsControlDontKnow(); 238 return bIs; 239 } 240 241 void MultiControlWrapperHelper::SetControlDontKnow( bool bSet ) 242 { 243 for( ControlWrpVecI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); aIt != aEnd; ++aIt ) 244 (*aIt)->SetControlDontKnow( bSet ); 245 } 246 247 // ============================================================================ 248 // Base connection classes 249 // ============================================================================ 250 251 ItemConnectionBase::ItemConnectionBase( ItemConnFlags nFlags ) : 252 mnFlags( nFlags ) 253 { 254 } 255 256 ItemConnectionBase::~ItemConnectionBase() 257 { 258 } 259 260 void ItemConnectionBase::Activate( bool bActive ) 261 { 262 if( bActive ) mnFlags &= ~ITEMCONN_INACTIVE; else mnFlags |= ITEMCONN_INACTIVE; 263 } 264 265 bool ItemConnectionBase::IsActive() const 266 { 267 return !(mnFlags & ITEMCONN_INACTIVE); 268 } 269 270 void ItemConnectionBase::DoApplyFlags( const SfxItemSet& rItemSet ) 271 { 272 if( IsActive() ) 273 ApplyFlags( rItemSet ); 274 } 275 276 void ItemConnectionBase::DoReset( const SfxItemSet& rItemSet ) 277 { 278 if( IsActive() ) 279 Reset( rItemSet ); 280 } 281 282 bool ItemConnectionBase::DoFillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet ) 283 { 284 return IsActive() && FillItemSet( rDestSet, rOldSet ); 285 } 286 287 TriState ItemConnectionBase::GetEnableState( bool bKnown ) const 288 { 289 return lclConvertToTriState( bKnown, mnFlags & ITEMCONN_ENABLE_KNOWN, mnFlags & ITEMCONN_DISABLE_UNKNOWN ); 290 } 291 292 TriState ItemConnectionBase::GetShowState( bool bKnown ) const 293 { 294 return lclConvertToTriState( bKnown, mnFlags & ITEMCONN_SHOW_KNOWN, mnFlags & ITEMCONN_HIDE_UNKNOWN ); 295 } 296 297 // ============================================================================ 298 // Standard connections 299 // ============================================================================ 300 301 DummyItemConnection::DummyItemConnection( sal_uInt16 nSlot, Window& rWindow, ItemConnFlags nFlags ) : 302 ItemConnectionBase( nFlags ), 303 DummyWindowWrapper( rWindow ), 304 mnSlot( nSlot ) 305 { 306 } 307 308 void DummyItemConnection::ApplyFlags( const SfxItemSet& rItemSet ) 309 { 310 bool bKnown = ItemWrapperHelper::IsKnownItem( rItemSet, mnSlot ); 311 ModifyControl( GetEnableState( bKnown ), GetShowState( bKnown ) ); 312 } 313 314 void DummyItemConnection::Reset( const SfxItemSet& /*rItemSet*/ ) 315 { 316 } 317 318 bool DummyItemConnection::FillItemSet( SfxItemSet& /*rDestSet*/, const SfxItemSet& /*rOldSet*/ ) 319 { 320 return false; // item set not changed 321 } 322 323 // ============================================================================ 324 // Array of connections 325 // ============================================================================ 326 327 class ItemConnectionArrayImpl 328 { 329 public: 330 void Append( ItemConnectionBase* pConnection ); 331 332 void ApplyFlags( const SfxItemSet& rItemSet ); 333 void Reset( const SfxItemSet& rItemSet ); 334 bool FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet ); 335 336 private: 337 typedef boost::shared_ptr< ItemConnectionBase > ItemConnectionRef; 338 typedef std::list< ItemConnectionRef > ItemConnectionList; 339 typedef ItemConnectionList::iterator ItemConnectionListIt; 340 341 ItemConnectionList maList; 342 }; 343 344 void ItemConnectionArrayImpl::Append( ItemConnectionBase* pConnection ) 345 { 346 if( pConnection ) 347 maList.push_back( ItemConnectionRef( pConnection ) ); 348 } 349 350 void ItemConnectionArrayImpl::ApplyFlags( const SfxItemSet& rItemSet ) 351 { 352 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt ) 353 (*aIt)->DoApplyFlags( rItemSet ); 354 } 355 356 void ItemConnectionArrayImpl::Reset( const SfxItemSet& rItemSet ) 357 { 358 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt ) 359 (*aIt)->DoReset( rItemSet ); 360 } 361 362 bool ItemConnectionArrayImpl::FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet ) 363 { 364 bool bChanged = false; 365 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt ) 366 bChanged |= (*aIt)->DoFillItemSet( rDestSet, rOldSet ); 367 return bChanged; 368 } 369 370 // ---------------------------------------------------------------------------- 371 372 ItemConnectionArray::ItemConnectionArray() : 373 mxImpl( new ItemConnectionArrayImpl ) 374 { 375 } 376 377 ItemConnectionArray::~ItemConnectionArray() 378 { 379 } 380 381 void ItemConnectionArray::AddConnection( ItemConnectionBase* pConnection ) 382 { 383 mxImpl->Append( pConnection ); 384 } 385 386 void ItemConnectionArray::ApplyFlags( const SfxItemSet& rItemSet ) 387 { 388 mxImpl->ApplyFlags( rItemSet ); 389 } 390 391 void ItemConnectionArray::Reset( const SfxItemSet& rItemSet ) 392 { 393 mxImpl->Reset( rItemSet ); 394 } 395 396 bool ItemConnectionArray::FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet ) 397 { 398 return mxImpl->FillItemSet( rDestSet, rOldSet ); 399 } 400 401 // ============================================================================ 402 403 } // namespace sfx 404 405