#include ONCE "window9.bi"

private function CreateItem(treeview as any ptr , iCurentPos as integer, iEndPos as integer, iParent as integer) as integer
	
	dim as GtkTreeIter itercur , iterend  , iterfirst , iter , iterparent

	dim as any ptr model = gtk_tree_view_get_model(treeview)	
	
	dim as Hbitmap pBitmap , pBitmapSel
	
	dim as zstring ptr pszTempString
	
	dim as string sText
	
	if gtk_tree_model_get_iter_first(model , @iterfirst) then
		
		itercur.stamp =  iterfirst.stamp
		
		iterend.stamp =  iterfirst.stamp 
		
		itercur.user_data = cast(any ptr,iCurentPos)
		
		if iParent then
	
			iterend.user_data = cast(any ptr,iParent)
			
		else
		
			iterend.user_data = cast(any ptr,iEndPos)

		EndIf		
		
		gtk_tree_model_get(model, @itercur, 1, @pszTempString,  -1)
		
		sText = *pszTempString
		
		dim as ListT ptr pListTemp
		
		dim as LinList ptr imglist = cast(any ptr, g_object_get_data (cast(any ptr,model), "tree_img_list"))
		
		if imglist <> 0 then
			
			pListTemp = cast(ListT ptr,imglist->findNodeFunc(iCurentPos))
			
			if pListTemp then
				
				pBitmap = cast(any ptr , pListTemp->anyThreeData)
				
				pBitmapSel = cast(any ptr , pListTemp->anyFourData)
				
			EndIf
			
		EndIf
		
		if iParent then

			if iEndPos = 0 then
			
				gtk_tree_store_insert(model, @iter , @iterend , 0)
				
			else
			
				gtk_tree_store_insert(model, @iter , @iterend , -1)
			
			endif
			
			gtk_tree_store_set(model, @iter, 0 , pBitmap, 1, pszTempString,  -1)
			
		else	

			dim as Long iPos
			
			if iEndPos = 0 orelse iEndPos = -1 then
	
				
				iPos = iEndPos
				
			else
			
				dim as any ptr path = gtk_tree_model_get_path(model , @iterend)
				
				dim as long ptr piIndex = gtk_tree_path_get_indices ( path )
				
				iPos = *piIndex +1
				
				gtk_tree_path_free(path)				

			EndIf
			
			
			if iEndPos <> 0 andalso iEndPos <> -1 andalso gtk_tree_model_iter_parent (model, @iterparent , @iterend) then

				gtk_tree_store_insert(model, @iter , @iterparent , iPos)
				
			else

				gtk_tree_store_insert(model, @iter , 0 , iPos)
				
			EndIf	
	
			gtk_tree_store_set(model, @iter, 0 , pBitmap, 1, pszTempString,  -1)	
			
		EndIf
		
		if iter.stamp then
			
			function =  cast(integer,iter.user_data)
			
			dim as LinList ptr imglist = cast(any ptr, g_object_get_data (cast(any ptr,model), "tree_img_list"))
			
			imglist->addNodeSub(cast(integer,iter.user_data)  , cast(integer,iter.stamp) , cast(integer , pBitmap) ,cast(integer , pBitmapSel))
			
		EndIf			
		
	EndIf		
	
End Function

private function CopyItem(treeview as any ptr , iCurentPos as integer, iEndPos as integer, iParent as integer) as integer
	
	Static As Integer iCounter 

	dim as integer iRetItem = CreateItem(treeview , iCurentPos , iEndPos , iParent)

	function = iRetItem
	
	dim as GtkTreeIter childiter , itercur , itercurcopy , iterfirst 
	
	dim as any ptr model = gtk_tree_view_get_model(treeview)

	if gtk_tree_model_get_iter_first(model , @iterfirst) then
	
		itercur.stamp =  iterfirst.stamp
		
		itercurcopy.stamp =  iterfirst.stamp
		
		itercur.user_data = cast(any ptr,iCurentPos)
		
		itercurcopy.user_data = cast(any ptr,iCurentPos)		

		Do

			if gtk_tree_model_iter_children(model , @childiter , @itercur) then
	
				iCounter+=1

				CopyItem(treeview , cast(integer , childiter.user_data) , -1 , iRetItem)
				
				if iCounter>0 then

					itercurcopy.stamp =  iterfirst.stamp
					
					itercurcopy.user_data = itercur.user_data

					if gtk_tree_model_iter_next (model, @itercurcopy) then

						dim as GtkTreeIter iterparent , iterch
						
						iterch.stamp =  iterfirst.stamp
						
						iterch.user_data = cast(any ptr, iRetItem)

						if gtk_tree_model_iter_parent (model, @iterparent , @iterch) then

							CopyItem(treeview ,Cast(Integer,itercurcopy.user_data),iEndPos, cast(integer ,iterparent.user_data))

						else

							CopyItem(treeview ,Cast(Integer,itercurcopy.user_data),iEndPos,0)

						EndIf
						
					else

						iCounter-=1

						Exit do 						

					EndIf
	
				else
				
					Exit do

				EndIf

			else
			
				if iCounter>0 then

					itercurcopy.stamp =  iterfirst.stamp
					
					itercurcopy.user_data = itercur.user_data

					if gtk_tree_model_iter_next (model, @itercurcopy) then

						dim as GtkTreeIter iterparent , iterch
						
						iterch.stamp =  iterfirst.stamp
						
						iterch.user_data = cast(any ptr, iRetItem)

						if gtk_tree_model_iter_parent (model, @iterparent , @iterch) then

							CopyItem(treeview ,Cast(Integer,itercurcopy.user_data),iEndPos, cast(integer ,iterparent.user_data))

						else

							CopyItem(treeview ,Cast(Integer,itercurcopy.user_data),iEndPos,0)

						EndIf
						
					else

						iCounter-=1

						Exit do 						

					EndIf
	
				else
				
					Exit do

				EndIf

			EndIf

		Loop Until iCounter=0	
	
	endif
	
End Function

function MoveItemTreeView(iGadget as long, iCurentPos as integer, iEndPos as integer, iParent as integer) as integer export
	
	dim as any ptr treeview = GadgetID(iGadget)
	
	dim as Byte bFlag

	scope

		dim as any ptr model =  gtk_tree_view_get_model(treeview)	
		
		dim as ListT ptr pListTemp
		
		dim as LinList ptr imglist = cast(any ptr, g_object_get_data (cast(any ptr,model), "tree_img_list"))
		
		if imglist <> 0 then

			pListTemp = cast(ListT ptr,imglist->findNodeFunc(iCurentPos))
			
			if pListTemp then
		
					bFlag = TRUE

			EndIf	
			
		EndIf	
	
	end scope
	
	if bFlag then
	
		function = CopyItem(treeview , iCurentPos , iEndPos , iParent)
		
		DeleteTreeViewItem(iGadget , iCurentPos)		

	EndIf	
	
End Function