]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "timer.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/window.h"
34 #include "wx/hashmap.h"
38 #include "wx/msw/private.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // define a hash containing all the timers: it is indexed by timer id and
45 // contains the corresponding timer
46 WX_DECLARE_HASH_MAP(unsigned long, wxTimer
*, wxIntegerHash
, wxIntegerEqual
,
49 // instead of using a global here, wrap it in a static function as otherwise it
50 // could have been used before being initialized if a timer object were created
52 static wxTimerMap
& TimerMap()
54 static wxTimerMap s_timerMap
;
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // timer callback used for all timers
64 void WINAPI
wxTimerProc(HWND hwnd
, UINT msg
, UINT idTimer
, DWORD dwTime
);
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
72 // ============================================================================
74 // ============================================================================
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
90 bool wxTimer::Start(int milliseconds
, bool oneShot
)
92 (void)wxTimerBase::Start(milliseconds
, oneShot
);
94 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
98 NULL
, // don't use window
99 1, // id ignored with NULL hwnd anyhow
100 (UINT
)m_milli
, // delay
101 wxTimerProc
// timer proc to call
106 wxLogSysError(_("Couldn't create a timer"));
111 // check that SetTimer() didn't reuse an existing id: according to the MSDN
112 // this can happen and this would be catastrophic to us as we rely on ids
113 // uniquely identifying the timers because we use them as keys in the hash
114 if ( TimerMap().find(m_id
) != TimerMap().end() )
116 wxLogError(_("Timer creation failed."));
118 ::KillTimer(NULL
, m_id
);
124 TimerMap()[m_id
] = this;
133 ::KillTimer(NULL
, m_id
);
135 TimerMap().erase(m_id
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 void wxProcessTimer(wxTimer
& timer
)
147 wxASSERT_MSG( timer
.m_id
!= 0, _T("bogus timer id") );
149 if ( timer
.IsOneShot() )
156 wxTimerProc(HWND
WXUNUSED(hwnd
),
159 DWORD
WXUNUSED(dwTime
))
161 wxTimerMap::iterator node
= TimerMap().find((unsigned long)idTimer
);
163 wxCHECK_RET( node
!= TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
165 wxProcessTimer(*(node
->second
));
168 #endif // wxUSE_TIMER