X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2fcd7f64a536513f8c8e3ecfac51c0a9ed7f92b7..189da67c55cb8ce76725d77e58b76c1fc666d9a6:/src/cocoa/timer.mm?ds=inline diff --git a/src/cocoa/timer.mm b/src/cocoa/timer.mm index ad99c3227a..4815472d7b 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,10 +22,13 @@ #if wxUSE_TIMER +#include "wx/timer.h" + #ifndef WX_PRECOMP - #include "wx/timer.h" #endif +#include "wx/cocoa/autorelease.h" + #import // ============================================================================ @@ -34,16 +37,6 @@ IMPLEMENT_CLASS(wxTimer, wxTimerBase) -// ======================================================================== -// wxNSTimerDelegate -// ======================================================================== -@interface wxNSTimerDelegate : NSObject -{ -} - -- (void)onNotify:(NSTimer *)theTimer; -@end // interface wxNSTimerDelegate : NSObject - // ======================================================================== // wxNSTimerData // ======================================================================== @@ -52,36 +45,44 @@ IMPLEMENT_CLASS(wxTimer, wxTimerBase) wxTimer* m_timer; } -- (id)setTimer:(wxTimer*)theTimer; +- (id)init; +- (id)initWithWxTimer:(wxTimer*)theTimer; - (wxTimer*)timer; +- (void)onNotify:(NSTimer *)theTimer; @end // interface wxNSTimerData : NSObject @implementation wxNSTimerData : NSObject -- (id)setTimer:(wxTimer*)theTimer; +- (id)init { + if(!(self = [super init])) + return nil; + m_timer = NULL; + return self; +} + +- (id)initWithWxTimer:(wxTimer*)theTimer; +{ + if(!(self = [super init])) + return nil; m_timer = theTimer; return self; } + - (wxTimer*)timer { return m_timer; } -@end -@implementation wxNSTimerDelegate : NSObject - (void)onNotify:(NSTimer *)theTimer { - wxNSTimerData* theData = [theTimer userInfo]; - [theData timer]->Notify(); //wxTimerBase method + m_timer->Notify(); //wxTimerBase method } -@end +@end // ---------------------------------------------------------------------------- // wxTimer // ---------------------------------------------------------------------------- -const wxObjcAutoRefFromAlloc wxTimer::sm_cocoaDelegate = [[wxNSTimerDelegate alloc] init]; - wxTimer::~wxTimer() { Stop(); @@ -94,13 +95,19 @@ void wxTimer::Init() bool wxTimer::Start(int millisecs, bool oneShot) { - m_cocoaNSTimer = [[NSTimer + Stop(); + + wxAutoNSAutoreleasePool thePool; + + wxNSTimerData *timerData = [[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(); } @@ -108,9 +115,10 @@ void wxTimer::Stop() { if (m_cocoaNSTimer) { + // FIXME: Is this safe to do if !isValid ? [m_cocoaNSTimer invalidate]; - [[m_cocoaNSTimer userInfo] release]; [m_cocoaNSTimer release]; + m_cocoaNSTimer = NULL; } }