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 if (self = [super init])
00120 {
00121 _buttonCount = 0;
00122 _buttons = [CPArray array];
00123 _alertStyle = CPWarningAlertStyle;
00124
00125 [self setWindowStyle:nil];
00126 }
00127
00128 return self;
00129 }
00130
00135 - (void)setWindowStyle:(int)styleMask
00136 {
00137 _windowStyle = styleMask;
00138
00139 _alertPanel = [[CPPanel alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 110.0) styleMask:styleMask ? styleMask | CPTitledWindowMask : CPTitledWindowMask];
00140 [_alertPanel setFloatingPanel:YES];
00141 [_alertPanel center];
00142
00143 [_messageLabel setTextColor:(styleMask & CPHUDBackgroundWindowMask) ? [CPColor whiteColor] : [CPColor blackColor]];
00144
00145 var count = [_buttons count];
00146 for(var i=0; i < count; i++)
00147 {
00148 var button = _buttons[i];
00149
00150 [button setFrameSize:CGSizeMake([button frame].size.width, (styleMask == CPHUDBackgroundWindowMask) ? 20.0 : 24.0)];
00151
00152 [button setTheme:(_windowStyle === CPHUDBackgroundWindowMask) ? [CPTheme themeNamed:"Aristo-HUD"] : [CPTheme defaultTheme]];
00153
00154 [[_alertPanel contentView] addSubview:button];
00155 }
00156
00157 if (!_messageLabel)
00158 {
00159 var bounds = [[_alertPanel contentView] bounds];
00160
00161 _messageLabel = [[CPTextField alloc] initWithFrame:CGRectMake(57.0, 10.0, CGRectGetWidth(bounds) - 73.0, 62.0)];
00162 [_messageLabel setFont:[CPFont boldSystemFontOfSize:13.0]];
00163 [_messageLabel setLineBreakMode:CPLineBreakByWordWrapping];
00164 [_messageLabel setAlignment:CPJustifiedTextAlignment];
00165 [_messageLabel setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
00166
00167 _alertImageView = [[CPImageView alloc] initWithFrame:CGRectMake(15.0, 12.0, 32.0, 32.0)];
00168 }
00169
00170 [[_alertPanel contentView] addSubview:_messageLabel];
00171 [[_alertPanel contentView] addSubview:_alertImageView];
00172 }
00173
00178 - (void)setTitle:(CPString)aTitle
00179 {
00180 _windowTitle = aTitle;
00181 }
00182
00186 - (CPString)title
00187 {
00188 return _windowTitle;
00189 }
00190
00194 - (int)windowStyle
00195 {
00196 return _windowStyle;
00197 }
00198
00203 - (void)setDelegate:(id)delegate
00204 {
00205 _delegate = delegate;
00206 }
00207
00211 - (void)delegate
00212 {
00213 return _delegate;
00214 }
00215
00220 - (void)setAlertStyle:(CPAlertStyle)style
00221 {
00222 _alertStyle = style;
00223 }
00224
00228 - (CPAlertStyle)alertStyle
00229 {
00230 return _alertStyle;
00231 }
00232
00237 - (void)setMessageText:(CPString)messageText
00238 {
00239 [_messageLabel setStringValue:messageText];
00240 }
00241
00245 - (CPString)messageText
00246 {
00247 return [_messageLabel stringValue];
00248 }
00249
00257 - (void)addButtonWithTitle:(CPString)title
00258 {
00259 var bounds = [[_alertPanel contentView] bounds],
00260 button = [[CPButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(bounds) - ((_buttonCount + 1) * 90.0), CGRectGetHeight(bounds) - 34.0, 80.0, (_windowStyle == CPHUDBackgroundWindowMask) ? 20.0 : 24.0)];
00261
00262 [button setTitle:title];
00263 [button setTarget:self];
00264 [button setTag:_buttonCount];
00265 [button setAction:@selector(_notifyDelegate:)];
00266
00267 [button setTheme:(_windowStyle === CPHUDBackgroundWindowMask) ? [CPTheme themeNamed:"Aristo-HUD"] : [CPTheme defaultTheme]];
00268 [button setAutoresizingMask:CPViewMinXMargin | CPViewMinYMargin];
00269
00270 [[_alertPanel contentView] addSubview:button];
00271
00272 if (_buttonCount == 0)
00273 [_alertPanel setDefaultButton:button];
00274
00275 _buttonCount++;
00276 [_buttons addObject:button];
00277 }
00278
00284 - (void)runModal
00285 {
00286 var theTitle;
00287
00288 switch (_alertStyle)
00289 {
00290 case CPWarningAlertStyle: [_alertImageView setImage:CPAlertWarningImage];
00291 theTitle = @"Warning";
00292 break;
00293 case CPInformationalAlertStyle: [_alertImageView setImage:CPAlertInformationImage];
00294 theTitle = @"Information";
00295 break;
00296 case CPCriticalAlertStyle: [_alertImageView setImage:CPAlertErrorImage];
00297 theTitle = @"Error";
00298 break;
00299 }
00300
00301 [_alertPanel setTitle:_windowTitle ? _windowTitle : theTitle];
00302
00303 [CPApp runModalForWindow:_alertPanel];
00304 }
00305
00306
00307 - (void)_notifyDelegate:(id)button
00308 {
00309 [CPApp abortModal];
00310 [_alertPanel close];
00311
00312 if (_delegate && [_delegate respondsToSelector:@selector(alertDidEnd:returnCode:)])
00313 [_delegate alertDidEnd:self returnCode:[button tag]];
00314 }
00315
00316 @end