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