]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: timer.cpp | |
3 | // Purpose: wxTimer implementation | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/timer.h" | |
15 | ||
16 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) | |
17 | ||
18 | #ifdef __WXMAC__ | |
19 | #include "wx/mac/private.h" | |
20 | #endif | |
21 | #ifndef __DARWIN__ | |
22 | #include <Timer.h> | |
23 | #endif | |
24 | ||
25 | #include "wx/dynarray.h" | |
26 | ||
27 | #define wxMAC_USE_CARBON_TIMER 1 | |
28 | ||
29 | #if wxMAC_USE_CARBON_TIMER | |
30 | ||
31 | struct MacTimerInfo | |
32 | { | |
33 | wxTimer* m_timer ; | |
34 | EventLoopTimerUPP m_proc ; | |
35 | EventLoopTimerRef m_timerRef ; | |
36 | } ; | |
37 | ||
38 | static pascal void wxProcessTimer( EventLoopTimerRef theTimer , void *data ) ; | |
39 | static pascal void wxProcessTimer( EventLoopTimerRef theTimer , void *data ) | |
40 | { | |
41 | if ( !data ) | |
42 | return ; | |
43 | ||
44 | wxTimer* timer = (wxTimer*) data ; | |
45 | ||
46 | if ( timer->IsOneShot() ) | |
47 | timer->Stop() ; | |
48 | ||
49 | timer->Notify(); | |
50 | } | |
51 | ||
52 | void wxTimer::Init() | |
53 | { | |
54 | m_info = new MacTimerInfo() ; | |
55 | m_info->m_timer = this ; | |
56 | m_info->m_proc = NULL ; | |
57 | m_info->m_timerRef = kInvalidID ; | |
58 | } | |
59 | ||
60 | bool wxTimer::IsRunning() const | |
61 | { | |
62 | return ( m_info->m_timerRef != kInvalidID ) ; | |
63 | } | |
64 | ||
65 | wxTimer::~wxTimer() | |
66 | { | |
67 | Stop(); | |
68 | if (m_info != NULL) { | |
69 | delete m_info ; | |
70 | m_info = NULL ; | |
71 | } | |
72 | } | |
73 | ||
74 | bool wxTimer::Start(int milliseconds,bool mode) | |
75 | { | |
76 | (void)wxTimerBase::Start(milliseconds, mode); | |
77 | ||
78 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") ); | |
79 | wxCHECK_MSG( m_info->m_timerRef == NULL , false, wxT("attempting to restart a timer") ); | |
80 | ||
81 | m_info->m_timer = this ; | |
82 | m_info->m_proc = NewEventLoopTimerUPP( &wxProcessTimer); | |
83 | verify_noerr( InstallEventLoopTimer ( | |
84 | GetMainEventLoop() , | |
85 | m_milli*kEventDurationMillisecond, | |
86 | IsOneShot() ? 0 : m_milli*kEventDurationMillisecond , | |
87 | m_info->m_proc, | |
88 | this, | |
89 | &m_info->m_timerRef) ) ; | |
90 | return true; | |
91 | } | |
92 | ||
93 | void wxTimer::Stop() | |
94 | { | |
95 | if (m_info->m_timerRef) | |
96 | RemoveEventLoopTimer( m_info->m_timerRef ) ; | |
97 | if (m_info->m_proc) | |
98 | DisposeEventLoopTimerUPP(m_info->m_proc) ; | |
99 | m_info->m_proc = NULL ; | |
100 | m_info->m_timerRef = kInvalidID ; | |
101 | } | |
102 | ||
103 | #else | |
104 | ||
105 | typedef struct MacTimerInfo | |
106 | { | |
107 | TMTask m_task; | |
108 | wxMacNotifierTableRef m_table ; | |
109 | wxTimer* m_timer ; | |
110 | } ; | |
111 | ||
112 | static void wxProcessTimer( unsigned long event , void *data ) ; | |
113 | ||
114 | static pascal void MacTimerProc( TMTask * t ) | |
115 | { | |
116 | MacTimerInfo * tm = (MacTimerInfo*) t ; | |
117 | wxMacAddEvent( tm->m_table , wxProcessTimer, 0 , (void*) tm->m_timer , TRUE ) ; | |
118 | } | |
119 | ||
120 | // we need this array to track timers that are being deleted within the Notify procedure | |
121 | // adding the timer before the Notify call and checking after whether it still is in there | |
122 | // as the destructor would have removed it from the array | |
123 | ||
124 | wxArrayPtrVoid gTimersInProcess ; | |
125 | ||
126 | static void wxProcessTimer( unsigned long event , void *data ) | |
127 | { | |
128 | if ( !data ) | |
129 | return ; | |
130 | ||
131 | wxTimer* timer = (wxTimer*) data ; | |
132 | ||
133 | if ( timer->IsOneShot() ) | |
134 | timer->Stop() ; | |
135 | ||
136 | gTimersInProcess.Add( timer ) ; | |
137 | ||
138 | timer->Notify(); | |
139 | ||
140 | int index = gTimersInProcess.Index( timer ) ; | |
141 | ||
142 | if ( index != wxNOT_FOUND ) | |
143 | { | |
144 | gTimersInProcess.RemoveAt( index ) ; | |
145 | ||
146 | if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr ) | |
147 | { | |
148 | PrimeTime( (QElemPtr) &timer->m_info->m_task , timer->GetInterval() ) ; | |
149 | } | |
150 | ||
151 | } | |
152 | } | |
153 | ||
154 | void wxTimer::Init() | |
155 | { | |
156 | m_info = new MacTimerInfo() ; | |
157 | m_info->m_task.tmAddr = NULL ; | |
158 | m_info->m_task.tmWakeUp = 0 ; | |
159 | m_info->m_task.tmReserved = 0 ; | |
160 | m_info->m_task.qType = 0 ; | |
161 | m_info->m_table = wxMacGetNotifierTable() ; | |
162 | m_info->m_timer = this ; | |
163 | } | |
164 | ||
165 | bool wxTimer::IsRunning() const | |
166 | { | |
167 | // as the qType may already indicate it is elapsed, but it | |
168 | // was not handled internally yet | |
169 | return ( m_info->m_task.tmAddr != NULL ) ; | |
170 | } | |
171 | ||
172 | wxTimer::~wxTimer() | |
173 | { | |
174 | Stop(); | |
175 | if (m_info != NULL) { | |
176 | delete m_info ; | |
177 | m_info = NULL ; | |
178 | } | |
179 | int index = gTimersInProcess.Index( this ) ; | |
180 | if ( index != wxNOT_FOUND ) | |
181 | gTimersInProcess.RemoveAt( index ) ; | |
182 | } | |
183 | ||
184 | bool wxTimer::Start(int milliseconds,bool mode) | |
185 | { | |
186 | (void)wxTimerBase::Start(milliseconds, mode); | |
187 | ||
188 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") ); | |
189 | wxCHECK_MSG( m_info->m_task.tmAddr == NULL , false, wxT("attempting to restart a timer") ); | |
190 | ||
191 | m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc ) ; | |
192 | m_info->m_task.tmWakeUp = 0 ; | |
193 | m_info->m_task.tmReserved = 0 ; | |
194 | m_info->m_task.qType = 0 ; | |
195 | m_info->m_timer = this ; | |
196 | InsXTime((QElemPtr) &m_info->m_task ) ; | |
197 | PrimeTime( (QElemPtr) &m_info->m_task , m_milli ) ; | |
198 | return true; | |
199 | } | |
200 | ||
201 | void wxTimer::Stop() | |
202 | { | |
203 | if ( m_info->m_task.tmAddr ) | |
204 | { | |
205 | RmvTime( (QElemPtr) &m_info->m_task ) ; | |
206 | DisposeTimerUPP(m_info->m_task.tmAddr) ; | |
207 | m_info->m_task.tmAddr = NULL ; | |
208 | } | |
209 | wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ; | |
210 | } | |
211 | ||
212 | #endif | |
213 | ||
214 |