Recent

Author Topic: [SOLVED] TShellTreeView question  (Read 2558 times)

petevick

  • Sr. Member
  • ****
  • Posts: 419
[SOLVED] TShellTreeView question
« on: January 24, 2024, 08:34:44 am »
I'm using a TShellTreeView as a folder browser, I'm able to create, delete and rename folders. When I delete a folder I display a modal message asking for confirmation, as soon as the message is displayed the node I'm deleting from collapses, see the attached gif. If I don't show the message the node stays expanded. Is this to be expected ??
« Last Edit: January 26, 2024, 09:01:54 pm by petevick »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12799
Re: TShellTreeView question
« Reply #1 on: January 24, 2024, 10:26:40 am »
TShellTreeView was written mainly to select and view directories, not as full Explorer replacement. Therefore, you must add some own code to achieve what you want. You do not show this code, therefore, I cannot tell what's wrong.

I am attaching the code that I would use, and it is working correctly.

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: TShellTreeView question
« Reply #2 on: January 24, 2024, 12:11:33 pm »
TShellTreeView was written mainly to select and view directories, not as full Explorer replacement. Therefore, you must add some own code to achieve what you want. You do not show this code, therefore, I cannot tell what's wrong.

I am attaching the code that I would use, and it is working correctly.
Thanks for the help wp. Using your example I've tracked the problem to the custom message dialogue I use, if I use MessageDlg(), as in your example, then the node no longer collapses. But I can't see anything in the custom message code I use, not that I know what I'm looking for !! The code I use is as below...

Code: Pascal  [Select][+][-]
  1. function MyDialog(const aCaption: String; const AMsg: string;
  2.           ADlgType: TMsgDlgType; AButtons: TMsgDlgButtons;
  3.           ADefButton: TBitBtnKind; const Apm: Boolean): TModalResult;
  4. var
  5.   MsgFrm: TForm;
  6.   I: Integer;
  7. begin
  8.   MsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons);
  9.   try
  10.     MsgFrm.FormStyle := fsStayOnTop;
  11.     for I := 0 to MsgFrm.ComponentCount - 1 do
  12.       if (MsgFrm.Components[I] is TBitBtn) then
  13.         if (MsgFrm.Components[I] as TBitBtn).Kind = ADefButton then
  14.           MsgFrm.ActiveControl := (MsgFrm.Components[I] as TBitBtn);
  15.     if Form1 <> nil then
  16.     begin
  17.       MsgFrm.Position := poDefaultSizeOnly;
  18.       MsgFrm.Caption:=aCaption;
  19.       MsgFrm.Left := Form1.Left + (Form1.Width - MsgFrm.Width) div 2;
  20.       MsgFrm.Top := Form1.Top + (Form1.Height - MsgFrm.Height) div 2;
  21.     end
  22.     else
  23.       MsgFrm.Position := poMainFormCenter;
  24.     Result := MsgFrm.ShowModal;
  25.   finally
  26.     MsgFrm.Free
  27.   end;
  28. end;
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12799
Re: TShellTreeView question
« Reply #3 on: January 24, 2024, 07:19:11 pm »
I replaced the MessageDlg in my project by your MyDialog, and cannot see any difference (well - to be exact: initially I did see your effect, but this was because I am using Laz/main which - in the {$IF LCL_FullVersion >= 3990000} branch - does not rebuild the tree after deletion from root, and a bug had crept into the start directory; fixed in attached project).

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: TShellTreeView question
« Reply #4 on: January 24, 2024, 09:04:26 pm »
I replaced the MessageDlg in my project by your MyDialog, and cannot see any difference (well - to be exact: initially I did see your effect, but this was because I am using Laz/main which - in the {$IF LCL_FullVersion >= 3990000} branch - does not rebuild the tree after deletion from root, and a bug had crept into the start directory; fixed in attached project).
Thanks again wp. I'll take a proper look tomorrow. Your new example certainly works here, but just using your MyDialog() in my project, I'm still getting the collapsing tree. There's obviously more to it than just copying MyDialog(). It's getting late and I need a drink  :D
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: TShellTreeView question
« Reply #5 on: January 25, 2024, 11:23:21 am »
@wp. There is obviously something else in my project that's influencing this behaviour. I've even replaced your ShellTreeView with the one from my project, and your project still behaves itself. I think I'll just stick with using MessageDlg(), I can live with it not centering over the form.
Thanks again for the help  ;)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: TShellTreeView question
« Reply #6 on: January 25, 2024, 12:22:06 pm »
I couldn't leave it  :D
The culprit is using the popup menu for deleting, if I use the buttons across the top, the tree does not collapse, but when the popup menu is used, the tree does collapse. The answer is to scrap the popup menu, it's a pity as I quite like it, but needs must when the devil drives  >:D  :D
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12799
Re: [SOLVED] TShellTreeView question
« Reply #7 on: January 25, 2024, 12:33:03 pm »
You are not talking of my demo project since it does not have a popup menu? Could you please extract the critical part of your code into a small project so that I can have a closer look at what you are doing exactly? BTW, is your signature correct? I.e. are you on Mint and do you use Laz 2.2.6? My tests so far were on Win 11 and with Laz/main.

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: [SOLVED] TShellTreeView question
« Reply #8 on: January 25, 2024, 01:08:39 pm »
You are not talking of my demo project since it does not have a popup menu? Could you please extract the critical part of your code into a small project so that I can have a closer look at what you are doing exactly? BTW, is your signature correct? I.e. are you on Mint and do you use Laz 2.2.6? My tests so far were on Win 11 and with Laz/main.
Yes my signature is correct. I primarily use Linux, but I compile projects in Windows as well, and Windows does not exhibit a collapsing tree when using the popup menu. I could still try and extract the browser to it's own project, but I see little point if you only use Windows.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12799
Re: [SOLVED] TShellTreeView question
« Reply #9 on: January 26, 2024, 12:17:50 am »
I ran my demo project in Linux Mint, and did not see any issue...

The only thing I could imagine which could prevent the node from collapsing is incorrect code for repopulating the tree after the other folder had been deleted. And this is why I would like to see your code, ideally as a small project which I can run through the compiler.

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: [SOLVED] TShellTreeView question
« Reply #10 on: January 26, 2024, 07:49:29 am »
I ran my demo project in Linux Mint, and did not see any issue...

The only thing I could imagine which could prevent the node from collapsing is incorrect code for repopulating the tree after the other folder had been deleted. And this is why I would like to see your code, ideally as a small project which I can run through the compiler.
Thinking it was the use of the popup menu that was causing the tree to collapse, I added a popup to your example, and it works just fine.
I have some other work that I need to do first but then I'll extract the browser into a project.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: [SOLVED] TShellTreeView question
« Reply #11 on: January 26, 2024, 05:11:51 pm »
I ran my demo project in Linux Mint, and did not see any issue...

The only thing I could imagine which could prevent the node from collapsing is incorrect code for repopulating the tree after the other folder had been deleted. And this is why I would like to see your code, ideally as a small project which I can run through the compiler.
@ wp. I've extracted my browser into it's own project (attached) and stripped out as much as I dare  :D I've also used your procedures for adding/deleting folders. This project still collapses the tree when deleting a folder using the popup menu in Linux (the top buttons work fine), Windows works as expected.
On the plus side, your procedures are way better and simpler than the ones I had, so I'll be cheeky use those anyway as I was getting problems in Windows with mine.  ;)

Thanks again for your help wp  ;)
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12799
Re: TShellTreeView question
« Reply #12 on: January 26, 2024, 08:21:15 pm »
OK, I think I found the issue. The problem seems to be that in Linux the form fires its OnActivate event each time when the popup and your messagbox close. This is different from Windows. Then your form runs through this event as if the application as just been freshly started: you repopulate the tree again and open the InitFolder forgetting about the previously opened folder...

You can avoid this by introducing a boolean state variable, FActivated. When the application has started FActivated is false, and you check this at the start of the OnActivate handler. Inside OnActivate, however, you set FActivated to true, and as a consequence, the OnActivate code is not executed any more if the event fires again for some reason later:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.   if FActivated then
  4.     exit;
  5.   FActivated := true;
  6.  
  7.   IsEdit:=False;
  8.   myTreeView.Items.Clear;
  9.   myTreeView.PopulateWithBaseFiles;
  10.   FindNodeWithPath(myTreeView, InitFolder);
  11.   myTreeView.SetFocus;
  12.   OpenFirst:=True;
  13. end;

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: TShellTreeView question
« Reply #13 on: January 26, 2024, 09:01:30 pm »
OK, I think I found the issue. The problem seems to be that in Linux the form fires its OnActivate event each time when the popup and your messagbox close. This is different from Windows. Then your form runs through this event as if the application as just been freshly started: you repopulate the tree again and open the InitFolder forgetting about the previously opened folder...

You can avoid this by introducing a boolean state variable, FActivated. When the application has started FActivated is false, and you check this at the start of the OnActivate handler. Inside OnActivate, however, you set FActivated to true, and as a consequence, the OnActivate code is not executed any more if the event fires again for some reason later:

Well I'll be damned. I really should have considered this as I had a similar problem with the main form in my project, and fixed in the same way, that's what OpenFirst:=True; was for !!.
Thanks wp, saved me a lot of headaches and I can now have my centered message  ;D !!
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

 

TinyPortal © 2005-2018