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"
32 #include "wx/msw/private.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
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,
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
46 static wxTimerMap& TimerMap()
48 static wxTimerMap s_timerMap;
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // timer callback used for all timers
58 void WINAPI wxTimerProc(HWND hwnd, UINT msg, UINT_PTR idTimer, DWORD dwTime);
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxMSWTimerImpl class
66 // ----------------------------------------------------------------------------
68 bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot)
70 if ( !wxTimerImpl::Start(milliseconds, oneShot) )
75 NULL, // don't use window
76 1, // id ignored with NULL hwnd anyhow
77 (UINT)m_milli, // delay
78 wxTimerProc // timer proc to call
83 wxLogSysError(_("Couldn't create a timer"));
88 // check that SetTimer() didn't reuse an existing id: according to the MSDN
89 // this can happen and this would be catastrophic to us as we rely on ids
90 // uniquely identifying the timers because we use them as keys in the hash
91 if ( TimerMap().find(m_id) != TimerMap().end() )
93 wxLogError(_("Timer creation failed."));
95 ::KillTimer(NULL, m_id);
101 TimerMap()[m_id] = this;
106 void wxMSWTimerImpl::Stop()
108 wxASSERT_MSG( m_id, _T("should be running") );
110 ::KillTimer(NULL, m_id);
112 TimerMap().erase(m_id);
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 void wxProcessTimer(wxMSWTimerImpl& timer)
123 wxASSERT_MSG( timer.IsRunning(), _T("bogus timer id") );
125 if ( timer.IsOneShot() )
132 wxTimerProc(HWND WXUNUSED(hwnd),
135 DWORD WXUNUSED(dwTime))
137 wxTimerMap::iterator node = TimerMap().find((unsigned long)idTimer);
139 wxCHECK_RET( node != TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
141 wxProcessTimer(*(node->second));
144 #endif // wxUSE_TIMER