]> git.saurik.com Git - wxWidgets.git/blob - src/msw/timer.cpp
Improve wxCheckListBox appearance under Vista/Win7.
[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 // ----------------------------------------------------------------------------
55 // private functions
56 // ----------------------------------------------------------------------------
57
58 LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
59 WPARAM wParam, LPARAM lParam);
60
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)
93
94 // ============================================================================
95 // implementation
96 // ============================================================================
97
98
99 // ----------------------------------------------------------------------------
100 // wxMSWTimerImpl class
101 // ----------------------------------------------------------------------------
102
103 bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot)
104 {
105 if ( !wxTimerImpl::Start(milliseconds, oneShot) )
106 return false;
107
108 // SetTimer() doesn't accept 0 timer id so use something else if the timer
109 // id at wx level is 0: as -1 (wxID_ANY) can't be used, we can safely
110 // replace 0 with it at MSW level
111 UINT idTimer = GetId();
112 if ( !idTimer )
113 idTimer = (UINT)-1;
114
115 // SetTimer() normally returns just idTimer but this might change in the
116 // future so use its return value to be safe
117 m_id = ::SetTimer
118 (
119 wxTimerHiddenWindowModule::GetHWND(), // window for WM_TIMER
120 idTimer, // timer ID to create
121 (UINT)m_milli, // delay
122 NULL // timer proc (unused)
123 );
124
125 if ( !m_id )
126 {
127 wxLogSysError(_("Couldn't create a timer"));
128
129 return false;
130 }
131
132 // check that SetTimer() didn't reuse an existing id: according to the MSDN
133 // this can happen and this would be catastrophic to us as we rely on ids
134 // uniquely identifying the timers because we use them as keys in the hash
135 //
136 // notice that this also happens if the same id is reused for multiple
137 // timers: this used to work in previous versions but was never supported
138 // and absolutely shouldn't be done, use wxID_ANY to assign an id to the
139 // timer automatically or ensure that all your timers have unique ids
140 if ( TimerMap().find(m_id) != TimerMap().end() )
141 {
142 wxLogError(_("Timer creation failed."));
143
144 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id);
145 m_id = 0;
146
147 return false;
148 }
149
150 TimerMap()[m_id] = this;
151
152 return true;
153 }
154
155 void wxMSWTimerImpl::Stop()
156 {
157 wxASSERT_MSG( m_id, wxT("should be running") );
158
159 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id);
160
161 TimerMap().erase(m_id);
162
163 m_id = 0;
164 }
165
166 // ----------------------------------------------------------------------------
167 // private functions
168 // ----------------------------------------------------------------------------
169
170 void wxProcessTimer(wxMSWTimerImpl& timer)
171 {
172 wxASSERT_MSG( timer.IsRunning(), wxT("bogus timer id") );
173
174 if ( timer.IsOneShot() )
175 timer.Stop();
176
177 timer.Notify();
178 }
179
180
181 LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
182 WPARAM wParam, LPARAM lParam)
183 {
184 if ( message == WM_TIMER )
185 {
186 wxTimerMap::iterator node = TimerMap().find(wParam);
187
188 wxCHECK_MSG( node != TimerMap().end(), 0, wxT("bogus timer id in wxTimerProc") );
189
190 wxProcessTimer(*(node->second));
191 }
192 else
193 {
194 return ::DefWindowProc(hWnd, message, wParam, lParam);
195 }
196 return 0;
197 }
198
199 // ----------------------------------------------------------------------------
200 // wxTimerHiddenWindowModule functions
201 // ----------------------------------------------------------------------------
202
203
204 HWND wxTimerHiddenWindowModule::ms_hwnd = NULL;
205
206 const wxChar *wxTimerHiddenWindowModule::ms_className = NULL;
207
208 bool wxTimerHiddenWindowModule::OnInit()
209 {
210 // do not initialize ms_hwnd to ms_className to NULL here: it may happen
211 // that our GetHWND() is called before the modules are initialized if a
212 // timer is created from wxApp-derived class ctor and in this case we
213 // shouldn't overwrite it
214
215 return true;
216 }
217
218 void wxTimerHiddenWindowModule::OnExit()
219 {
220 if ( ms_hwnd )
221 {
222 if ( !::DestroyWindow(ms_hwnd) )
223 {
224 wxLogLastError(wxT("DestroyWindow(wxTimerHiddenWindow)"));
225 }
226
227 ms_hwnd = NULL;
228 }
229
230 if ( ms_className )
231 {
232 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
233 {
234 wxLogLastError(wxT("UnregisterClass(\"wxTimerHiddenWindow\")"));
235 }
236
237 ms_className = NULL;
238 }
239 }
240
241 /* static */
242 HWND wxTimerHiddenWindowModule::GetHWND()
243 {
244 static const wxChar *HIDDEN_WINDOW_CLASS = wxT("wxTimerHiddenWindow");
245 if ( !ms_hwnd )
246 {
247 ms_hwnd = wxCreateHiddenWindow(&ms_className, HIDDEN_WINDOW_CLASS,
248 wxTimerWndProc);
249 }
250
251 return ms_hwnd;
252 }
253
254 #endif // wxUSE_TIMER