]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/timer.cpp
Prevent crash in wxString::Mid under STL builds (fixes #1231725).
[wxWidgets.git] / src / mac / classic / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "timer.h"
14 #endif
15
16 #include "wx/timer.h"
17
18 #if !USE_SHARED_LIBRARY
19 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
20 #endif
21
22 #ifdef __WXMAC__
23 #include "wx/mac/private.h"
24 #endif
25 #ifndef __DARWIN__
26 #include <Timer.h>
27 #endif
28
29 #include "wx/dynarray.h"
30
31 typedef struct MacTimerInfo
32 {
33 TMTask m_task;
34 wxMacNotifierTableRef m_table ;
35 wxTimer* m_timer ;
36 } ;
37
38 static void wxProcessTimer( unsigned long event , void *data ) ;
39
40 static pascal void MacTimerProc( TMTask * t )
41 {
42 MacTimerInfo * tm = (MacTimerInfo*) t ;
43 wxMacAddEvent( tm->m_table , wxProcessTimer, 0 , (void*) tm->m_timer , TRUE ) ;
44 }
45
46 // we need this array to track timers that are being deleted within the Notify procedure
47 // adding the timer before the Notify call and checking after whether it still is in there
48 // as the destructor would have removed it from the array
49
50 wxArrayPtrVoid gTimersInProcess ;
51
52 static void wxProcessTimer( unsigned long event , void *data )
53 {
54 if ( !data )
55 return ;
56
57 wxTimer* timer = (wxTimer*) data ;
58
59 if ( timer->IsOneShot() )
60 timer->Stop() ;
61
62 gTimersInProcess.Add( timer ) ;
63
64 timer->Notify();
65
66 int index = gTimersInProcess.Index( timer ) ;
67
68 if ( index != wxNOT_FOUND )
69 {
70 gTimersInProcess.RemoveAt( index ) ;
71
72 if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr )
73 {
74 PrimeTime( (QElemPtr) &timer->m_info->m_task , timer->GetInterval() ) ;
75 }
76
77 }
78 }
79
80 void wxTimer::Init()
81 {
82 m_info = new MacTimerInfo() ;
83 m_info->m_task.tmAddr = NULL ;
84 m_info->m_task.tmWakeUp = 0 ;
85 m_info->m_task.tmReserved = 0 ;
86 m_info->m_task.qType = 0 ;
87 m_info->m_table = wxMacGetNotifierTable() ;
88 m_info->m_timer = this ;
89 }
90
91 bool wxTimer::IsRunning() const
92 {
93 // as the qType may already indicate it is elapsed, but it
94 // was not handled internally yet
95 return ( m_info->m_task.tmAddr != NULL ) ;
96 }
97
98 wxTimer::~wxTimer()
99 {
100 Stop();
101 if (m_info != NULL) {
102 delete m_info ;
103 m_info = NULL ;
104 }
105 int index = gTimersInProcess.Index( this ) ;
106 if ( index != wxNOT_FOUND )
107 gTimersInProcess.RemoveAt( index ) ;
108 }
109
110 bool wxTimer::Start(int milliseconds,bool mode)
111 {
112 (void)wxTimerBase::Start(milliseconds, mode);
113
114 wxCHECK_MSG( m_milli > 0, FALSE, wxT("invalid value for timer timeout") );
115 wxCHECK_MSG( m_info->m_task.tmAddr == NULL , FALSE, wxT("attempting to restart a timer") );
116
117 #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
118 m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc ) ;
119 #else
120 m_info->m_task.tmAddr = NewTimerProc( MacTimerProc ) ;
121 #endif
122 m_info->m_task.tmWakeUp = 0 ;
123 m_info->m_task.tmReserved = 0 ;
124 m_info->m_task.qType = 0 ;
125 m_info->m_timer = this ;
126 InsXTime((QElemPtr) &m_info->m_task ) ;
127 PrimeTime( (QElemPtr) &m_info->m_task , m_milli ) ;
128 return TRUE;
129 }
130
131 void wxTimer::Stop()
132 {
133 if ( m_info->m_task.tmAddr )
134 {
135 RmvTime( (QElemPtr) &m_info->m_task ) ;
136 DisposeTimerUPP(m_info->m_task.tmAddr) ;
137 m_info->m_task.tmAddr = NULL ;
138 }
139 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ;
140 }
141
142
143