From 347af018a31d607cd615a2001bad7e4285f5b461 Mon Sep 17 00:00:00 2001 From: Jake MacMullin Date: Thu, 16 Oct 2008 10:10:14 +1100 Subject: [PATCH] Added an initial implementation of CPAlert. --- AppKit/CPAlert.j | 199 +++++++++++++++++++++++ AppKit/Resources/CPAlert/dialog-error.png | Bin 0 -> 1645 bytes AppKit/Resources/CPAlert/dialog-information.png | Bin 0 -> 1910 bytes AppKit/Resources/CPAlert/dialog-warning.png | Bin 0 -> 1391 bytes 4 files changed, 199 insertions(+), 0 deletions(-) create mode 100644 AppKit/CPAlert.j create mode 100644 AppKit/Resources/CPAlert/dialog-error.png create mode 100644 AppKit/Resources/CPAlert/dialog-information.png create mode 100644 AppKit/Resources/CPAlert/dialog-warning.png diff --git a/AppKit/CPAlert.j b/AppKit/CPAlert.j new file mode 100644 index 0000000..427ef02 --- /dev/null +++ b/AppKit/CPAlert.j @@ -0,0 +1,199 @@ +/* + * CPAlert.j + * AppKit + * + * Created by Jake MacMullin. + * Copyright 2008, Jake MacMullin. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +import +import + +import +import +import +import +import +import +import +import + +/* + @global + @group CPAlertStyle +*/ +CPWarningAlertStyle = 0; +/* + @global + @group CPAlertStyle +*/ +CPInformationalAlertStyle = 1; +/* + @global + @group CPAlertStyle +*/ +CPCriticalAlertStyle = 2; + +/* + CPAlert is an alert panel that can be displayed modally to present the + user with a message and one or more options. + + It can be used to display an information message (
CPInformationalAlertStyle
), + a warning message (
CPWarningAlertStyle
- which is the default), or a critical + alert (
CPCriticalAlertStyle
). In each case the user can be presented with one + or more options by adding buttons using the
addButtonWithTitle:
method. + + The panel is displayed modally by calling
runModal
and once the user has + dismissed the panel, a message will be sent to the panel's delegate (if set), informing + it which button was clicked (see delegate methods). + + @delegate -(void)alertDidEnd:(CPAlert)theAlert returnCode:(int)returnCode; + Called when the user dismisses the alert by clicking one of the buttons. + @param theAlert the alert panel that the user dismissed + @param returnCode the index of the button that the user clicked (starting from 0, + representing the first button added to the alert which appears on the + right, 1 representing the next button to the left and so on) +*/ +@implementation CPAlert : CPObject +{ + CPString _messageText; + CPPanel _alertPanel; + CPTextField _messageLabel; + CPAlertStyle _alertStyle; + int _buttonCount; + id _delegate; +} + +/* + Initializes a
CPAlert
panel with the default alert style (
CPWarningAlertStyle
). +*/ +- (id)init +{ + self = [super init]; + _buttonCount = 0; + _alertStyle = CPWarningAlertStyle; + + _alertPanel = [[CPPanel alloc] initWithContentRect:CGRectMake(0, 0, 300, 150) styleMask:CPHUDBackgroundWindowMask|CPTitledWindowMask]; + [_alertPanel setFloatingPanel:YES]; + [_alertPanel center]; + + _messageLabel = [[CPTextField alloc] initWithFrame: CGRectMake(70,10, 200, 100)]; + [_messageLabel setFont: [CPFont fontWithName: "Helvetica Neue" size: 12.0]]; + [_messageLabel setTextColor: [CPColor whiteColor]]; + [_messageLabel setLineBreakMode:CPLineBreakByWordWrapping]; + + [[_alertPanel contentView] addSubview: _messageLabel]; + + return self; +} + +/* + Sets the receiver’s delegate. + @param delegate - Delegate for the alert. nil removes the delegate. +*/ +- (void)setDelegate:(id)delegate +{ + _delegate = delegate; +} + +/* + Gets the receiver's delegate. +*/ +- (void)delegate +{ + return _delegate; +} + +/* + Sets the alert style of the receiver. + @param style - Alert style for the alert. +*/ +- (void)setAlertStyle:(CPAlertStyle)style +{ + _alertStyle = style; +} + +/* + Gets the alert style. +*/ +- (CPAlertStyle)alertStyle +{ + return _alertStyle; +} + +/* + Set’s the receiver’s message text, or title, to a given text. + @param messageText - Message text for the alert. +*/ +- (void)setMessageText:(CPString)messageText +{ + _messageText = messageText; + [_messageLabel setStringValue:_messageText]; +} + +/* + Adds a button with a given title to the receiver. + Buttons will be added starting from the right hand side of the
CPAlert
panel. + The first button will have the index 0, the second button 1 and so on. + + You really shouldn't need more than 3 buttons. +*/ +- (void)addButtonWithTitle:(CPString)title +{ + var button = [[CPButton alloc] initWithFrame:CGRectMake(190 - (_buttonCount * 90),80,80,18)]; + [button setTitle:title]; + [button setTarget:self]; + [button setTag:_buttonCount]; + [button setAction:@selector(notifyDelegate:)]; + [[_alertPanel contentView] addSubview:button]; + _buttonCount++; +} + +/* + Displays the
CPAlert
panel as a modal dialog. The user will not be + able to interact with any other controls until s/he has dismissed the alert + by clicking on one of the buttons. +*/ +- (void)runModal +{ + var alertImage; + var bundle = [CPBundle bundleForClass:[self class]]; + switch (_alertStyle) + { + case CPWarningAlertStyle: alertImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-warning.png"] size:CGSizeMake(32,32)]; + break; + case CPInformationalAlertStyle: alertImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-information.png"] size:CGSizeMake(32,32)]; + break; + case CPCriticalAlertStyle: alertImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"CPAlert/dialog-error.png"] size:CGSizeMake(32,32)]; + break; + } + var alertImageView = [[CPImageView alloc] initWithFrame:CGRectMake(25,12,32,32)]; + [alertImageView setImage:alertImage]; + [[_alertPanel contentView] addSubview: alertImageView]; + [CPApp runModalForWindow:_alertPanel]; +} + +/* @ignore */ +- (void)notifyDelegate:(id)button +{ + if (_delegate && [_delegate respondsToSelector:@selector(alertDidEnd:returnCode:)]) + { + [_delegate alertDidEnd:self returnCode:[button tag]]; + } + [CPApp abortModal]; + [_alertPanel close]; +} \ No newline at end of file diff --git a/AppKit/Resources/CPAlert/dialog-error.png b/AppKit/Resources/CPAlert/dialog-error.png new file mode 100644 index 0000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_DKkBnQ~I(Nmd-CFb7aPlPl;0!dj z^RpFz05ZT3kogWwPQ#D0@JKS1P}#9UDp|~852vw*l6X#&R;@vMb%XVrYfaB>S?h4_ zP59N@a0RFXPGsH@ARPb@pni2LBVP+y12hw%QN;oMTE)9ZBJ z_i)~%^Pf@OO$pL;zT3h~q=PvXNCJq{`CnlIqaNS_O*$zLC3L*m@BB^x(dV(iVG2kF rj^lye2K)#>8)yZN-9R_sG&=qR=b8{jvze+~00000NkvXXu0mjfkhc>` literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPAlert/dialog-information.png b/AppKit/Resources/CPAlert/dialog-information.png new file mode 100644 index 0000000000000000000000000000000000000000..2ac57475c3b36e46ff4c4ea0506892e2fe884fd7 GIT binary patch literal 1910 zcmV-+2Z{KJP)W~QdMmlr4dytt0lBR3q?&; zsf}7ieW{ceqQ0~w%0ngR(l(+>6RT|+k)U#E5K0IT8=Mf^q}b(RJRV=>K69@7oZW{E z6%PeG5mk@0w56kc*8i=4U(N#l&kxlzdElw1LXir#G9KKfp!(p+CIA4}as(RoJ?ph< z1O7I&f4_Oh0lvTIXM4f;uUc2F*Hc}+k+#-Wgu^;CM({lsi;Ii6bn(1#X=2n?p8Ult zFa3J(jsOe{JflYw)=*=zShA`mbU-k=^sX0S3bG^fyY9h&Y0*p?-h1EjYvd&-0xsN}5e{ec8zp`u9n(Vo=?+@1vuxr;( zdmq{sySgZ`X-yEFtvWOgk1=u?K@veSA{RswfCnfq-Vjh%=E~LCoS`1w+!r}> z`egsc4Sj>7qo=I@ro(Oqz;w2~xpQOK_66FLgqDN`0whSeMC33|HYc(f!R7=@<79G1 zR#?~UctgM_n@*u@OS`XsbC~ID`%QIj>OTwYZ0%eZ%2h0^Y}4Vx_lPN%Kn_7UM6fv{ zi!o+0P8Ja?jWdIDG6RBDf;y{S&XkIM_uZ#~={xJ+gud6+5RKznriy?F03ZoPF5|En zk;92O#FS(p7l7|GQp#7bUC(1g#h|W*3RRm{G^d)Oyw!C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z?C(NRC2&yO8H ze*BM~=Xn79n$UFzRPCK#bEB>$pqi*&8$}I72M}m)Z*TT}pJy_eVoksWVAn#{!U162 wYzgYQ39nnK)ttN0UffXS^-8b1am$VPZ%s-(_>=>Px# literal 0 HcmV?d00001 diff --git a/AppKit/Resources/CPAlert/dialog-warning.png b/AppKit/Resources/CPAlert/dialog-warning.png new file mode 100644 index 0000000000000000000000000000000000000000..7233d45d8e6e41ef8fcb318c76303a9b6f23997e GIT binary patch literal 1391 zcmV-#1(5oQP)}piCG~<@Tj_&{ee%V|SZW{gB8?y>5~HGEkRU`Q5xgR16NK8$#%7av&gnyv zB_>9ji%*39&LbK3qFu zI7(%ZZ%;}2Gqh=wf?-g1;X+qD8r}8{{PEcV%0X!tqH%6yq(DC>&`hf#DjvXW3(1702fiEY= zF5vsm0&oGwD!P6+uzb1JdigTStXXLJ`Pjl?)VXtsUA;>7;>9+ps^67D`lthR!lCrM zJiGbuAzhIO`u_b0*{$vea+<+#n+u)TG0)=^W&Y8i0fonJZU{&O3K7Vlni6`w*i02KV;P{rfRu zG0etBGUm-QC7c=+fU_{mF@F3e|CA|e+x6?%3JOrCPscC}T3cI*Mx&UfiIS0lW6Kua z*4E+=1XR`QT~P`vMhu|BsqFlGD_&EBWm!0O?*~ty__PKSBsxE>~io%jH4{flNukRa8X# z{rjXQCrg{pH?0i59u|NFLJ0B8)HQ2(TUCW{yU};B)sYgKfwF=i#fQ*|T%prYYO&>#^_Jg-l9HIC3}~D2jrjDCoMLxDnFn#I<)X9Z#O% zl~TASP4c~kT|)vm3aV6;lT*TB*>>d$((gwvEKE2_WWLjh)9HL~UI>A6!v?geQ)#cO zBP%CIQ>6UickujYfDG8-Td-iF{q<|n@$@N&XN+4a)1MXii!#xt%)zy89UYGz zG12e0NnI~2gPDT@C~$aEUY=^+y^GP-hO?w((CCB^XqtxXDMlUN#ETcC z1p+4hp6CQXDP-AvzM{13Y_09qE$nO8qUGca8odyL^73*nUc5M{b0Gw_%uGDnx6}FT z856u-Rdc(;WsuwJSM3x1@yun*tghR)G2?Oc_3NRv6|2V>LcBLlv~F)$efs`;+#5F% zJ#z;0`E&f~>DJ4JhLgbjz5vpKd|!698oPE4fasPj{qtqSV!U}nQhK`b970DyKML_) z0Ovbsj@-OC=8MgnrPMUAEG)wS)5J22?y+HDnPzw2Z#-bF|D0)J#^W@