LCL, ShellTreeView: Implement property Path. Issue #22538

git-svn-id: trunk@38174 -
This commit is contained in:
juha 2012-08-05 12:06:33 +00:00
parent fc7d4fcba1
commit 6e442d0bf6

View File

@ -53,7 +53,9 @@ type
FShellListView: TCustomShellListView; FShellListView: TCustomShellListView;
FFileSortType: TFileSortType; FFileSortType: TFileSortType;
{ Setters and getters } { Setters and getters }
function GetPath: string;
procedure SetFileSortType(const AValue: TFileSortType); procedure SetFileSortType(const AValue: TFileSortType);
procedure SetPath(AValue: string);
procedure SetRoot(const AValue: string); procedure SetRoot(const AValue: string);
procedure SetShellListView(const Value: TCustomShellListView); procedure SetShellListView(const Value: TCustomShellListView);
protected protected
@ -74,14 +76,15 @@ type
class procedure GetFilesInDir(const ABaseDir: string; class procedure GetFilesInDir(const ABaseDir: string;
AMask: string; AObjectTypes: TObjectTypes; AResult: TStrings; AFileSortType: TFileSortType = fstNone); AMask: string; AObjectTypes: TObjectTypes; AResult: TStrings; AFileSortType: TFileSortType = fstNone);
{ Other methods specific to Lazarus } { Other methods specific to Lazarus }
function GetPathFromNode(ANode: TTreeNode): string; function GetPathFromNode(ANode: TTreeNode; AIncludeRoot: Boolean = True): string;
function GetSelectedNodePath(): string; function GetSelectedNodePath: string;
{ Properties } { Properties }
property ObjectTypes: TObjectTypes read FObjectTypes write FObjectTypes; property ObjectTypes: TObjectTypes read FObjectTypes write FObjectTypes;
property ShellListView: TCustomShellListView read FShellListView write SetShellListView; property ShellListView: TCustomShellListView read FShellListView write SetShellListView;
property FileSortType: TFileSortType read FFileSortType write SetFileSortType; property FileSortType: TFileSortType read FFileSortType write SetFileSortType;
property Root: string read FRoot write SetRoot; property Root: string read FRoot write SetRoot;
property Path: string read GetPath write SetPath;
{ Protected properties which users may want to access, see bug 15374 } { Protected properties which users may want to access, see bug 15374 }
property Items; property Items;
@ -116,6 +119,7 @@ type
property ReadOnly; property ReadOnly;
property RightClickSelect; property RightClickSelect;
property Root; property Root;
property Path;
property RowSelect; property RowSelect;
property ScrollBars; property ScrollBars;
property SelectionColor; property SelectionColor;
@ -575,6 +579,7 @@ function TCustomShellTreeView.GetRootPath: string;
begin begin
if FRoot <> '' then Result := FRoot if FRoot <> '' then Result := FRoot
else Result := GetBasePath(); else Result := GetBasePath();
Result := IncludeTrailingPathDelimiter(Result);
end; end;
{ Returns true if at least one item was added, false otherwise } { Returns true if at least one item was added, false otherwise }
@ -666,40 +671,84 @@ begin
FShellListView.Root := GetPathFromNode(Selected); FShellListView.Root := GetPathFromNode(Selected);
end; end;
function TCustomShellTreeView.GetPathFromNode(ANode: TTreeNode): string; function TCustomShellTreeView.GetPathFromNode(ANode: TTreeNode; AIncludeRoot: Boolean): string;
var var
rootDir : String; nodeDir : String;
begin begin
// If nothing is selected, then the base is selected Result := '';
if ANode = nil then Exit(GetRootPath()); // return the base if nothing is selected
if ANode = nil then
begin
if AIncludeRoot then
Result := GetRootPath();
Exit;
end;
// In the future use ANode.Data instead of ANode.Text // Build the path. In the future use ANode.Data instead of ANode.Text
rootDir := PChar(ANode.Text); nodeDir := ANode.Text;
while (ANode.Parent <> nil) do while (ANode.Parent <> nil) do
begin begin
ANode := ANode.Parent; ANode := ANode.Parent;
if (PChar(ANode.Text) <> PathDelim) then // Was tested in original code (and didn't make sense), now replaced with assertion. [JuMa]
rootDir := PChar(ANode.Text) + PathDelim + rootDir Assert(PChar(ANode.Text) <> PathDelim,
else Format('TCustomShellTreeView.GetPathFromNode: ANode.Text (%s) is PathDelim', [ANode.Text]));
rootDir := PChar(ANode.Text) + rootDir; nodeDir := ANode.Text + PathDelim + nodeDir;
end; end;
// Check if the base directory should be taken into account // Check if root directory should be included
if FRoot = '' then if AIncludeRoot then
begin Result := GetRootPath() + nodeDir
if GetBasePath() <> '' then
Result := rootDir
else
Result := GetBasePath + rootDir;
end
else else
Result := IncludeTrailingPathDelimiter(FRoot) + rootDir; Result := nodeDir;
end; end;
function TCustomShellTreeView.GetSelectedNodePath(): string; function TCustomShellTreeView.GetSelectedNodePath: string;
begin begin
Result := GetPathFromNode(Selected); Result := GetPathFromNode(Selected);
end; end;
function TCustomShellTreeView.GetPath: string;
begin
Result := GetPathFromNode(Selected, False);
end;
procedure TCustomShellTreeView.SetPath(AValue: string);
var
sl: TStringList;
Node: TTreeNode;
i: integer;
begin
// We got a full path, make it relative
if DirectoryExistsUTF8(AValue) then
AValue := CreateRelativePath(AValue, GetRootPath());
// Make sure the path is correct now
if not DirectoryExistsUTF8(GetRootPath()+AValue) then Exit;
sl := TStringList.Create;
sl.Delimiter := PathDelim;
sl.StrictDelimiter := True;
sl.DelimitedText := TrimFilename(AValue); // Clean the path and then split it
BeginUpdate;
try
Node := Items.GetFirstVisibleNode;
for i := 0 to sl.Count-1 do
begin
while (Node <> Nil) and (Node.Text <> sl[i]) do
Node := Node.GetNextVisibleSibling;
if Node <> Nil then
begin
Node.Expanded := True;
Node.Selected := True;
Node := Node.GetFirstVisibleChild;
end
else
Break;
end;
finally
sl.free;
EndUpdate;
end;
end;
{ TCustomShellListView } { TCustomShellListView }
procedure TCustomShellListView.SetShellTreeView( procedure TCustomShellListView.SetShellTreeView(