Skip to content

Commit 16ab0f5

Browse files
committed
fix: Tab in grid cell editors jumps into next editable cell, also on row wrap
Closes #1831
1 parent 06df3b2 commit 16ab0f5

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎components/virtualtreeview/Source/VirtualTrees.BaseTree.pas‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9516,7 +9516,11 @@ function TBaseVirtualTree.DoEndEdit(pCancel: Boolean = False): Boolean;
95169516
if Assigned(FOnEdited) then
95179517
FOnEdited(Self, FFocusedNode, FEditColumn);
95189518
end;
9519-
TrySetFocus();
9519+
// A new edit can have been started while releasing the edit link above, e.g.
9520+
// when the editor link of a grid jumps to the next cell on the Tab key.
9521+
// Do not steal the focus from that new editor.
9522+
if not (tsEditing in FStates) then
9523+
TrySetFocus();
95209524
end;
95219525

95229526
//----------------------------------------------------------------------------------------------------------------------

‎source/grideditlinks.pas‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface
88
Winapi.Windows, Vcl.Forms, Vcl.Graphics, Winapi.Messages, VirtualTrees, VirtualTrees.BaseTree, VirtualTrees.Types, Vcl.ComCtrls, System.SysUtils, System.Classes,
99
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.CheckLst, Vcl.Controls, System.Types, Vcl.Dialogs, Vcl.Menus, Vcl.Mask, System.DateUtils, System.Math,
1010
dbconnection, dbstructures, apphelpers, texteditor, bineditor, gnugettext,
11-
System.StrUtils, System.UITypes, SynRegExpr, Vcl.Themes, extra_controls;
11+
System.StrUtils, System.UITypes, SynRegExpr, Vcl.Themes, extra_controls, System.TypInfo;
1212

1313
type
1414
// Radio buttons and checkboxes which do not pass <Enter> key to their parent control
@@ -298,10 +298,16 @@ destructor TBaseGridEditorLink.Destroy;
298298
end;
299299
if not Assigned(NewNode) then
300300
Break;
301-
if not FTree.CanEdit(NewNode, NewColumn) then
301+
// Skip columns which cannot be focused or edited, e.g. the row number
302+
// column in data grids, which no editor can be created for.
303+
if not FTree.Header.AllowFocus(NewColumn) or not FTree.CanEdit(NewNode, NewColumn) then
302304
Continue;
303305
FTree.ClearSelection;
304306
FTree.Selected[NewNode] := True;
307+
// EditNode only sets the focused node, not the focused column, so without
308+
// this the cell focus stays on the previous column, visibly wrong when
309+
// Tab jumps from the last column back to the first column of a row.
310+
FTree.FocusedColumn := NewColumn;
305311
FTree.EditNode(NewNode, NewColumn);
306312
Break;
307313
end;
@@ -371,6 +377,8 @@ function TBaseGridEditorLink.EndEditHelper(NewText: String): Boolean;
371377
end;
372378

373379
procedure TBaseGridEditorLink.ReleaseEditorControl(AControl: TWinControl);
380+
var
381+
NullMethod: TMethod;
374382
begin
375383
// Do not free editor controls synchronously here. Our destructor regularly
376384
// runs while such a control is still processing a message, as its OnKeyDown
@@ -381,6 +389,13 @@ procedure TBaseGridEditorLink.ReleaseEditorControl(AControl: TWinControl);
381389
// VCL's key dispatch.
382390
if (csDestroying in AControl.ComponentState) or (csCreating in AControl.ControlState) then
383391
Exit;
392+
// Prevent the deferred control from calling back into this destroyed editor
393+
// link. Most importantly, the OnExit event fires on the focus change when
394+
// another editor link takes over the focus, e.g. when Tab moves the edit to
395+
// the next cell. Without this, the stale handler would end the *new* edit
396+
// immediately after it was started, and also run on freed memory.
397+
NullMethod := Default(TMethod);
398+
SetMethodProp(AControl, 'OnExit', NullMethod);
384399
if FParentForm is TExtForm then
385400
TExtForm(FParentForm).DeferFreeControl(AControl)
386401
else

0 commit comments

Comments
 (0)