]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/timer.cpp
Commented out call that breaks metal style setting before Create
[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 #include "wx/timer.h"
15
16 #ifdef __WXMAC__
17 #include "wx/mac/private.h"
18 #endif
19
20 #include "wx/dynarray.h"
21
22 #ifndef __DARWIN__
23 #include <Timer.h>
24 #endif
25
26 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
27
28 #define wxMAC_USE_CARBON_TIMER 1
29
30 #if wxMAC_USE_CARBON_TIMER
31
32 struct MacTimerInfo
33 {
34 wxTimer* m_timer;
35 EventLoopTimerUPP m_proc;
36 EventLoopTimerRef m_timerRef;
37 };
38
39 static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data );
40 static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data )
41 {
42 if ( data == NULL )
43 return;
44
45 wxTimer* timer = (wxTimer*)data;
46
47 if ( timer->IsOneShot() )
48 timer->Stop();
49
50 timer->Notify();
51 }
52
53 void wxTimer::Init()
54 {
55 m_info = new MacTimerInfo();
56 m_info->m_timer = this;
57 m_info->m_proc = NULL;
58 m_info->m_timerRef = kInvalidID;
59 }
60
61 bool wxTimer::IsRunning() const
62 {
63 return ( m_info->m_timerRef != kInvalidID );
64 }
65
66 wxTimer::~wxTimer()
67 {
68 Stop();
69 if (m_info != NULL)
70 {
71 delete m_info;
72 m_info = NULL;
73 }
74 }
75
76 bool wxTimer::Start( int milliseconds, bool mode )
77 {
78 (void)wxTimerBase::Start(milliseconds, mode);
79
80 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
81 wxCHECK_MSG( m_info->m_timerRef == NULL, false, wxT("attempting to restart a timer") );
82
83 m_info->m_timer = this;
84 m_info->m_proc = NewEventLoopTimerUPP( &wxProcessTimer );
85
86 OSStatus err = InstallEventLoopTimer(
87 GetMainEventLoop(),
88 m_milli*kEventDurationMillisecond,
89 IsOneShot() ? 0 : m_milli * kEventDurationMillisecond,
90 m_info->m_proc,
91 this,
92 &m_info->m_timerRef );
93 verify_noerr( err );
94
95 return true;
96 }
97
98 void wxTimer::Stop()
99 {
100 if (m_info->m_timerRef)
101 RemoveEventLoopTimer( m_info->m_timerRef );
102 if (m_info->m_proc)
103 DisposeEventLoopTimerUPP( m_info->m_proc );
104
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 == NULL )
135 return;
136
137 wxTimer* timer = (wxTimer*) data;
138 if ( timer->IsOneShot() )
139 timer->Stop();
140
141 gTimersInProcess.Add( timer );
142 timer->Notify();
143
144 int index = gTimersInProcess.Index( timer );
145 if ( index != wxNOT_FOUND )
146 {
147 gTimersInProcess.RemoveAt( index );
148
149 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
150 PrimeTime( (QElemPtr) &timer->m_info->m_task, timer->GetInterval() );
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 {
177 delete m_info;
178 m_info = NULL;
179 }
180
181 int index = gTimersInProcess.Index( this );
182 if ( index != wxNOT_FOUND )
183 gTimersInProcess.RemoveAt( index );
184 }
185
186 bool wxTimer::Start( int milliseconds, bool mode )
187 {
188 (void)wxTimerBase::Start( milliseconds, mode );
189
190 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
191 wxCHECK_MSG( m_info->m_task.tmAddr == NULL, false, wxT("attempting to restart a timer") );
192
193 m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc );
194 m_info->m_task.tmWakeUp = 0;
195 m_info->m_task.tmReserved = 0;
196 m_info->m_task.qType = 0;
197 m_info->m_timer = this;
198 InsXTime( (QElemPtr) &m_info->m_task );
199 PrimeTime( (QElemPtr) &m_info->m_task, m_milli );
200
201 return true;
202 }
203
204 void wxTimer::Stop()
205 {
206 if ( m_info->m_task.tmAddr )
207 {
208 RmvTime( (QElemPtr) &m_info->m_task );
209 DisposeTimerUPP( m_info->m_task.tmAddr );
210 m_info->m_task.tmAddr = NULL;
211 }
212
213 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable(), this );
214 }
215
216 #endif