Skip to content

Commit 485fb7d

Browse files
committed
fix: ban Windows API code used in OnAdvancedHeaderDraw for painting column headers with sort chars
Fixes wrong measured text before column headers get ellipsed
1 parent 377d402 commit 485fb7d

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

source/main.pas

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5626,14 +5626,12 @@ procedure TMainForm.AnyGridAdvancedHeaderDraw(Sender: TVTHeader;
56265626
var PaintInfo: THeaderPaintInfo; const Elements: THeaderPaintElements);
56275627
var
56285628
PaintArea, TextArea, IconArea, SortArea: TRect;
5629-
SortText, ColCaptionW, ColIndex: WideString;
5630-
ColCaption: String;
5631-
TextSpace, ColSortIndex, NumCharTop: Integer;
5629+
SortText, ColCaption: String;
5630+
ColIndex, TextSpace, ColSortIndex, NumCharTop: Integer;
56325631
ColSortDirection: laz.VirtualTrees.TSortDirection;
5633-
TextSize: TSize;
5634-
DeviceContext: HDC;
5635-
DrawFormat: Cardinal;
5632+
TextWidth: Integer;
56365633
ColInfo: TTableColumn;
5634+
TS: TTextStyle;
56375635
const
56385636
NumSortChars: Array of String = ['¹','²','³','⁴','⁵','⁶','⁷','⁸','⁹','⁺'];
56395637

@@ -5667,29 +5665,31 @@ procedure TMainForm.AnyGridAdvancedHeaderDraw(Sender: TVTHeader;
56675665

56685666
PaintArea := PaintInfo.PaintRectangle;
56695667
PaintArea.Inflate(-PaintInfo.Column.Margin, 0);
5670-
DeviceContext := PaintInfo.TargetCanvas.Handle;
5671-
DrawFormat := DT_TOP or DT_NOPREFIX or DT_LEFT;
5668+
// Paint text without background color:
5669+
//PaintInfo.TargetCanvas.Brush.Style := bsClear;
5670+
TS := PaintInfo.TargetCanvas.TextStyle;
5671+
TS.Opaque := False;
56725672

56735673
// Draw column name. Code taken from TVirtualTreeColumns.DrawButtonText and modified for our needs
56745674
if hpeText in Elements then begin
56755675

56765676
TextArea := PaintArea;
5677-
SetBkMode(DeviceContext, TRANSPARENT);
56785677

56795678
if AppSettings.ReadBool(asShowRowId) and (PaintInfo.Column.Index > 0) then begin
56805679
// Paint gray column number left to its caption
5681-
ColIndex := PaintInfo.Column.Index.ToString;
5680+
ColIndex := PaintInfo.Column.Index;
56825681
if Sender.Treeview = DataGrid then begin
56835682
ColInfo := SelectedTableColumns.FindByName(PaintInfo.Column.Text);
56845683
if Assigned(ColInfo) then
5685-
ColIndex := (SelectedTableColumns.IndexOf(ColInfo) + 1).ToString;
5684+
ColIndex := SelectedTableColumns.IndexOf(ColInfo) + 1;
56865685
end;
56875686

5688-
SetTextColor(DeviceContext, ColorToRGB(clGrayText));
5689-
DrawTextW(DeviceContext, PWideChar(ColIndex), Length(ColIndex), PaintArea, DrawFormat);
5687+
PaintInfo.TargetCanvas.Font.Color := clGrayText;
5688+
// Note: Canvas.TextOut does not paint transparent text
5689+
PaintInfo.TargetCanvas.TextRect(PaintArea, PaintArea.Left, PaintArea.Top, ColIndex.ToString, TS);
56905690
// Move caption text to right
5691-
GetTextExtentPoint32W(DeviceContext, PWideChar(ColIndex), Length(ColIndex), TextSize);
5692-
Inc(TextArea.Left, TextSize.cx + 5);
5691+
TextWidth := PaintInfo.TargetCanvas.TextWidth(ColIndex.ToString);
5692+
Inc(TextArea.Left, TextWidth + 5);
56935693
end;
56945694

56955695
ColCaption := PaintInfo.Column.Text;
@@ -5702,15 +5702,14 @@ procedure TMainForm.AnyGridAdvancedHeaderDraw(Sender: TVTHeader;
57025702

57035703
if not (coWrapCaption in PaintInfo.Column.Options) then begin
57045704
// Do we need to shorten the caption due to limited space?
5705-
GetTextExtentPoint32W(DeviceContext, PWideChar(ColCaption), Length(ColCaption), TextSize);
5705+
TextWidth := PaintInfo.TargetCanvas.TextWidth(ColCaption);
57065706
TextSpace := TextArea.Right - TextArea.Left;
5707-
if TextSpace < TextSize.cx then
5708-
ColCaption := laz.VirtualTrees.ShortenString(DeviceContext, ColCaption, TextSpace);
5707+
if TextSpace < TextWidth then
5708+
ColCaption := laz.VirtualTrees.ShortenString(PaintInfo.TargetCanvas.Handle, ColCaption, TextSpace);
57095709
end;
57105710

5711-
SetTextColor(DeviceContext, ColorToRGB(clWindowText));
5712-
ColCaptionW := UTF8ToUTF16(ColCaption);
5713-
DrawTextW(DeviceContext, PWideChar(ColCaptionW), Length(ColCaption), TextArea, DrawFormat);
5711+
PaintInfo.TargetCanvas.Font.Color := clWindowText;
5712+
PaintInfo.TargetCanvas.TextRect(TextArea, TextArea.Left, TextArea.Top, ColCaption, TS);
57145713
end;
57155714

57165715
// Draw image, if any
@@ -5729,26 +5728,21 @@ procedure TMainForm.AnyGridAdvancedHeaderDraw(Sender: TVTHeader;
57295728
Inc(SortArea.Left, SortArea.Width - Sender.Images.Width);
57305729
GetSortIndex(PaintInfo.Column, ColSortIndex, ColSortDirection);
57315730
if ColSortIndex > -1 then begin
5732-
// Prepare default font size, also if user selected a bigger one for the grid - we reserved a 16x16 space.
5733-
// Font.Height + Font.Size must be set with these values to get this working, larger or smaller Size/Height
5734-
// result in wrong size for multiple sort columns.
5735-
PaintInfo.TargetCanvas.Font.Height := -11;
5736-
PaintInfo.TargetCanvas.Font.Size := 10;
5731+
// Take care to keep the default font size, the user may have selected a bigger one for the grid - we reserved a 16x16 space.
5732+
// Would result in wrong size for multiple sort columns.
57375733
if ColSortDirection = sdAscending then begin
57385734
// Care for arrow characters available in most fonts. See #1090
5739-
SortText := WideChar($25B2); // BLACK UP-POINTING TRIANGLE
5735+
SortText := '▲'; // BLACK UP-POINTING TRIANGLE
57405736
NumCharTop := 0;
57415737
end else begin
5742-
SortText := WideChar($25BC); // BLACK DOWN-POINTING TRIANGLE
5738+
SortText := '▼'; // BLACK DOWN-POINTING TRIANGLE
57435739
NumCharTop := 5;
57445740
end;
57455741
// Paint arrow:
5746-
DrawTextW(DeviceContext, PWideChar(SortText), Length(SortText), SortArea, DrawFormat);
5742+
PaintInfo.TargetCanvas.TextRect(SortArea, SortArea.Left, SortArea.Top, SortText, TS);
57475743
// ... and superscript number right besides:
57485744
SortText := IfThen(ColSortIndex<9, NumSortChars[ColSortIndex], NumSortChars[9]);
5749-
Inc(SortArea.Left, 9);
5750-
Inc(SortArea.Top, NumCharTop);
5751-
DrawTextW(DeviceContext, PWideChar(SortText), Length(SortText), SortArea, DrawFormat);
5745+
PaintInfo.TargetCanvas.TextRect(SortArea, SortArea.Left+9, SortArea.Top+NumCharTop, SortText, TS);
57525746
end;
57535747
end;
57545748
end;

0 commit comments

Comments
 (0)