More WinCE mods
[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
52 void WINAPI _EXPORT wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD);
53
54 // ----------------------------------------------------------------------------
55 // macros
56 // ----------------------------------------------------------------------------
57
58 #ifdef __WIN32__
59 #define _EXPORT
60 #else
61 #define _EXPORT _export
62 #endif
63
64 // should probably be in wx/msw/private.h
65 #ifdef __WXMICROWIN__
66 #define MakeProcInstance(proc, hinst) proc
67 #endif
68
69 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject)
70
71 // ============================================================================
72 // implementation
73 // ============================================================================
74
75 // ----------------------------------------------------------------------------
76 // wxTimer class
77 // ----------------------------------------------------------------------------
78
79 void wxTimer::Init()
80 {
81 m_id = 0;
82 }
83
84 wxTimer::~wxTimer()
85 {
86 long id = m_id;
87
88 wxTimer::Stop();
89
90 wxTimerList.erase(id);
91 }
92
93 bool wxTimer::Start(int milliseconds, bool oneShot)
94 {
95 (void)wxTimerBase::Start(milliseconds, oneShot);
96
97 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeour") );
98
99 #ifdef __WXWINCE__
100 m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1),
101 (UINT)m_milli, (void (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,unsigned long)) wxTimerProc);
102 #else
103 TIMERPROC wxTimerProcInst = (TIMERPROC)
104 MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance());
105
106 m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1),
107 (UINT)m_milli, wxTimerProcInst);
108 #endif
109
110 if ( m_id > 0 )
111 {
112 wxTimerList[m_id] = this;
113
114 return true;
115 }
116 else
117 {
118 wxLogSysError(_("Couldn't create a timer"));
119
120 return false;
121 }
122 }
123
124 void wxTimer::Stop()
125 {
126 if ( m_id )
127 {
128 ::KillTimer(NULL, (UINT)m_id);
129
130 wxTimerList.erase(m_id);
131 }
132
133 m_id = 0;
134 }
135
136 // ----------------------------------------------------------------------------
137 // private functions
138 // ----------------------------------------------------------------------------
139
140 void wxProcessTimer(wxTimer& timer)
141 {
142 // Avoid to process spurious timer events
143 if ( timer.m_id == 0)
144 return;
145
146 if ( timer.IsOneShot() )
147 timer.Stop();
148
149 timer.Notify();
150 }
151
152 void WINAPI _EXPORT wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD)
153 {
154
155 wxTimerMap::iterator node = wxTimerList.find((long)idTimer);
156
157 wxASSERT_MSG( node != wxTimerList.end(), wxT("bogus timer id in wxTimerProc") );
158
159 wxProcessTimer(*(node->second));
160
161 // return 0;
162 }
163
164 #endif // wxUSE_TIMER