update dev300-m57
[ooovba.git] / sfx2 / source / dialog / itemconnect.cxx
blobddf97257bcbccaddf284122eccafc52a083e2ccc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: itemconnect.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
33 #include <sfx2/itemconnect.hxx>
35 #include <boost/shared_ptr.hpp>
36 #include <list>
37 #include <svtools/itempool.hxx>
39 // ============================================================================
41 namespace sfx {
43 // ============================================================================
44 // Helpers
45 // ============================================================================
47 namespace {
49 TriState lclConvertToTriState( bool bKnown, bool bIsKnownFlag, bool bIsUnknownFlag )
51 return (bKnown && bIsKnownFlag) ? STATE_CHECK : ((!bKnown && bIsUnknownFlag) ? STATE_NOCHECK : STATE_DONTKNOW);
54 } // namespace
56 // ----------------------------------------------------------------------------
58 USHORT ItemWrapperHelper::GetWhichId( const SfxItemSet& rItemSet, USHORT nSlot )
60 return rItemSet.GetPool()->GetWhich( nSlot );
63 bool ItemWrapperHelper::IsKnownItem( const SfxItemSet& rItemSet, USHORT nSlot )
65 return rItemSet.GetItemState( GetWhichId( rItemSet, nSlot ), TRUE ) != SFX_ITEM_UNKNOWN;
68 const SfxPoolItem* ItemWrapperHelper::GetUniqueItem( const SfxItemSet& rItemSet, USHORT nSlot )
70 USHORT nWhich = GetWhichId( rItemSet, nSlot );
71 return (rItemSet.GetItemState( nWhich, TRUE ) >= SFX_ITEM_DEFAULT) ? rItemSet.GetItem( nWhich, TRUE ) : 0;
74 const SfxPoolItem& ItemWrapperHelper::GetDefaultItem( const SfxItemSet& rItemSet, USHORT nSlot )
76 return rItemSet.GetPool()->GetDefaultItem( GetWhichId( rItemSet, nSlot ) );
79 void ItemWrapperHelper::RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, USHORT nSlot )
81 USHORT nWhich = GetWhichId( rDestSet, nSlot );
82 if( rOldSet.GetItemState( nWhich, FALSE ) == SFX_ITEM_DEFAULT )
83 rDestSet.ClearItem( nWhich );
86 // ============================================================================
87 // Base control wrapper classes
88 // ============================================================================
90 ControlWrapperBase::~ControlWrapperBase()
94 // ============================================================================
95 // Single control wrappers
96 // ============================================================================
98 DummyWindowWrapper::DummyWindowWrapper( Window& rWindow ) :
99 SingleControlWrapperType( rWindow )
103 bool DummyWindowWrapper::IsControlDontKnow() const
105 return false;
108 void DummyWindowWrapper::SetControlDontKnow( bool )
112 void* DummyWindowWrapper::GetControlValue() const
114 return 0;
117 void DummyWindowWrapper::SetControlValue( void* )
121 // ----------------------------------------------------------------------------
123 CheckBoxWrapper::CheckBoxWrapper( CheckBox& rCheckBox ) :
124 SingleControlWrapperType( rCheckBox )
128 bool CheckBoxWrapper::IsControlDontKnow() const
130 return GetControl().GetState() == STATE_DONTKNOW;
133 void CheckBoxWrapper::SetControlDontKnow( bool bSet )
135 GetControl().EnableTriState( bSet );
136 GetControl().SetState( bSet ? STATE_DONTKNOW : STATE_NOCHECK );
139 BOOL CheckBoxWrapper::GetControlValue() const
141 return GetControl().IsChecked();
144 void CheckBoxWrapper::SetControlValue( BOOL bValue )
146 GetControl().Check( bValue );
149 // ----------------------------------------------------------------------------
151 EditWrapper::EditWrapper( Edit& rEdit ) :
152 SingleControlWrapperType( rEdit )
156 bool EditWrapper::IsControlDontKnow() const
158 // no "don't know" state - empty string is a valid value of an Edit
159 return false;
162 void EditWrapper::SetControlDontKnow( bool bSet )
164 if( bSet )
165 GetControl().SetText( String() );
168 String EditWrapper::GetControlValue() const
170 return GetControl().GetText();
173 void EditWrapper::SetControlValue( String aValue )
175 GetControl().SetText( aValue );
178 // ----------------------------------------------------------------------------
180 ColorListBoxWrapper::ColorListBoxWrapper(ColorListBox & rListBox):
181 SingleControlWrapper< ColorListBox, Color >(rListBox)
184 ColorListBoxWrapper::~ColorListBoxWrapper()
187 bool ColorListBoxWrapper::IsControlDontKnow() const
189 return GetControl().GetSelectEntryCount() == 0;
192 void ColorListBoxWrapper::SetControlDontKnow( bool bSet )
194 if( bSet ) GetControl().SetNoSelection();
197 Color ColorListBoxWrapper::GetControlValue() const
199 return GetControl().GetSelectEntryColor();
202 void ColorListBoxWrapper::SetControlValue( Color aColor )
204 GetControl().SelectEntry( aColor );
207 // ============================================================================
208 // Multi control wrappers
209 // ============================================================================
211 typedef std::vector< ControlWrapperBase* > ControlWrpVec;
212 typedef ControlWrpVec::iterator ControlWrpVecI;
213 typedef ControlWrpVec::const_iterator ControlWrpVecCI;
215 struct MultiControlWrapperHelper_Impl
217 ControlWrpVec maVec;
220 MultiControlWrapperHelper::MultiControlWrapperHelper() :
221 mxImpl( new MultiControlWrapperHelper_Impl )
225 MultiControlWrapperHelper::~MultiControlWrapperHelper()
229 void MultiControlWrapperHelper::RegisterControlWrapper( ControlWrapperBase& rWrapper )
231 mxImpl->maVec.push_back( &rWrapper );
234 void MultiControlWrapperHelper::ModifyControl( TriState eEnable, TriState eShow )
236 for( ControlWrpVecI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); aIt != aEnd; ++aIt )
237 (*aIt)->ModifyControl( eEnable, eShow );
240 bool MultiControlWrapperHelper::IsControlDontKnow() const
242 bool bIs = !mxImpl->maVec.empty();
243 for( ControlWrpVecCI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); bIs && (aIt != aEnd); ++aIt )
244 bIs &= (*aIt)->IsControlDontKnow();
245 return bIs;
248 void MultiControlWrapperHelper::SetControlDontKnow( bool bSet )
250 for( ControlWrpVecI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); aIt != aEnd; ++aIt )
251 (*aIt)->SetControlDontKnow( bSet );
254 // ============================================================================
255 // Base connection classes
256 // ============================================================================
258 ItemConnectionBase::ItemConnectionBase( ItemConnFlags nFlags ) :
259 mnFlags( nFlags )
263 ItemConnectionBase::~ItemConnectionBase()
267 void ItemConnectionBase::Activate( bool bActive )
269 if( bActive ) mnFlags &= ~ITEMCONN_INACTIVE; else mnFlags |= ITEMCONN_INACTIVE;
272 bool ItemConnectionBase::IsActive() const
274 return !(mnFlags & ITEMCONN_INACTIVE);
277 void ItemConnectionBase::DoApplyFlags( const SfxItemSet& rItemSet )
279 if( IsActive() )
280 ApplyFlags( rItemSet );
283 void ItemConnectionBase::DoReset( const SfxItemSet& rItemSet )
285 if( IsActive() )
286 Reset( rItemSet );
289 bool ItemConnectionBase::DoFillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet )
291 return IsActive() && FillItemSet( rDestSet, rOldSet );
294 TriState ItemConnectionBase::GetEnableState( bool bKnown ) const
296 return lclConvertToTriState( bKnown, mnFlags & ITEMCONN_ENABLE_KNOWN, mnFlags & ITEMCONN_DISABLE_UNKNOWN );
299 TriState ItemConnectionBase::GetShowState( bool bKnown ) const
301 return lclConvertToTriState( bKnown, mnFlags & ITEMCONN_SHOW_KNOWN, mnFlags & ITEMCONN_HIDE_UNKNOWN );
304 // ============================================================================
305 // Standard connections
306 // ============================================================================
308 DummyItemConnection::DummyItemConnection( USHORT nSlot, Window& rWindow, ItemConnFlags nFlags ) :
309 ItemConnectionBase( nFlags ),
310 DummyWindowWrapper( rWindow ),
311 mnSlot( nSlot )
315 void DummyItemConnection::ApplyFlags( const SfxItemSet& rItemSet )
317 bool bKnown = ItemWrapperHelper::IsKnownItem( rItemSet, mnSlot );
318 ModifyControl( GetEnableState( bKnown ), GetShowState( bKnown ) );
321 void DummyItemConnection::Reset( const SfxItemSet& /*rItemSet*/ )
325 bool DummyItemConnection::FillItemSet( SfxItemSet& /*rDestSet*/, const SfxItemSet& /*rOldSet*/ )
327 return false; // item set not changed
330 // ============================================================================
331 // Array of connections
332 // ============================================================================
334 class ItemConnectionArrayImpl
336 public:
337 void Append( ItemConnectionBase* pConnection );
339 void ApplyFlags( const SfxItemSet& rItemSet );
340 void Reset( const SfxItemSet& rItemSet );
341 bool FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet );
343 private:
344 typedef boost::shared_ptr< ItemConnectionBase > ItemConnectionRef;
345 typedef std::list< ItemConnectionRef > ItemConnectionList;
346 typedef ItemConnectionList::iterator ItemConnectionListIt;
348 ItemConnectionList maList;
351 void ItemConnectionArrayImpl::Append( ItemConnectionBase* pConnection )
353 if( pConnection )
354 maList.push_back( ItemConnectionRef( pConnection ) );
357 void ItemConnectionArrayImpl::ApplyFlags( const SfxItemSet& rItemSet )
359 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt )
360 (*aIt)->DoApplyFlags( rItemSet );
363 void ItemConnectionArrayImpl::Reset( const SfxItemSet& rItemSet )
365 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt )
366 (*aIt)->DoReset( rItemSet );
369 bool ItemConnectionArrayImpl::FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet )
371 bool bChanged = false;
372 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt )
373 bChanged |= (*aIt)->DoFillItemSet( rDestSet, rOldSet );
374 return bChanged;
377 // ----------------------------------------------------------------------------
379 ItemConnectionArray::ItemConnectionArray() :
380 mxImpl( new ItemConnectionArrayImpl )
384 ItemConnectionArray::~ItemConnectionArray()
388 void ItemConnectionArray::AddConnection( ItemConnectionBase* pConnection )
390 mxImpl->Append( pConnection );
393 void ItemConnectionArray::ApplyFlags( const SfxItemSet& rItemSet )
395 mxImpl->ApplyFlags( rItemSet );
398 void ItemConnectionArray::Reset( const SfxItemSet& rItemSet )
400 mxImpl->Reset( rItemSet );
403 bool ItemConnectionArray::FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet )
405 return mxImpl->FillItemSet( rDestSet, rOldSet );
408 // ============================================================================
410 } // namespace sfx