Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / os2 / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/17/99
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/os2/private/timer.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/list.h"
18 #include "wx/window.h"
19 #include "wx/event.h"
20 #include "wx/app.h"
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #endif
24
25 #include "wx/os2/private.h"
26
27 #include <time.h>
28 #include <sys/types.h>
29
30 #include <sys/timeb.h>
31
32 // ----------------------------------------------------------------------------
33 // private globals
34 // ----------------------------------------------------------------------------
35
36 // define a hash containing all the timers: it is indexed by timer id and
37 // contains the corresponding timer
38 WX_DECLARE_HASH_MAP(unsigned long, wxOS2TimerImpl *, wxIntegerHash, wxIntegerEqual,
39 wxTimerMap);
40
41 // instead of using a global here, wrap it in a static function as otherwise it
42 // could have been used before being initialized if a timer object were created
43 // globally
44 static wxTimerMap& TimerMap()
45 {
46 static wxTimerMap s_timerMap;
47
48 return s_timerMap;
49 }
50
51 // ----------------------------------------------------------------------------
52 // private functions
53 // ----------------------------------------------------------------------------
54
55 // timer callback used for all timers
56 ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG);
57
58 // ============================================================================
59 // implementation
60 // ============================================================================
61
62 // ----------------------------------------------------------------------------
63 // wxTimer class
64 // ----------------------------------------------------------------------------
65
66 bool wxOS2TimerImpl::Start( int nMilliseconds, bool bOneShot )
67 {
68 if ( !wxTimerImpl::Start( nMilliseconds, bOneShot ) )
69 return false;
70
71 wxWindow* pWin = NULL;
72
73 if (m_owner)
74 {
75 pWin = (wxWindow*)m_owner;
76 m_ulId = ::WinStartTimer( m_Hab
77 ,pWin->GetHWND()
78 ,m_idTimer
79 ,(ULONG)nMilliseconds
80 );
81 }
82 else
83 {
84 m_ulId = ::WinStartTimer( m_Hab
85 ,NULLHANDLE
86 ,0
87 ,(ULONG)nMilliseconds
88 );
89 }
90
91 if (m_ulId > 0L)
92 {
93 // check that SetTimer() didn't reuse an existing id: according to
94 // the MSDN this can happen and this would be catastrophic to us as
95 // we rely on ids uniquely identifying the timers because we use
96 // them as keys in the hash
97 if ( TimerMap().find(m_ulId) != TimerMap().end() )
98 {
99 wxLogError(_("Timer creation failed."));
100
101 ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId);
102 m_ulId = 0;
103
104 return false;
105 }
106
107 TimerMap()[m_ulId] = this;
108
109 return true;
110 }
111 else
112 {
113 wxLogSysError(_("Couldn't create a timer"));
114
115 return false;
116 }
117 }
118
119 void wxOS2TimerImpl::Stop()
120 {
121 if ( m_ulId )
122 {
123 if (m_owner)
124 {
125 wxWindow* pWin = (wxWindow*)m_owner;
126
127 ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId);
128 }
129 else
130 ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId);
131
132 TimerMap().erase(m_ulId);
133 }
134 m_ulId = 0L;
135 }
136
137 // ----------------------------------------------------------------------------
138 // private functions
139 // ----------------------------------------------------------------------------
140
141 void wxProcessTimer(
142 wxOS2TimerImpl& rTimer
143 )
144 {
145 //
146 // Avoid to process spurious timer events
147 //
148 if (rTimer.m_ulId == 0L)
149 return;
150
151 if (rTimer.IsOneShot())
152 rTimer.Stop();
153
154 rTimer.Notify();
155 }
156
157 ULONG wxTimerProc(
158 HWND WXUNUSED(hwnd)
159 , ULONG
160 , int nIdTimer
161 , ULONG
162 )
163 {
164 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
165
166 wxCHECK_MSG(node != TimerMap().end(), 0,
167 wxT("bogus timer id in wxTimerProc") );
168 wxProcessTimer(*(node->second));
169 return 0;
170 }