]> git.saurik.com Git - wxWidgets.git/blame - src/msw/timer.cpp
more wxSocket code wx-ification: use wxDynamicLibrary instead of raw Win32 calls
[wxWidgets.git] / src / msw / timer.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
8ecff181 2// Name: src/msw/timer.cpp
2bda0e17
KB
3// Purpose: wxTimer implementation
4// Author: Julian Smart
6d20f7ae 5// Modified by: Vadim Zeitlin (use hash map instead of list, global rewrite)
2bda0e17
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
c085e333 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
1e6feb95
VZ
19#if wxUSE_TIMER
20
c2ca375c 21#include "wx/msw/private/timer.h"
c0badb70 22
2bda0e17 23#ifndef WX_PRECOMP
c085e333 24 #include "wx/list.h"
2432b92d 25 #include "wx/event.h"
c085e333 26 #include "wx/app.h"
0470b1e6
VZ
27 #include "wx/intl.h"
28 #include "wx/log.h"
df69528b 29 #include "wx/hashmap.h"
2bda0e17
KB
30#endif
31
0470b1e6 32#include "wx/msw/private.h"
2bda0e17 33
c085e333 34// ----------------------------------------------------------------------------
97f278b4 35// private globals
c085e333
VZ
36// ----------------------------------------------------------------------------
37
97f278b4
VZ
38// define a hash containing all the timers: it is indexed by timer id and
39// contains the corresponding timer
c2ca375c 40WX_DECLARE_HASH_MAP(unsigned long, wxMSWTimerImpl *, wxIntegerHash, wxIntegerEqual,
3f5c62f9 41 wxTimerMap);
5b6c8794 42
ee1025a7
VZ
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
45// globally
46static wxTimerMap& TimerMap()
47{
48 static wxTimerMap s_timerMap;
49
50 return s_timerMap;
51}
4676948b 52
c085e333 53// ----------------------------------------------------------------------------
97f278b4 54// private functions
eccd1992
VZ
55// ----------------------------------------------------------------------------
56
6d20f7ae 57// timer callback used for all timers
106d80ad 58void WINAPI wxTimerProc(HWND hwnd, UINT msg, UINT_PTR idTimer, DWORD dwTime);
eccd1992 59
c085e333
VZ
60// ============================================================================
61// implementation
62// ============================================================================
63
64// ----------------------------------------------------------------------------
c2ca375c 65// wxMSWTimerImpl class
c085e333 66// ----------------------------------------------------------------------------
73974df1 67
c2ca375c 68bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot)
2bda0e17 69{
c2ca375c
VZ
70 if ( !wxTimerImpl::Start(milliseconds, oneShot) )
71 return false;
caa36aab 72
eccd1992
VZ
73 m_id = ::SetTimer
74 (
97f278b4
VZ
75 NULL, // don't use window
76 1, // id ignored with NULL hwnd anyhow
77 (UINT)m_milli, // delay
6d20f7ae 78 wxTimerProc // timer proc to call
eccd1992
VZ
79 );
80
81 if ( !m_id )
c085e333
VZ
82 {
83 wxLogSysError(_("Couldn't create a timer"));
84
04cd30de 85 return false;
c085e333 86 }
eccd1992 87
97f278b4
VZ
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
ee1025a7 91 if ( TimerMap().find(m_id) != TimerMap().end() )
97f278b4
VZ
92 {
93 wxLogError(_("Timer creation failed."));
94
95 ::KillTimer(NULL, m_id);
96 m_id = 0;
97
98 return false;
99 }
100
ee1025a7 101 TimerMap()[m_id] = this;
eccd1992
VZ
102
103 return true;
2bda0e17
KB
104}
105
c2ca375c 106void wxMSWTimerImpl::Stop()
2bda0e17 107{
c2ca375c 108 wxASSERT_MSG( m_id, _T("should be running") );
62f17a18 109
c2ca375c
VZ
110 ::KillTimer(NULL, m_id);
111
112 TimerMap().erase(m_id);
0470b1e6
VZ
113
114 m_id = 0;
2bda0e17
KB
115}
116
c085e333
VZ
117// ----------------------------------------------------------------------------
118// private functions
119// ----------------------------------------------------------------------------
73974df1 120
c2ca375c 121void wxProcessTimer(wxMSWTimerImpl& timer)
2bda0e17 122{
c2ca375c 123 wxASSERT_MSG( timer.IsRunning(), _T("bogus timer id") );
c085e333 124
0470b1e6 125 if ( timer.IsOneShot() )
c085e333
VZ
126 timer.Stop();
127
128 timer.Notify();
2bda0e17
KB
129}
130
6d20f7ae
VZ
131void WINAPI
132wxTimerProc(HWND WXUNUSED(hwnd),
133 UINT WXUNUSED(msg),
106d80ad 134 UINT_PTR idTimer,
6d20f7ae 135 DWORD WXUNUSED(dwTime))
c085e333 136{
ee1025a7 137 wxTimerMap::iterator node = TimerMap().find((unsigned long)idTimer);
c085e333 138
ee1025a7 139 wxCHECK_RET( node != TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
c085e333 140
222ed1d6 141 wxProcessTimer(*(node->second));
c085e333 142}
1e6feb95
VZ
143
144#endif // wxUSE_TIMER