[PATCH] Fix: TListBox.ItemHeight does not work on Mac / Carbon
Original Reporter info from Mantis: s.kopf
-
Reporter name: Stefan Kopf
Original Reporter info from Mantis: s.kopf
- Reporter name: Stefan Kopf
Description:
The ItemHeight property of TListBox does not work consistently on the Mac. There
has even been a change request to update the documentation that ItemHeight is
not supported on the Mac (http://bugs.freepascal.org/view.php?id=27799).
The attached fix solves this problem.
Please
- Accept this patch
- Revert documentation changes of ID 27799 accepted in r51847.
Abstract
When setting the Font of a Carbon x, the ItemHeight is automatically set to the
Font height. So we need to always set the ItemHeights after setting the Font.
Current problem
Setting the ItemHeight does only work on the initial creation of the TListBox
on the Mac, for instance by setting it in the designer. But when trying to set
the ItemHeight at runtime, then the ItemHeight is automatically set to the
height of the Font instead of the given value.
Problem analysis
Further analysis showed that the ItemHeight is no longer respected when the
widget is re-created due to property updates.
When updating a property, the widget is typically re-created by a call to
RecreateWnd (/lcl/controls.pp). This procedure invokes DestroyHandle which
invokes DestroyWnd. This procedure sets the wcfColorChanged and wcfFontChanged
flags in FWinControlFlags. After destroying the handle, RecreateWnd initiates
the creation again. TWinControl.CreateWnd creates the FHandle again, which
ulimately invokes TCarbonListBox.CreateWidget. This procedure sets up the
ItemHeight properly.
TWinControl.CreateWnd invokes InitializeWnd afterwards, which checks for the
wcfFontChanged flag in FWinControlFlags and sets the Font on the control. This
re-sets the ItemHeight to the Font height again and the value set up by
TCarbonListBox.CreateWidget is ignored.
The fix
- Restrict setting the ItemHeight to owner drawn lists with a fixed line height
- Whenever the font is set, the ItemHeight is set again afterwards
Steps to reproduce:
- Add a TListBox to a Form in the designer
- Add some lines to the ListBox
- Set style to lbOwnerDrawnFixed
- Set an ItemHeight of 25 px
--> note: the designer will not show this
- Add a button with this code "ListBox1.ItemHeight := 30;"
- Run it
Observation:
In contrast to the designer UI, the ListBox actually has an ItemHeight of 25px
- Click the button
Expected:
The lines in the ListBox expand to the new ItemHeight of 30 px
However:
The lines shrink to the text height of 19px