]> git.saurik.com Git - wxWidgets.git/blob - src/os2/timer.cpp
bracket 10.5 only code
[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 bool wxOS2TimerImpl::Start( int nMilliseconds, bool bOneShot )
68 {
69 if ( !wxTimerImpl::Start( nMilliseconds, bOneShot ) )
70 return false;
71
72 wxWindow* pWin = NULL;
73
74 if (m_owner)
75 {
76 pWin = (wxWindow*)m_owner;
77 m_ulId = ::WinStartTimer( m_Hab
78 ,pWin->GetHWND()
79 ,m_idTimer
80 ,(ULONG)nMilliseconds
81 );
82 }
83 else
84 {
85 m_ulId = ::WinStartTimer( m_Hab
86 ,NULLHANDLE
87 ,0
88 ,(ULONG)nMilliseconds
89 );
90 }
91
92 if (m_ulId > 0L)
93 {
94 // check that SetTimer() didn't reuse an existing id: according to
95 // the MSDN this can happen and this would be catastrophic to us as
96 // we rely on ids uniquely identifying the timers because we use
97 // them as keys in the hash
98 if ( TimerMap().find(m_ulId) != TimerMap().end() )
99 {
100 wxLogError(_("Timer creation failed."));
101
102 ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId);
103 m_ulId = 0;
104
105 return false;
106 }
107
108 TimerMap()[m_ulId] = this;
109
110 return true;
111 }
112 else
113 {
114 wxLogSysError(_("Couldn't create a timer"));
115
116 return false;
117 }
118 }
119
120 void wxOS2TimerImpl::Stop()
121 {
122 if ( m_ulId )
123 {
124 if (m_owner)
125 {
126 wxWindow* pWin = (wxWindow*)m_owner;
127
128 ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId);
129 }
130 else
131 ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId);
132
133 TimerMap().erase(m_ulId);
134 }
135 m_ulId = 0L;
136 }
137
138 // ----------------------------------------------------------------------------
139 // private functions
140 // ----------------------------------------------------------------------------
141
142 void wxProcessTimer(
143 wxOS2TimerImpl& rTimer
144 )
145 {
146 //
147 // Avoid to process spurious timer events
148 //
149 if (rTimer.m_ulId == 0L)
150 return;
151
152 if (rTimer.IsOneShot())
153 rTimer.Stop();
154
155 rTimer.Notify();
156 }
157
158 ULONG wxTimerProc(
159 HWND WXUNUSED(hwnd)
160 , ULONG
161 , int nIdTimer
162 , ULONG
163 )
164 {
165 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
166
167 wxCHECK_MSG(node != TimerMap().end(), 0,
168 wxT("bogus timer id in wxTimerProc") );
169 wxProcessTimer(*(node->second));
170 return 0;
171 }