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