Recent

Author Topic: How to compute TComboBox.Width to fit for a certain text?  (Read 4870 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1131
Re: How to compute TComboBox.Width to fit for a certain text?
« Reply #15 on: July 23, 2020, 10:57:13 am »
I don't think there is a reliable cross-platform solution that covers all possible variations of font name and font size.
I would had no problems to use 2 complete different solutions, one for Windows and one for Linux. And it would be enough, if it worked for "a couple of common fonts" (like I used in my  examples) in "normal" sizes. But I learned, that this is difficult...

Quote
How often do you actually need to change the combobox font from its Parent's value? I would just go for a routine that gives reliable results for a chosen font specification, and then leave the font unchanged.
On Windows I never had the idea to change the Default Fonts, because I was always satisfied with them. But on Linux I'm absolutely not satisfied with them. So there in many cases I changed / will change Fonts or their sizes to test them for a while, if I like this more or less. And just now I'm writing a new program with a couple of ComboBoxes and it would have been smart, if I had a function to compute the needed width automatically. But it's not worth to spend too much effort for this - I had hoped that it would be easier...

Quote
The point of the MulDiv line was to adapt the code for any possible monitor that might run the code. For any one given monitor (e. g. your monitor), obviously its PixelsPerInch value is a constant, and in your case the calculated "width" does not need scaling, as it happens.
Yes, I only replaced the MulDiv line temporarily with a const to make testing easier.

Windows seems to work when a size-dependent margin is added (Combobox.Canvas.TextWidth('M')) in addition to a small constant offset (2 pixels, which of course must be scaled to the screen resolution: ScaleX(2, 96) where 96 is the unscaled resolution ).
I made a test on Windows 7 and added ACombobox.Canvas.TextWidth('M') plus a const 'MD' additionally to AComboBox.Canvas.TextWidth(AComboBox.Items[]).
For 'Times New Roman' with Height=12 I needed MD=16, for Height=22 I needed MD=10 and for Height=30 I needed MD=6. So unfortunately it does not work.

Quote
Linux qt is almost perfect, a constant margin of 8 pixels (to be scaled) seems to be sufficient
Linux gkt2 is the worst...
But I also guess that when the user changes themes in qt or gtk2 there will be new values again.
...
So, optimizing this is wasted time... Add ample of margin, and focus on something else...
I'm on gtk2. I agree, I give up. It's not worth to spend more effort for this.

Thanks again to both of you for your suggestions and trying to help me. And again I learned a couple of new things :-)

Hartmut

  • Hero Member
  • *****
  • Posts: 1131
Re: How to compute TComboBox.Width to fit for a certain text?
« Reply #16 on: July 23, 2020, 11:06:34 am »
Hello sstvmaster,
in the hope it could work I gave your suggestion a try on Ubuntu 18.04 but the computed Width is always too small. And the amount, how much it is too small, is dependent of the selected Height of the Font (see attached screenshot).

From my understanding your algorithm works exactly the same than wp's algorithm in reply #1 (only with a little detour by a Bitmap, but returns the same results) and has exactly the same generally disadvantage which I already wrote in reply #4:

But we don't need a constant correction offset, we need one, which depends on the selected Font and Font Height.

 

TinyPortal © 2005-2018