From 0a85b4b38369e528c8bba20b93a4d33a2e69a98a Mon Sep 17 00:00:00 2001 From: Alexandre Wilhelm Date: Thu, 27 Mar 2014 13:52:59 -0700 Subject: [PATCH] New: Added protocol CPImageDelegate. Added protocol CPImageDelegate. Also, now, delegate methods are called as we are used to do it in Cappuccino --- AppKit/CPImage.j | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/AppKit/CPImage.j b/AppKit/CPImage.j index e6b67317c..2d701ffde 100644 --- a/AppKit/CPImage.j +++ b/AppKit/CPImage.j @@ -30,6 +30,20 @@ @import "CGGeometry.j" @import "CPCompatibility.j" + +@protocol CPImageDelegate + +@optional +- (void)imageDidLoad:(CPImage)anImage; +- (void)imageDidError:(CPImage)anImage; +- (void)imageDidAbort:(CPImage)anImage; + +@end + +var CPImageDelegate_imageDidLoad_ = 1 << 1, + CPImageDelegate_imageDidError_ = 1 << 2, + CPImageDelegate_imageDidAbort_ = 1 << 3; + CPImageLoadStatusInitialized = 0; CPImageLoadStatusLoading = 1; CPImageLoadStatusCompleted = 2; @@ -119,14 +133,15 @@ function CPAppKitImage(aFilename, aSize) */ @implementation CPImage : CPObject { - CGSize _size; - CPString _filename; - CPString _name; + CGSize _size; + CPString _filename; + CPString _name; - id _delegate; - unsigned _loadStatus; + id _delegate; + unsigned _loadStatus; + unsigned _implementedDelegateMethods; - Image _image; + Image _image; } - (id)init @@ -321,7 +336,20 @@ function CPAppKitImage(aFilename, aSize) */ - (void)setDelegate:(id)aDelegate { + if (_delegate === aDelegate) + return; + _delegate = aDelegate; + _implementedDelegateMethods = 0; + + if ([_delegate respondsToSelector:@selector(imageDidLoad:)]) + _implementedDelegateMethods |= CPImageDelegate_imageDidLoad_; + + if ([_delegate respondsToSelector:@selector(imageDidError:)]) + _implementedDelegateMethods |= CPImageDelegate_imageDidError_; + + if ([_delegate respondsToSelector:@selector(imageDidAbort:)]) + _implementedDelegateMethods |= CPImageDelegate_imageDidAbort_; } /*! @@ -457,7 +485,7 @@ function CPAppKitImage(aFilename, aSize) postNotificationName:CPImageDidLoadNotification object:self]; - if ([_delegate respondsToSelector:@selector(imageDidLoad:)]) + if (_implementedDelegateMethods & CPImageDelegate_imageDidLoad_) [_delegate imageDidLoad:self]; } @@ -466,7 +494,7 @@ function CPAppKitImage(aFilename, aSize) { _loadStatus = CPImageLoadStatusReadError; - if ([_delegate respondsToSelector:@selector(imageDidError:)]) + if (_implementedDelegateMethods & CPImageDelegate_imageDidError_) [_delegate imageDidError:self]; } @@ -475,7 +503,7 @@ function CPAppKitImage(aFilename, aSize) { _loadStatus = CPImageLoadStatusCancelled; - if ([_delegate respondsToSelector:@selector(imageDidAbort:)]) + if (_implementedDelegateMethods & CPImageDelegate_imageDidAbort_) [_delegate imageDidAbort:self]; }