[SOLVED]TShellTreeView - how to rename a folder lazarus / 31-LCL ---------------------------------------------------------------------- petevick · 2022-02-02 18:16 I have a TShellTreeView in a small test project. With the ReadOnly property set to False, you can appear to edit/rename a folder caption by double clicking with a delay. This all looked good for renaming, but when the TShellTreeView is refreshed, the folder caption is not renamed. Have I missed some other property ?? I've even tried a simplistic way of renaming the folder...... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TreeView.Selected.Text := 'Lets try this';...but that doesn't work either. ---------------------------------------------------------------------- wp · 2022-02-02 18:19 The ShellTreeView does not propagate the renaming action to the file system. You must rename the file/directory yourself. ---------------------------------------------------------------------- petevick · 2022-02-02 18:26 Quote from: wp on February 02, 2022, 07:19:15 pmThe ShellTreeView does not propage the renaming action to the file system. You must rename the file/directory yourself. Thanks for the reply. So how would one go about that ---------------------------------------------------------------------- balazsszekely · 2022-02-02 19:48 Create an OnEdited event for the tree: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses LazFileUtils, StrUtils; procedure TForm1.ShellTreeView1Edited(Sender: TObject; Node: TTreeNode;  var S: string);var  OldPath: String;  NewPath: String;  P: Integer;begin  OldPath := AppendPathDelim(Node.GetTextPath);  OldPath := StringReplace(OldPath, '/', DirectorySeparator, [rfReplaceAll]);  NewPath := ReverseString(OldPath);  Delete(NewPath, 1, 1);  P := Pos(DirectorySeparator, NewPath);  Delete(NewPath, 1, P - 1);  NewPath := AppendPathDelim(ReverseString(NewPath) + S);  ShowMessage('Old path: ' + OldPath + sLineBreak +              'New path: ' + Newpath);end; ---------------------------------------------------------------------- petevick · 2022-02-02 20:01 Thanks for the reply GetMem, I'll take a look later when I've got time to properly understand the code ---------------------------------------------------------------------- petevick · 2022-02-02 20:15 Quote from: GetMem on February 02, 2022, 08:48:27 pmCreate an OnEdited event for the tree: Unfortunately GetMem it's not working. It all seems to work, OldPath and Newpath variables display correctly, as does the folder text in the tree after closing the message dialog. But after a refresh of the tree the folder text reverts back to OldPath ---------------------------------------------------------------------- balazsszekely · 2022-02-02 20:19 @petevick QuoteUnfortunately GetMem it's not working. It all seems to work, OldPath and Newpath variables display correctly, as does the folder text in the tree after closing the message dialog. But after a refresh of the tree the folder text reverts back to OldPath Well I assumed that you will do the renaming. Anyways instead of  the last liine(Showmessage...), you need: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if RenameFile(OldPath, NewPath) then   //successelse  //error handling ---------------------------------------------------------------------- petevick · 2022-02-02 20:26 Quote from: GetMem on February 02, 2022, 09:19:23 pmWell I assumed that you will do the renaming. Anyways instead of  the last liine(Showmessage...), you need: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if RenameFile(OldPath, NewPath) then   //successelse  //error handling ermm yes, well, that would have helped I guess........well would you look at that works just fine. I blame it on a brain fart moment  Thanks again GetMem ---------------------------------------------------------------------- balazsszekely · 2022-02-03 07:59 @petevick I forgot that you can also add files to TShellTreeView, so a better and more simplistic approach would be something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses LazFileUtils; procedure TForm1.ShellTreeView1Edited(Sender: TObject; Node: TTreeNode;  var S: string);var  OldPath: String;  NewPath: String;begin  OldPath := TShellTreeNode(Node).FullFilename;  NewPath := TShellTreeNode(Node).BasePath + S;  if TShellTreeNode(Node).IsDirectory then  begin    OldPath := AppendPathDelim(OldPath);    NewPath := AppendPathDelim(NewPath);  end;   if not RenameFile(OldPath, NewPath) then  begin    S := Node.Text;    MessageDlg('Cannot rename object! Error message:'  + sLineBreak + '"' + SysErrorMessage(GetLastOSError) + '"', mtError, [mbOK], 0);  endend; Unfortunately both method fail on windows. The issue is related to the newly introduced shell icons. After renaming the node, the tree will try to load the icons from the old file, which is no longer exists(unit: win32wsshellctrls , method: TWin32WSCustomShellTreeView.DrawBuiltInIcon). ---------------------------------------------------------------------- petevick · 2022-02-03 09:40 Quote from: GetMem on February 03, 2022, 08:59:11 amUnfortunately both method fail on windows. The issue is related to the newly introduced shell icons. After renaming the node, the tree will try to load the icons from the old file, which is no longer exists(unit: win32wsshellctrls , method: TWin32WSCustomShellTreeView.DrawBuiltInIcon). Thanks for the updated code GetMem. I'm a bit confused though, both this and the previous version work in my Windows 10 VM ---------------------------------------------------------------------- balazsszekely · 2022-02-03 13:59 QuoteI'm a bit confused though, both this and the previous version work in my Windows 10 VM I'm working with Lazarus trunk, where the issue is clearly visible. ---------------------------------------------------------------------- wp · 2022-02-03 15:58 Quote from: GetMem on February 03, 2022, 08:59:11 amUnfortunately both method fail on windows. The issue is related to the newly introduced shell icons. After renaming the node, the tree will try to load the icons from the old file, which is no longer exists(unit: win32wsshellctrls , method: TWin32WSCustomShellTreeView.DrawBuiltInIcon). GetMem, being the author of the ShellTreeView icons on Windows I feel responsible for this. Can you post a project so that I can see the issue? ---------------------------------------------------------------------- balazsszekely · 2022-02-03 16:21 @wp QuoteGetMem, being the author of the ShellTreeView icons on Windows I feel responsible for this. Can you post a project so that I can see the issue?Please run attached project, then edit a node(make sure you edit a dummy folder node, since the changes will be propagated back to the filesystem). An exception should occur in unit win32wsshellctrls , method TWin32WSCustomShellTreeView.DrawBuiltInIcon. The icon is not found since the tree is looking for the old file name, not the new one. ---------------------------------------------------------------------- wp · 2022-02-03 20:11 Thank you. The problem is that the text displayed in the node is not the true filename which is stored in the internal FileInfo record of the TShellTreeNode. So, when a node is renamed in the tree, only the Node.Text is changed, but not the filename in the FileInfo. Unfortunately, the FileInfo is pretty well-sealed within the TShellTreeNode from the outside world, and I am afraid that the issue can only be solved within the ShellTreeViews unit. For test purposes, I added the following public method Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TCustomShellTreeView.Rename(ANode: TTreeNode; ANewName: String): Boolean;var  oldFullName, newFullName: String;begin  oldFullName := TShellTreeNode(ANode).FullFilename;  newFullName := TShellTreeNode(ANode).BasePath + ANewName;  if TShellTreeNode(ANode).IsDirectory then  begin    oldFullName := AppendPathDelim(oldFullName);    newFullname := AppendPathDelim(newFullName);  end;  Result := RenameFile(oldFullName, newFullName);  if Result then  begin    TShellTreeNode(ANode).FFileInfo.Name := ANewName;    ANode.Text := ANewName;  end;end; And the event handler of your code would be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ShellTreeView1Edited(Sender: TObject; Node: TTreeNode;  var S: string);begin  if not ShellTreeView1.Rename(Node, S) then  begin    S := Node.Text;    MessageDlg('Cannot rename object! Error message:'  + sLineBreak + '"' + SysErrorMessage(GetLastOSError) + '"', mtError, [mbOK], 0);  endend; This works correctly in my tests. What I don't like is that the user probably expects that the ANewName argument could contain a full path in order to move the file to a different directory. However, if this were allowed the node would have to be moved to a completely different parent node, but it is not guaranteed that the new parent node exists in the tree at all. Another, more elegant solution would be to override the setter for the node's Text property so that it automatically updates the internal FileInfo field. But this would require to declare the SetText method as virtual. And at the moment I don't overlook the issues that might occur when a new node is getting its caption for the first time. I also could imagine that there are use cases in which the displayed node text should be different from the filename. ---------------------------------------------------------------------- balazsszekely · 2022-02-03 20:49 QuoteThis works correctly in my tests. What I don't like is that the user probably expects that the ANewName argument could contain a full path in order to move the file to a different directory. However, if this were allowed the node would have to be moved to a completely different parent node, but it is not guaranteed that the new parent node exists in the tree at all.TCustomShellTreeView.Rename looks good and in my opinion is not confusing at all, however if we allow rename to propagate to the filesystem, we should also allow delete/new to do the same. I'm afraid we gonna open Pandora's box. Better we leave the filesystem and the tree disconnected. Can you prevent the exception by checking if Ico <> nil? Perhaps load a default dir icon when GetShellIcon fails. ---------------------------------------------------------------------- wp · 2022-02-03 22:45 Looking at it again, I think the "real" issue is that we have double storage of the file name in TShellTreeNode: first in the Text property of the node, second in the FileInfo.Name (FileInfo is the SearchRec obtained by FindFirst/FindNext). That's always a source of trouble... Since the FileInfo is not directly accessible by user, only by methods ShortFileName and LongFileName, we could simply ignore the FileInfo.Name and use the Node.Text for construction of the short and long filenames: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TShellTreeNode.ShortFilename: String;begin  Result := Text;end; function TShellTreeNode.FullFilename: String;begin  if (FBasePath <> '') then    Result := AppendPathDelim(FBasePath) + Text  else    //root nodes    Result := Text;  {$if defined(windows) and not defined(wince)}  if (Length(Result) = 2) and (Result[2] = DriveSeparator) then    Result := Result + PathDelim;  {$endif}end; After having done these changes in unit ShellCtrls, GetMem's sample project runs without crashing. ---------------------------------------------------------------------- balazsszekely · 2022-02-04 07:14 @wp I can confirm that the above changes works fine. ---------------------------------------------------------------------- wp · 2022-02-04 09:55 Committed it to Laz/main and requested backporting to fixes. Wrote a bugreport to document the reason why it is done (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/39629). [EDIT] I also checked whether the very similar TShellListView has the same issue with renaming - no, it has not, it is working correctly out of the box. ---------------------------------------------------------------------- balazsszekely · 2022-02-05 20:12 Quote from: wp on February 04, 2022, 10:55:41 amCommitted it to Laz/main and requested backporting to fixes. Wrote a bugreport to document the reason why it is done (https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/39629). [EDIT] I also checked whether the very similar TShellListView has the same issue with renaming - no, it has not, it is working correctly out of the box. Thanks wp!