]> git.saurik.com Git - wxWidgets.git/blame - src/os2/timer.cpp
adapting ownership semantics to cocoa convention
[wxWidgets.git] / src / os2 / timer.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
521bf4ff 2// Name: src/os2/timer.cpp
0e320a79 3// Purpose: wxTimer implementation
d90895ac 4// Author: David Webster
0e320a79 5// Modified by:
d90895ac 6// Created: 10/17/99
0e320a79 7// RCS-ID: $Id$
d90895ac 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
d90895ac
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
c2ca375c 15#include "wx/os2/private/timer.h"
c0badb70 16
d90895ac 17#ifndef WX_PRECOMP
d90895ac 18 #include "wx/list.h"
8ecff181 19 #include "wx/window.h"
d90895ac
DW
20 #include "wx/event.h"
21 #include "wx/app.h"
88a7a4e1 22 #include "wx/intl.h"
e4db172a 23 #include "wx/log.h"
0e320a79
DW
24#endif
25
521bf4ff 26#include "wx/os2/private.h"
d90895ac 27
d90895ac
DW
28#include <time.h>
29#include <sys/types.h>
30
31#include <sys/timeb.h>
2461cfa0
SN
32
33// ----------------------------------------------------------------------------
34// private globals
35// ----------------------------------------------------------------------------
36
37// define a hash containing all the timers: it is indexed by timer id and
38// contains the corresponding timer
c2ca375c 39WX_DECLARE_HASH_MAP(unsigned long, wxOS2TimerImpl *, wxIntegerHash, wxIntegerEqual,
2461cfa0
SN
40 wxTimerMap);
41
42// instead of using a global here, wrap it in a static function as otherwise it
43// could have been used before being initialized if a timer object were created
44// globally
45static wxTimerMap& TimerMap()
46{
47 static wxTimerMap s_timerMap;
48
49 return s_timerMap;
50}
51
d90895ac
DW
52// ----------------------------------------------------------------------------
53// private functions
54// ----------------------------------------------------------------------------
55
2461cfa0 56// timer callback used for all timers
9ed0fac8 57ULONG wxTimerProc(HWND hwnd, ULONG, int nIdTimer, ULONG);
d90895ac 58
2461cfa0
SN
59// ============================================================================
60// implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// wxTimer class
65// ----------------------------------------------------------------------------
66
c2ca375c 67bool wxOS2TimerImpl::Start( int nMilliseconds, bool bOneShot )
0e320a79 68{
55bfbcb9
VZ
69 if ( !wxTimerImpl::Start( nMilliseconds, bOneShot ) )
70 return false;
d90895ac 71
8ecff181 72 wxWindow* pWin = NULL;
5d644707
DW
73
74 if (m_owner)
75 {
76 pWin = (wxWindow*)m_owner;
77 m_ulId = ::WinStartTimer( m_Hab
78 ,pWin->GetHWND()
79 ,m_idTimer
80 ,(ULONG)nMilliseconds
81 );
82 }
83 else
55bfbcb9 84 {
5d644707
DW
85 m_ulId = ::WinStartTimer( m_Hab
86 ,NULLHANDLE
87 ,0
88 ,(ULONG)nMilliseconds
89 );
55bfbcb9
VZ
90 }
91
9ed0fac8
DW
92 if (m_ulId > 0L)
93 {
2461cfa0
SN
94 // check that SetTimer() didn't reuse an existing id: according to
95 // the MSDN this can happen and this would be catastrophic to us as
96 // we rely on ids uniquely identifying the timers because we use
97 // them as keys in the hash
98 if ( TimerMap().find(m_ulId) != TimerMap().end() )
99 {
100 wxLogError(_("Timer creation failed."));
101
102 ::WinStopTimer(m_Hab, pWin?(pWin->GetHWND()):NULL, m_ulId);
103 m_ulId = 0;
104
105 return false;
106 }
107
108 TimerMap()[m_ulId] = this;
109
110 return true;
d90895ac
DW
111 }
112 else
113 {
114 wxLogSysError(_("Couldn't create a timer"));
115
8ecff181 116 return false;
d90895ac 117 }
0e320a79
DW
118}
119
c2ca375c 120void wxOS2TimerImpl::Stop()
0e320a79 121{
9ed0fac8 122 if ( m_ulId )
d90895ac 123 {
5d644707
DW
124 if (m_owner)
125 {
126 wxWindow* pWin = (wxWindow*)m_owner;
127
128 ::WinStopTimer(m_Hab, pWin->GetHWND(), m_ulId);
129 }
130 else
131 ::WinStopTimer(m_Hab, NULLHANDLE, m_ulId);
2461cfa0
SN
132
133 TimerMap().erase(m_ulId);
d90895ac 134 }
5d644707 135 m_ulId = 0L;
0e320a79
DW
136}
137
d90895ac
DW
138// ----------------------------------------------------------------------------
139// private functions
140// ----------------------------------------------------------------------------
141
9ed0fac8 142void wxProcessTimer(
c2ca375c 143 wxOS2TimerImpl& rTimer
9ed0fac8 144)
d90895ac 145{
9ed0fac8 146 //
d90895ac 147 // Avoid to process spurious timer events
9ed0fac8
DW
148 //
149 if (rTimer.m_ulId == 0L)
d90895ac
DW
150 return;
151
9ed0fac8
DW
152 if (rTimer.IsOneShot())
153 rTimer.Stop();
d90895ac 154
9ed0fac8 155 rTimer.Notify();
d90895ac
DW
156}
157
9ed0fac8
DW
158ULONG wxTimerProc(
159 HWND WXUNUSED(hwnd)
160, ULONG
161, int nIdTimer
162, ULONG
163)
d90895ac 164{
2461cfa0 165 wxTimerMap::iterator node = TimerMap().find((ULONG)nIdTimer);
d90895ac 166
2461cfa0
SN
167 wxCHECK_MSG(node != TimerMap().end(), 0,
168 wxT("bogus timer id in wxTimerProc") );
169 wxProcessTimer(*(node->second));
d90895ac
DW
170 return 0;
171}