]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
eccd1992 | 2 | // Name: 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 | ||
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 | |
c085e333 | 40 | // ---------------------------------------------------------------------------- |
97f278b4 | 41 | // private globals |
c085e333 VZ |
42 | // ---------------------------------------------------------------------------- |
43 | ||
97f278b4 VZ |
44 | // define a hash containing all the timers: it is indexed by timer id and |
45 | // contains the corresponding timer | |
46 | WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual, | |
5b6c8794 VZ |
47 | wxTimerMap); |
48 | ||
ee1025a7 VZ |
49 | // instead of using a global here, wrap it in a static function as otherwise it |
50 | // could have been used before being initialized if a timer object were created | |
51 | // globally | |
52 | static wxTimerMap& TimerMap() | |
53 | { | |
54 | static wxTimerMap s_timerMap; | |
55 | ||
56 | return s_timerMap; | |
57 | } | |
4676948b | 58 | |
c085e333 | 59 | // ---------------------------------------------------------------------------- |
97f278b4 | 60 | // private functions |
eccd1992 VZ |
61 | // ---------------------------------------------------------------------------- |
62 | ||
6d20f7ae VZ |
63 | // timer callback used for all timers |
64 | void WINAPI wxTimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime); | |
eccd1992 VZ |
65 | |
66 | // ---------------------------------------------------------------------------- | |
97f278b4 | 67 | // macros |
eccd1992 VZ |
68 | // ---------------------------------------------------------------------------- |
69 | ||
313feadc | 70 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) |
eccd1992 | 71 | |
c085e333 VZ |
72 | // ============================================================================ |
73 | // implementation | |
74 | // ============================================================================ | |
75 | ||
76 | // ---------------------------------------------------------------------------- | |
77 | // wxTimer class | |
78 | // ---------------------------------------------------------------------------- | |
73974df1 | 79 | |
ed791986 | 80 | void wxTimer::Init() |
2bda0e17 | 81 | { |
0470b1e6 | 82 | m_id = 0; |
2bda0e17 KB |
83 | } |
84 | ||
73974df1 | 85 | wxTimer::~wxTimer() |
2bda0e17 | 86 | { |
0470b1e6 | 87 | wxTimer::Stop(); |
2bda0e17 KB |
88 | } |
89 | ||
0470b1e6 | 90 | bool wxTimer::Start(int milliseconds, bool oneShot) |
2bda0e17 | 91 | { |
eccd1992 | 92 | (void)wxTimerBase::Start(milliseconds, oneShot); |
c085e333 | 93 | |
3c907f1a | 94 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") ); |
caa36aab | 95 | |
eccd1992 VZ |
96 | m_id = ::SetTimer |
97 | ( | |
97f278b4 VZ |
98 | NULL, // don't use window |
99 | 1, // id ignored with NULL hwnd anyhow | |
100 | (UINT)m_milli, // delay | |
6d20f7ae | 101 | wxTimerProc // timer proc to call |
eccd1992 VZ |
102 | ); |
103 | ||
104 | if ( !m_id ) | |
c085e333 VZ |
105 | { |
106 | wxLogSysError(_("Couldn't create a timer")); | |
107 | ||
04cd30de | 108 | return false; |
c085e333 | 109 | } |
eccd1992 | 110 | |
97f278b4 VZ |
111 | // check that SetTimer() didn't reuse an existing id: according to the MSDN |
112 | // this can happen and this would be catastrophic to us as we rely on ids | |
113 | // uniquely identifying the timers because we use them as keys in the hash | |
ee1025a7 | 114 | if ( TimerMap().find(m_id) != TimerMap().end() ) |
97f278b4 VZ |
115 | { |
116 | wxLogError(_("Timer creation failed.")); | |
117 | ||
118 | ::KillTimer(NULL, m_id); | |
119 | m_id = 0; | |
120 | ||
121 | return false; | |
122 | } | |
123 | ||
ee1025a7 | 124 | TimerMap()[m_id] = this; |
eccd1992 VZ |
125 | |
126 | return true; | |
2bda0e17 KB |
127 | } |
128 | ||
73974df1 | 129 | void wxTimer::Stop() |
2bda0e17 | 130 | { |
0470b1e6 | 131 | if ( m_id ) |
73974df1 | 132 | { |
97f278b4 | 133 | ::KillTimer(NULL, m_id); |
62f17a18 | 134 | |
ee1025a7 | 135 | TimerMap().erase(m_id); |
c085e333 | 136 | } |
0470b1e6 VZ |
137 | |
138 | m_id = 0; | |
2bda0e17 KB |
139 | } |
140 | ||
c085e333 VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // private functions | |
143 | // ---------------------------------------------------------------------------- | |
73974df1 | 144 | |
06e38c8e | 145 | void wxProcessTimer(wxTimer& timer) |
2bda0e17 | 146 | { |
5b6c8794 | 147 | wxASSERT_MSG( timer.m_id != 0, _T("bogus timer id") ); |
c085e333 | 148 | |
0470b1e6 | 149 | if ( timer.IsOneShot() ) |
c085e333 VZ |
150 | timer.Stop(); |
151 | ||
152 | timer.Notify(); | |
2bda0e17 KB |
153 | } |
154 | ||
6d20f7ae VZ |
155 | void WINAPI |
156 | wxTimerProc(HWND WXUNUSED(hwnd), | |
157 | UINT WXUNUSED(msg), | |
158 | UINT idTimer, | |
159 | DWORD WXUNUSED(dwTime)) | |
c085e333 | 160 | { |
ee1025a7 | 161 | wxTimerMap::iterator node = TimerMap().find((unsigned long)idTimer); |
c085e333 | 162 | |
ee1025a7 | 163 | wxCHECK_RET( node != TimerMap().end(), wxT("bogus timer id in wxTimerProc") ); |
c085e333 | 164 | |
222ed1d6 | 165 | wxProcessTimer(*(node->second)); |
c085e333 | 166 | } |
1e6feb95 VZ |
167 | |
168 | #endif // wxUSE_TIMER | |
5da0803c | 169 |