Columns width for procedure that sets best fit

General TRichView support forum. Please post your questions here
Post Reply
vit
Posts: 115
Joined: Tue Feb 03, 2009 3:11 pm

Columns width for procedure that sets best fit

Post by vit »

Hello!

Related topic for this question is http://www.trichview.com/forums/viewtop ... f=2&t=8709

Now I want to implement SetTableBestFit for the table. As you can see from the source topic, some columns are wider than required and they should be make narrower. So my procedure calcs the content width for each cell in each column (using TCustomRichView.GetItemCoordsEx), puts values into an array of integer (ColsWidth) and then sets BestWidth of all columns to values from the array (Table.Cells[0, c].BestWidth := ColsWidth[c]). And I also set BestWidth to the table:

Code: Select all

Table.BestWidth := SumColsWidth(Table, ColsWidth);

class function TForm1.SumColsWidth(Table: TRVTableItemInfo; ColsWidth:
  TIntegerDynArray): Integer;
var
  c: Integer;
begin
  Result := 0;
  for c in ColsWidth do
    Result := Result + c;

  if Table.ColCount > 0 then
    Result := Result + Table.CellBorderWidth * 2 * Table.ColCount;
end;
Please notice that I add Table.CellBorderWidth * 2 * Table.ColCount to the value that SumColsWidth returns.

When I calculate width of cell items I also increase the result by Width := Width + Table.CellHPadding + Table.CellBorderWidth * 2; (remember that this value is going to be set to Table.Cells[0, c].BestWidth as I described above).

Are these correction that I do in SumColsWidth (Result := Result + Table.CellBorderWidth * 2 * Table.ColCount) and in column's width calculation (Width := Width + Table.CellHPadding + Table.CellBorderWidth * 2) neccessary and adequate for values that are going to be set to Cell.BestWidth and Table.BestWidth? Although the table looks good, I might miss something and the table with other settings like border width, paddigns and spacings will not look in the way I want (word breaks will differ)

DemoApp1.rar
Project. Be sure that Table.rvf is under the same path where exe is compiled to
(60.16 KiB) Downloaded 1617 times
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Columns width for procedure that sets best fit

Post by Sergey Tkachenko »

Cell.BestWidth (when measured in pixels) does not include cell padding and border width.
So, when the cell width is set by BestWidth, its full width is:
Full-Cell-Width = (CellBorderWidth + CellHPadding) * 2 + BestWidth.
The complete table width is (for simplicity, let all cells have the same width, Full-Cell-Width):
(BorderWidth + BorderHSpacing) * 2 + (Full-Cell-Width) * ColCount + CellHSpacing * (ColCount - 1)
Table.BestWidth includes everything, all cells, spacing and borders.
vit
Posts: 115
Joined: Tue Feb 03, 2009 3:11 pm

Re: Columns width for procedure that sets best fit

Post by vit »

Sergey Tkachenko wrote: Fri Nov 03, 2017 3:16 pm Cell.BestWidth (when measured in pixels) does not include cell padding and border width.
So, when the cell width is set by BestWidth, its full width is:
Full-Cell-Width = (CellBorderWidth + CellHPadding) * 2 + BestWidth.
In my proc I only have cell-content-width. So I need to calc Cell.BestWidth basing on cell-content-width.
Post Reply