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"
29 #include "wx/cocoa/autorelease.h"
31 #import <Foundation/NSTimer.h>
33 // ============================================================================
35 // ============================================================================
37 IMPLEMENT_CLASS(wxTimer, wxTimerBase)
39 // ========================================================================
41 // ========================================================================
42 @interface wxNSTimerDelegate : NSObject
46 - (void)onNotify:(NSTimer *)theTimer;
47 @end // interface wxNSTimerDelegate : NSObject
49 // ========================================================================
51 // ========================================================================
52 @interface wxNSTimerData : NSObject
58 - (id)initWithWxTimer:(wxTimer*)theTimer;
60 @end // interface wxNSTimerData : NSObject
62 @implementation wxNSTimerData : NSObject
65 if(!(self = [super init]))
71 - (id)initWithWxTimer:(wxTimer*)theTimer;
73 if(!(self = [super init]))
85 @implementation wxNSTimerDelegate : NSObject
86 - (void)onNotify:(NSTimer *)theTimer
88 wxNSTimerData* theData = [theTimer userInfo];
89 [theData timer]->Notify(); //wxTimerBase method
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init];
106 m_cocoaNSTimer = NULL;
109 bool wxTimer::Start(int millisecs, bool oneShot)
113 wxAutoNSAutoreleasePool thePool;
115 wxNSTimerData *userInfo = [[wxNSTimerData alloc] initWithWxTimer:this];
116 m_cocoaNSTimer = [[NSTimer
117 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
118 target: wxTimer::sm_cocoaDelegate
119 selector: @selector(onNotify:)
121 repeats: oneShot == false] retain];
131 // FIXME: Is this safe to do if !isValid ?
132 [m_cocoaNSTimer invalidate];
133 [m_cocoaNSTimer release];
134 m_cocoaNSTimer = NULL;
138 bool wxTimer::IsRunning() const
140 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
143 #endif // wxUSE_TIMER