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