]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/timer.cpp
f7eb84297f7f538df10627d53ad6800d72ab43e5
[wxWidgets.git] / src / mac / classic / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "timer.h"
14 #endif
15
16 #include "wx/timer.h"
17
18 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
19
20 #ifdef __WXMAC__
21 #include "wx/mac/private.h"
22 #endif
23 #ifndef __DARWIN__
24 #include <Timer.h>
25 #endif
26
27 #include "wx/dynarray.h"
28
29 typedef struct MacTimerInfo
30 {
31 TMTask m_task;
32 wxMacNotifierTableRef m_table ;
33 wxTimer* m_timer ;
34 } ;
35
36 static void wxProcessTimer( unsigned long event , void *data ) ;
37
38 static pascal void MacTimerProc( TMTask * t )
39 {
40 MacTimerInfo * tm = (MacTimerInfo*) t ;
41 wxMacAddEvent( tm->m_table , wxProcessTimer, 0 , (void*) tm->m_timer , TRUE ) ;
42 }
43
44 // we need this array to track timers that are being deleted within the Notify procedure
45 // adding the timer before the Notify call and checking after whether it still is in there
46 // as the destructor would have removed it from the array
47
48 wxArrayPtrVoid gTimersInProcess ;
49
50 static void wxProcessTimer( unsigned long event , void *data )
51 {
52 if ( !data )
53 return ;
54
55 wxTimer* timer = (wxTimer*) data ;
56
57 if ( timer->IsOneShot() )
58 timer->Stop() ;
59
60 gTimersInProcess.Add( timer ) ;
61
62 timer->Notify();
63
64 int index = gTimersInProcess.Index( timer ) ;
65
66 if ( index != wxNOT_FOUND )
67 {
68 gTimersInProcess.RemoveAt( index ) ;
69
70 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
71 {
72 PrimeTime( (QElemPtr) &timer->m_info->m_task , timer->GetInterval() ) ;
73 }
74
75 }
76 }
77
78 void 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
89 bool wxTimer::IsRunning() const
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
96 wxTimer::~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 )
105 gTimersInProcess.RemoveAt( index ) ;
106 }
107
108 bool wxTimer::Start(int milliseconds,bool mode)
109 {
110 (void)wxTimerBase::Start(milliseconds, mode);
111
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") );
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 ) ;
126 return TRUE;
127 }
128
129 void 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 }
139
140
141