]>
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/window.h"
30 #include "wx/hashmap.h"
34 #include "wx/msw/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // define a hash containing all the timers: it is indexed by timer id and
41 // contains the corresponding timer
42 WX_DECLARE_HASH_MAP(unsigned long, wxTimer
*, wxIntegerHash
, wxIntegerEqual
,
45 // instead of using a global here, wrap it in a static function as otherwise it
46 // could have been used before being initialized if a timer object were created
48 static wxTimerMap
& TimerMap()
50 static wxTimerMap s_timerMap
;
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // timer callback used for all timers
60 void WINAPI
wxTimerProc(HWND hwnd
, UINT msg
, UINT_PTR idTimer
, DWORD dwTime
);
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
86 bool wxTimer::Start(int milliseconds
, bool oneShot
)
88 (void)wxTimerBase::Start(milliseconds
, oneShot
);
90 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
94 NULL
, // don't use window
95 1, // id ignored with NULL hwnd anyhow
96 (UINT
)m_milli
, // delay
97 wxTimerProc
// timer proc to call
102 wxLogSysError(_("Couldn't create a timer"));
107 // check that SetTimer() didn't reuse an existing id: according to the MSDN
108 // this can happen and this would be catastrophic to us as we rely on ids
109 // uniquely identifying the timers because we use them as keys in the hash
110 if ( TimerMap().find(m_id
) != TimerMap().end() )
112 wxLogError(_("Timer creation failed."));
114 ::KillTimer(NULL
, m_id
);
120 TimerMap()[m_id
] = this;
129 ::KillTimer(NULL
, m_id
);
131 TimerMap().erase(m_id
);
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 void wxProcessTimer(wxTimer
& timer
)
143 wxASSERT_MSG( timer
.m_id
!= 0, _T("bogus timer id") );
145 if ( timer
.IsOneShot() )
152 wxTimerProc(HWND
WXUNUSED(hwnd
),
155 DWORD
WXUNUSED(dwTime
))
157 wxTimerMap::iterator node
= TimerMap().find((unsigned long)idTimer
);
159 wxCHECK_RET( node
!= TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
161 wxProcessTimer(*(node
->second
));
164 #endif // wxUSE_TIMER