Lazarus

Programming => LCL => Topic started by: dgrhoads on February 07, 2021, 12:24:09 am

Title: Column widths set by AutoSizeColumns are too narrow
Post by: dgrhoads on February 07, 2021, 12:24:09 am
I find that when I use AutoSizeColumns to specify the widths of all the controls that I routinely use (TMemo, TListBox and TStringList), only a fraction of the characters at the right ends of the longest lines are visible.  I see this happening with every font and type sizes that I have tested.  Am I the only one with this problem?

My solution to this is to manually calculate the width of a column and then to increase the width by 3 to 10%. 

I would appreciate your collective wisdom on this subject.
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: lucamar on February 07, 2021, 05:17:08 am
AFAIK, the only standard control that has an AutoSizeColumns method is TStringGrid so you'll have to explain with a little more care what you mean ... :-\
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: dgrhoads on February 07, 2021, 04:22:21 pm
I am running on Windows 10 (64 bit).  Laz build: 2.0.10

Attached is a little program which will demonstrates the issues for me.  It also allows you to change fonts as well as increasing the widths of the columns up to 125% of the original. 

It turns out that the AutoColumnSizing already increases the calculated amount by a few percent.

So back to the original question-- Do others of you see this issue as well with the default system?

BTW:  Kudos to Jamie, Lucamar and all the other heroes.  You do a great job of monitoring all these posts and responding to the vast majority.  Thank you very much.  Clearly a labor of love.  I learn a lot from them.

BTW2:  Some weeks ago, there was a discussion on how to calculate text widths.  Most of the proposed solutions were pretty complex.  In my code, there is a very simple procedure (GetTextWidth) to deal with that problem.
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: wp on February 07, 2021, 06:03:13 pm
Not sure what you want me to see. I click "LoadGrid" --> the grid is loaded, the columns are at their default widths (64). I click "AutoSize" --> the grid widths increase to 91 and 119, respectively, but nothing is truncated.

It also allows you to change fonts
No effect when I change the font. I see in your code that you change the font of the form, but all controls on the form have ParentFont set to false. What do you want to achieve with that? Why don't you change the font of the grid directly?

[...] as well as increasing the widths of the columns up to 125% of the original. 
It looks as if the changes are much smaller than specified. In your code you calculate the width of the longest colum cell and apply the percent increase to it. But the grid does more: it adds the value of "varCellPadding" before and after the text as margin, and it adds one GridLineWidth; in a default system (at 96ppI) these are 2*3+1 = 7 pixels. Due to this offset I do not change the overall column width with the requested percentage. Let's put this offset into accout with the example of the Digits column. After AutoSize its width is 91. Subtracting the offset by varCellPadding and GridLineWidth, I get 84 pixels text width. Applying a percentag of - say - 20% adds 16 pixels to 100 -- and this is exactly what your label is displaying. So, what is the problem?

It turns out that the AutoColumnSizing already increases the calculated amount by a few percent.
Yes, it increases the pure text width by the varCellPadding and GridLineWidth offset, but this is a constant offset, not a constant percentage!

P.S.
It hurts to see that you literally kill the form by calling Halt. (Call Close instead)

P.P.S
Never use the form name in the code of the class because then the code will stop working when the instance of the class has a different name.
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: dgrhoads on February 08, 2021, 09:13:44 pm
WP:  Thank you for your comments.  Yes, I am a newbie.  Only 17 posts.  Decades ago in the MS-DOS days, I was an avid user of UCSD Pascal and later of Modula-2.  Now I'm learning Lazarus.  Language is similar, but the environment is so very different with dramatically increased richness and complexity.

Quote
No effect when I change the font. I see in your code that you change the font of the form, but all controls on the form have ParentFont set to false. What do you want to achieve with that? Why don't you change the font of the grid directly?

My observation is that I don't have to.  In general, the fonts that I set for the parent just flow through to most of the children.

Quote
It looks as if the changes are much smaller than specified. ... So, what is the problem?

Clearly I can calculate the widths of the columns.  That isn't the problem.  The problem is that I cannot use AutoSizeColumns to do it.  You may have a Default system, I don't.  My ppi is 240 (vs your 96).  My default value of varCellPadding is 8(vs your 2).  I have attached a image of the grid which shows what I am seeing here. 

Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: MarkMLl on February 08, 2021, 10:21:05 pm
Decades ago in the MS-DOS days, I was an avid user of UCSD Pascal and later of Modula-2.

You'll find a number of M2 refugees here. You'll also notice that most of the post-Wirth language extensions are more like M2 than Pascal, i.e. with an explicit end.

MarkMLl
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: wp on February 09, 2021, 12:13:23 am
I looked at your project again, and the lfm file, in fact, contains an entry on DesignTimePPI=240. In the lpi code I see "Application.Scaled := true" which tells me that LCL scaling is active - and this is what I would have told you to activate. OK - you already did.

I've never worked with such a huge resolution, but I played with your demo on my Win7 VM which runs at 150% (144ppi). Indeed I see that the longest text in the auto-sized 1st column is truncated similar to your screenshot, but only by about 1 pixel.

Since the overall scaling is correct, and since only a few pixels are missing, my gut feeling tells me that this could be related to the cell border, varCellPadding. Luckily, this is a global variable in the Grids unit, and it can be changed to any value.

The debugger tells me that varCellPadding on my 144ppi system has the value 5. When I reduce it to 4 the longest text in the grid is not truncated any more!

Where does this issue come frome? It must be related to the AutoAdjustColumn method which is called by the AutoSizeColumns method. The method calculates the width of the longest text in the column (similar to your code) and then adds the cell margin. But instead of adding two times the value of varCellPadding (for left and right border) it adds a constant DEFAUTOADJPADDING=8 in Laz 2.0.10. This is a bug because the cell border must be scaled with screen resolution.

Going back to my standard Win10 development system which runs at 96ppi and looking at the same code position I see that this already has been fixed in Laz trunk.

So, this is what you can do to fix the issue for your Laz 2.0.10:
Code: Pascal  [Select][+][-]
  1.   W := W + imgWidth;
  2.   if W=0 then
  3.     W := DefaultColWidth
  4.   else
  5.     W := W + 2*varCellpadding + 1;   // <---- CHANGE IN THIS LINE
  6.  
  7.   ColWidths[aCol] := W;  

When you recompile your program it should be correct now.
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: dgrhoads on February 09, 2021, 09:19:19 pm
WP:  Many thanks for looking into this problem and suggesting a fix.

I made the changes you suggested in grids.pas, then tested it using the little program I posted here a few days ago.  Problem fixed!!

Again, thank you.
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: dgrhoads on April 06, 2021, 03:24:08 am
Quote
Going back to my standard Win10 development system which runs at 96ppi and looking at the same code position I see that this already has been fixed in Laz trunk.

Sorry, but this issue did NOT get fixed in LCL 2.0.12.

Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: wp on April 06, 2021, 03:39:01 pm
Did you re-apply the changes that you did with 2.0.10?
Title: Re: Column widths set by AutoSizeColumns are too narrow
Post by: dgrhoads on April 06, 2021, 09:23:05 pm
Yes.

The original code that I had to change in 2.0.10 was present in 2.0.12.  I made the changes that you told me to and it worked properly.
TinyPortal © 2005-2018