1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef SC_REFRESHTIMER_HXX 29 #define SC_REFRESHTIMER_HXX 30 31 #include <tools/list.hxx> 32 #include <vcl/timer.hxx> 33 #include <vos/mutex.hxx> 34 #include <scdllapi.h> 35 36 #define SC_REFRESHTIMER_CONTROL_LIST 0 37 #if SC_REFRESHTIMER_CONTROL_LIST 38 class ScRefreshTimer; 39 DECLARE_LIST( ScRefreshTimerList, ScRefreshTimer* ) 40 #endif 41 42 class ScRefreshTimerControl 43 { 44 private: 45 ::vos::OMutex aMutex; 46 sal_uInt16 nBlockRefresh; 47 48 public: 49 #if SC_REFRESHTIMER_CONTROL_LIST 50 ScRefreshTimerList aList; 51 #endif 52 53 ScRefreshTimerControl() : nBlockRefresh(0) {} 54 55 void SetAllowRefresh( sal_Bool b ) 56 { 57 if ( b && nBlockRefresh ) 58 --nBlockRefresh; 59 else if ( !b && nBlockRefresh < (sal_uInt16)(~0) ) 60 ++nBlockRefresh; 61 } 62 sal_Bool IsRefreshAllowed() const { return !nBlockRefresh; } 63 ::vos::OMutex& GetMutex() { return aMutex; } 64 }; 65 66 67 class ScRefreshTimerProtector 68 { 69 private: 70 ScRefreshTimerControl * const * ppControl; 71 public: 72 ScRefreshTimerProtector( ScRefreshTimerControl * const * pp ); 73 ~ScRefreshTimerProtector() 74 { 75 if ( ppControl && *ppControl ) 76 (*ppControl)->SetAllowRefresh( sal_True ); 77 } 78 }; 79 80 81 class ScRefreshTimer : public AutoTimer 82 { 83 private: 84 ScRefreshTimerControl * const * ppControl; 85 86 void AppendToControl() 87 { 88 #if SC_REFRESHTIMER_CONTROL_LIST 89 if ( ppControl && *ppControl ) 90 (*ppControl)->aList.Insert( this, LIST_APPEND ); 91 #endif 92 } 93 void RemoveFromControl() 94 { 95 #if SC_REFRESHTIMER_CONTROL_LIST 96 if ( ppControl && *ppControl ) 97 (*ppControl)->aList.Remove( this ); 98 #endif 99 } 100 101 void Start() 102 { 103 if ( GetTimeout() ) 104 AutoTimer::Start(); 105 } 106 107 public: 108 ScRefreshTimer() : ppControl(0) 109 { SetTimeout( 0 ); } 110 ScRefreshTimer( sal_uLong nSeconds ) : ppControl(0) 111 { 112 SetTimeout( nSeconds * 1000 ); 113 Start(); 114 } 115 ScRefreshTimer( const ScRefreshTimer& r ) 116 : AutoTimer( r ), ppControl(0) 117 {} 118 virtual ~ScRefreshTimer(); 119 120 ScRefreshTimer& operator=( const ScRefreshTimer& r ) 121 { 122 SetRefreshControl(0); 123 AutoTimer::operator=( r ); 124 return *this; 125 } 126 127 sal_Bool operator==( const ScRefreshTimer& r ) const 128 { return GetTimeout() == r.GetTimeout(); } 129 130 sal_Bool operator!=( const ScRefreshTimer& r ) const 131 { return !ScRefreshTimer::operator==( r ); } 132 133 void StartRefreshTimer() 134 { Start(); } 135 136 void SetRefreshControl( ScRefreshTimerControl * const * pp ) 137 { 138 RemoveFromControl(); 139 ppControl = pp; 140 AppendToControl(); 141 } 142 143 void SetRefreshHandler( const Link& rLink ) 144 { SetTimeoutHdl( rLink ); } 145 146 sal_uLong GetRefreshDelay() const 147 { return GetTimeout() / 1000; } 148 149 void StopRefreshTimer() 150 { Stop(); } 151 152 SC_DLLPUBLIC virtual void SetRefreshDelay( sal_uLong nSeconds ); 153 SC_DLLPUBLIC virtual void Timeout(); 154 }; 155 156 157 #endif // SC_REFRESHTIMER_HXX 158 159