. , , ( node). , VT RTL- ( ):
type
TVirtualStringTree = class(VirtualTrees.TVirtualStringTree)
protected
function GetSpanColumn(Node: PVirtualNode): TColumnIndex; virtual;
public
procedure AutoFitSpanColumn(DestColumn: TColumnIndex; Node: PVirtualNode);
end;
implementation
{ TVirtualStringTree }
function TVirtualStringTree.GetSpanColumn(Node: PVirtualNode): TColumnIndex;
begin
{ this returns visible span column for the given node, InvalidColumn otherwise }
Result := Header.Columns.GetLastVisibleColumn;
while ColumnIsEmpty(Node, Result) and (Result <> InvalidColumn) do
Result := Header.Columns.GetPreviousVisibleColumn(Result);
end;
procedure TVirtualStringTree.AutoFitSpanColumn(DestColumn: TColumnIndex; Node: PVirtualNode);
var
ColsWidth: Integer;
SpanWidth: Integer;
SpanOffset: Integer;
SpanColumn: TColumnIndex;
begin
SpanColumn := GetSpanColumn(Node);
if SpanColumn <> InvalidColumn then
begin
{ get the total width of the header }
ColsWidth := Header.Columns.TotalWidth;
{ get the width of the span text cell as it would be autosized }
SpanWidth := DoGetNodeWidth(Node, SpanColumn) + DoGetNodeExtraWidth(Node, SpanColumn) +
DoGetCellContentMargin(Node, SpanColumn).X + Margin;
{ and get the left position of the span cell column in header }
SpanOffset := Header.Columns[SpanColumn].GetRect.Left;
{ now, the width of the autosized column we increase by the subtraction of the fully
visible span cell width and all columns width increased by offset of the span cell
column; in other words, we'll just increase or decrease width of the DestColumn to
the difference of width needed for the span column to be fully visible, or "fit" }
Header.Columns[DestColumn].Width := Header.Columns[DestColumn].Width +
SpanWidth - ColsWidth + SpanOffset;
end;
end;