Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / refreshtimer.cxx
blob9834aaed3f21c73edd2155dd336e4844665aa36e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "refreshtimer.hxx"
21 #include "refreshtimerprotector.hxx"
23 void ScRefreshTimerControl::SetAllowRefresh( sal_Bool b )
25 if ( b && nBlockRefresh )
26 --nBlockRefresh;
27 else if ( !b && nBlockRefresh < (sal_uInt16)(~0) )
28 ++nBlockRefresh;
31 ScRefreshTimerProtector::ScRefreshTimerProtector( ScRefreshTimerControl * const * pp )
33 ppControl( pp )
35 if ( ppControl && *ppControl )
37 (*ppControl)->SetAllowRefresh( false );
38 // wait for any running refresh in another thread to finnish
39 ::osl::MutexGuard aGuard( (*ppControl)->GetMutex() );
43 ScRefreshTimerProtector::~ScRefreshTimerProtector()
45 if ( ppControl && *ppControl )
46 (*ppControl)->SetAllowRefresh( true );
49 ScRefreshTimer::ScRefreshTimer() : ppControl(0)
51 SetTimeout( 0 );
54 ScRefreshTimer::ScRefreshTimer( sal_uLong nSeconds ) : ppControl(0)
56 SetTimeout( nSeconds * 1000 );
57 Start();
60 ScRefreshTimer::ScRefreshTimer( const ScRefreshTimer& r ) : AutoTimer( r ), ppControl(0)
64 ScRefreshTimer::~ScRefreshTimer()
66 if ( IsActive() )
67 Stop();
70 ScRefreshTimer& ScRefreshTimer::operator=( const ScRefreshTimer& r )
72 SetRefreshControl(0);
73 AutoTimer::operator=( r );
74 return *this;
77 sal_Bool ScRefreshTimer::operator==( const ScRefreshTimer& r ) const
79 return GetTimeout() == r.GetTimeout();
82 sal_Bool ScRefreshTimer::operator!=( const ScRefreshTimer& r ) const
84 return !ScRefreshTimer::operator==( r );
87 void ScRefreshTimer::StartRefreshTimer()
89 Start();
92 void ScRefreshTimer::SetRefreshControl( ScRefreshTimerControl * const * pp )
94 ppControl = pp;
97 void ScRefreshTimer::SetRefreshHandler( const Link& rLink )
99 SetTimeoutHdl( rLink );
102 sal_uLong ScRefreshTimer::GetRefreshDelay() const
104 return GetTimeout() / 1000;
107 void ScRefreshTimer::StopRefreshTimer()
109 Stop();
112 void ScRefreshTimer::SetRefreshDelay( sal_uLong nSeconds )
114 sal_Bool bActive = IsActive();
115 if ( bActive && !nSeconds )
116 Stop();
117 SetTimeout( nSeconds * 1000 );
118 if ( !bActive && nSeconds )
119 Start();
122 void ScRefreshTimer::Timeout()
124 if ( ppControl && *ppControl && (*ppControl)->IsRefreshAllowed() )
126 // now we COULD make the call in another thread ...
127 ::osl::MutexGuard aGuard( (*ppControl)->GetMutex() );
128 maTimeoutHdl.Call( this );
129 // restart from now on, don't execute immediately again if timed out
130 // a second time during refresh
131 if ( IsActive() )
132 Start();
136 void ScRefreshTimer::Start()
138 if ( GetTimeout() )
139 AutoTimer::Start();
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */