diff --git a/AppKit/CPSound.j b/AppKit/CPSound.j index 3b5dda2ed..bf25bda98 100644 --- a/AppKit/CPSound.j +++ b/AppKit/CPSound.j @@ -23,6 +23,17 @@ @import @import + +@protocol CPSoundDelegate + +@optional +- (void)sound:(CPSound)aSound didFinishPlaying:(BOOL)finishedLoading; + +@end + + +var CPSoundDelegate_sound_didFinishPlaying_ = 1 << 1; + CPSoundLoadStateEmpty = 0; CPSoundLoadStateLoading = 1; CPSoundLoadStateCanBePlayed = 2; @@ -50,6 +61,7 @@ CPSoundPlayBackStatePause = 2; HTMLAudioElement _audioTag; int _loadStatus; int _playBackStatus; + unsigned _implementedDelegateMethods; } #pragma mark - @@ -133,6 +145,25 @@ CPSoundPlayBackStatePause = 2; } +#pragma mark - +#pragma mark Delegate methods + +/*! + Sets the sound's delegate. + @param aDelegate the new delegate +*/ +- (void)setDelegate:(id)aDelegate +{ + if (_delegate === aDelegate) + return; + + _delegate = aDelegate; + _implementedDelegateMethods = 0; + + if ([_delegate respondsToSelector:@selector(sound:didFinishPlaying:)]) + _implementedDelegateMethods |= CPSoundDelegate_sound_didFinishPlaying_; +} + #pragma mark - #pragma mark Events listener @@ -207,8 +238,7 @@ CPSoundPlayBackStatePause = 2; _audioTag.currentTime = 0.0; _playBackStatus = CPSoundPlayBackStateStop; - if (_delegate && [_delegate respondsToSelector:@selector(sound:didFinishPlaying:)]) - [_delegate sound:self didFinishPlaying:YES]; + [self _sendDelegateSoundDidFinishPlaying:YES]; return YES; } @@ -316,3 +346,20 @@ CPSoundPlayBackStatePause = 2; } @end + + +@implementation CPSound (CPSoundDelegate) + +/*! + @ignore + Call delegate sound:didFinishPlaying: +*/ +- (void)_sendDelegateSoundDidFinishPlaying:(BOOL)finishedPlaying +{ + if (!(_implementedDelegateMethods & CPSoundDelegate_sound_didFinishPlaying_)) + return; + + [_delegate sound:self didFinishPlaying:finishedPlaying]; +} + +@end \ No newline at end of file