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