Advertisement
stjernan

xD

Feb 9th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.29 KB | None | 0 0
  1. #include <winapi.au3>
  2. #include <winapishellex.au3>
  3. #include <File.au3>
  4. #include <_Array.au3>
  5. #include <WindowsConstants.au3>
  6. #include <GUIConstantsEx.au3>
  7. #include <GuiImageList.au3>
  8. #include <GUITreeview.au3>
  9. #include <WinAPIFiles.au3>
  10. ;Opt("GUIOnEventMode", 1); DOsent work bcuz _treeview -.-
  11.  
  12.  
  13. Local $_getSciteDir = _getSciteDir()
  14. Local $_GetTrashCan = @ScriptDir & "\___TrashCan"
  15.  
  16.  
  17. Local $SciteWidth = 1024, $SciteHeight = 768, $LastSciteWidth, $LastSciteHeight
  18.  
  19. Local $hTreeView, $hTreeClickType
  20. Local $hGUI = GUICreate("Test", $SciteWidth, $SciteHeight, -1, -1, $WS_SIZEBOX + $WS_MINIMIZEBOX + $WS_MAXIMIZEBOX, $WS_EX_ACCEPTFILES)
  21. GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY"); Keep rightclick on same ID
  22.  
  23.  
  24.  
  25. Local $hTreeView = GUICtrlCreateTreeView(0, 0, $SciteWidth, $SciteHeight); _guictrltreeview gör någon konstig bug
  26. GUICtrlSetResizing($hTreeView , $GUI_DOCKAUTO)
  27.  
  28. Local $sRoot = "C:\Autoit\Livia Spelmotor"
  29. Local $hContextMenu = GUICtrlCreateContextMenu($hTreeView)
  30. Local $hContextItem_renameFile = GUICtrlCreateMenuItem("Test", $hContextMenu)
  31. Local $hContextItem_deleteFile = GUICtrlCreateMenuItem("Remove", $hContextMenu)
  32.  
  33. Local $hItems = _GuictrlTreeview__FileListToArrayRec($hTreeView, $sRoot, ".au3", "BackUp;.git")
  34.  
  35.  
  36.  
  37. ;Set same colours as Scite SciTEUser SciTEUserSciTEUserSciTEUserSciTEUserSciTEUser
  38. ;_GUICtrlTreeView_SetIcon
  39. ;_GUICtrlTreeView_SetBkColor($hTreeView, 0x272822)
  40. ;GUICtrlSetColor($hTreeView, 0xFFFFFF)
  41.  
  42. GUISetState()
  43.  
  44. Local $CurClickEvent = Null
  45. While 1
  46.  
  47. Switch GUIGetMsg()
  48. Case $GUI_EVENT_CLOSE, $idOK
  49. ExitLoop
  50. Case $hContextItem_renameFile
  51. _App_EditFileName()
  52. Case $hContextItem_deleteFile
  53. _App_DeleteFile()
  54. EndSwitch
  55.  
  56. ;Specialevent for "double click
  57. If $CurClickEvent == $NM_DBLCLK Then
  58. ;_App_DeleteFile()
  59. Local $Get = _GetSelectedTreeViewItem()
  60.  
  61.  
  62. Local $CallBack = _SciteGOTO($Get[2])
  63.  
  64. ConsoleWrite("#"&$CallBack&@CRLF)
  65.  
  66.  
  67.  
  68. ;TEST
  69. $CurClickEvent= Null
  70. EndIf
  71.  
  72. #cs
  73. $wSize = WinGetClientSize($hGUI)
  74.  
  75. If $LastSciteWidth <> $wSize[0] Then
  76. $LastSciteWidth = $wSize[0]
  77. _WinAPI_MoveWindow($h_HWND, $hTreeWidth, 0, $LastSciteWidth - $hTreeWidth, $LastSciteHeight, True)
  78. EndIf
  79.  
  80. If $LastSciteHeight <> $wSize[1] Then
  81. $LastSciteHeight = $wSize[1]
  82. _WinAPI_MoveWindow($h_HWND, $hTreeWidth, 0, $LastSciteWidth - $hTreeWidth, $LastSciteHeight, True)
  83. EndIf
  84. #ce
  85. WEnd
  86.  
  87. Func _SciteGOTO($s_au3, $iLine = 1, $s_scite = Default)
  88.  
  89. If Not FileExists($s_au3) Then Return SetError(1, 0, "")
  90.  
  91. If $iLine < 1 Then Return SetError(2, 0, "")
  92.  
  93. If $s_scite = Default Then
  94. $s_scite = $_getSciteDir & "SciTE.exe"
  95. EndIf
  96.  
  97. If Not FileExists($s_scite) Then Return SetError(3, 0, "")
  98.  
  99. $s_au3 = StringReplace($s_au3, "\", "\\")
  100.  
  101. Run($s_scite & ' "-open:' & $s_au3 & '" -goto:' & $iLine)
  102. If @error Then Return SetError(4, 0, "")
  103.  
  104. Return True
  105. EndFunc ;==>_SciteGOTO
  106.  
  107.  
  108. Func _App_DeleteFile()
  109. Local $aData = _GetSelectedTreeViewItem(); 0 hwnd, 1 id 2 Full file path
  110. Local $hItem = $aData[0]
  111. Local $aListID = $aData[1]
  112. Local $originalFilePath = $aData[2]
  113. Local $ShortName = _Misc_GetFileName($originalFilePath, False)
  114. Local $ErrText = ""
  115. Local $Success = True
  116.  
  117. Local $sFileType = FileGetAttrib($originalFilePath)
  118.  
  119.  
  120. ; Move the original to our fake trashcan (User has to delete the files himself)
  121. If $sFileType == "D" Then
  122. Local $TrashFileName = StringFormat("%s\%s_%s", $_GetTrashCan, $ShortName, @HOUR & @MIN & @MSEC)
  123. Local $MsgBox = MsgBox(4, "", StringFormat("Are you sure you want to delete this DIRECTORY '%s'?", $ShortName))
  124. Else
  125. Local $TrashFileName = StringFormat("%s\%s_%s.deleted", $_GetTrashCan, $ShortName, @HOUR & @MIN & @MSEC)
  126. Local $MsgBox = MsgBox(4, "", StringFormat("Are you sure you want to delete this file '%s' ?", $ShortName))
  127. EndIf
  128.  
  129. If $MsgBox <> 6 Then Return False
  130.  
  131.  
  132. If $sFileType == "D" Then
  133.  
  134. ; Remove parent from hItems
  135. _Array_Remove($hItems, $aListID)
  136.  
  137. Local $aFileList = _FileListToArrayRec($originalFilePath, "*.au3", $FLTAR_FILESFOLDERS, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
  138. ; Remove children of parents hItems (If there is any)
  139.  
  140. If IsArray($aFileList) Then
  141. Do
  142. Local $Complete = True
  143. For $i = 1 To $hItems[0] - 1
  144.  
  145. For $j = 1 To $aFileList[0]
  146. If $hItems[$i] == $aFileList[$j] Then
  147. _Array_Remove($hItems, $i)
  148. $Complete = False
  149. ExitLoop
  150. EndIf
  151.  
  152. If Not $Complete Then ExitLoop
  153.  
  154. Next
  155. Next
  156. Until $Complete == True
  157. EndIf
  158.  
  159. ;Move the entire directory
  160. $dir_copy1 = DirCopy($originalFilePath, $TrashFileName)
  161. if $dir_copy1 Then
  162. DirRemove($originalFilePath, 1)
  163. Else
  164. $ErrText &= StringFormat("Failed to copy file from '%s' => '%s'", $originalFilePath, $TrashFileName) & @CRLF
  165. $Success = False
  166. EndIf
  167.  
  168. Else
  169. Local $copy_2 = FileCopy($originalFilePath, $TrashFileName)
  170.  
  171. If Not $copy_2 Then
  172. $ErrText &= StringFormat("Failed to copy file from '%s' => '%s'", $originalFilePath, $TrashFileName) & @CRLF
  173. $Success = False
  174. EndIf
  175.  
  176. ;Delete the original
  177. Local $delete_1 = FileDelete($originalFilePath)
  178.  
  179. If Not $delete_1 Then
  180. $ErrText &= StringFormat("Failed to delete original file '%s' ", $originalFilePath) & @CRLF
  181. $Success = False
  182. EndIf
  183. EndIf
  184.  
  185. If Not $Success Then Return MsgBox(0, "Failed to delete the file.", $ErrText)
  186.  
  187. _GUICtrlTreeView_Delete($hTreeView, $hItem)
  188.  
  189. EndFunc ;==>_App_DeleteFile
  190.  
  191. Func _App_EditFileName()
  192. Local $aData = _GetSelectedTreeViewItem(); 0 hwnd, 1 id 2 Full file path
  193. Local $hItem = $aData[0]
  194. Local $aListID = $aData[1]
  195. Local $originalFilePath = $aData[2]
  196.  
  197. Local $ShortName = _Misc_GetFileName($originalFilePath)
  198. Local $Success = True
  199. Local $ErrText = ""
  200.  
  201. If _WinAPI_FileInUse($originalFilePath) Then
  202. $ErrText &= StringFormat("The file '%s' is currently in use by another program for read\writing", $ShortName) & @CRLF
  203. $Success = False
  204. EndIf
  205.  
  206. Do
  207. Local $NewFileName = InputBox("New name", StringFormat("Rename %s", $ShortName), $ShortName, "", -1, 130)
  208. ;If user aborts
  209. If $NewFileName == "" Or $NewFileName == $ShortName Then Return False
  210.  
  211. If StringRight($NewFileName, 4) <> ".au3" Then
  212. $ErrText &= StringFormat("The file '%s' has an invalid file-extension", $NewFileName) & @CRLF
  213. $Success = False
  214. Else
  215. $Success = True
  216. EndIf
  217.  
  218. ;Get the new full file name
  219. Local $aTmp = StringSplit($originalFilePath, "\")
  220. $aTmp[$aTmp[0]] = $NewFileName
  221. _ArrayDelete($aTmp, 0)
  222. Local $NewFilePath = _ArrayToString($aTmp, "\")
  223.  
  224. ; check if new file exists
  225. If FileExists($NewFilePath) Then
  226. $ErrText &= StringFormat("The file '%s' already exists..", $NewFileName) & @CRLF
  227. $Success = False
  228. EndIf
  229.  
  230. If Not FileExists($originalFilePath) Then
  231. $ErrText &= StringFormat("The file '%s' you are trying to change dosent exist anymore... ", $originalFilePath) & @CRLF
  232. $Success = False
  233. EndIf
  234.  
  235. If Not $Success Then MsgBox(0, "Failed to change filename", $ErrText)
  236.  
  237. Until $Success == True
  238.  
  239. ; Copy the file with a new name
  240. Local $copy_1 = FileCopy($originalFilePath, $NewFilePath)
  241.  
  242. If Not $copy_1 Then
  243. $ErrText &= StringFormat("Failed to rename file from '%s' => '%s'", $originalFilePath, $NewFilePath) & @CRLF
  244. $Success = False
  245. EndIf
  246.  
  247. ; Move the original to our fake trashcan (User has to delete the files himself)
  248. Local $TrashFileName = StringFormat("%s\%s_%s.deleted", $_GetTrashCan, $ShortName, @HOUR & @MIN & @MSEC)
  249. Local $copy_2 = FileCopy($originalFilePath, $TrashFileName)
  250.  
  251. If Not $copy_2 Then
  252. $ErrText &= StringFormat("Failed to copy file from '%s' => '%s'", $originalFilePath, $TrashFileName) & @CRLF
  253. $Success = False
  254. EndIf
  255.  
  256. ;Delete the original
  257. Local $delete_1 = FileDelete($originalFilePath)
  258.  
  259. If Not $delete_1 Then
  260. $ErrText &= StringFormat("Failed to delete original file '%s' ", $originalFilePath) & @CRLF
  261. $Success = False
  262. EndIf
  263.  
  264. If Not $Success Then Return MsgBox(0, "Failed to change filename, it exist some damage.", $ErrText)
  265.  
  266. ;Visually change the name
  267. _GUICtrlTreeView_SetText($hTreeView, $aData[0], _Misc_GetFileName($NewFileName, False))
  268.  
  269. ;Updates the Actual list
  270. $hItems[$aListID] = $NewFilePath
  271.  
  272. EndFunc ;==>_App_EditFileName
  273.  
  274. Func _GetSelectedTreeViewItem(); 0 hItem, 1 Index, 2 Full file path
  275. $iIndex = 0
  276. Local $aRet[3]
  277.  
  278. $hItem = _GUICtrlTreeView_GetFirstItem($hTreeView)
  279.  
  280. For $i = 0 To _GUICtrlTreeView_GetCount($hTreeView) - 1
  281. If _GUICtrlTreeView_GetSelected($hTreeView, $hItem) Then
  282. $iIndex = $i + 1
  283. ExitLoop
  284. EndIf
  285. $hItem = _GUICtrlTreeView_GetNext($hTreeView, $hItem)
  286. Next
  287.  
  288. $aRet[0] = $hItem
  289. $aRet[1] = $iIndex
  290. $aRet[2] = $hItems[$iIndex]
  291.  
  292. Return $aRet
  293. EndFunc ;==>_GetSelectedTreeViewItem
  294.  
  295. Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  296. #forceref $hWnd, $iMsg, $wParam
  297. $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
  298. $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
  299. $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
  300. $iCode = DllStructGetData($tNMHDR, "Code")
  301. $CurClickEvent = $iCode
  302.  
  303. Switch $iIDFrom
  304. Case $hTreeView
  305. Switch $iCode
  306. Case $NM_DBLCLK
  307.  
  308. $CurClickEvent = $iCode
  309.  
  310. Case $NM_RCLICK
  311. Local $tPoint = _WinAPI_GetMousePos(True, $hWndFrom), $tHitTest
  312. $tHitTest = _GUICtrlTreeView_HitTestEx($hWndFrom, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2))
  313. If BitAND(DllStructGetData($tHitTest, "Flags"), $TVHT_ONITEM) Then
  314. _GUICtrlTreeView_SelectItem($hWndFrom, DllStructGetData($tHitTest, 'Item'))
  315. EndIf
  316.  
  317. Case $TVN_ENDLABELEDITA, $TVN_ENDLABELEDITW
  318. Local $tInfo = DllStructCreate($tagNMHDR & ";" & $tagTVITEMEX, $lParam)
  319. If DllStructGetData($tInfo, "Text") <> 0 Then
  320. Local $tBuffer = DllStructCreate("char Text[" & DllStructGetData($tInfo, "TextMax") & "]", DllStructGetData($tInfo, "Text"))
  321. _GUICtrlTreeView_SetText($hTreeView, _GUICtrlTreeView_GetSelection($hTreeView), DllStructGetData($tBuffer, "Text"))
  322. EndIf
  323. EndSwitch
  324. EndSwitch
  325. Return $GUI_RUNDEFMSG
  326. EndFunc ;==>WM_NOTIFY
  327.  
  328. Func _getSciteDir()
  329. Local $s_AutoIt_InstallPath = RegRead('HKLM\SOFTWARE\AutoIt v3\Autoit', 'InstallDir')
  330. Local $s_Scite_InstallPath = StringFormat("%s\%s\", $s_AutoIt_InstallPath, "SciTE")
  331. Return $s_Scite_InstallPath
  332. EndFunc ;==>_getSciteDir
  333.  
  334. Func _Misc_GetFileName($sFilePath, $KeepExtension = True)
  335. ; Gets the filename
  336. Local $StringSPlit = StringSplit($sFilePath, "\")
  337.  
  338. If Not $KeepExtension Then Return StringRegExpReplace($StringSPlit[$StringSPlit[0]], "\.(.*?)[a-zA-Z1234567890_-]{1,}+", "")
  339. Return $StringSPlit[$StringSPlit[0]]
  340. EndFunc ;==>_Misc_GetFileName
  341.  
  342.  
  343.  
  344. Func _GuictrlTreeview__FileListToArrayRec($hTreeView, $sRoot, $sAcceptedFiles = ".", $sExcludedFolders = "")
  345. Local $aReturn[1];All files stored here in 1D array
  346. Local $AcceptedFiles = StringSplit($sAcceptedFiles, ";")
  347. Local $ExcludedFolders = StringSplit($sExcludedFolders, ";")
  348. $FileListToArray = _FileListToSortedArray($sRoot, $AcceptedFiles, $ExcludedFolders)
  349.  
  350. ; Dummy icon control.
  351. Local $hImageList = _GUIImageList_Create(16, 16, 5, 1)
  352. _GUICtrlTreeView_SetNormalImageList($hTreeView, $hImageList)
  353.  
  354. _GUICtrlTreeView_BeginUpdate($hTreeView)
  355. _SortedArrayToTreeView($aReturn, $FileListToArray, 0, $hImageList)
  356. _GUICtrlTreeView_EndUpdate($hTreeView)
  357. Return $aReturn
  358. EndFunc ;==>_GuictrlTreeview__FileListToArrayRec
  359.  
  360.  
  361. Func _SortedArrayToTreeView(ByRef $aReturn, ByRef $sSourceFolder, $hItem, $hImageList)
  362. Local $sCurFileName = "unkown :P", $aFileList, $newItem, $hIcon, $aCurFileList, $hIcon_Index
  363.  
  364.  
  365. For $i = 2 To $sSourceFolder[0]; Last arg of array push lets us start from 2 instead so we can save 2 for stuff
  366. ; Current "file array"
  367. $aFileList = $sSourceFolder[$i]
  368.  
  369. ; Get Type
  370. If IsArray($aFileList) Then
  371. $aCurFileList = $aFileList[1]
  372. _Array_Push($aReturn, $aCurFileList)
  373. Else
  374. $aCurFileList = $aFileList
  375. _Array_Push($aReturn, $aCurFileList)
  376. EndIf
  377.  
  378. ; Get short name
  379. $sCurFileName = _________GetFileName($aCurFileList)
  380.  
  381. ; Get icon of file
  382. $hIcon = _WinAPI_ShellExtractAssociatedIcon($aCurFileList, 1)
  383. $hIcon_Index = _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon)
  384. _WinAPI_DestroyIcon($hIcon)
  385.  
  386.  
  387. ; Check if "Folder"
  388. If IsArray($aFileList) Then
  389. ;Ghetto get the folder name
  390.  
  391. $newItem = _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sCurFileName, $hIcon_Index, $hIcon_Index)
  392. _SortedArrayToTreeView($aReturn, $aFileList, $newItem, $hImageList)
  393.  
  394. Else
  395. ;(THIS WILL IGNORE EMPTY DIRECTORYS) SEARCH FOR THIS
  396. If Not $sCurFileName Then
  397.  
  398. Else; Regular file
  399. _GUICtrlTreeView_AddChild($hTreeView, $hItem, $sCurFileName, $hIcon_Index, $hIcon_Index)
  400. EndIf
  401. EndIf
  402.  
  403.  
  404. Next
  405.  
  406.  
  407. EndFunc ;==>_SortedArrayToTreeView
  408.  
  409.  
  410. ;// Fuck yeah...
  411. Func _FileListToSortedArray($sSourceFolder, $AcceptedFiles, $ExcludedFolders)
  412. Local $newTree[1]
  413.  
  414. Local $sFile, $sCurFullFilePath, $DeniedFolder, $DeniedFile
  415.  
  416. ; Force a trailing \
  417. If StringRight($sSourceFolder, 1) <> "\" Then $sSourceFolder &= "\"
  418.  
  419. ; Add basedir to array
  420. _Array_Push($newTree, $sSourceFolder)
  421.  
  422. ; Start the search
  423. Local $hSearch = FileFindFirstFile($sSourceFolder & "*.*")
  424. ; If no files found then return
  425. If $hSearch = -1 Then Return ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<<
  426.  
  427. ; Now run through the contents of the folder
  428. While 1
  429. ; reset
  430. $DeniedFolder = False
  431. $DeniedFile = False
  432. ; Get next match
  433. $sFile = FileFindNextFile($hSearch)
  434. $sCurFullFilePath = $sSourceFolder & $sFile
  435.  
  436. If @error Then ExitLoop ; This is where we break the recursive loop <<<<<<<<<<<<<<<<<<<<<<<<<<
  437.  
  438. ; Check if a folder
  439. If @extended Then
  440. ; If so then call the function recursively
  441.  
  442. ;Check if exluced folder
  443. For $i = 1 To $ExcludedFolders[0]
  444. If StringInStr($sFile, $ExcludedFolders[$i]) Then $DeniedFolder = True
  445. Next
  446.  
  447. If $DeniedFolder Then ContinueLoop
  448.  
  449. ;Fix empty dirs (THIS WILL IGNORE EMPTY DIRECTORYS) SEARCH FOR THIS
  450. Local $try = _FileListToSortedArray($sCurFullFilePath, $AcceptedFiles, $ExcludedFolders)
  451. If IsArray($try) Then
  452. _Array_Push($newTree, $try)
  453. EndIf
  454.  
  455. Else
  456. ; If a file than write path and name
  457. For $i = 1 To $AcceptedFiles[0]
  458. If Not StringInStr($sFile, $AcceptedFiles[$i]) Then $DeniedFile = True
  459. Next
  460. If $DeniedFile Then ContinueLoop
  461. _Array_Push($newTree, $sCurFullFilePath)
  462. EndIf
  463. WEnd
  464.  
  465. ;Sort this nigga
  466. _ArraySort($newTree, 0, 2)
  467. Return $newTree
  468.  
  469. EndFunc ;==>_FileListToSortedArray
  470.  
  471.  
  472. Func _________GetFileName($sFilePath)
  473. If Not StringInStr($sFilePath, "\") Then Return False; $sFilePath (THIS WILL IGNORE EMPTY DIRECTORYS) SEARCH FOR THIS
  474. Local $StringSplit = StringSplit($sFilePath, "\")
  475. If StringRegExp($StringSplit[$StringSplit[0]], "\.(.*?)[a-zA-Z1234567890_-]{1,}+") Then Return $StringSplit[$StringSplit[0]]
  476. Return $StringSplit[$StringSplit[0] - 1]
  477. EndFunc ;==>_Misc_GetFileName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement