Index: vtkCocoaRenderWindowInteractor.h =================================================================== RCS file: /cvsroot/VTK/VTK/Rendering/vtkCocoaRenderWindowInteractor.h,v retrieving revision 1.7 diff -u -3 -r1.7 vtkCocoaRenderWindowInteractor.h --- vtkCocoaRenderWindowInteractor.h 30 Mar 2006 15:57:07 -0000 1.7 +++ vtkCocoaRenderWindowInteractor.h 27 Oct 2006 20:25:10 -0000 @@ -96,6 +96,7 @@ ~vtkCocoaRenderWindowInteractor(); int InstallMessageProc; + void *TimerDictionary; // Really an NSMutableDictionary* //BTX // Description: Index: vtkCocoaRenderWindowInteractor.mm =================================================================== RCS file: /cvsroot/VTK/VTK/Rendering/vtkCocoaRenderWindowInteractor.mm,v retrieving revision 1.11 diff -u -3 -r1.11 vtkCocoaRenderWindowInteractor.mm --- vtkCocoaRenderWindowInteractor.mm 31 Mar 2006 13:53:15 -0000 1.11 +++ vtkCocoaRenderWindowInteractor.mm 27 Oct 2006 20:25:10 -0000 @@ -39,10 +39,11 @@ { NSTimer *timer; vtkCocoaRenderWindowInteractor *interactor; + int timerId; } -- (id)initWithInteractor:(vtkCocoaRenderWindowInteractor *)myInteractor; -- (void)startTimer:(NSTimeInterval)interval repeating:(BOOL)repeating; +- (id)initWithInteractor:(vtkCocoaRenderWindowInteractor *)myInteractor timerId:(int)myTimerId; +- (void)startTimerWithInterval:(NSTimeInterval)interval repeating:(BOOL)repeating; - (void)stopTimer; - (void)timerFired:(NSTimer *)myTimer; @@ -51,12 +52,13 @@ //---------------------------------------------------------------------------- @implementation vtkCocoaTimer -- (id)initWithInteractor:(vtkCocoaRenderWindowInteractor *)myInteractor +- (id)initWithInteractor:(vtkCocoaRenderWindowInteractor *)myInteractor timerId:(int)myTimerId { self = [super init]; if (self) { interactor = myInteractor; + timerId = myTimerId; } return self; } @@ -64,17 +66,16 @@ - (void)timerFired:(NSTimer *)myTimer { (void)myTimer; - int vtkTimerId = interactor->GetVTKTimerId((int) self); - interactor->InvokeEvent(vtkCommand::TimerEvent, (void *) &vtkTimerId); + interactor->InvokeEvent(vtkCommand::TimerEvent, &timerId); } -- (void)startTimer:(NSTimeInterval)interval repeating:(BOOL)repeating +- (void)startTimerWithInterval:(NSTimeInterval)interval repeating:(BOOL)repeating { - timer = [NSTimer timerWithTimeInterval:interval + timer = [[NSTimer timerWithTimeInterval:interval target:self selector:@selector(timerFired:) userInfo:nil - repeats:repeating]; + repeats:repeating] retain]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode]; } @@ -82,6 +83,8 @@ - (void)stopTimer { [timer invalidate]; + [timer release]; + timer = nil; } @end @@ -90,12 +93,15 @@ vtkCocoaRenderWindowInteractor::vtkCocoaRenderWindowInteractor() { this->InstallMessageProc = 1; + this->TimerDictionary = (void*)[[NSMutableDictionary dictionary] retain]; } //---------------------------------------------------------------------------- vtkCocoaRenderWindowInteractor::~vtkCocoaRenderWindowInteractor() { this->Enabled = 0; + NSMutableDictionary* timerDict = (NSMutableDictionary*)(this->TimerDictionary); + [timerDict release]; } //---------------------------------------------------------------------------- @@ -187,7 +193,7 @@ } //---------------------------------------------------------------------------- -int vtkCocoaRenderWindowInteractor::InternalCreateTimer(int vtkNotUsed(timerId), +int vtkCocoaRenderWindowInteractor::InternalCreateTimer(int timerId, int timerType, unsigned long duration) { BOOL repeating = NO; @@ -197,20 +203,44 @@ repeating = YES; } - int platformTimerId = (int)[[vtkCocoaTimer alloc] initWithInteractor:this]; - [(vtkCocoaTimer*)platformTimerId startTimer:((NSTimeInterval)duration/1000.0) + // Create a vtkCocoaTimer and add it to a dictionary using the timerId + // as key, this will let us find the vtkCocoaTimer later by timerId + vtkCocoaTimer* cocoaTimer = [[vtkCocoaTimer alloc] initWithInteractor:this + timerId:timerId]; + NSString* timerIdAsStr = [NSString stringWithFormat:@"%i", timerId]; + NSMutableDictionary* timerDict = (NSMutableDictionary*)(this->TimerDictionary); + [timerDict setObject:cocoaTimer forKey:timerIdAsStr]; + [cocoaTimer startTimerWithInterval:((NSTimeInterval)duration/1000.0) repeating:repeating]; + // In this implementation, timerId and platformTimerId are the same + int platformTimerId = timerId; + return platformTimerId; } //---------------------------------------------------------------------------- int vtkCocoaRenderWindowInteractor::InternalDestroyTimer(int platformTimerId) { - [(vtkCocoaTimer*)platformTimerId stopTimer]; - [(vtkCocoaTimer*)platformTimerId release]; + // In this implementation, timerId and platformTimerId are the same; + // but calling this anyway is more correct + int timerId = this->GetVTKTimerId(platformTimerId); - return 1; + NSString* timerIdAsStr = [NSString stringWithFormat:@"%i", timerId]; + NSMutableDictionary* timerDict = (NSMutableDictionary*)(this->TimerDictionary); + vtkCocoaTimer* cocoaTimer = [timerDict objectForKey:timerIdAsStr]; + [timerDict removeObjectForKey:timerIdAsStr]; + + if (nil != cocoaTimer) + { + [cocoaTimer stopTimer]; + [cocoaTimer release]; + return 1; // success + } + else + { + return 0; // fail + } } //----------------------------------------------------------------------------