1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/timer.mm
3 // Purpose: wxTimer for wxCocoa
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 #import <Foundation/NSTimer.h>
31 // ============================================================================
33 // ============================================================================
35 IMPLEMENT_CLASS(wxTimer, wxTimerBase)
37 // ========================================================================
39 // ========================================================================
40 @interface wxNSTimerDelegate : NSObject
44 - (void)onNotify:(NSTimer *)theTimer;
45 @end // interface wxNSTimerDelegate : NSObject
47 // ========================================================================
49 // ========================================================================
50 @interface wxNSTimerData : NSObject
55 - (id)setTimer:(wxTimer*)theTimer;
57 @end // interface wxNSTimerData : NSObject
59 @implementation wxNSTimerData : NSObject
60 - (id)setTimer:(wxTimer*)theTimer;
71 @implementation wxNSTimerDelegate : NSObject
72 - (void)onNotify:(NSTimer *)theTimer
74 wxNSTimerData* theData = [theTimer userInfo];
75 [theData timer]->Notify(); //wxTimerBase method
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init];
92 m_cocoaNSTimer = NULL;
95 bool wxTimer::Start(int millisecs, bool oneShot)
97 m_cocoaNSTimer = [[NSTimer
98 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
99 target: wxTimer::sm_cocoaDelegate
100 selector: @selector(onNotify:)
101 userInfo: [[wxNSTimerData alloc] setTimer:this]
102 repeats: oneShot == false] retain];
111 [m_cocoaNSTimer invalidate];
112 [[m_cocoaNSTimer userInfo] release];
113 [m_cocoaNSTimer release];
117 bool wxTimer::IsRunning() const
119 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
122 #endif // wxUSE_TIMER