Page 1 of 1

Refreshing ExplorerTreeGadget and preserve expanded nodes?

Posted: Sun Oct 21, 2007 7:57 pm
by eJan
Hi!, i have found the way how to refresh ExplorerTreeGadget: http://www.purebasic.fr/english/viewtopic.php?t=24711
I have tried my poor method, but how to preserve expanded nodes? I need it to user can see which files/folders have been created/deleted.
Maybe some API trick can do the job. Please help!

Code: Select all

OpenWindow(1, 0, 0, 290, 370, "ExplorerTreeGadget Refresh", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered) 

CreateGadgetList(WindowID(1)) 
ExplorerTreeGadget(1, 10, 10, 270, 300, "") 
ButtonGadget(2, 45, 320, 200, 40, "Refresh") 

Repeat 
  WindowEventID = WaitWindowEvent() 

  If WindowEventID = #PB_Event_Gadget And EventGadget() = 2
    ExplorerTreeGadget(1, 10, 10, 270, 300, "")
  EndIf
   
Until WindowEventID = #PB_Event_CloseWindow

Posted: Tue Oct 23, 2007 2:45 pm
by Shardik
This problem is a rather tricky one. For an ExplorerTreeGadget the function GetGadgetText() returns only the currently highlighted folder name. If you expand or collapse a folder by clicking on [+] or [-], GetGadgetText() doesn't report this action. Instead it always returns the currently highlighted directory path or an empty string. I therefore programmed a callback that detects the expand or collapse action and highlights the folder name item behind the [+] or [-]. (If you want to remove this highlighting afterwards you can remove the comment before the SendMessage_() line.) The callback writes the current directory path into the Shared variable CurrentDirPath, so that SetGadgetText(#ExplorerTree, CurrentDirPath + "*.*") can always refresh the last highlighted folder. If the last action was a left click on a directory thus highlighting it, this directory path is used.

The example program creates the empty file Empty.Txt in the currently highlighted directory after a click on the Create File button and refreshes its contents, so that you can see the newly created file:

Code: Select all

Enumeration 
  #ExplorerTree
  #Button
EndEnumeration

Procedure ExplorerTreeCallback(hWnd, Msg, wParam, lParam)
  Shared CurrentDirPath.S

  If Msg = #WM_NOTIFY 
    *NMH.NMHDR = lParam 
    *NMTV.NM_TREEVIEW = lParam

    If *NMH\hwndFrom = GadgetID(#ExplorerTree)
      If *NMH\code = #TVN_ITEMEXPANDING
        If *NMTV\action = #TVE_COLLAPSE
          SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, *NMTV\itemNew\hItem)
          CurrentDirPath = GetGadgetText(#ExplorerTree)
          Debug CurrentDirPath
;           SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, 0)
        ElseIf *NMTV\action = #TVE_EXPAND
          SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, *NMTV\itemNew\hItem)
          CurrentDirPath = GetGadgetText(#ExplorerTree)
;           SendMessage_(GadgetID(#ExplorerTree), #TVM_SELECTITEM, #TVGN_CARET, 0)
          Debug CurrentDirPath
        EndIf
      ElseIf *NMH\code = #TVN_SELCHANGED
        CurrentDirPath = GetGadgetText(#ExplorerTree)
        Debug CurrentDirPath
      EndIf 
    EndIf
  EndIf

  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure


OpenWindow(1, 0, 0, 290, 300, "Refresh ExplorerTreeGadget", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

CreateGadgetList(WindowID(1))
ExplorerTreeGadget(#ExplorerTree, 10, 10, 270, 230, "")
ButtonGadget(#Button, 45, 250, 200, 40, "Create new file 'Empty.Txt' in current highlighted Dir and refresh ExplorerTree", #PB_Button_MultiLine | #PB_Button_Default) 

SetWindowCallback(@ExplorerTreeCallback())

Repeat
  WindowEvent = WaitWindowEvent()

  If CurrentDirPath = ""
    If ButtonDisabled = #False
      DisableGadget(#Button, #True)
      ButtonDisabled = #True
    EndIf
  Else
    If ButtonDisabled = #True
      DisableGadget(#Button, #False)
      ButtonDisabled = #False
    EndIf
  EndIf  

  If WindowEvent = #PB_Event_Gadget And EventGadget() = #Button
    If CreateFile(1, CurrentDirPath + "Empty.Txt") = 0
      MessageRequester("Error", "Creation of " + CurrentDirPath + "'Empty.Txt' failed!", #MB_ICONERROR)
      End
    EndIf

    CloseFile(1)
    SetGadgetText(#ExplorerTree, CurrentDirPath + "*.*")
    Debug "ExplorerTree refreshed"
  EndIf
Until WindowEvent = #PB_Event_CloseWindow

Posted: Sun Oct 28, 2007 5:03 pm
by eJan
Thanks Shardik!