xref: /aoo41x/main/svl/source/notify/lstner.cxx (revision 40df464e)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir #ifndef GCC
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifndef DEBUG_HXX
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <svl/hint.hxx>
34cdf0e10cSrcweir #include <svl/brdcst.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir SV_DECL_PTRARR( SfxBroadcasterArr_Impl, SfxBroadcaster*, 0, 2 )
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define _SFX_LSTNER_CXX
39cdf0e10cSrcweir #include <svl/lstner.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //====================================================================
42cdf0e10cSrcweir DBG_NAME(SfxListener)
43cdf0e10cSrcweir TYPEINIT0(SfxListener);
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //====================================================================
46cdf0e10cSrcweir // simple ctor of class SfxListener
47cdf0e10cSrcweir 
SfxListener()48cdf0e10cSrcweir SfxListener::SfxListener()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	DBG_CTOR(SfxListener, 0);
51cdf0e10cSrcweir }
52cdf0e10cSrcweir //--------------------------------------------------------------------
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // copy ctor of class SfxListener
55cdf0e10cSrcweir 
SfxListener(const SfxListener & rListener)56cdf0e10cSrcweir SfxListener::SfxListener( const SfxListener &rListener )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	DBG_CTOR(SfxListener, 0);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	for ( sal_uInt16 n = 0; n < rListener.aBCs.Count(); ++n )
61cdf0e10cSrcweir 		StartListening( *rListener.aBCs[n] );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir //--------------------------------------------------------------------
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // unregisteres the SfxListener from its SfxBroadcasters
66cdf0e10cSrcweir 
~SfxListener()67cdf0e10cSrcweir SfxListener::~SfxListener()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	DBG_DTOR(SfxListener, 0);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	// unregister at all remainding broadcasters
72cdf0e10cSrcweir 	for ( sal_uInt16 nPos = 0; nPos < aBCs.Count(); ++nPos )
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir 		SfxBroadcaster *pBC = aBCs[nPos];
75cdf0e10cSrcweir 		pBC->RemoveListener(*this);
76cdf0e10cSrcweir 	}
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir //--------------------------------------------------------------------
80cdf0e10cSrcweir 
81cdf0e10cSrcweir // unregisteres at a specific SfxBroadcaster
82cdf0e10cSrcweir 
RemoveBroadcaster_Impl(SfxBroadcaster & rBC)83cdf0e10cSrcweir void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBC )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	DBG_CHKTHIS(SfxListener, 0);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	const SfxBroadcaster *pBC = &rBC;
88cdf0e10cSrcweir 	aBCs.Remove( aBCs.GetPos(pBC), 1 );
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir //--------------------------------------------------------------------
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // registeres at a specific SfxBroadcaster
94cdf0e10cSrcweir 
StartListening(SfxBroadcaster & rBroadcaster,sal_Bool bPreventDups)95cdf0e10cSrcweir sal_Bool SfxListener::StartListening( SfxBroadcaster& rBroadcaster, sal_Bool bPreventDups )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	DBG_CHKTHIS(SfxListener, 0);
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	if ( !bPreventDups || !IsListening( rBroadcaster ) )
100cdf0e10cSrcweir 	{
101cdf0e10cSrcweir 		if ( rBroadcaster.AddListener(*this) )
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			const SfxBroadcaster *pBC = &rBroadcaster;
104cdf0e10cSrcweir 			aBCs.Insert( pBC, aBCs.Count() );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 			DBG_ASSERT( IsListening(rBroadcaster), "StartListening failed" );
107cdf0e10cSrcweir 			return sal_True;
108cdf0e10cSrcweir 		}
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir 	return sal_False;
112cdf0e10cSrcweir }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir //--------------------------------------------------------------------
115cdf0e10cSrcweir 
116cdf0e10cSrcweir // unregisteres at a specific SfxBroadcaster
117cdf0e10cSrcweir 
EndListening(SfxBroadcaster & rBroadcaster,sal_Bool bAllDups)118cdf0e10cSrcweir sal_Bool SfxListener::EndListening( SfxBroadcaster& rBroadcaster, sal_Bool bAllDups )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir 	DBG_CHKTHIS(SfxListener, 0);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if ( !IsListening( rBroadcaster ) )
123cdf0e10cSrcweir 		return sal_False;
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	do
126cdf0e10cSrcweir 	{
127cdf0e10cSrcweir 		rBroadcaster.RemoveListener(*this);
128cdf0e10cSrcweir 		const SfxBroadcaster *pBC = &rBroadcaster;
129cdf0e10cSrcweir 		aBCs.Remove( aBCs.GetPos(pBC), 1 );
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir 	while ( bAllDups && IsListening( rBroadcaster ) );
132cdf0e10cSrcweir 	return sal_True;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir //--------------------------------------------------------------------
136cdf0e10cSrcweir 
137cdf0e10cSrcweir // unregisteres at a specific SfxBroadcaster by index
138cdf0e10cSrcweir 
EndListening(sal_uInt16 nNo)139cdf0e10cSrcweir void SfxListener::EndListening( sal_uInt16 nNo )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir 	DBG_CHKTHIS(SfxListener, 0);
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	SfxBroadcaster *pBC = aBCs.GetObject(nNo);
144cdf0e10cSrcweir 	pBC->RemoveListener(*this);
145cdf0e10cSrcweir 	aBCs.Remove( nNo, 1 );
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir //--------------------------------------------------------------------
149cdf0e10cSrcweir 
150cdf0e10cSrcweir // unregisteres all Broadcasters
151cdf0e10cSrcweir 
EndListeningAll()152cdf0e10cSrcweir void SfxListener::EndListeningAll()
153cdf0e10cSrcweir {
154cdf0e10cSrcweir 	DBG_CHKTHIS(SfxListener, 0);
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	// MI: bei Optimierung beachten: Seiteneffekte von RemoveListener beachten!
157cdf0e10cSrcweir 	while ( aBCs.Count() )
158cdf0e10cSrcweir 	{
159cdf0e10cSrcweir 		SfxBroadcaster *pBC = aBCs.GetObject(0);
160cdf0e10cSrcweir 		pBC->RemoveListener(*this);
161cdf0e10cSrcweir 		aBCs.Remove( 0, 1 );
162cdf0e10cSrcweir 	}
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir //--------------------------------------------------------------------
166cdf0e10cSrcweir 
IsListening(SfxBroadcaster & rBroadcaster) const167cdf0e10cSrcweir sal_Bool SfxListener::IsListening( SfxBroadcaster& rBroadcaster ) const
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	const SfxBroadcaster *pBC = &rBroadcaster;
170cdf0e10cSrcweir 	return USHRT_MAX != aBCs.GetPos( pBC );
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir //--------------------------------------------------------------------
174cdf0e10cSrcweir 
175cdf0e10cSrcweir // base implementation of notification handler
176cdf0e10cSrcweir 
177cdf0e10cSrcweir #ifdef DBG_UTIL
Notify(SfxBroadcaster & rBC,const SfxHint &)178cdf0e10cSrcweir void SfxListener::Notify( SfxBroadcaster& rBC, const SfxHint& )
179cdf0e10cSrcweir #else
180cdf0e10cSrcweir void SfxListener::Notify( SfxBroadcaster&, const SfxHint& )
181cdf0e10cSrcweir #endif
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     #ifdef DBG_UTIL
184cdf0e10cSrcweir 	const SfxBroadcaster *pBC = &rBC;
185cdf0e10cSrcweir 	DBG_ASSERT( USHRT_MAX != aBCs.GetPos(pBC),
186cdf0e10cSrcweir 				"notification from unregistered broadcaster" );
187cdf0e10cSrcweir     #endif
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190