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
00081 @implementation CPAlert : CPObject
00082 {
00083 CPPanel _alertPanel;
00084
00085 CPTextField _messageLabel;
00086 CPImageView _alertImageView;
00087
00088 CPAlertStyle _alertStyle;
00089 CPString _windowTitle;
00090 int _windowStyle;
00091 int _buttonCount;
00092 CPArray _buttons;
00093
00094 id _delegate;
00095 }
00096
00097 + (void)initialize
00098 {
00099 if (self != CPAlert)
00100 return;
00101
00102 var bundle = [CPBundle bundleForClass:[self class]];
00103
00104 CPAlertWarningImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-warning.png"]
00105 size:CGSizeMake(32.0, 32.0)];
00106
00107 CPAlertInformationImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-information.png"]
00108 size:CGSizeMake(32.0, 32.0)];
00109
00110 CPAlertErrorImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-error.png"]
00111 size:CGSizeMake(32.0, 32.0)];
00112 }
00113
00117 - (id)init
00118 {
00119 self = [super init];
00120
00121 if (self)
00122 {
00123 _buttonCount = 0;
00124 _buttons = [CPArray array];
00125 _alertStyle = CPWarningAlertStyle;
00126
00127 _messageLabel = [[CPTextField alloc] initWithFrame:CGRectMake(57.0, 10.0, 220.0, 80.0)];
00128 [_messageLabel setFont:[CPFont systemFontOfSize:12.0]];
00129 [_messageLabel setLineBreakMode:CPLineBreakByWordWrapping];
00130 [_messageLabel setAlignment:CPJustifiedTextAlignment];
00131
00132 _alertImageView = [[CPImageView alloc] initWithFrame:CGRectMake(15.0, 12.0, 32.0, 32.0)];
00133
00134 [self setWindowStyle:nil];
00135 }
00136
00137 return self;
00138 }
00139
00144 - (void)setWindowStyle:(int)styleMask
00145 {
00146 _windowStyle = styleMask;
00147
00148 _alertPanel = [[CPPanel alloc] initWithContentRect:CGRectMake(0.0, 0.0, 300.0, 130.0) styleMask:styleMask ? styleMask | CPTitledWindowMask : CPTitledWindowMask];
00149 [_alertPanel setFloatingPanel:YES];
00150 [_alertPanel center];
00151
00152 [_messageLabel setTextColor:(styleMask == CPHUDBackgroundWindowMask) ? [CPColor whiteColor] : [CPColor blackColor]];
00153
00154 var count = [_buttons count];
00155 for(var i=0; i < count; i++)
00156 {
00157 var button = _buttons[i];
00158
00159 [button setFrameSize:CGSizeMake([button frame].size.width, (styleMask == CPHUDBackgroundWindowMask) ? 20.0 : 24.0)];
00160 [button setBezelStyle:(styleMask == CPHUDBackgroundWindowMask) ? CPHUDBezelStyle : CPRoundedBezelStyle];
00161
00162 [[_alertPanel contentView] addSubview:button];
00163 }
00164
00165 [[_alertPanel contentView] addSubview:_messageLabel];
00166 [[_alertPanel contentView] addSubview:_alertImageView];
00167 }
00168
00173 - (void)setTitle:(CPString)aTitle
00174 {
00175 _windowTitle = aTitle;
00176 }
00177
00181 - (CPString)title
00182 {
00183 return _windowTitle;
00184 }
00185
00189 - (int)windowStyle
00190 {
00191 return _windowStyle;
00192 }
00193
00198 - (void)setDelegate:(id)delegate
00199 {
00200 _delegate = delegate;
00201 }
00202
00206 - (void)delegate
00207 {
00208 return _delegate;
00209 }
00210
00215 - (void)setAlertStyle:(CPAlertStyle)style
00216 {
00217 _alertStyle = style;
00218 }
00219
00223 - (CPAlertStyle)alertStyle
00224 {
00225 return _alertStyle;
00226 }
00227
00232 - (void)setMessageText:(CPString)messageText
00233 {
00234 [_messageLabel setStringValue:messageText];
00235 }
00236
00240 - (CPString)messageText
00241 {
00242 return [_messageLabel stringValue];
00243 }
00244
00252 - (void)addButtonWithTitle:(CPString)title
00253 {
00254 var button = [[CPButton alloc] initWithFrame:CGRectMake(200.0 - (_buttonCount * 90.0), 98.0, 80.0, (_windowStyle == CPHUDBackgroundWindowMask) ? 20.0 : 24.0)];
00255
00256 [button setTitle:title];
00257 [button setTarget:self];
00258 [button setTag:_buttonCount];
00259 [button setAction:@selector(_notifyDelegate:)];
00260
00261 [button setBezelStyle:(_windowStyle == CPHUDBackgroundWindowMask) ? CPHUDBezelStyle : CPRoundRectBezelStyle];
00262 [[_alertPanel contentView] addSubview:button];
00263
00264 if (_buttonCount == 0)
00265 [_alertPanel setDefaultButton:button];
00266
00267 _buttonCount++;
00268 [_buttons addObject:button];
00269 }
00270
00276 - (void)runModal
00277 {
00278 var theTitle;
00279
00280 switch (_alertStyle)
00281 {
00282 case CPWarningAlertStyle: [_alertImageView setImage:CPAlertWarningImage];
00283 theTitle = @"Warning";
00284 break;
00285 case CPInformationalAlertStyle: [_alertImageView setImage:CPAlertInformationImage];
00286 theTitle = @"Information";
00287 break;
00288 case CPCriticalAlertStyle: [_alertImageView setImage:CPAlertErrorImage];
00289 theTitle = @"Error";
00290 break;
00291 }
00292
00293 [_alertPanel setTitle:_windowTitle ? _windowTitle : theTitle];
00294
00295 [CPApp runModalForWindow:_alertPanel];
00296 }
00297
00298
00299 - (void)_notifyDelegate:(id)button
00300 {
00301 if (_delegate && [_delegate respondsToSelector:@selector(alertDidEnd:returnCode:)])
00302 [_delegate alertDidEnd:self returnCode:[button tag]];
00303
00304 [CPApp abortModal];
00305 [_alertPanel close];
00306 }
00307
00308 @end