Difference between revisions of "Cocoa Interface"

From Lazarus wiki
Jump to navigationJump to search
(Removed iOS only banner - it does not use Cocoa, but the separate and different Cocoa Touch framework; fixed typos; wiki markup; clarified)
Line 1: Line 1:
 
{{Platform only|macOS|macOS|macOS}}
 
{{Platform only|macOS|macOS|macOS}}
{{Platform only|iOS|iOS|iOS}}
 
 
{{Other Interfaces}}
 
{{Other Interfaces}}
==Cocoa bindings==
 
This interface uses the native support in Free Pascal for direct communication with Objective-C was added through the [[FPC PasCocoa|Objective Pascal]] dialect.
 
  
==Compiling==
+
=Cocoa bindings=
===Use FPC3.0.4a===
+
 
For Mojave 10.14 and later you should be installing fpc 3.0.4a available at the official site.
+
The Cocoa interface uses the native support in Free Pascal for direct communication with Objective-C which was added through the [[FPC PasCocoa|Objective Pascal]] dialect.
 +
 
 +
=Compiling=
 +
 
 +
==Use FPC 3.0.4a==
 +
 
 +
For macOS 10.14 Mojave and later you should be installing FPC 3.0.4a available at the official site.
 +
 
 +
==Prepare your Lazarus project for using Cocoa==
  
===Prepare your Lazarus project for using Cocoa===
 
 
You may need to set the Target to the 64bit processor and select the Cocoa Widget set:
 
You may need to set the Target to the 64bit processor and select the Cocoa Widget set:
  
*Open your project with Lazarus and click Project/ProjectOptions
+
*Open your project with Lazarus and click Project/Project Options
 
*In the "Config and Target" panel set the "Target CPU family" to be "x86_64"
 
*In the "Config and Target" panel set the "Target CPU family" to be "x86_64"
 
*In the "Additions and Overrides" panel click on "Set LCLWidgetType" pulldown and set the value to "Cocoa"
 
*In the "Additions and Overrides" panel click on "Set LCLWidgetType" pulldown and set the value to "Cocoa"
Line 18: Line 22:
 
*Now compile your project - with any luck it will work OK.
 
*Now compile your project - with any luck it will work OK.
  
===Error ATSUFindFontFromName===
+
==Error ATSUFindFontFromName==
if you're getting an error
+
 
 +
If you're getting the error:
 +
 
 
  carbonproc.pp(563,13) Error: Identifier not found "ATSUFindFontFromName"
 
  carbonproc.pp(563,13) Error: Identifier not found "ATSUFindFontFromName"
when compiling a project for macOS using FPC 3.0.4 you need:
 
* either set CPU target explicitly to i386. (FPC 3.0.4 compiles to x86_64 for darwin target by default. This is done due Apple stopping support for 32-bit target)
 
:*You either set the target in Project options (by switching it from default to i386)
 
:*or if you compiling from command-line (i.e building an IDE) set up the following parameter to make command
 
CPU_TARGET=i386
 
* either set LCL target (widgetset) to "Cocoa"
 
  
==Cocoa FAQ==
+
when compiling a project for macOS using FPC 3.0.4, you either:
  
===TButton looks too small!===
+
* set the CPU target explicitly to i386. (FPC 3.0.4 compiles to x86_64 for the Darwin target by default. This is done due Apple dropping support for 32-bit target in macOS 10.15 Catalina due for release towards the end of 2019.)
If you design a button in another widgetset with Autosize=Off it might happen that the button looks too small in Cocoa, and a number of people complained about this, such as in these BTS reports: [https://bugs.freepascal.org/view.php?id=31185].  
+
:*by setting the target in Project options (switching it from default to i386)
 +
:*by setting the ''CPU_TARGET=i386'' parameter for the make command if compiling from the command line.
 +
* or set the LCL target (widgetset) to "Cocoa".
  
If you don't care about the button size, just set AutoSize=True. If you want to have a custom width for the button, but wants to allow the LCL to choose the right Height so that the button will look good in Cocoa, then the solution in this case is to set the following properties in the Object Inspector:
+
=Cocoa FAQ=
 +
 
 +
==TButton looks too small!==
 +
 
 +
If you design a button in another widgetset with Autosize=False it might happen that the button looks too small in Cocoa, and a number of people have complained about this, such as in [https://bugs.freepascal.org/view.php?id=31185 this bug report].
 +
 
 +
If you don't care about the button size, just set AutoSize=True. If you want to have a custom width for the button, but want to allow the LCL to choose the right Height so that the button will look good in Cocoa, then the solution in this case is to set the following properties in the Object Inspector:
  
 
* AutoSize=True
 
* AutoSize=True
 
* Constrains.MinWidth = Constrains.MaxWidth = your desired width.
 
* Constrains.MinWidth = Constrains.MaxWidth = your desired width.
  
===Overlapping Widgets===
+
==Overlapping Widgets==
Lazarus allows you to set the depth of different widgets, such that when two widgets overlap, the "closer" object blocks the view of the more "distant" object. You can do this at design time (right-click on object and click "Z-order") or at run time with functions like "BringToFront" and "SendToBack". Be aware that this may not always work with Cocoa. This is a 'feature' of Cocoa, as clipping is optimized for performance. Therefore, if you plan to compile your projects for Cocoa it is a good strategy to avoid overlapping widgets or to place them on different panels to provide explicit control of Z-order. For more details [https://bugs.freepascal.org/view.php?id=32991].
+
 
 +
Lazarus allows you to set the depth of different widgets, such that when two widgets overlap, the "closer" object blocks the view of the more "distant" object. You can do this at design time (right-click on object and click "Z-order") or at run time with functions like "BringToFront" and "SendToBack". Be aware that this may not always work with Cocoa. This is a 'feature' of Cocoa, as clipping is optimized for performance. Therefore, if you plan to compile your projects for Cocoa it is a good strategy to avoid overlapping widgets or to place them on different panels to provide explicit control of Z-order. For more details [https://bugs.freepascal.org/view.php?id=32991 see this bug report].
  
==Roadmap==
+
=Roadmap=
  
Located here: [[Roadmap#Status_of_features_on_each_widgetset]]
+
[[Roadmap|Roadmap: Status of Features in each Widget set]]
  
 
[[Category:Mac OS X]]
 
[[Category:Mac OS X]]
 
[[Category:Cocoa]]
 
[[Category:Cocoa]]

Revision as of 11:23, 14 July 2019

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

Other Interfaces

Platform specific Tips

Interface Development Articles

Cocoa bindings

The Cocoa interface uses the native support in Free Pascal for direct communication with Objective-C which was added through the Objective Pascal dialect.

Compiling

Use FPC 3.0.4a

For macOS 10.14 Mojave and later you should be installing FPC 3.0.4a available at the official site.

Prepare your Lazarus project for using Cocoa

You may need to set the Target to the 64bit processor and select the Cocoa Widget set:

  • Open your project with Lazarus and click Project/Project Options
  • In the "Config and Target" panel set the "Target CPU family" to be "x86_64"
  • In the "Additions and Overrides" panel click on "Set LCLWidgetType" pulldown and set the value to "Cocoa"
  • In the past, for some reason Lazarus kept setting the compiler to "/usr/local/bin/ppc386" - which results in 32 bit apps. Make sure under Tools->Options that "Compiler Executable" is set to "/usr/local/bin/fpc" to get 64 bit apps.
  • Now compile your project - with any luck it will work OK.

Error ATSUFindFontFromName

If you're getting the error:

carbonproc.pp(563,13) Error: Identifier not found "ATSUFindFontFromName"

when compiling a project for macOS using FPC 3.0.4, you either:

  • set the CPU target explicitly to i386. (FPC 3.0.4 compiles to x86_64 for the Darwin target by default. This is done due Apple dropping support for 32-bit target in macOS 10.15 Catalina due for release towards the end of 2019.)
  • by setting the target in Project options (switching it from default to i386)
  • by setting the CPU_TARGET=i386 parameter for the make command if compiling from the command line.
  • or set the LCL target (widgetset) to "Cocoa".

Cocoa FAQ

TButton looks too small!

If you design a button in another widgetset with Autosize=False it might happen that the button looks too small in Cocoa, and a number of people have complained about this, such as in this bug report.

If you don't care about the button size, just set AutoSize=True. If you want to have a custom width for the button, but want to allow the LCL to choose the right Height so that the button will look good in Cocoa, then the solution in this case is to set the following properties in the Object Inspector:

  • AutoSize=True
  • Constrains.MinWidth = Constrains.MaxWidth = your desired width.

Overlapping Widgets

Lazarus allows you to set the depth of different widgets, such that when two widgets overlap, the "closer" object blocks the view of the more "distant" object. You can do this at design time (right-click on object and click "Z-order") or at run time with functions like "BringToFront" and "SendToBack". Be aware that this may not always work with Cocoa. This is a 'feature' of Cocoa, as clipping is optimized for performance. Therefore, if you plan to compile your projects for Cocoa it is a good strategy to avoid overlapping widgets or to place them on different panels to provide explicit control of Z-order. For more details see this bug report.

Roadmap

Roadmap: Status of Features in each Widget set