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