1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <sfx2/itemconnect.hxx>
31 #include <boost/shared_ptr.hpp>
33 #include <svl/itempool.hxx>
35 // ============================================================================
39 // ============================================================================
41 // ============================================================================
45 TriState
lclConvertToTriState( bool bKnown
, bool bIsKnownFlag
, bool bIsUnknownFlag
)
47 return (bKnown
&& bIsKnownFlag
) ? STATE_CHECK
: ((!bKnown
&& bIsUnknownFlag
) ? STATE_NOCHECK
: STATE_DONTKNOW
);
52 // ----------------------------------------------------------------------------
54 sal_uInt16
ItemWrapperHelper::GetWhichId( const SfxItemSet
& rItemSet
, sal_uInt16 nSlot
)
56 return rItemSet
.GetPool()->GetWhich( nSlot
);
59 bool ItemWrapperHelper::IsKnownItem( const SfxItemSet
& rItemSet
, sal_uInt16 nSlot
)
61 return rItemSet
.GetItemState( GetWhichId( rItemSet
, nSlot
), sal_True
) != SFX_ITEM_UNKNOWN
;
64 const SfxPoolItem
* ItemWrapperHelper::GetUniqueItem( const SfxItemSet
& rItemSet
, sal_uInt16 nSlot
)
66 sal_uInt16 nWhich
= GetWhichId( rItemSet
, nSlot
);
67 return (rItemSet
.GetItemState( nWhich
, sal_True
) >= SFX_ITEM_DEFAULT
) ? rItemSet
.GetItem( nWhich
, sal_True
) : 0;
70 const SfxPoolItem
& ItemWrapperHelper::GetDefaultItem( const SfxItemSet
& rItemSet
, sal_uInt16 nSlot
)
72 return rItemSet
.GetPool()->GetDefaultItem( GetWhichId( rItemSet
, nSlot
) );
75 void ItemWrapperHelper::RemoveDefaultItem( SfxItemSet
& rDestSet
, const SfxItemSet
& rOldSet
, sal_uInt16 nSlot
)
77 sal_uInt16 nWhich
= GetWhichId( rDestSet
, nSlot
);
78 if( rOldSet
.GetItemState( nWhich
, sal_False
) == SFX_ITEM_DEFAULT
)
79 rDestSet
.ClearItem( nWhich
);
82 // ============================================================================
83 // Base control wrapper classes
84 // ============================================================================
86 ControlWrapperBase::~ControlWrapperBase()
90 // ============================================================================
91 // Single control wrappers
92 // ============================================================================
94 DummyWindowWrapper::DummyWindowWrapper( Window
& rWindow
) :
95 SingleControlWrapperType( rWindow
)
99 bool DummyWindowWrapper::IsControlDontKnow() const
104 void DummyWindowWrapper::SetControlDontKnow( bool )
108 void* DummyWindowWrapper::GetControlValue() const
113 void DummyWindowWrapper::SetControlValue( void* )
117 // ----------------------------------------------------------------------------
119 CheckBoxWrapper::CheckBoxWrapper( CheckBox
& rCheckBox
) :
120 SingleControlWrapperType( rCheckBox
)
124 bool CheckBoxWrapper::IsControlDontKnow() const
126 return GetControl().GetState() == STATE_DONTKNOW
;
129 void CheckBoxWrapper::SetControlDontKnow( bool bSet
)
131 GetControl().EnableTriState( bSet
);
132 GetControl().SetState( bSet
? STATE_DONTKNOW
: STATE_NOCHECK
);
135 sal_Bool
CheckBoxWrapper::GetControlValue() const
137 return GetControl().IsChecked();
140 void CheckBoxWrapper::SetControlValue( sal_Bool bValue
)
142 GetControl().Check( bValue
);
145 // ----------------------------------------------------------------------------
147 ColorListBoxWrapper::ColorListBoxWrapper(ColorListBox
& rListBox
):
148 SingleControlWrapper
< ColorListBox
, Color
>(rListBox
)
151 ColorListBoxWrapper::~ColorListBoxWrapper()
154 bool ColorListBoxWrapper::IsControlDontKnow() const
156 return GetControl().GetSelectEntryCount() == 0;
159 void ColorListBoxWrapper::SetControlDontKnow( bool bSet
)
161 if( bSet
) GetControl().SetNoSelection();
164 Color
ColorListBoxWrapper::GetControlValue() const
166 return GetControl().GetSelectEntryColor();
169 void ColorListBoxWrapper::SetControlValue( Color aColor
)
171 GetControl().SelectEntry( aColor
);
174 // ============================================================================
175 // Multi control wrappers
176 // ============================================================================
178 typedef std::vector
< ControlWrapperBase
* > ControlWrpVec
;
179 typedef ControlWrpVec::iterator ControlWrpVecI
;
180 typedef ControlWrpVec::const_iterator ControlWrpVecCI
;
182 struct MultiControlWrapperHelper_Impl
187 MultiControlWrapperHelper::MultiControlWrapperHelper() :
188 mxImpl( new MultiControlWrapperHelper_Impl
)
192 MultiControlWrapperHelper::~MultiControlWrapperHelper()
196 void MultiControlWrapperHelper::RegisterControlWrapper( ControlWrapperBase
& rWrapper
)
198 mxImpl
->maVec
.push_back( &rWrapper
);
201 void MultiControlWrapperHelper::ModifyControl( TriState eEnable
, TriState eShow
)
203 for( ControlWrpVecI aIt
= mxImpl
->maVec
.begin(), aEnd
= mxImpl
->maVec
.end(); aIt
!= aEnd
; ++aIt
)
204 (*aIt
)->ModifyControl( eEnable
, eShow
);
207 bool MultiControlWrapperHelper::IsControlDontKnow() const
209 bool bIs
= !mxImpl
->maVec
.empty();
210 for( ControlWrpVecCI aIt
= mxImpl
->maVec
.begin(), aEnd
= mxImpl
->maVec
.end(); bIs
&& (aIt
!= aEnd
); ++aIt
)
211 bIs
&= (*aIt
)->IsControlDontKnow();
215 void MultiControlWrapperHelper::SetControlDontKnow( bool bSet
)
217 for( ControlWrpVecI aIt
= mxImpl
->maVec
.begin(), aEnd
= mxImpl
->maVec
.end(); aIt
!= aEnd
; ++aIt
)
218 (*aIt
)->SetControlDontKnow( bSet
);
221 // ============================================================================
222 // Base connection classes
223 // ============================================================================
225 ItemConnectionBase::ItemConnectionBase( ItemConnFlags nFlags
) :
230 ItemConnectionBase::~ItemConnectionBase()
234 bool ItemConnectionBase::IsActive() const
236 return !(mnFlags
& ITEMCONN_INACTIVE
);
239 void ItemConnectionBase::DoApplyFlags( const SfxItemSet
& rItemSet
)
242 ApplyFlags( rItemSet
);
245 void ItemConnectionBase::DoReset( const SfxItemSet
& rItemSet
)
251 bool ItemConnectionBase::DoFillItemSet( SfxItemSet
& rDestSet
, const SfxItemSet
& rOldSet
)
253 return IsActive() && FillItemSet( rDestSet
, rOldSet
);
256 TriState
ItemConnectionBase::GetEnableState( bool bKnown
) const
258 return lclConvertToTriState( bKnown
, mnFlags
& ITEMCONN_ENABLE_KNOWN
, mnFlags
& ITEMCONN_DISABLE_UNKNOWN
);
261 TriState
ItemConnectionBase::GetShowState( bool bKnown
) const
263 return lclConvertToTriState( bKnown
, mnFlags
& ITEMCONN_SHOW_KNOWN
, mnFlags
& ITEMCONN_HIDE_UNKNOWN
);
266 // ============================================================================
267 // Standard connections
268 // ============================================================================
270 DummyItemConnection::DummyItemConnection( sal_uInt16 nSlot
, Window
& rWindow
, ItemConnFlags nFlags
) :
271 ItemConnectionBase( nFlags
),
272 DummyWindowWrapper( rWindow
),
277 void DummyItemConnection::ApplyFlags( const SfxItemSet
& rItemSet
)
279 bool bKnown
= ItemWrapperHelper::IsKnownItem( rItemSet
, mnSlot
);
280 ModifyControl( GetEnableState( bKnown
), GetShowState( bKnown
) );
283 void DummyItemConnection::Reset( const SfxItemSet
& /*rItemSet*/ )
287 bool DummyItemConnection::FillItemSet( SfxItemSet
& /*rDestSet*/, const SfxItemSet
& /*rOldSet*/ )
289 return false; // item set not changed
292 // ============================================================================
293 // Array of connections
294 // ============================================================================
296 class ItemConnectionArrayImpl
299 void Append( ItemConnectionBase
* pConnection
);
301 void ApplyFlags( const SfxItemSet
& rItemSet
);
302 void Reset( const SfxItemSet
& rItemSet
);
303 bool FillItemSet( SfxItemSet
& rDestSet
, const SfxItemSet
& rOldSet
);
306 typedef boost::shared_ptr
< ItemConnectionBase
> ItemConnectionRef
;
307 typedef std::list
< ItemConnectionRef
> ItemConnectionList
;
308 typedef ItemConnectionList::iterator ItemConnectionListIt
;
310 ItemConnectionList maList
;
313 void ItemConnectionArrayImpl::Append( ItemConnectionBase
* pConnection
)
316 maList
.push_back( ItemConnectionRef( pConnection
) );
319 void ItemConnectionArrayImpl::ApplyFlags( const SfxItemSet
& rItemSet
)
321 for( ItemConnectionListIt aIt
= maList
.begin(), aEnd
= maList
.end(); aIt
!= aEnd
; ++aIt
)
322 (*aIt
)->DoApplyFlags( rItemSet
);
325 void ItemConnectionArrayImpl::Reset( const SfxItemSet
& rItemSet
)
327 for( ItemConnectionListIt aIt
= maList
.begin(), aEnd
= maList
.end(); aIt
!= aEnd
; ++aIt
)
328 (*aIt
)->DoReset( rItemSet
);
331 bool ItemConnectionArrayImpl::FillItemSet( SfxItemSet
& rDestSet
, const SfxItemSet
& rOldSet
)
333 bool bChanged
= false;
334 for( ItemConnectionListIt aIt
= maList
.begin(), aEnd
= maList
.end(); aIt
!= aEnd
; ++aIt
)
335 bChanged
|= (*aIt
)->DoFillItemSet( rDestSet
, rOldSet
);
339 // ----------------------------------------------------------------------------
341 ItemConnectionArray::ItemConnectionArray() :
342 mxImpl( new ItemConnectionArrayImpl
)
346 ItemConnectionArray::~ItemConnectionArray()
350 void ItemConnectionArray::AddConnection( ItemConnectionBase
* pConnection
)
352 mxImpl
->Append( pConnection
);
355 void ItemConnectionArray::ApplyFlags( const SfxItemSet
& rItemSet
)
357 mxImpl
->ApplyFlags( rItemSet
);
360 void ItemConnectionArray::Reset( const SfxItemSet
& rItemSet
)
362 mxImpl
->Reset( rItemSet
);
365 bool ItemConnectionArray::FillItemSet( SfxItemSet
& rDestSet
, const SfxItemSet
& rOldSet
)
367 return mxImpl
->FillItemSet( rDestSet
, rOldSet
);
370 // ============================================================================
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */