Skip to content

Commit c8e8bed

Browse files
committed
fix: next attempt to fix ERangeError crash in TBaseVirtualTree.UpdateVerticalRange
- Moves call to FixVT from the often called ApplyFontToGrids to FormCreate. This hopefully heals the crash. - Fixes GetTextHeight to use the tree's font, not the one on MainForm. This was likely another culprit here. - Also moves code for setting IncrementalSearch on grids to FixVT Refs #2344
1 parent 2367ac6 commit c8e8bed

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

source/apphelpers.pas

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ TAppSettings = class(TObject)
368368
procedure StreamToClipboard(Text, HTML: TStream);
369369
function WideHexToBin(text: String): AnsiString;
370370
function BinToWideHex(bin: AnsiString): String;
371-
procedure FixVT(VT: TVirtualStringTree; MultiLineCount: Word=1);
371+
procedure FixVT(VT: TVirtualStringTree; IsResultGrid: Boolean=False);
372372
function GetTextHeight(Font: TFont): Integer;
373373
function ColorAdjustBrightness(Col: TColor; Shift: SmallInt): TColor;
374374
procedure DeInitializeVTNodes(Sender: TBaseVirtualTree);
@@ -1455,19 +1455,22 @@ procedure StreamToClipboard(Text, HTML: TStream);
14551455
end;
14561456

14571457

1458-
procedure FixVT(VT: TVirtualStringTree; MultiLineCount: Word=1);
1458+
procedure FixVT(VT: TVirtualStringTree; IsResultGrid: Boolean=False);
14591459
var
1460-
SingleLineHeight: Integer;
1460+
SingleLineHeight, MultiLineCount: Integer;
14611461
Node: PVirtualNode;
14621462
begin
1463-
// This is called either in some early stage, or from preferences dialog
1463+
// This is called either in some early stage (and probably from preferences/apply button?)
14641464
SingleLineHeight := GetTextHeight(VT.Font) + 7;
14651465
// Multiline nodes?
1466-
// Node height calculation has some hard to find bug, see issue #2344
1467-
// So we'll leave Header.MinHeight at its default value.
1466+
if IsResultGrid then
1467+
MultiLineCount := AppSettings.ReadInt(asGridRowLineCount)
1468+
else
1469+
MultiLineCount := 1;
1470+
// Issue #2344: TBaseVirtualTree.UpdateVerticalRange crashes with ERangeError, due to FRangeY/Cardinal
1471+
// getting a negative value
1472+
// Happening when DefaultNodeHeight is set after clearing nodes and then with 0 nodes
14681473
VT.DefaultNodeHeight := SingleLineHeight * MultiLineCount;
1469-
//VT.Header.MinHeight := SingleLineHeight;
1470-
VT.Header.Height := SingleLineHeight;
14711474
if MultiLineCount > 1 then begin
14721475
VT.BeginUpdate;
14731476
Node := VT.GetFirstInitialized;
@@ -1488,21 +1491,33 @@ procedure FixVT(VT: TVirtualStringTree; MultiLineCount: Word=1);
14881491
VT.OnMouseWheel := MainForm.AnyGridMouseWheel;
14891492
VT.ShowHint := True;
14901493

1491-
if toGridExtensions in VT.TreeOptions.MiscOptions then
1492-
VT.HintMode := hmHint // Show cell contents with linebreakds in datagrid and querygrid's
1494+
if IsResultGrid then begin
1495+
VT.HintMode := hmHint; // Show cell contents with linebreakds in datagrid and querygrid's
1496+
if AppSettings.ReadBool(asIncrementalSearch) then begin
1497+
// Apply case insensitive incremental search event
1498+
VT.IncrementalSearch := isInitializedOnly;
1499+
VT.OnIncrementalSearch := Mainform.AnyGridIncrementalSearch;
1500+
end
1501+
else begin
1502+
VT.IncrementalSearch := isNone;
1503+
end;
1504+
end
14931505
else
14941506
VT.HintMode := hmTooltip; // Just a quick tooltip for clipped nodes
1495-
// Apply case insensitive incremental search event
1496-
if VT.IncrementalSearch <> laz.VirtualTrees.isNone then
1497-
VT.OnIncrementalSearch := Mainform.AnyGridIncrementalSearch;
14981507
VT.OnStartOperation := Mainform.AnyGridStartOperation;
14991508
VT.OnEndOperation := Mainform.AnyGridEndOperation;
15001509
end;
15011510

15021511

15031512
function GetTextHeight(Font: TFont): Integer;
1513+
var
1514+
Bmp: Graphics.TBitmap;
15041515
begin
1505-
Result := MainForm.Canvas.TextHeight('Äy');
1516+
Bmp := Graphics.TBitmap.Create;
1517+
Bmp.Canvas.Font.Name := Font.Name;
1518+
Bmp.Canvas.Font.Size := Font.Size;
1519+
Result := Bmp.Canvas.TextHeight('Äy');
1520+
Bmp.Free;
15061521
end;
15071522

15081523

source/main.pas

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,7 @@ procedure TMainForm.FormCreate(Sender: TObject);
20852085
FixVT(ListProcesses);
20862086
FixVT(ListCommandStats);
20872087
FixVT(ListTables);
2088+
FixVT(DataGrid, True);
20882089
FixVT(treeQueryHelpers);
20892090

20902091
// Set noderoot for query helpers box
@@ -9084,13 +9085,12 @@ procedure TMainForm.ApplyFontToGrids;
90849085
QueryTab: TQueryTab;
90859086
ResultTab: TResultTab;
90869087
Grid: TVirtualStringTree;
9087-
IncrementalSearchActive: Boolean;
90889088
AllGrids: TObjectList<TVirtualStringTree>;
90899089
begin
9090-
// Apply changed settings to all existing data and query grids
9090+
// Apply (changed?) font setting to all data and query grids
9091+
// Do not change other things on the grid, or call FixVT here
90919092
LogSQL('Apply grid settings...', lcDebug);
90929093
AllGrids := TObjectList<TVirtualStringTree>.Create(False);
9093-
IncrementalSearchActive := AppSettings.ReadBool(asIncrementalSearch);
90949094
AllGrids.Add(DataGrid); // Data tab grid
90959095
AllGrids.Add(QueryGrid); // Mother query grid
90969096
for QueryTab in QueryTabs do begin // Query tab child grids
@@ -9101,11 +9101,6 @@ procedure TMainForm.ApplyFontToGrids;
91019101
for Grid in AllGrids do begin
91029102
Grid.Font.Name := AppSettings.ReadString(asDataFontName);
91039103
Grid.Font.Size := AppSettings.ReadInt(asDataFontSize);
9104-
FixVT(Grid, AppSettings.ReadInt(asGridRowLineCount));
9105-
if IncrementalSearchActive then
9106-
Grid.IncrementalSearch := isInitializedOnly
9107-
else
9108-
Grid.IncrementalSearch := isNone;
91099104
end;
91109105
AllGrids.Free;
91119106
end;
@@ -15432,7 +15427,7 @@ constructor TResultTab.Create(AOwner: TQueryTab);
1543215427
Grid.OnNewText := OrgGrid.OnNewText;
1543315428
Grid.OnPaintText := OrgGrid.OnPaintText;
1543415429
Grid.OnStartOperation := OrgGrid.OnStartOperation;
15435-
FixVT(Grid, AppSettings.ReadInt(asGridRowLineCount));
15430+
FixVT(Grid, True);
1543615431
FTabIndex := QueryTab.ResultTabs.Count; // Will be 0 for the first one, even if we're already creating the first one here!
1543715432
end;
1543815433

0 commit comments

Comments
 (0)