]>
Commit | Line | Data |
---|---|---|
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 |
d90895ac | 7 | // Copyright: (c) David Webster |
65571936 | 8 | // Licence: wxWindows licence |
0e320a79 DW |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
d90895ac DW |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
c2ca375c | 14 | #include "wx/os2/private/timer.h" |
c0badb70 | 15 | |
d90895ac | 16 | #ifndef WX_PRECOMP |
d90895ac | 17 | #include "wx/list.h" |
8ecff181 | 18 | #include "wx/window.h" |
d90895ac DW |
19 | #include "wx/event.h" |
20 | #include "wx/app.h" | |
88a7a4e1 | 21 | #include "wx/intl.h" |
e4db172a | 22 | #include "wx/log.h" |
0e320a79 DW |
23 | #endif |
24 | ||
521bf4ff | 25 | #include "wx/os2/private.h" |
d90895ac | 26 | |
d90895ac DW |
27 | #include <time.h> |
28 | #include <sys/types.h> | |
29 | ||
30 | #include <sys/timeb.h> | |
2461cfa0 SN |
31 | |
32 | // ---------------------------------------------------------------------------- | |
33 | // private globals | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | // define a hash containing all the timers: it is indexed by timer id and | |
37 | // contains the corresponding timer | |
c2ca375c | 38 | WX_DECLARE_HASH_MAP(unsigned long, wxOS2TimerImpl *, wxIntegerHash, wxIntegerEqual, |
2461cfa0 SN |
39 | wxTimerMap); |
40 | ||
41 | // instead of using a global here, wrap it in a static function as otherwise it | |
42 | // could have been used before being initialized if a timer object were created | |
43 | // globally | |
44 | static wxTimerMap& TimerMap() | |
45 | { | |
46 | static wxTimerMap s_timerMap; | |
47 | ||
48 | return s_timerMap; | |
49 | } | |
50 | ||
d90895ac DW |
51 | // ---------------------------------------------------------------------------- |
52 | // private functions | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
2461cfa0 | 55 | // timer callback used for all timers |
9ed0fac8 | 56 | ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG); |
d90895ac | 57 | |
2461cfa0 SN |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // wxTimer class | |
64 | // ---------------------------------------------------------------------------- | |
65 | ||
c2ca375c | 66 | bool wxOS2TimerImpl::Start( int nMilliseconds, bool bOneShot ) |
0e320a79 | 67 | { |
55bfbcb9 VZ |
68 | if ( !wxTimerImpl::Start( nMilliseconds, bOneShot ) ) |
69 | return false; | |
d90895ac | 70 | |
8ecff181 | 71 | wxWindow* pWin = NULL; |
5d644707 DW |
72 | |
73 | if (m_owner) | |
74 | { | |
75 | pWin = (wxWindow*)m_owner; | |
76 | m_ulId = ::WinStartTimer( m_Hab | |
77 | ,pWin->GetHWND() | |
78 | ,m_idTimer | |
79 | ,(ULONG)nMilliseconds | |
80 | ); | |
81 | } | |
82 | else | |
55bfbcb9 | 83 | { |
5d644707 DW |
84 | m_ulId = ::WinStartTimer( m_Hab |
85 | ,NULLHANDLE | |
86 | ,0 | |
87 | ,(ULONG)nMilliseconds | |
88 | ); | |
55bfbcb9 VZ |
89 | } |
90 | ||
9ed0fac8 DW |
91 | if (m_ulId > 0L) |
92 | { | |
2461cfa0 SN |
93 | // check that SetTimer() didn't reuse an existing id: according to |
94 | // the MSDN this can happen and this would be catastrophic to us as | |
95 | // we rely on ids uniquely identifying the timers because we use | |
96 | // them as keys in the hash | |
97 | if ( TimerMap().find(m_ulId) != TimerMap().end() ) | |
98 | { | |
99 | wxLogError(_("Timer creation failed.")); | |
100 | ||
101 | ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId); | |
102 | m_ulId = 0; | |
103 | ||
104 | return false; | |
105 | } | |
106 | ||
107 | TimerMap()[m_ulId] = this; | |
108 | ||
109 | return true; | |
d90895ac DW |
110 | } |
111 | else | |
112 | { | |
113 | wxLogSysError(_("Couldn't create a timer")); | |
114 | ||
8ecff181 | 115 | return false; |
d90895ac | 116 | } |
0e320a79 DW |
117 | } |
118 | ||
c2ca375c | 119 | void wxOS2TimerImpl::Stop() |
0e320a79 | 120 | { |
9ed0fac8 | 121 | if ( m_ulId ) |
d90895ac | 122 | { |
5d644707 DW |
123 | if (m_owner) |
124 | { | |
125 | wxWindow* pWin = (wxWindow*)m_owner; | |
126 | ||
127 | ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId); | |
128 | } | |
129 | else | |
130 | ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId); | |
2461cfa0 SN |
131 | |
132 | TimerMap().erase(m_ulId); | |
d90895ac | 133 | } |
5d644707 | 134 | m_ulId = 0L; |
0e320a79 DW |
135 | } |
136 | ||
d90895ac DW |
137 | // ---------------------------------------------------------------------------- |
138 | // private functions | |
139 | // ---------------------------------------------------------------------------- | |
140 | ||
9ed0fac8 | 141 | void wxProcessTimer( |
c2ca375c | 142 | wxOS2TimerImpl& rTimer |
9ed0fac8 | 143 | ) |
d90895ac | 144 | { |
9ed0fac8 | 145 | // |
d90895ac | 146 | // Avoid to process spurious timer events |
9ed0fac8 DW |
147 | // |
148 | if (rTimer.m_ulId == 0L) | |
d90895ac DW |
149 | return; |
150 | ||
9ed0fac8 DW |
151 | if (rTimer.IsOneShot()) |
152 | rTimer.Stop(); | |
d90895ac | 153 | |
9ed0fac8 | 154 | rTimer.Notify(); |
d90895ac DW |
155 | } |
156 | ||
9ed0fac8 DW |
157 | ULONG wxTimerProc( |
158 | HWND WXUNUSED(hwnd) | |
159 | , ULONG | |
160 | , int nIdTimer | |
161 | , ULONG | |
162 | ) | |
d90895ac | 163 | { |
2461cfa0 | 164 | wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer); |
d90895ac | 165 | |
2461cfa0 SN |
166 | wxCHECK_MSG(node != TimerMap().end(), 0, |
167 | wxT("bogus timer id in wxTimerProc") ); | |
168 | wxProcessTimer(*(node->second)); | |
d90895ac DW |
169 | return 0; |
170 | } |