;-- TIME_STAMP   2016-12-27 14:39:10   v 0.1

#include-once
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrl_TreeView2Style_Create
; Description ...: Creates 2 treeview with different styles at the same position, to swap the style on-the-fly
; Syntax ........: _GUICtrl_TreeView2Style_Create($x, $y, $w, $h, $iStyle_1, $iStyle_2[, $iStyleEx_1=-1[, $iStyleEx_2=-1]])
; Parameters ....: $x           x-Position
; ...............: $y           y-Position
; ...............: $w           width
; ...............: $h           height
; ...............: $iStyle_1    1st style
; ...............: $iStyle_2    2nd style
; ...............: $iStyleEx_1  1st extended style
; ...............: $iStyleEx_2  2nd extended style
; Return values .: Array [current ID, ID ctrl style_1, ID ctrl style_2, index current ID]
; Author ........: BugFix ( AutoIt@bug-fix.info )
;================================================================================================================================
Func _GUICtrl_TreeView2Style_Create($x, $y, $w, $h, $iStyle_1, $iStyle_2, $iStyleEx_1=-1, $iStyleEx_2=-1)
	Local $aID[4] = [ _
	0, _                                                             ; current Ctrl ID
	GUICtrlCreateTreeView($x, $y, $w, $h, $iStyle_1, $iStyleEx_1), _ ; with Style 1
	GUICtrlCreateTreeView($x, $y, $w, $h, $iStyle_2, $iStyleEx_2), _ ; with Style 2
	1]                                                               ; current Ctrl Index
	GUICtrlSetState($aID[2], 32)                                     ; $GUI_HIDE Style 2
	$aID[0] = $aID[1]                                                ; assign current Ctrl ID
	Return $aID
EndFunc  ;==>_GUICtrl_TreeView2Style_Create

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrl_TreeView2Style_Swap
; Description ...: Swaps between the two treeview created with _GUICtrl_TreeView2Style_Create()
; Syntax ........: _GUICtrl_TreeView2Style_Swap($aID[, $fReFill=True])
; Parameters ....: $aID         Returned array from _GUICtrl_TreeView2Style_Create()
; ...............: $fReFill     True (Default): copy all item from last active to current active treeview
; Return values .: none
; Author ........: BugFix ( AutoIt@bug-fix.info )
;================================================================================================================================
Func _GUICtrl_TreeView2Style_Swap(ByRef $aID, $fReFill=True)
	Local $fromIndex = $aID[3]
	Local $fromID = $aID[$fromIndex]
	Local $toID = ($fromIndex = 1) ? $aID[2] : $aID[1]
	If $fReFill Then
		Local $aItemOrder = _GUICtrlTreeView_GetItemOrder($fromID, False)
		If Not @error Then
			_GUICtrlTreeView_FillFromOrderArray($toID, $aItemOrder)
		EndIf
	EndIf
	GUICtrlSetState($aID[0], $GUI_HIDE)
	$aID[0] = $aID[(($aID[3] = 1) ? 2 : 1)]
	GUICtrlSetState($aID[0], $GUI_SHOW)
	$aID[3] = ($aID[3] = 1) ? 2 : 1
EndFunc  ;==>_GUICtrl_TreeView2Style_Swap


; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlTreeView_GetItemOrder
; Description ...: Reads all treeview item with a related order-string into a string (Default) or array
; ...............: The order string corresponds to the number of item occurrences in its level
; ...............: The first item in level 1 gets "1" (the root), its siblings gets "2", "3" and so on
; ...............: Children from item "1" gets "1.1", "1.2" ... "1.n"
; ...............: Children from item "2" gets "2.n+1", "2.n+2" ... and so also for all level
; Syntax ........: _GUICtrlTreeView_GetItemOrder($_hWnd[, $_bString = True[, $_sDelim = -1]])
; Parameters ....: $_hWnd       Treeview handle
; ...............: $_bString    1 (Default): Output as string "order-number DELIMITER item-text CRLF", 0 returns an array
; ...............: $_sDelim     -1 (Default): Char from Opt(GUIDataSeparatorChar)
; Return values .: Success      String to store item with their order to file or
; ...............:              Array [[item-handle, order-number, item-text]]
; Author ........: BugFix ( AutoIt@bug-fix.info )
;================================================================================================================================
Func _GUICtrlTreeView_GetItemOrder($_hWnd, $_bString=True, $_sDelim=-1)
	If $_sDelim = -1 Then $_sDelim = Opt("GUIDataSeparatorChar")
	Local $iCount = _GUICtrlTreeView_GetCount($_hWnd)
	If $iCount = 0 Then Return SetError(1,0,0)
	Local $hItem = _GUICtrlTreeView_GetFirstItem($_hWnd)
	Local $aItem[$iCount][3] = [[$hItem, '1', _GUICtrlTreeView_GetText($_hWnd, $hItem)]]
	__GetItemArray($_hWnd, $hItem, $aItem)
	If Not $_bString Then Return $aItem
	Local $sRet = ''
	For $i = 0 To UBound($aItem) -1
		$sRet &= $aItem[$i][1] & $_sDelim & $aItem[$i][2] & @CRLF
	Next
	Return $sRet
EndFunc  ;==>_GUICtrlTreeView_GetItemOrder

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlTreeView_FillFromOrderArray
; Description ...: Creates treeview item related to the stored order
; ...............: The order string corresponds to the number of item occurrences in its level
; ...............: The first item in level 1 has "1" (the root), its siblings has "2", "3" and so on
; ...............: Children from item "1" has "1.1", "1.2" ... "1.n"
; ...............: Children from item "2" has "2.n+1", "2.n+2" ... and so also for all level
; Syntax ........: _GUICtrlTreeView_FillFromOrderArray($_hWnd, $_aItem, [$_fDelete=True])
; Parameters ....: $_hWnd       Treeview handle
; ...............: $_aItem      Return from _GUICtrlTreeView_GetItemOrder()
; ...............: $_fDelete    True (Default): Deletes all Item before filling
; Return values .: none
; Author ........: BugFix ( AutoIt@bug-fix.info )
;================================================================================================================================
Func _GUICtrlTreeView_FillFromOrderArray($_hWnd, $_aItem, $_fDelete=True)
	Local $n = 1, $aTree[UBound($_aItem)+1][2] = [[UBound($_aItem)]]  ; == [[Handle-Item, sOrder]]
	Local $sOrderParent
	_GUICtrlTreeView_BeginUpdate($_hWnd)
	If $_fDelete Then _GUICtrlTreeView_DeleteAll($_hWnd)
	For $i = 0 To UBound($_aItem) -1
		$sOrderParent = __GetParentOrderNumber($_aItem[$i][1])
		Switch $sOrderParent
			Case ''
				$aTree[$n][0] = _GUICtrlTreeView_Add($_hWnd, 0, $_aItem[$i][2])
			Case Else
				For $j = 1 To $n -1
					If $aTree[$j][1] = $sOrderParent Then
						$aTree[$n][0] = _GUICtrlTreeView_AddChild($_hWnd, $aTree[$j][0], $_aItem[$i][2])
						ExitLoop
					EndIf
				Next
		EndSwitch
		$aTree[$n][1] = $_aItem[$i][1]     ; == sOrder
		$n += 1
	Next
	_GUICtrlTreeView_EndUpdate($_hWnd)
EndFunc  ;==>_GUICtrlTreeView_FillFromOrderArray

Func __GetItemArray($_hWnd, $_hParent, ByRef $_aItem)
	Local $aLevel[256] = [1], $iIndex = 1
	Local $hItem = _GUICtrlTreeView_GetNext($_hWnd, $_hParent)
	While $hItem <> 0
		$_aItem[$iIndex][0] = $hItem
		$_aItem[$iIndex][1] = __OrderNumber($aLevel, _GUICtrlTreeView_Level($_hWnd, $hItem))
		$_aItem[$iIndex][2] = _GUICtrlTreeView_GetText($_hWnd, $hItem)
		$iIndex += 1
		$hItem = _GUICtrlTreeView_GetNext($_hWnd, $hItem)
	WEnd
EndFunc  ;==>__GetItemArray

Func __OrderNumber(ByRef $_aLevel, $_iLevel)
	$_aLevel[$_iLevel] += 1
	Local $sOrder = ''
	For $i = 0 To $_iLevel
		$sOrder &= $_aLevel[$i] & '.'
	Next
	Return StringTrimRight($sOrder, 1)
EndFunc  ;==>__OrderNumber

Func __GetParentOrderNumber($_sOrder)
	If $_sOrder = '1' Then Return ''
	Local $iPos = StringInStr($_sOrder, '.', 1, -1)
	Return StringLeft($_sOrder, $iPos -1)
EndFunc  ;==>__GetParentOrderNumber