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