#include Once "window9.bi"

#ifdef __USE_GTK3__

Function GetPrevItemTreeView(iGadget As Long , iItem As Integer) As Integer
	
	Dim As Any Ptr treeview = Gadgetid(iGadget)
	
	Dim As Any Ptr treestore = Cast(Any Ptr , gtk_tree_view_get_model(treeview))
	
	Dim As GtkTreeIter iterChild , iterfirst 
	
	If gtk_tree_model_get_iter_first(treestore , @iterfirst) Then
		
		iterChild.stamp = iterfirst.stamp
		
		iterChild.user_data = Cast(Any Ptr,iItem)
		
		If gtk_tree_model_iter_previous(treestore, @iterChild) Then
			
			If iterChild.stamp Then
				
				Return Cast(Integer , iterChild.user_data)
				
			Endif
			
		Endif
		
	Endif
	
End Function

#else

Function GetPrevItemTreeView(iGadget As Long , iItem As Integer) As Integer
	
	Dim As Any Ptr treeview = Gadgetid(iGadget)
	
	Dim As Any Ptr treestore = Cast(Any Ptr , gtk_tree_view_get_model(treeview))
	
	Dim As GtkTreeIter iterCurent , iterFirst , iterPrev
	
	If gtk_tree_model_get_iter_first(treestore , @iterFirst) Then
		
		iterCurent.stamp = iterFirst.stamp
		
		iterCurent.user_data = Cast(Any Ptr,iItem)
		
		Dim As GtkTreePath Ptr path = gtk_tree_model_get_path(treestore , @iterCurent)
		
		If path Then
			
			If gtk_tree_path_prev(path) Then
				
				If gtk_tree_model_get_iter(treestore, @iterPrev , path) Then
					
					If iterPrev.stamp Then
						
						Function = Cast(Integer , iterPrev.user_data)
						
					Endif					
					
				Endif
			
			Endif
			
			gtk_tree_path_free(path)
			
		Endif
		
	Endif
	
End Function

#endif