]> git.saurik.com Git - wxWidgets.git/blame - src/os2/timer.cpp
using new API (no visual difference)
[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
d90895ac 15#ifndef WX_PRECOMP
521bf4ff 16 #include "wx/window.h"
d90895ac
DW
17 #include "wx/list.h"
18 #include "wx/event.h"
19 #include "wx/app.h"
0e320a79
DW
20#endif
21
521bf4ff 22#include "wx/os2/private.h"
d90895ac
DW
23#include "wx/intl.h"
24#include "wx/log.h"
25
0e320a79
DW
26#include "wx/timer.h"
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
9ed0fac8
DW
99bool wxTimer::Start(
100 int nMilliseconds
101, bool bOneShot
102)
0e320a79 103{
9ed0fac8
DW
104 (void)wxTimerBase::Start( nMilliseconds
105 ,bOneShot
106 );
d90895ac 107
9ed0fac8 108 wxCHECK_MSG( m_milli > 0L, FALSE, wxT("invalid value for timer") );
d90895ac 109
5d644707
DW
110 wxWindow* pWin = NULL;
111
112 if (m_owner)
113 {
114 pWin = (wxWindow*)m_owner;
115 m_ulId = ::WinStartTimer( m_Hab
116 ,pWin->GetHWND()
117 ,m_idTimer
118 ,(ULONG)nMilliseconds
119 );
120 }
121 else
122 m_ulId = ::WinStartTimer( m_Hab
123 ,NULLHANDLE
124 ,0
125 ,(ULONG)nMilliseconds
126 );
9ed0fac8
DW
127 if (m_ulId > 0L)
128 {
2461cfa0
SN
129 // check that SetTimer() didn't reuse an existing id: according to
130 // the MSDN this can happen and this would be catastrophic to us as
131 // we rely on ids uniquely identifying the timers because we use
132 // them as keys in the hash
133 if ( TimerMap().find(m_ulId) != TimerMap().end() )
134 {
135 wxLogError(_("Timer creation failed."));
136
137 ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId);
138 m_ulId = 0;
139
140 return false;
141 }
142
143 TimerMap()[m_ulId] = this;
144
145 return true;
d90895ac
DW
146 }
147 else
148 {
149 wxLogSysError(_("Couldn't create a timer"));
150
9ed0fac8 151 return(FALSE);
d90895ac 152 }
0e320a79
DW
153}
154
155void wxTimer::Stop()
156{
9ed0fac8 157 if ( m_ulId )
d90895ac 158 {
5d644707
DW
159 if (m_owner)
160 {
161 wxWindow* pWin = (wxWindow*)m_owner;
162
163 ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId);
164 }
165 else
166 ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId);
2461cfa0
SN
167
168 TimerMap().erase(m_ulId);
d90895ac 169 }
5d644707 170 m_ulId = 0L;
0e320a79
DW
171}
172
d90895ac
DW
173// ----------------------------------------------------------------------------
174// private functions
175// ----------------------------------------------------------------------------
176
9ed0fac8
DW
177void wxProcessTimer(
178 wxTimer& rTimer
179)
d90895ac 180{
9ed0fac8 181 //
d90895ac 182 // Avoid to process spurious timer events
9ed0fac8
DW
183 //
184 if (rTimer.m_ulId == 0L)
d90895ac
DW
185 return;
186
9ed0fac8
DW
187 if (rTimer.IsOneShot())
188 rTimer.Stop();
d90895ac 189
9ed0fac8 190 rTimer.Notify();
d90895ac
DW
191}
192
9ed0fac8
DW
193ULONG wxTimerProc(
194 HWND WXUNUSED(hwnd)
195, ULONG
196, int nIdTimer
197, ULONG
198)
d90895ac 199{
2461cfa0 200 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
d90895ac 201
2461cfa0
SN
202 wxCHECK_MSG(node != TimerMap().end(), 0,
203 wxT("bogus timer id in wxTimerProc") );
204 wxProcessTimer(*(node->second));
d90895ac
DW
205 return 0;
206}