Thanks a lot to howardpc for this suggestion, but unfortunately it does not work on my Ubuntu 18.04 (64-bit) with Lazarus 2.0.6 / FPC 3.0.4. The width is always too big (see attached screenshot for Default-Font and 'DejaVu Sans Mono' both with Height=22).
I played with length of const 'MARGIN' to check, if another length would fit, but:
- MARGIN-length = 8 would fit for Default-Font (Height=22) while
- MARGIN-length = 4 would fit for 'DejaVu Sans Mono' or 'Courier New' (Height=22)
So we would need different MARGIN-lengths for differen Fonts...
Then I modified your function Combobox_Autowidth() to display the result of ThemeServices.GetDetailSize() which
always returns -1, which might mean an error. I tried to find infos about this function, but Lazarus-Online-Help (F1) showed nothing usable and with google in a reasonable time the same, only 2 fixed errors in
https://bugs.freepascal.org/view.php?id=0027381 and
https://bugs.freepascal.org/view.php?id=32437Then I tried on Windows 7 (32-bit) - real Windows, no VM or WINE - with Lazarus 2.0.6 / FPC 3.0.4. Again the width was always too big with original MARGIN-length of 15. Again I played with it's length to check, if another length would fit, but:
- MARGIN-length = 5 would fit for Default-Font (Height=22) while
- MARGIN-length = 6 would fit for 'Times New Roman' (Height=22) while
- MARGIN-length = 2 would fit for 'Courier New' (Height=22).
On Windows too ThemeServices.GetDetailSize() returns -1. I tried also Lazarus 2.1.0 rev=62449 / FPC 3.3.1 rev=43796 but it's the same.
Do you have an idea to avoid this?
I tested on "real" Windows (you probably are on Wine?), and it works correctly, even with different font sizes.
I too used "real" Windows 7 (32-bit).
With 'Courier New' it works correctly, but e.g. with 'Times New Roman' not (please see screenshot CB_Windows.jpg in reply #4).
I also ran Manjaro Linux in a VM (gtk2), and I confirm that my code does not work there. It does not seem to yield the correct result for GetSystemMetrics. Maybe another element of the long list of SM_*** constants is more appropriate, but I did not come to a quick solution.
From my understanding, GetSystemMetrics() does not know the selected Combobox Font and it's size? So I fear, even with another SM_*** constant it cannot work, because we need a correction offset, which depends on the selected Font and Font Height...
Yes, Howard's way of employing the theme services looks very promising; but I see problems here on Cocoa where my sparse experience warns that themeservices do not seem to work at all on macOS.
This wouldn't be a problem, I don't have macOS.
Thanks to both of you for your help.