]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/timer.cpp
fixed wxGetTranslation() prototype in the !wxUSE_INTL case
[wxWidgets.git] / src / mac / carbon / timer.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
0ec80ebe 2// Name: sec/mac/carbon/timer.cpp
e9576ca5 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
ad9835c9 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
72c1ba98
VZ
14#if wxUSE_TIMER
15
ad9835c9
WS
16#ifndef WX_PRECOMP
17 #include "wx/dynarray.h"
18#endif
19
e9576ca5
SC
20#include "wx/timer.h"
21
76a5e5d2 22#ifdef __WXMAC__
ad9835c9 23 #include "wx/mac/private.h"
76a5e5d2 24#endif
0ec80ebe 25
66a09d47 26#ifndef __DARWIN__
ad9835c9 27 #include <Timer.h>
66a09d47 28#endif
76a5e5d2 29
0ec80ebe 30IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
e5f0419b 31
514982b2
SC
32#define wxMAC_USE_CARBON_TIMER 1
33
34#if wxMAC_USE_CARBON_TIMER
35
d0ee33f5 36struct MacTimerInfo
514982b2 37{
0ec80ebe
DS
38 wxTimer* m_timer;
39 EventLoopTimerUPP m_proc;
40 EventLoopTimerRef m_timerRef;
41};
514982b2 42
0ec80ebe
DS
43static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data );
44static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data )
514982b2 45{
0ec80ebe
DS
46 if ( data == NULL )
47 return;
d0ee33f5 48
0ec80ebe 49 wxTimer* timer = (wxTimer*)data;
d0ee33f5 50
514982b2 51 if ( timer->IsOneShot() )
0ec80ebe 52 timer->Stop();
514982b2
SC
53
54 timer->Notify();
55}
56
57void wxTimer::Init()
58{
0ec80ebe
DS
59 m_info = new MacTimerInfo();
60 m_info->m_timer = this;
61 m_info->m_proc = NULL;
62 m_info->m_timerRef = kInvalidID;
514982b2
SC
63}
64
d0ee33f5 65bool wxTimer::IsRunning() const
514982b2 66{
0ec80ebe 67 return ( m_info->m_timerRef != kInvalidID );
514982b2
SC
68}
69
70wxTimer::~wxTimer()
71{
72 Stop();
0ec80ebe
DS
73 if (m_info != NULL)
74 {
75 delete m_info;
76 m_info = NULL;
514982b2
SC
77 }
78}
79
0ec80ebe 80bool wxTimer::Start( int milliseconds, bool mode )
514982b2
SC
81{
82 (void)wxTimerBase::Start(milliseconds, mode);
83
d0ee33f5 84 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
0ec80ebe
DS
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 );
514982b2 89
0ec80ebe
DS
90 OSStatus err = InstallEventLoopTimer(
91 GetMainEventLoop(),
514982b2 92 m_milli*kEventDurationMillisecond,
0ec80ebe 93 IsOneShot() ? 0 : m_milli * kEventDurationMillisecond,
514982b2
SC
94 m_info->m_proc,
95 this,
0ec80ebe
DS
96 &m_info->m_timerRef );
97 verify_noerr( err );
98
d0ee33f5 99 return true;
514982b2
SC
100}
101
102void wxTimer::Stop()
103{
104 if (m_info->m_timerRef)
0ec80ebe 105 RemoveEventLoopTimer( m_info->m_timerRef );
514982b2 106 if (m_info->m_proc)
0ec80ebe
DS
107 DisposeEventLoopTimerUPP( m_info->m_proc );
108
109 m_info->m_proc = NULL;
110 m_info->m_timerRef = kInvalidID;
514982b2
SC
111}
112
113#else
114
76a5e5d2
SC
115typedef struct MacTimerInfo
116{
117 TMTask m_task;
0ec80ebe
DS
118 wxMacNotifierTableRef m_table;
119 wxTimer* m_timer;
120};
76a5e5d2 121
0ec80ebe 122static void wxProcessTimer( unsigned long event, void *data );
2f1ae414 123
d015713e 124static pascal void MacTimerProc( TMTask * t )
2f1ae414 125{
0ec80ebe
DS
126 MacTimerInfo * tm = (MacTimerInfo*) t;
127 wxMacAddEvent( tm->m_table, wxProcessTimer, 0, (void*) tm->m_timer, true );
2f1ae414
SC
128}
129
604b66c1 130// we need this array to track timers that are being deleted within the Notify procedure
d0ee33f5 131// adding the timer before the Notify call and checking after whether it still is in there
604b66c1
SC
132// as the destructor would have removed it from the array
133
0ec80ebe 134wxArrayPtrVoid gTimersInProcess;
e5f0419b 135
0ec80ebe 136static void wxProcessTimer( unsigned long event, void *data )
2f1ae414 137{
0ec80ebe
DS
138 if ( data == NULL )
139 return;
d0ee33f5 140
0ec80ebe 141 wxTimer* timer = (wxTimer*) data;
e40298d5 142 if ( timer->IsOneShot() )
0ec80ebe 143 timer->Stop();
d0ee33f5 144
0ec80ebe 145 gTimersInProcess.Add( timer );
2f1ae414
SC
146 timer->Notify();
147
0ec80ebe 148 int index = gTimersInProcess.Index( timer );
e5f0419b 149 if ( index != wxNOT_FOUND )
2f1ae414 150 {
0ec80ebe 151 gTimersInProcess.RemoveAt( index );
d0ee33f5 152
e5f0419b 153 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
0ec80ebe 154 PrimeTime( (QElemPtr) &timer->m_info->m_task, timer->GetInterval() );
2f1ae414
SC
155 }
156}
e9576ca5 157
d015713e 158void wxTimer::Init()
e9576ca5 159{
0ec80ebe
DS
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;
2f1ae414
SC
167}
168
d0ee33f5 169bool wxTimer::IsRunning() const
2f1ae414 170{
604b66c1
SC
171 // as the qType may already indicate it is elapsed, but it
172 // was not handled internally yet
0ec80ebe 173 return ( m_info->m_task.tmAddr != NULL );
e9576ca5
SC
174}
175
176wxTimer::~wxTimer()
177{
178 Stop();
0ec80ebe
DS
179 if (m_info != NULL)
180 {
181 delete m_info;
182 m_info = NULL;
f5bb2251 183 }
0ec80ebe
DS
184
185 int index = gTimersInProcess.Index( this );
e5f0419b 186 if ( index != wxNOT_FOUND )
0ec80ebe 187 gTimersInProcess.RemoveAt( index );
e9576ca5
SC
188}
189
0ec80ebe 190bool wxTimer::Start( int milliseconds, bool mode )
e9576ca5 191{
0ec80ebe 192 (void)wxTimerBase::Start( milliseconds, mode );
e9576ca5 193
d0ee33f5 194 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
0ec80ebe
DS
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
d0ee33f5 205 return true;
e9576ca5
SC
206}
207
208void wxTimer::Stop()
209{
76a5e5d2 210 if ( m_info->m_task.tmAddr )
2f1ae414 211 {
0ec80ebe
DS
212 RmvTime( (QElemPtr) &m_info->m_task );
213 DisposeTimerUPP( m_info->m_task.tmAddr );
214 m_info->m_task.tmAddr = NULL;
2f1ae414 215 }
0ec80ebe
DS
216
217 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable(), this );
e9576ca5
SC
218}
219
72c1ba98
VZ
220#endif // wxMAC_USE_CARBON_TIMER
221
222#endif // wxUSE_TIMER
223