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