]> git.saurik.com Git - wxWidgets.git/blame - src/os2/timer.cpp
PalmOS native colour dialog.
[wxWidgets.git] / src / os2 / timer.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: timer.cpp
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
272ebf16
SN
12#ifdef __GNUG__
13 #pragma implementation "timer.h"
14#endif
15
d90895ac
DW
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"
0e320a79
DW
27#endif
28
d90895ac
DW
29#include "wx/intl.h"
30#include "wx/log.h"
31
0e320a79
DW
32#include "wx/timer.h"
33
d90895ac
DW
34#include <time.h>
35#include <sys/types.h>
36
37#include <sys/timeb.h>
2461cfa0
SN
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
45WX_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
51static wxTimerMap& TimerMap()
52{
53 static wxTimerMap s_timerMap;
54
55 return s_timerMap;
56}
57
d90895ac
DW
58// ----------------------------------------------------------------------------
59// private functions
60// ----------------------------------------------------------------------------
61
2461cfa0 62// timer callback used for all timers
9ed0fac8 63ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG);
d90895ac
DW
64
65// ----------------------------------------------------------------------------
66// macros
67// ----------------------------------------------------------------------------
68
313feadc 69IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
0e320a79 70
2461cfa0
SN
71// ============================================================================
72// implementation
73// ============================================================================
74
75// ----------------------------------------------------------------------------
76// wxTimer class
77// ----------------------------------------------------------------------------
78
496fd9fb 79void wxTimer::Init()
0e320a79 80{
9ed0fac8 81 m_ulId = 0;
0e320a79
DW
82}
83
84wxTimer::~wxTimer()
85{
05a8bfed 86 wxTimer::Stop();
0e320a79
DW
87}
88
233d6db5
DW
89void 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
5d644707 98 wxTimerEvent vEvent( m_idTimer
233d6db5
DW
99 ,m_milli
100 );
101
102 (void)m_owner->ProcessEvent(vEvent);
103} // end of wxTimer::Notify
104
9ed0fac8
DW
105bool wxTimer::Start(
106 int nMilliseconds
107, bool bOneShot
108)
0e320a79 109{
9ed0fac8
DW
110 (void)wxTimerBase::Start( nMilliseconds
111 ,bOneShot
112 );
d90895ac 113
9ed0fac8 114 wxCHECK_MSG( m_milli > 0L, FALSE, wxT("invalid value for timer") );
d90895ac 115
5d644707
DW
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 );
9ed0fac8
DW
133 if (m_ulId > 0L)
134 {
2461cfa0
SN
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;
d90895ac
DW
152 }
153 else
154 {
155 wxLogSysError(_("Couldn't create a timer"));
156
9ed0fac8 157 return(FALSE);
d90895ac 158 }
0e320a79
DW
159}
160
161void wxTimer::Stop()
162{
9ed0fac8 163 if ( m_ulId )
d90895ac 164 {
5d644707
DW
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);
2461cfa0
SN
173
174 TimerMap().erase(m_ulId);
d90895ac 175 }
5d644707 176 m_ulId = 0L;
0e320a79
DW
177}
178
d90895ac
DW
179// ----------------------------------------------------------------------------
180// private functions
181// ----------------------------------------------------------------------------
182
9ed0fac8
DW
183void wxProcessTimer(
184 wxTimer& rTimer
185)
d90895ac 186{
9ed0fac8 187 //
d90895ac 188 // Avoid to process spurious timer events
9ed0fac8
DW
189 //
190 if (rTimer.m_ulId == 0L)
d90895ac
DW
191 return;
192
9ed0fac8
DW
193 if (rTimer.IsOneShot())
194 rTimer.Stop();
d90895ac 195
9ed0fac8 196 rTimer.Notify();
d90895ac
DW
197}
198
9ed0fac8
DW
199ULONG wxTimerProc(
200 HWND WXUNUSED(hwnd)
201, ULONG
202, int nIdTimer
203, ULONG
204)
d90895ac 205{
2461cfa0 206 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
d90895ac 207
2461cfa0
SN
208 wxCHECK_MSG(node != TimerMap().end(), 0,
209 wxT("bogus timer id in wxTimerProc") );
210 wxProcessTimer(*(node->second));
d90895ac
DW
211 return 0;
212}
0e320a79 213