]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/timer.cpp
Back to previous call with -1 replacement.
[wxWidgets.git] / src / mac / classic / timer.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
ad9835c9 2// Name: src/mac/classic/timer.cpp
2646f485
SC
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
ad9835c9 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
ad9835c9
WS
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
c0badb70
WS
15#include "wx/timer.h"
16
ad9835c9
WS
17#ifndef WX_PRECOMP
18 #include "wx/dynarray.h"
19#endif
20
313feadc 21IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
2646f485
SC
22
23#ifdef __WXMAC__
24#include "wx/mac/private.h"
25#endif
26#ifndef __DARWIN__
27#include <Timer.h>
28#endif
29
2646f485
SC
30typedef struct MacTimerInfo
31{
32 TMTask m_task;
33 wxMacNotifierTableRef m_table ;
34 wxTimer* m_timer ;
35} ;
36
37static void wxProcessTimer( unsigned long event , void *data ) ;
38
39static pascal void MacTimerProc( TMTask * t )
40{
41 MacTimerInfo * tm = (MacTimerInfo*) t ;
42 wxMacAddEvent( tm->m_table , wxProcessTimer, 0 , (void*) tm->m_timer , TRUE ) ;
43}
44
45// we need this array to track timers that are being deleted within the Notify procedure
ad9835c9 46// adding the timer before the Notify call and checking after whether it still is in there
2646f485
SC
47// as the destructor would have removed it from the array
48
49wxArrayPtrVoid gTimersInProcess ;
50
51static void wxProcessTimer( unsigned long event , void *data )
52{
53 if ( !data )
54 return ;
ad9835c9 55
2646f485 56 wxTimer* timer = (wxTimer*) data ;
ad9835c9 57
2646f485
SC
58 if ( timer->IsOneShot() )
59 timer->Stop() ;
ad9835c9 60
2646f485 61 gTimersInProcess.Add( timer ) ;
ad9835c9 62
2646f485
SC
63 timer->Notify();
64
65 int index = gTimersInProcess.Index( timer ) ;
ad9835c9 66
2646f485
SC
67 if ( index != wxNOT_FOUND )
68 {
69 gTimersInProcess.RemoveAt( index ) ;
ad9835c9 70
2646f485
SC
71 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
72 {
73 PrimeTime( (QElemPtr) &timer->m_info->m_task , timer->GetInterval() ) ;
74 }
2646f485
SC
75 }
76}
77
78void wxTimer::Init()
79{
80 m_info = new MacTimerInfo() ;
81 m_info->m_task.tmAddr = NULL ;
82 m_info->m_task.tmWakeUp = 0 ;
83 m_info->m_task.tmReserved = 0 ;
84 m_info->m_task.qType = 0 ;
85 m_info->m_table = wxMacGetNotifierTable() ;
86 m_info->m_timer = this ;
87}
88
ad9835c9 89bool wxTimer::IsRunning() const
2646f485
SC
90{
91 // as the qType may already indicate it is elapsed, but it
92 // was not handled internally yet
93 return ( m_info->m_task.tmAddr != NULL ) ;
94}
95
96wxTimer::~wxTimer()
97{
98 Stop();
99 if (m_info != NULL) {
100 delete m_info ;
101 m_info = NULL ;
102 }
103 int index = gTimersInProcess.Index( this ) ;
104 if ( index != wxNOT_FOUND )
ad9835c9 105 gTimersInProcess.RemoveAt( index ) ;
2646f485
SC
106}
107
108bool wxTimer::Start(int milliseconds,bool mode)
109{
110 (void)wxTimerBase::Start(milliseconds, mode);
111
ad9835c9
WS
112 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
113 wxCHECK_MSG( m_info->m_task.tmAddr == NULL , false, wxT("attempting to restart a timer") );
2646f485
SC
114
115#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
116 m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc ) ;
117#else
118 m_info->m_task.tmAddr = NewTimerProc( MacTimerProc ) ;
119#endif
120 m_info->m_task.tmWakeUp = 0 ;
121 m_info->m_task.tmReserved = 0 ;
122 m_info->m_task.qType = 0 ;
123 m_info->m_timer = this ;
124 InsXTime((QElemPtr) &m_info->m_task ) ;
125 PrimeTime( (QElemPtr) &m_info->m_task , m_milli ) ;
ad9835c9 126 return true;
2646f485
SC
127}
128
129void wxTimer::Stop()
130{
131 if ( m_info->m_task.tmAddr )
132 {
133 RmvTime( (QElemPtr) &m_info->m_task ) ;
134 DisposeTimerUPP(m_info->m_task.tmAddr) ;
135 m_info->m_task.tmAddr = NULL ;
136 }
137 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ;
138}