Recent

Author Topic: Column widths set by AutoSizeColumns are too narrow  (Read 959 times)

dgrhoads

  • New Member
  • *
  • Posts: 25
Column widths set by AutoSizeColumns are too narrow
« 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.

jamie

  • Hero Member
  • *****
  • Posts: 4439
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #1 on: February 07, 2021, 02:18:26 am »
what OS ?. widget etc ?

and what controls are you really talking about here?

I know there was an issue with some fonts not calculating correctly on one of the Linux oses using GTK2 or something
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 3844
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #2 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 ... :-\
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dgrhoads

  • New Member
  • *
  • Posts: 25
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #3 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.

wp

  • Hero Member
  • *****
  • Posts: 8367
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #4 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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

dgrhoads

  • New Member
  • *
  • Posts: 25
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #5 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. 


MarkMLl

  • Hero Member
  • *****
  • Posts: 2473
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #6 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 8367
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #7 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:
  • Open the unid grids.pas
  • Find the method TCustomStringGrid.AutoAdjustColumn
  • Scroll towards its end and replace the DEFAUTOADJPADDING by "2*varcellPadding + 1" (the +1 probably is some safety measure)
  • Finally, the end of the AutoAdjustColumn method should look like this:
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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

dgrhoads

  • New Member
  • *
  • Posts: 25
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #8 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.

dgrhoads

  • New Member
  • *
  • Posts: 25
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #9 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.


wp

  • Hero Member
  • *****
  • Posts: 8367
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #10 on: April 06, 2021, 03:39:01 pm »
Did you re-apply the changes that you did with 2.0.10?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

dgrhoads

  • New Member
  • *
  • Posts: 25
Re: Column widths set by AutoSizeColumns are too narrow
« Reply #11 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