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