#include ONCE "window9.bi"

private function DeleteListItemRecursion(model as any ptr, iter as GtkTreeIter ptr , findIter as GtkTreeIter ptr , iFlag as Byte) as long
	
	dim as GtkTreeIter tempiter , tempiter2 
	
	tempiter2.stamp = iter->stamp
	
	tempiter2.user_data = iter->user_data
	
	dim as long iRet = 1  , iRetFunc
	
	while iRet	
		
		if tempiter2.user_data = findIter->user_data then
			
			iFlag = 1
			
			iRetFunc = 1
			
		EndIf
		
		if gtk_tree_model_iter_children (model, @tempiter, @tempiter2) then
			
			if iRetFunc = 0 then
				
				iRetFunc = DeleteListItemRecursion (model , @tempiter , findIter , iFlag )
				
			else
				
				DeleteListItemRecursion (model , @tempiter , findIter , iFlag )
				
			EndIf				
			
		EndIf
		
		if iFlag = 1 andalso tempiter2.user_data = findIter->user_data then
			
			dim as LinList ptr imglist = cast(any ptr, g_object_get_data (cast(any ptr,model), "tree_img_list"))
			
			imglist->removeNodeSub(cast(integer,tempiter2.user_data))
			
			iFlag = 0
			
		elseif 	iFlag = 1 then
			
			dim as LinList ptr imglist = cast(any ptr, g_object_get_data (cast(any ptr,model), "tree_img_list"))
			
			imglist->removeNodeSub(cast(integer,tempiter2.user_data))
			
		EndIf			
		
		iRet = gtk_tree_model_iter_next (model, @tempiter2)	
		
	Wend
	
	return iRetFunc
	
End function

sub DeleteTreeViewItem(iGadget as Long , iItem as integer) export
	
	dim as any ptr treeview = GadgetID(iGadget)
	
	dim as long iRet
	
	if iItem = 0 orelse iItem =-1 then
		
		exit sub
		
	EndIf
	
	if treeview then
		
		dim as any ptr model = gtk_tree_view_get_model(treeview)
		
		if model then
			
			dim as GtkTreeIter iter  , finditer , iterparent
			
			iRet = gtk_tree_model_get_iter_first(model , @iter)
			
			if iRet then
				
				finditer.stamp = iter.stamp
				
				finditer.user_data = cast(any ptr,iItem)
				
			EndIf
			
			if DeleteListItemRecursion(model , @iter , @finditer , 0) then
				
				if gtk_tree_model_iter_parent (model, @iterparent , @finditer) then
					
					if gtk_tree_model_iter_n_children(model , @iterparent) < 2 then 
						
						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(cint(iterparent.user_data)))
							
						EndIf	
						
						if pListTemp <> 0 then
							
							gtk_tree_store_set(model, @iterparent, 0 , pListTemp->anyThreeData ,  -1)
							
						EndIf						
						
					EndIf 
					
				EndIf
				
				gtk_tree_store_remove(model , @finditer)
				
			EndIf		
			
		EndIf		
		
	EndIf
	
End Sub