diff --git a/ChangeLog b/ChangeLog index 046581dc6..2b8abcb03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,7 @@ systems guarantee that the rename() system call is atomic. * Source/NSArray.m: New MacOS-X methods ([-initWithArray:copyItems:]) ([-exchangeObjectAtIndex:withObjectAtIndex:]) + * Source/NSTimer.m: Update for compatibility with latest MacOS-X 2002-08-25 Richard Frith-Macdonald diff --git a/Headers/gnustep/base/NSTimer.h b/Headers/gnustep/base/NSTimer.h index 91a5495e4..c122b8e80 100644 --- a/Headers/gnustep/base/NSTimer.h +++ b/Headers/gnustep/base/NSTimer.h @@ -66,23 +66,23 @@ repeats: (BOOL)f; - (void) fire; +- (NSDate*) fireDate; - (void) invalidate; +- (id) userInfo; + #ifndef STRICT_OPENSTEP +- (id) initWithFireDate: (NSDate*)fd + interval: (NSTimeInterval)ti + target: (id)object + selector: (SEL)selector + userInfo: (id)info + repeats: (BOOL)f; - (BOOL) isValid; +- (void) setFireDate: (NSDate*)fireDate; - (NSTimeInterval) timeInterval; #endif -- (NSDate*) fireDate; -- (id) userInfo; - -#ifndef NO_GNUSTEP -- (id) initWithTimeInterval: (NSTimeInterval)ti - targetOrInvocation: (id)object - selector: (SEL)selector - userInfo: (id)info - repeats: (BOOL)f; -#endif @end #endif diff --git a/Source/NSRunLoop.m b/Source/NSRunLoop.m index e09cc82c3..b04a7be37 100644 --- a/Source/NSRunLoop.m +++ b/Source/NSRunLoop.m @@ -294,11 +294,12 @@ static inline BOOL timerInvalidated(NSTimer* timer) target = RETAIN(aTarget); argument = RETAIN(anArgument); timer = [[NSTimer allocWithZone: NSDefaultMallocZone()] - initWithTimeInterval: delay - targetOrInvocation: self - selector: @selector(fire) - userInfo: nil - repeats: NO]; + initWithFireDate: nil + interval: delay + target: self + selector: @selector(fire) + userInfo: nil + repeats: NO]; } return self; } @@ -1662,8 +1663,10 @@ if (0) { } -/* Adding timers. They are removed when they are invalid. */ - +/** + * Adds a timer to the loop in the specified mode.
+ * Timers are removed automatically when they are invalid.
+ */ - (void) addTimer: (NSTimer*)timer forMode: (NSString*)mode { diff --git a/Source/NSTimer.m b/Source/NSTimer.m index e202ab1f9..343e4de16 100644 --- a/Source/NSTimer.m +++ b/Source/NSTimer.m @@ -48,25 +48,42 @@ static Class NSDate_class; /** * - * Initialise a newly allocated NSTimer object.
- * The ti argument specifies the time (in seconds) between the firing.
- * The f argument specifies whether the timer will fire repeatedly.
- * The object and info arguments will be retained until the timer is + * Initialise the receive, a newly allocated NSTimer object.
+ * The fd argument specifies an initial fire date ... if it is not + * supplied (a nil object) then the ti argument is used to create + * a astart date relative to the current time.
+ * The ti argument specifies the time (in seconds) between the firing. + * If it is less than or equal to 0.0 then a small interval is chosen + * automatically.
+ * The f argument specifies whether the timer will fire repeatedly + * or just once.
+ * If the selector argument is zero, then then object is an invocation + * to be used when the timer fires. otherwise, the object is sent the + * message specified by the selector and with the timer as an argument.
+ * The fd, object and info arguments will be retained until the timer is * invalidated.
*/ -- (id) initWithTimeInterval: (NSTimeInterval)ti - targetOrInvocation: (id)object - selector: (SEL)selector - userInfo: (id)info - repeats: (BOOL)f +- (id) initWithFireDate: (NSDate*)fd + interval: (NSTimeInterval)ti + target: (id)object + selector: (SEL)selector + userInfo: (id)info + repeats: (BOOL)f { if (ti <= 0) { ti = 0.0001; } _interval = ti; - _date = [[NSDate_class allocWithZone: [self zone]] - initWithTimeIntervalSinceNow: ti]; + if (fd == nil) + { + _date = [[NSDate_class allocWithZone: NSDefaultMallocZone()] + initWithTimeIntervalSinceReferenceDate: _interval]; + } + else + { + _date = [fd copy]; + } _target = RETAIN(object); _selector = selector; _info = RETAIN(info); @@ -83,11 +100,12 @@ static Class NSDate_class; invocation: (NSInvocation*)invocation repeats: (BOOL)f { - return AUTORELEASE([[self alloc] initWithTimeInterval: ti - targetOrInvocation: invocation - selector: NULL - userInfo: nil - repeats: f]); + return AUTORELEASE([[self alloc] initWithFireDate: nil + interval: ti + target: invocation + selector: NULL + userInfo: nil + repeats: f]); } /** @@ -103,18 +121,19 @@ static Class NSDate_class; userInfo: (id)info repeats: (BOOL)f { - return AUTORELEASE([[self alloc] initWithTimeInterval: ti - targetOrInvocation: object - selector: selector - userInfo: info - repeats: f]); + return AUTORELEASE([[self alloc] initWithFireDate: nil + interval: ti + target: object + selector: selector + userInfo: info + repeats: f]); } /** - * Create a timer wchich will fire after ti seconds and, if f is YES, + * Create a timer which will fire after ti seconds and, if f is YES, * every ti seconds thereafter. On firing, invocation will be performed.
* This timer will automatically be added to the current run loop and - * will fire in the defaut run loop mode. + * will fire in the default run loop mode. */ + (NSTimer*) scheduledTimerWithTimeInterval: (NSTimeInterval)ti invocation: (NSInvocation*)invocation @@ -128,12 +147,12 @@ static Class NSDate_class; } /** - * Create a timer wchich will fire after ti seconds and, if f is YES, + * Create a timer which will fire after ti seconds and, if f is YES, * every ti seconds thereafter. On firing, the target object will be * sent a message specified by selector and with the object info as an * argument.
* This timer will automatically be added to the current run loop and - * will fire in the defaut run loop mode. + * will fire in the default run loop mode. */ + (NSTimer*) scheduledTimerWithTimeInterval: (NSTimeInterval)ti target: (id)object @@ -198,7 +217,7 @@ static Class NSDate_class; } #endif RELEASE(_date); - _date = [[NSDate_class allocWithZone: [self zone]] + _date = [[NSDate_class allocWithZone: NSDefaultMallocZone()] initWithTimeIntervalSinceReferenceDate: nxt]; } } @@ -247,6 +266,18 @@ static Class NSDate_class; return _date; } +/** + * Change the fire date for the receiver.
+ * NB. You should NOT use this method for a timer which has + * been added to a run loop. The only time whan it is safe to modify + * the fire date of a timer in a run loop is for a repeating timer + * when the timer is actually in the process of firing. + */ +- (void) setFireDate: (NSDate*)fireDate +{ + ASSIGN(_date, fireDate); +} + /** * Returns the interval beteen firings. */