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