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)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/msw/private/timer.h"
29 #include "wx/hashmap.h"
30 #include "wx/module.h"
33 #include "wx/msw/private.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
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
,
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
47 static wxTimerMap
& TimerMap()
49 static wxTimerMap s_timerMap
;
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 LRESULT APIENTRY _EXPORT
wxTimerWndProc(HWND hWnd
, UINT message
,
59 WPARAM wParam
, LPARAM lParam
);
61 // implemented in utils.cpp
62 extern "C" WXDLLIMPEXP_BASE HWND
63 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
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
70 // ----------------------------------------------------------------------------
72 class wxTimerHiddenWindowModule
: public wxModule
75 // module init/finalize
76 virtual bool OnInit();
77 virtual void OnExit();
79 // get the hidden window (creates on demand)
80 static HWND
GetHWND();
83 // the HWND of the hidden window
86 // the class used to create it
87 static const wxChar
*ms_className
;
89 DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule
)
92 IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule
, wxModule
)
94 // ============================================================================
96 // ============================================================================
99 // ----------------------------------------------------------------------------
100 // wxMSWTimerImpl class
101 // ----------------------------------------------------------------------------
103 bool wxMSWTimerImpl::Start(int milliseconds
, bool oneShot
)
105 if ( !wxTimerImpl::Start(milliseconds
, oneShot
) )
109 wxTimerHiddenWindowModule::GetHWND(), // window to send the messages to
111 (UINT
)m_milli
, // delay
112 NULL
// timer proc. Not used since we pass hwnd
117 wxLogSysError(_("Couldn't create a timer"));
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
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
130 if ( TimerMap().find(m_id
) != TimerMap().end() )
132 wxLogError(_("Timer creation failed."));
134 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id
);
140 TimerMap()[m_id
] = this;
145 void wxMSWTimerImpl::Stop()
147 wxASSERT_MSG( m_id
, _T("should be running") );
149 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id
);
151 TimerMap().erase(m_id
);
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 void wxProcessTimer(wxMSWTimerImpl
& timer
)
162 wxASSERT_MSG( timer
.IsRunning(), _T("bogus timer id") );
164 if ( timer
.IsOneShot() )
171 LRESULT APIENTRY _EXPORT
wxTimerWndProc(HWND hWnd
, UINT message
,
172 WPARAM wParam
, LPARAM lParam
)
174 if ( message
== WM_TIMER
)
176 wxTimerMap::iterator node
= TimerMap().find(wParam
);
178 wxCHECK_MSG( node
!= TimerMap().end(), 0, wxT("bogus timer id in wxTimerProc") );
180 wxProcessTimer(*(node
->second
));
184 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
189 // ----------------------------------------------------------------------------
190 // wxTimerHiddenWindowModule functions
191 // ----------------------------------------------------------------------------
194 HWND
wxTimerHiddenWindowModule::ms_hwnd
= NULL
;
196 const wxChar
*wxTimerHiddenWindowModule::ms_className
= NULL
;
198 bool wxTimerHiddenWindowModule::OnInit()
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
208 void wxTimerHiddenWindowModule::OnExit()
212 if ( !::DestroyWindow(ms_hwnd
) )
214 wxLogLastError(_T("DestroyWindow(wxTimerHiddenWindow)"));
222 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
224 wxLogLastError(_T("UnregisterClass(\"wxTimerHiddenWindow\")"));
232 HWND
wxTimerHiddenWindowModule::GetHWND()
234 static const wxChar
*HIDDEN_WINDOW_CLASS
= _T("wxTimerHiddenWindow");
237 ms_hwnd
= wxCreateHiddenWindow(&ms_className
, HIDDEN_WINDOW_CLASS
,
244 #endif // wxUSE_TIMER