1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lstner.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"
37 #include <tools/debug.hxx>
40 #include <svtools/hint.hxx>
41 #include <svtools/brdcst.hxx>
43 SV_DECL_PTRARR( SfxBroadcasterArr_Impl
, SfxBroadcaster
*, 0, 2 )
45 #define _SFX_LSTNER_CXX
46 #include <svtools/lstner.hxx>
48 //====================================================================
50 TYPEINIT0(SfxListener
);
52 //====================================================================
53 // simple ctor of class SfxListener
55 SfxListener::SfxListener()
57 DBG_CTOR(SfxListener
, 0);
59 //--------------------------------------------------------------------
61 // copy ctor of class SfxListener
63 SfxListener::SfxListener( const SfxListener
&rListener
)
65 DBG_CTOR(SfxListener
, 0);
67 for ( USHORT n
= 0; n
< rListener
.aBCs
.Count(); ++n
)
68 StartListening( *rListener
.aBCs
[n
] );
70 //--------------------------------------------------------------------
72 // unregisteres the SfxListener from its SfxBroadcasters
74 SfxListener::~SfxListener()
76 DBG_DTOR(SfxListener
, 0);
78 // unregister at all remainding broadcasters
79 for ( USHORT nPos
= 0; nPos
< aBCs
.Count(); ++nPos
)
81 SfxBroadcaster
*pBC
= aBCs
[nPos
];
82 pBC
->RemoveListener(*this);
86 //--------------------------------------------------------------------
88 // unregisteres at a specific SfxBroadcaster
90 void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster
& rBC
)
92 DBG_CHKTHIS(SfxListener
, 0);
94 const SfxBroadcaster
*pBC
= &rBC
;
95 aBCs
.Remove( aBCs
.GetPos(pBC
), 1 );
98 //--------------------------------------------------------------------
100 // registeres at a specific SfxBroadcaster
102 BOOL
SfxListener::StartListening( SfxBroadcaster
& rBroadcaster
, BOOL bPreventDups
)
104 DBG_CHKTHIS(SfxListener
, 0);
106 if ( !bPreventDups
|| !IsListening( rBroadcaster
) )
108 if ( rBroadcaster
.AddListener(*this) )
110 const SfxBroadcaster
*pBC
= &rBroadcaster
;
111 aBCs
.Insert( pBC
, aBCs
.Count() );
113 DBG_ASSERT( IsListening(rBroadcaster
), "StartListening failed" );
121 //--------------------------------------------------------------------
123 // unregisteres at a specific SfxBroadcaster
125 BOOL
SfxListener::EndListening( SfxBroadcaster
& rBroadcaster
, BOOL bAllDups
)
127 DBG_CHKTHIS(SfxListener
, 0);
129 if ( !IsListening( rBroadcaster
) )
134 rBroadcaster
.RemoveListener(*this);
135 const SfxBroadcaster
*pBC
= &rBroadcaster
;
136 aBCs
.Remove( aBCs
.GetPos(pBC
), 1 );
138 while ( bAllDups
&& IsListening( rBroadcaster
) );
142 //--------------------------------------------------------------------
144 // unregisteres at a specific SfxBroadcaster by index
146 void SfxListener::EndListening( USHORT nNo
)
148 DBG_CHKTHIS(SfxListener
, 0);
150 SfxBroadcaster
*pBC
= aBCs
.GetObject(nNo
);
151 pBC
->RemoveListener(*this);
152 aBCs
.Remove( nNo
, 1 );
155 //--------------------------------------------------------------------
157 // unregisteres all Broadcasters
159 void SfxListener::EndListeningAll()
161 DBG_CHKTHIS(SfxListener
, 0);
163 // MI: bei Optimierung beachten: Seiteneffekte von RemoveListener beachten!
164 while ( aBCs
.Count() )
166 SfxBroadcaster
*pBC
= aBCs
.GetObject(0);
167 pBC
->RemoveListener(*this);
172 //--------------------------------------------------------------------
174 BOOL
SfxListener::IsListening( SfxBroadcaster
& rBroadcaster
) const
176 const SfxBroadcaster
*pBC
= &rBroadcaster
;
177 return USHRT_MAX
!= aBCs
.GetPos( pBC
);
180 //--------------------------------------------------------------------
182 // base implementation of notification handler
185 void SfxListener::Notify( SfxBroadcaster
& rBC
, const SfxHint
& )
187 void SfxListener::Notify( SfxBroadcaster
&, const SfxHint
& )
191 const SfxBroadcaster
*pBC
= &rBC
;
192 DBG_ASSERT( USHRT_MAX
!= aBCs
.GetPos(pBC
),
193 "notification from unregistered broadcaster" );