Difference between revisions of "Cocoa Interface"

From Free Pascal wiki
(External links: New section)
 
(39 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Other Interfaces}}
+
{{Cocoa Interface}}
==Cocoa bindings==
+
{{Platform only|macOS|macOS|macOS}}
When this interface was started it used a set of wrappers to access Cocoa, called [[PasCocoa]]. After this native support in Free Pascal for direct communication with Objective-C was added, thought the [[Objective Pascal]] dialect.  
+
 
==Compiling the Cocoa Interfaces==
+
== Cocoa bindings ==
 +
 
 +
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 ==
 +
 
 +
For macOS 10.14 Mojave and later you should be installing FPC 3.2.0 available from the official [https://sourceforge.net/projects/lazarus/files/ Lazarus IDE file area].
 +
 
 +
=== FPC 3.2.0 for macOS 10.10 and earlier ===
 +
 
 +
The precompiled CocoaAll header comes with an instruction to link the CoreImage framework. The framework was introduced in macOS 10.11 and later. (The CoreImage APIs were available since 10.4, BUT there was a stand-alone CoreImage framework. They were all part of QuartzCore framework).
 +
Compiling any unit using CocoaAll will likely cause the linking stage to fail on macOS 10.10 and earlier, as the framework doesn't exist.
 +
 
 +
In order to fix that one might want to update headers and recompile them from the sources.
 +
 
 +
1. Get the FPC sources (you might have them already installed with Lazarus, as it's a prerequisite for the IDE).
 +
 
 +
2. Go to %sourcesdir%/packages/cocoaint/src
 +
 
 +
3. Edit CocoaAll.pas and comment out or remove The CoreImage linking line:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
unit CocoaAll;
 +
interface
 +
 +
{$linkframework Cocoa}
 +
{$linkframework Foundation}
 +
//{$linkframework CoreImage} comment out this line
 +
{$linkframework QuartzCore}
 +
{$linkframework CoreData}
 +
{$linkframework AppKit}
 +
</syntaxhighlight>
 +
 
 +
4. Headers now needs to be recompiled.
 +
 
 +
5. Go back to the root directory of the sources.
 +
 
 +
6. Compile the RTL packages.
 +
make rtl
 +
This should compile a 64-bit version of the RTL. The RTL is needed to compile packages in the next step.
 +
 
 +
7. Compile the packages:
 +
make packages
 +
This should compile A 64-bit version of the FPC packages.
 +
 
 +
8. Go to %sourcesdir%/packages/cocoaint
 +
 
 +
9. Run the command to install the package (to the default system location). Do not forget to add OPT=" if you're on 10.7 or earlier:
 +
sudo make install
 +
 
 +
=== 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, you either:
 +
 
 +
* set the CPU target explicitly to i386. (FPC 3.2.0 compiles to x86_64 for the Darwin target by default. This is done due Apple having dropped support for 32-bit target in macOS 10.15 Catalina released in October 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 Debugging ===
 +
 
 +
See the [[Cocoa_Debugging|Cocoa Debugging]] article.
  
If you want to try Cocoa widgetset, or use some Cocoa features from Carbon application, you'll need to install PasCocoa library first.
+
== Cocoa FAQ ==
  
'''1 - Download PasCocoa from subversion'''
+
=== DPI and scaling issues ===
  
Instructions here: [[PasCocoa#Subversion]]
+
See the [[Cocoa_DPI|Cocoa DPI]] article.
  
'''2- Add the path to PasCocoa to your fpc.cfg file'''
+
=== TButton looks too small! ===
  
In a standard installation this file will be located in: /etc/fpc.cfg
+
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].  
  
Please add at the end of this file the following (remember to adapt it to the paths in your system!):
+
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:
  
<pre>
+
* AutoSize=True
# Search path for Cocoa bindings
+
* Constrains.MinWidth = Constrains.MaxWidth = your desired width.
-Fu/Users/felipe/Programas/lazarus-ccr/bindings/objc
 
-Fu/Users/felipe/Programas/lazarus-ccr/bindings/pascocoa/appkit
 
-Fu/Users/felipe/Programas/lazarus-ccr/bindings/pascocoa/foundation
 
-Fu/Users/felipe/Programas/lazarus-ccr/bindings/pascocoa/applicationservices/coregraphics
 
</pre>
 
  
Since the file is write protected, only administrator accounts will be able to edit it. It is also possible to have a local fpc.cfg file. For more information google or ask in the mailling list.
+
=== Overlapping Widgets ===
  
'''3 - Compile a test PasCocoa project'''
+
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].
  
In Lazarus, open /Users/felipe/Programas/lazarus-ccr/bindings/pascocoa/example/statusitem/statusitem.lpi (or similar acording to your local paths) and build the project.
+
== Roadmap ==
  
This should guarantee that PasCocoa was installed correctly and create the .o files for it.
+
[[Roadmap|Roadmap: Status of Features in each Widget set]]
  
'''4 - Compile LCL-Cocoa'''
+
== See also ==
  
In Lazarus go to the Tools-->Configure Build Lazarus menu. Go to advanced, set everything to NONE and only LCL to clean+build, set the widgetset to Cocoa and push the Build button.
+
* [[Cocoa Internals]]
  
You can now build LCL-Cocoa applications by setting the target widgetset to cocoa in the Compiler Options dialog of your projects.
+
== External links ==
  
==Roadmap==
+
* [https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/Introduction/Introduction.html Apple: Cocoa Event Handling]
 +
* [https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Button/Button.html Apple: Button Programming Topics]
 +
* [https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html Apple: Cocoa Bindings Programming Topics]
 +
* [https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Introduction/Introduction.html Apple: Cocoa Text Architecture Guide]
  
Located here: [[Roadmap#Status_of_features_on_each_widgetset]]
+
{{Other Interfaces}}

Latest revision as of 02:39, 24 February 2021

English (en)

macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

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

For macOS 10.14 Mojave and later you should be installing FPC 3.2.0 available from the official Lazarus IDE file area.

FPC 3.2.0 for macOS 10.10 and earlier

The precompiled CocoaAll header comes with an instruction to link the CoreImage framework. The framework was introduced in macOS 10.11 and later. (The CoreImage APIs were available since 10.4, BUT there was a stand-alone CoreImage framework. They were all part of QuartzCore framework). Compiling any unit using CocoaAll will likely cause the linking stage to fail on macOS 10.10 and earlier, as the framework doesn't exist.

In order to fix that one might want to update headers and recompile them from the sources.

1. Get the FPC sources (you might have them already installed with Lazarus, as it's a prerequisite for the IDE).

2. Go to %sourcesdir%/packages/cocoaint/src

3. Edit CocoaAll.pas and comment out or remove The CoreImage linking line:

 unit CocoaAll;
 interface
 
 {$linkframework Cocoa}
 {$linkframework Foundation}
 //{$linkframework CoreImage} comment out this line
 {$linkframework QuartzCore}
 {$linkframework CoreData}
 {$linkframework AppKit}

4. Headers now needs to be recompiled.

5. Go back to the root directory of the sources.

6. Compile the RTL packages.

make rtl

This should compile a 64-bit version of the RTL. The RTL is needed to compile packages in the next step.

7. Compile the packages:

make packages

This should compile A 64-bit version of the FPC packages.

8. Go to %sourcesdir%/packages/cocoaint

9. Run the command to install the package (to the default system location). Do not forget to add OPT=" if you're on 10.7 or earlier:

sudo make install

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, you either:

  • set the CPU target explicitly to i386. (FPC 3.2.0 compiles to x86_64 for the Darwin target by default. This is done due Apple having dropped support for 32-bit target in macOS 10.15 Catalina released in October 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 Debugging

See the Cocoa Debugging article.

Cocoa FAQ

DPI and scaling issues

See the Cocoa DPI article.

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

See also

External links

Other Interfaces

Platform specific Tips

Interface Development Articles