merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / notify / brdcst.cxx
blob83bdb1cee9543f48307603a6f1ebc6b949b7ac2a
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: brdcst.cxx,v $
10 * $Revision: 1.8.60.1 $
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_svtools.hxx"
34 #ifndef GCC
35 #endif
36 #include <tools/debug.hxx>
38 #include <svtools/hint.hxx>
39 #include <svtools/smplhint.hxx>
40 #include <svtools/lstner.hxx>
42 SV_DECL_PTRARR( SfxListenerArr_Impl, SfxListener*, 0, 2 )
44 #define _SFX_BRDCST_CXX
45 #include <svtools/brdcst.hxx>
47 //====================================================================
48 DBG_NAME(SfxBroadcaster)
49 TYPEINIT0(SfxBroadcaster);
51 //====================================================================
53 //====================================================================
54 // broadcast immediately
57 void SfxBroadcaster::Broadcast( const SfxHint &rHint )
59 DBG_CHKTHIS(SfxBroadcaster, 0);
61 // is anybody to notify?
62 if ( aListeners.Count() /*! || aGlobListeners.Count() */ )
64 // notify all registered listeners exactly once
65 for ( USHORT n = 0; n < aListeners.Count(); ++n )
67 SfxListener* pListener = aListeners[n];
68 if ( pListener )
69 pListener->Notify( *this, rHint );
74 //--------------------------------------------------------------------
76 // broadcast after a timeout
79 void SfxBroadcaster::BroadcastDelayed( const SfxHint& rHint )
81 DBG_WARNING( "not implemented" );
82 Broadcast(rHint);
84 //--------------------------------------------------------------------
86 // broadcast in idle-handler
88 void SfxBroadcaster::BroadcastInIdle( const SfxHint& rHint )
90 DBG_WARNING( "not implemented" );
91 Broadcast(rHint);
93 //--------------------------------------------------------------------
95 // unregister all listeners
97 SfxBroadcaster::~SfxBroadcaster()
99 DBG_DTOR(SfxBroadcaster, 0);
101 Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
103 // remove all still registered listeners
104 for ( USHORT nPos = 0; nPos < aListeners.Count(); ++nPos )
106 SfxListener *pListener = aListeners[nPos];
107 if ( pListener )
108 pListener->RemoveBroadcaster_Impl(*this);
112 //--------------------------------------------------------------------
114 // simple ctor of class SfxBroadcaster
116 SfxBroadcaster::SfxBroadcaster()
118 DBG_CTOR(SfxBroadcaster, 0);
121 //--------------------------------------------------------------------
123 // copy ctor of class SfxBroadcaster
126 SfxBroadcaster::SfxBroadcaster( const SfxBroadcaster &rBC )
128 DBG_CTOR(SfxBroadcaster, 0);
130 for ( USHORT n = 0; n < rBC.aListeners.Count(); ++n )
132 SfxListener *pListener = rBC.aListeners[n];
133 if ( pListener )
134 pListener->StartListening( *this );
138 //--------------------------------------------------------------------
140 // add a new SfxListener to the list
142 BOOL SfxBroadcaster::AddListener( SfxListener& rListener )
144 DBG_CHKTHIS(SfxBroadcaster, 0);
145 const SfxListener *pListener = &rListener;
146 const SfxListener *pNull = 0;
147 USHORT nFreePos = aListeners.GetPos( pNull );
148 if ( nFreePos < aListeners.Count() )
149 aListeners.GetData()[nFreePos] = pListener;
150 else if ( aListeners.Count() < (USHRT_MAX-1) )
151 aListeners.Insert( pListener, aListeners.Count() );
152 else
154 DBG_ERROR( "array overflow" );
155 return FALSE;
158 DBG_ASSERT( USHRT_MAX != aListeners.GetPos(pListener),
159 "AddListener failed" );
160 return TRUE;
163 //--------------------------------------------------------------------
165 // called, if no more listeners exists
167 void SfxBroadcaster::ListenersGone()
169 DBG_CHKTHIS(SfxBroadcaster,0);
172 //--------------------------------------------------------------------
174 // forward a notification to all registered listeners
176 void SfxBroadcaster::Forward(SfxBroadcaster& rBC, const SfxHint& rHint)
178 const USHORT nCount = aListeners.Count();
179 for ( USHORT i = 0; i < nCount; ++i )
181 SfxListener *pListener = aListeners[i];
182 if ( pListener )
183 pListener->Notify( rBC, rHint );
187 //--------------------------------------------------------------------
189 // remove one SfxListener from the list
191 void SfxBroadcaster::RemoveListener( SfxListener& rListener )
193 {DBG_CHKTHIS(SfxBroadcaster, 0);}
194 const SfxListener *pListener = &rListener;
195 USHORT nPos = aListeners.GetPos(pListener);
196 DBG_ASSERT( nPos != USHRT_MAX, "RemoveListener: Listener unknown" );
197 aListeners.GetData()[nPos] = 0;
198 if ( !HasListeners() )
199 ListenersGone();
202 //--------------------------------------------------------------------
204 BOOL SfxBroadcaster::HasListeners() const
206 for ( USHORT n = 0; n < aListeners.Count(); ++n )
207 if ( aListeners.GetObject(n) != 0 )
208 return TRUE;
209 return FALSE;
212 //--------------------------------------------------------------------