Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / dialog / itemconnect.cxx
blobfb70443aef36e41fcabfcbfbee7da5efc72ddb71
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 .
20 #include <sfx2/itemconnect.hxx>
22 #include <boost/shared_ptr.hpp>
23 #include <list>
24 #include <svl/itempool.hxx>
26 // ============================================================================
28 namespace sfx {
30 // ============================================================================
31 // Helpers
32 // ============================================================================
34 namespace {
36 TriState lclConvertToTriState( bool bKnown, bool bIsKnownFlag, bool bIsUnknownFlag )
38 return (bKnown && bIsKnownFlag) ? STATE_CHECK : ((!bKnown && bIsUnknownFlag) ? STATE_NOCHECK : STATE_DONTKNOW);
41 } // namespace
43 // ----------------------------------------------------------------------------
45 sal_uInt16 ItemWrapperHelper::GetWhichId( const SfxItemSet& rItemSet, sal_uInt16 nSlot )
47 return rItemSet.GetPool()->GetWhich( nSlot );
50 bool ItemWrapperHelper::IsKnownItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot )
52 return rItemSet.GetItemState( GetWhichId( rItemSet, nSlot ), sal_True ) != SFX_ITEM_UNKNOWN;
55 const SfxPoolItem* ItemWrapperHelper::GetUniqueItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot )
57 sal_uInt16 nWhich = GetWhichId( rItemSet, nSlot );
58 return (rItemSet.GetItemState( nWhich, sal_True ) >= SFX_ITEM_DEFAULT) ? rItemSet.GetItem( nWhich, sal_True ) : 0;
61 const SfxPoolItem& ItemWrapperHelper::GetDefaultItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot )
63 return rItemSet.GetPool()->GetDefaultItem( GetWhichId( rItemSet, nSlot ) );
66 void ItemWrapperHelper::RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, sal_uInt16 nSlot )
68 sal_uInt16 nWhich = GetWhichId( rDestSet, nSlot );
69 if( rOldSet.GetItemState( nWhich, sal_False ) == SFX_ITEM_DEFAULT )
70 rDestSet.ClearItem( nWhich );
73 // ============================================================================
74 // Base control wrapper classes
75 // ============================================================================
77 ControlWrapperBase::~ControlWrapperBase()
81 // ============================================================================
82 // Single control wrappers
83 // ============================================================================
85 DummyWindowWrapper::DummyWindowWrapper( Window& rWindow ) :
86 SingleControlWrapperType( rWindow )
90 bool DummyWindowWrapper::IsControlDontKnow() const
92 return false;
95 void DummyWindowWrapper::SetControlDontKnow( bool )
99 void* DummyWindowWrapper::GetControlValue() const
101 return 0;
104 void DummyWindowWrapper::SetControlValue( void* )
108 // ----------------------------------------------------------------------------
110 CheckBoxWrapper::CheckBoxWrapper( CheckBox& rCheckBox ) :
111 SingleControlWrapperType( rCheckBox )
115 bool CheckBoxWrapper::IsControlDontKnow() const
117 return GetControl().GetState() == STATE_DONTKNOW;
120 void CheckBoxWrapper::SetControlDontKnow( bool bSet )
122 GetControl().EnableTriState( bSet );
123 GetControl().SetState( bSet ? STATE_DONTKNOW : STATE_NOCHECK );
126 sal_Bool CheckBoxWrapper::GetControlValue() const
128 return GetControl().IsChecked();
131 void CheckBoxWrapper::SetControlValue( sal_Bool bValue )
133 GetControl().Check( bValue );
136 // ----------------------------------------------------------------------------
138 ColorListBoxWrapper::ColorListBoxWrapper(ColorListBox & rListBox):
139 SingleControlWrapper< ColorListBox, Color >(rListBox)
142 ColorListBoxWrapper::~ColorListBoxWrapper()
145 bool ColorListBoxWrapper::IsControlDontKnow() const
147 return GetControl().GetSelectEntryCount() == 0;
150 void ColorListBoxWrapper::SetControlDontKnow( bool bSet )
152 if( bSet ) GetControl().SetNoSelection();
155 Color ColorListBoxWrapper::GetControlValue() const
157 return GetControl().GetSelectEntryColor();
160 void ColorListBoxWrapper::SetControlValue( Color aColor )
162 GetControl().SelectEntry( aColor );
165 // ============================================================================
166 // Multi control wrappers
167 // ============================================================================
169 typedef std::vector< ControlWrapperBase* > ControlWrpVec;
170 typedef ControlWrpVec::iterator ControlWrpVecI;
171 typedef ControlWrpVec::const_iterator ControlWrpVecCI;
173 struct MultiControlWrapperHelper_Impl
175 ControlWrpVec maVec;
178 MultiControlWrapperHelper::MultiControlWrapperHelper() :
179 mxImpl( new MultiControlWrapperHelper_Impl )
183 MultiControlWrapperHelper::~MultiControlWrapperHelper()
187 void MultiControlWrapperHelper::RegisterControlWrapper( ControlWrapperBase& rWrapper )
189 mxImpl->maVec.push_back( &rWrapper );
192 void MultiControlWrapperHelper::ModifyControl( TriState eEnable, TriState eShow )
194 for( ControlWrpVecI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); aIt != aEnd; ++aIt )
195 (*aIt)->ModifyControl( eEnable, eShow );
198 bool MultiControlWrapperHelper::IsControlDontKnow() const
200 bool bIs = !mxImpl->maVec.empty();
201 for( ControlWrpVecCI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); bIs && (aIt != aEnd); ++aIt )
202 bIs &= (*aIt)->IsControlDontKnow();
203 return bIs;
206 void MultiControlWrapperHelper::SetControlDontKnow( bool bSet )
208 for( ControlWrpVecI aIt = mxImpl->maVec.begin(), aEnd = mxImpl->maVec.end(); aIt != aEnd; ++aIt )
209 (*aIt)->SetControlDontKnow( bSet );
212 // ============================================================================
213 // Base connection classes
214 // ============================================================================
216 ItemConnectionBase::ItemConnectionBase( ItemConnFlags nFlags ) :
217 mnFlags( nFlags )
221 ItemConnectionBase::~ItemConnectionBase()
225 bool ItemConnectionBase::IsActive() const
227 return !(mnFlags & ITEMCONN_INACTIVE);
230 void ItemConnectionBase::DoApplyFlags( const SfxItemSet& rItemSet )
232 if( IsActive() )
233 ApplyFlags( rItemSet );
236 void ItemConnectionBase::DoReset( const SfxItemSet& rItemSet )
238 if( IsActive() )
239 Reset( rItemSet );
242 bool ItemConnectionBase::DoFillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet )
244 return IsActive() && FillItemSet( rDestSet, rOldSet );
247 TriState ItemConnectionBase::GetEnableState( bool bKnown ) const
249 return lclConvertToTriState( bKnown, mnFlags & ITEMCONN_ENABLE_KNOWN, mnFlags & ITEMCONN_DISABLE_UNKNOWN );
252 TriState ItemConnectionBase::GetShowState( bool bKnown ) const
254 return lclConvertToTriState( bKnown, mnFlags & ITEMCONN_SHOW_KNOWN, mnFlags & ITEMCONN_HIDE_UNKNOWN );
257 // ============================================================================
258 // Standard connections
259 // ============================================================================
261 DummyItemConnection::DummyItemConnection( sal_uInt16 nSlot, Window& rWindow, ItemConnFlags nFlags ) :
262 ItemConnectionBase( nFlags ),
263 DummyWindowWrapper( rWindow ),
264 mnSlot( nSlot )
268 void DummyItemConnection::ApplyFlags( const SfxItemSet& rItemSet )
270 bool bKnown = ItemWrapperHelper::IsKnownItem( rItemSet, mnSlot );
271 ModifyControl( GetEnableState( bKnown ), GetShowState( bKnown ) );
274 void DummyItemConnection::Reset( const SfxItemSet& /*rItemSet*/ )
278 bool DummyItemConnection::FillItemSet( SfxItemSet& /*rDestSet*/, const SfxItemSet& /*rOldSet*/ )
280 return false; // item set not changed
283 // ============================================================================
284 // Array of connections
285 // ============================================================================
287 class ItemConnectionArrayImpl
289 public:
290 void Append( ItemConnectionBase* pConnection );
292 void ApplyFlags( const SfxItemSet& rItemSet );
293 void Reset( const SfxItemSet& rItemSet );
294 bool FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet );
296 private:
297 typedef boost::shared_ptr< ItemConnectionBase > ItemConnectionRef;
298 typedef std::list< ItemConnectionRef > ItemConnectionList;
299 typedef ItemConnectionList::iterator ItemConnectionListIt;
301 ItemConnectionList maList;
304 void ItemConnectionArrayImpl::Append( ItemConnectionBase* pConnection )
306 if( pConnection )
307 maList.push_back( ItemConnectionRef( pConnection ) );
310 void ItemConnectionArrayImpl::ApplyFlags( const SfxItemSet& rItemSet )
312 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt )
313 (*aIt)->DoApplyFlags( rItemSet );
316 void ItemConnectionArrayImpl::Reset( const SfxItemSet& rItemSet )
318 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt )
319 (*aIt)->DoReset( rItemSet );
322 bool ItemConnectionArrayImpl::FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet )
324 bool bChanged = false;
325 for( ItemConnectionListIt aIt = maList.begin(), aEnd = maList.end(); aIt != aEnd; ++aIt )
326 bChanged |= (*aIt)->DoFillItemSet( rDestSet, rOldSet );
327 return bChanged;
330 // ----------------------------------------------------------------------------
332 ItemConnectionArray::ItemConnectionArray() :
333 mxImpl( new ItemConnectionArrayImpl )
337 ItemConnectionArray::~ItemConnectionArray()
341 void ItemConnectionArray::AddConnection( ItemConnectionBase* pConnection )
343 mxImpl->Append( pConnection );
346 void ItemConnectionArray::ApplyFlags( const SfxItemSet& rItemSet )
348 mxImpl->ApplyFlags( rItemSet );
351 void ItemConnectionArray::Reset( const SfxItemSet& rItemSet )
353 mxImpl->Reset( rItemSet );
356 bool ItemConnectionArray::FillItemSet( SfxItemSet& rDestSet, const SfxItemSet& rOldSet )
358 return mxImpl->FillItemSet( rDestSet, rOldSet );
361 // ============================================================================
363 } // namespace sfx
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */