Skip to content

Commit 728f958

Browse files
committed
fix: scale VirtualTree fonts and node heights on per-monitor DPI change in lazarus port
1 parent eb287a6 commit 728f958

4 files changed

Lines changed: 119 additions & 16 deletions

File tree

‎source/apphelpers.pas‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,11 @@ procedure FixVT(VT: TVirtualStringTree; IsResultGrid: Boolean=False);
15411541
begin
15421542
// This is called either in some early stage (and probably from preferences/apply button?)
15431543
SingleLineHeight := GetTextHeight(VT.Font) + 7;
1544+
// GetTextHeight measures on a bitmap canvas with 96 PPI, which rescales the
1545+
// font back to 96 PPI, so scale the result to the font's own PPI. Needed on
1546+
// high-DPI monitors and after a per-monitor DPI change.
1547+
if (VT.Font.PixelsPerInch > 0) and (VT.Font.PixelsPerInch <> 96) then
1548+
SingleLineHeight := MulDiv(SingleLineHeight, VT.Font.PixelsPerInch, 96);
15441549
// Multiline nodes?
15451550
if IsResultGrid then
15461551
MultiLineCount := AppSettings.ReadInt(asGridRowLineCount)
@@ -1550,6 +1555,15 @@ procedure FixVT(VT: TVirtualStringTree; IsResultGrid: Boolean=False);
15501555
// Fixed through form files back at 96 PPI
15511556
VT.DefaultNodeHeight := SingleLineHeight * MultiLineCount;
15521557
VT.Header.Height := SingleLineHeight;
1558+
// Existing nodes cache their own height, so update them too.
1559+
VT.BeginUpdate;
1560+
Node := VT.GetFirstInitialized;
1561+
while Assigned(Node) do begin
1562+
if VT.NodeHeight[Node] <> VT.DefaultNodeHeight then
1563+
VT.NodeHeight[Node] := VT.DefaultNodeHeight;
1564+
Node := VT.GetNextInitialized(Node);
1565+
end;
1566+
VT.EndUpdate;
15531567
if MultiLineCount > 1 then begin
15541568
VT.BeginUpdate;
15551569
Node := VT.GetFirstInitialized;

‎source/extra_controls.pas‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,29 @@ interface
1616
WM_HEIDISQL_DEFERREDFREE = $1100;
1717

1818
type
19+
TMonitorDpiChangedEvent = procedure(Sender: TObject; OldDPI: Integer; NewDPI: Integer) of object;
20+
1921
// Form with a sizegrip in the lower right corner, without the need for a statusbar
2022

2123
{ TExtForm }
2224

2325
TExtForm = class(TForm)
26+
private
27+
FCurrentPPI: Integer;
28+
FOnMonitorDpiChanged: TMonitorDpiChangedEvent;
29+
procedure FixTreeFontsOnDPIChange(Owner: TComponent; NewDPI, OldDPI: Integer);
2430
protected
2531
procedure FilterNodesByEdit(Edit: TEditButton; Tree: TLazVirtualStringTree);
2632
procedure WMDeferredFree(var Message: TLMessage); message WM_HEIDISQL_DEFERREDFREE;
33+
procedure WMDPIChanged(var Msg: TLMessage); message LM_DPICHANGED;
34+
procedure DoShow; override;
35+
procedure DoMonitorDpiChanged(OldDPI, NewDPI: Integer); virtual;
2736
public
2837
constructor Create(AOwner: TComponent); override;
38+
// Applies the tree font/height fixes for the given DPI and fires
39+
// OnMonitorDpiChanged if the DPI changed. Called from WMDPIChanged and
40+
// once at startup from DoShow.
41+
procedure ApplyDPIFix(NewDPI: Integer);
2942
procedure DeferFreeControl(AControl: TControl);
3043
class procedure InheritFont(AFont: TFont);
3144
class procedure SaveListSetup(List: TLazVirtualStringTree);
@@ -37,6 +50,7 @@ TExtForm = class(TForm)
3750
function Space(Multiply: Integer=1): Integer;
3851
class procedure PageControlTabHighlight(PageControl: TPageControl);
3952
class procedure ShowPopup(ClickedControl: TControl; PopupMenu: TPopupMenu);
53+
property OnMonitorDpiChanged: TMonitorDpiChangedEvent read FOnMonitorDpiChanged write FOnMonitorDpiChanged;
4054
end;
4155

4256
TExtComboBox = class(TComboBox)
@@ -63,11 +77,95 @@ constructor TExtForm.Create(AOwner: TComponent);
6377
var
6478
OldImageList: TCustomImageList;
6579
begin
80+
FCurrentPPI := 0;
6681
inherited;
6782

6883
InheritFont(Font);
6984
end;
7085

86+
procedure TExtForm.WMDPIChanged(var Msg: TLMessage);
87+
begin
88+
// Let the LCL scale fonts and bounds first (AutoAdjustLayout inside
89+
// TCustomForm.WMDPIChanged); the new DPI comes directly from the message.
90+
inherited;
91+
ApplyDPIFix(hi(Cardinal(Msg.wParam)));
92+
end;
93+
94+
procedure TExtForm.DoShow;
95+
begin
96+
inherited;
97+
// Trigger the DPI fix once at startup (and when re-shown on a monitor with a
98+
// different DPI): on a high-DPI primary monitor no WM_DPICHANGED is sent at
99+
// startup, so without this the tree fonts/heights would not be scaled.
100+
ApplyDPIFix(Monitor.PixelsPerInch);
101+
end;
102+
103+
procedure TExtForm.ApplyDPIFix(NewDPI: Integer);
104+
var
105+
OldDPI: Integer;
106+
begin
107+
OldDPI := FCurrentPPI;
108+
if OldDPI <= 0 then
109+
OldDPI := 96; // design-time DPI
110+
if (NewDPI > 0) and (NewDPI <> FCurrentPPI) then begin
111+
// Moving window to a different screen or user changed the DPI setting.
112+
FCurrentPPI := NewDPI;
113+
FixTreeFontsOnDPIChange(Self, NewDPI, OldDPI);
114+
DoMonitorDpiChanged(OldDPI, NewDPI);
115+
end;
116+
end;
117+
118+
procedure TExtForm.FixTreeFontsOnDPIChange(Owner: TComponent; NewDPI, OldDPI: Integer);
119+
var
120+
i: Integer;
121+
VT: TLazVirtualStringTree;
122+
Node: PVirtualNode;
123+
begin
124+
// Recurse through owned components: query tabs etc. are owned by the form,
125+
// but their trees are owned by panels/tabsheets owned by them.
126+
for i := 0 to Owner.ComponentCount - 1 do begin
127+
if Owner.Components[i] is TLazVirtualStringTree then begin
128+
VT := TLazVirtualStringTree(Owner.Components[i]);
129+
// VirtualTrees paint into an offscreen bitmap whose canvas font PPI stays
130+
// at Screen.PixelsPerInch (primary monitor). TFont.Assign converts via Size
131+
// when PPIs differ, which would downconvert the LCL-scaled font back to
132+
// 96 dpi, so pin the PPI to the buffer canvas PPI and set the pixel height
133+
// for the monitor DPI directly. ParentFont must be set to False explicitly:
134+
// assigning Font clears it internally (TControl.FontChanged), and then the
135+
// parent font would no longer be copied on the next DPI change.
136+
VT.ParentFont := False;
137+
VT.Font.PixelsPerInch := Screen.PixelsPerInch;
138+
VT.Font.Height := -MulDiv(Font.Size, NewDPI, 72);
139+
// The header has its own font (ParentFont default False) which the LCL
140+
// does not scale either; apply the same fix.
141+
VT.Header.ParentFont := False;
142+
VT.Header.Font.PixelsPerInch := Screen.PixelsPerInch;
143+
VT.Header.Font.Height := VT.Font.Height;
144+
// VT does not scale node/header heights on a DPI change, so scale them by
145+
// the DPI ratio (this preserves multiline multipliers set by FixVT).
146+
VT.DefaultNodeHeight := MulDiv(VT.DefaultNodeHeight, NewDPI, OldDPI);
147+
VT.Header.Height := MulDiv(VT.Header.Height, NewDPI, OldDPI);
148+
VT.BeginUpdate;
149+
try
150+
Node := VT.GetFirstInitialized;
151+
while Assigned(Node) do begin
152+
VT.NodeHeight[Node] := MulDiv(VT.NodeHeight[Node], NewDPI, OldDPI);
153+
Node := VT.GetNextInitialized(Node);
154+
end;
155+
finally
156+
VT.EndUpdate;
157+
end;
158+
end;
159+
FixTreeFontsOnDPIChange(Owner.Components[i], NewDPI, OldDPI);
160+
end;
161+
end;
162+
163+
procedure TExtForm.DoMonitorDpiChanged(OldDPI, NewDPI: Integer);
164+
begin
165+
if Assigned(FOnMonitorDpiChanged) then
166+
FOnMonitorDpiChanged(Self, OldDPI, NewDPI);
167+
end;
168+
71169

72170
class procedure TExtForm.InheritFont(AFont: TFont);
73171
var

‎source/main.lfm‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ object MainForm: TMainForm
1212
ShowHint = True
1313
LCLVersion = '4.8.0.0'
1414
OnActivate = FormActivate
15-
OnChangeBounds = FormChangeBounds
1615
OnCloseQuery = FormCloseQuery
1716
OnCreate = FormCreate
1817
OnDestroy = FormDestroy
@@ -2964,7 +2963,7 @@ object MainForm: TMainForm
29642963
RootNodeCount = 6
29652964
TabOrder = 1
29662965
TreeOptions.AutoOptions = [toAutoDropExpand, toAutoScrollOnExpand, toAutoSort, toAutoSpanColumns, toAutoTristateTracking, toAutoDeleteMovedNodes, toAutoChangeScale]
2967-
TreeOptions.MiscOptions = [toAcceptOLEDrop, toCheckSupport, toFullRepaintOnResize, toInitOnSave, toToggleOnDblClick, toWheelPanning, toVariableNodeHeight, toEditOnClick]
2966+
TreeOptions.MiscOptions = [toAcceptOLEDrop, toCheckSupport, toFullRepaintOnResize, toInitOnSave, toToggleOnDblClick, toWheelPanning, toEditOnClick]
29682967
TreeOptions.PaintOptions = [toHotTrack, toShowButtons, toShowDropmark, toShowRoot, toShowTreeLines, toThemeAware, toUseBlendedImages, toUseExplorerTheme, toHideTreeLinesIfThemed]
29692968
TreeOptions.SelectionOptions = [toExtendedFocus, toFullRowSelect, toMultiSelect, toRightClickSelect]
29702969
OnBeforeCellPaint = treeQueryHelpersBeforeCellPaint

‎source/main.pas‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ TMainForm = class(TExtForm)
12421242
procedure actSynMoveUpExecute(Sender: TObject);
12431243
procedure actCopyTabsToSpacesExecute(Sender: TObject);
12441244
procedure actCopyUpdate(Sender: TObject);
1245-
procedure FormChangeBounds(Sender: TObject);
1245+
procedure FormMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);
12461246
procedure menuToggleAllClick(Sender: TObject);
12471247
procedure menuAutoFitAllClick(Sender: TObject);
12481248
procedure menuCopyColumnNamesClick(Sender: TObject);
@@ -1320,7 +1320,6 @@ TMainForm = class(TExtForm)
13201320
FEditorCommandStrings: TStringList;
13211321
//FHelpData: TSimpleKeyValuePairs;
13221322
FMainWinMaximized: Boolean;
1323-
FCurrentPixelsPerInch: Integer;
13241323
FRestoreTabsDone: Boolean;
13251324
FCommandLineFileNames: TStringList;
13261325

@@ -1752,20 +1751,13 @@ procedure TMainForm.StoreLastSessions;
17521751
end;
17531752

17541753

1755-
procedure TMainForm.FormChangeBounds(Sender: TObject);
1754+
procedure TMainForm.FormMonitorDpiChanged(Sender: TObject; OldDPI, NewDPI: Integer);
17561755
var
17571756
Factor: Extended;
1758-
NewDPI, OldDPI: Integer;
17591757
begin
1760-
NewDPI := Monitor.PixelsPerInch;
1761-
OldDPI := FCurrentPixelsPerInch;
1762-
if NewDPI <> OldDPI then begin
1763-
// Moving window to different screen or user changed DPI setting for current screen
1764-
FCurrentPixelsPerInch := NewDPI;
1765-
Factor := 100 / 96 * NewDPI;
1766-
LogSQL(f_('Scaling controls to screen DPI: %d%%', [Round(Factor)]));
1767-
//LogSQL('PixelsPerInchDesigned:'+PixelsPerInchDesigned.ToString+' OldDPI:'+OldDPI.ToString+' NewDPI:'+NewDPI.ToString);
1768-
end;
1758+
// Tree font fixes are done generically in TExtForm.WMDPIChanged; here we only log.
1759+
Factor := 100 / 96 * NewDPI;
1760+
LogSQL(f_('Scaling controls to screen DPI: %d%%', [Round(Factor)]));
17691761
end;
17701762

17711763

@@ -2211,7 +2203,7 @@ procedure TMainForm.FormCreate(Sender: TObject);
22112203

22122204
FLastMouseButtonUpOnGrid := mbLeft;
22132205
FLastCaptionChange := 0;
2214-
FCurrentPixelsPerInch := 0;
2206+
OnMonitorDpiChanged := FormMonitorDpiChanged;
22152207
FFormatSettings := DefaultFormatSettings;
22162208
FFormatSettings.DecimalSeparator := '.';
22172209
FFormatSettings.ThousandSeparator := ' ';;

0 commit comments

Comments
 (0)