How can i get the Column Size ?

General TRichView support forum. Please post your questions here
Post Reply
mediasofts
Posts: 6
Joined: Thu Feb 07, 2008 12:35 pm

How can i get the Column Size ?

Post by mediasofts »

It's possible to set the column size with "Table.ResizeCol"
But how can i get a column size ?

I use the property "Table.cells[0,i].Width", but it doesn't work well ...

~-~
More informations :

"I need to memorize the column width of a table
and set there column widths to another table (with the same count of column)"

:arrow: How I get the colWidths :

Code: Select all

for j := 0 to TableSource.ColCount-1 do
begin
  ColWidth[j] := TableSource.cells[0,j].Width; // Problem Here ! this is not the real width
end;

:arrow: How I Set the ColWidth to another table :

Code: Select all

for j := 0 to ColWidth.Count-1 do
begin
   Table.ResizeCol(j,ColWidth[j]) ,true);    
end;

:) Thanks !
David
mediasofts
Posts: 6
Joined: Thu Feb 07, 2008 12:35 pm

More informations

Post by mediasofts »

Hi ,

I Use a "non-percent" width (width > 0).

I find this on "RVTable.pas" L 7557:

Code: Select all

procedure SetWidth(c,w,x: Integer; Decrement: Boolean);
  var r,a,b,a1,b1,oldw: Integer;
      Changed: Boolean;
      Cell: TRVTableCellData;
  begin
    oldw := w-CellHPadding*2;
    if Decrement then begin
      if c<ColCount-1 then
        x := Fmt.ColStarts[c+1] - (CellBorderWidth+(CellHSpacing+1) div 2)
      else
        x := Fmt.FWidth-(BorderWidth+BorderHSpacing+CellBorderWidth{+1}) div 2;
      x  := x + (Fmt.ColStarts[c]-Fmt.ColStarts[c-1] - (CellHSpacing+1) div 2)-w-CellBorderWidth;
      w := x - Fmt.ColStarts[c];
    end;
    a := w;
    dec(w, CellHPadding*2);
    if c<ColCount-1 then begin
      dec(w,CellBorderWidth);
      dec(w, CellHSpacing div 2);
      end
    else
      dec(w, (BorderWidth+BorderHSpacing+CellBorderWidth+1) div 2);
    //inc(x, w-a);
    if w<=0 then w := 1;
    Changed := False;
    for r := 0 to RowCount-1 do
      if (Cells[r,c]<>nil) and (Cells[r,c].ColSpan=1) then begin
        SetCellBestWidth(w,r,c);
        Changed := True;
      end;
    for r := 0 to RowCount-1 do
      if (Cells[r,c]=nil) then begin
        Cell := Rows.GetMainCell(r,c,a,b);
        if (b=c) or Decrement then continue;
        if (c=ColCount-1) or
           (Cells[r,c+1]<>nil) or
           (Cell<>Rows.GetMainCell(r,c+1,a1,b1)) then begin
          if Changed then
            w := 0
          else begin
           // w := x-Cell.Left;
            w := Cell.Width - Fmt.ColWidths[c]+oldw;
            if w<=0 then
              w := 1;
          end;
          SetCellBestWidth(w,a,b)
        end;
      end;
  end;
I try to understand how it works :D

Thanks for help !
David
mediasofts
Posts: 6
Joined: Thu Feb 07, 2008 12:35 pm

Investigation....

Post by mediasofts »

:idea: This might be correct :

Code: Select all

if (C=0) or (C=ColCount-1) then
  ColWidth := CellWidth + (CellBorder*2) + BorderHSpacing + (CellHSpacing div 2)
else
  ColWidth := CellWidth + (CellBorder*2) + (CellHSpacing);

:lol: David
Sergey Tkachenko
Site Admin
Posts: 17522
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Normally, width of cells are calculated basing on BestWidth properties of table cells. Consider assigning BestWidths properties from one table to another. There may be a problem with merged cells, or if cells in the same column have different BestWidths, though.

Widths of columns are stored in Fmt.ColWidths, but you cannot access it because it is a private property. Cell.Width may help, but it does not include border width and spacing. Honestly, I do not remember this stuff exactly, but if your code look right, and if it works, it is correct :)
mediasofts
Posts: 6
Joined: Thu Feb 07, 2008 12:35 pm

Final function ....

Post by mediasofts »

Works only if there are no Merged Cells.
And without percentCellWidth (width > 0).

Code: Select all

function ComputeColWidth(Table: TRVTableItemInfo; NoCol: integer): integer;
var
  Delta : Integer;
begin
  Delta :=  Table.CellPadding *2;
  if (NoCol=Table.ColCount-1) then
  begin
    // Last Column
    Delta := Delta + (Table.BorderWidth+Table.BorderHSpacing+Table.CellBorderWidth+1)div 2;
  end
  else
  begin
    // Other Column
    Delta := Delta + Table.CellBorderWidth + Table.CellHSpacing div 2;
  end;

  result := Table.Cells[0,NoCol].BestWidth + Delta;
end;
I've created a public property

Code: Select all

property ColWidths_Mediasoft:TRVIntegerList read GetColWidths_Mediasoft;

function TRVTableItemInfo.GetColWidths_Mediasoft: TRVIntegerList;
begin
  result := fmt.ColWidths;
end;
But it doesn't work like i want ...

Thanks Sergey
David
Pieter Zijlstra
Posts: 42
Joined: Sat Oct 08, 2005 3:56 pm
Location: The Netherlands
Contact:

Post by Pieter Zijlstra »

The following is what I use to determine the colwidths of the current row (see RVRuler.pas in UpdateTableEditor for more).

Code: Select all

...
          if RulerType = rtHorizontal then
          begin
            // Setup columns
            RVCellLast := nil;
            for C := 0 to RVTable.Rows[Row].Count - 1 do
            begin
              RVCell := RVTable.Cells[Row, C];
              if RVCell <> nil then
              begin
                TableEditor.Cells.Add.CellWidth := M * RVCell.Width;
                RVCellLast := RVCell;
              end
              else
              begin
                RVCell := RVTable.Rows.GetMainCell(Row, C, MainRow, MainCol);
                if RVCellLast <> RVCell then
                begin
                  TableEditor.Cells.Add.CellWidth := M * RVCell.Width;
                  RVCellLast := RVCell;
                end
                else
                  TableEditor.Cells.Add.CellWidth := -1;
              end;
            end;
..
Note that this is the width of the cell without borders, spacing, etc.

The hint is to check GetMainCell when dealing with merged cell.

HTH
Post Reply