]> git.saurik.com Git - wxWidgets.git/blame - src/msw/timer.cpp
Upgrade bundled zlib to 1.2.8.
[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 6// Created: 04/01/98
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
2bda0e17
KB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
c085e333 15 #pragma hdrstop
2bda0e17
KB
16#endif
17
1e6feb95
VZ
18#if wxUSE_TIMER
19
c2ca375c 20#include "wx/msw/private/timer.h"
c0badb70 21
2bda0e17 22#ifndef WX_PRECOMP
c085e333 23 #include "wx/list.h"
2432b92d 24 #include "wx/event.h"
c085e333 25 #include "wx/app.h"
0470b1e6
VZ
26 #include "wx/intl.h"
27 #include "wx/log.h"
df69528b 28 #include "wx/hashmap.h"
0e16e5c3 29 #include "wx/module.h"
2bda0e17
KB
30#endif
31
0470b1e6 32#include "wx/msw/private.h"
b481194f 33#include "wx/msw/private/hiddenwin.h"
2bda0e17 34
c085e333 35// ----------------------------------------------------------------------------
97f278b4 36// private globals
c085e333
VZ
37// ----------------------------------------------------------------------------
38
97f278b4
VZ
39// define a hash containing all the timers: it is indexed by timer id and
40// contains the corresponding timer
491acfc7 41WX_DECLARE_HASH_MAP(WPARAM, wxMSWTimerImpl *, wxIntegerHash, wxIntegerEqual,
3f5c62f9 42 wxTimerMap);
5b6c8794 43
ee1025a7
VZ
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
47static wxTimerMap& TimerMap()
48{
49 static wxTimerMap s_timerMap;
50
51 return s_timerMap;
52}
4676948b 53
1ed7a206
RD
54// This gets a unique, non-zero timer ID and creates an entry in the TimerMap
55UINT_PTR GetNewTimerId(wxMSWTimerImpl *t)
56{
57 static UINT_PTR lastTimerId = 0;
58
59 while (lastTimerId == 0 ||
60 TimerMap().find(lastTimerId) != TimerMap().end())
61 {
62 lastTimerId = lastTimerId + 1;
63 }
64
65 TimerMap()[lastTimerId] = t;
66
67 return lastTimerId;
68}
69
70
71
c085e333 72// ----------------------------------------------------------------------------
97f278b4 73// private functions
eccd1992 74// ----------------------------------------------------------------------------
0e16e5c3 75
9cfc7f4f
RD
76LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
77 WPARAM wParam, LPARAM lParam);
eccd1992 78
9cfc7f4f
RD
79// ----------------------------------------------------------------------------
80// wxTimerHiddenWindowModule: used to manage the hidden window used for
81// catching timer messages (we need a module to ensure that the window is
82// always deleted)
83// ----------------------------------------------------------------------------
84
85class wxTimerHiddenWindowModule : public wxModule
86{
87public:
88 // module init/finalize
89 virtual bool OnInit();
90 virtual void OnExit();
91
92 // get the hidden window (creates on demand)
93 static HWND GetHWND();
94
95private:
96 // the HWND of the hidden window
97 static HWND ms_hwnd;
98
99 // the class used to create it
100 static const wxChar *ms_className;
101
102 DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule)
103};
104
105IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule, wxModule)
eccd1992 106
c085e333
VZ
107// ============================================================================
108// implementation
109// ============================================================================
110
9cfc7f4f 111
c085e333 112// ----------------------------------------------------------------------------
c2ca375c 113// wxMSWTimerImpl class
c085e333 114// ----------------------------------------------------------------------------
73974df1 115
c2ca375c 116bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot)
2bda0e17 117{
c2ca375c
VZ
118 if ( !wxTimerImpl::Start(milliseconds, oneShot) )
119 return false;
caa36aab 120
b6ae7297 121 m_id = GetNewTimerId(this);
fe0c3289
VZ
122 // SetTimer() normally returns just idTimer but this might change in the
123 // future so use its return value to be safe
1ed7a206 124 UINT_PTR ret = ::SetTimer
fe0c3289
VZ
125 (
126 wxTimerHiddenWindowModule::GetHWND(), // window for WM_TIMER
1ed7a206 127 m_id, // timer ID to create
fe0c3289
VZ
128 (UINT)m_milli, // delay
129 NULL // timer proc (unused)
130 );
eccd1992 131
1ed7a206 132 if ( ret == 0 )
c085e333
VZ
133 {
134 wxLogSysError(_("Couldn't create a timer"));
135
04cd30de 136 return false;
c085e333 137 }
eccd1992 138
eccd1992 139 return true;
2bda0e17
KB
140}
141
c2ca375c 142void wxMSWTimerImpl::Stop()
2bda0e17 143{
9cfc7f4f 144 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id);
b6ae7297
RD
145 TimerMap().erase(m_id);
146 m_id = 0;
2bda0e17
KB
147}
148
c085e333
VZ
149// ----------------------------------------------------------------------------
150// private functions
151// ----------------------------------------------------------------------------
73974df1 152
c2ca375c 153void wxProcessTimer(wxMSWTimerImpl& timer)
2bda0e17 154{
9a83f860 155 wxASSERT_MSG( timer.IsRunning(), wxT("bogus timer id") );
c085e333 156
0470b1e6 157 if ( timer.IsOneShot() )
c085e333
VZ
158 timer.Stop();
159
160 timer.Notify();
2bda0e17
KB
161}
162
9cfc7f4f
RD
163
164LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
165 WPARAM wParam, LPARAM lParam)
166{
167 if ( message == WM_TIMER )
168 {
491acfc7 169 wxTimerMap::iterator node = TimerMap().find(wParam);
9cfc7f4f 170
86c6fc77
VZ
171 if ( node != TimerMap().end() )
172 {
173 wxProcessTimer(*(node->second));
174
175 return 0;
176 }
177 //else: Unknown timer, probably one of our timers that had fired just
178 // before being removed from the timers map by Stop().
9cfc7f4f 179 }
86c6fc77
VZ
180
181 return ::DefWindowProc(hWnd, message, wParam, lParam);
9cfc7f4f
RD
182}
183
184// ----------------------------------------------------------------------------
185// wxTimerHiddenWindowModule functions
186// ----------------------------------------------------------------------------
187
188
189HWND wxTimerHiddenWindowModule::ms_hwnd = NULL;
190
191const wxChar *wxTimerHiddenWindowModule::ms_className = NULL;
192
193bool wxTimerHiddenWindowModule::OnInit()
194{
24a21a5f
VZ
195 // do not initialize ms_hwnd to ms_className to NULL here: it may happen
196 // that our GetHWND() is called before the modules are initialized if a
197 // timer is created from wxApp-derived class ctor and in this case we
198 // shouldn't overwrite it
9cfc7f4f
RD
199
200 return true;
201}
202
203void wxTimerHiddenWindowModule::OnExit()
c085e333 204{
9cfc7f4f
RD
205 if ( ms_hwnd )
206 {
207 if ( !::DestroyWindow(ms_hwnd) )
208 {
9a83f860 209 wxLogLastError(wxT("DestroyWindow(wxTimerHiddenWindow)"));
9cfc7f4f
RD
210 }
211
212 ms_hwnd = NULL;
213 }
c085e333 214
9cfc7f4f
RD
215 if ( ms_className )
216 {
217 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
218 {
9a83f860 219 wxLogLastError(wxT("UnregisterClass(\"wxTimerHiddenWindow\")"));
9cfc7f4f
RD
220 }
221
222 ms_className = NULL;
223 }
224}
225
226/* static */
227HWND wxTimerHiddenWindowModule::GetHWND()
228{
9a83f860 229 static const wxChar *HIDDEN_WINDOW_CLASS = wxT("wxTimerHiddenWindow");
9cfc7f4f
RD
230 if ( !ms_hwnd )
231 {
232 ms_hwnd = wxCreateHiddenWindow(&ms_className, HIDDEN_WINDOW_CLASS,
233 wxTimerWndProc);
234 }
c085e333 235
9cfc7f4f 236 return ms_hwnd;
c085e333 237}
1e6feb95
VZ
238
239#endif // wxUSE_TIMER