[fpc-devel] Creating a Cocoa Application

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Mon Jan 7 10:37:03 CET 2008


I reread the docs and it seams that when calling alloc I should pass
the class id as "self", then use it's result as "self" for init and
then use the result from init to call subsequent methods.

Ok, this worked fine now =) Final version of the simple pascal cocoa app bellow.

This is excelent, because it means we can have cocoa bindings purely
written in pascal.

program cocoamsgbox;

{$mode objfpc}{$H+}

uses
  objc, FPCMacOSAll, appkit;

const
  Str_NSAutoreleasePool = 'NSAutoreleasePool';
  Str_alloc = 'alloc';
  Str_init = 'init';
  Str_release = 'release';
  Str_Panel_Title = 'This is the title';
  Str_Panel_Message = 'This is the message';
var
  { classes }
  NSAutoreleasePoolId: objc.id;
  { objects }
  allocbuf, pool: objc.id;
  { strings }
  CFTitle, CFMessage: CFStringRef;
begin
//  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSAutoreleasePoolId := objc_getClass(PChar(Str_NSAutoreleasePool));
  allocbuf := objc_msgSend(NSAutoreleasePoolId,
sel_registerName(PChar(Str_alloc)), []);
  pool := objc_msgSend(allocbuf, sel_registerName(PChar(Str_init)), []);

  NSApplicationLoad();

  CFTitle := CFStringCreateWithCString(nil, PChar(Str_Panel_Title),
kCFStringEncodingUTF8);
  CFMessage := CFStringCreateWithCString(nil,
PChar(Str_Panel_Message), kCFStringEncodingUTF8);

  { uses a default "OK" button and no alternate buttons }
  NSRunAlertPanel(CFTitle, CFMessage, nil, nil, nil, []);

  CFRelease(CFTitle);
  CFRelease(CFMessage);

//  [pool release];
  objc_msgSend(pool, sel_registerName(PChar(Str_release)), []);
end.



More information about the fpc-devel mailing list