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)
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/msw/private/timer.h"
28 #include "wx/hashmap.h"
29 #include "wx/module.h"
32 #include "wx/msw/private.h"
33 #include "wx/msw/private/hiddenwin.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
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(WPARAM
, wxMSWTimerImpl
*, wxIntegerHash
, wxIntegerEqual
,
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
47 static wxTimerMap
& TimerMap()
49 static wxTimerMap s_timerMap
;
54 // This gets a unique, non-zero timer ID and creates an entry in the TimerMap
55 UINT_PTR
GetNewTimerId(wxMSWTimerImpl
*t
)
57 static UINT_PTR lastTimerId
= 0;
59 while (lastTimerId
== 0 ||
60 TimerMap().find(lastTimerId
) != TimerMap().end())
62 lastTimerId
= lastTimerId
+ 1;
65 TimerMap()[lastTimerId
] = t
;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 LRESULT APIENTRY _EXPORT
wxTimerWndProc(HWND hWnd
, UINT message
,
77 WPARAM wParam
, LPARAM lParam
);
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
83 // ----------------------------------------------------------------------------
85 class wxTimerHiddenWindowModule
: public wxModule
88 // module init/finalize
89 virtual bool OnInit();
90 virtual void OnExit();
92 // get the hidden window (creates on demand)
93 static HWND
GetHWND();
96 // the HWND of the hidden window
99 // the class used to create it
100 static const wxChar
*ms_className
;
102 DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule
)
105 IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule
, wxModule
)
107 // ============================================================================
109 // ============================================================================
112 // ----------------------------------------------------------------------------
113 // wxMSWTimerImpl class
114 // ----------------------------------------------------------------------------
116 bool wxMSWTimerImpl::Start(int milliseconds
, bool oneShot
)
118 if ( !wxTimerImpl::Start(milliseconds
, oneShot
) )
121 m_id
= GetNewTimerId(this);
122 // SetTimer() normally returns just idTimer but this might change in the
123 // future so use its return value to be safe
124 UINT_PTR ret
= ::SetTimer
126 wxTimerHiddenWindowModule::GetHWND(), // window for WM_TIMER
127 m_id
, // timer ID to create
128 (UINT
)m_milli
, // delay
129 NULL
// timer proc (unused)
134 wxLogSysError(_("Couldn't create a timer"));
142 void wxMSWTimerImpl::Stop()
144 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id
);
145 TimerMap().erase(m_id
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxProcessTimer(wxMSWTimerImpl
& timer
)
155 wxASSERT_MSG( timer
.IsRunning(), wxT("bogus timer id") );
157 if ( timer
.IsOneShot() )
164 LRESULT APIENTRY _EXPORT
wxTimerWndProc(HWND hWnd
, UINT message
,
165 WPARAM wParam
, LPARAM lParam
)
167 if ( message
== WM_TIMER
)
169 wxTimerMap::iterator node
= TimerMap().find(wParam
);
171 if ( node
!= TimerMap().end() )
173 wxProcessTimer(*(node
->second
));
177 //else: Unknown timer, probably one of our timers that had fired just
178 // before being removed from the timers map by Stop().
181 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
184 // ----------------------------------------------------------------------------
185 // wxTimerHiddenWindowModule functions
186 // ----------------------------------------------------------------------------
189 HWND
wxTimerHiddenWindowModule::ms_hwnd
= NULL
;
191 const wxChar
*wxTimerHiddenWindowModule::ms_className
= NULL
;
193 bool wxTimerHiddenWindowModule::OnInit()
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
203 void wxTimerHiddenWindowModule::OnExit()
207 if ( !::DestroyWindow(ms_hwnd
) )
209 wxLogLastError(wxT("DestroyWindow(wxTimerHiddenWindow)"));
217 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
219 wxLogLastError(wxT("UnregisterClass(\"wxTimerHiddenWindow\")"));
227 HWND
wxTimerHiddenWindowModule::GetHWND()
229 static const wxChar
*HIDDEN_WINDOW_CLASS
= wxT("wxTimerHiddenWindow");
232 ms_hwnd
= wxCreateHiddenWindow(&ms_className
, HIDDEN_WINDOW_CLASS
,
239 #endif // wxUSE_TIMER