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)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/msw/private/timer.h"
29 #include "wx/hashmap.h"
30 #include "wx/module.h"
33 #include "wx/msw/private.h"
34 #include "wx/msw/private/hiddenwin.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
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(WPARAM
, wxMSWTimerImpl
*, wxIntegerHash
, wxIntegerEqual
,
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
48 static wxTimerMap
& TimerMap()
50 static wxTimerMap s_timerMap
;
55 // This gets a unique, non-zero timer ID and creates an entry in the TimerMap
56 UINT_PTR
GetNewTimerId(wxMSWTimerImpl
*t
)
58 static UINT_PTR lastTimerId
= 0;
60 while (lastTimerId
== 0 ||
61 TimerMap().find(lastTimerId
) != TimerMap().end())
63 lastTimerId
= lastTimerId
+ 1;
66 TimerMap()[lastTimerId
] = t
;
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 LRESULT APIENTRY _EXPORT
wxTimerWndProc(HWND hWnd
, UINT message
,
78 WPARAM wParam
, LPARAM lParam
);
80 // ----------------------------------------------------------------------------
81 // wxTimerHiddenWindowModule: used to manage the hidden window used for
82 // catching timer messages (we need a module to ensure that the window is
84 // ----------------------------------------------------------------------------
86 class wxTimerHiddenWindowModule
: public wxModule
89 // module init/finalize
90 virtual bool OnInit();
91 virtual void OnExit();
93 // get the hidden window (creates on demand)
94 static HWND
GetHWND();
97 // the HWND of the hidden window
100 // the class used to create it
101 static const wxChar
*ms_className
;
103 DECLARE_DYNAMIC_CLASS(wxTimerHiddenWindowModule
)
106 IMPLEMENT_DYNAMIC_CLASS(wxTimerHiddenWindowModule
, wxModule
)
108 // ============================================================================
110 // ============================================================================
113 // ----------------------------------------------------------------------------
114 // wxMSWTimerImpl class
115 // ----------------------------------------------------------------------------
117 bool wxMSWTimerImpl::Start(int milliseconds
, bool oneShot
)
119 if ( !wxTimerImpl::Start(milliseconds
, oneShot
) )
122 m_id
= GetNewTimerId(this);
123 // SetTimer() normally returns just idTimer but this might change in the
124 // future so use its return value to be safe
125 UINT_PTR ret
= ::SetTimer
127 wxTimerHiddenWindowModule::GetHWND(), // window for WM_TIMER
128 m_id
, // timer ID to create
129 (UINT
)m_milli
, // delay
130 NULL
// timer proc (unused)
135 wxLogSysError(_("Couldn't create a timer"));
143 void wxMSWTimerImpl::Stop()
145 ::KillTimer(wxTimerHiddenWindowModule::GetHWND(), m_id
);
146 TimerMap().erase(m_id
);
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 void wxProcessTimer(wxMSWTimerImpl
& timer
)
156 wxASSERT_MSG( timer
.IsRunning(), wxT("bogus timer id") );
158 if ( timer
.IsOneShot() )
165 LRESULT APIENTRY _EXPORT
wxTimerWndProc(HWND hWnd
, UINT message
,
166 WPARAM wParam
, LPARAM lParam
)
168 if ( message
== WM_TIMER
)
170 wxTimerMap::iterator node
= TimerMap().find(wParam
);
172 if ( node
!= TimerMap().end() )
174 wxProcessTimer(*(node
->second
));
178 //else: Unknown timer, probably one of our timers that had fired just
179 // before being removed from the timers map by Stop().
182 return ::DefWindowProc(hWnd
, message
, wParam
, lParam
);
185 // ----------------------------------------------------------------------------
186 // wxTimerHiddenWindowModule functions
187 // ----------------------------------------------------------------------------
190 HWND
wxTimerHiddenWindowModule::ms_hwnd
= NULL
;
192 const wxChar
*wxTimerHiddenWindowModule::ms_className
= NULL
;
194 bool wxTimerHiddenWindowModule::OnInit()
196 // do not initialize ms_hwnd to ms_className to NULL here: it may happen
197 // that our GetHWND() is called before the modules are initialized if a
198 // timer is created from wxApp-derived class ctor and in this case we
199 // shouldn't overwrite it
204 void wxTimerHiddenWindowModule::OnExit()
208 if ( !::DestroyWindow(ms_hwnd
) )
210 wxLogLastError(wxT("DestroyWindow(wxTimerHiddenWindow)"));
218 if ( !::UnregisterClass(ms_className
, wxGetInstance()) )
220 wxLogLastError(wxT("UnregisterClass(\"wxTimerHiddenWindow\")"));
228 HWND
wxTimerHiddenWindowModule::GetHWND()
230 static const wxChar
*HIDDEN_WINDOW_CLASS
= wxT("wxTimerHiddenWindow");
233 ms_hwnd
= wxCreateHiddenWindow(&ms_className
, HIDDEN_WINDOW_CLASS
,
240 #endif // wxUSE_TIMER