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 #include "wx/cocoa/objc/objc_uniquifying.h"
30 #import <Foundation/NSTimer.h>
32 // ========================================================================
34 // ========================================================================
35 @interface wxNSTimerData : NSObject
37 wxCocoaTimerImpl* m_timer;
41 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
42 - (wxCocoaTimerImpl*)timer;
43 - (void)onNotify:(NSTimer *)theTimer;
44 @end // interface wxNSTimerData : NSObject
45 WX_DECLARE_GET_OBJC_CLASS(wxNSTimerData,NSObject)
47 @implementation wxNSTimerData : NSObject
50 if(!(self = [super init]))
56 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
58 if(!(self = [super init]))
64 - (wxCocoaTimerImpl*)timer
69 - (void)onNotify:(NSTimer *)theTimer
74 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSTimerData,NSObject)
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 wxCocoaTimerImpl::~wxCocoaTimerImpl()
85 void wxCocoaTimerImpl::Init()
87 m_cocoaNSTimer = NULL;
90 bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot)
94 wxAutoNSAutoreleasePool thePool;
96 wxNSTimerData *timerData = [[WX_GET_OBJC_CLASS(wxNSTimerData) alloc] initWithWxTimer:this];
97 m_cocoaNSTimer = [[NSTimer
98 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
100 selector: @selector(onNotify:)
102 repeats: oneShot == false] retain];
108 void wxCocoaTimerImpl::Stop()
112 // FIXME: Is this safe to do if !isValid ?
113 [m_cocoaNSTimer invalidate];
114 [m_cocoaNSTimer release];
115 m_cocoaNSTimer = NULL;
119 bool wxCocoaTimerImpl::IsRunning() const
121 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
124 #endif // wxUSE_TIMER