1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/timer.mm
3 // Purpose: wxTimer for wxCocoa
5 // Modified by: David Elliott
7 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
24 #include "wx/cocoa/private/timer.h"
25 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/objc/objc_uniquifying.h"
29 #import <Foundation/NSTimer.h>
31 // ========================================================================
33 // ========================================================================
34 @interface wxNSTimerData : NSObject
36 wxCocoaTimerImpl* m_timer;
40 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
41 - (wxCocoaTimerImpl*)timer;
42 - (void)onNotify:(NSTimer *)theTimer;
43 @end // interface wxNSTimerData : NSObject
44 WX_DECLARE_GET_OBJC_CLASS(wxNSTimerData,NSObject)
46 @implementation wxNSTimerData : NSObject
49 if(!(self = [super init]))
55 - (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer;
57 if(!(self = [super init]))
63 - (wxCocoaTimerImpl*)timer
68 - (void)onNotify:(NSTimer *)theTimer
73 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSTimerData,NSObject)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxCocoaTimerImpl::~wxCocoaTimerImpl()
84 void wxCocoaTimerImpl::Init()
86 m_cocoaNSTimer = NULL;
89 bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot)
93 wxAutoNSAutoreleasePool thePool;
95 wxNSTimerData *timerData = [[WX_GET_OBJC_CLASS(wxNSTimerData) alloc] initWithWxTimer:this];
96 m_cocoaNSTimer = [[NSTimer
97 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
99 selector: @selector(onNotify:)
101 repeats: oneShot == false] retain];
107 void wxCocoaTimerImpl::Stop()
111 // FIXME: Is this safe to do if !isValid ?
112 [m_cocoaNSTimer invalidate];
113 [m_cocoaNSTimer release];
114 m_cocoaNSTimer = NULL;
118 bool wxCocoaTimerImpl::IsRunning() const
120 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
123 #endif // wxUSE_TIMER