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