]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: timer.cpp | |
3 | // Purpose: wxTimer implementation | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 SC |
12 | #include "wx/wxprec.h" |
13 | ||
e9576ca5 SC |
14 | #include "wx/timer.h" |
15 | ||
313feadc | 16 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) |
2f1ae414 | 17 | |
76a5e5d2 SC |
18 | #ifdef __WXMAC__ |
19 | #include "wx/mac/private.h" | |
20 | #endif | |
66a09d47 SC |
21 | #ifndef __DARWIN__ |
22 | #include <Timer.h> | |
23 | #endif | |
76a5e5d2 | 24 | |
e5f0419b SC |
25 | #include "wx/dynarray.h" |
26 | ||
514982b2 SC |
27 | #define wxMAC_USE_CARBON_TIMER 1 |
28 | ||
29 | #if wxMAC_USE_CARBON_TIMER | |
30 | ||
d0ee33f5 | 31 | struct MacTimerInfo |
514982b2 SC |
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 ; | |
d0ee33f5 | 43 | |
514982b2 | 44 | wxTimer* timer = (wxTimer*) data ; |
d0ee33f5 | 45 | |
514982b2 SC |
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 | ||
d0ee33f5 | 60 | bool wxTimer::IsRunning() const |
514982b2 SC |
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 | ||
d0ee33f5 WS |
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") ); | |
514982b2 SC |
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) ) ; | |
d0ee33f5 | 90 | return true; |
514982b2 SC |
91 | } |
92 | ||
93 | void wxTimer::Stop() | |
94 | { | |
95 | if (m_info->m_timerRef) | |
d0ee33f5 | 96 | RemoveEventLoopTimer( m_info->m_timerRef ) ; |
514982b2 SC |
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 | ||
76a5e5d2 SC |
105 | typedef struct MacTimerInfo |
106 | { | |
107 | TMTask m_task; | |
108 | wxMacNotifierTableRef m_table ; | |
109 | wxTimer* m_timer ; | |
110 | } ; | |
111 | ||
2f1ae414 SC |
112 | static void wxProcessTimer( unsigned long event , void *data ) ; |
113 | ||
d015713e | 114 | static pascal void MacTimerProc( TMTask * t ) |
2f1ae414 | 115 | { |
e40298d5 JS |
116 | MacTimerInfo * tm = (MacTimerInfo*) t ; |
117 | wxMacAddEvent( tm->m_table , wxProcessTimer, 0 , (void*) tm->m_timer , TRUE ) ; | |
2f1ae414 SC |
118 | } |
119 | ||
604b66c1 | 120 | // we need this array to track timers that are being deleted within the Notify procedure |
d0ee33f5 | 121 | // adding the timer before the Notify call and checking after whether it still is in there |
604b66c1 SC |
122 | // as the destructor would have removed it from the array |
123 | ||
e5f0419b SC |
124 | wxArrayPtrVoid gTimersInProcess ; |
125 | ||
d015713e | 126 | static void wxProcessTimer( unsigned long event , void *data ) |
2f1ae414 | 127 | { |
e40298d5 JS |
128 | if ( !data ) |
129 | return ; | |
d0ee33f5 | 130 | |
e40298d5 | 131 | wxTimer* timer = (wxTimer*) data ; |
d0ee33f5 | 132 | |
e40298d5 JS |
133 | if ( timer->IsOneShot() ) |
134 | timer->Stop() ; | |
d0ee33f5 | 135 | |
e40298d5 | 136 | gTimersInProcess.Add( timer ) ; |
d0ee33f5 | 137 | |
2f1ae414 SC |
138 | timer->Notify(); |
139 | ||
e5f0419b | 140 | int index = gTimersInProcess.Index( timer ) ; |
d0ee33f5 | 141 | |
e5f0419b | 142 | if ( index != wxNOT_FOUND ) |
2f1ae414 | 143 | { |
e5f0419b | 144 | gTimersInProcess.RemoveAt( index ) ; |
d0ee33f5 | 145 | |
e5f0419b SC |
146 | if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr ) |
147 | { | |
e40298d5 | 148 | PrimeTime( (QElemPtr) &timer->m_info->m_task , timer->GetInterval() ) ; |
e5f0419b | 149 | } |
d0ee33f5 | 150 | |
2f1ae414 SC |
151 | } |
152 | } | |
e9576ca5 | 153 | |
d015713e | 154 | void wxTimer::Init() |
e9576ca5 | 155 | { |
76a5e5d2 | 156 | m_info = new MacTimerInfo() ; |
e40298d5 JS |
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 ; | |
2f1ae414 SC |
163 | } |
164 | ||
d0ee33f5 | 165 | bool wxTimer::IsRunning() const |
2f1ae414 | 166 | { |
604b66c1 SC |
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 ) ; | |
e9576ca5 SC |
170 | } |
171 | ||
172 | wxTimer::~wxTimer() | |
173 | { | |
174 | Stop(); | |
f5bb2251 GD |
175 | if (m_info != NULL) { |
176 | delete m_info ; | |
177 | m_info = NULL ; | |
178 | } | |
e5f0419b SC |
179 | int index = gTimersInProcess.Index( this ) ; |
180 | if ( index != wxNOT_FOUND ) | |
d0ee33f5 | 181 | gTimersInProcess.RemoveAt( index ) ; |
e9576ca5 SC |
182 | } |
183 | ||
184 | bool wxTimer::Start(int milliseconds,bool mode) | |
185 | { | |
2f1ae414 | 186 | (void)wxTimerBase::Start(milliseconds, mode); |
e9576ca5 | 187 | |
d0ee33f5 WS |
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") ); | |
e9576ca5 | 190 | |
76a5e5d2 | 191 | m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc ) ; |
76a5e5d2 SC |
192 | m_info->m_task.tmWakeUp = 0 ; |
193 | m_info->m_task.tmReserved = 0 ; | |
e40298d5 JS |
194 | m_info->m_task.qType = 0 ; |
195 | m_info->m_timer = this ; | |
76a5e5d2 SC |
196 | InsXTime((QElemPtr) &m_info->m_task ) ; |
197 | PrimeTime( (QElemPtr) &m_info->m_task , m_milli ) ; | |
d0ee33f5 | 198 | return true; |
e9576ca5 SC |
199 | } |
200 | ||
201 | void wxTimer::Stop() | |
202 | { | |
76a5e5d2 | 203 | if ( m_info->m_task.tmAddr ) |
2f1ae414 | 204 | { |
e40298d5 JS |
205 | RmvTime( (QElemPtr) &m_info->m_task ) ; |
206 | DisposeTimerUPP(m_info->m_task.tmAddr) ; | |
207 | m_info->m_task.tmAddr = NULL ; | |
2f1ae414 SC |
208 | } |
209 | wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ; | |
e9576ca5 SC |
210 | } |
211 | ||
514982b2 | 212 | #endif |
e9576ca5 | 213 | |
2f1ae414 | 214 |