]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: timer.cpp | |
3 | // Purpose: wxTimer implementation | |
d90895ac | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
d90895ac | 6 | // Created: 10/17/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
d90895ac | 8 | // Copyright: (c) David Webster |
65571936 | 9 | // Licence: wxWindows licence |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
d90895ac DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include "wx/window.h" | |
16 | #include "wx/os2/private.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/setup.h" | |
20 | #include "wx/list.h" | |
21 | #include "wx/event.h" | |
22 | #include "wx/app.h" | |
0e320a79 DW |
23 | #endif |
24 | ||
d90895ac DW |
25 | #include "wx/intl.h" |
26 | #include "wx/log.h" | |
27 | ||
0e320a79 DW |
28 | #include "wx/timer.h" |
29 | ||
d90895ac DW |
30 | #include <time.h> |
31 | #include <sys/types.h> | |
32 | ||
33 | #include <sys/timeb.h> | |
2461cfa0 SN |
34 | |
35 | // ---------------------------------------------------------------------------- | |
36 | // private globals | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | // define a hash containing all the timers: it is indexed by timer id and | |
40 | // contains the corresponding timer | |
41 | WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual, | |
42 | wxTimerMap); | |
43 | ||
44 | // instead of using a global here, wrap it in a static function as otherwise it | |
45 | // could have been used before being initialized if a timer object were created | |
46 | // globally | |
47 | static wxTimerMap& TimerMap() | |
48 | { | |
49 | static wxTimerMap s_timerMap; | |
50 | ||
51 | return s_timerMap; | |
52 | } | |
53 | ||
d90895ac DW |
54 | // ---------------------------------------------------------------------------- |
55 | // private functions | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
2461cfa0 | 58 | // timer callback used for all timers |
9ed0fac8 | 59 | ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG); |
d90895ac DW |
60 | |
61 | // ---------------------------------------------------------------------------- | |
62 | // macros | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
313feadc | 65 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) |
0e320a79 | 66 | |
2461cfa0 SN |
67 | // ============================================================================ |
68 | // implementation | |
69 | // ============================================================================ | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // wxTimer class | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
496fd9fb | 75 | void wxTimer::Init() |
0e320a79 | 76 | { |
9ed0fac8 | 77 | m_ulId = 0; |
0e320a79 DW |
78 | } |
79 | ||
80 | wxTimer::~wxTimer() | |
81 | { | |
05a8bfed | 82 | wxTimer::Stop(); |
0e320a79 DW |
83 | } |
84 | ||
233d6db5 DW |
85 | void wxTimer::Notify() |
86 | { | |
87 | // | |
88 | // The base class version generates an event if it has owner - which it | |
89 | // should because otherwise nobody can process timer events, but it does | |
90 | // not use the OS's ID, which OS/2 must have to figure out which timer fired | |
91 | // | |
92 | wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") ); | |
93 | ||
5d644707 | 94 | wxTimerEvent vEvent( m_idTimer |
233d6db5 DW |
95 | ,m_milli |
96 | ); | |
97 | ||
98 | (void)m_owner->ProcessEvent(vEvent); | |
99 | } // end of wxTimer::Notify | |
100 | ||
9ed0fac8 DW |
101 | bool wxTimer::Start( |
102 | int nMilliseconds | |
103 | , bool bOneShot | |
104 | ) | |
0e320a79 | 105 | { |
9ed0fac8 DW |
106 | (void)wxTimerBase::Start( nMilliseconds |
107 | ,bOneShot | |
108 | ); | |
d90895ac | 109 | |
9ed0fac8 | 110 | wxCHECK_MSG( m_milli > 0L, FALSE, wxT("invalid value for timer") ); |
d90895ac | 111 | |
5d644707 DW |
112 | wxWindow* pWin = NULL; |
113 | ||
114 | if (m_owner) | |
115 | { | |
116 | pWin = (wxWindow*)m_owner; | |
117 | m_ulId = ::WinStartTimer( m_Hab | |
118 | ,pWin->GetHWND() | |
119 | ,m_idTimer | |
120 | ,(ULONG)nMilliseconds | |
121 | ); | |
122 | } | |
123 | else | |
124 | m_ulId = ::WinStartTimer( m_Hab | |
125 | ,NULLHANDLE | |
126 | ,0 | |
127 | ,(ULONG)nMilliseconds | |
128 | ); | |
9ed0fac8 DW |
129 | if (m_ulId > 0L) |
130 | { | |
2461cfa0 SN |
131 | // check that SetTimer() didn't reuse an existing id: according to |
132 | // the MSDN this can happen and this would be catastrophic to us as | |
133 | // we rely on ids uniquely identifying the timers because we use | |
134 | // them as keys in the hash | |
135 | if ( TimerMap().find(m_ulId) != TimerMap().end() ) | |
136 | { | |
137 | wxLogError(_("Timer creation failed.")); | |
138 | ||
139 | ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId); | |
140 | m_ulId = 0; | |
141 | ||
142 | return false; | |
143 | } | |
144 | ||
145 | TimerMap()[m_ulId] = this; | |
146 | ||
147 | return true; | |
d90895ac DW |
148 | } |
149 | else | |
150 | { | |
151 | wxLogSysError(_("Couldn't create a timer")); | |
152 | ||
9ed0fac8 | 153 | return(FALSE); |
d90895ac | 154 | } |
0e320a79 DW |
155 | } |
156 | ||
157 | void wxTimer::Stop() | |
158 | { | |
9ed0fac8 | 159 | if ( m_ulId ) |
d90895ac | 160 | { |
5d644707 DW |
161 | if (m_owner) |
162 | { | |
163 | wxWindow* pWin = (wxWindow*)m_owner; | |
164 | ||
165 | ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId); | |
166 | } | |
167 | else | |
168 | ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId); | |
2461cfa0 SN |
169 | |
170 | TimerMap().erase(m_ulId); | |
d90895ac | 171 | } |
5d644707 | 172 | m_ulId = 0L; |
0e320a79 DW |
173 | } |
174 | ||
d90895ac DW |
175 | // ---------------------------------------------------------------------------- |
176 | // private functions | |
177 | // ---------------------------------------------------------------------------- | |
178 | ||
9ed0fac8 DW |
179 | void wxProcessTimer( |
180 | wxTimer& rTimer | |
181 | ) | |
d90895ac | 182 | { |
9ed0fac8 | 183 | // |
d90895ac | 184 | // Avoid to process spurious timer events |
9ed0fac8 DW |
185 | // |
186 | if (rTimer.m_ulId == 0L) | |
d90895ac DW |
187 | return; |
188 | ||
9ed0fac8 DW |
189 | if (rTimer.IsOneShot()) |
190 | rTimer.Stop(); | |
d90895ac | 191 | |
9ed0fac8 | 192 | rTimer.Notify(); |
d90895ac DW |
193 | } |
194 | ||
9ed0fac8 DW |
195 | ULONG wxTimerProc( |
196 | HWND WXUNUSED(hwnd) | |
197 | , ULONG | |
198 | , int nIdTimer | |
199 | , ULONG | |
200 | ) | |
d90895ac | 201 | { |
2461cfa0 | 202 | wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer); |
d90895ac | 203 | |
2461cfa0 SN |
204 | wxCHECK_MSG(node != TimerMap().end(), 0, |
205 | wxT("bogus timer id in wxTimerProc") ); | |
206 | wxProcessTimer(*(node->second)); | |
d90895ac DW |
207 | return 0; |
208 | } | |
0e320a79 | 209 |