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