Forum Navigation
You need to log in to create posts and topics.

URL Launcher

Posted by: kshmidheiser <kshmidheiser@...>

Building on Steve VanVoorst's tutorials, this crude URL launcher
demonstrates basic programmatic creation and formatting of an
NSTextField (in my opinion they are easier to format than FB Edit
Fields, but creating code to make them work is several degrees more
difficult.) It also links the field and launch button to a controller
method.

As Steve earlier pointed out, if the NSTextField was created in
Interface Builder, the -(IBOutlet) creation would be automatic, but
in our FB programmatic construction we must create the action methods
and wire them in code.

Some nice additions to this code would be a "Launch URL..." menu
item, and adding carriage return detection to NSTextField. Maybe
someone is up to the challenge.

I should note that the URL creation here is rudimentary and would
require extra code, such as the inclusion of percentage sign escapes,
for complicated URLs.

And one major disappointment with NSTextField is there is no easy way
to change the caret color. I originally created the field with a
black background and white text. But the black caret disappeared into
the black background. So for this demo I changed the background color
to gray. I hear you may be able to get the underlying NSTextView and
change it's caret color, but that's way beyond the scope of this.

The point here is to demonstrate some simple formatting and linking.

Ken

'------ begin code -------
compile as "Objective-C"

if system(_sysVers) < 1060 then stop "Requires OS 10.6 or newer
unless line 78 is remarked out"

BeginCDeclaration
@interface AppDelegate : NSObject
//methods automatically called
@end //interface
EndC

BeginCFunction
@implementation AppDelegate
// --- Taken from NSApplicationDelegate Protocol Reference ---- //
-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication
*) theApp;
{
return YES;
}
@end //implementation
EndC

BeginCDeclaration
@interface AppController : NSObject
{

}
// ---- Methods listed here ---- //
- (BOOL) myBtnAction:(id)sender;

@end //interface
EndC

BeginCFunction
@implementation AppController

id gURLFieldID;

// --- Methods defined here ---- //
-(BOOL) myBtnAction:(id)sender
{
// Grab string from NSTextField
NSString *urlStr = [gURLFieldID stringValue];
NSString *httpStr = @"http://";
NSString *launchStr;
NSRange textRange;
BOOL isOK;

// The NSURL method URLWithString requires a URL string to be
// prepended with "http://". Here we do a quick search for
// the HyperText Transfer Protocol, and if it is not present
// we prepend it to the URL string.

textRange = [urlStr rangeOfString:httpStr options:NSCaseInsensitiveSearch];
if( textRange.location != NSNotFound )
{
// ...found it so copy urlStr to launchStr
launchStr = [NSString stringWithString: urlStr];
}
else
{
// ...didn't find it so prepend "http://" to urlStr
launchStr = [httpStr stringByAppendingString:urlStr];
}

// Launch the URL in the default browser
isOK = [[NSWorkspace sharedWorkspace] openURL:[NSURL
URLWithString:launchStr]];
return( isOK );
}
@end //implementation
EndC

BeginCCode
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

[NSApplication sharedApplication];

// NOTE: Remark out the following line to run in OS 10.5
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

// --- Menu bar and menus ---- //
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id quitMenuItem = [[[NSMenuItem alloc]
initWithTitle:@"Quit"action:@selector(terminate:) keyEquivalent:@"q"]
autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
// ---- Window ------- //
#define _wndW 430
#define _wndH 150
id window = [ [ NSWindow alloc ]
initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSClosableWindowMask | NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window center];
[window setTitle: @"Controller/Delegate_demo" ];
[window makeKeyAndOrderFront:nil];

// Create an instance of AppDelegate (NSObject)
id myDelegate = [[AppDelegate alloc]init];
[NSApp setDelegate:myDelegate];

// Create an instance of AppController (NSObject)
id myController = [[AppController alloc]init];

// Programatically create and format NSTextField with some typical options
NSTextField *urlField = [[NSTextField
alloc]initWithFrame:NSMakeRect( 40, _wndH - 70, 350, 30 )];
[[window contentView] addSubview: urlField];
[urlField setEditable:YES];
[urlField setSelectable:YES];
[urlField setBezeled:YES];
[urlField setFont:[NSFont fontWithName:@"Arial Bold" size:18]];
[urlField setTextColor:[NSColor whiteColor]];
[urlField setAlignment:NSCenterTextAlignment];
[urlField setStringValue: @"https://welovegod.org/groups/fbcocoa/" ];
[urlField setBackgroundColor:[NSColor grayColor]];
[urlField setDrawsBackground:YES];
gURLFieldID = urlField;
[urlField release];

NSButton *launchBtn = [[NSButton alloc]initWithFrame:NSMakeRect(
_wndW - 270, 20, 140, 30 )];
[launchBtn setBezelStyle:NSRoundedBezelStyle ];
[launchBtn setTitle: @"Launch URL..." ];
[launchBtn setTarget:myController];
[launchBtn setAction:@selector(myBtnAction:)];
[[window contentView] addSubview: launchBtn];
[launchBtn release];

NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect(
_wndW - 130, 20, 95, 30 )];
[quitBtn setBezelStyle:NSRoundedBezelStyle ];
[quitBtn setTitle: @"Quit" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
[quitBtn release];

[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
[pool drain];
return NSApplicationMain(argc, (const char **) argv);
}
EndC
'------ end code -------