]> git.saurik.com Git - wxWidgets.git/blob - src/os2/timer.cpp
minor cleanup
[wxWidgets.git] / src / os2 / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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"
23 #endif
24
25 #include "wx/intl.h"
26 #include "wx/log.h"
27
28 #include "wx/timer.h"
29
30 #include <time.h>
31 #include <sys/types.h>
32
33 #include <sys/timeb.h>
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
54 // ----------------------------------------------------------------------------
55 // private functions
56 // ----------------------------------------------------------------------------
57
58 // timer callback used for all timers
59 ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG);
60
61 // ----------------------------------------------------------------------------
62 // macros
63 // ----------------------------------------------------------------------------
64
65 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxTimer class
73 // ----------------------------------------------------------------------------
74
75 void wxTimer::Init()
76 {
77 m_ulId = 0;
78 }
79
80 wxTimer::~wxTimer()
81 {
82 wxTimer::Stop();
83 }
84
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
94 wxTimerEvent vEvent( m_idTimer
95 ,m_milli
96 );
97
98 (void)m_owner->ProcessEvent(vEvent);
99 } // end of wxTimer::Notify
100
101 bool wxTimer::Start(
102 int nMilliseconds
103 , bool bOneShot
104 )
105 {
106 (void)wxTimerBase::Start( nMilliseconds
107 ,bOneShot
108 );
109
110 wxCHECK_MSG( m_milli > 0L, FALSE, wxT("invalid value for timer") );
111
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 );
129 if (m_ulId > 0L)
130 {
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;
148 }
149 else
150 {
151 wxLogSysError(_("Couldn't create a timer"));
152
153 return(FALSE);
154 }
155 }
156
157 void wxTimer::Stop()
158 {
159 if ( m_ulId )
160 {
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);
169
170 TimerMap().erase(m_ulId);
171 }
172 m_ulId = 0L;
173 }
174
175 // ----------------------------------------------------------------------------
176 // private functions
177 // ----------------------------------------------------------------------------
178
179 void wxProcessTimer(
180 wxTimer& rTimer
181 )
182 {
183 //
184 // Avoid to process spurious timer events
185 //
186 if (rTimer.m_ulId == 0L)
187 return;
188
189 if (rTimer.IsOneShot())
190 rTimer.Stop();
191
192 rTimer.Notify();
193 }
194
195 ULONG wxTimerProc(
196 HWND WXUNUSED(hwnd)
197 , ULONG
198 , int nIdTimer
199 , ULONG
200 )
201 {
202 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
203
204 wxCHECK_MSG(node != TimerMap().end(), 0,
205 wxT("bogus timer id in wxTimerProc") );
206 wxProcessTimer(*(node->second));
207 return 0;
208 }
209