Allow more than one timer with the same ID. Closes #11699.
[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 #include "wx/module.h"
31 #endif
32
33 #include "wx/msw/private.h"
34
35 // ----------------------------------------------------------------------------
36 // private globals
37 // ----------------------------------------------------------------------------
38
39 // define a hash containing all the timers: it is indexed by timer id and
40 // contains the corresponding timer
41 WX_DECLARE_HASH_MAP(WPARAM, wxMSWTimerImpl *, wxIntegerHash, wxIntegerEqual,
42 wxTimerMap);
43
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 }
53
54 // This gets a unique, non-zero timer ID and creates an entry in the TimerMap
55 UINT_PTR GetNewTimerId(wxMSWTimerImpl *t)
56 {
57 static UINT_PTR lastTimerId = 0;
58
59 while (lastTimerId == 0 ||
60 TimerMap().find(lastTimerId) != TimerMap().end())
61 {
62 lastTimerId = lastTimerId + 1;
63 }
64
65 TimerMap()[lastTimerId] = t;
66
67 return lastTimerId;
68 }
69
70
71
72 // ----------------------------------------------------------------------------
73 // private functions
74 // ----------------------------------------------------------------------------
75
76 LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
77 WPARAM wParam, LPARAM lParam);
78
79 // implemented in utils.cpp
80 extern "C" WXDLLIMPEXP_BASE HWND
81 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
82
83
84 // ----------------------------------------------------------------------------
85 // wxTimerHiddenWindowModule: used to manage the hidden window used for
86 // catching timer messages (we need a module to ensure that the window is
87 // always deleted)
88 // ----------------------------------------------------------------------------
89
90 class wxTimerHiddenWindowModule : public wxModule
91 {
92 public:
93 // module init/finalize
94 virtual bool OnInit();
95 virtual void OnExit();
96
97 // get the hidden window (creates on demand)
98 static HWND GetHWND();
99
100 private:
101 // the HWND of the hidden window
102 static HWND ms_hwnd;
103
104 // the class used to create it
105 static const wxChar *ms_className;
106
107 DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule)
108 };
109
110 IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule, wxModule)
111
112 // ============================================================================
113 // implementation
114 // ============================================================================
115
116
117 // ----------------------------------------------------------------------------
118 // wxMSWTimerImpl class
119 // ----------------------------------------------------------------------------
120
121 wxMSWTimerImpl::wxMSWTimerImpl(wxTimer *timer)
122 :wxTimerImpl(timer)
123 {
124 m_id = GetNewTimerId(this);
125 }
126
127 wxMSWTimerImpl::~wxMSWTimerImpl()
128 {
129 TimerMap().erase(m_id);
130 }
131
132 bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot)
133 {
134 if ( !wxTimerImpl::Start(milliseconds, oneShot) )
135 return false;
136
137 // SetTimer() normally returns just idTimer but this might change in the
138 // future so use its return value to be safe
139 UINT_PTR ret = ::SetTimer
140 (
141 wxTimerHiddenWindowModule::GetHWND(), // window for WM_TIMER
142 m_id, // timer ID to create
143 (UINT)m_milli, // delay
144 NULL // timer proc (unused)
145 );
146
147 if ( ret == 0 )
148 {
149 wxLogSysError(_("Couldn't create a timer"));
150
151 return false;
152 }
153
154 return true;
155 }
156
157 void wxMSWTimerImpl::Stop()
158 {
159 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id);
160 }
161
162 // ----------------------------------------------------------------------------
163 // private functions
164 // ----------------------------------------------------------------------------
165
166 void wxProcessTimer(wxMSWTimerImpl& timer)
167 {
168 wxASSERT_MSG( timer.IsRunning(), wxT("bogus timer id") );
169
170 if ( timer.IsOneShot() )
171 timer.Stop();
172
173 timer.Notify();
174 }
175
176
177 LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
178 WPARAM wParam, LPARAM lParam)
179 {
180 if ( message == WM_TIMER )
181 {
182 wxTimerMap::iterator node = TimerMap().find(wParam);
183
184 wxCHECK_MSG( node != TimerMap().end(), 0, wxT("bogus timer id in wxTimerProc") );
185
186 wxProcessTimer(*(node->second));
187 }
188 else
189 {
190 return ::DefWindowProc(hWnd, message, wParam, lParam);
191 }
192 return 0;
193 }
194
195 // ----------------------------------------------------------------------------
196 // wxTimerHiddenWindowModule functions
197 // ----------------------------------------------------------------------------
198
199
200 HWND wxTimerHiddenWindowModule::ms_hwnd = NULL;
201
202 const wxChar *wxTimerHiddenWindowModule::ms_className = NULL;
203
204 bool wxTimerHiddenWindowModule::OnInit()
205 {
206 // do not initialize ms_hwnd to ms_className to NULL here: it may happen
207 // that our GetHWND() is called before the modules are initialized if a
208 // timer is created from wxApp-derived class ctor and in this case we
209 // shouldn't overwrite it
210
211 return true;
212 }
213
214 void wxTimerHiddenWindowModule::OnExit()
215 {
216 if ( ms_hwnd )
217 {
218 if ( !::DestroyWindow(ms_hwnd) )
219 {
220 wxLogLastError(wxT("DestroyWindow(wxTimerHiddenWindow)"));
221 }
222
223 ms_hwnd = NULL;
224 }
225
226 if ( ms_className )
227 {
228 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
229 {
230 wxLogLastError(wxT("UnregisterClass(\"wxTimerHiddenWindow\")"));
231 }
232
233 ms_className = NULL;
234 }
235 }
236
237 /* static */
238 HWND wxTimerHiddenWindowModule::GetHWND()
239 {
240 static const wxChar *HIDDEN_WINDOW_CLASS = wxT("wxTimerHiddenWindow");
241 if ( !ms_hwnd )
242 {
243 ms_hwnd = wxCreateHiddenWindow(&ms_className, HIDDEN_WINDOW_CLASS,
244 wxTimerWndProc);
245 }
246
247 return ms_hwnd;
248 }
249
250 #endif // wxUSE_TIMER