X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0b6d471427717055bcc483a244311687d603a0a6..eea4d01c65f9b29baa1193db762b4c6b8144af24:/src/cocoa/timer.mm diff --git a/src/cocoa/timer.mm b/src/cocoa/timer.mm index 9cef73ce20..259011013d 100644 --- a/src/cocoa/timer.mm +++ b/src/cocoa/timer.mm @@ -2,7 +2,7 @@ // Name: src/cocoa/timer.mm // Purpose: wxTimer for wxCocoa // Author: Ryan Norton -// Modified by: +// Modified by: David Elliott // Created: 2005-02-04 // RCS-ID: $Id$ // Copyright: (c) Ryan Norton @@ -22,107 +22,101 @@ #if wxUSE_TIMER -#ifndef WX_PRECOMP - #include "wx/timer.h" -#endif - +#include "wx/cocoa/private/timer.h" #include "wx/cocoa/autorelease.h" -#import - -// ============================================================================ -// implementation -// ============================================================================ - -IMPLEMENT_CLASS(wxTimer, wxTimerBase) - -// ======================================================================== -// wxNSTimerDelegate -// ======================================================================== -@interface wxNSTimerDelegate : NSObject -{ -} +#include "wx/cocoa/objc/objc_uniquifying.h" -- (void)onNotify:(NSTimer *)theTimer; -@end // interface wxNSTimerDelegate : NSObject +#import // ======================================================================== // wxNSTimerData // ======================================================================== @interface wxNSTimerData : NSObject { - wxTimer* m_timer; + wxCocoaTimerImpl* m_timer; } -- (id)setTimer:(wxTimer*)theTimer; -- (wxTimer*)timer; +- (id)init; +- (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer; +- (wxCocoaTimerImpl*)timer; +- (void)onNotify:(NSTimer *)theTimer; @end // interface wxNSTimerData : NSObject +WX_DECLARE_GET_OBJC_CLASS(wxNSTimerData,NSObject) @implementation wxNSTimerData : NSObject -- (id)setTimer:(wxTimer*)theTimer; +- (id)init { + if(!(self = [super init])) + return nil; + m_timer = NULL; + return self; +} + +- (id)initWithWxTimer:(wxCocoaTimerImpl*)theTimer; +{ + if(!(self = [super init])) + return nil; m_timer = theTimer; return self; } -- (wxTimer*)timer + +- (wxCocoaTimerImpl*)timer { return m_timer; } -@end -@implementation wxNSTimerDelegate : NSObject - (void)onNotify:(NSTimer *)theTimer { - wxNSTimerData* theData = [theTimer userInfo]; - [theData timer]->Notify(); //wxTimerBase method + m_timer->Notify(); } -@end +@end +WX_IMPLEMENT_GET_OBJC_CLASS(wxNSTimerData,NSObject) // ---------------------------------------------------------------------------- -// wxTimer +// wxCocoaTimerImpl // ---------------------------------------------------------------------------- -const wxObjcAutoRefFromAlloc wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init]; - -wxTimer::~wxTimer() +wxCocoaTimerImpl::~wxCocoaTimerImpl() { Stop(); } -void wxTimer::Init() +void wxCocoaTimerImpl::Init() { m_cocoaNSTimer = NULL; } -bool wxTimer::Start(int millisecs, bool oneShot) +bool wxCocoaTimerImpl::Start(int millisecs, bool oneShot) { Stop(); - + wxAutoNSAutoreleasePool thePool; - m_cocoaNSTimer = [[NSTimer + wxNSTimerData *timerData = [[WX_GET_OBJC_CLASS(wxNSTimerData) alloc] initWithWxTimer:this]; + m_cocoaNSTimer = [[NSTimer scheduledTimerWithTimeInterval: millisecs / 1000.0 //seconds - target: wxTimer::sm_cocoaDelegate - selector: @selector(onNotify:) - userInfo: [[wxNSTimerData alloc] setTimer:this] - repeats: oneShot == false] retain]; - + target: timerData + selector: @selector(onNotify:) + userInfo: nil + repeats: oneShot == false] retain]; + [timerData release]; + return IsRunning(); } -void wxTimer::Stop() +void wxCocoaTimerImpl::Stop() { if (m_cocoaNSTimer) { - NSObject* theUserInfo = [m_cocoaNSTimer userInfo]; + // FIXME: Is this safe to do if !isValid ? [m_cocoaNSTimer invalidate]; - [theUserInfo release]; [m_cocoaNSTimer release]; m_cocoaNSTimer = NULL; } } -bool wxTimer::IsRunning() const +bool wxCocoaTimerImpl::IsRunning() const { return m_cocoaNSTimer != NULL && [m_cocoaNSTimer isValid]; }