]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: timer.cpp | |
3 | // Purpose: wxTimer implementation | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa JS |
8 | // Copyright: (c) Julian Smart |
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 VZ |
40 | // ---------------------------------------------------------------------------- |
41 | // private functions | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
222ed1d6 | 44 | WX_DECLARE_HASH_MAP( long, |
dcc970af | 45 | wxTimer *, |
222ed1d6 MB |
46 | wxIntegerHash, |
47 | wxIntegerEqual, | |
48 | wxTimerMap ); | |
49 | ||
50 | wxTimerMap wxTimerList; | |
4676948b | 51 | |
5da0803c | 52 | void WINAPI wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD); |
2bda0e17 | 53 | |
c085e333 VZ |
54 | // ---------------------------------------------------------------------------- |
55 | // macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
5da0803c | 58 | // should probably be in wx/msw/missing.h |
62f17a18 VZ |
59 | #ifdef __WXMICROWIN__ |
60 | #define MakeProcInstance(proc, hinst) proc | |
61 | #endif | |
62 | ||
ed791986 | 63 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject) |
2bda0e17 | 64 | |
c085e333 VZ |
65 | // ============================================================================ |
66 | // implementation | |
67 | // ============================================================================ | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // wxTimer class | |
71 | // ---------------------------------------------------------------------------- | |
73974df1 | 72 | |
ed791986 | 73 | void wxTimer::Init() |
2bda0e17 | 74 | { |
0470b1e6 | 75 | m_id = 0; |
2bda0e17 KB |
76 | } |
77 | ||
73974df1 | 78 | wxTimer::~wxTimer() |
2bda0e17 | 79 | { |
222ed1d6 MB |
80 | long id = m_id; |
81 | ||
0470b1e6 | 82 | wxTimer::Stop(); |
2bda0e17 | 83 | |
222ed1d6 | 84 | wxTimerList.erase(id); |
2bda0e17 KB |
85 | } |
86 | ||
0470b1e6 | 87 | bool wxTimer::Start(int milliseconds, bool oneShot) |
2bda0e17 | 88 | { |
0470b1e6 | 89 | (void)wxTimerBase::Start(milliseconds, oneShot); |
c085e333 | 90 | |
04cd30de | 91 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeour") ); |
c085e333 | 92 | |
4676948b JS |
93 | #ifdef __WXWINCE__ |
94 | m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1), | |
09785dd3 | 95 | (UINT)m_milli, (TIMERPROC) wxTimerProc); |
4676948b | 96 | #else |
c085e333 VZ |
97 | TIMERPROC wxTimerProcInst = (TIMERPROC) |
98 | MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance()); | |
99 | ||
62f17a18 VZ |
100 | m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1), |
101 | (UINT)m_milli, wxTimerProcInst); | |
4676948b | 102 | #endif |
04ef50df | 103 | |
0470b1e6 | 104 | if ( m_id > 0 ) |
c085e333 | 105 | { |
222ed1d6 | 106 | wxTimerList[m_id] = this; |
c085e333 | 107 | |
04cd30de | 108 | return true; |
c085e333 VZ |
109 | } |
110 | else | |
111 | { | |
112 | wxLogSysError(_("Couldn't create a timer")); | |
113 | ||
04cd30de | 114 | return false; |
c085e333 | 115 | } |
2bda0e17 KB |
116 | } |
117 | ||
73974df1 | 118 | void wxTimer::Stop() |
2bda0e17 | 119 | { |
0470b1e6 | 120 | if ( m_id ) |
73974df1 | 121 | { |
62f17a18 VZ |
122 | ::KillTimer(NULL, (UINT)m_id); |
123 | ||
222ed1d6 | 124 | wxTimerList.erase(m_id); |
c085e333 | 125 | } |
0470b1e6 VZ |
126 | |
127 | m_id = 0; | |
2bda0e17 KB |
128 | } |
129 | ||
c085e333 VZ |
130 | // ---------------------------------------------------------------------------- |
131 | // private functions | |
132 | // ---------------------------------------------------------------------------- | |
73974df1 | 133 | |
06e38c8e | 134 | void wxProcessTimer(wxTimer& timer) |
2bda0e17 | 135 | { |
c085e333 | 136 | // Avoid to process spurious timer events |
0470b1e6 | 137 | if ( timer.m_id == 0) |
c085e333 VZ |
138 | return; |
139 | ||
0470b1e6 | 140 | if ( timer.IsOneShot() ) |
c085e333 VZ |
141 | timer.Stop(); |
142 | ||
143 | timer.Notify(); | |
2bda0e17 KB |
144 | } |
145 | ||
5da0803c | 146 | void WINAPI wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD) |
c085e333 | 147 | { |
222ed1d6 MB |
148 | |
149 | wxTimerMap::iterator node = wxTimerList.find((long)idTimer); | |
c085e333 | 150 | |
4676948b | 151 | wxASSERT_MSG( node != wxTimerList.end(), wxT("bogus timer id in wxTimerProc") ); |
c085e333 | 152 | |
222ed1d6 | 153 | wxProcessTimer(*(node->second)); |
c085e333 | 154 | } |
1e6feb95 VZ |
155 | |
156 | #endif // wxUSE_TIMER | |
5da0803c | 157 |