Difference between revisions of "FPC PasCocoa"

From Lazarus wiki
Jump to navigationJump to search
 
(24 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
{{Platform only|macOS|macOS|macOS}}
 +
{{Platform only|iOS|iOS|iOS}}
 
== Introduction ==
 
== Introduction ==
  
A large and growing fraction of the Mac OS X system frameworks are written in Objective-C and only offer an Objective-C interface. This makes it unwieldy to use them directly from other programming languages. The Objective-C run time library, which is used for everything from defining classes to sending messages (calling methods), has a public procedural C interface however, which makes it possible to integrate other languages directly with Objective-C code. Two general approaches exist.
+
A large and growing fraction of the macOS system frameworks are written in Objective-C and only offer an Objective-C interface. This makes it unwieldy to use them directly from other programming languages. The Objective-C run time library, which is used for everything from defining classes to sending messages (calling methods), has a public procedural C interface however, which makes it possible to integrate other languages directly with Objective-C code. Two general approaches exist.
  
 
The first approach is to create a ''bridge''. In this case calls to the Objective-C run time helpers are wrapped in host language constructs and helpers. The host language itself is generally not modified. A number of examples of Cocoa bridges can be found at http://www.cocoadev.com/index.pl?CocoaBridges.
 
The first approach is to create a ''bridge''. In this case calls to the Objective-C run time helpers are wrapped in host language constructs and helpers. The host language itself is generally not modified. A number of examples of Cocoa bridges can be found at http://www.cocoadev.com/index.pl?CocoaBridges.
Line 11: Line 13:
 
Before the compiler supported Objective-Pascal (as described below), the primary way for Objective-C interfacing was through ObjFPC wrapper classes. This process is described on the [[PasCocoa]] page, and falls in the ''bridge'' approach mentioned above. This approach is no longer recommended nor maintained.
 
Before the compiler supported Objective-Pascal (as described below), the primary way for Objective-C interfacing was through ObjFPC wrapper classes. This process is described on the [[PasCocoa]] page, and falls in the ''bridge'' approach mentioned above. This approach is no longer recommended nor maintained.
  
== Objective-C FPC Compiler ==
+
== Objective-Pascal FPC Compiler ==
  
All Objective-C support is now available in svn trunk. For instructions on how to check it out, see http://www.freepascal.org/develop.var#svn
+
All Objective-Pascal support is available in FPC 2.6.0 and later.
  
For an overview of some conceptual differences between Objective-Pascal and Object Pascal on the one hand, and between Objective-Pascal and Objective-C on the other hand, see [[FPC_PasCocoa/Differences]].
+
For an overview of some conceptual differences between Objective-Pascal and Object Pascal on the one hand, and between Objective-Pascal and Objective-C on the other hand, see [[FPC_PasCocoa/Differences|FPC PasCocoa differences]].
  
 
== Syntax (implemented) ==
 
== Syntax (implemented) ==
Line 21: Line 23:
 
=== Enabling Objective-C/Objective-Pascal ===
 
=== Enabling Objective-C/Objective-Pascal ===
  
The compiler has mode switches to enable the use of Objective-C-related constructs. To enabled Objective-C 1.0 language features, either add ''{$modeswitch objectivec1}'' to a source file, or using the ''-Mobjectivec1'' command line switch. Similarly, for Objective-C 2.0 language features use ''{$modeswitch objectivec2}'' resp. ''-Mobjectivec2'' (this automatically also enables Objective-C 1.0 features). Note that in the latter case, the resulting program may only work on Mac OS X 10.5 and later. Due to these being mode switches rather than actual syntax modes, they can be used in combination with every general syntax mode (fpc, objfpc, tp, delphi, macpas).
+
The compiler has mode switches to enable the use of Objective-C-related constructs. To enabled Objective-C 1.0 language features, either add ''{$modeswitch objectivec1}'' to a source file, or using the ''-Mobjectivec1'' command line switch. Similarly, for Objective-C 2.0 language features use ''{$modeswitch objectivec2}'' resp. ''-Mobjectivec2'' (this automatically also enables Objective-C 1.0 features). Note that in the latter case, the resulting program may only work on Leopard 10.5 and later. Due to these being mode switches rather than actual syntax modes, they can be used in combination with every general syntax mode (fpc, objfpc, tp, delphi, macpas).
  
 
Note that mode switches are reset when the general syntax mode is changed using, e.g., ''{$mode objfpc}''. So if you have such a statement in a unit, compiling it with ''-Mobjectivec1'' will not change anything. You will have to add ''{$modeswitch objectivec1}'' in the source code after the ''{$mode objfpc}'' in that case.
 
Note that mode switches are reset when the general syntax mode is changed using, e.g., ''{$mode objfpc}''. So if you have such a statement in a unit, compiling it with ''-Mobjectivec1'' will not change anything. You will have to add ''{$modeswitch objectivec1}'' in the source code after the ''{$mode objfpc}'' in that case.
Line 31: Line 33:
 
To declare a selector for a method, use the ''objcselector()'' statement. It returns a value of the type ''objc.SEL''. It accepts one parameter, which can either be a constant string representing an Objective-C method name, or a method of an Objective-C class.
 
To declare a selector for a method, use the ''objcselector()'' statement. It returns a value of the type ''objc.SEL''. It accepts one parameter, which can either be a constant string representing an Objective-C method name, or a method of an Objective-C class.
  
<source>
+
<syntaxhighlight lang="pascal">
 
{$modeswitch objectivec1}
 
{$modeswitch objectivec1}
 
var
 
var
Line 39: Line 41:
 
   a:=objcselector('myMethod');
 
   a:=objcselector('myMethod');
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
  
Line 46: Line 48:
 
In general methods are declared in the same way as in Object Pascal, with the exception that each objcclass method must also have a messaging name specified:
 
In general methods are declared in the same way as in Object Pascal, with the exception that each objcclass method must also have a messaging name specified:
  
<source>
+
<syntaxhighlight lang="pascal">
 
NSSomeObject = objcclass(NSObject)
 
NSSomeObject = objcclass(NSObject)
 
   procedure method(params: Integer); message 'method:';
 
   procedure method(params: Integer); message 'method:';
 
   class procedure classmethod(para: char); override; // "message 'classmethod:'" not required, compiler will get this from the parent class
 
   class procedure classmethod(para: char); override; // "message 'classmethod:'" not required, compiler will get this from the parent class
 
end;
 
end;
</source>
+
</syntaxhighlight>
  
 
Every objcclass/objcprotocol/objccategory method must be associated with an Objective-C message name, a.k.a. ''selector''. This can happen in two different ways:
 
Every objcclass/objcprotocol/objccategory method must be associated with an Objective-C message name, a.k.a. ''selector''. This can happen in two different ways:
Line 66: Line 68:
  
 
To declare an Objective-C class, use the keyword ''objcclass''. Objective-C classes must be declared like regular Object Pascal types in a ''type'' block:
 
To declare an Objective-C class, use the keyword ''objcclass''. Objective-C classes must be declared like regular Object Pascal types in a ''type'' block:
<source>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   ObjCClassName =  objcclass [external [name 'ExternalClassName']] [(ObjCSuperClassName|ProtocolName [, ProtocolName, ProtocolName])]  
 
   ObjCClassName =  objcclass [external [name 'ExternalClassName']] [(ObjCSuperClassName|ProtocolName [, ProtocolName, ProtocolName])]  
Line 73: Line 75:
 
     [method declarations]
 
     [method declarations]
 
end;  
 
end;  
</source>
+
</syntaxhighlight>
  
 
* ''objcclass'': defines an Objective-C class type declaration
 
* ''objcclass'': defines an Objective-C class type declaration
 
* ''external'': many Objective-C classes are implemented in external frameworks, such as Cocoa. The ''external'' modifiers enables you to declare such external classes for use in Pascal code. It is possible to specify a different external name for the class than the identifier used in Pascal code, because the duplicate identifier rules in Objective-C are laxer than those of Pascal (e.g., there are both a class and a protocol called ''NSObject'' in Cocoa, so one of the two has to be renamed in Pascal)
 
* ''external'': many Objective-C classes are implemented in external frameworks, such as Cocoa. The ''external'' modifiers enables you to declare such external classes for use in Pascal code. It is possible to specify a different external name for the class than the identifier used in Pascal code, because the duplicate identifier rules in Objective-C are laxer than those of Pascal (e.g., there are both a class and a protocol called ''NSObject'' in Cocoa, so one of the two has to be renamed in Pascal)
* ObjCSuperClassName: defines parent (or super) class for the class. If name is not specified, the class defines a new root class. See http://wiki.freepascal.org/FPC_PasCocoa/Differences#No_unique_root_class for more information.
+
* ObjCSuperClassName: defines parent (or super) class for the class. If name is not specified, the class defines a new root class. See [[FPC_PasCocoa/Differences#No_unique_root_class|no unique root class]] for more information.
  
 
For example:
 
For example:
<source>
+
 
 +
<syntaxhighlight lang="pascal">
 
// NSView is an external class, declared in some external framework
 
// NSView is an external class, declared in some external framework
 
// External classes from Apple's framework
 
// External classes from Apple's framework
Line 115: Line 118:
 
begin
 
begin
 
end;
 
end;
 
+
</syntaxhighlight>
 
 
</source>
 
  
 
====Formal declaration====
 
====Formal declaration====
Line 128: Line 129:
 
To declare a ''formal objcclass'', use the following syntax:
 
To declare a ''formal objcclass'', use the following syntax:
  
<source>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   // note:
 
   // note:
Line 135: Line 136:
 
   //    to which this formal definition resolves later on
 
   //    to which this formal definition resolves later on
 
   MyExternalClass = objcclass external;
 
   MyExternalClass = objcclass external;
</source>
+
</syntaxhighlight>
  
 
Example:
 
Example:
<source>
+
 
 +
<syntaxhighlight lang="pascal">
 
unit ContainerClass;
 
unit ContainerClass;
  
Line 230: Line 232:
 
   l:=c.getItem.getContent;
 
   l:=c.getItem.getContent;
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
====Dynamically allocating classes at runtime====
+
====Objective-Pascal class references====
  
When dynamically allocating classes at runtime you need to declare an additional type which references the Objective-C meta-class (which is the type NSObject.classClass returns and what pobjc_class represents).
+
Just like in Object Pascal, it is possible to declare a class reference type for an Objective-Pascal class. In Objective-C, these are called meta-classes and are represented by the ''pobjc_class'' type. If you wish to mix code using the ''pobjc_class'' type and the Pascal syntax for class references, you have to use typecasts, as shown below.
  
<source>
+
<syntaxhighlight lang="pascal">
 
{$mode objfpc}
 
{$mode objfpc}
 
{$modeswitch objectivec1}
 
{$modeswitch objectivec1}
Line 245: Line 247:
 
   TMyClass = objcclass (NSObject)
 
   TMyClass = objcclass (NSObject)
 
   end;
 
   end;
  TMyClassMeta = class of TMyClass;  // declare the meta-class type for dynamic allocation
+
  TMyClassMeta = class of TMyClass;  // declare the meta-class type for dynamic allocation
  
 
var
 
var
 
   theClass: pobjc_class;
 
   theClass: pobjc_class;
 
begin
 
begin
   theClass := TMyClass.classClass;      // get the metal-class reference for TMyClass
+
   theClass := TMyClass.classClass;      // get the meta-class reference for TMyClass; could
 
   TMyClassMeta(theClass).alloc.init;    // cast theClass as TMyClassMeta and send messages as normal
 
   TMyClassMeta(theClass).alloc.init;    // cast theClass as TMyClassMeta and send messages as normal
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
 
=== Protocol declaration ===
 
=== Protocol declaration ===
Line 259: Line 261:
 
An Objective-C Protocol is pretty much the same as an ''interface'' in Object Pascal: it lists a number of methods to be implemented by classes that conform to the protocol. The only difference is that protocol methods can be ''optional''. Moreover, unlike in Object Pascal, a protocol can inherit from multiple other protocols:
 
An Objective-C Protocol is pretty much the same as an ''interface'' in Object Pascal: it lists a number of methods to be implemented by classes that conform to the protocol. The only difference is that protocol methods can be ''optional''. Moreover, unlike in Object Pascal, a protocol can inherit from multiple other protocols:
  
<source>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   ObjCProtocolName =  objcprotocol [external [name 'ExternalClassName']] [(ObjCParentProtocol1[, ObjCParentProtocol2, ...])]  
 
   ObjCProtocolName =  objcprotocol [external [name 'ExternalClassName']] [(ObjCParentProtocol1[, ObjCParentProtocol2, ...])]  
Line 265: Line 267:
 
     [method declarations]
 
     [method declarations]
 
end;
 
end;
</source>
+
</syntaxhighlight>
  
 
* ''objcprotocol'': defines an Objective-C protocol type declaration
 
* ''objcprotocol'': defines an Objective-C protocol type declaration
Line 273: Line 275:
  
 
Example:
 
Example:
<source>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   MyProtocol = objccprotocol
 
   MyProtocol = objccprotocol
Line 291: Line 293:
 
     function aSecondRequiredMethod: longint;
 
     function aSecondRequiredMethod: longint;
 
   end;
 
   end;
</source>
+
</syntaxhighlight>
  
 
Similar to ''objcclass''es, ''objcprotocol''s can be external in case they are implemented in e.g. the Cocoa framework.
 
Similar to ''objcclass''es, ''objcprotocol''s can be external in case they are implemented in e.g. the Cocoa framework.
Line 301: Line 303:
 
Note that a category cannot extend another category. Furthermore, if two categories add/replace a method with the same selector in the same class, it is undefined which method will actually be called at run time. This would depend on the order in which the categories are registered with the Objective-C run time.
 
Note that a category cannot extend another category. Furthermore, if two categories add/replace a method with the same selector in the same class, it is undefined which method will actually be called at run time. This would depend on the order in which the categories are registered with the Objective-C run time.
  
<source>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   ObjCCategoryName =  objccategory [external [name 'ExternalCategoryName']] (ExtendedObjCClass[,ObjCProtocol1, ObjCProtocol2, ...])
 
   ObjCCategoryName =  objccategory [external [name 'ExternalCategoryName']] (ExtendedObjCClass[,ObjCProtocol1, ObjCProtocol2, ...])
 
     [method declarations]
 
     [method declarations]
 
end;
 
end;
</source>
+
</syntaxhighlight>
  
 
* ''objccategory'': defines an Objective-C category type declaration
 
* ''objccategory'': defines an Objective-C category type declaration
Line 314: Line 316:
  
 
Example:
 
Example:
<source>
+
<syntaxhighlight lang="pascal">
 
type
 
type
 
   MyProtocol = objcprotocol
 
   MyProtocol = objcprotocol
Line 333: Line 335:
 
   end;
 
   end;
  
</source>
+
</syntaxhighlight>
  
 
Similar to ''objcclass''es, ''objccategory''s can be external in case they are implemented in e.g. the Cocoa framework.
 
Similar to ''objcclass''es, ''objccategory''s can be external in case they are implemented in e.g. the Cocoa framework.
Line 341: Line 343:
 
The ''id'' type is special in Objective-C/Pascal. It is assignment-compatible with instances of every objcclass and objcprotocol type, in two directions. This means that you can assign variables of any objcclass/objcprotocol type to a variable of the type ''id'', but also that you can assign these ''id'' variables to variables of any particular objcclass/objcprotocol type. In neither case an explicit typecast is required.
 
The ''id'' type is special in Objective-C/Pascal. It is assignment-compatible with instances of every objcclass and objcprotocol type, in two directions. This means that you can assign variables of any objcclass/objcprotocol type to a variable of the type ''id'', but also that you can assign these ''id'' variables to variables of any particular objcclass/objcprotocol type. In neither case an explicit typecast is required.
  
Furthermore, it is possible to call any Objective-C method declared in an objcclass or objccategory that's in scope using an ''id''-typed variable. If, at run time, the objcclass instance stored in the ''id''-typed variable does not respond to the sent message, the program will terminate with a run time error.
+
Furthermore, it is possible [[FPC_PasCocoa/Differences#Sending_messages_to_id|to call any Objective-C method declared]] in an objcclass or objccategory that's in scope using an ''id''-typed variable. If, at run time, the objcclass instance stored in the ''id''-typed variable does not respond to the sent message, the program will terminate with a run time error.
  
 
When there are multiple methods with the same Pascal identifier, the compiler will use the standard overload resolution logic to pick the most appropriate method. In this process, it will behave as if all objcclass/objccategory methods in scope have been declared as global procedures/functions with the ''overload'' specifier. If the compiler cannot determine which overloaded method to call, it will print an error. If you compile with -vh, it will print a list of all methods that could be used to implement the call when it cannot determine the appropriate overloaded method. In this case, you will have to use an explicit type cast to clarify the class type from which you wish to call a method.
 
When there are multiple methods with the same Pascal identifier, the compiler will use the standard overload resolution logic to pick the most appropriate method. In this process, it will behave as if all objcclass/objccategory methods in scope have been declared as global procedures/functions with the ''overload'' specifier. If the compiler cannot determine which overloaded method to call, it will print an error. If you compile with -vh, it will print a list of all methods that could be used to implement the call when it cannot determine the appropriate overloaded method. In this case, you will have to use an explicit type cast to clarify the class type from which you wish to call a method.
Line 350: Line 352:
  
 
Example:
 
Example:
<source>
+
<syntaxhighlight lang="pascal">
 
{$mode delphi}
 
{$mode delphi}
 
{$modeswitch objectivec2}
 
{$modeswitch objectivec2}
Line 388: Line 390:
 
   pool.release;
 
   pool.release;
 
end.
 
end.
</source>
+
</syntaxhighlight>
  
 
== Syntax (proposed) ==
 
== Syntax (proposed) ==
Line 398: Line 400:
 
-- the dollar sign could work... ex. $theString
 
-- the dollar sign could work... ex. $theString
  
== ObjectiveP Examples ==
+
== Objective Pascal Examples ==
  
=== Creating and running application with one menu item ===
+
=== Application with one menu item ===
  
 
Please note, that for the creation of the menu, the application must be located in the application bundle. Lazarus may create the bundle for you.
 
Please note, that for the creation of the menu, the application must be located in the application bundle. Lazarus may create the bundle for you.
  
<source>
+
<syntaxhighlight lang="pascal">
 
  program project1;
 
  program project1;
 
   
 
   
Line 471: Line 473:
 
   pool.release;
 
   pool.release;
 
  end.
 
  end.
</source>
+
</syntaxhighlight>
 +
 
 +
===Application with a single window and no menus===
 +
 
 +
<syntaxhighlight lang="pascal">
 +
program simplewindow;
 +
 
 +
{$mode objfpc}{$H+}
 +
{$modeswitch objectivec2}
 +
 
 +
uses
 +
  CocoaAll;
 +
 
 +
var
 +
  appName: NSString;
 +
  window: NSWindow;
 +
  pool: NSAutoreleasePool;
 +
begin
 +
  pool := NSAutoreleasePool.new;
 +
  NSApp := NSApplication.sharedApplication;
 +
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
 +
  appName := NSProcessInfo.processInfo.processName;
 +
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
 +
    NSTitledWindowMask, NSBackingStoreBuffered, False).autorelease;
 +
  window.center;
 +
  window.setTitle(appName);
 +
  window.makeKeyAndOrderFront(nil);
 +
  NSApp.activateIgnoringOtherApps(true);
 +
  NSApp.run;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
===Application with a simple window and menus===
 +
 
 +
<syntaxhighlight lang="pascal">
 +
program simplewindow;
 +
 
 +
{$mode objfpc}{$H+}
 +
{$modeswitch objectivec2}
 +
 
 +
uses
 +
  CocoaAll;
 +
 
 +
var
 +
  menubar: NSMenu;
 +
  appMenu: NSMenu;
 +
  appName: NSString;
 +
  quitMenuItem: NSMenuItem;
 +
  appMenuItem: NSMenuItem;
 +
  quitTitle: NSString;
 +
  window: NSWindow;
 +
  pool: NSAutoreleasePool;
 +
begin
 +
  pool := NSAutoreleasePool.new;
 +
  NSApp := NSApplication.sharedApplication;
 +
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
 +
  menubar := NSMenu.alloc.autorelease;
 +
  appMenuItem := NSMenuItem.alloc.autorelease;
 +
  menubar.addItem(appMenuItem);
 +
  NSApp.setMainMenu(Menubar);
 +
  appMenu := NSMenu.new.autorelease;
 +
  appName := NSProcessInfo.processInfo.processName;
 +
  quitTitle := NSSTR('Quit ').stringByAppendingString(appName);
 +
  quitMenuItem := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(quitTitle,
 +
  sel_registerName(PChar('terminate:')),
 +
  NSSTR('q'));
 +
  appMenu.addItem(quitMenuItem);
 +
  appMenuitem.setSubmenu(appMenu);
 +
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
 +
    NSTitledWindowMask, NSBackingStoreBuffered, False).autorelease;
 +
  window.center;
 +
  window.setTitle(appName);
 +
  window.makeKeyAndOrderFront(nil);
 +
  NSApp.activateIgnoringOtherApps(true);
 +
  NSApp.run;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
===Simple window and a non-editable text field inside it===
 +
 
 +
This shows how to create and position a sub-control in a window:
 +
 
 +
<syntaxhighlight lang="pascal">
 +
program simplewindow;
 +
 
 +
{$mode objfpc}{$H+}
 +
{$modeswitch objectivec2}
 +
 
 +
uses
 +
  CocoaAll;
 +
 
 +
var
 +
  appName: NSString;
 +
  window: NSWindow;
 +
  pool: NSAutoreleasePool;
 +
  lText: NSTextField;
 +
begin
 +
  // Autorelease pool, app and window creation
 +
  pool := NSAutoreleasePool.new;
 +
  NSApp := NSApplication.sharedApplication;
 +
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
 +
  appName := NSProcessInfo.processInfo.processName;
 +
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
 +
    NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,
 +
    NSBackingStoreBuffered, False).autorelease;
 +
 
 +
  // text label
 +
  lText := NSTextField.alloc.initWithFrame(NSMakeRect(50, 50, 200, 50)).autorelease;
 +
  lText.setBezeled(False);
 +
  lText.setDrawsBackground(False);
 +
  lText.setEditable(False);
 +
  lText.setSelectable(False);
 +
  lText.setStringValue(NSSTR('NSTextField'));
 +
  window.contentView.addSubview(lText);
 +
 
 +
  // Window showing and app running
 +
  window.center;
 +
  window.setTitle(appName);
 +
  window.makeKeyAndOrderFront(nil);
 +
  NSApp.activateIgnoringOtherApps(true);
 +
  NSApp.run;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
===Button which handles an event===
 +
 
 +
This event shows how to make a button that responds to clicking on it.
 +
 
 +
<syntaxhighlight lang="pascal">
 +
program simplewindow;
 +
 
 +
{$mode objfpc}{$H+}
 +
{$modeswitch objectivec1}
 +
 
 +
uses
 +
  CocoaAll;
 +
 
 +
type
 +
  { TMyDelegate }
 +
  TMyDelegate = objcclass(NSObject)
 +
  public
 +
    procedure HandleButtonClick(sender: id); message 'HandleButtonClick:';
 +
  end;
 +
 
 +
var
 +
  appName: NSString;
 +
  window: NSWindow;
 +
  pool: NSAutoreleasePool;
 +
  lText: NSTextField;
 +
  lButton: NSButton;
 +
  lDelegate: TMyDelegate;
 +
 
 +
procedure TMyDelegate.HandleButtonClick(sender: id);
 +
begin
 +
  lText.setStringValue(NSSTR('New text!'));
 +
end;
 +
 
 +
begin
 +
  // Autorelease pool, app and window creation
 +
  pool := NSAutoreleasePool.new;
 +
  NSApp := NSApplication.sharedApplication;
 +
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
 +
  appName := NSProcessInfo.processInfo.processName;
 +
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
 +
    NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,
 +
    NSBackingStoreBuffered, False).autorelease;
 +
 
 +
  // text label
 +
  lText := NSTextField.alloc.initWithFrame(NSMakeRect(50, 50, 120, 50)).autorelease;
 +
  lText.setBezeled(False);
 +
  lText.setDrawsBackground(False);
 +
  lText.setEditable(False);
 +
  lText.setSelectable(False);
 +
  lText.setStringValue(NSSTR('NSTextField'));
 +
  window.contentView.addSubview(lText);
 +
 
 +
  // button
 +
  lButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 100, 120, 50)).autorelease;
 +
  window.contentView.addSubview(lButton);
 +
  lButton.setTitle(NSSTR('Button title!'));
 +
  lButton.setButtonType(NSMomentaryLightButton);
 +
  lButton.setBezelStyle(NSRoundedBezelStyle);
 +
 
 +
  // button event handler setting
 +
  lDelegate := TMyDelegate.alloc.init;
 +
  lButton.setTarget(lDelegate);
 +
  lButton.setAction(ObjCSelector(lDelegate.HandleButtonClick));
 +
 
 +
  // Window showing and app running
 +
  window.center;
 +
  window.setTitle(appName);
 +
  window.makeKeyAndOrderFront(nil);
 +
  NSApp.activateIgnoringOtherApps(true);
 +
  NSApp.run;
 +
end.
 +
</syntaxhighlight>
  
 
[[Category:Objective-C]]
 
[[Category:Objective-C]]
 
[[Category:Objective Pascal]]
 
[[Category:Objective Pascal]]
[[Category:Mac OS X]]
+
[[Category:macOS]]
 
[[Category:Headers and Bindings]]
 
[[Category:Headers and Bindings]]
 
[[Category:iOS]]
 
[[Category:iOS]]
 +
[[Category:Cocoa]]

Latest revision as of 13:50, 1 May 2021

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

Apple iOS new.svg

This article applies to iOS only.

See also: Multiplatform Programming Guide

Introduction

A large and growing fraction of the macOS system frameworks are written in Objective-C and only offer an Objective-C interface. This makes it unwieldy to use them directly from other programming languages. The Objective-C run time library, which is used for everything from defining classes to sending messages (calling methods), has a public procedural C interface however, which makes it possible to integrate other languages directly with Objective-C code. Two general approaches exist.

The first approach is to create a bridge. In this case calls to the Objective-C run time helpers are wrapped in host language constructs and helpers. The host language itself is generally not modified. A number of examples of Cocoa bridges can be found at http://www.cocoadev.com/index.pl?CocoaBridges.

A second approach is to extend the host language so that it can directly interface with Objective-C code using new language constructs. A list of such language extensions can be found at http://www.cocoadev.com/index.pl?CocoaLanguages. As far as Pascal is concerned, a first proposal for Objective-Pascal was made by MetroWerks in 1997. Based on this preliminary proposal (it was never finalised nor implemented by MetroWerks) and on evolutions of the Object Pascal language since that time, we have designed our own Objective-Pascal dialect.

PasCocoa wrappers

Before the compiler supported Objective-Pascal (as described below), the primary way for Objective-C interfacing was through ObjFPC wrapper classes. This process is described on the PasCocoa page, and falls in the bridge approach mentioned above. This approach is no longer recommended nor maintained.

Objective-Pascal FPC Compiler

All Objective-Pascal support is available in FPC 2.6.0 and later.

For an overview of some conceptual differences between Objective-Pascal and Object Pascal on the one hand, and between Objective-Pascal and Objective-C on the other hand, see FPC PasCocoa differences.

Syntax (implemented)

Enabling Objective-C/Objective-Pascal

The compiler has mode switches to enable the use of Objective-C-related constructs. To enabled Objective-C 1.0 language features, either add {$modeswitch objectivec1} to a source file, or using the -Mobjectivec1 command line switch. Similarly, for Objective-C 2.0 language features use {$modeswitch objectivec2} resp. -Mobjectivec2 (this automatically also enables Objective-C 1.0 features). Note that in the latter case, the resulting program may only work on Leopard 10.5 and later. Due to these being mode switches rather than actual syntax modes, they can be used in combination with every general syntax mode (fpc, objfpc, tp, delphi, macpas).

Note that mode switches are reset when the general syntax mode is changed using, e.g., {$mode objfpc}. So if you have such a statement in a unit, compiling it with -Mobjectivec1 will not change anything. You will have to add {$modeswitch objectivec1} in the source code after the {$mode objfpc} in that case.

Whether or not the compiler has support for Objective-C-related constructs can be checked using {$ifdef FPC_HAS_FEATURE_OBJECTIVEC1}. There will be no separate switches when new Objective-C-related features are implemented. Since all this works happens in FPC trunk, simply expect that people are using the latest version available.

Selectors

To declare a selector for a method, use the objcselector() statement. It returns a value of the type objc.SEL. It accepts one parameter, which can either be a constant string representing an Objective-C method name, or a method of an Objective-C class.

{$modeswitch objectivec1}
var
  a: SEL;
begin
  a:=objcselector('initiWithWidth:andHeight:');
  a:=objcselector('myMethod');
end.


Method declaration

In general methods are declared in the same way as in Object Pascal, with the exception that each objcclass method must also have a messaging name specified:

NSSomeObject = objcclass(NSObject)
  procedure method(params: Integer); message 'method:';
  class procedure classmethod(para: char); override; // "message 'classmethod:'" not required, compiler will get this from the parent class
end;

Every objcclass/objcprotocol/objccategory method must be associated with an Objective-C message name, a.k.a. selector. This can happen in two different ways:

  • an inherited method, an implemented protocol method or a method reintroduced by a category will automatically use the message name of the inherited/implemented/reintroduced method. If an explicit message name is specified, it must match the message name specified for the corresponding method.
  • in other cases, the message keyword must be used, followed by a constant string that contains the selector name.

Instance and class methods correspond respectively to methods preceded with "-" and "+" in Objective-C. Furthermore, every method is by definition virtual in Objective-C/Pascal.

Note: it's common for Objective-C to start method names with a lower case character.

Class declaration

Regular declaration/definition

To declare an Objective-C class, use the keyword objcclass. Objective-C classes must be declared like regular Object Pascal types in a type block:

type
  ObjCClassName =  objcclass [external [name 'ExternalClassName']] [(ObjCSuperClassName|ProtocolName [, ProtocolName, ProtocolName])] 
  [private, protected, public]
    [variables declarations]
    [method declarations]
end;
  • objcclass: defines an Objective-C class type declaration
  • external: many Objective-C classes are implemented in external frameworks, such as Cocoa. The external modifiers enables you to declare such external classes for use in Pascal code. It is possible to specify a different external name for the class than the identifier used in Pascal code, because the duplicate identifier rules in Objective-C are laxer than those of Pascal (e.g., there are both a class and a protocol called NSObject in Cocoa, so one of the two has to be renamed in Pascal)
  • ObjCSuperClassName: defines parent (or super) class for the class. If name is not specified, the class defines a new root class. See no unique root class for more information.

For example:

// NSView is an external class, declared in some external framework
// External classes from Apple's framework
// have all fields in private sections
NSView = objcclass external (NSResponder)
private
  _subview  : id; // private field
public
  function initWithFrame(rect : NSRect): id; message 'initWithFrame:';
  procedure addSubview(aview: NSView); message 'addSubview:';
  procedure setAutoresizingMask(mask: NSUInteger); message 'setAutoresizingMask:';
  procedure setAutoresizesSubviews(flag: LongBool); message 'setAutoresizesSubviews:';
  procedure drawRect(dirtyRect: NSRect); message 'drawRect:';
end;

// MyView is a locally implemented class
// that should be declared in the local unit
// * drawRect_ method is overriden (the 'message' of the inherited method will be reused)
// * customMessage_ is a newly added method
// * data is a newly added field
MyView = objcclass(MSView)
public
  data : Integer;
  procedure customMessage(dirtyRect: NSRect); message 'customMessage';
  procedure drawRect(dirtyRect: NSRect); override;
end;

... 
procedure MyView.customMessage(dirtyRect: NSRect); 
begin
end;

procedure MyView.drawRect(dirtyRect: NSRect); 
begin
end;

Formal declaration

In Objective-C, a class can also be declared formally using the @class SomeClass syntax. This is called a forward declaration in Objective-C, but because this term has a different meaning in Pascal, we have termed this a formal declaration in Objective-Pascal.

A formal declaration of an objcclass instructs the compiler to assume that somewhere, there is a complete definition of this objcclass (including fields, methods, etc), but that it is not in this unit. The practical upshot is that it is possible to use such a formal objcclass type for parameter types, field types, function result types and variable types, but that it is not possible to inherit from them, to send any messages to such instances, nor to access any fields (since the compiler does not know the full definition).

The compiler will automatically resolve any uses of formal objcclasses to the real declaration when this real declaration is also in scope. Hence, as soon as the unit containing the real declaration is added to the uses clause, the aforementioned restrictions no longer apply and the compiler will treat the occurrences of the formal objcclass type the same as the actual type.

To declare a formal objcclass, use the following syntax:

type
  // note:
  //  * no inheritance is specified
  //  * the external name (if any) must match the external name of any real definition
  //    to which this formal definition resolves later on
  MyExternalClass = objcclass external;

Example:

unit ContainerClass;

{$mode objfpc}
{$modeswitch objectivec1}

interface

type
  // Formal declaration of MyItemClass, which is defined in another unit
  MyItemClass = objcclass external;

  MyContainerClass = objcclass
    private
     item: MyItemClass;
    public
     function getItem: MyItemClass; message 'getItem';
  end;

implementation

  function MyContainerClass.getItem: MyItemClass;
    begin
       // we cannot send any message to item, but
       // we can use it in assignments
       result:=item;
    end;

end.

// ======================
// ======================

unit ItemClass;

{$mode objfpc}
{$modeswitch objectivec1}

interface


type
  // the actual definition of MyItemClass
  MyItemClass = objcclass(NSObject)
   private
    content : longint;
   public
     function initWithContent(c: longint): MyItemClass; message 'initWithContent:';
     function getContent: longint; message 'getContent';
  end;

implementation

  function MyItemClass.initWithContent(c: longint): MyItemClass;
    begin
      content:=c;
      result:=self;
    end;

  function MyItemClass.getContent: longint;
    begin
      result:=content;
    end;

end.

// ======================
// ======================

Program test;

{$mode objfpc}
{$modeswitch objectivec1}

// regardless of the order of the units in the uses-clause, as soon as
// ItemClass is used then the full declaration of MyItemClass is in
// scope and all uses of the formal declaration are replaced by the
// actual declaration
uses
  ItemClass, ContainerClass;

var
  c: MyContainerClass;
  l: longint;
begin
  c:=MyContainerClass.alloc.init;
 ...
  // even though MyContainerClass.getItem returns a "formal" MyItemClass,
  // the fact that the real declaration is in scope means that it can be
  // used as a regular MyItemClass instance
  l:=c.getItem.getContent;
end.

Objective-Pascal class references

Just like in Object Pascal, it is possible to declare a class reference type for an Objective-Pascal class. In Objective-C, these are called meta-classes and are represented by the pobjc_class type. If you wish to mix code using the pobjc_class type and the Pascal syntax for class references, you have to use typecasts, as shown below.

{$mode objfpc}
{$modeswitch objectivec1}

program Test;

type
  TMyClass = objcclass (NSObject)
  end;
  TMyClassMeta = class of TMyClass;  // declare the meta-class type for dynamic allocation

var
  theClass: pobjc_class;
begin
  theClass := TMyClass.classClass;      // get the meta-class reference for TMyClass; could 
  TMyClassMeta(theClass).alloc.init;    // cast theClass as TMyClassMeta and send messages as normal
end.

Protocol declaration

An Objective-C Protocol is pretty much the same as an interface in Object Pascal: it lists a number of methods to be implemented by classes that conform to the protocol. The only difference is that protocol methods can be optional. Moreover, unlike in Object Pascal, a protocol can inherit from multiple other protocols:

type
  ObjCProtocolName =  objcprotocol [external [name 'ExternalClassName']] [(ObjCParentProtocol1[, ObjCParentProtocol2, ...])] 
   [required, optional]
    [method declarations]
end;
  • objcprotocol: defines an Objective-C protocol type declaration
  • external: same as with objcclass
  • ObjCParentProtocols: the protocols this protocol inherits from (so a class conforming to this protocol, will also have to implement all methods from those other protocols)
  • required, optional: If nothing is specified, the default is required. optional methods do not have to be implemented by classes conforming to this protocol.

Example:

type
  MyProtocol = objccprotocol
    // default is required
    procedure aRequiredMethod; message 'aRequiredMethod';
   optional
    procedure anOptionalMethodWithPara(para: longint); message 'anOptionalMethodWithPara:';
    procedure anotherOptionalMethod; message 'anotherOptionalMethod';
   required
    function aSecondRequiredMethod: longint; message 'aSecondRequiredMethod';
  end;

  MyClassImplementingProtocol = objcclass(NSObject,MyProtocol)
    // the "message 'xxx'" modifiers can be repeated, but this is not required
    procedure aRequiredMethod;
    procedure anOptionalMethodWithPara(para: longint);
    function aSecondRequiredMethod: longint;
  end;

Similar to objcclasses, objcprotocols can be external in case they are implemented in e.g. the Cocoa framework.

Category declaration

An Objective-C category is similar to a class helper in Object Pascal: it allows adding methods to an existing class, without inheriting from that class. In Objective-C, this functionality goes even further: multiple categories can be in scope at the same time for a particular class, and a category can also replace existing methods in another class rather than only add new ones. Since all methods are virtual in Objective-C, this also means that this method changes for all classes that inherit from the class in which the method was replaced (unless they override it). An Objective-C category can also implement protocols.

Note that a category cannot extend another category. Furthermore, if two categories add/replace a method with the same selector in the same class, it is undefined which method will actually be called at run time. This would depend on the order in which the categories are registered with the Objective-C run time.

type
  ObjCCategoryName =  objccategory [external [name 'ExternalCategoryName']] (ExtendedObjCClass[,ObjCProtocol1, ObjCProtocol2, ...])
    [method declarations]
end;
  • objccategory: defines an Objective-C category type declaration
  • external: same as with objcclass. As of Objective-C 2.0, the name of a category is optional. Not yet implemented: to declare an external category without a name, use an empty string.
  • method declarations: the methods that this category adds or replaces in the extended class. These can be both class and instance methods. Note that when replacing a method in the extended class, you must mark this new method as reintroduce. The reason that we do not use override here, is that the method is really replaced in the original class. This means, e.g., that if you call inherited from the new method, that you will call this method in the parent class of the extended class, and not the replaced method. The replaced method is "lost".
  • ObjCParentProtocols: the protocols this category implements. The category will have to implement all required methods from these protocols.

Example:

type
  MyProtocol = objcprotocol
    procedure protocolmethod; message 'protocolmethod';
  end;

  MyCategory = objccategory(NSObject,MyProtocol)
   // method replaced in NSObject. This means that every objcclass that does not
   // override the hash function will now use this method instead. 
   function hash: cuint; reintroduce;

   // we have to implement this method, since we declare that we implement the
   // MyProtocol protocol. This method is added to NSObject afterwards.
   procedure protocolmethod;

   // a random class methods added to NSObject
   class procedure newmethod; message 'newmethod';
  end;

Similar to objcclasses, objccategorys can be external in case they are implemented in e.g. the Cocoa framework.

The id type

The id type is special in Objective-C/Pascal. It is assignment-compatible with instances of every objcclass and objcprotocol type, in two directions. This means that you can assign variables of any objcclass/objcprotocol type to a variable of the type id, but also that you can assign these id variables to variables of any particular objcclass/objcprotocol type. In neither case an explicit typecast is required.

Furthermore, it is possible to call any Objective-C method declared in an objcclass or objccategory that's in scope using an id-typed variable. If, at run time, the objcclass instance stored in the id-typed variable does not respond to the sent message, the program will terminate with a run time error.

When there are multiple methods with the same Pascal identifier, the compiler will use the standard overload resolution logic to pick the most appropriate method. In this process, it will behave as if all objcclass/objccategory methods in scope have been declared as global procedures/functions with the overload specifier. If the compiler cannot determine which overloaded method to call, it will print an error. If you compile with -vh, it will print a list of all methods that could be used to implement the call when it cannot determine the appropriate overloaded method. In this case, you will have to use an explicit type cast to clarify the class type from which you wish to call a method.

Fast enumeration

Fast enumeration is a convention that enables enumerating the elements in a Cocoa container class in a generic yet fast way. The syntax used for this feature is the for-in construct, both in Objective-C and in Objective-Pascal. This feature behaves identically in both languages. It requires the Objective-C 2.0 mode switch to be activated.

Example:

{$mode delphi}
{$modeswitch objectivec2}

uses
  CocoaAll;

var
  arr: NSMutableArray;
  element: NSString;
  pool: NSAutoreleasePool;
  i: longint;
begin
  pool:=NSAutoreleasePool.alloc.init;
  arr:=NSMutableArray.arrayWithObjects(
    NSSTR('One'),
    NSSTR('Two'),
    NSSTR('Three'),
    NSSTR('Four'),
    NSSTR('Five'),
    NSSTR('Six'),
    NSSTR('Seven'),
    nil);

  i:=0;
  for element in arr do
    begin
      inc(i);
      if i=2 then
        continue;
      if i=5 then
        break;
      if i in [2,5..10] then
        halt(1);
      NSLog(NSSTR('element: %@'),element);
    end;
  pool.release;
end.

Syntax (proposed)

todo:

a) how to declare a constant NSString/CFString in Pascal (the @"somestring" from Objective-C)

-- the dollar sign could work... ex. $theString

Objective Pascal Examples

Application with one menu item

Please note, that for the creation of the menu, the application must be located in the application bundle. Lazarus may create the bundle for you.

 program project1;
 
 {$mode objfpc}{$H+}
 {$modeswitch objectivec1}
  
 uses
   CocoaAll;
   
 type
   { MyCallback }
   MyCallback = objcclass(NSObject)
   public
     procedure haltMenuEvent(sender: id); message 'haltMenuEvent:';
   end;
 
 var
   pool     : NSAutoreleasePool;
   filemenu : NSMenu;
   subitem  : NSMenuItem;
   app      : NSApplication;
   callback : MyCallback;
  
 function NSStr(const s: AnsiString): NSString;
 begin
   if s='' then Result:=NSString.alloc.init
   else Result:=NSString.alloc.initWithCString(@s[1]);
 end;
 
 { MyCallback }
 
 procedure MyCallback.haltMenuEvent(sender:id);
 begin
   WriteLn('HALT!');
   app.stop(self);
 end;
 
 procedure initMenus;
 var
   root      : NSMenu;
   rootitem  : NSMenuItem;
 begin
   root:=NSMenu.alloc.init;
   rootitem:=root.addItemWithTitle_action_keyEquivalent(NSStr(''), nil, NSStr(''));
   filemenu:=NSMenu.alloc.initWithTitle(NSStr('File'));
   rootitem.setSubmenu(filemenu);
   rootitem.setEnabled(true);
   subitem:=filemenu.addItemWithTitle_action_keyEquivalent(NSStr('Halt'), nil, NSStr(''));
   subitem.setEnabled(true);
   app.setMainMenu(root);
 end;
 
 procedure InitCallback;
 begin
   callback:=MyCallback.alloc;
   subitem.setAction(ObjCSelector(callback.haltMenuEvent));
   subitem.setTarget(callback);
 end;
 
 begin
   pool:=NSAutoreleasePool.alloc.init;
   app:=NSApplication.sharedApplication;
   initMenus;
   initCallback;
   app.run;
   pool.release;
 end.

Application with a single window and no menus

program simplewindow;

{$mode objfpc}{$H+}
{$modeswitch objectivec2}

uses
  CocoaAll;

var
  appName: NSString;
  window: NSWindow;
  pool: NSAutoreleasePool;
begin
  pool := NSAutoreleasePool.new;
  NSApp := NSApplication.sharedApplication;
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
  appName := NSProcessInfo.processInfo.processName;
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
    NSTitledWindowMask, NSBackingStoreBuffered, False).autorelease;
  window.center;
  window.setTitle(appName);
  window.makeKeyAndOrderFront(nil);
  NSApp.activateIgnoringOtherApps(true);
  NSApp.run;
end.

Application with a simple window and menus

program simplewindow;

{$mode objfpc}{$H+}
{$modeswitch objectivec2}

uses
  CocoaAll;

var
  menubar: NSMenu;
  appMenu: NSMenu;
  appName: NSString;
  quitMenuItem: NSMenuItem;
  appMenuItem: NSMenuItem;
  quitTitle: NSString;
  window: NSWindow;
  pool: NSAutoreleasePool;
begin
  pool := NSAutoreleasePool.new;
  NSApp := NSApplication.sharedApplication;
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
  menubar := NSMenu.alloc.autorelease;
  appMenuItem := NSMenuItem.alloc.autorelease;
  menubar.addItem(appMenuItem);
  NSApp.setMainMenu(Menubar);
  appMenu := NSMenu.new.autorelease;
  appName := NSProcessInfo.processInfo.processName;
  quitTitle := NSSTR('Quit ').stringByAppendingString(appName);
  quitMenuItem := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(quitTitle,
  sel_registerName(PChar('terminate:')),
  NSSTR('q'));
  appMenu.addItem(quitMenuItem);
  appMenuitem.setSubmenu(appMenu);
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
    NSTitledWindowMask, NSBackingStoreBuffered, False).autorelease;
  window.center;
  window.setTitle(appName);
  window.makeKeyAndOrderFront(nil);
  NSApp.activateIgnoringOtherApps(true);
  NSApp.run;
end.

Simple window and a non-editable text field inside it

This shows how to create and position a sub-control in a window:

program simplewindow;

{$mode objfpc}{$H+}
{$modeswitch objectivec2}

uses
  CocoaAll;

var
  appName: NSString;
  window: NSWindow;
  pool: NSAutoreleasePool;
  lText: NSTextField;
begin
  // Autorelease pool, app and window creation
  pool := NSAutoreleasePool.new;
  NSApp := NSApplication.sharedApplication;
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
  appName := NSProcessInfo.processInfo.processName;
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
    NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,
    NSBackingStoreBuffered, False).autorelease;

  // text label
  lText := NSTextField.alloc.initWithFrame(NSMakeRect(50, 50, 200, 50)).autorelease;
  lText.setBezeled(False);
  lText.setDrawsBackground(False);
  lText.setEditable(False);
  lText.setSelectable(False);
  lText.setStringValue(NSSTR('NSTextField'));
  window.contentView.addSubview(lText);

  // Window showing and app running
  window.center;
  window.setTitle(appName);
  window.makeKeyAndOrderFront(nil);
  NSApp.activateIgnoringOtherApps(true);
  NSApp.run;
end.

Button which handles an event

This event shows how to make a button that responds to clicking on it.

program simplewindow;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}

uses
  CocoaAll;

type
  { TMyDelegate }
  TMyDelegate = objcclass(NSObject)
  public
    procedure HandleButtonClick(sender: id); message 'HandleButtonClick:';
  end;

var
  appName: NSString;
  window: NSWindow;
  pool: NSAutoreleasePool;
  lText: NSTextField;
  lButton: NSButton;
  lDelegate: TMyDelegate;

procedure TMyDelegate.HandleButtonClick(sender: id);
begin
  lText.setStringValue(NSSTR('New text!'));
end;

begin
  // Autorelease pool, app and window creation
  pool := NSAutoreleasePool.new;
  NSApp := NSApplication.sharedApplication;
  NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
  appName := NSProcessInfo.processInfo.processName;
  window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),
    NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,
    NSBackingStoreBuffered, False).autorelease;

  // text label
  lText := NSTextField.alloc.initWithFrame(NSMakeRect(50, 50, 120, 50)).autorelease;
  lText.setBezeled(False);
  lText.setDrawsBackground(False);
  lText.setEditable(False);
  lText.setSelectable(False);
  lText.setStringValue(NSSTR('NSTextField'));
  window.contentView.addSubview(lText);

  // button
  lButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 100, 120, 50)).autorelease;
  window.contentView.addSubview(lButton);
  lButton.setTitle(NSSTR('Button title!'));
  lButton.setButtonType(NSMomentaryLightButton);
  lButton.setBezelStyle(NSRoundedBezelStyle);

  // button event handler setting
  lDelegate := TMyDelegate.alloc.init;
  lButton.setTarget(lDelegate);
  lButton.setAction(ObjCSelector(lDelegate.HandleButtonClick));

  // Window showing and app running
  window.center;
  window.setTitle(appName);
  window.makeKeyAndOrderFront(nil);
  NSApp.activateIgnoringOtherApps(true);
  NSApp.run;
end.