]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/timer.cpp
Compilation fix for wxMSW with wxUSE_MSGBOX_HOOK==0.
[wxWidgets.git] / src / osx / core / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/timer.cpp
3 // Purpose: wxTimer implementation using CoreFoundation
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 2008-07-01
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_TIMER
14
15 #ifndef WX_PRECOMP
16 #include "wx/dynarray.h"
17 #endif
18
19 #include "wx/osx/private.h"
20 #include "wx/osx/private/timer.h"
21
22 struct wxOSXTimerInfo
23 {
24 wxOSXTimerImpl* m_timer;
25 CFRunLoopTimerRef m_timerRef;
26 };
27
28 void wxProcessTimer(CFRunLoopTimerRef WXUNUSED(theTimer), void *data)
29 {
30 if ( data == NULL )
31 return;
32
33 wxOSXTimerImpl* timer = (wxOSXTimerImpl*)data;
34
35 if ( timer->IsOneShot() )
36 timer->Stop();
37
38 timer->Notify();
39 }
40
41 wxOSXTimerImpl::wxOSXTimerImpl(wxTimer *timer)
42 : wxTimerImpl(timer)
43 {
44 m_info = new wxOSXTimerInfo();
45 m_info->m_timer = this;
46 m_info->m_timerRef = kInvalidID;
47 }
48
49 bool wxOSXTimerImpl::IsRunning() const
50 {
51 return ( m_info->m_timerRef != kInvalidID && CFRunLoopTimerIsValid(m_info->m_timerRef));
52 }
53
54 wxOSXTimerImpl::~wxOSXTimerImpl()
55 {
56 if (m_info->m_timerRef)
57 {
58 if ( CFRunLoopTimerIsValid(m_info->m_timerRef) )
59 CFRunLoopTimerInvalidate( m_info->m_timerRef );
60 CFRelease( m_info->m_timerRef );
61 }
62 delete m_info;
63 }
64
65 bool wxOSXTimerImpl::Start( int milliseconds, bool mode )
66 {
67 (void)wxTimerImpl::Start(milliseconds, mode);
68
69 wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
70 wxCHECK_MSG( m_info->m_timerRef == NULL, false, wxT("attempting to restart a timer") );
71
72 CFGregorianUnits gumilli ;
73 memset(&gumilli,0,sizeof(gumilli) );
74 gumilli.seconds = m_milli / 1000.0;
75
76 CFRunLoopTimerContext ctx ;
77 memset( &ctx, 0 , sizeof(ctx) );
78 ctx.version = 0;
79 ctx.info = this;
80
81 m_info->m_timer = this;
82 m_info->m_timerRef = CFRunLoopTimerCreate(
83 kCFAllocatorDefault,
84 CFAbsoluteTimeAddGregorianUnits( CFAbsoluteTimeGetCurrent() , NULL, gumilli ),
85 IsOneShot() ? 0 : CFTimeInterval( m_milli / 1000.0 ) ,
86 0, 0, wxProcessTimer, &ctx);
87
88 wxASSERT_MSG( m_info->m_timerRef != NULL, wxT("unable to create timer"));
89
90 CFRunLoopRef runLoop = 0;
91 #if wxOSX_USE_IPHONE
92 runLoop = CFRunLoopGetMain();
93 #else
94 runLoop = CFRunLoopGetCurrent();
95 #endif
96 CFRunLoopAddTimer( runLoop, m_info->m_timerRef, kCFRunLoopCommonModes) ;
97
98
99 return true;
100 }
101
102 void wxOSXTimerImpl::Stop()
103 {
104 if (m_info->m_timerRef)
105 {
106 CFRunLoopTimerInvalidate( m_info->m_timerRef );
107 CFRelease( m_info->m_timerRef );
108 }
109 m_info->m_timerRef = kInvalidID;
110 }
111
112 #endif // wxUSE_TIMER
113