]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
eccd1992 | 2 | // Name: msw/timer.cpp |
2bda0e17 KB |
3 | // Purpose: wxTimer implementation |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c085e333 | 13 | #pragma implementation "timer.h" |
2bda0e17 KB |
14 | #endif |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
c085e333 | 20 | #pragma hdrstop |
2bda0e17 KB |
21 | #endif |
22 | ||
1e6feb95 VZ |
23 | #if wxUSE_TIMER |
24 | ||
2bda0e17 | 25 | #ifndef WX_PRECOMP |
0470b1e6 | 26 | #include "wx/window.h" |
c085e333 | 27 | #include "wx/list.h" |
2432b92d | 28 | #include "wx/event.h" |
c085e333 | 29 | #include "wx/app.h" |
0470b1e6 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
2bda0e17 KB |
32 | #endif |
33 | ||
dcc970af VZ |
34 | #include "wx/hashmap.h" |
35 | ||
2bda0e17 | 36 | #include "wx/timer.h" |
2bda0e17 | 37 | |
0470b1e6 | 38 | #include "wx/msw/private.h" |
2bda0e17 | 39 | |
eccd1992 | 40 | // from utils.cpp |
487f2d58 | 41 | extern "C" WXDLLIMPEXP_BASE HWND |
eccd1992 VZ |
42 | wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc); |
43 | ||
c085e333 | 44 | // ---------------------------------------------------------------------------- |
97f278b4 | 45 | // private globals |
c085e333 VZ |
46 | // ---------------------------------------------------------------------------- |
47 | ||
97f278b4 VZ |
48 | // define a hash containing all the timers: it is indexed by timer id and |
49 | // contains the corresponding timer | |
50 | WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual, | |
5b6c8794 VZ |
51 | wxTimerMap); |
52 | ||
53 | static wxTimerMap g_timerMap; | |
4676948b | 54 | |
c085e333 | 55 | // ---------------------------------------------------------------------------- |
97f278b4 | 56 | // private functions |
eccd1992 VZ |
57 | // ---------------------------------------------------------------------------- |
58 | ||
97f278b4 | 59 | void WINAPI wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD); |
eccd1992 VZ |
60 | |
61 | // ---------------------------------------------------------------------------- | |
97f278b4 | 62 | // macros |
eccd1992 VZ |
63 | // ---------------------------------------------------------------------------- |
64 | ||
313feadc | 65 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) |
eccd1992 | 66 | |
c085e333 VZ |
67 | // ============================================================================ |
68 | // implementation | |
69 | // ============================================================================ | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // wxTimer class | |
73 | // ---------------------------------------------------------------------------- | |
73974df1 | 74 | |
ed791986 | 75 | void wxTimer::Init() |
2bda0e17 | 76 | { |
0470b1e6 | 77 | m_id = 0; |
2bda0e17 KB |
78 | } |
79 | ||
73974df1 | 80 | wxTimer::~wxTimer() |
2bda0e17 | 81 | { |
0470b1e6 | 82 | wxTimer::Stop(); |
2bda0e17 KB |
83 | } |
84 | ||
0470b1e6 | 85 | bool wxTimer::Start(int milliseconds, bool oneShot) |
2bda0e17 | 86 | { |
eccd1992 | 87 | (void)wxTimerBase::Start(milliseconds, oneShot); |
c085e333 | 88 | |
caa36aab VZ |
89 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeour") ); |
90 | ||
eccd1992 VZ |
91 | m_id = ::SetTimer |
92 | ( | |
97f278b4 VZ |
93 | NULL, // don't use window |
94 | 1, // id ignored with NULL hwnd anyhow | |
95 | (UINT)m_milli, // delay | |
96 | (TIMERPROC)wxTimerProc // timer proc to call | |
eccd1992 VZ |
97 | ); |
98 | ||
99 | if ( !m_id ) | |
c085e333 VZ |
100 | { |
101 | wxLogSysError(_("Couldn't create a timer")); | |
102 | ||
04cd30de | 103 | return false; |
c085e333 | 104 | } |
eccd1992 | 105 | |
97f278b4 VZ |
106 | // check that SetTimer() didn't reuse an existing id: according to the MSDN |
107 | // this can happen and this would be catastrophic to us as we rely on ids | |
108 | // uniquely identifying the timers because we use them as keys in the hash | |
109 | if ( g_timerMap.find(m_id) != g_timerMap.end() ) | |
110 | { | |
111 | wxLogError(_("Timer creation failed.")); | |
112 | ||
113 | ::KillTimer(NULL, m_id); | |
114 | m_id = 0; | |
115 | ||
116 | return false; | |
117 | } | |
118 | ||
119 | g_timerMap[m_id] = this; | |
eccd1992 VZ |
120 | |
121 | return true; | |
2bda0e17 KB |
122 | } |
123 | ||
73974df1 | 124 | void wxTimer::Stop() |
2bda0e17 | 125 | { |
0470b1e6 | 126 | if ( m_id ) |
73974df1 | 127 | { |
97f278b4 | 128 | ::KillTimer(NULL, m_id); |
62f17a18 | 129 | |
97f278b4 | 130 | g_timerMap.erase(m_id); |
c085e333 | 131 | } |
0470b1e6 VZ |
132 | |
133 | m_id = 0; | |
2bda0e17 KB |
134 | } |
135 | ||
c085e333 VZ |
136 | // ---------------------------------------------------------------------------- |
137 | // private functions | |
138 | // ---------------------------------------------------------------------------- | |
73974df1 | 139 | |
06e38c8e | 140 | void wxProcessTimer(wxTimer& timer) |
2bda0e17 | 141 | { |
5b6c8794 | 142 | wxASSERT_MSG( timer.m_id != 0, _T("bogus timer id") ); |
c085e333 | 143 | |
0470b1e6 | 144 | if ( timer.IsOneShot() ) |
c085e333 VZ |
145 | timer.Stop(); |
146 | ||
147 | timer.Notify(); | |
2bda0e17 KB |
148 | } |
149 | ||
97f278b4 | 150 | void WINAPI wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD) |
c085e333 | 151 | { |
97f278b4 | 152 | wxTimerMap::iterator node = g_timerMap.find(idTimer); |
c085e333 | 153 | |
5b6c8794 | 154 | wxCHECK_RET( node != g_timerMap.end(), wxT("bogus timer id in wxTimerProc") ); |
c085e333 | 155 | |
222ed1d6 | 156 | wxProcessTimer(*(node->second)); |
c085e333 | 157 | } |
1e6feb95 VZ |
158 | |
159 | #endif // wxUSE_TIMER | |
5da0803c | 160 |