]> git.saurik.com Git - wxWidgets.git/blob - src/msw/timer.cpp
Ensure the wxVListBox gets the focus when it is clicked upon
[wxWidgets.git] / src / msw / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (use hash map instead of list, global rewrite)
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 globals
42 // ----------------------------------------------------------------------------
43
44 // define a hash containing all the timers: it is indexed by timer id and
45 // contains the corresponding timer
46 WX_DECLARE_HASH_MAP(unsigned long, wxTimer *, wxIntegerHash, wxIntegerEqual,
47 wxTimerMap);
48
49 // instead of using a global here, wrap it in a static function as otherwise it
50 // could have been used before being initialized if a timer object were created
51 // globally
52 static wxTimerMap& TimerMap()
53 {
54 static wxTimerMap s_timerMap;
55
56 return s_timerMap;
57 }
58
59 // ----------------------------------------------------------------------------
60 // private functions
61 // ----------------------------------------------------------------------------
62
63 // timer callback used for all timers
64 void WINAPI wxTimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime);
65
66 // ----------------------------------------------------------------------------
67 // macros
68 // ----------------------------------------------------------------------------
69
70 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
71
72 // ============================================================================
73 // implementation
74 // ============================================================================
75
76 // ----------------------------------------------------------------------------
77 // wxTimer class
78 // ----------------------------------------------------------------------------
79
80 void wxTimer::Init()
81 {
82 m_id = 0;
83 }
84
85 wxTimer::~wxTimer()
86 {
87 wxTimer::Stop();
88 }
89
90 bool wxTimer::Start(int milliseconds, bool oneShot)
91 {
92 (void)wxTimerBase::Start(milliseconds, oneShot);
93
94 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
95
96 m_id = ::SetTimer
97 (
98 NULL, // don't use window
99 1, // id ignored with NULL hwnd anyhow
100 (UINT)m_milli, // delay
101 wxTimerProc // timer proc to call
102 );
103
104 if ( !m_id )
105 {
106 wxLogSysError(_("Couldn't create a timer"));
107
108 return false;
109 }
110
111 // check that SetTimer() didn't reuse an existing id: according to the MSDN
112 // this can happen and this would be catastrophic to us as we rely on ids
113 // uniquely identifying the timers because we use them as keys in the hash
114 if ( TimerMap().find(m_id) != TimerMap().end() )
115 {
116 wxLogError(_("Timer creation failed."));
117
118 ::KillTimer(NULL, m_id);
119 m_id = 0;
120
121 return false;
122 }
123
124 TimerMap()[m_id] = this;
125
126 return true;
127 }
128
129 void wxTimer::Stop()
130 {
131 if ( m_id )
132 {
133 ::KillTimer(NULL, m_id);
134
135 TimerMap().erase(m_id);
136 }
137
138 m_id = 0;
139 }
140
141 // ----------------------------------------------------------------------------
142 // private functions
143 // ----------------------------------------------------------------------------
144
145 void wxProcessTimer(wxTimer& timer)
146 {
147 wxASSERT_MSG( timer.m_id != 0, _T("bogus timer id") );
148
149 if ( timer.IsOneShot() )
150 timer.Stop();
151
152 timer.Notify();
153 }
154
155 void WINAPI
156 wxTimerProc(HWND WXUNUSED(hwnd),
157 UINT WXUNUSED(msg),
158 UINT idTimer,
159 DWORD WXUNUSED(dwTime))
160 {
161 wxTimerMap::iterator node = TimerMap().find((unsigned long)idTimer);
162
163 wxCHECK_RET( node != TimerMap().end(), wxT("bogus timer id in wxTimerProc") );
164
165 wxProcessTimer(*(node->second));
166 }
167
168 #endif // wxUSE_TIMER
169