]> git.saurik.com Git - wxWidgets.git/blame - src/msw/timer.cpp
Don't warn about wxMetaFile in configure by default.
[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
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
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
c085e333 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
1e6feb95
VZ
19#if wxUSE_TIMER
20
c2ca375c 21#include "wx/msw/private/timer.h"
c0badb70 22
2bda0e17 23#ifndef WX_PRECOMP
c085e333 24 #include "wx/list.h"
2432b92d 25 #include "wx/event.h"
c085e333 26 #include "wx/app.h"
0470b1e6
VZ
27 #include "wx/intl.h"
28 #include "wx/log.h"
df69528b 29 #include "wx/hashmap.h"
0e16e5c3 30 #include "wx/module.h"
2bda0e17
KB
31#endif
32
0470b1e6 33#include "wx/msw/private.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// implemented in utils.cpp
80extern "C" WXDLLIMPEXP_BASE HWND
81wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc);
82
83
84// ----------------------------------------------------------------------------
85// wxTimerHiddenWindowModule: used to manage the hidden window used for
86// catching timer messages (we need a module to ensure that the window is
87// always deleted)
88// ----------------------------------------------------------------------------
89
90class wxTimerHiddenWindowModule : public wxModule
91{
92public:
93 // module init/finalize
94 virtual bool OnInit();
95 virtual void OnExit();
96
97 // get the hidden window (creates on demand)
98 static HWND GetHWND();
99
100private:
101 // the HWND of the hidden window
102 static HWND ms_hwnd;
103
104 // the class used to create it
105 static const wxChar *ms_className;
106
107 DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule)
108};
109
110IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule, wxModule)
eccd1992 111
c085e333
VZ
112// ============================================================================
113// implementation
114// ============================================================================
115
9cfc7f4f 116
c085e333 117// ----------------------------------------------------------------------------
c2ca375c 118// wxMSWTimerImpl class
c085e333 119// ----------------------------------------------------------------------------
73974df1 120
c2ca375c 121bool wxMSWTimerImpl::Start(int milliseconds, bool oneShot)
2bda0e17 122{
c2ca375c
VZ
123 if ( !wxTimerImpl::Start(milliseconds, oneShot) )
124 return false;
caa36aab 125
b6ae7297 126 m_id = GetNewTimerId(this);
fe0c3289
VZ
127 // SetTimer() normally returns just idTimer but this might change in the
128 // future so use its return value to be safe
1ed7a206 129 UINT_PTR ret = ::SetTimer
fe0c3289
VZ
130 (
131 wxTimerHiddenWindowModule::GetHWND(), // window for WM_TIMER
1ed7a206 132 m_id, // timer ID to create
fe0c3289
VZ
133 (UINT)m_milli, // delay
134 NULL // timer proc (unused)
135 );
eccd1992 136
1ed7a206 137 if ( ret == 0 )
c085e333
VZ
138 {
139 wxLogSysError(_("Couldn't create a timer"));
140
04cd30de 141 return false;
c085e333 142 }
eccd1992 143
eccd1992 144 return true;
2bda0e17
KB
145}
146
c2ca375c 147void wxMSWTimerImpl::Stop()
2bda0e17 148{
9cfc7f4f 149 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id);
b6ae7297
RD
150 TimerMap().erase(m_id);
151 m_id = 0;
2bda0e17
KB
152}
153
c085e333
VZ
154// ----------------------------------------------------------------------------
155// private functions
156// ----------------------------------------------------------------------------
73974df1 157
c2ca375c 158void wxProcessTimer(wxMSWTimerImpl& timer)
2bda0e17 159{
9a83f860 160 wxASSERT_MSG( timer.IsRunning(), wxT("bogus timer id") );
c085e333 161
0470b1e6 162 if ( timer.IsOneShot() )
c085e333
VZ
163 timer.Stop();
164
165 timer.Notify();
2bda0e17
KB
166}
167
9cfc7f4f
RD
168
169LRESULT APIENTRY _EXPORT wxTimerWndProc(HWND hWnd, UINT message,
170 WPARAM wParam, LPARAM lParam)
171{
172 if ( message == WM_TIMER )
173 {
491acfc7 174 wxTimerMap::iterator node = TimerMap().find(wParam);
9cfc7f4f 175
86c6fc77
VZ
176 if ( node != TimerMap().end() )
177 {
178 wxProcessTimer(*(node->second));
179
180 return 0;
181 }
182 //else: Unknown timer, probably one of our timers that had fired just
183 // before being removed from the timers map by Stop().
9cfc7f4f 184 }
86c6fc77
VZ
185
186 return ::DefWindowProc(hWnd, message, wParam, lParam);
9cfc7f4f
RD
187}
188
189// ----------------------------------------------------------------------------
190// wxTimerHiddenWindowModule functions
191// ----------------------------------------------------------------------------
192
193
194HWND wxTimerHiddenWindowModule::ms_hwnd = NULL;
195
196const wxChar *wxTimerHiddenWindowModule::ms_className = NULL;
197
198bool wxTimerHiddenWindowModule::OnInit()
199{
24a21a5f
VZ
200 // do not initialize ms_hwnd to ms_className to NULL here: it may happen
201 // that our GetHWND() is called before the modules are initialized if a
202 // timer is created from wxApp-derived class ctor and in this case we
203 // shouldn't overwrite it
9cfc7f4f
RD
204
205 return true;
206}
207
208void wxTimerHiddenWindowModule::OnExit()
c085e333 209{
9cfc7f4f
RD
210 if ( ms_hwnd )
211 {
212 if ( !::DestroyWindow(ms_hwnd) )
213 {
9a83f860 214 wxLogLastError(wxT("DestroyWindow(wxTimerHiddenWindow)"));
9cfc7f4f
RD
215 }
216
217 ms_hwnd = NULL;
218 }
c085e333 219
9cfc7f4f
RD
220 if ( ms_className )
221 {
222 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
223 {
9a83f860 224 wxLogLastError(wxT("UnregisterClass(\"wxTimerHiddenWindow\")"));
9cfc7f4f
RD
225 }
226
227 ms_className = NULL;
228 }
229}
230
231/* static */
232HWND wxTimerHiddenWindowModule::GetHWND()
233{
9a83f860 234 static const wxChar *HIDDEN_WINDOW_CLASS = wxT("wxTimerHiddenWindow");
9cfc7f4f
RD
235 if ( !ms_hwnd )
236 {
237 ms_hwnd = wxCreateHiddenWindow(&ms_className, HIDDEN_WINDOW_CLASS,
238 wxTimerWndProc);
239 }
c085e333 240
9cfc7f4f 241 return ms_hwnd;
c085e333 242}
1e6feb95
VZ
243
244#endif // wxUSE_TIMER