]>
git.saurik.com Git - wxWidgets.git/blob - 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)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/window.h"
30 #include "wx/hashmap.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(unsigned long, wxTimer
*, 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 // timer callback used for all timers
59 void WINAPI
wxTimerProc(HWND hwnd
, UINT msg
, UINT_PTR idTimer
, DWORD dwTime
);
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
85 bool wxTimer::Start(int milliseconds
, bool oneShot
)
87 (void)wxTimerBase::Start(milliseconds
, oneShot
);
89 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
93 NULL
, // don't use window
94 1, // id ignored with NULL hwnd anyhow
95 (UINT
)m_milli
, // delay
96 wxTimerProc
// timer proc to call
101 wxLogSysError(_("Couldn't create a timer"));
106 // check that SetTimer() didn't reuse an existing id: according to the MSDN
107 // this can happen and this would be catastrophic to us as we rely on ids
108 // uniquely identifying the timers because we use them as keys in the hash
109 if ( TimerMap().find(m_id
) != TimerMap().end() )
111 wxLogError(_("Timer creation failed."));
113 ::KillTimer(NULL
, m_id
);
119 TimerMap()[m_id
] = this;
128 ::KillTimer(NULL
, m_id
);
130 TimerMap().erase(m_id
);
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 void wxProcessTimer(wxTimer
& timer
)
142 wxASSERT_MSG( timer
.m_id
!= 0, _T("bogus timer id") );
144 if ( timer
.IsOneShot() )
151 wxTimerProc(HWND
WXUNUSED(hwnd
),
154 DWORD
WXUNUSED(dwTime
))
156 wxTimerMap::iterator node
= TimerMap().find((unsigned long)idTimer
);
158 wxCHECK_RET( node
!= TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
160 wxProcessTimer(*(node
->second
));
163 #endif // wxUSE_TIMER