xref: /AOO41X/main/svtools/source/control/asynclink.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <svtools/asynclink.hxx>
32*cdf0e10cSrcweir #include <vos/mutex.hxx>
33*cdf0e10cSrcweir #include <tools/debug.hxx>
34*cdf0e10cSrcweir #include <vcl/timer.hxx>
35*cdf0e10cSrcweir #include <vcl/svapp.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //--------------------------------------------------------------------
38*cdf0e10cSrcweir namespace svtools {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir void AsynchronLink::CreateMutex()
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir     if( !_pMutex ) _pMutex = new vos::OMutex;
43*cdf0e10cSrcweir }
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir void AsynchronLink::Call( void* pObj, sal_Bool
46*cdf0e10cSrcweir #ifdef DBG_UTIL
47*cdf0e10cSrcweir bAllowDoubles
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir , sal_Bool bUseTimer )
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir #ifdef DBG_UTIL
52*cdf0e10cSrcweir 	if ( bUseTimer || !_bInCall )
53*cdf0e10cSrcweir 		DBG_WARNING( "Recursives Call. Eher ueber Timer. TLX Fragen" );
54*cdf0e10cSrcweir #endif
55*cdf0e10cSrcweir 	if( _aLink.IsSet() )
56*cdf0e10cSrcweir 	{
57*cdf0e10cSrcweir 		_pArg = pObj;
58*cdf0e10cSrcweir 		DBG_ASSERT( bAllowDoubles ||
59*cdf0e10cSrcweir 					( !_nEventId && ( !_pTimer || !_pTimer->IsActive() ) ),
60*cdf0e10cSrcweir 					"Schon ein Call unterwegs" );
61*cdf0e10cSrcweir 		if( _nEventId )
62*cdf0e10cSrcweir 		{
63*cdf0e10cSrcweir 			if( _pMutex ) _pMutex->acquire();
64*cdf0e10cSrcweir 			Application::RemoveUserEvent( _nEventId );
65*cdf0e10cSrcweir 			if( _pMutex ) _pMutex->release();
66*cdf0e10cSrcweir 		}
67*cdf0e10cSrcweir 		if( _pTimer )_pTimer->Stop();
68*cdf0e10cSrcweir 		if( bUseTimer )
69*cdf0e10cSrcweir 		{
70*cdf0e10cSrcweir 			if( !_pTimer )
71*cdf0e10cSrcweir 			{
72*cdf0e10cSrcweir 				_pTimer = new Timer;
73*cdf0e10cSrcweir 				_pTimer->SetTimeout( 0 );
74*cdf0e10cSrcweir 				_pTimer->SetTimeoutHdl( STATIC_LINK(
75*cdf0e10cSrcweir 					this, AsynchronLink, HandleCall) );
76*cdf0e10cSrcweir 			}
77*cdf0e10cSrcweir 			_pTimer->Start();
78*cdf0e10cSrcweir 		}
79*cdf0e10cSrcweir 		else
80*cdf0e10cSrcweir 		{
81*cdf0e10cSrcweir 			if( _pMutex ) _pMutex->acquire();
82*cdf0e10cSrcweir             Application::PostUserEvent( _nEventId, STATIC_LINK( this, AsynchronLink, HandleCall), 0 );
83*cdf0e10cSrcweir 			if( _pMutex ) _pMutex->release();
84*cdf0e10cSrcweir 		}
85*cdf0e10cSrcweir 	}
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir AsynchronLink::~AsynchronLink()
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	if( _nEventId )
91*cdf0e10cSrcweir 	{
92*cdf0e10cSrcweir 		Application::RemoveUserEvent( _nEventId );
93*cdf0e10cSrcweir 	}
94*cdf0e10cSrcweir 	delete _pTimer;
95*cdf0e10cSrcweir 	if( _pDeleted ) *_pDeleted = sal_True;
96*cdf0e10cSrcweir 	delete _pMutex;
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir IMPL_STATIC_LINK( AsynchronLink, HandleCall, void*, EMPTYARG )
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir 	if( pThis->_pMutex ) pThis->_pMutex->acquire();
102*cdf0e10cSrcweir 	pThis->_nEventId = 0;
103*cdf0e10cSrcweir 	if( pThis->_pMutex ) pThis->_pMutex->release();
104*cdf0e10cSrcweir 	pThis->Call_Impl( pThis->_pArg );
105*cdf0e10cSrcweir 	return 0;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir void AsynchronLink::ForcePendingCall()
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	ClearPendingCall();
111*cdf0e10cSrcweir 	Call_Impl( _pArg );
112*cdf0e10cSrcweir }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir void AsynchronLink::ClearPendingCall()
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     if( _pMutex ) _pMutex->acquire();
117*cdf0e10cSrcweir 	if( _nEventId )
118*cdf0e10cSrcweir 	{
119*cdf0e10cSrcweir 		Application::RemoveUserEvent( _nEventId );
120*cdf0e10cSrcweir 		_nEventId = 0;
121*cdf0e10cSrcweir 	}
122*cdf0e10cSrcweir     if( _pMutex ) _pMutex->release();
123*cdf0e10cSrcweir 	if( _pTimer ) _pTimer->Stop();
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir void AsynchronLink::Call_Impl( void* pArg )
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir 	_bInCall = sal_True;
129*cdf0e10cSrcweir 	sal_Bool bDeleted = sal_False;
130*cdf0e10cSrcweir 	_pDeleted = &bDeleted;
131*cdf0e10cSrcweir 	_aLink.Call( pArg );
132*cdf0e10cSrcweir 	if( !bDeleted )
133*cdf0e10cSrcweir 	{
134*cdf0e10cSrcweir 		_bInCall = sal_False;
135*cdf0e10cSrcweir 		_pDeleted = 0;
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir }
140