1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "timer.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/window.h"
34 #include "wx/hashmap.h"
35 #include "wx/module.h"
39 #include "wx/msw/private.h"
43 wxCreateHiddenWindow(LPCTSTR
*pclassname
, LPCTSTR classname
, WNDPROC wndproc
);
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 WX_DECLARE_HASH_MAP( long,
55 wxTimerMap wxTimerList
;
57 void WINAPI
wxTimerProc(HWND hwnd
, WORD
, int idTimer
, DWORD
);
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxObject
)
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // these variables are for timer shared hwnd management
70 static const wxChar
*wxMSWTIMER_WNDCLASSNAME
= wxT("_wxTimer_Internal_Class");
71 static LPCTSTR s_classnameTimerWnd
= NULL
;
72 static HWND s_hwndTimer
= NULL
;
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 class wxTimerModule
: public wxModule
81 virtual bool OnInit() { return true; }
86 ::DestroyWindow(s_hwndTimer
);
89 if ( !::UnregisterClass(wxMSWTIMER_WNDCLASSNAME
, wxGetInstance()) )
91 wxLogLastError(_T("UnregisterClass(wxTimerClass)"));
94 s_classnameTimerWnd
= NULL
;
99 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
118 // save id as Stop() changes it
123 wxTimerList
.erase(id
);
126 bool wxTimer::Start(int milliseconds
, bool oneShot
)
128 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeour") );
130 (void)wxTimerBase::Start(milliseconds
, oneShot
);
132 // find a window for SetTimer(): it should be a valid HWND owned by this
133 // thread (even if we had a non NULL m_hwnd before, reset it in case the
134 // owner has changed)
137 // first try the owner window
140 wxWindow
*win
= wxDynamicCast(m_owner
, wxWindow
);
143 m_hwnd
= win
->GetHWND();
147 // if not, use a shared hidden window
152 s_hwndTimer
= wxCreateHiddenWindow
154 &s_classnameTimerWnd
,
155 wxMSWTIMER_WNDCLASSNAME
,
161 wxASSERT_MSG( s_hwndTimer
, wxT("can't create a HWND for wxTimer") );
166 m_hwnd
= (WXHWND
)s_hwndTimer
;
173 (UINT
)(m_id
? m_id
: 1),
175 (TIMERPROC
)wxTimerProc
180 wxLogSysError(_("Couldn't create a timer"));
185 wxTimerList
[m_id
] = this;
194 ::KillTimer((HWND
)m_hwnd
, (UINT
)m_id
);
197 wxTimerList
.erase(m_id
);
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 void wxProcessTimer(wxTimer
& timer
)
209 // Avoid to process spurious timer events
210 if ( timer
.m_id
== 0)
213 if ( timer
.IsOneShot() )
219 void WINAPI
wxTimerProc(HWND
WXUNUSED(hwnd
), WORD
, int idTimer
, DWORD
)
221 wxTimerMap::iterator node
= wxTimerList
.find((long)idTimer
);
223 wxASSERT_MSG( node
!= wxTimerList
.end(), wxT("bogus timer id in wxTimerProc") );
225 wxProcessTimer(*(node
->second
));
228 #endif // wxUSE_TIMER