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 #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
57 - (id)setTimer:(wxTimer*)theTimer;
59 @end // interface wxNSTimerData : NSObject
61 @implementation wxNSTimerData : NSObject
62 - (id)setTimer:(wxTimer*)theTimer;
73 @implementation wxNSTimerDelegate : NSObject
74 - (void)onNotify:(NSTimer *)theTimer
76 wxNSTimerData* theData = [theTimer userInfo];
77 [theData timer]->Notify(); //wxTimerBase method
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 const wxObjcAutoRefFromAlloc<struct objc_object*> wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init];
94 m_cocoaNSTimer = NULL;
97 bool wxTimer::Start(int millisecs, bool oneShot)
101 wxAutoNSAutoreleasePool thePool;
103 m_cocoaNSTimer = [[NSTimer
104 scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds
105 target: wxTimer::sm_cocoaDelegate
106 selector: @selector(onNotify:)
107 userInfo: [[wxNSTimerData alloc] setTimer:this]
108 repeats: oneShot == false] retain];
117 NSObject* theUserInfo = [m_cocoaNSTimer userInfo];
118 [m_cocoaNSTimer invalidate];
119 [theUserInfo release];
120 [m_cocoaNSTimer release];
121 m_cocoaNSTimer = NULL;
125 bool wxTimer::IsRunning() const
127 return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid];
130 #endif // wxUSE_TIMER