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