Skip to content

Commit 3d7aef5

Browse files
committed
fix: header height counts line breaks in shortened captions
Closes #2652
1 parent a077402 commit 3d7aef5

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

‎source/main.pas‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ TMainForm = class(TExtForm)
11811181
var PaintInfo: THeaderPaintInfo; var Elements: THeaderPaintElements);
11821182
procedure AnyGridAdvancedHeaderDraw(Sender: TVTHeader;
11831183
var PaintInfo: THeaderPaintInfo; const Elements: THeaderPaintElements);
1184+
function ShortenHeaderCaption(Tree: TVirtualStringTree; Col: TVirtualTreeColumn): String;
11841185
procedure SynMemoQueryScanForFoldRanges(Sender: TObject;
11851186
FoldRanges: TSynFoldRanges; LinesToScan: TStrings; FromLine,
11861187
ToLine: Integer);
@@ -3461,14 +3462,20 @@ procedure TMainForm.AfterQueryExecution(Thread: TQueryThread);
34613462
col.ImageIndex := ICONINDEX_UNIQUEKEY
34623463
else if NewTab.Results.ColIsKeyPart(i) then
34633464
col.ImageIndex := ICONINDEX_INDEXKEY;
3464-
HeaderLineBreaks := Max(HeaderLineBreaks, col.text.CountChar(#10));
34653465
end;
34663466
NewTab.Grid.Header.Columns.EndUpdate;
3467-
NewTab.Grid.Header.Height := GetTextHeight(NewTab.Grid.Font) * (HeaderLineBreaks+1) + HeaderPadding;
34683467
NewTab.Grid.RootNodeCount := NewTab.Results.RecordCount;
34693468
NewTab.Grid.EndUpdate;
34703469
for i:=0 to NewTab.Grid.Header.Columns.Count-1 do
34713470
AutoCalcColWidth(NewTab.Grid, i);
3471+
// Header height depends on the line breaks in the caption which is actually
3472+
// visible, i.e. after shortening to the calculated column width. See issue #1566
3473+
for i:=0 to NewTab.Grid.Header.Columns.Count-1 do begin
3474+
col := NewTab.Grid.Header.Columns[i];
3475+
if coVisible in col.Options then
3476+
HeaderLineBreaks := Max(HeaderLineBreaks, ShortenHeaderCaption(NewTab.Grid, col).CountChar(#10));
3477+
end;
3478+
NewTab.Grid.Header.Height := GetTextHeight(NewTab.Grid.Font) * (HeaderLineBreaks+1) + HeaderPadding;
34723479
if Tab.tabsetQuery.TabIndex = -1 then
34733480
Tab.tabsetQuery.TabIndex := 0;
34743481
end;
@@ -6012,6 +6019,41 @@ procedure TMainForm.AnyGridAdvancedHeaderDraw(Sender: TVTHeader;
60126019
end;
60136020

60146021

6022+
function TMainForm.ShortenHeaderCaption(Tree: TVirtualStringTree; Col: TVirtualTreeColumn): String;
6023+
// Returns the column caption which is actually visible in the header, i.e. shortened
6024+
// to the available column width the same way as AnyGridAdvancedHeaderDraw does at
6025+
// paint time. Callers can base calculations on it, e.g. the header height, so the
6026+
// invisible tail of a too long caption does not affect it. See issue #1566.
6027+
var
6028+
DeviceContext: HDC;
6029+
IndexText: String;
6030+
TextSize: TSize;
6031+
TextSpace: Integer;
6032+
begin
6033+
Result := Col.Text;
6034+
if coWrapCaption in Col.Options then
6035+
Exit;
6036+
Tree.Canvas.Font := Tree.Font;
6037+
DeviceContext := Tree.Canvas.Handle;
6038+
TextSpace := Col.Width - Col.Margin*2;
6039+
if AppSettings.ReadBool(asShowRowId) and (Col.Index > 0) then begin
6040+
IndexText := Col.Index.ToString;
6041+
GetTextExtentPoint32W(DeviceContext, PWideChar(IndexText), Length(IndexText), TextSize);
6042+
Dec(TextSpace, TextSize.cx + 5);
6043+
end;
6044+
if Assigned(Tree.Header.Images) then begin
6045+
if Col.ImageIndex > -1 then
6046+
Dec(TextSpace, Tree.Header.Images.Width);
6047+
if (Tree = DataGrid) and Assigned(FDataGridSortItems.FindByColumn(Col.Text)) or
6048+
(Tree <> DataGrid) and (Tree.Header.SortColumn = Col.Index) then
6049+
Dec(TextSpace, Tree.Header.Images.Width);
6050+
end;
6051+
GetTextExtentPoint32W(DeviceContext, PWideChar(Result), Length(Result), TextSize);
6052+
if TextSpace < TextSize.cx then
6053+
Result := VirtualTrees.Utils.ShortenString(DeviceContext, Result, TextSpace);
6054+
end;
6055+
6056+
60156057
procedure TMainForm.DataGridBeforePaint(Sender: TBaseVirtualTree; TargetCanvas: TCanvas);
60166058
var
60176059
vt: TVirtualStringTree;

0 commit comments

Comments
 (0)