TShellTreeView, How to mask file extensions? lazarus / 31-LCL ---------------------------------------------------------------------- jamie · 2019-08-16 01:01 I would like to use this control because it would be nice to put this on a tabsheet and it does work that way however, I don't see any premade way to set the filter mask for the files I want to view only.  I am interested in viewing a series of image file extensions but the way it works now it is showing all file types when I enable the object types to do so.  Also, I see there is a Method in there "GetFilesInDir" which accepts a MASK string to specify the types I want accumulated but not for the viewing of the TreeView ? It shows all the files and I only what the files of interest to show in the branches..  Where did I sleep? ---------------------------------------------------------------------- wp · 2019-08-16 09:15 Quote from: jamie on August 16, 2019, 03:01:58 amAlso, I see there is a Method in there "GetFilesInDir" which accepts a MASK string to specify the types I want accumulated but not for the viewing of the TreeView ? It shows all the files and I only what the files of interest to show in the branches.. I tried to introduce a Mask property to be used by GetFilesInDir. The consequence, however, is that the Mask is applied also to directory names, and this is probably not what you want. Certainly, this can be fixed but requires some larger changes with more intense testing than I can do at the moment... Then I tried to introduce an event OnAddingFile with a boolean var- parameter "Accept" to prevent a file from inclusion in the file list under some circumstances. But this failed too because GetFilesInDir is a class procedure which does not have access to the event... Again, this can be made, but would break the code in existing projects using GetFilesInDir without creating a class instance. So, in total, no easy way... Why don't you combine a folders-only ShellTreeView with ShellListView? That's way I always have been using TShellTreeView. And, in fact, it is very useful this way. ---------------------------------------------------------------------- Bart · 2019-08-16 11:02 Does Delphi's TTreeView support this? If so, file a bugreport please. I second wp's advice to use a mask for the attached TShellListView? Bart ---------------------------------------------------------------------- wp · 2019-08-16 11:22 Quote from: Bart on August 16, 2019, 01:02:19 pmDoes Delphi's TTreeView support this? In current Delphi XE 10.3, I don't see TShellTreeView any more, it did exist in Delphi 7, though. But even this was without a "Mask" property (there was an OnAddFolder event, though, with a "CanAdd" var-parameter - but we are talking here about files, not folders). I remember the Delphi7 ShellTreeView as being extremely hard to use and fragile. The Lazarus ShellTreeView (and ShellListView) are much more stable and very straightforward to use. ---------------------------------------------------------------------- Bart · 2019-08-16 13:59 An optional paramter could be used to declare wether the supplied mask is for both files and folders (the default behaviour) or for files only. Not very pretty, I know. It could be done, it probably would not break any code. But IMO TShellTreeView simply wasn't designed for this kind of thing. Personally I don't even like that it can show files at all. Bart ---------------------------------------------------------------------- wp · 2019-08-16 14:43 Quote from: Bart on August 16, 2019, 03:59:43 pmBut IMO TShellTreeView simply wasn't designed for this kind of thing. Personally I don't even like that it can show files at all.Like myself. ---------------------------------------------------------------------- Bart · 2019-08-16 21:09 Your use of Pos() to determine wether the mask matches is wrong. You could use MatchesMaskList() function for that instead ... I don't think we will incorporate such behaviour in TShellTreeView though. You could maybe override PopulateTreeNodeWithFiles in a derived class? Bart ---------------------------------------------------------------------- wp · 2019-08-16 21:29 I think you get more flexibility when you use an event "OnAddItem" which gets the entire SearchRec as a parameter and has a boolean var- parameter CanAdd which must be set to false to exclude the file/folder from 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 type  TAddItemEvent = procedure(Sender: TObject; const ABasePath: String;  const AFileInfo: TSearchRec; var CanAdd: Boolean) of object;   TCustomShellTreeView = class(TCustomTreeView)  private    ...    FOnAddItem: TAddItemEvent;  protected    ...    property OnAddItem: TAddItemEvent read FOnAddItem write FOnAddItem;  end;   TShellTreeView = class(TCustomShellTreeView)  ...  published    property OnAddItem;... function TCustomShellTreeView.PopulateTreeNodeWithFiles(  ANode: TTreeNode; ANodePath: string): Boolean;var  i: Integer;  Files: TStringList;  NewNode: TTreeNode;  canAdd: Boolean;   // <<< new...begin...  Files := TStringList.Create;  try    Files.OwnsObjects := True;    GetFilesInDir(ANodePath, AllFilesMask, FObjectTypes, Files, FFileSortType);    Result := Files.Count > 0;     for i := 0 to Files.Count - 1 do    begin      // <<<<<<<<< new from here....      canAdd := true;      if Assigned(FOnAddItem) then        with TFileItem(Files.Objects[i]) do          FOnAddItem(Self, FBasePath, FileInfo, canAdd);      if not canAdd then        Continue;    // ... to here >>>>>>>>>>>>>>       NewNode := Items.AddChildObject(ANode, Files.Strings[i], nil);      ....  The following event handler, for example, filters all files with extensions ".pas" and ".pp" which were created this year: 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.ShellTreeView1AddItem(Sender: TObject;  const ABasePath: String; const AFileInfo: TSearchRec; var CanAdd: Boolean);var  ext: String;begin  ext := Lowercase(ExtractFileExt(AFileInfo.Name));  CanAdd := (AFileInfo.Attr and faDirectory <> 0) or (    (YearOf(FileDateToDateTime(AFileInfo.Time)) = 2019) and ((ext = '.pas') or (ext = '.pp'))  );end; Delphi 7 has an OnAddFolder event handler for the same purpose; it is not compatible though because it has a TShellObj parameter instead of the TSearchRec. ---------------------------------------------------------------------- Bart · 2019-08-16 21:50 That would probably make a sensible addition. W.r.t. coding style I prefer 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 canAdd then  begin   //rest of original code  end; over: 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 not canAdd then        Continue; Since Lazarus TShellTreeView is not really compatible with Delphi's one anyway, IMO such a thing could be implemented. Bart ---------------------------------------------------------------------- Bart · 2019-08-17 10:04 Quote from: jamie on August 17, 2019, 01:15:37 amI want something that works with the OI and I am not writing a new control just for this little nitch. Hack the system then. You only override the method in question. You give the control the same classname: 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 unit MyShellTree;...type  TShellTreeView = class(ShellCtrls.TShellTreeView)  ...  end;  Then in your project simply use MyShellTree after ShellCtrls. Drop a regular TShellTreeView on the form. At runtime this will now become an instance of MyShellTree.TShellTreeView. It will stream (=load) OK, because you did not alter any published property. Bart ---------------------------------------------------------------------- wp · 2019-08-17 10:23 Bart, I think it is required that the method PopulateTreeNodeWithFiles() is virtual, otherwise it will not be called. ---------------------------------------------------------------------- Bart · 2019-08-17 11:38 It can be made virtual if so requested. We've done that before. Bart ---------------------------------------------------------------------- Bart · 2019-08-17 20:27 Quote from: jamie on August 17, 2019, 08:46:38 pm I get the feeling that I have disrupted someone's holy grail of code and there just isn't going to be any changing it, even when all it does is improves it with no crutches of current support. Well, excuse me for having an opinion about this (and having done work on TShellTreeView). I'll refrain from any further comment in this topic. I'll remind myself not to reply to anything you say on this forum. Bart