]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/timer.cpp
If using the system Window menu on OS X, integrate any Window wxMenu items into the...
[wxWidgets.git] / src / mac / carbon / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sec/mac/carbon/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 #if wxUSE_TIMER
15
16 #ifndef WX_PRECOMP
17 #include "wx/dynarray.h"
18 #endif
19
20 #include "wx/timer.h"
21
22 #ifdef __WXMAC__
23 #include "wx/mac/private.h"
24 #endif
25
26 #ifndef __DARWIN__
27 #include <Timer.h>
28 #endif
29
30 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
31
32 #define wxMAC_USE_CARBON_TIMER 1
33
34 #if wxMAC_USE_CARBON_TIMER
35
36 struct MacTimerInfo
37 {
38 wxTimer* m_timer;
39 EventLoopTimerUPP m_proc;
40 EventLoopTimerRef m_timerRef;
41 };
42
43 static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data );
44 static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data )
45 {
46 if ( data == NULL )
47 return;
48
49 wxTimer* timer = (wxTimer*)data;
50
51 if ( timer->IsOneShot() )
52 timer->Stop();
53
54 timer->Notify();
55 }
56
57 void wxTimer::Init()
58 {
59 m_info = new MacTimerInfo();
60 m_info->m_timer = this;
61 m_info->m_proc = NULL;
62 m_info->m_timerRef = kInvalidID;
63 }
64
65 bool wxTimer::IsRunning() const
66 {
67 return ( m_info->m_timerRef != kInvalidID );
68 }
69
70 wxTimer::~wxTimer()
71 {
72 Stop();
73 if (m_info != NULL)
74 {
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
90 OSStatus err = InstallEventLoopTimer(
91 GetMainEventLoop(),
92 m_milli*kEventDurationMillisecond,
93 IsOneShot() ? 0 : m_milli * kEventDurationMillisecond,
94 m_info->m_proc,
95 this,
96 &m_info->m_timerRef );
97 verify_noerr( err );
98
99 return true;
100 }
101
102 void wxTimer::Stop()
103 {
104 if (m_info->m_timerRef)
105 RemoveEventLoopTimer( m_info->m_timerRef );
106 if (m_info->m_proc)
107 DisposeEventLoopTimerUPP( m_info->m_proc );
108
109 m_info->m_proc = NULL;
110 m_info->m_timerRef = kInvalidID;
111 }
112
113 #else
114
115 typedef struct MacTimerInfo
116 {
117 TMTask m_task;
118 wxMacNotifierTableRef m_table;
119 wxTimer* m_timer;
120 };
121
122 static void wxProcessTimer( unsigned long event, void *data );
123
124 static pascal void MacTimerProc( TMTask * t )
125 {
126 MacTimerInfo * tm = (MacTimerInfo*) t;
127 wxMacAddEvent( tm->m_table, wxProcessTimer, 0, (void*) tm->m_timer, true );
128 }
129
130 // we need this array to track timers that are being deleted within the Notify procedure
131 // adding the timer before the Notify call and checking after whether it still is in there
132 // as the destructor would have removed it from the array
133
134 wxArrayPtrVoid gTimersInProcess;
135
136 static void wxProcessTimer( unsigned long event, void *data )
137 {
138 if ( data == NULL )
139 return;
140
141 wxTimer* timer = (wxTimer*) data;
142 if ( timer->IsOneShot() )
143 timer->Stop();
144
145 gTimersInProcess.Add( timer );
146 timer->Notify();
147
148 int index = gTimersInProcess.Index( timer );
149 if ( index != wxNOT_FOUND )
150 {
151 gTimersInProcess.RemoveAt( index );
152
153 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
154 PrimeTime( (QElemPtr) &timer->m_info->m_task, timer->GetInterval() );
155 }
156 }
157
158 void wxTimer::Init()
159 {
160 m_info = new MacTimerInfo();
161 m_info->m_task.tmAddr = NULL;
162 m_info->m_task.tmWakeUp = 0;
163 m_info->m_task.tmReserved = 0;
164 m_info->m_task.qType = 0;
165 m_info->m_table = wxMacGetNotifierTable();
166 m_info->m_timer = this;
167 }
168
169 bool wxTimer::IsRunning() const
170 {
171 // as the qType may already indicate it is elapsed, but it
172 // was not handled internally yet
173 return ( m_info->m_task.tmAddr != NULL );
174 }
175
176 wxTimer::~wxTimer()
177 {
178 Stop();
179 if (m_info != NULL)
180 {
181 delete m_info;
182 m_info = NULL;
183 }
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
205 return true;
206 }
207
208 void wxTimer::Stop()
209 {
210 if ( m_info->m_task.tmAddr )
211 {
212 RmvTime( (QElemPtr) &m_info->m_task );
213 DisposeTimerUPP( m_info->m_task.tmAddr );
214 m_info->m_task.tmAddr = NULL;
215 }
216
217 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable(), this );
218 }
219
220 #endif // wxMAC_USE_CARBON_TIMER
221
222 #endif // wxUSE_TIMER
223