]> git.saurik.com Git - wxWidgets.git/blob - src/msw/timer.cpp
Added GUI part of AppTraits for OS/2.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 wxTimerProc(HWND hwnd, WORD, int idTimer, DWORD);
53
54 // ----------------------------------------------------------------------------
55 // macros
56 // ----------------------------------------------------------------------------
57
58 // should probably be in wx/msw/missing.h
59 #ifdef __WXMICROWIN__
60 #define MakeProcInstance(proc, hinst) proc
61 #endif
62
63 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject)
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // wxTimer class
71 // ----------------------------------------------------------------------------
72
73 void wxTimer::Init()
74 {
75 m_id = 0;
76 }
77
78 wxTimer::~wxTimer()
79 {
80 long id = m_id;
81
82 wxTimer::Stop();
83
84 wxTimerList.erase(id);
85 }
86
87 bool wxTimer::Start(int milliseconds, bool oneShot)
88 {
89 (void)wxTimerBase::Start(milliseconds, oneShot);
90
91 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeour") );
92
93 #ifdef __WXWINCE__
94 m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1),
95 (UINT)m_milli, (TIMERPROC) wxTimerProc);
96 #else
97 TIMERPROC wxTimerProcInst = (TIMERPROC)
98 MakeProcInstance((FARPROC)wxTimerProc, wxGetInstance());
99
100 m_id = ::SetTimer(NULL, (UINT)(m_id ? m_id : 1),
101 (UINT)m_milli, wxTimerProcInst);
102 #endif
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 void WINAPI wxTimerProc(HWND WXUNUSED(hwnd), WORD, int idTimer, DWORD)
147 {
148
149 wxTimerMap::iterator node = wxTimerList.find((long)idTimer);
150
151 wxASSERT_MSG( node != wxTimerList.end(), wxT("bogus timer id in wxTimerProc") );
152
153 wxProcessTimer(*(node->second));
154 }
155
156 #endif // wxUSE_TIMER
157