]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: timer.cpp | |
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 |
e40298d5 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "timer.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/timer.h" | |
17 | ||
2f1ae414 | 18 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 19 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject) |
2f1ae414 SC |
20 | #endif |
21 | ||
76a5e5d2 SC |
22 | #ifdef __WXMAC__ |
23 | #include "wx/mac/private.h" | |
24 | #endif | |
66a09d47 SC |
25 | #ifndef __DARWIN__ |
26 | #include <Timer.h> | |
27 | #endif | |
76a5e5d2 | 28 | |
e5f0419b SC |
29 | #include "wx/dynarray.h" |
30 | ||
76a5e5d2 SC |
31 | typedef struct MacTimerInfo |
32 | { | |
33 | TMTask m_task; | |
34 | wxMacNotifierTableRef m_table ; | |
35 | wxTimer* m_timer ; | |
36 | } ; | |
37 | ||
2f1ae414 SC |
38 | static void wxProcessTimer( unsigned long event , void *data ) ; |
39 | ||
d015713e | 40 | static pascal void MacTimerProc( TMTask * t ) |
2f1ae414 | 41 | { |
e40298d5 JS |
42 | MacTimerInfo * tm = (MacTimerInfo*) t ; |
43 | wxMacAddEvent( tm->m_table , wxProcessTimer, 0 , (void*) tm->m_timer , TRUE ) ; | |
2f1ae414 SC |
44 | } |
45 | ||
e5f0419b SC |
46 | wxArrayPtrVoid gTimersInProcess ; |
47 | ||
d015713e | 48 | static void wxProcessTimer( unsigned long event , void *data ) |
2f1ae414 | 49 | { |
e40298d5 JS |
50 | if ( !data ) |
51 | return ; | |
52 | ||
53 | wxTimer* timer = (wxTimer*) data ; | |
54 | ||
55 | if ( timer->IsOneShot() ) | |
56 | timer->Stop() ; | |
57 | ||
58 | gTimersInProcess.Add( timer ) ; | |
59 | ||
2f1ae414 SC |
60 | timer->Notify(); |
61 | ||
e5f0419b SC |
62 | int index = gTimersInProcess.Index( timer ) ; |
63 | ||
64 | if ( index != wxNOT_FOUND ) | |
2f1ae414 | 65 | { |
e5f0419b SC |
66 | gTimersInProcess.RemoveAt( index ) ; |
67 | ||
68 | if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr ) | |
69 | { | |
e40298d5 | 70 | PrimeTime( (QElemPtr) &timer->m_info->m_task , timer->GetInterval() ) ; |
e5f0419b SC |
71 | } |
72 | ||
2f1ae414 SC |
73 | } |
74 | } | |
e9576ca5 | 75 | |
d015713e | 76 | void wxTimer::Init() |
e9576ca5 | 77 | { |
76a5e5d2 | 78 | m_info = new MacTimerInfo() ; |
e40298d5 JS |
79 | m_info->m_task.tmAddr = NULL ; |
80 | m_info->m_task.tmWakeUp = 0 ; | |
81 | m_info->m_task.tmReserved = 0 ; | |
82 | m_info->m_task.qType = 0 ; | |
83 | m_info->m_table = wxMacGetNotifierTable() ; | |
84 | m_info->m_timer = this ; | |
2f1ae414 SC |
85 | } |
86 | ||
87 | bool wxTimer::IsRunning() const | |
88 | { | |
e40298d5 | 89 | return ( m_info->m_task.qType & kTMTaskActive ) ; |
e9576ca5 SC |
90 | } |
91 | ||
92 | wxTimer::~wxTimer() | |
93 | { | |
94 | Stop(); | |
f5bb2251 GD |
95 | if (m_info != NULL) { |
96 | delete m_info ; | |
97 | m_info = NULL ; | |
98 | } | |
e5f0419b SC |
99 | int index = gTimersInProcess.Index( this ) ; |
100 | if ( index != wxNOT_FOUND ) | |
101 | gTimersInProcess.RemoveAt( index ) ; | |
e9576ca5 SC |
102 | } |
103 | ||
104 | bool wxTimer::Start(int milliseconds,bool mode) | |
105 | { | |
2f1ae414 | 106 | (void)wxTimerBase::Start(milliseconds, mode); |
e9576ca5 | 107 | |
2f1ae414 | 108 | wxCHECK_MSG( m_milli > 0, FALSE, wxT("invalid value for timer timeour") ); |
76a5e5d2 | 109 | wxCHECK_MSG( m_info->m_task.tmAddr == NULL , FALSE, wxT("attempting to restart a timer") ); |
e9576ca5 | 110 | |
2f1ae414 | 111 | m_milli = milliseconds; |
4114d0af | 112 | #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340) |
76a5e5d2 | 113 | m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc ) ; |
03e11df5 | 114 | #else |
76a5e5d2 | 115 | m_info->m_task.tmAddr = NewTimerProc( MacTimerProc ) ; |
03e11df5 | 116 | #endif |
76a5e5d2 SC |
117 | m_info->m_task.tmWakeUp = 0 ; |
118 | m_info->m_task.tmReserved = 0 ; | |
e40298d5 JS |
119 | m_info->m_task.qType = 0 ; |
120 | m_info->m_timer = this ; | |
76a5e5d2 SC |
121 | InsXTime((QElemPtr) &m_info->m_task ) ; |
122 | PrimeTime( (QElemPtr) &m_info->m_task , m_milli ) ; | |
c5990c00 | 123 | return TRUE; |
e9576ca5 SC |
124 | } |
125 | ||
126 | void wxTimer::Stop() | |
127 | { | |
e9576ca5 | 128 | m_milli = 0 ; |
76a5e5d2 | 129 | if ( m_info->m_task.tmAddr ) |
2f1ae414 | 130 | { |
e40298d5 JS |
131 | RmvTime( (QElemPtr) &m_info->m_task ) ; |
132 | DisposeTimerUPP(m_info->m_task.tmAddr) ; | |
133 | m_info->m_task.tmAddr = NULL ; | |
2f1ae414 SC |
134 | } |
135 | wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ; | |
e9576ca5 SC |
136 | } |
137 | ||
138 | ||
2f1ae414 | 139 |