Permalink
Browse files

Enhance remembering of current grid selection by also remembering the…

… focused node. Fires OnFocusChanged in case there was one and it's still there.
  • Loading branch information...
ansgarbecker committed Aug 29, 2018
1 parent d7d0ac1 commit b2e3a3079bd864060f230e302819fdec1101c781
Showing with 24 additions and 10 deletions.
  1. +20 −7 source/apphelpers.pas
  2. +4 −3 source/main.pas
View
@@ -302,8 +302,8 @@ TAppSettings = class(TObject)
function FindNode(VT: TVirtualStringTree; idx: Int64; ParentNode: PVirtualNode): PVirtualNode;
procedure SelectNode(VT: TVirtualStringTree; idx: Int64; ParentNode: PVirtualNode=nil); overload;
procedure SelectNode(VT: TVirtualStringTree; Node: PVirtualNode); overload;
function GetVTSelection(VT: TVirtualStringTree): TStringList;
procedure SetVTSelection(VT: TVirtualStringTree; Captions: TStringList);
procedure GetVTSelection(VT: TVirtualStringTree; var SelectedCaptions: TStringList; var FocusedCaption: String);
procedure SetVTSelection(VT: TVirtualStringTree; SelectedCaptions: TStringList; FocusedCaption: String);
function GetNextNode(Tree: TVirtualStringTree; CurrentNode: PVirtualNode; Selected: Boolean=False): PVirtualNode;
function GetPreviousNode(Tree: TVirtualStringTree; CurrentNode: PVirtualNode; Selected: Boolean=False): PVirtualNode;
function DateBackFriendlyCaption(d: TDateTime): String;
@@ -1599,37 +1599,50 @@ procedure SelectNode(VT: TVirtualStringTree; Node: PVirtualNode); overload;
end;
function GetVTSelection(VT: TVirtualStringTree): TStringList;
procedure GetVTSelection(VT: TVirtualStringTree; var SelectedCaptions: TStringList; var FocusedCaption: String);
var
Node: PVirtualNode;
InvalidationTag: Integer;
begin
// Return captions of selected nodes
InvalidationTag := vt.Tag;
vt.Tag := VTREE_LOADED;
Result := TStringList.Create;
SelectedCaptions.Clear;
Node := GetNextNode(VT, nil, true);
while Assigned(Node) do begin
Result.Add(VT.Text[Node, VT.Header.MainColumn]);
SelectedCaptions.Add(VT.Text[Node, VT.Header.MainColumn]);
if Node = VT.FocusedNode then begin
FocusedCaption := VT.Text[Node, VT.Header.MainColumn];
end;
Node := GetNextNode(VT, Node, true);
end;
vt.Tag := InvalidationTag;
end;
procedure SetVTSelection(VT: TVirtualStringTree; Captions: TStringList);
procedure SetVTSelection(VT: TVirtualStringTree; SelectedCaptions: TStringList; FocusedCaption: String);
var
Node: PVirtualNode;
idx: Integer;
DoFocusChange: Boolean;
begin
// Restore selected nodes based on captions list
DoFocusChange := False;
Node := GetNextNode(VT, nil, false);
while Assigned(Node) do begin
idx := Captions.IndexOf(VT.Text[Node, VT.Header.MainColumn]);
idx := SelectedCaptions.IndexOf(VT.Text[Node, VT.Header.MainColumn]);
if idx > -1 then
VT.Selected[Node] := True;
if (not FocusedCaption.IsEmpty) and (VT.Text[Node, VT.Header.MainColumn] = FocusedCaption) then begin
VT.FocusedNode := Node;
DoFocusChange := True;
end;
Node := GetNextNode(VT, Node, false);
end;
// Fire focus change event if there was a focused one before
if DoFocusChange and Assigned(VT.OnFocusChanged) then begin
VT.OnFocusChanged(VT, VT.FocusedNode, VT.FocusedColumn);
end;
end;
View
@@ -9565,7 +9565,7 @@ procedure TMainForm.HostListBeforePaint(Sender: TBaseVirtualTree; TargetCanvas:
IS_objects: TDBObjectList;
Obj: TDBObject;
ProcessColumns: TTableColumnList;
Columns: String;
Columns, FocusedCaption: String;
Col: TVirtualTreeColumn;
begin
// Display server variables
@@ -9585,7 +9585,8 @@ procedure TMainForm.HostListBeforePaint(Sender: TBaseVirtualTree; TargetCanvas:
Exit;
end;
SelectedCaptions := GetVTSelection(vt);
SelectedCaptions := TStringList.Create;
GetVTSelection(vt, SelectedCaptions, FocusedCaption);
SelectNode(vt, nil);
vt.BeginUpdate;
OldOffset := vt.OffsetXY;
@@ -9724,7 +9725,7 @@ procedure TMainForm.HostListBeforePaint(Sender: TBaseVirtualTree; TargetCanvas:
// Display number of listed values on tab
Tab.Caption := FHostTabCaptions[Tab.PageIndex] + ' (' + IntToStr(vt.RootNodeCount) + ')';
// Restore selection
SetVTSelection(vt, SelectedCaptions);
SetVTSelection(vt, SelectedCaptions, FocusedCaption);
end;

0 comments on commit b2e3a30

Please sign in to comment.