[futurebasic] Re: [FB] use of obj-c

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 2011 : Group Archive : Group : All Groups

From: Bernie <fblist.bw@...>
Date: Thu, 22 Sep 2011 14:22:34 +0100
Wave wrote:

> I realize I am pretty new to Obj-C.
> 
> BeginCFunction
> - (void) MyShowAlert: ( CFStringRef )alertMsg: ( CFStringRef )informMsg: ( CFStringRef )defaultBtn: ( CFStringRef )alternateBtn: ( WindowRef )parentW
> {
>    NSAlert * alert = [NSAlert alertWithMessageText: (NSString *) alertMsg
>                                      defaultButton: (NSString *) defaultBtn
>                                    alternateButton: (NSString *) alternateBtn
> 	                                       otherButton: nil
>                          informativeTextWithFormat: (NSString *) informMsg ];
>                               [alert setAlertStyle: NSWarningAlertStyle];
> 
>                    [alert beginSheetModalForWindow: (NSWindow *) parentW
>                                        modalDelegate: self didEndSelector: @selector(alertEnded:code:context:)
>                                          contextInfo: NULL ];
> }
> EndC
> 
> BeginCFunction
> - (void)alertDidEnd:(NSAlert *)alert returnCode: (NSInteger)returnCode contextInfo: (void *)contextInfo 
> {
>    if (returnCode == NSAlertFirstButtonReturn) {
>         SysBeep(1);
>    }
> }
> EndC
> toolbox MyShowAlert( CFStringRef alertMsg, CFStringRef informMsg, CFStringRef defaultBtn, CFStringRef alternateBtn, WindowRef parentW  )


> What am I doing wrong?

It looks like you're trying to treat a method like a function. I think you need a c-wrapper. 
I've had limited success attaching Cocoa alert sheets to Carbon windows, mainly because (AFAIK) a WindowRef cannot be cast to NSWindow object. There are ways around it, but the sheet can occasionally become detached from the window.
alertDidEnd: is a selector method, so I don't think it's going to work unless you create a class and stuff it between @implementation…@end.

Bog-standard-working-class-modal-alert:
'---------------
compile as "Objective-C"

include "ConsoleWindow"

BeginCDeclaration
SInt32 MyShowAlert( UInt32 warningStyle, CFStringRef alertMsg, CFStringRef informMsg, CFStringRef defaultBtn, CFStringRef alternateBtn, CFStringRef otherBtn );
EndC

BeginCFunction
SInt32 MyShowAlert( UInt32 warningStyle, CFStringRef alertMsg, CFStringRef informMsg, CFStringRef defaultBtn, CFStringRef alternateBtn, CFStringRef otherBtn )
{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
 [alert setAlertStyle:(NSAlertStyle)warningStyle];
 [alert setMessageText:(NSString *)alertMsg];
 [alert setInformativeText:(NSString *)informMsg];
 [alert addButtonWithTitle:(NSString *)defaultBtn];
 [alert addButtonWithTitle:(NSString *)alternateBtn];
 [alert addButtonWithTitle:(NSString *)otherBtn];
 NSInteger whichBtn = [alert runModal];
 [pool drain];
 return whichBtn;
}
EndC

toolbox fn MyShowAlert( UInt32 warningStyle, CFStringRef alertMsg, CFStringRef informMsg, CFStringRef defaultBtn, CFStringRef alternateBtn, CFStringRef otherBtn ) = SInt32
toolbox fn NSApplicationLoad = Boolean
fn NSApplicationLoad()


// alert style
begin enum
_kNSWarningAlertStyle
_kNSInformationalAlertStyle
_kNSCriticalAlertStyle
end enum

// button return values
begin enum 1000
_kNSAlertFirstButtonReturn  
_kNSAlertSecondButtonReturn 
_kNSAlertThirdButtonReturn
end enum


select ( fn MyShowAlert( _kNSWarningAlertStyle, @"Delete contents of disk?", @"Warning: Could be fatal!", @"OK", @"Cancel", @"Other" ) )
case _kNSAlertFirstButtonReturn  : print "OK"
case _kNSAlertSecondButtonReturn : print "Cancel"
case _kNSAlertThirdButtonReturn  : print "Other"
end select
'---------------

But, there again, all this can be done in Carbon.

Bernie