tdf#146549 sw: Make the formatting toolbar visible
[LibreOffice.git] / sc / source / core / tool / refreshtimer.cxx
blob1ad6734d9cbc94751729954d83bcbdafba9c4ad9
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( bool b )
25 if ( b && nBlockRefresh )
26 --nBlockRefresh;
27 else if ( !b && nBlockRefresh < sal_uInt16(~0) )
28 ++nBlockRefresh;
31 ScRefreshTimerProtector::ScRefreshTimerProtector( std::unique_ptr<ScRefreshTimerControl> const & rp )
33 m_rpControl( rp )
35 if ( m_rpControl )
37 m_rpControl->SetAllowRefresh( false );
38 // wait for any running refresh in another thread to finish
39 std::scoped_lock aGuard( m_rpControl->GetMutex() );
43 ScRefreshTimerProtector::~ScRefreshTimerProtector()
45 if ( m_rpControl )
46 m_rpControl->SetAllowRefresh( true );
49 ScRefreshTimer::ScRefreshTimer() : AutoTimer("ScRefreshTimer"), ppControl(nullptr)
51 SetTimeout( 0 );
54 ScRefreshTimer::ScRefreshTimer( sal_Int32 nRefreshDelaySeconds ) : AutoTimer("ScRefreshTimer"), ppControl(nullptr)
56 SetTimeout( nRefreshDelaySeconds * 1000 );
57 Launch();
60 ScRefreshTimer::ScRefreshTimer( const ScRefreshTimer& r ) : AutoTimer( r ), ppControl(nullptr)
64 ScRefreshTimer::~ScRefreshTimer()
66 if ( IsActive() )
67 Stop();
70 ScRefreshTimer& ScRefreshTimer::operator=( const ScRefreshTimer& r )
72 if(this == &r)
73 return *this;
75 SetRefreshControl(nullptr);
76 AutoTimer::operator=( r );
77 return *this;
80 bool ScRefreshTimer::operator==( const ScRefreshTimer& r ) const
82 return GetTimeout() == r.GetTimeout();
85 bool ScRefreshTimer::operator!=( const ScRefreshTimer& r ) const
87 return !ScRefreshTimer::operator==( r );
90 void ScRefreshTimer::SetRefreshControl( std::unique_ptr<ScRefreshTimerControl> const * pp )
92 ppControl = pp;
95 void ScRefreshTimer::SetRefreshHandler( const Link<Timer *, void>& rLink )
97 SetInvokeHandler( rLink );
100 sal_Int32 ScRefreshTimer::GetRefreshDelaySeconds() const
102 return GetTimeout() / 1000;
105 void ScRefreshTimer::StopRefreshTimer()
107 Stop();
110 void ScRefreshTimer::SetRefreshDelay( sal_Int32 nSeconds )
112 bool bActive = IsActive();
113 if ( bActive && !nSeconds )
114 Stop();
115 SetTimeout( nSeconds * 1000 );
116 if ( !bActive && nSeconds )
117 Launch();
120 void ScRefreshTimer::Invoke()
122 if ( ppControl && *ppControl && (*ppControl)->IsRefreshAllowed() )
124 // now we COULD make the call in another thread ...
125 std::scoped_lock aGuard( (*ppControl)->GetMutex() );
126 Timer::Invoke();
127 // restart from now on, don't execute immediately again if timed out
128 // a second time during refresh
129 if ( IsActive() )
130 Launch();
134 void ScRefreshTimer::Launch()
136 if ( GetTimeout() )
137 AutoTimer::Start();
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */