]> git.saurik.com Git - wxWidgets.git/blame - src/os2/timer.cpp
compilation fix for wxUSE_UNICODE_MSLU && !WXWIN_COMPATIBILITY_2_6 (bug 1587408)
[wxWidgets.git] / src / os2 / timer.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
521bf4ff 2// Name: src/os2/timer.cpp
0e320a79 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
c0badb70
WS
15#include "wx/timer.h"
16
d90895ac 17#ifndef WX_PRECOMP
d90895ac 18 #include "wx/list.h"
8ecff181 19 #include "wx/window.h"
d90895ac
DW
20 #include "wx/event.h"
21 #include "wx/app.h"
88a7a4e1 22 #include "wx/intl.h"
e4db172a 23 #include "wx/log.h"
0e320a79
DW
24#endif
25
521bf4ff 26#include "wx/os2/private.h"
d90895ac 27
d90895ac
DW
28#include <time.h>
29#include <sys/types.h>
30
31#include <sys/timeb.h>
2461cfa0
SN
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
39WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, 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
45static wxTimerMap& TimerMap()
46{
47 static wxTimerMap s_timerMap;
48
49 return s_timerMap;
50}
51
d90895ac
DW
52// ----------------------------------------------------------------------------
53// private functions
54// ----------------------------------------------------------------------------
55
2461cfa0 56// timer callback used for all timers
9ed0fac8 57ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG);
d90895ac
DW
58
59// ----------------------------------------------------------------------------
60// macros
61// ----------------------------------------------------------------------------
62
313feadc 63IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
0e320a79 64
2461cfa0
SN
65// ============================================================================
66// implementation
67// ============================================================================
68
69// ----------------------------------------------------------------------------
70// wxTimer class
71// ----------------------------------------------------------------------------
72
496fd9fb 73void wxTimer::Init()
0e320a79 74{
9ed0fac8 75 m_ulId = 0;
0e320a79
DW
76}
77
78wxTimer::~wxTimer()
79{
05a8bfed 80 wxTimer::Stop();
0e320a79
DW
81}
82
233d6db5
DW
83void wxTimer::Notify()
84{
85 //
86 // The base class version generates an event if it has owner - which it
87 // should because otherwise nobody can process timer events, but it does
88 // not use the OS's ID, which OS/2 must have to figure out which timer fired
89 //
90 wxCHECK_RET( m_owner, _T("wxTimer::Notify() should be overridden.") );
91
5d644707 92 wxTimerEvent vEvent( m_idTimer
233d6db5
DW
93 ,m_milli
94 );
95
96 (void)m_owner->ProcessEvent(vEvent);
97} // end of wxTimer::Notify
98
8ecff181 99bool wxTimer::Start( int nMilliseconds, bool bOneShot )
0e320a79 100{
8ecff181 101 (void)wxTimerBase::Start( nMilliseconds, bOneShot );
d90895ac 102
8ecff181 103 wxCHECK_MSG( m_milli > 0L, false, wxT("invalid value for timer") );
d90895ac 104
8ecff181 105 wxWindow* pWin = NULL;
5d644707
DW
106
107 if (m_owner)
108 {
109 pWin = (wxWindow*)m_owner;
110 m_ulId = ::WinStartTimer( m_Hab
111 ,pWin->GetHWND()
112 ,m_idTimer
113 ,(ULONG)nMilliseconds
114 );
115 }
116 else
117 m_ulId = ::WinStartTimer( m_Hab
118 ,NULLHANDLE
119 ,0
120 ,(ULONG)nMilliseconds
121 );
9ed0fac8
DW
122 if (m_ulId > 0L)
123 {
2461cfa0
SN
124 // check that SetTimer() didn't reuse an existing id: according to
125 // the MSDN this can happen and this would be catastrophic to us as
126 // we rely on ids uniquely identifying the timers because we use
127 // them as keys in the hash
128 if ( TimerMap().find(m_ulId) != TimerMap().end() )
129 {
130 wxLogError(_("Timer creation failed."));
131
132 ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId);
133 m_ulId = 0;
134
135 return false;
136 }
137
138 TimerMap()[m_ulId] = this;
139
140 return true;
d90895ac
DW
141 }
142 else
143 {
144 wxLogSysError(_("Couldn't create a timer"));
145
8ecff181 146 return false;
d90895ac 147 }
0e320a79
DW
148}
149
150void wxTimer::Stop()
151{
9ed0fac8 152 if ( m_ulId )
d90895ac 153 {
5d644707
DW
154 if (m_owner)
155 {
156 wxWindow* pWin = (wxWindow*)m_owner;
157
158 ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId);
159 }
160 else
161 ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId);
2461cfa0
SN
162
163 TimerMap().erase(m_ulId);
d90895ac 164 }
5d644707 165 m_ulId = 0L;
0e320a79
DW
166}
167
d90895ac
DW
168// ----------------------------------------------------------------------------
169// private functions
170// ----------------------------------------------------------------------------
171
9ed0fac8
DW
172void wxProcessTimer(
173 wxTimer& rTimer
174)
d90895ac 175{
9ed0fac8 176 //
d90895ac 177 // Avoid to process spurious timer events
9ed0fac8
DW
178 //
179 if (rTimer.m_ulId == 0L)
d90895ac
DW
180 return;
181
9ed0fac8
DW
182 if (rTimer.IsOneShot())
183 rTimer.Stop();
d90895ac 184
9ed0fac8 185 rTimer.Notify();
d90895ac
DW
186}
187
9ed0fac8
DW
188ULONG wxTimerProc(
189 HWND WXUNUSED(hwnd)
190, ULONG
191, int nIdTimer
192, ULONG
193)
d90895ac 194{
2461cfa0 195 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
d90895ac 196
2461cfa0
SN
197 wxCHECK_MSG(node != TimerMap().end(), 0,
198 wxT("bogus timer id in wxTimerProc") );
199 wxProcessTimer(*(node->second));
d90895ac
DW
200 return 0;
201}