]> git.saurik.com Git - wxWidgets.git/blob - src/msw/timer.cpp
More details about the internal module name changes
[wxWidgets.git] / src / msw / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "timer.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_TIMER
24
25 #ifndef WX_PRECOMP
26 #include "wx/window.h"
27 #include "wx/list.h"
28 #include "wx/event.h"
29 #include "wx/app.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #endif
33
34 #include "wx/hashmap.h"
35
36 #include "wx/timer.h"
37
38 #include "wx/msw/private.h"
39
40 // from utils.cpp
41 extern "C" WXDLLIMPEXP_BASE HWND
42 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
43
44 // ----------------------------------------------------------------------------
45 // private globals
46 // ----------------------------------------------------------------------------
47
48 // define a hash containing all the timers: it is indexed by timer id and
49 // contains the corresponding timer
50 WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual,
51 wxTimerMap);
52
53 static wxTimerMap g_timerMap;
54
55 // ----------------------------------------------------------------------------
56 // private functions
57 // ----------------------------------------------------------------------------
58
59 void WINAPI wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD);
60
61 // ----------------------------------------------------------------------------
62 // macros
63 // ----------------------------------------------------------------------------
64
65 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject)
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 timeour") );
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 (TIMERPROC)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 ( g_timerMap.find(m_id) != g_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 g_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 g_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 wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD)
151 {
152 wxTimerMap::iterator node = g_timerMap.find(idTimer);
153
154 wxCHECK_RET( node != g_timerMap.end(), wxT("bogus timer id in wxTimerProc") );
155
156 wxProcessTimer(*(node->second));
157 }
158
159 #endif // wxUSE_TIMER
160