MSW compilation (and other) fixes
[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
74 wxTimer::wxTimer()
75 {
76 milli = 0;
77 lastMilli = -1;
78 id = 0;
79 }
80
81 wxTimer::~wxTimer()
82 {
83 Stop();
84
85 wxTimerList.DeleteObject(this);
86 }
87
88 bool wxTimer::Start(int milliseconds, bool mode)
89 {
90 oneShot = mode;
91 if (milliseconds < 0)
92 milliseconds = lastMilli;
93
94 wxCHECK_MSG( milliseconds > 0, FALSE, T("invalid value for timer timeour") );
95
96 lastMilli = milli = milliseconds;
97
98 wxTimerList.DeleteObject(this);
99 TIMERPROC wxTimerProcInst = (TIMERPROC)
100 MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance());
101
102 id = SetTimer(NULL, (UINT)(id ? id : 1),
103 (UINT)milliseconds, wxTimerProcInst);
104 if (id > 0)
105 {
106 wxTimerList.Append(id, this);
107
108 return TRUE;
109 }
110 else
111 {
112 wxLogSysError(_("Couldn't create a timer"));
113
114 return FALSE;
115 }
116 }
117
118 void wxTimer::Stop()
119 {
120 if ( id )
121 {
122 KillTimer(NULL, (UINT)id);
123 wxTimerList.DeleteObject(this);
124 }
125 id = 0;
126 milli = 0;
127 }
128
129 // ----------------------------------------------------------------------------
130 // private functions
131 // ----------------------------------------------------------------------------
132
133 void wxProcessTimer(wxTimer& timer)
134 {
135 // Avoid to process spurious timer events
136 if ( timer.id == 0)
137 return;
138
139 if ( timer.oneShot )
140 timer.Stop();
141
142 timer.Notify();
143 }
144
145 UINT WINAPI _EXPORT wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD)
146 {
147 wxNode *node = wxTimerList.Find((long)idTimer);
148
149 wxCHECK_MSG( node, 0, T("bogus timer id in wxTimerProc") );
150
151 wxProcessTimer(*(wxTimer *)node->Data());
152
153 return 0;
154 }