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