00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 @import <Foundation/CPObject.j>
00027 @import <Foundation/CPString.j>
00028
00029 @import <AppKit/CPApplication.j>
00030 @import <AppKit/CPButton.j>
00031 @import <AppKit/CPColor.j>
00032 @import <AppKit/CPFont.j>
00033 @import <AppKit/CPImage.j>
00034 @import <AppKit/CPImageView.j>
00035 @import <AppKit/CPPanel.j>
00036 @import <AppKit/CPTextField.j>
00037
00038
00039
00040
00041
00042 CPWarningAlertStyle = 0;
00043
00044
00045
00046
00047 CPInformationalAlertStyle = 1;
00048
00049
00050
00051
00052 CPCriticalAlertStyle = 2;
00053
00054
00055 var CPAlertWarningImage,
00056 CPAlertInformationImage,
00057 CPAlertErrorImage;
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 @implementation CPAlert : CPObject
00080 {
00081 CPPanel _alertPanel;
00082
00083 CPTextField _messageLabel;
00084 CPImageView _alertImageView;
00085
00086 CPAlertStyle _alertStyle;
00087 int _buttonCount;
00088
00089 id _delegate;
00090 }
00091
00092 + (void)initialize
00093 {
00094 if (self != CPAlert)
00095 return;
00096
00097 var bundle = [CPBundle bundleForClass:[self class]];
00098
00099 CPAlertWarningImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-warning.png"]
00100 size:CGSizeMake(32,32)];
00101
00102 CPAlertInformationImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-information.png"]
00103 size:CGSizeMake(32,32)];
00104
00105 CPAlertErrorImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-error.png"]
00106 size:CGSizeMake(32,32)];
00107 }
00108
00112 - (id)init
00113 {
00114 self = [super init];
00115
00116 if (self)
00117 {
00118 _buttonCount = 0;
00119 _alertStyle = CPWarningAlertStyle;
00120
00121 _alertPanel = [[CPPanel alloc] initWithContentRect:CGRectMake(0, 0, 300, 150) styleMask:CPHUDBackgroundWindowMask|CPTitledWindowMask];
00122 [_alertPanel setFloatingPanel:YES];
00123 [_alertPanel center];
00124
00125 _messageLabel = [[CPTextField alloc] initWithFrame: CGRectMake(70,10, 200, 100)];
00126 [_messageLabel setFont: [CPFont fontWithName: "Helvetica Neue" size: 12.0]];
00127 [_messageLabel setTextColor: [CPColor whiteColor]];
00128 [_messageLabel setLineBreakMode:CPLineBreakByWordWrapping];
00129
00130 [[_alertPanel contentView] addSubview: _messageLabel];
00131
00132 _alertImageView = [[CPImageView alloc] initWithFrame:CGRectMake(25,12,32,32)];
00133
00134 [[_alertPanel contentView] addSubview: _alertImageView];
00135 }
00136
00137 return self;
00138 }
00139
00144 - (void)setDelegate:(id)delegate
00145 {
00146 _delegate = delegate;
00147 }
00148
00152 - (void)delegate
00153 {
00154 return _delegate;
00155 }
00156
00161 - (void)setAlertStyle:(CPAlertStyle)style
00162 {
00163 _alertStyle = style;
00164 }
00165
00169 - (CPAlertStyle)alertStyle
00170 {
00171 return _alertStyle;
00172 }
00173
00178 - (void)setMessageText:(CPString)messageText
00179 {
00180 [_messageLabel setStringValue:messageText];
00181 }
00182
00186 - (CPString)messageText
00187 {
00188 return [_messageLabel stringValue];
00189 }
00190
00198 - (void)addButtonWithTitle:(CPString)title
00199 {
00200 var button = [[CPButton alloc] initWithFrame:CGRectMake(190 - (_buttonCount * 90),80,80,18)];
00201
00202 [button setTitle:title];
00203 [button setTarget:self];
00204 [button setTag:_buttonCount];
00205 [button setBezelStyle:CPHUDBezelStyle];
00206 [button setAction:@selector(_notifyDelegate:)];
00207
00208 [[_alertPanel contentView] addSubview:button];
00209
00210 _buttonCount++;
00211 }
00212
00218 - (void)runModal
00219 {
00220 switch (_alertStyle)
00221 {
00222 case CPWarningAlertStyle: [_alertImageView setImage:CPAlertWarningImage];
00223 break;
00224 case CPInformationalAlertStyle: [_alertImageView setImage:CPAlertInformationImage];
00225 break;
00226 case CPCriticalAlertStyle: [_alertImageView setImage:CPAlertErrorImage];
00227 break;
00228 }
00229
00230 [CPApp runModalForWindow:_alertPanel];
00231 }
00232
00233
00234 - (void)_notifyDelegate:(id)button
00235 {
00236 if (_delegate && [_delegate respondsToSelector:@selector(alertDidEnd:returnCode:)])
00237 [_delegate alertDidEnd:self returnCode:[button tag]];
00238
00239 [CPApp abortModal];
00240 [_alertPanel close];
00241 }
00242
00243 @end