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