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