]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8ecff181 | 2 | // Name: src/msw/timer.cpp |
2bda0e17 KB |
3 | // Purpose: wxTimer implementation |
4 | // Author: Julian Smart | |
6d20f7ae | 5 | // Modified by: Vadim Zeitlin (use hash map instead of list, global rewrite) |
2bda0e17 KB |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2bda0e17 KB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
c085e333 | 16 | #pragma hdrstop |
2bda0e17 KB |
17 | #endif |
18 | ||
1e6feb95 VZ |
19 | #if wxUSE_TIMER |
20 | ||
c2ca375c | 21 | #include "wx/msw/private/timer.h" |
c0badb70 | 22 | |
2bda0e17 | 23 | #ifndef WX_PRECOMP |
c085e333 | 24 | #include "wx/list.h" |
2432b92d | 25 | #include "wx/event.h" |
c085e333 | 26 | #include "wx/app.h" |
0470b1e6 VZ |
27 | #include "wx/intl.h" |
28 | #include "wx/log.h" | |
df69528b | 29 | #include "wx/hashmap.h" |
0e16e5c3 | 30 | #include "wx/module.h" |
2bda0e17 KB |
31 | #endif |
32 | ||
0470b1e6 | 33 | #include "wx/msw/private.h" |
2bda0e17 | 34 | |
c085e333 | 35 | // ---------------------------------------------------------------------------- |
97f278b4 | 36 | // private globals |
c085e333 VZ |
37 | // ---------------------------------------------------------------------------- |
38 | ||
97f278b4 VZ |
39 | // define a hash containing all the timers: it is indexed by timer id and |
40 | // contains the corresponding timer | |
491acfc7 | 41 | WX_DECLARE_HASH_MAP(WPARAM, wxMSWTimerImpl *, wxIntegerHash, wxIntegerEqual, |
3f5c62f9 | 42 | wxTimerMap); |
5b6c8794 | 43 | |
ee1025a7 VZ |
44 | // instead of using a global here, wrap it in a static function as otherwise it |
45 | // could have been used before being initialized if a timer object were created | |
46 | // globally | |
47 | static wxTimerMap& TimerMap() | |
48 | { | |
49 | static wxTimerMap s_timerMap; | |
50 | ||
51 | return s_timerMap; | |
52 | } | |
4676948b | 53 | |
c085e333 | 54 | // ---------------------------------------------------------------------------- |
97f278b4 | 55 | // private functions |
eccd1992 | 56 | // ---------------------------------------------------------------------------- |
0e16e5c3 | 57 | |
9cfc7f4f RD |
58 | LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message, |
59 | WPARAM wParam, LPARAM lParam); | |
eccd1992 | 60 | |
9cfc7f4f RD |
61 | // implemented in utils.cpp |
62 | extern "C" WXDLLIMPEXP_BASE HWND | |
63 | wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc); | |
64 | ||
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // wxTimerHiddenWindowModule: used to manage the hidden window used for | |
68 | // catching timer messages (we need a module to ensure that the window is | |
69 | // always deleted) | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | class wxTimerHiddenWindowModule : public wxModule | |
73 | { | |
74 | public: | |
75 | // module init/finalize | |
76 | virtual bool OnInit(); | |
77 | virtual void OnExit(); | |
78 | ||
79 | // get the hidden window (creates on demand) | |
80 | static HWND GetHWND(); | |
81 | ||
82 | private: | |
83 | // the HWND of the hidden window | |
84 | static HWND ms_hwnd; | |
85 | ||
86 | // the class used to create it | |
87 | static const wxChar *ms_className; | |
88 | ||
89 | DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule) | |
90 | }; | |
91 | ||
92 | IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule, wxModule) | |
eccd1992 | 93 | |
c085e333 VZ |
94 | // ============================================================================ |
95 | // implementation | |
96 | // ============================================================================ | |
97 | ||
9cfc7f4f | 98 | |
c085e333 | 99 | // ---------------------------------------------------------------------------- |
c2ca375c | 100 | // wxMSWTimerImpl class |
c085e333 | 101 | // ---------------------------------------------------------------------------- |
73974df1 | 102 | |
c2ca375c | 103 | bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot) |
2bda0e17 | 104 | { |
c2ca375c VZ |
105 | if ( !wxTimerImpl::Start(milliseconds, oneShot) ) |
106 | return false; | |
caa36aab | 107 | |
9cfc7f4f RD |
108 | m_id = ::SetTimer( |
109 | wxTimerHiddenWindowModule::GetHWND(), // window to send the messages to | |
110 | GetId(), // timer ID | |
111 | (UINT)m_milli, // delay | |
112 | NULL // timer proc. Not used since we pass hwnd | |
113 | ); | |
eccd1992 VZ |
114 | |
115 | if ( !m_id ) | |
c085e333 VZ |
116 | { |
117 | wxLogSysError(_("Couldn't create a timer")); | |
118 | ||
04cd30de | 119 | return false; |
c085e333 | 120 | } |
eccd1992 | 121 | |
97f278b4 VZ |
122 | // check that SetTimer() didn't reuse an existing id: according to the MSDN |
123 | // this can happen and this would be catastrophic to us as we rely on ids | |
124 | // uniquely identifying the timers because we use them as keys in the hash | |
7ca19ddb VZ |
125 | // |
126 | // notice that this also happens if the same id is reused for multiple | |
127 | // timers: this used to work in previous versions but was never supported | |
128 | // and absolutely shouldn't be done, use wxID_ANY to assign an id to the | |
129 | // timer automatically or ensure that all your timers have unique ids | |
ee1025a7 | 130 | if ( TimerMap().find(m_id) != TimerMap().end() ) |
97f278b4 VZ |
131 | { |
132 | wxLogError(_("Timer creation failed.")); | |
133 | ||
9cfc7f4f | 134 | ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id); |
97f278b4 VZ |
135 | m_id = 0; |
136 | ||
137 | return false; | |
138 | } | |
139 | ||
ee1025a7 | 140 | TimerMap()[m_id] = this; |
eccd1992 VZ |
141 | |
142 | return true; | |
2bda0e17 KB |
143 | } |
144 | ||
c2ca375c | 145 | void wxMSWTimerImpl::Stop() |
2bda0e17 | 146 | { |
c2ca375c | 147 | wxASSERT_MSG( m_id, _T("should be running") ); |
62f17a18 | 148 | |
9cfc7f4f | 149 | ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id); |
c2ca375c VZ |
150 | |
151 | TimerMap().erase(m_id); | |
0470b1e6 VZ |
152 | |
153 | m_id = 0; | |
2bda0e17 KB |
154 | } |
155 | ||
c085e333 VZ |
156 | // ---------------------------------------------------------------------------- |
157 | // private functions | |
158 | // ---------------------------------------------------------------------------- | |
73974df1 | 159 | |
c2ca375c | 160 | void wxProcessTimer(wxMSWTimerImpl& timer) |
2bda0e17 | 161 | { |
c2ca375c | 162 | wxASSERT_MSG( timer.IsRunning(), _T("bogus timer id") ); |
c085e333 | 163 | |
0470b1e6 | 164 | if ( timer.IsOneShot() ) |
c085e333 VZ |
165 | timer.Stop(); |
166 | ||
167 | timer.Notify(); | |
2bda0e17 KB |
168 | } |
169 | ||
9cfc7f4f RD |
170 | |
171 | LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message, | |
172 | WPARAM wParam, LPARAM lParam) | |
173 | { | |
174 | if ( message == WM_TIMER ) | |
175 | { | |
491acfc7 | 176 | wxTimerMap::iterator node = TimerMap().find(wParam); |
9cfc7f4f RD |
177 | |
178 | wxCHECK_MSG( node != TimerMap().end(), 0, wxT("bogus timer id in wxTimerProc") ); | |
179 | ||
180 | wxProcessTimer(*(node->second)); | |
181 | } | |
182 | else | |
183 | { | |
184 | return ::DefWindowProc(hWnd, message, wParam, lParam); | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
190 | // wxTimerHiddenWindowModule functions | |
191 | // ---------------------------------------------------------------------------- | |
192 | ||
193 | ||
194 | HWND wxTimerHiddenWindowModule::ms_hwnd = NULL; | |
195 | ||
196 | const wxChar *wxTimerHiddenWindowModule::ms_className = NULL; | |
197 | ||
198 | bool wxTimerHiddenWindowModule::OnInit() | |
199 | { | |
24a21a5f VZ |
200 | // do not initialize ms_hwnd to ms_className to NULL here: it may happen |
201 | // that our GetHWND() is called before the modules are initialized if a | |
202 | // timer is created from wxApp-derived class ctor and in this case we | |
203 | // shouldn't overwrite it | |
9cfc7f4f RD |
204 | |
205 | return true; | |
206 | } | |
207 | ||
208 | void wxTimerHiddenWindowModule::OnExit() | |
c085e333 | 209 | { |
9cfc7f4f RD |
210 | if ( ms_hwnd ) |
211 | { | |
212 | if ( !::DestroyWindow(ms_hwnd) ) | |
213 | { | |
214 | wxLogLastError(_T("DestroyWindow(wxTimerHiddenWindow)")); | |
215 | } | |
216 | ||
217 | ms_hwnd = NULL; | |
218 | } | |
c085e333 | 219 | |
9cfc7f4f RD |
220 | if ( ms_className ) |
221 | { | |
222 | if ( !::UnregisterClass(ms_className, wxGetInstance()) ) | |
223 | { | |
224 | wxLogLastError(_T("UnregisterClass(\"wxTimerHiddenWindow\")")); | |
225 | } | |
226 | ||
227 | ms_className = NULL; | |
228 | } | |
229 | } | |
230 | ||
231 | /* static */ | |
232 | HWND wxTimerHiddenWindowModule::GetHWND() | |
233 | { | |
234 | static const wxChar *HIDDEN_WINDOW_CLASS = _T("wxTimerHiddenWindow"); | |
235 | if ( !ms_hwnd ) | |
236 | { | |
237 | ms_hwnd = wxCreateHiddenWindow(&ms_className, HIDDEN_WINDOW_CLASS, | |
238 | wxTimerWndProc); | |
239 | } | |
c085e333 | 240 | |
9cfc7f4f | 241 | return ms_hwnd; |
c085e333 | 242 | } |
1e6feb95 VZ |
243 | |
244 | #endif // wxUSE_TIMER |