[futurebasic] Re: [FB] Re: Alerts (was code+key)

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : June 2012 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Wed, 20 Jun 2012 06:39:11 -0400
Ted lamented,

I guess I'm pining for the old days where everything I needed to do
was spelled out in words of one syllable or less in the FB manual...
I know... Those days are history.

Ted,

I feel your pain. But consider this: Today in FB we can directly
write in BASIC, C, Objective-C, Unix and-- with some imagination--
perhaps languages some I have forgotten. For instance, the following
six lines are a complete program. Try that in ZBasic or FB^3 or 4.

compile as "Objective-C"
BeginCCode
NSURL *movieURL = [NSURL URLWithString: @"http://media.clickablebliss.com/billable/introduction_small.mov"];
[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObjects:movieURL, nil] withAppBundleIdentifier:@"com.apple.quicktimeplayer" options:(int)nil additionalEventParamDescriptor:nil launchIdentifiers:NULL];
EndC

Here are a few tips when mixing FB and Objective-C, along with some
Alert code you should be able to port into your project which allows
you to interact with the buttons you build in the alert.

First, when mixing FB and Objective-C, make it a habit to include
as your first line of code:

   compile as "Objective-C"

This overrides the FBtoC default should you have forgotten to set it.
I think that was your problem when your NSAlert was not be recognized.

Second, do CFStringRef formatting and retain/releasing outside of 
Objective-C code. That means if you create or copy a CFStringRef,
let it pass through the Objective-C Code, and not try to release it
in Objective-C. (An exception here would be a CFStringRef created
within Objective-C, it should be released inside the code.)

Even though they are toll-free bridged, there are subtle differences
in the way memory management is handled for NSStrings and CFStringRefs.
If you can pass string literals to the Objective-C, that is the way to go.

Creating and releasing an NSPool can be overkill when dealing with
a single object. Simply create/release the object in Objective-C.

And concerning the discussion about prototyping code, in FB you can
prototype code with Def Fn, and in C you declare your functions as
prototypes. However, in Objective-C there is a different way of thinking:
Classes. Classes require two components interfaces and implementations.
You might think of the interface as the place where a prototype is 
defined, and the implementation as where it is coded. (In reality,
interface and implementation do much more.) 

Many of the Objective-C examples we have posted to this list-- several
of mine included-- have simply been Objective-C methods stripped out
of their classes and substituted for FB functions. We can get away
with this, but it's somewhat like using a screwdriver as a prybar.

At any rate, here is a simple Alert demo encompassing some of the
aforementioned. (Note the values returned by the Alert Buttons
in the order of their creation beginning at 1000 for the Okay button.)

Ken

/*
   Simple Alert Demo
   Ken Shmidheiser
   June 20, 2012
*/

compile as "Objective-C"

BeginCFunction
short DoAlert( CFStringRef messageText, CFStringRef informativeText ) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText: (NSString *)messageText];
[alert setInformativeText: (NSString *)informativeText];
[alert setAlertStyle: NSInformationalAlertStyle];
[alert addButtonWithTitle: @"Okay"];
[alert addButtonWithTitle: @"Second Button..."];
[alert addButtonWithTitle: @"Third Button..."];
[alert addButtonWithTitle: @"Cancel"];

int result = [alert runModal];
[alert release];
return result;
}
EndC
toolbox fn DoAlert( CFStringRef messageText, CFStringRef informativeText ) = short

window 1 : text _geneva, 14 : print

dim as CFStringRef flagCFStrRef
dim as short       whichButtonHit

flagCFStrRef = fn CFStringCreateWithFormat( _kCFAllocatorDefault, NULL, @"%@%d", @"Flag is #", 769 )
whichButtonHit = fn DoAlert( cfStr, @"Some informative text for this test" )
CFRelease( flagCFStrRef )

select case ( whichButtonHit )
case 1000 : print "User hit Okay button"
case 1001 : print "User hit Second Button"
case 1002 : print "User hit Third Button"
case 1003 : print "User hit Cancel"
end select

RunApplicationEventLoop()