Rookie needs help with Treeview and files

Just starting out? Need help? Post your questions and find answers here.
User avatar
pb-user
User
User
Posts: 10
Joined: Tue Mar 02, 2010 6:36 pm

Rookie needs help with Treeview and files

Post by pb-user »

Hello to all,

I'm new in PurBasic but have some experience with TurboBasic long time ago :lol:

After extensive search on the forum without success I would like to point out my problem.


Short description on my problem:

I code a software for reading manuals. These are stored in folders and subfolders (pdf-files). I do not want to use the ExplorerTree because the user should only search in the allowed tree-structure on the CD.

What I have done:
I tried to list all folders in a Treeview Gadget (only folders and subfolders should be in this) and on selection on a specific folder another Listview shows the files. (not coded)

Problem in code:
The only problem is that with the code only the first subfolder comes into the treeview. But i need the complete tree structure like in Explorertree. And if I filter all the files I only get the first folder of the tree. :cry:

The code is from some samples I only modified it.

I would be very glad for some hints to solve my problem. :idea:

Code: Select all


DirectoryName.s 

#WindowWidth  = 640
#WindowHeight = 480


LoadImage(0, "img0.png")
LoadImage(1, "img1.png")


Procedure ListFiles(EntryPath.s)

   EntryPath + "\"

  UsedDirectory = ExamineDirectory(#PB_Any, EntryPath, "*.*")

  While NextDirectoryEntry(UsedDirectory)

    EntryType.l = DirectoryEntryType(UsedDirectory)
    EntryName.s = DirectoryEntryName(UsedDirectory)
    
    If EntryName = "." Or EntryName = "..": Continue: EndIf

    If EntryType = #PB_DirectoryEntry_File
       NDir$=EntryPath+EntryName
       AddGadgetItem(2, -1, DirectoryEntryName(UsedDirectory) , ImageID(1),1) 
    EndIf
     
    If EntryType = #PB_DirectoryEntry_Directory
       
       Level=0 ; test to try different levels
       ListFiles(EntryPath + EntryName)
       AddGadgetItem(2, -1, DirectoryEntryName(UsedDirectory) , ImageID(3),Level)
       Level=0
     EndIf 


  Wend: FinishDirectory(UsedDirectory)

EndProcedure


If OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Tree structure", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)


  
  TreeGadget    (2,  10, 50, 550, 400)
    
  ;SetGadgetText(4, "Initialize Ok... Welcome !")
  
  ; Fill Up the Tree gadget with Folders
  
  DirectoryName = PathRequester("Please choose a folder...", "") 
  ListFiles(DirectoryName)


  
  Repeat
    EventID = WaitWindowEvent()

    If EventID = #PB_Event_Gadget
      
      Select EventGadget()
        Case 1
          MessageRequester("Information", "You did it !", 0)
      
        Case 2
          SetGadgetText(4, "Tree Gadget. Item selected: "+Str(GetGadgetState(2)))
          
          If EventType() = 2
            MessageRequester("Information", "Doubleclick: item"+Str(GetGadgetState(2))+", Text: "+GetGadgetText(2), 0)
          ElseIf EventType() = 1
            DisplayPopupMenu(0, WindowID(0))
          EndIf
          
        Case 5
          SetGadgetText(4, "ListIcon Gadget. Item selected: "+Str(GetGadgetState(5)))
          
          If EventType() = 2
            MessageRequester("Information", "Doubleclick: item"+Str(GetGadgetState(5))+", Text: "+GetGadgetText(5), 0)
          ElseIf EventType() = 1
            DisplayPopupMenu(0, WindowID(0))
          EndIf
          
     EndSelect

    EndIf
    
  Until EventID = #PB_Event_CloseWindow
EndIf

End 
Pb-User

Windows 7 - 64bit - PureBasic 4.51
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Rookie needs help with Treeview and files

Post by Trond »

Welcome
First, you need UsePngDecoder() to use png files. I will take a look at the rest of your code.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Rookie needs help with Treeview and files

Post by Trond »

After looking at it for a while, I can't find any SIMPLE and easy-to-understand way to do this. The files and directories are not separated. Still, it works:

Code: Select all

; Put all filenames in Path and subfolders into the list Files()
Procedure ExamineDirectoryRecursive(Path.s, List Files.s())
  If Right(Path, 1) <> "\"
    Path + "\"
  EndIf
  Dir = ExamineDirectory(#PB_Any, Path, "")
  If Dir
    While NextDirectoryEntry(Dir)
      
      Name.s = DirectoryEntryName(Dir)
      
      Select DirectoryEntryType(Dir)
        Case #PB_DirectoryEntry_Directory
          If Name <> "." And Name <> ".."
            AddElement(Files())  ; code to go one level deeper in the tree
            Files() = "+" + Name ; entries starting with + or - is our secret code!
            ExamineDirectoryRecursive(Path + Name + "\", Files())
            AddElement(Files())
            Files() = "-"        ; go left one level
          EndIf
        
        Case #PB_DirectoryEntry_File
          AddElement(Files())
          Files() = Path+Name
      EndSelect
      
    Wend
    FinishDirectory(Dir)

  EndIf
EndProcedure

; Fill in a treeview from a list of filenames
Procedure FilenamesToTreeview(Tree, List Files.s())
  CurrentLevel = 0
  Item = 0
  ForEach Files()
    Select Left(Files(), 1)
      Case "+" ; folder
        AddGadgetItem(Tree, Item, Mid(Files(), 2), ImageID(0), CurrentLevel)
        SetGadgetItemData(Tree, Item, ListIndex(Files()))
        Item + 1
        CurrentLevel + 1
      
      Case "-" ; end folder
        CurrentLevel - 1
      
      Default  ; no folder
        AddGadgetItem(Tree, -1, GetFilePart(Files()), ImageID(1), CurrentLevel)
        SetGadgetItemData(Tree, Item, ListIndex(Files()))
        Item + 1
    EndSelect
    
  Next
EndProcedure

; Load icons
UsePNGImageDecoder()
LoadImage(0, "c:\img1.png")
LoadImage(1, "c:\img2.png")

; Create a list to hold the filenames
NewList Files.s()


OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)

TreeGadget(0, 10, 10, 400, 300)

; Set the path (i didn't want to see the open requester all the time while testing...)
Path.s = "C:\fasm\fasmw"

; Put the filenames in Path.s into the list Files()
ExamineDirectoryRecursive(Path, Files())

; Verify that you found your files OK
; ForEach Files()
;   Debug Files()
; Next

; Add files to tree gadget
FilenamesToTreeview(0, Files())

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          If EventType() = #PB_EventType_LeftClick
            SelectElement(Files(), GetGadgetItemData(0, GetGadgetState(0)))
            Debug Files()
          EndIf
      EndSelect
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

User avatar
pb-user
User
User
Posts: 10
Joined: Tue Mar 02, 2010 6:36 pm

Re: Rookie needs help with Treeview and files

Post by pb-user »

Trond,

I'm absolutly impressed, wow :!:

That is exact what I had in my mind, but you did it :idea:

Thank you for this extremly fast reply and your perfect solution.

:D :D


P.S. I will play a little bit with your code to learn and understand.
Pb-User

Windows 7 - 64bit - PureBasic 4.51
Post Reply