compilation fix
[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
9 // Licence: wxWindows licence
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/window.h"
27 #include "wx/list.h"
28 #include "wx/event.h"
29 #include "wx/app.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #endif
33
34 #include "wx/hashmap.h"
35
36 #include "wx/timer.h"
37
38 #include "wx/msw/private.h"
39
40 // ----------------------------------------------------------------------------
41 // private functions
42 // ----------------------------------------------------------------------------
43
44 WX_DECLARE_HASH_MAP( long,
45 wxTimer *,
46 wxIntegerHash,
47 wxIntegerEqual,
48 wxTimerMap );
49
50 wxTimerMap wxTimerList;
51 UINT WINAPI _EXPORT wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD);
52
53 // ----------------------------------------------------------------------------
54 // macros
55 // ----------------------------------------------------------------------------
56
57 #ifdef __WIN32__
58 #define _EXPORT
59 #else
60 #define _EXPORT _export
61 #endif
62
63 // should probably be in wx/msw/private.h
64 #ifdef __WXMICROWIN__
65 #define MakeProcInstance(proc, hinst) proc
66 #endif
67
68 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject)
69
70 // ============================================================================
71 // implementation
72 // ============================================================================
73
74 // ----------------------------------------------------------------------------
75 // wxTimer class
76 // ----------------------------------------------------------------------------
77
78 void wxTimer::Init()
79 {
80 m_id = 0;
81 }
82
83 wxTimer::~wxTimer()
84 {
85 long id = m_id;
86
87 wxTimer::Stop();
88
89 wxTimerList.erase(id);
90 }
91
92 bool wxTimer::Start(int milliseconds, bool oneShot)
93 {
94 (void)wxTimerBase::Start(milliseconds, oneShot);
95
96 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeour") );
97
98 TIMERPROC wxTimerProcInst = (TIMERPROC)
99 MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance());
100
101 m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1),
102 (UINT)m_milli, wxTimerProcInst);
103
104 if ( m_id > 0 )
105 {
106 wxTimerList[m_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 ( m_id )
121 {
122 ::KillTimer(NULL, (UINT)m_id);
123
124 wxTimerList.erase(m_id);
125 }
126
127 m_id = 0;
128 }
129
130 // ----------------------------------------------------------------------------
131 // private functions
132 // ----------------------------------------------------------------------------
133
134 void wxProcessTimer(wxTimer& timer)
135 {
136 // Avoid to process spurious timer events
137 if ( timer.m_id == 0)
138 return;
139
140 if ( timer.IsOneShot() )
141 timer.Stop();
142
143 timer.Notify();
144 }
145
146 UINT WINAPI _EXPORT wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD)
147 {
148
149 wxTimerMap::iterator node = wxTimerList.find((long)idTimer);
150
151 wxCHECK_MSG( node != wxTimerList.end(), 0,
152 wxT("bogus timer id in wxTimerProc") );
153
154 wxProcessTimer(*(node->second));
155
156 return 0;
157 }
158
159 #endif // wxUSE_TIMER