Sign in Join
Wiki copytemporary

Carbon interface internals: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Introduction
22
33> **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.
44
55This page gives an overview of the LCL Carbon interface for macOS and will help new developers.
66
77For installation and creating a first Carbon application refer to [[Carbon Interface]].
88
99## Documentation about Carbon
1010
1111- [Apple Carbon docs](http://developer.apple.com/documentation/Carbon)
1212
1313- [Apple Mailing List search](http://search.lists.apple.com)
1414
1515- [Carbon Book for download](http://www.mactech.com/macintosh-c/downloads.html)
1616
1717- [Carbon Dev](http://www.carbondev.com/site/)
1818
1919- ["Learning Carbon" sample book chapter](http://www.oreilly.com/catalog/learncarbon)
2020
2121- Free Pascal's Carbon API unit is FPCMacOSAll.pas in /usr/local/share/fpcsrc/packages/extra/univint.
2222
2323## Carbon interface development basics
2424
2525- Target Mac Tiger version is 10.4
2626- Avoid using obsolete or deprecated APIs and functions (e. g. QuickDraw vs. *Quartz 2D*)
2727- Use object approach
2828- Check Carbon calls result with OSError function
2929- Assume UTF-8 encoded strings between LCL and Carbon widgetset (for future Lazarus Unicode support)
3030
3131## What is already working ?
3232
3333- Features - see [[Roadmap|Roadmap#Status_of_features_on_each_widgetset]]
3434- Forms and controls - see [[Roadmap|Roadmap#Status_of_components_on_each_widgetset]]
3535- Creating TOpenGLControl with AGL context (see components/opengl/)
3636- Mouse events
3737- Keyboard events
3838
3939![](/wiki-images/d/d3/Carbon_interface_show_case.png)
4040
4141## What needs to be done next?
4242
4343See [Bug Tracker Carbon open issues](http://www.freepascal.org/mantis/view_all_set.php?type=3&source_query_id=1017)
4444
4545## Carbon IDE Bugs
4646
47-<thead>
48-
49-</thead>
50-<tbody>
51-
52-File New should be Cmd+N<br />
53-File Save As should be Shift+Cmd+S<br />
54-File Quit should be Cmd+Q<br />
55-Search Find should be Cmd+F<br />
56-Search Find Next should be Cmd+G<br />
57-Search Find Previous should be Shift+Cmd+G<br />
58-(Goto line and Procedure List need to be reassigned)<br />
59-Option+Cmd+F does not bring up Search Results<br />
60-F11 behaves weird!</td>
61-
62-</tbody>
47+| Item | Note | Dependencies | Responsible |
48+|---|---|---|---|
49+| '...' buttons in dialogs | Now they are too large I think | | |
50+| OK/Cancel | Many dialogs have non-Mac check-X glyphs | | |
51+| \|Menu bar | Too wide for 1024x768 display resolution | | |
52+| \|Lazarus menu | Help About and app prefs (Environment options) should be accessible here | | |
53+| \|Menus | Many missing or non-standard menu shortcuts: · File New should be Cmd+N · File Save As should be Shift+Cmd+S · File Quit should be Cmd+Q · Search Find should be Cmd+F · Search Find Next should be Cmd+G · Search Find Previous should be Shift+Cmd+G · (Goto line and Procedure List need to be reassigned) · Option+Cmd+F does not bring up Search Results · F11 behaves weird! | \| | |
6354
6455See [Bug Tracker Carbon IDE open issues](http://www.freepascal.org/mantis/view_all_set.php?type=3&source_query_id=1018)
6556
6657## Implementation Details
6758
6859### Keyboard
6960
7061- Apple Command key is mapped to ssMeta
7162- Apple Control key is mapped to ssCtrl
7263- Apple Option key is mapped to its inscription, i.e. ssAlt
7364- Virtual key codes mapping is not reliable (depends on keyboard language layout!)
7465
7566### Application
7667
7768- Title: You cannot change it at runtime. You have to set it in Application Bundle.
7869- 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.
7970
8071### Drawing and measuring text precisely in parts
8172
8273If 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:
8374
8475- CarbonWidgetSet.SetTextFractional(Canvas, False); // from CarbonInt unit
8576- TCarbonCustomControl(CustomControl.Handle).TextFractional := False; // from CarbonPrivate unit
8677- TCarbonDeviceContext(Canvas.Handle)..TextFractional := False; // from CarbonCanvas unit
8778
8879### Screenshot taking
8980
9081The 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.
9182
9283After 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.
9384
9485### Focusing
9586
9687macOS 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.
9788
9889There'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.
9990
10091### Canvas Clipping
10192
10293Because 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.
10394
10495## Compatibility issues
10596
10697These are things that will probably never be solved.
10798
10899### Mouse.CursorPos changing does not generate mouse (move) events
109100
110101### Release mouse capture is not supported
111102
112103### Command line parameters
113104
114105Because Carbon applications are executed via Application Bundle, command line parameters are not passed. You have to use OnDropFiles event to detect openning associated files.
115106
116107### Shortcuts indication is not supported
117108
118109### Drawing on Canvas outside OnPaint event is not supported
119110
120111### Drawing on screen device context is not supported
121112
122113### TCustomControl.Color of clBtnFace makes its background transparent
123114
124115### TWinControl.Font fsStrikeOut is not supported
125116
126117### TForm
127118
128119- Icon is not supported
129120- ShowInTaskbar is not supported
130121- TForm.top=0 is not good. Use TForm.top=23
131122
132123### TEdit.PasswordChar different then default is not supported
133124
134125### TMemo.WordWrap when is disabled, does not allow to scroll text horizontally
135126
136127### TListBox.Columns is not supported
137128
138129### TComboBox
139130
140131- DroppedDown does not show drop down list when style is csDropDownList
141132- DropDownCount is not supported
142133- Style: csSimple, csOwnerDrawFixed and csOwnerDrawVariable are not supported
143134
144135### TPanel.Bevelxxx: bvLowered and bvSpace are not supported
145136
146137### TBitBtn.Spacing is not supported
147138
148139### TTrackBar
149140
150141- LineSize is not supported
151142- ScalePos is not supported
152143- TickMarks are not supported
153144
154145### TProgressBar
155146
156147- BarShowText is not supported
157148- Smooth is not supported
158149- Step is not supported
159150
160151### TColorDialog.Title is not supported
161152
162153### macOS System (Windows' and Controls') Handles
163154
164155It’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.
165156
166157``` pascal
167158var
168159 AHiView: HiViewRef;
169160begin
170161...
171162//this is incorrect way of getting system handle for Carbon widgetset
172163//AHiView:= HiViewRef(Button1.Handle)
173164
174165//this is correct way
175166AHiView := TCarbonWidget(Button1.Handle).Widget;
176167...
177168end;
178169```
179170
180171You 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:
181172
182173``` pascal
183174var
184175 MacWin: WindowRef;
185176begin
186177...
187178MacWin := TCarbonWidget(Form1.Handle).Widget;
188179...
189180end;
190181```
191182
192183If you’re using svn LCL version (latest trunk), then you should use the following way:
193184
194185``` pascal
195186uses
196187 ..CarbonPrivate..
197188var
198189 MacWin: WindowRef;
199190begin
200191...
201192MacWin := TCarbonWindow(Form1.Handle).Window;
202193...
203194end;
204195```
205196
206197Keep 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.
207198
208199### TTrayIcon
209200
210201#### Checking if the TTrayIcon menu is visible
211202
212203Unfortunately 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:
213204
214205``` pascal
215206uses
216207 CarbonWSExtCtrls;
217208
218209...
219210
220211begin
221212 if TCarbonWSCustomTrayIcon.IsTrayIconMenuVisible(TrayIcon1) then Caption := 'Visible'
222213 else Caption := 'Not visible';
223214end;
224215```
225216
226217## How to add a new control
227218
228219For example TButton.
229220
230221TButton is defined in lcl/buttons.pp. This is the platform independent part of the LCL, which is used by the normal LCL programmer.
231222
232223Its widgetset class is in lcl/widgetset/wsbuttons.pp. This is the platform independent base for all widgetsets (carbon, gtk, win32, ...).
233224
234225Its Carbon interface class is in lcl/interfaces/carbon/carbonwsbuttons.pp:
235226
236227``` pascal
237228TCarbonWSButton = class(TWSButton)
238229private
239230protected
240231public
241232 class function CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLIntfHandle; override;
242233end;
243234```
244235
245236Every WS class that actually implements something must be registered. See the initialization section at the end of the carbonwsXXX.pp unit:
246237
247238``` pascal
248239RegisterWSComponent(TCustomButton, TCarbonWSButton);
249240```
250241
251242TCarbonWSButton 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.
252243
253244---
254245*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).*

Back to history