1*38d50f7bSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*38d50f7bSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*38d50f7bSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*38d50f7bSAndrew Rist * distributed with this work for additional information 6*38d50f7bSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*38d50f7bSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*38d50f7bSAndrew Rist * "License"); you may not use this file except in compliance 9*38d50f7bSAndrew Rist * with the License. You may obtain a copy of the License at 10*38d50f7bSAndrew Rist * 11*38d50f7bSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*38d50f7bSAndrew Rist * 13*38d50f7bSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*38d50f7bSAndrew Rist * software distributed under the License is distributed on an 15*38d50f7bSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*38d50f7bSAndrew Rist * KIND, either express or implied. See the License for the 17*38d50f7bSAndrew Rist * specific language governing permissions and limitations 18*38d50f7bSAndrew Rist * under the License. 19*38d50f7bSAndrew Rist * 20*38d50f7bSAndrew Rist *************************************************************/ 21*38d50f7bSAndrew Rist 22*38d50f7bSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef SC_REFRESHTIMER_HXX 25cdf0e10cSrcweir #define SC_REFRESHTIMER_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <tools/list.hxx> 28cdf0e10cSrcweir #include <vcl/timer.hxx> 29cdf0e10cSrcweir #include <vos/mutex.hxx> 30cdf0e10cSrcweir #include <scdllapi.h> 31cdf0e10cSrcweir 32cdf0e10cSrcweir #define SC_REFRESHTIMER_CONTROL_LIST 0 33cdf0e10cSrcweir #if SC_REFRESHTIMER_CONTROL_LIST 34cdf0e10cSrcweir class ScRefreshTimer; 35cdf0e10cSrcweir DECLARE_LIST( ScRefreshTimerList, ScRefreshTimer* ) 36cdf0e10cSrcweir #endif 37cdf0e10cSrcweir 38cdf0e10cSrcweir class ScRefreshTimerControl 39cdf0e10cSrcweir { 40cdf0e10cSrcweir private: 41cdf0e10cSrcweir ::vos::OMutex aMutex; 42cdf0e10cSrcweir sal_uInt16 nBlockRefresh; 43cdf0e10cSrcweir 44cdf0e10cSrcweir public: 45cdf0e10cSrcweir #if SC_REFRESHTIMER_CONTROL_LIST 46cdf0e10cSrcweir ScRefreshTimerList aList; 47cdf0e10cSrcweir #endif 48cdf0e10cSrcweir ScRefreshTimerControl()49cdf0e10cSrcweir ScRefreshTimerControl() : nBlockRefresh(0) {} 50cdf0e10cSrcweir SetAllowRefresh(sal_Bool b)51cdf0e10cSrcweir void SetAllowRefresh( sal_Bool b ) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir if ( b && nBlockRefresh ) 54cdf0e10cSrcweir --nBlockRefresh; 55cdf0e10cSrcweir else if ( !b && nBlockRefresh < (sal_uInt16)(~0) ) 56cdf0e10cSrcweir ++nBlockRefresh; 57cdf0e10cSrcweir } IsRefreshAllowed() const58cdf0e10cSrcweir sal_Bool IsRefreshAllowed() const { return !nBlockRefresh; } GetMutex()59cdf0e10cSrcweir ::vos::OMutex& GetMutex() { return aMutex; } 60cdf0e10cSrcweir }; 61cdf0e10cSrcweir 62cdf0e10cSrcweir 63cdf0e10cSrcweir class ScRefreshTimerProtector 64cdf0e10cSrcweir { 65cdf0e10cSrcweir private: 66cdf0e10cSrcweir ScRefreshTimerControl * const * ppControl; 67cdf0e10cSrcweir public: 68cdf0e10cSrcweir ScRefreshTimerProtector( ScRefreshTimerControl * const * pp ); ~ScRefreshTimerProtector()69cdf0e10cSrcweir ~ScRefreshTimerProtector() 70cdf0e10cSrcweir { 71cdf0e10cSrcweir if ( ppControl && *ppControl ) 72cdf0e10cSrcweir (*ppControl)->SetAllowRefresh( sal_True ); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir }; 75cdf0e10cSrcweir 76cdf0e10cSrcweir 77cdf0e10cSrcweir class ScRefreshTimer : public AutoTimer 78cdf0e10cSrcweir { 79cdf0e10cSrcweir private: 80cdf0e10cSrcweir ScRefreshTimerControl * const * ppControl; 81cdf0e10cSrcweir AppendToControl()82cdf0e10cSrcweir void AppendToControl() 83cdf0e10cSrcweir { 84cdf0e10cSrcweir #if SC_REFRESHTIMER_CONTROL_LIST 85cdf0e10cSrcweir if ( ppControl && *ppControl ) 86cdf0e10cSrcweir (*ppControl)->aList.Insert( this, LIST_APPEND ); 87cdf0e10cSrcweir #endif 88cdf0e10cSrcweir } RemoveFromControl()89cdf0e10cSrcweir void RemoveFromControl() 90cdf0e10cSrcweir { 91cdf0e10cSrcweir #if SC_REFRESHTIMER_CONTROL_LIST 92cdf0e10cSrcweir if ( ppControl && *ppControl ) 93cdf0e10cSrcweir (*ppControl)->aList.Remove( this ); 94cdf0e10cSrcweir #endif 95cdf0e10cSrcweir } 96cdf0e10cSrcweir Start()97cdf0e10cSrcweir void Start() 98cdf0e10cSrcweir { 99cdf0e10cSrcweir if ( GetTimeout() ) 100cdf0e10cSrcweir AutoTimer::Start(); 101cdf0e10cSrcweir } 102cdf0e10cSrcweir 103cdf0e10cSrcweir public: ScRefreshTimer()104cdf0e10cSrcweir ScRefreshTimer() : ppControl(0) 105cdf0e10cSrcweir { SetTimeout( 0 ); } ScRefreshTimer(sal_uLong nSeconds)106cdf0e10cSrcweir ScRefreshTimer( sal_uLong nSeconds ) : ppControl(0) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir SetTimeout( nSeconds * 1000 ); 109cdf0e10cSrcweir Start(); 110cdf0e10cSrcweir } ScRefreshTimer(const ScRefreshTimer & r)111cdf0e10cSrcweir ScRefreshTimer( const ScRefreshTimer& r ) 112cdf0e10cSrcweir : AutoTimer( r ), ppControl(0) 113cdf0e10cSrcweir {} 114cdf0e10cSrcweir virtual ~ScRefreshTimer(); 115cdf0e10cSrcweir operator =(const ScRefreshTimer & r)116cdf0e10cSrcweir ScRefreshTimer& operator=( const ScRefreshTimer& r ) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir SetRefreshControl(0); 119cdf0e10cSrcweir AutoTimer::operator=( r ); 120cdf0e10cSrcweir return *this; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir operator ==(const ScRefreshTimer & r) const123cdf0e10cSrcweir sal_Bool operator==( const ScRefreshTimer& r ) const 124cdf0e10cSrcweir { return GetTimeout() == r.GetTimeout(); } 125cdf0e10cSrcweir operator !=(const ScRefreshTimer & r) const126cdf0e10cSrcweir sal_Bool operator!=( const ScRefreshTimer& r ) const 127cdf0e10cSrcweir { return !ScRefreshTimer::operator==( r ); } 128cdf0e10cSrcweir StartRefreshTimer()129cdf0e10cSrcweir void StartRefreshTimer() 130cdf0e10cSrcweir { Start(); } 131cdf0e10cSrcweir SetRefreshControl(ScRefreshTimerControl * const * pp)132cdf0e10cSrcweir void SetRefreshControl( ScRefreshTimerControl * const * pp ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir RemoveFromControl(); 135cdf0e10cSrcweir ppControl = pp; 136cdf0e10cSrcweir AppendToControl(); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir SetRefreshHandler(const Link & rLink)139cdf0e10cSrcweir void SetRefreshHandler( const Link& rLink ) 140cdf0e10cSrcweir { SetTimeoutHdl( rLink ); } 141cdf0e10cSrcweir GetRefreshDelay() const142cdf0e10cSrcweir sal_uLong GetRefreshDelay() const 143cdf0e10cSrcweir { return GetTimeout() / 1000; } 144cdf0e10cSrcweir StopRefreshTimer()145cdf0e10cSrcweir void StopRefreshTimer() 146cdf0e10cSrcweir { Stop(); } 147cdf0e10cSrcweir 148cdf0e10cSrcweir SC_DLLPUBLIC virtual void SetRefreshDelay( sal_uLong nSeconds ); 149cdf0e10cSrcweir SC_DLLPUBLIC virtual void Timeout(); 150cdf0e10cSrcweir }; 151cdf0e10cSrcweir 152cdf0e10cSrcweir 153cdf0e10cSrcweir #endif // SC_REFRESHTIMER_HXX 154cdf0e10cSrcweir 155