Treeview Crashes.

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Inner.

;=============== CUT HERE ==============

#WindowWidth = 325
#WindowHeight = 580
;-----------------------------------------------------------------------------
;
; Scan Files
;
;-----------------------------------------------------------------------------
Procedure FileScan(gadget,path$)
If ExamineDirectory(1024, path$, "*.*")
Repeat
FileType = NextDirectoryEntry()
FileName$ = DirectoryEntryName()
If FileType = 1
AddGadgetItem(gadget,0,FileName$)
EndIf
Until FileType=0
EndIf
EndProcedure
;-----------------------------------------------------------------------------
;
; Scan Folders Recursive, and obtain file tree
;
;-----------------------------------------------------------------------------
Procedure Recursive_FileScan(gadget,path$,dir_num)
Debug(dir_num)
If ExamineDirectory(dir_num, path$, "*.*")
Repeat
FileType = NextDirectoryEntry()
FileName$ = DirectoryEntryName()
If FileType = 2
If FileName$ "." And FileName$ ".."
fullpath$=path$+FileName$+"\"
If(dir_num=0) : CloseTreeGadgetNode(gadget) : EndIf
AddGadgetItem(gadget,0,FileName$+Str(dir_num))
OpenTreeGadgetNode(gadget)
FileScan(gadget,fullpath$)
If(dir_num=>1) : CloseTreeGadgetNode(gadget) : EndIf
Recursive_FileScan(gadget,fullpath$,dir_num+1)
UseDirectory(dir_num)
EndIf
EndIf
Until FileType=0
EndIf
EndProcedure
;-----------------------------------------------------------------------------
;
; Wrapper Procedure
;
;-----------------------------------------------------------------------------
Procedure ScanToTreeView(gadget,path$)
FileScan(gadget,path$)
Recursive_FileScan(gadget,path$,0)
EndProcedure

If OpenWindow(0, 100, 120, #WindowWidth, #WindowHeight, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget, "PB - Organizer")

If CreateGadgetList(WindowID())
Text$ = "First Item"
StringGadget(1, 10, 20, 300, 100, Text$ )
tv_view=TreeGadget (2, 10, 150, 300, 400, #PB_Tree_AlwaysShowSelection )
ButtonGadget(3, 40, 125, 80, 20, "Add Item")
ButtonGadget(4, 120, 125, 80, 20, "Insert Item")
ButtonGadget(5, 200, 125, 80, 20, "Add Group")
ScanToTreeView(2,"D:\IT\")
EndIf

Repeat
EventID = WaitWindowEvent()
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 2
Case 3
Case 4
Case 5
EndSelect
EndIf

Until EventID = #PB_EventCloseWindow
EndIf

End
;=============== END CUT HERE ==============

if you change this line from " If(dir_num=>1) : CloseTreeGadgetNode(gadget) : EndIf "
to " If(dir_num=>8) : CloseTreeGadgetNode(gadget) : EndIf "

it will crash, it's also not going beond 2 sub trees deep for some odd reason.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Inner.

I give up it can't be done; there isn't a way in this language to copy a (hard drive file table)[explorer style] to a list tree in this language it seams. :(
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Don't give up that easily, it's just that your recursive routine is crappy :wink:, you shouldn't use the FileScan() procedure cause that's what makes your routine build a tree only two levels deep.

If you replace your stuff with this procedure it should work ok, it does for me at least:

Code: Select all

Procedure GetFilesRecursive(gadget.l, depth.l, path.s)
  If ExamineDirectory(depth, path, "")
    Repeat
      result.l = NextDirectoryEntry()
      If result = 1
        name.s = DirectoryEntryName()
        AddGadgetItem(gadget, 0, name)
      ElseIf result = 2
        name = DirectoryEntryName()
        If name  "." And name  ".."
          AddGadgetItem(gadget, 0, name+Str(depth))
          OpenTreeGadgetNode(gadget)
          GetFilesRecursive(gadget, depth+1, path+name+"\")
          CloseTreeGadgetNode(gadget)
          UseDirectory(depth)
        EndIf
      EndIf
    Until result = 0
  EndIf
EndProcedure
If i'm not misstaken someone has already posted something like this before, so a search might uncover some interesting stuff too...

EDIT: Corrected a stupid misstake that i made..
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Inner.

I need many searchs :) including doing research on how it's done in other languages other than C which I already had source for .. it works thanks :) {{ hugs }}

Procedure GetFilesRecursive(gadget.l, depth.l, path.s)
If ExamineDirectory(depth, path, "")
Repeat
result.l = NextDirectoryEntry()
If result = 1
name.s = DirectoryEntryName()
AddGadgetItem(gadget, 0, name)
ElseIf result = 2
name = DirectoryEntryName()
If name "." And name ".."
AddGadgetItem(gadget, 0, name+Str(depth))
OpenTreeGadgetNode(gadget)
;
; slight error :) or maybe you put that in to test me :)
; GetFilesRecursiveNew(gadget, depth+1, path+name+"\")
;
GetFilesRecursive(gadget, depth+1, path+name+"\")
CloseTreeGadgetNode(gadget)
UseDirectory(depth)
EndIf
EndIf
Until result = 0
EndIf
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

>;
>; slight error :) or maybe you put that in to test me :)
>; GetFilesRecursiveNew(gadget, depth+1, path+name+"\")
>;

Really sorry about that, non-intentional error on my side :wink: A simple copy and paste error, with some manual editing, to make it look a little better, that went wrong...
Post Reply