]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
73974df1 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
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 |
c085e333 | 26 | #include "wx/setup.h" |
0470b1e6 | 27 | #include "wx/window.h" |
c085e333 | 28 | #include "wx/list.h" |
2432b92d | 29 | #include "wx/event.h" |
c085e333 | 30 | #include "wx/app.h" |
0470b1e6 VZ |
31 | #include "wx/intl.h" |
32 | #include "wx/log.h" | |
2bda0e17 KB |
33 | #endif |
34 | ||
35 | #include "wx/timer.h" | |
2bda0e17 | 36 | |
0470b1e6 | 37 | #include "wx/msw/private.h" |
2bda0e17 | 38 | |
c085e333 VZ |
39 | // ---------------------------------------------------------------------------- |
40 | // private functions | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
2bda0e17 KB |
43 | wxList wxTimerList(wxKEY_INTEGER); |
44 | UINT WINAPI _EXPORT wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD); | |
45 | ||
c085e333 VZ |
46 | // ---------------------------------------------------------------------------- |
47 | // macros | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | #ifdef __WIN32__ | |
73974df1 | 51 | #define _EXPORT |
c085e333 VZ |
52 | #else |
53 | #define _EXPORT _export | |
54 | #endif | |
55 | ||
ed791986 | 56 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject) |
2bda0e17 | 57 | |
c085e333 VZ |
58 | // ============================================================================ |
59 | // implementation | |
60 | // ============================================================================ | |
61 | ||
62 | // ---------------------------------------------------------------------------- | |
63 | // wxTimer class | |
64 | // ---------------------------------------------------------------------------- | |
73974df1 | 65 | |
ed791986 | 66 | void wxTimer::Init() |
2bda0e17 | 67 | { |
0470b1e6 | 68 | m_id = 0; |
2bda0e17 KB |
69 | } |
70 | ||
73974df1 | 71 | wxTimer::~wxTimer() |
2bda0e17 | 72 | { |
0470b1e6 | 73 | wxTimer::Stop(); |
2bda0e17 | 74 | |
c085e333 | 75 | wxTimerList.DeleteObject(this); |
2bda0e17 KB |
76 | } |
77 | ||
0470b1e6 | 78 | bool wxTimer::Start(int milliseconds, bool oneShot) |
2bda0e17 | 79 | { |
0470b1e6 | 80 | (void)wxTimerBase::Start(milliseconds, oneShot); |
c085e333 | 81 | |
0470b1e6 | 82 | wxCHECK_MSG( m_milli > 0, FALSE, wxT("invalid value for timer timeour") ); |
c085e333 VZ |
83 | |
84 | wxTimerList.DeleteObject(this); | |
04ef50df JS |
85 | |
86 | #ifdef __WXMICROWIN__ | |
87 | m_id = SetTimer(NULL, (UINT)(m_id ? m_id : 1), | |
88 | (UINT)milliseconds, (TIMERPROC) wxTimerProc); | |
89 | #else | |
c085e333 VZ |
90 | TIMERPROC wxTimerProcInst = (TIMERPROC) |
91 | MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance()); | |
92 | ||
0470b1e6 VZ |
93 | m_id = SetTimer(NULL, (UINT)(m_id ? m_id : 1), |
94 | (UINT)milliseconds, wxTimerProcInst); | |
04ef50df JS |
95 | #endif |
96 | ||
0470b1e6 | 97 | if ( m_id > 0 ) |
c085e333 | 98 | { |
0470b1e6 | 99 | wxTimerList.Append(m_id, this); |
c085e333 VZ |
100 | |
101 | return TRUE; | |
102 | } | |
103 | else | |
104 | { | |
105 | wxLogSysError(_("Couldn't create a timer")); | |
106 | ||
107 | return FALSE; | |
108 | } | |
2bda0e17 KB |
109 | } |
110 | ||
73974df1 | 111 | void wxTimer::Stop() |
2bda0e17 | 112 | { |
0470b1e6 | 113 | if ( m_id ) |
73974df1 | 114 | { |
0470b1e6 | 115 | KillTimer(NULL, (UINT)m_id); |
73974df1 | 116 | wxTimerList.DeleteObject(this); |
c085e333 | 117 | } |
0470b1e6 VZ |
118 | |
119 | m_id = 0; | |
2bda0e17 KB |
120 | } |
121 | ||
c085e333 VZ |
122 | // ---------------------------------------------------------------------------- |
123 | // private functions | |
124 | // ---------------------------------------------------------------------------- | |
73974df1 | 125 | |
06e38c8e | 126 | void wxProcessTimer(wxTimer& timer) |
2bda0e17 | 127 | { |
c085e333 | 128 | // Avoid to process spurious timer events |
0470b1e6 | 129 | if ( timer.m_id == 0) |
c085e333 VZ |
130 | return; |
131 | ||
0470b1e6 | 132 | if ( timer.IsOneShot() ) |
c085e333 VZ |
133 | timer.Stop(); |
134 | ||
135 | timer.Notify(); | |
2bda0e17 KB |
136 | } |
137 | ||
c085e333 VZ |
138 | UINT WINAPI _EXPORT wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD) |
139 | { | |
140 | wxNode *node = wxTimerList.Find((long)idTimer); | |
141 | ||
223d09f6 | 142 | wxCHECK_MSG( node, 0, wxT("bogus timer id in wxTimerProc") ); |
c085e333 VZ |
143 | |
144 | wxProcessTimer(*(wxTimer *)node->Data()); | |
145 | ||
146 | return 0; | |
147 | } | |
1e6feb95 VZ |
148 | |
149 | #endif // wxUSE_TIMER |