]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/timer.cpp
Merged wxRichTextAttr and wxTextAttrEx into wxTextAttr, and added a font table
[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/mac/private/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 #define wxMAC_USE_CARBON_TIMER 1
31
32 #if wxMAC_USE_CARBON_TIMER
33
34 struct MacTimerInfo
35 {
36 wxCarbonTimerImpl* m_timer;
37 EventLoopTimerUPP m_proc;
38 EventLoopTimerRef m_timerRef;
39 };
40
41 static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data )
42 {
43 if ( data == NULL )
44 return;
45
46 wxCarbonTimerImpl* timer = (wxCarbonTimerImpl*)data;
47
48 if ( timer->IsOneShot() )
49 timer->Stop();
50
51 timer->Notify();
52 }
53
54 wxCarbonTimerImpl::wxCarbonTimerImpl(wxTimer *timer)
55 : wxTimerImpl(timer)
56 {
57 m_info = new MacTimerInfo();
58 m_info->m_timer = this;
59 m_info->m_proc = NULL;
60 m_info->m_timerRef = kInvalidID;
61 }
62
63 bool wxCarbonTimerImpl::IsRunning() const
64 {
65 return ( m_info->m_timerRef != kInvalidID );
66 }
67
68 wxCarbonTimerImpl::~wxCarbonTimerImpl()
69 {
70 delete m_info;
71 }
72
73 bool wxCarbonTimerImpl::Start( int milliseconds, bool mode )
74 {
75 (void)wxTimerImpl::Start(milliseconds, mode);
76
77 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
78 wxCHECK_MSG( m_info->m_timerRef == NULL, false, wxT("attempting to restart a timer") );
79
80 m_info->m_timer = this;
81 m_info->m_proc = NewEventLoopTimerUPP( &wxProcessTimer );
82
83 OSStatus err = 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 verify_noerr( err );
91
92 return true;
93 }
94
95 void wxCarbonTimerImpl::Stop()
96 {
97 if (m_info->m_timerRef)
98 RemoveEventLoopTimer( m_info->m_timerRef );
99 if (m_info->m_proc)
100 DisposeEventLoopTimerUPP( m_info->m_proc );
101
102 m_info->m_proc = NULL;
103 m_info->m_timerRef = kInvalidID;
104 }
105
106 #else // !wxMAC_USE_CARBON_TIMER
107
108 typedef struct MacTimerInfo
109 {
110 TMTask m_task;
111 wxMacNotifierTableRef m_table;
112 wxCarbonTimerImpl* m_timer;
113 };
114
115 static void wxProcessTimer( unsigned long event, void *data );
116
117 static pascal void MacTimerProc( TMTask * t )
118 {
119 MacTimerInfo * tm = (MacTimerInfo*) t;
120 wxMacAddEvent( tm->m_table, wxProcessTimer, 0, (void*) tm->m_timer, true );
121 }
122
123 // we need this array to track timers that are being deleted within the Notify procedure
124 // adding the timer before the Notify call and checking after whether it still is in there
125 // as the destructor would have removed it from the array
126
127 wxArrayPtrVoid gTimersInProcess;
128
129 static void wxProcessTimer( unsigned long event, void *data )
130 {
131 if ( data == NULL )
132 return;
133
134 wxCarbonTimerImpl* timer = (wxCarbonTimerImpl*) data;
135 if ( timer->m_oneShot )
136 timer->Stop();
137
138 gTimersInProcess.Add( timer );
139 timer->Notify();
140
141 int index = gTimersInProcess.Index( timer );
142 if ( index != wxNOT_FOUND )
143 {
144 gTimersInProcess.RemoveAt( index );
145
146 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
147 PrimeTime( (QElemPtr) &timer->m_info->m_task, timer->GetInterval() );
148 }
149 }
150
151 void wxCarbonTimerImpl::Init()
152 {
153 m_info = new MacTimerInfo();
154 m_info->m_task.tmAddr = NULL;
155 m_info->m_task.tmWakeUp = 0;
156 m_info->m_task.tmReserved = 0;
157 m_info->m_task.qType = 0;
158 m_info->m_table = wxMacGetNotifierTable();
159 m_info->m_timer = this;
160 }
161
162 bool wxCarbonTimerImpl::IsRunning() const
163 {
164 // as the qType may already indicate it is elapsed, but it
165 // was not handled internally yet
166 return ( m_info->m_task.tmAddr != NULL );
167 }
168
169 wxCarbonTimerImpl::~wxCarbonTimerImpl()
170 {
171 Stop();
172 if (m_info != NULL)
173 {
174 delete m_info;
175 m_info = NULL;
176 }
177
178 int index = gTimersInProcess.Index( this );
179 if ( index != wxNOT_FOUND )
180 gTimersInProcess.RemoveAt( index );
181 }
182
183 bool wxCarbonTimerImpl::Start( int milliseconds, bool mode )
184 {
185 (void)wxTimerBase::Start( milliseconds, mode );
186
187 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
188 wxCHECK_MSG( m_info->m_task.tmAddr == NULL, false, wxT("attempting to restart a timer") );
189
190 m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc );
191 m_info->m_task.tmWakeUp = 0;
192 m_info->m_task.tmReserved = 0;
193 m_info->m_task.qType = 0;
194 m_info->m_timer = this;
195 InsXTime( (QElemPtr) &m_info->m_task );
196 PrimeTime( (QElemPtr) &m_info->m_task, m_milli );
197
198 return true;
199 }
200
201 void wxCarbonTimerImpl::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
210 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable(), this );
211 }
212
213 #endif // wxMAC_USE_CARBON_TIMER/!wxMAC_USE_CARBON_TIMER
214
215 #endif // wxUSE_TIMER
216