From 09eca10eb603e02abe48b8d1581084a212b3f0a3 Mon Sep 17 00:00:00 2001 From: Alexandre Wilhelm Date: Sun, 8 Dec 2013 18:01:06 -0800 Subject: [PATCH 1/3] New : Added protocol CPAlertDelegate This PR adds the protocol CPAlertDelegate. The delegate methods are called as we are used to in Cappuccino. --- AppKit/CPAlert.j | 122 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 98 insertions(+), 24 deletions(-) diff --git a/AppKit/CPAlert.j b/AppKit/CPAlert.j index 28edbe53d..56ab8fcb4 100644 --- a/AppKit/CPAlert.j +++ b/AppKit/CPAlert.j @@ -41,6 +41,9 @@ @global CPApp +var CPAlertDelegate_alertShowHelp_ = 1 << 0, + CPAlertDelegate_alertDidEnd_returnCode_ = 1 << 1; + /* @global @group CPAlertStyle @@ -59,6 +62,14 @@ CPCriticalAlertStyle = 2; var bottomHeight = 71; +@protocol CPAlertDelegate + +@optional +- (BOOL)alertShowHelp:(CPAlert)alert; +- (void)alertDidEnd:(CPAlert)theAlert returnCode:(int)returnCode; + +@end + /*! @ingroup appkit @@ -83,32 +94,33 @@ var bottomHeight = 71; */ @implementation CPAlert : CPObject { - BOOL _showHelp @accessors(property=showsHelp); - BOOL _showSuppressionButton @accessors(property=showsSuppressionButton); + BOOL _showHelp @accessors(property=showsHelp); + BOOL _showSuppressionButton @accessors(property=showsSuppressionButton); - CPAlertStyle _alertStyle @accessors(property=alertStyle); - CPString _title @accessors(property=title); - CPView _accessoryView @accessors(property=accessoryView); - CPImage _icon @accessors(property=icon); + CPAlertStyle _alertStyle @accessors(property=alertStyle); + CPString _title @accessors(property=title); + CPView _accessoryView @accessors(property=accessoryView); + CPImage _icon @accessors(property=icon); - CPArray _buttons @accessors(property=buttons, readonly); - CPCheckBox _suppressionButton @accessors(property=suppressionButton, readonly); + CPArray _buttons @accessors(property=buttons, readonly); + CPCheckBox _suppressionButton @accessors(property=suppressionButton, readonly); - id _delegate @accessors(property=delegate); - id _modalDelegate; - SEL _didEndSelector @accessors(property=didEndSelector); - Function _didEndBlock; + id _delegate @accessors(property=delegate); + id _modalDelegate; + SEL _didEndSelector @accessors(property=didEndSelector); + Function _didEndBlock; + unsigned _implementedDelegateMethods; - _CPAlertThemeView _themeView @accessors(property=themeView, readonly); - CPWindow _window @accessors(property=window, readonly); - int _defaultWindowStyle; + _CPAlertThemeView _themeView @accessors(property=themeView, readonly); + CPWindow _window @accessors(property=window, readonly); + int _defaultWindowStyle; - CPImageView _alertImageView; - CPTextField _informativeLabel; - CPTextField _messageLabel; - CPButton _alertHelpButton; + CPImageView _alertImageView; + CPTextField _informativeLabel; + CPTextField _messageLabel; + CPButton _alertHelpButton; - BOOL _needsLayout; + BOOL _needsLayout; } #pragma mark Creating Alerts @@ -187,6 +199,30 @@ var bottomHeight = 71; return self; } + +#pragma mark - +#pragma mark Delegate + +/*! + Set the delegate of the receiver + @param aDelegate the delegate object for the alert. +*/ +- (void)setDelegate:(id )aDelegate +{ + if (_delegate === aDelegate) + return; + + _delegate = aDelegate; + _implementedDelegateMethods = 0; + + if ([_delegate respondsToSelector:@selector(alertShowHelp:)]) + _implementedDelegateMethods |= CPAlertDelegate_alertShowHelp_; + + if ([_delegate respondsToSelector:@selector(alertDidEnd:returnCode:)]) + _implementedDelegateMethods |= CPAlertDelegate_alertDidEnd_returnCode_; +} + + #pragma mark Accessors - (CPTheme)theme @@ -696,8 +732,7 @@ var bottomHeight = 71; */ - (@action)_showHelp:(id)aSender { - if ([_delegate respondsToSelector:@selector(alertShowHelp:)]) - [_delegate alertShowHelp:self]; + [self _sendDelegateAlertShowHelp]; } /* @@ -743,13 +778,52 @@ var bottomHeight = 71; { if (_didEndSelector) objj_msgSend(_delegate, _didEndSelector, self, returnCode); - else if ([_delegate respondsToSelector:@selector(alertDidEnd:returnCode:)]) - [_delegate alertDidEnd:self returnCode:returnCode]; + else + [self _sendDelegateAlertDidEndReturnCode:returnCode]; } } @end + +@implementation CPAlert (CPAlertDelegate) + +/* + @ignore + Return YES if the delegate implements alertShowHelp: +*/ +- (BOOL)_delegateRespondsToAlertShowHelp +{ + return _implementedDelegateMethods & CPAlertDelegate_alertShowHelp_; +} + +/*! + @ignore + Call the delegate alertDidEnd:returnCode +*/ +- (void)_sendDelegateAlertDidEndReturnCode:(int)returnCode +{ + if (!(_implementedDelegateMethods & CPAlertDelegate_alertDidEnd_returnCode_)) + return; + + [_delegate alertDidEnd:self returnCode:returnCode]; +} + +/*! + @ignore + Call the delegate alertShowHelp: +*/ +- (BOOL)_sendDelegateAlertShowHelp +{ + if (!(_implementedDelegateMethods & CPAlertDelegate_alertShowHelp_)) + return YES; + + return [_delegate alertShowHelp:self]; +} + +@end + + @implementation _CPAlertThemeView : CPView + (CPString)defaultThemeClass From b81a325b28e4acc4c4cdeec6266771facdd32403 Mon Sep 17 00:00:00 2001 From: Alexandre Wilhelm Date: Sun, 8 Dec 2013 18:06:43 -0800 Subject: [PATCH 2/3] Fixed : Remove unused method --- AppKit/CPAlert.j | 9 --------- 1 file changed, 9 deletions(-) diff --git a/AppKit/CPAlert.j b/AppKit/CPAlert.j index 56ab8fcb4..c85a70173 100644 --- a/AppKit/CPAlert.j +++ b/AppKit/CPAlert.j @@ -788,15 +788,6 @@ var bottomHeight = 71; @implementation CPAlert (CPAlertDelegate) -/* - @ignore - Return YES if the delegate implements alertShowHelp: -*/ -- (BOOL)_delegateRespondsToAlertShowHelp -{ - return _implementedDelegateMethods & CPAlertDelegate_alertShowHelp_; -} - /*! @ignore Call the delegate alertDidEnd:returnCode From fa3ebf2ca7a565b11bad68c24316ddfabab23847 Mon Sep 17 00:00:00 2001 From: Alexandre Wilhelm Date: Mon, 9 Dec 2013 01:24:51 -0800 Subject: [PATCH 3/3] Fixed: CPAlertDelegate protocol does not implement CPObject protocol --- AppKit/CPAlert.j | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AppKit/CPAlert.j b/AppKit/CPAlert.j index c85a70173..38aeef5d8 100644 --- a/AppKit/CPAlert.j +++ b/AppKit/CPAlert.j @@ -62,7 +62,7 @@ CPCriticalAlertStyle = 2; var bottomHeight = 71; -@protocol CPAlertDelegate +@protocol CPAlertDelegate @optional - (BOOL)alertShowHelp:(CPAlert)alert;