Skip to content

Commit 5add9ce

Browse files
committed
Fix for issue #1228:Columns EndUpdate and duplicate position
1 parent 23892be commit 5add9ce

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

Source/VirtualTrees.Header.pas

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ implementation
452452
WinApi.UxTheme,
453453
System.Math,
454454
System.SysUtils,
455+
System.Generics.Defaults,
455456
Vcl.Forms,
456457
VirtualTrees.HeaderPopup,
457458
VirtualTrees.BaseTree,
@@ -4397,38 +4398,36 @@ procedure TVirtualTreeColumns.DrawButtonText(DC : HDC; Caption : string; Bounds
43974398
//----------------------------------------------------------------------------------------------------------------------
43984399

43994400
procedure TVirtualTreeColumns.FixPositions;
4400-
44014401
// Fixes column positions after loading from DFM or Bidi mode change.
4402-
44034402
var
4404-
I : Integer;
4405-
LoopAgain: Boolean;
4403+
LColumnsByPos: TList<TVirtualTreeColumn>;
4404+
I: Integer;
44064405
begin
4407-
// Fix positions that too large, see #1179
4408-
// Fix duplicate positions, see #1228
4409-
repeat
4410-
LoopAgain := False;
4411-
for I := 0 to Count - 1 do
4406+
LColumnsByPos := TList<TVirtualTreeColumn>.Create;
4407+
try
4408+
LColumnsByPos.Capacity := Self.Count;
4409+
for I := 0 to Self.Count-1 do
4410+
LColumnsByPos.Add(Items[I]);
4411+
4412+
LColumnsByPos.Sort(
4413+
TComparer<TVirtualTreeColumn>.Construct(
4414+
function(const A, B: TVirtualTreeColumn): Integer
4415+
begin
4416+
Result := CompareValue(A.Position, B.Position);
4417+
if Result = 0 then
4418+
Result := CompareValue(A.Index, B.Index);
4419+
end)
4420+
);
4421+
4422+
for I := 0 to LColumnsByPos.Count-1 do
44124423
begin
4413-
if Integer(Items[I].FPosition) >= Count then
4414-
begin
4415-
Items[I].Position := Count -1;
4416-
LoopAgain := True;
4417-
end;
4418-
if (i < Count -1) and (Items[I].Position = Items[I+1].FPosition) then
4419-
begin
4420-
if Items[I].FPosition > 0 then
4421-
Dec(Items[I].FPosition)
4422-
else
4423-
Inc(Items[I].FPosition);
4424-
LoopAgain := True;
4425-
end;
4426-
end; // for
4427-
until not LoopAgain;
4424+
LColumnsByPos[I].FPosition := I;
4425+
Self.FPositionToIndex[I] := LColumnsByPos[I].Index;
4426+
end;
44284427

4429-
// Update position array
4430-
for I := 0 to Count - 1 do
4431-
FPositionToIndex[Items[I].Position] := I;
4428+
finally
4429+
LColumnsByPos.Free;
4430+
end;
44324431

44334432
FNeedPositionsFix := False;
44344433
UpdatePositions(True);

0 commit comments

Comments
 (0)