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