]> git.saurik.com Git - wxWidgets.git/blame - src/msw/timer.cpp
fixed LastRead() after Read(wxOutputStream&) (patch 1658301)
[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
c0badb70
WS
21#include "wx/timer.h"
22
2bda0e17 23#ifndef WX_PRECOMP
c085e333 24 #include "wx/list.h"
8ecff181 25 #include "wx/window.h"
2432b92d 26 #include "wx/event.h"
c085e333 27 #include "wx/app.h"
0470b1e6
VZ
28 #include "wx/intl.h"
29 #include "wx/log.h"
df69528b 30 #include "wx/hashmap.h"
2bda0e17
KB
31#endif
32
0470b1e6 33#include "wx/msw/private.h"
2bda0e17 34
c085e333 35// ----------------------------------------------------------------------------
97f278b4 36// private globals
c085e333
VZ
37// ----------------------------------------------------------------------------
38
97f278b4
VZ
39// define a hash containing all the timers: it is indexed by timer id and
40// contains the corresponding timer
41WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual,
3f5c62f9 42 wxTimerMap);
5b6c8794 43
ee1025a7
VZ
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
46// globally
47static wxTimerMap& TimerMap()
48{
49 static wxTimerMap s_timerMap;
50
51 return s_timerMap;
52}
4676948b 53
c085e333 54// ----------------------------------------------------------------------------
97f278b4 55// private functions
eccd1992
VZ
56// ----------------------------------------------------------------------------
57
6d20f7ae 58// timer callback used for all timers
106d80ad 59void WINAPI wxTimerProc(HWND hwnd, UINT msg, UINT_PTR idTimer, DWORD dwTime);
eccd1992
VZ
60
61// ----------------------------------------------------------------------------
97f278b4 62// macros
eccd1992
VZ
63// ----------------------------------------------------------------------------
64
313feadc 65IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
eccd1992 66
c085e333
VZ
67// ============================================================================
68// implementation
69// ============================================================================
70
71// ----------------------------------------------------------------------------
72// wxTimer class
73// ----------------------------------------------------------------------------
73974df1 74
ed791986 75void wxTimer::Init()
2bda0e17 76{
0470b1e6 77 m_id = 0;
2bda0e17
KB
78}
79
73974df1 80wxTimer::~wxTimer()
2bda0e17 81{
0470b1e6 82 wxTimer::Stop();
2bda0e17
KB
83}
84
0470b1e6 85bool wxTimer::Start(int milliseconds, bool oneShot)
2bda0e17 86{
eccd1992 87 (void)wxTimerBase::Start(milliseconds, oneShot);
c085e333 88
3c907f1a 89 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
caa36aab 90
eccd1992
VZ
91 m_id = ::SetTimer
92 (
97f278b4
VZ
93 NULL, // don't use window
94 1, // id ignored with NULL hwnd anyhow
95 (UINT)m_milli, // delay
6d20f7ae 96 wxTimerProc // timer proc to call
eccd1992
VZ
97 );
98
99 if ( !m_id )
c085e333
VZ
100 {
101 wxLogSysError(_("Couldn't create a timer"));
102
04cd30de 103 return false;
c085e333 104 }
eccd1992 105
97f278b4
VZ
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
ee1025a7 109 if ( TimerMap().find(m_id) != TimerMap().end() )
97f278b4
VZ
110 {
111 wxLogError(_("Timer creation failed."));
112
113 ::KillTimer(NULL, m_id);
114 m_id = 0;
115
116 return false;
117 }
118
ee1025a7 119 TimerMap()[m_id] = this;
eccd1992
VZ
120
121 return true;
2bda0e17
KB
122}
123
73974df1 124void wxTimer::Stop()
2bda0e17 125{
0470b1e6 126 if ( m_id )
73974df1 127 {
97f278b4 128 ::KillTimer(NULL, m_id);
62f17a18 129
ee1025a7 130 TimerMap().erase(m_id);
c085e333 131 }
0470b1e6
VZ
132
133 m_id = 0;
2bda0e17
KB
134}
135
c085e333
VZ
136// ----------------------------------------------------------------------------
137// private functions
138// ----------------------------------------------------------------------------
73974df1 139
06e38c8e 140void wxProcessTimer(wxTimer& timer)
2bda0e17 141{
5b6c8794 142 wxASSERT_MSG( timer.m_id != 0, _T("bogus timer id") );
c085e333 143
0470b1e6 144 if ( timer.IsOneShot() )
c085e333
VZ
145 timer.Stop();
146
147 timer.Notify();
2bda0e17
KB
148}
149
6d20f7ae
VZ
150void WINAPI
151wxTimerProc(HWND WXUNUSED(hwnd),
152 UINT WXUNUSED(msg),
106d80ad 153 UINT_PTR idTimer,
6d20f7ae 154 DWORD WXUNUSED(dwTime))
c085e333 155{
ee1025a7 156 wxTimerMap::iterator node = TimerMap().find((unsigned long)idTimer);
c085e333 157
ee1025a7 158 wxCHECK_RET( node != TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
c085e333 159
222ed1d6 160 wxProcessTimer(*(node->second));
c085e333 161}
1e6feb95
VZ
162
163#endif // wxUSE_TIMER