Cocoa Interface: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | *(macOS only)* | |
| 2 | 2 | ||
| 3 | 3 | ## Cocoa bindings | |
| 4 | 4 | ||
| 5 | 5 | 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. | |
| 6 | 6 | ||
| 7 | 7 | ## Compiling | |
| 8 | 8 | ||
| 9 | 9 | For macOS 10.14 Mojave and later you should be installing FPC 3.2.0 available from the official [Lazarus IDE file area](https://sourceforge.net/projects/lazarus/files/). | |
| 10 | 10 | ||
| 11 | 11 | ### FPC 3.2.0 for macOS 10.10 and earlier | |
| 12 | 12 | ||
| 13 | 13 | 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. | |
| 14 | 14 | ||
| 15 | 15 | In order to fix that one might want to update headers and recompile them from the sources. | |
| 16 | 16 | ||
| 17 | 17 | 1\. Get the FPC sources (you might have them already installed with Lazarus, as it's a prerequisite for the IDE). | |
| 18 | 18 | ||
| 19 | 19 | 2\. Go to %sourcesdir%/packages/cocoaint/src | |
| 20 | 20 | ||
| 21 | 21 | 3\. Edit CocoaAll.pas and comment out or remove The CoreImage linking line: | |
| 22 | 22 | ||
| 23 | 23 | ``` pascal | |
| 24 | 24 | unit CocoaAll; | |
| 25 | 25 | interface | |
| 26 | 26 | ||
| 27 | 27 | {$linkframework Cocoa} | |
| 28 | 28 | {$linkframework Foundation} | |
| 29 | 29 | //{$linkframework CoreImage} comment out this line | |
| 30 | 30 | {$linkframework QuartzCore} | |
| 31 | 31 | {$linkframework CoreData} | |
| 32 | 32 | {$linkframework AppKit} | |
| 33 | 33 | ``` | |
| 34 | 34 | ||
| 35 | 35 | 4\. Headers now needs to be recompiled. | |
| 36 | 36 | ||
| 37 | 37 | 5\. Go back to the root directory of the sources. | |
| 38 | 38 | ||
| 39 | 39 | 6\. Compile the RTL packages. | |
| 40 | 40 | ||
| 41 | - | ||
| 41 | + | ``` | |
| 42 | + | make rtl | |
| 43 | + | ``` | |
| 42 | 44 | ||
| 43 | 45 | This should compile a 64-bit version of the RTL. The RTL is needed to compile packages in the next step. | |
| 44 | 46 | ||
| 45 | 47 | 7\. Compile the packages: | |
| 46 | 48 | ||
| 47 | - | ||
| 49 | + | ``` | |
| 50 | + | make packages | |
| 51 | + | ``` | |
| 48 | 52 | ||
| 49 | 53 | This should compile A 64-bit version of the FPC packages. | |
| 50 | 54 | ||
| 51 | 55 | 8\. Go to %sourcesdir%/packages/cocoaint | |
| 52 | 56 | ||
| 53 | 57 | 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: | |
| 54 | 58 | ||
| 55 | - | ||
| 59 | + | ``` | |
| 60 | + | sudo make install | |
| 61 | + | ``` | |
| 56 | 62 | ||
| 57 | 63 | ### Prepare your Lazarus project for using Cocoa | |
| 58 | 64 | ||
| 59 | 65 | You may need to set the Target to the 64bit processor and select the Cocoa Widget set: | |
| 60 | 66 | ||
| 61 | 67 | - Open your project with Lazarus and click Project/Project Options | |
| 62 | 68 | - In the "Config and Target" panel set the "Target CPU family" to be "x86_64" | |
| 63 | 69 | - In the "Additions and Overrides" panel click on "Set LCLWidgetType" pulldown and set the value to "Cocoa" | |
| 64 | 70 | - 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. | |
| 65 | 71 | - Now compile your project - with any luck it will work OK. | |
| 66 | 72 | ||
| 67 | 73 | ### Error ATSUFindFontFromName | |
| 68 | 74 | ||
| 69 | 75 | If you're getting the error: | |
| 70 | 76 | ||
| 71 | - | ||
| 77 | + | ``` | |
| 78 | + | carbonproc.pp(563,13) Error: Identifier not found "ATSUFindFontFromName" | |
| 79 | + | ``` | |
| 72 | 80 | ||
| 73 | 81 | when compiling a project for macOS using FPC, you either: | |
| 74 | 82 | ||
| 75 | 83 | - 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.) | |
| 76 | 84 | ||
| 77 | 85 | :*by setting the target in Project options (switching it from default to i386) | |
| 78 | 86 | ||
| 79 | 87 | :*by setting the *CPU_TARGET=i386* parameter for the make command if compiling from the command line. | |
| 80 | 88 | ||
| 81 | 89 | - or set the LCL target (widgetset) to "Cocoa". | |
| 82 | 90 | ||
| 83 | 91 | ### Cocoa Debugging | |
| 84 | 92 | ||
| 85 | 93 | See the [[Cocoa Debugging]] article. | |
| 86 | 94 | ||
| 87 | 95 | ## Cocoa FAQ | |
| 88 | 96 | ||
| 89 | 97 | ### DPI and scaling issues | |
| 90 | 98 | ||
| 91 | 99 | See the [[Cocoa DPI]] article. | |
| 92 | 100 | ||
| 93 | 101 | ### TButton looks too small! | |
| 94 | 102 | ||
| 95 | 103 | 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](https://bugs.freepascal.org/view.php?id=31185). | |
| 96 | 104 | ||
| 97 | 105 | 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: | |
| 98 | 106 | ||
| 99 | 107 | - AutoSize=True | |
| 100 | 108 | - Constrains.MinWidth = Constrains.MaxWidth = your desired width. | |
| 101 | 109 | ||
| 102 | 110 | ### Overlapping Widgets | |
| 103 | 111 | ||
| 104 | 112 | 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](https://bugs.freepascal.org/view.php?id=32991). | |
| 105 | 113 | ||
| 106 | 114 | ## Roadmap | |
| 107 | 115 | ||
| 108 | 116 | [[Roadmap|Roadmap: Status of Features in each Widget set]] | |
| 109 | 117 | ||
| 110 | 118 | ## See also | |
| 111 | 119 | ||
| 112 | 120 | - [[Cocoa Internals]] | |
| 113 | 121 | ||
| 114 | 122 | ## External links | |
| 115 | 123 | ||
| 116 | 124 | - [Apple: Cocoa Event Handling](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/Introduction/Introduction.html) | |
| 117 | 125 | - [Apple: Button Programming Topics](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Button/Button.html) | |
| 118 | 126 | - [Apple: Cocoa Bindings Programming Topics](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html) | |
| 119 | 127 | - [Apple: Cocoa Text Architecture Guide](https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Introduction/Introduction.html) | |
| 120 | 128 | ||
| 121 | 129 | --- | |
| 122 | 130 | *From the Free Pascal wiki, page "Cocoa Interface", as it stood on 2021-02-24 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
