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