Carbon interface internals: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## Introduction | |
| 2 | 2 | ||
| 3 | 3 | > **Warning:** With the release of macOS 10.15 Catalina in October 2019, Apple has removed all support for the 32 bit Carbon framework from the operating system in favour of the [[Cocoa Internals|64 bit Cocoa framework]]. Consequently, the Carbon widgetset is no longer being developed. | |
| 4 | 4 | ||
| 5 | 5 | This page gives an overview of the LCL Carbon interface for macOS and will help new developers. | |
| 6 | 6 | ||
| 7 | 7 | For installation and creating a first Carbon application refer to [[Carbon Interface]]. | |
| 8 | 8 | ||
| 9 | 9 | ## Documentation about Carbon | |
| 10 | 10 | ||
| 11 | 11 | - [Apple Carbon docs](http://developer.apple.com/documentation/Carbon) | |
| 12 | 12 | ||
| 13 | 13 | - [Apple Mailing List search](http://search.lists.apple.com) | |
| 14 | 14 | ||
| 15 | 15 | - [Carbon Book for download](http://www.mactech.com/macintosh-c/downloads.html) | |
| 16 | 16 | ||
| 17 | 17 | - [Carbon Dev](http://www.carbondev.com/site/) | |
| 18 | 18 | ||
| 19 | 19 | - ["Learning Carbon" sample book chapter](http://www.oreilly.com/catalog/learncarbon) | |
| 20 | 20 | ||
| 21 | 21 | - Free Pascal's Carbon API unit is FPCMacOSAll.pas in /usr/local/share/fpcsrc/packages/extra/univint. | |
| 22 | 22 | ||
| 23 | 23 | ## Carbon interface development basics | |
| 24 | 24 | ||
| 25 | 25 | - Target Mac Tiger version is 10.4 | |
| 26 | 26 | - Avoid using obsolete or deprecated APIs and functions (e. g. QuickDraw vs. *Quartz 2D*) | |
| 27 | 27 | - Use object approach | |
| 28 | 28 | - Check Carbon calls result with OSError function | |
| 29 | 29 | - Assume UTF-8 encoded strings between LCL and Carbon widgetset (for future Lazarus Unicode support) | |
| 30 | 30 | ||
| 31 | 31 | ## What is already working ? | |
| 32 | 32 | ||
| 33 | 33 | - Features - see [[Roadmap|Roadmap#Status_of_features_on_each_widgetset]] | |
| 34 | 34 | - Forms and controls - see [[Roadmap|Roadmap#Status_of_components_on_each_widgetset]] | |
| 35 | 35 | - Creating TOpenGLControl with AGL context (see components/opengl/) | |
| 36 | 36 | - Mouse events | |
| 37 | 37 | - Keyboard events | |
| 38 | 38 | ||
| 39 | - | ||
| 40 | 39 | <img src="Carbon_interface_show_case.png" title="Working components in the Carbon interface" width="640" /> | |
| 41 | - | ||
| 42 | - | ||
| 43 | 40 | ||
| 44 | 41 | ## What needs to be done next? | |
| 45 | 42 | ||
| 46 | 43 | See [Bug Tracker Carbon open issues](http://www.freepascal.org/mantis/view_all_set.php?type=3&source_query_id=1017) | |
| 47 | 44 | ||
| 48 | 45 | ## Carbon IDE Bugs | |
| 49 | 46 | ||
| 50 | - | ||
| 51 | 47 | <thead> | |
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 48 | + | ||
| 58 | 49 | </thead> | |
| 59 | 50 | <tbody> | |
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 51 | + | ||
| 87 | 52 | File New should be Cmd+N<br /> | |
| 88 | 53 | File Save As should be Shift+Cmd+S<br /> | |
| 89 | 54 | File Quit should be Cmd+Q<br /> | |
| 90 | 55 | Search Find should be Cmd+F<br /> | |
| 91 | 56 | Search Find Next should be Cmd+G<br /> | |
| 92 | 57 | Search Find Previous should be Shift+Cmd+G<br /> | |
| 93 | 58 | (Goto line and Procedure List need to be reassigned)<br /> | |
| 94 | 59 | Option+Cmd+F does not bring up Search Results<br /> | |
| 95 | 60 | F11 behaves weird!</td> | |
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 61 | + | ||
| 99 | 62 | </tbody> | |
| 100 | - | ||
| 101 | 63 | ||
| 102 | 64 | See [Bug Tracker Carbon IDE open issues](http://www.freepascal.org/mantis/view_all_set.php?type=3&source_query_id=1018) | |
| 103 | 65 | ||
| 104 | 66 | ## Implementation Details | |
| 105 | 67 | ||
| 106 | 68 | ### Keyboard | |
| 107 | 69 | ||
| 108 | 70 | - Apple Command key is mapped to ssMeta | |
| 109 | 71 | - Apple Control key is mapped to ssCtrl | |
| 110 | 72 | - Apple Option key is mapped to its inscription, i.e. ssAlt | |
| 111 | 73 | - Virtual key codes mapping is not reliable (depends on keyboard language layout!) | |
| 112 | 74 | ||
| 113 | 75 | ### Application | |
| 114 | 76 | ||
| 115 | 77 | - Title: You cannot change it at runtime. You have to set it in Application Bundle. | |
| 116 | 78 | - OnDropFiles event is fired when file is dropped on application dock icon or opened via Finder if is associated. You have to enable this event in Application Bundle. | |
| 117 | 79 | ||
| 118 | 80 | ### Drawing and measuring text precisely in parts | |
| 119 | 81 | ||
| 120 | 82 | If you want to draw (via TextOut, TextRect) or measure (via TextWidth, TextHeight) text divided into various parts and rely it will be displayed same each time not depending on its division, you have to disable some default typographic features (like kerning, fractional positioning, ...) of Canvas. You can choose one of the following: | |
| 121 | 83 | ||
| 122 | 84 | - CarbonWidgetSet.SetTextFractional(Canvas, False); // from CarbonInt unit | |
| 123 | 85 | - TCarbonCustomControl(CustomControl.Handle).TextFractional := False; // from CarbonPrivate unit | |
| 124 | 86 | - TCarbonDeviceContext(Canvas.Handle)..TextFractional := False; // from CarbonCanvas unit | |
| 125 | 87 | ||
| 126 | 88 | ### Screenshot taking | |
| 127 | 89 | ||
| 128 | 90 | The only possible efficient way of taking a screenshot on macOS is using OpenGL. Apple provides a demonstration function which returns a CGImageRef with the screenshot. This function was converted to Pascal and is available on the glgrab.pas unit on the carbon interface directory. This function requires the Apple-specific parts of the Apple OpenGL headers, and those aren't yet on the FPC Packages, so they were translated and are located on lazarus/lcl/interfaces/carbon/opengl.pas until they are released on a stable Free Pascal. | |
| 129 | 91 | ||
| 130 | 92 | After obtaining a CGImageRef, the next step to implement the RawImage_fromDevice method is obtaining a local copy of it's pixel data. It isn't possible to directly access the bytes of a CGImageRef, so the image needs to be drawn to a CGContextRef which uses a memory area allocated by us as buffer. This method has the great advantage of converting from the internal format of the screenshot bytes to the very convenient ARGB, 32-bits depth, 8-bits per channel format that is default to LCL. | |
| 131 | 93 | ||
| 132 | 94 | ### Focusing | |
| 133 | 95 | ||
| 134 | 96 | macOS has two possible ways of focusing: Full keyboard navigation (enabled with Accessibility options), Text field navigation (only controls that are to receive text input should be focused). Full keyboard navigation is the same as any other widgetset. | |
| 135 | 97 | ||
| 136 | 98 | There's no way to say if control is text-input or not. Native OS controls will handle focus themselves. The Carbon Widgetset checks if bound LCLObject is SynEdit and will switch the focus to it. | |
| 137 | 99 | ||
| 138 | 100 | ### Canvas Clipping | |
| 139 | 101 | ||
| 140 | 102 | Because Quartz 2D's clipping is decreasing, care must be taken for proper clipping region change. There should be no code, that changes CGContext's clip region directly. Carbon widget API's functions should be used instead. | |
| 141 | 103 | ||
| 142 | 104 | ## Compatibility issues | |
| 143 | 105 | ||
| 144 | 106 | These are things that will probably never be solved. | |
| 145 | 107 | ||
| 146 | 108 | ### Mouse.CursorPos changing does not generate mouse (move) events | |
| 147 | 109 | ||
| 148 | 110 | ### Release mouse capture is not supported | |
| 149 | 111 | ||
| 150 | 112 | ### Command line parameters | |
| 151 | 113 | ||
| 152 | 114 | Because Carbon applications are executed via Application Bundle, command line parameters are not passed. You have to use OnDropFiles event to detect openning associated files. | |
| 153 | 115 | ||
| 154 | 116 | ### Shortcuts indication is not supported | |
| 155 | 117 | ||
| 156 | 118 | ### Drawing on Canvas outside OnPaint event is not supported | |
| 157 | 119 | ||
| 158 | 120 | ### Drawing on screen device context is not supported | |
| 159 | 121 | ||
| 160 | 122 | ### TCustomControl.Color of clBtnFace makes its background transparent | |
| 161 | 123 | ||
| 162 | 124 | ### TWinControl.Font fsStrikeOut is not supported | |
| 163 | 125 | ||
| 164 | 126 | ### TForm | |
| 165 | 127 | ||
| 166 | 128 | - Icon is not supported | |
| 167 | 129 | - ShowInTaskbar is not supported | |
| 168 | 130 | - TForm.top=0 is not good. Use TForm.top=23 | |
| 169 | 131 | ||
| 170 | 132 | ### TEdit.PasswordChar different then default is not supported | |
| 171 | 133 | ||
| 172 | 134 | ### TMemo.WordWrap when is disabled, does not allow to scroll text horizontally | |
| 173 | 135 | ||
| 174 | 136 | ### TListBox.Columns is not supported | |
| 175 | 137 | ||
| 176 | 138 | ### TComboBox | |
| 177 | 139 | ||
| 178 | 140 | - DroppedDown does not show drop down list when style is csDropDownList | |
| 179 | 141 | - DropDownCount is not supported | |
| 180 | 142 | - Style: csSimple, csOwnerDrawFixed and csOwnerDrawVariable are not supported | |
| 181 | 143 | ||
| 182 | 144 | ### TPanel.Bevelxxx: bvLowered and bvSpace are not supported | |
| 183 | 145 | ||
| 184 | 146 | ### TBitBtn.Spacing is not supported | |
| 185 | 147 | ||
| 186 | 148 | ### TTrackBar | |
| 187 | 149 | ||
| 188 | 150 | - LineSize is not supported | |
| 189 | 151 | - ScalePos is not supported | |
| 190 | 152 | - TickMarks are not supported | |
| 191 | 153 | ||
| 192 | 154 | ### TProgressBar | |
| 193 | 155 | ||
| 194 | 156 | - BarShowText is not supported | |
| 195 | 157 | - Smooth is not supported | |
| 196 | 158 | - Step is not supported | |
| 197 | 159 | ||
| 198 | 160 | ### TColorDialog.Title is not supported | |
| 199 | 161 | ||
| 200 | 162 | ### macOS System (Windows' and Controls') Handles | |
| 201 | 163 | ||
| 202 | 164 | It’s some times necessary to access system windows or control handles directly. For example, you wish to use some system features that are not available (not yet implemented?) for LCL. It’s very common for LCL Windows developers (and Delphi developers) to use TControl.Handle property, since Handle is a system window handle for Win32 widgetset. It can be used with any system function. But it’s not so for Carbon widgetset. TControl.Handle would return handle native to widgetset not macOS. TControl.Handle would return TCarbonWidget class (declared at CarbonDef unit). This’s a wrapper class, and you can use it to get system handle. | |
| 203 | 165 | ||
| 204 | 166 | ``` pascal | |
| 205 | 167 | var | |
| 206 | 168 | AHiView: HiViewRef; | |
| 207 | 169 | begin | |
| 208 | 170 | ... | |
| 209 | 171 | //this is incorrect way of getting system handle for Carbon widgetset | |
| 210 | 172 | //AHiView:= HiViewRef(Button1.Handle) | |
| 211 | 173 | ||
| 212 | 174 | //this is correct way | |
| 213 | 175 | AHiView := TCarbonWidget(Button1.Handle).Widget; | |
| 214 | 176 | ... | |
| 215 | 177 | end; | |
| 216 | 178 | ``` | |
| 217 | 179 | ||
| 218 | 180 | You should also note, that there’s difference in getting WindowRef in LCL versions. If you’re using Lazarus 0.9.26 or earlier you should get WindowRef in the following way: | |
| 219 | 181 | ||
| 220 | 182 | ``` pascal | |
| 221 | 183 | var | |
| 222 | 184 | MacWin: WindowRef; | |
| 223 | 185 | begin | |
| 224 | 186 | ... | |
| 225 | 187 | MacWin := TCarbonWidget(Form1.Handle).Widget; | |
| 226 | 188 | ... | |
| 227 | 189 | end; | |
| 228 | 190 | ``` | |
| 229 | 191 | ||
| 230 | 192 | If you’re using svn LCL version (latest trunk), then you should use the following way: | |
| 231 | 193 | ||
| 232 | 194 | ``` pascal | |
| 233 | 195 | uses | |
| 234 | 196 | ..CarbonPrivate.. | |
| 235 | 197 | var | |
| 236 | 198 | MacWin: WindowRef; | |
| 237 | 199 | begin | |
| 238 | 200 | ... | |
| 239 | 201 | MacWin := TCarbonWindow(Form1.Handle).Window; | |
| 240 | 202 | ... | |
| 241 | 203 | end; | |
| 242 | 204 | ``` | |
| 243 | 205 | ||
| 244 | 206 | Keep in mind, that LCL is designed to be cross-platform library, and accessing system handles to use with system functions makes you program hardly portable. The way of getting system objects handle might also be changed in future. | |
| 245 | 207 | ||
| 246 | 208 | ### TTrayIcon | |
| 247 | 209 | ||
| 248 | 210 | #### Checking if the TTrayIcon menu is visible | |
| 249 | 211 | ||
| 250 | 212 | Unfortunately Apple only added a facility to check menu visibility in Snow Leopard 10.6. To overcome this limitation in a simple way in previous versions one can use the following code, which should work in previous versions using a class method in TCarbonWSCustomTrayIcon: | |
| 251 | 213 | ||
| 252 | 214 | ``` pascal | |
| 253 | 215 | uses | |
| 254 | 216 | CarbonWSExtCtrls; | |
| 255 | 217 | ||
| 256 | 218 | ... | |
| 257 | 219 | ||
| 258 | 220 | begin | |
| 259 | 221 | if TCarbonWSCustomTrayIcon.IsTrayIconMenuVisible(TrayIcon1) then Caption := 'Visible' | |
| 260 | 222 | else Caption := 'Not visible'; | |
| 261 | 223 | end; | |
| 262 | 224 | ``` | |
| 263 | 225 | ||
| 264 | 226 | ## How to add a new control | |
| 265 | 227 | ||
| 266 | 228 | For example TButton. | |
| 267 | 229 | ||
| 268 | 230 | TButton is defined in lcl/buttons.pp. This is the platform independent part of the LCL, which is used by the normal LCL programmer. | |
| 269 | 231 | ||
| 270 | 232 | Its widgetset class is in lcl/widgetset/wsbuttons.pp. This is the platform independent base for all widgetsets (carbon, gtk, win32, ...). | |
| 271 | 233 | ||
| 272 | 234 | Its Carbon interface class is in lcl/interfaces/carbon/carbonwsbuttons.pp: | |
| 273 | 235 | ||
| 274 | 236 | ``` pascal | |
| 275 | 237 | TCarbonWSButton = class(TWSButton) | |
| 276 | 238 | private | |
| 277 | 239 | protected | |
| 278 | 240 | public | |
| 279 | 241 | class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override; | |
| 280 | 242 | end; | |
| 281 | 243 | ``` | |
| 282 | 244 | ||
| 283 | 245 | Every WS class that actually implements something must be registered. See the initialization section at the end of the carbonwsXXX.pp unit: | |
| 284 | 246 | ||
| 285 | 247 | ``` pascal | |
| 286 | 248 | RegisterWSComponent(TCustomButton, TCarbonWSButton); | |
| 287 | 249 | ``` | |
| 288 | 250 | ||
| 289 | 251 | TCarbonWSButton overrides CreateHandle to create a Carbon button helper class called TCarbonButton in carbonprivate.pp, which really creates the button control in the Carbon and installs event handlers. | |
| 290 | 252 | ||
| 291 | 253 | --- | |
| 292 | 254 | *From the Free Pascal wiki, page "Carbon interface internals", as it stood on 2021-08-04 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).* |
