]> git.saurik.com Git - wxWidgets.git/blob - src/msw/timer.cpp
fixed LastRead() after Read(wxOutputStream&) (patch 1658301)
[wxWidgets.git] / src / msw / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (use hash map instead of list, global rewrite)
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_TIMER
20
21 #include "wx/timer.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/list.h"
25 #include "wx/window.h"
26 #include "wx/event.h"
27 #include "wx/app.h"
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #include "wx/hashmap.h"
31 #endif
32
33 #include "wx/msw/private.h"
34
35 // ----------------------------------------------------------------------------
36 // private globals
37 // ----------------------------------------------------------------------------
38
39 // define a hash containing all the timers: it is indexed by timer id and
40 // contains the corresponding timer
41 WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual,
42 wxTimerMap);
43
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
47 static wxTimerMap& TimerMap()
48 {
49 static wxTimerMap s_timerMap;
50
51 return s_timerMap;
52 }
53
54 // ----------------------------------------------------------------------------
55 // private functions
56 // ----------------------------------------------------------------------------
57
58 // timer callback used for all timers
59 void WINAPI wxTimerProc(HWND hwnd, UINT msg, UINT_PTR idTimer, DWORD dwTime);
60
61 // ----------------------------------------------------------------------------
62 // macros
63 // ----------------------------------------------------------------------------
64
65 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
66
67 // ============================================================================
68 // implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxTimer class
73 // ----------------------------------------------------------------------------
74
75 void wxTimer::Init()
76 {
77 m_id = 0;
78 }
79
80 wxTimer::~wxTimer()
81 {
82 wxTimer::Stop();
83 }
84
85 bool wxTimer::Start(int milliseconds, bool oneShot)
86 {
87 (void)wxTimerBase::Start(milliseconds, oneShot);
88
89 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
90
91 m_id = ::SetTimer
92 (
93 NULL, // don't use window
94 1, // id ignored with NULL hwnd anyhow
95 (UINT)m_milli, // delay
96 wxTimerProc // timer proc to call
97 );
98
99 if ( !m_id )
100 {
101 wxLogSysError(_("Couldn't create a timer"));
102
103 return false;
104 }
105
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
109 if ( TimerMap().find(m_id) != TimerMap().end() )
110 {
111 wxLogError(_("Timer creation failed."));
112
113 ::KillTimer(NULL, m_id);
114 m_id = 0;
115
116 return false;
117 }
118
119 TimerMap()[m_id] = this;
120
121 return true;
122 }
123
124 void wxTimer::Stop()
125 {
126 if ( m_id )
127 {
128 ::KillTimer(NULL, m_id);
129
130 TimerMap().erase(m_id);
131 }
132
133 m_id = 0;
134 }
135
136 // ----------------------------------------------------------------------------
137 // private functions
138 // ----------------------------------------------------------------------------
139
140 void wxProcessTimer(wxTimer& timer)
141 {
142 wxASSERT_MSG( timer.m_id != 0, _T("bogus timer id") );
143
144 if ( timer.IsOneShot() )
145 timer.Stop();
146
147 timer.Notify();
148 }
149
150 void WINAPI
151 wxTimerProc(HWND WXUNUSED(hwnd),
152 UINT WXUNUSED(msg),
153 UINT_PTR idTimer,
154 DWORD WXUNUSED(dwTime))
155 {
156 wxTimerMap::iterator node = TimerMap().find((unsigned long)idTimer);
157
158 wxCHECK_RET( node != TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
159
160 wxProcessTimer(*(node->second));
161 }
162
163 #endif // wxUSE_TIMER