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