LCL: Implemented TTreeView.SelectionCount and TTreeView.Selections[] for Delphi compatibility.Part of issue #8351

git-svn-id: trunk@28463 -
This commit is contained in:
zeljko 2010-11-24 17:32:47 +00:00
parent e1453ef20a
commit ac69cf5cee
2 changed files with 67 additions and 4 deletions

View File

@ -2227,6 +2227,7 @@ type
TTreeNodes = class(TPersistent)
private
FCount: integer;
FSelection: TFPList;
FFirstMultiSelected: TTreeNode;
FLastMultiSelected: TTreeNode;
FKeepCollapsedNodes: boolean;
@ -2239,6 +2240,7 @@ type
procedure ClearCache;
function GetHandle: THandle;
function GetNodeFromIndex(Index: Integer): TTreeNode;
function GetSelectionCount: Cardinal;
function GetTopLvlItems(Index: integer): TTreeNode;
procedure GrowTopLvlItems;
function IndexOfTopLvlItem(Node: TTreeNode): integer;
@ -2287,6 +2289,7 @@ type
function GetLastNode: TTreeNode; // last top level node
function GetLastSubNode: TTreeNode; // absolute last node
function GetLastExpandedSubNode: TTreeNode; // absolute last node
function GetSelections(const AIndex: Integer): TTreeNode;
function FindTopLvlNode(const NodeText: string): TTreeNode;
function FindNodeWithText(const NodeText: string): TTreeNode;
function FindNodeWithData(const NodeData: Pointer): TTreeNode;
@ -2296,6 +2299,7 @@ type
function InsertBehind(PrevNode: TTreeNode; const S: string): TTreeNode;
function InsertObjectBehind(PrevNode: TTreeNode; const S: string;
Data: Pointer): TTreeNode;
procedure SelectionsChanged(ANode: TTreeNode; const AIsSelected: Boolean);
procedure SortTopLevelNodes(SortProc: TTreeNodeCompare);
procedure ConsistencyCheck;
procedure WriteDebugReport(const Prefix: string; AllNodes: boolean);
@ -2304,6 +2308,7 @@ type
property KeepCollapsedNodes: boolean
read FKeepCollapsedNodes write FKeepCollapsedNodes;
property Owner: TCustomTreeView read FOwner;
property SelectionCount: Cardinal read GetSelectionCount;
property TopLvlCount: integer read FTopLvlCount;
property TopLvlItems[Index: integer]: TTreeNode
read GetTopLvlItems write SetTopLvlItems;
@ -2446,6 +2451,8 @@ type
function GetRightClickSelect: boolean;
function GetRowSelect: boolean;
function GetSelection: TTreeNode;
function GetSelectionCount: Cardinal;
function GetSelections(AIndex: Integer): TTreeNode;
function GetShowButtons: boolean;
function GetShowLines: boolean;
function GetShowRoot: boolean;
@ -2669,7 +2676,9 @@ type
property ScrollBars: TScrollStyle
read FScrollBars write SetScrollBars default ssBoth;
property Selected: TTreeNode read GetSelection write SetSelection;
property SelectionCount: Cardinal read GetSelectionCount;
property SelectionColor: TColor read FSelectedColor write SetSelectedColor default clHighlight;
property Selections[AIndex: Integer]: TTreeNode read GetSelections;
property SeparatorColor: TColor read fSeparatorColor write SetSeparatorColor default clGray;
property TopItem: TTreeNode read GetTopItem write SetTopItem;
property TreeLineColor: TColor read FTreeLineColor write FTreeLineColor default clWindowFrame;

View File

@ -661,11 +661,17 @@ var
begin
if AValue=GetSelected then exit;
TV:=TreeView;
if AValue then begin
if AValue then
begin
Include(FStates,nsSelected);
if (TV<>nil) then begin
if (TV<>nil) then
begin
TV.EndEditing(true);
MultiSelect:=tvoAllowMultiselect in TV.Options;
if not MultiSelect and Assigned(FOwner) then
FOwner.SelectionsChanged(Self, True);
if MultiSelect then TV.LockSelectionChangeEvent;
try
TV.Selected:=Self;
@ -677,9 +683,13 @@ begin
if MultiSelect then TV.UnlockSelectionChangeEvent;
end;
end;
end else begin
end else
begin
if not MultiSelected and Assigned(FOwner) then
FOwner.SelectionsChanged(Self, False);
Exclude(FStates,nsSelected);
if (TV<>nil) and (TV.Selected=Self) then begin
if (TV<>nil) and (TV.Selected=Self) then
begin
TV.EndEditing(true);
TV.Selected:=nil;
if TV.Selected=Self then
@ -1030,9 +1040,13 @@ begin
if AValue then begin
if (Treeview<>nil) and (not (tvoAllowMultiselect in TreeView.Options)) then
exit;
if Assigned(FOwner) then
FOwner.SelectionsChanged(Self, True);
Include(FStates,nsMultiSelected);
if TreeNodes<>nil then BindToMultiSelected;
end else begin
if Assigned(FOwner) then
FOwner.SelectionsChanged(Self, False);
Exclude(FStates,nsMultiSelected);
if TreeNodes<>nil then UnbindFromMultiSelected;
end;
@ -1817,12 +1831,14 @@ end;
constructor TTreeNodes.Create(AnOwner: TCustomTreeView);
begin
inherited Create;
FSelection := TFPList.Create;
FOwner := AnOwner;
end;
destructor TTreeNodes.Destroy;
begin
Clear;
FreeThenNil(FSelection);
inherited Destroy;
end;
@ -1863,6 +1879,7 @@ begin
if (FUpdateCount=0) and (Owner<>nil) then
Owner.Invalidate;
end;
FSelection.Clear;
EndUpdate;
end;
@ -1940,6 +1957,25 @@ begin
Result := InternalAddObject(ParentNode, S, Data, taAddFirst);
end;
procedure TTreeNodes.SelectionsChanged(ANode: TTreeNode; const AIsSelected: Boolean);
begin
if ANode <> nil then
begin
if AIsSelected then
FSelection.Add(ANode)
else
FSelection.Remove(ANode);
end;
end;
function TTreeNodes.GetSelections(const AIndex: Integer): TTreeNode;
begin
if (AIndex < 0) or (AIndex > FSelection.Count - 1) then
TreeNodeError('TTreeNodes.GetSelections Index '+IntToStr(AIndex)
+' out of bounds (Count='+IntToStr(FSelection.Count)+')');
Result := TTreeNode(FSelection.Items[AIndex]);
end;
function TTreeNodes.Add(SiblingNode: TTreeNode; const S: string): TTreeNode;
begin
Result := AddObject(SiblingNode, S, nil);
@ -2186,6 +2222,11 @@ begin
FNodeCache.CacheIndex := Index;
end;
function TTreeNodes.GetSelectionCount: Cardinal;
begin
Result := Cardinal(FSelection.Count);
end;
{function TTreeNodes.GetNode(ItemId: HTreeItem): TTreeNode;
var
Item: TTVItem;
@ -3828,6 +3869,19 @@ begin
Result := FSelectedNode;
end;
function TCustomTreeView.GetSelectionCount: Cardinal;
begin
Result := Items.SelectionCount;
end;
function TCustomTreeView.GetSelections(AIndex: Integer): TTreeNode;
begin
if (AIndex >= 0) and (AIndex < Items.SelectionCount) then
Result := Items.GetSelections(AIndex)
else
Result := nil;
end;
procedure TCustomTreeView.SetSelection(Value: TTreeNode);
var OldNode: TTreeNode;
begin