[fpc-devel] More about calling objc methods from pascal

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Sat Jan 19 10:33:45 CET 2008


Hello,

I am proceding to build the initial part of what would be Cocoa Pascal
bindings, but I got really stuck in one part.

Calling methods without any parameters works fine, but I just started
with the first method with parameters and it just doesn't work. It's a
trivial program that just shows a window. The objc version works fine
and shows the window. The pascal version runs fine, except that no
window is shown. I have already compared both assemblers, but couldn't
find anything that shows the reason of the problem =/

I tryed various possibilities: pass the parameters in the reverse
order, pass a pointer of the full structure of a NSRect in one
parameter, etc.

Here is the problematic part:

constructor NSWindow.initWithContentRect(
  contentRect: NSRect;
  aStyle: cuint;
  bufferingType: NSBackingStoreType;
  defer: CBOOL);
begin
{    NSWindow* MainWindow = [[NSWindow alloc] initWithContentRect:
MainWindowRect
      styleMask: NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask
        backing: NSBackingStoreBuffered
          defer: NO]; }
  ClassId := objc_getClass(PChar(Str_NSWindow));
  allocbuf := objc_msgSend(ClassId, sel_registerName(PChar(Str_alloc)), []);
  Handle := objc_msgSend(allocbuf,
sel_registerName(PChar(Str_initWithContentRect)),
   [contentRect.origin.x, contentRect.origin.y,
contentRect.size.width, contentRect.size.height,
    aStyle, bufferingType, defer]);
end;

procedure NSWindow.orderFrontRegardless;
begin
  objc_msgSend(Handle, sel_registerName(PChar(Str_orderFrontRegardless)), []);
end;

Here is the obj program:

int main(int argc, char *argv[])
{
    // Creates an autorelease pool
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // Creates the application NSApp object
    [NSApplication sharedApplication];

    // Creates a simple window
    NSRect MainWindowRect = NSMakeRect(300, 300, 300, 500);

    NSWindow* MainWindow = [[NSWindow alloc] initWithContentRect: MainWindowRect
      styleMask: NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask
        backing: NSBackingStoreBuffered
          defer: NO];

    [MainWindow orderFrontRegardless];

 //   CreateMenu();

    // Enters main message loop

    [NSApp run];

    // Call release method from object pool
    [pool release];

    return 0;
}

Here is the pascal program (class calls are wrapped inside the bindings):

var
  { classes }
  pool: NSAutoreleasePool;
  MainWindow: NSWindow;
  { strings }
  CFTitle, CFMessage: CFStringRef;
  { sizes }
  MainWindowRect: NSRect;
begin
  {  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; }
  pool := NSAutoreleasePool.Create;

  // Creates the application NSApp object
  NSApp := NSApplication.sharedApplication;

  // Creates a simple window
  MainWindowRect.origin.x := 300.0;
  MainWindowRect.origin.y := 300.0;
  MainWindowRect.size.width := 300.0;
  MainWindowRect.size.height := 500.0;

  MainWindow := NSWindow.initWithContentRect(MainWindowRect,
    NSTitledWindowMask or NSClosableWindowMask or
NSMiniaturizableWindowMask or NSResizableWindowMask,
    NSBackingStoreBuffered, NO);

  MainWindow.orderFrontRegardless;

//    CreateMenu();

  { Enters main message loop }

  NSApp.run;

  {  [pool release]; }
  pool.Free;
end.

thanks,
-- 
Felipe Monteiro de Carvalho



More information about the fpc-devel mailing list