1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/timer.mm
3 // Purpose: wxTimer for wxCocoa
5 // Modified by: David Elliott
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
25 #include "wx/cocoa/private/timer.h"
26 #include "wx/cocoa/autorelease.h"
28 #import <Foundation/NSTimer.h>
30 // ========================================================================
32 // ========================================================================
33 @interface wxNSTimerData : NSObject
35 wxCocoaTimerImpl* m_timer;
39 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
40 - (wxCocoaTimerImpl*)timer;
41 - (void)onNotify:(NSTimer *)theTimer;
42 @end // interface wxNSTimerData : NSObject
44 @implementation wxNSTimerData : NSObject
47 if(!(self = [super init]))
53 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
55 if(!(self = [super init]))
61 - (wxCocoaTimerImpl*)timer
66 - (void)onNotify:(NSTimer *)theTimer
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 wxCocoaTimerImpl::~wxCocoaTimerImpl()
81 void wxCocoaTimerImpl::Init()
83 m_cocoaNSTimer = NULL;
86 bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot)
90 wxAutoNSAutoreleasePool thePool;
92 wxNSTimerData *timerData = [[wxNSTimerData alloc] initWithWxTimer:this];
93 m_cocoaNSTimer = [[NSTimer
94 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
96 selector: @selector(onNotify:)
98 repeats: oneShot == false] retain];
104 void wxCocoaTimerImpl::Stop()
108 // FIXME: Is this safe to do if !isValid ?
109 [m_cocoaNSTimer invalidate];
110 [m_cocoaNSTimer release];
111 m_cocoaNSTimer = NULL;
115 bool wxCocoaTimerImpl::IsRunning() const
117 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
120 #endif // wxUSE_TIMER