#include Once "window9.bi"

Dim Shared f_move_splitter_callback As Sub (w  As Any Ptr , iCurPos As Long )

Dim Shared f_size_allocate_splitter_callback As Sub (widget  As Any Ptr , ix As Long , iy As Long , iw As Long , ih As Long)

Sub size_allocate_splitter_signals Cdecl(Gwindow As GtkWidget Ptr, allocation As GdkRectangle Ptr, user_data As gpointer)
	
	Dim As Any Ptr w = Gadgetid(*Cast(Long Ptr, user_data))
	
	If w Then
		
		Dim As Long iWp = Cint(g_object_get_data (Cast(Any Ptr, w), "W_SIZE"))
		
		Dim As Long iHp = Cint(g_object_get_data (Cast(Any Ptr, w), "H_SIZE"))
		
		Dim As Long iGadget1 , iGadget2 , iCurPos , iMax , iMin 
		
		Dim As gint Ptr iCurSavePos
		
		g_object_get(w, "max-position", @iMax, "min-position", @iMin, NULL)	
		
		If *(Cast(Zstring Ptr,g_object_get_data(w, "ORI"))) = "V" Then
			
			iCurSavePos = g_object_get_data(w, "position_prev_V")
			
			If iMax = 0 Then iMax = allocation->height
			
		Else
			
			iCurSavePos = g_object_get_data(w, "position_prev_H")
			
			If iMax = 0 Then iMax = allocation->Width
			
		Endif
		
		iGadget1 = Cint(g_object_get_data(w, "ngad1"))
		
		iGadget2 = Cint(g_object_get_data(w, "ngad2"))
		
		iCurPos = gtk_paned_get_position(w)
		
		If Gadgetid(iGadget1) Andalso Gadgetid(iGadget2) Then
			
			If *(Cast(Zstring Ptr,g_object_get_data(w, "ORI"))) = "V" Then
				
				If Gadgetwidth(iGadget1) <> iWp Orelse _
				Gadgetheight(iGadget1)+Gadgetheight(iGadget2) <> iMax Then
					
					Resizegadget(iGadget1 , 0 , 0 , iWp , iCurPos )
					
					Resizegadget(iGadget2 , 0 , 0 , iWp, iMax - iCurPos)
					
				Endif
				
			Else
				
				If Gadgetwidth(iGadget1)+Gadgetwidth(iGadget2) <> iMax _
				Orelse Gadgetheight(iGadget1) <> allocation->height Then
					
					Resizegadget(iGadget1 , 0 , 0 , iCurPos , iHp )

					Resizegadget(iGadget2 , 0 , 0 , iMax - iCurPos , iHp)
					
				Endif
				
			Endif
			
		Endif
		
		If f_size_allocate_splitter_callback Then
			
			f_size_allocate_splitter_callback(w , allocation->x , allocation->y , allocation->Width , allocation->height)
			
		Endif
		
	Endif
	
End Sub

Function move_splitter Cdecl(w  As Any Ptr , pAny As Any Ptr, p As gObject) As gboolean
	
	Dim As gint iMax , iMin , iCurPos
	
	Dim As gint Ptr iCurSavePos
	
	If *(Cast(Zstring Ptr,g_object_get_data(w, "ORI"))) = "V" Then
		
		iCurSavePos = g_object_get_data(w, "position_prev_V")
		
	Else
		
		iCurSavePos = g_object_get_data(w, "position_prev_H")
		
	Endif
	
	g_object_get(w, "max-position", @iMax, "min-position", @iMin, NULL)
	
	iCurPos = gtk_paned_get_position(w)
	
	If iCurPos <> *iCurSavePos Andalso iCurPos <> -1 Then
		
		If iMax <= 0 Then iMax = iCurPos
		
		If iMin < 0 Then iMin = 0
		
		If iCurPos < iMin Then
			
			gtk_paned_set_position(w , 0)
			
			If f_move_splitter_callback Then
				
				f_move_splitter_callback( w , iCurPos)
				
			Endif
			
			Return 0
			
		Elseif (iCurPos > iMax Andalso iMax > 0)  Then
			
			If iCurPos > 0 Then
				
				gtk_paned_set_position(w , iMax)
				
			Endif
			
			If f_move_splitter_callback Then
				
				f_move_splitter_callback( w , iCurPos)
				
			Endif
			
			Return 0
			
		Else
			
			Dim As Long iGadget1 , iGadget2
			
			iGadget1 = Cint(g_object_get_data(w, "ngad1"))
			
			iGadget2 = Cint(g_object_get_data(w, "ngad2"))		
			
			If Gadgetid(iGadget1) Andalso Gadgetid(iGadget2) Then
				
				If *(Cast(Zstring Ptr,g_object_get_data(w, "ORI"))) = "V" Then
					
					Resizegadget(iGadget1 , 0 , 0 , , iCurPos )
					
					Resizegadget(iGadget2 , 0 , 0 , , iMax - iCurPos)
					
				Else
					
					Resizegadget(iGadget1 , 0 , 0 , iCurPos )
					
					Resizegadget(iGadget2 , 0 , 0 , iMax - iCurPos)
					
				Endif
				
				*iCurSavePos = iCurPos
				
			Endif
			
		Endif
		
	Endif
	
	If f_move_splitter_callback Then
		
		f_move_splitter_callback( w , iCurPos)
		
	Endif
	
	Return 0
	
End Function

Sub SplitterGadgetAddGadget(iGadgetSplitter As Long , iGadgetChild1 As Long , iGadgetChild2 As Long) Export
	
	Dim As Any Ptr pan , child1 , child2
	
	pan = Gadgetid(iGadgetSplitter)
	
	child1 = Gadgetid(iGadgetChild1)
	
	child2 = Gadgetid(iGadgetChild2)
	
	gtk_paned_add1 (GTK_PANED (pan), child1)
	
	gtk_paned_add1 (GTK_PANED (pan), child2)
	
	g_object_set_data (Cast(Any Ptr, pan), "gad1" , Cast(Any Ptr,child1))
	
	g_object_set_data (Cast(Any Ptr, pan), "gad2" , Cast(Any Ptr,child2))
	
	g_object_set_data (Cast(Any Ptr, pan), "ngad1" , Cast(Any Ptr,Cint(iGadgetChild1)))
	
	g_object_set_data (Cast(Any Ptr, pan), "ngad2" , Cast(Any Ptr,Cint(iGadgetChild2)))
	
End Sub

Function SplitterGadget(iGadget As Long , iX As Long , iY As Long , iWidth As Long , iHeight As Long, iPos As Long , iStyle As Long = 0) As HWND Export
	
	Dim As HWND LayoutContainer , vBox , mainBox , eventbox 
	
	Dim As ListT Ptr pListTemp
	
	Freegadget(iGadget)
	
	pGlobalTypeWindow9->OptGroup = 0 ' reset Group OptionGadget
	
	pListTemp = Cast(ListT Ptr,pGlobalTypeWindow9->ListWinAndContainers->findNodeFunc(Cint(pGlobalTypeWindow9->CurentHwnd)))
	
	mainBox = Cast(hwnd , pListTemp->anyTwoData)	
	
	vbox = gtk_fixed_new()
	
	gtk_container_add (GTK_CONTAINER(mainBox), vbox)	
	
	If iStyle Then
		
		LayoutContainer = gtk_hpaned_new ()
		
		g_object_set_data(Cast(Any Ptr,LayoutContainer), "ORI", Cast(Any Ptr, @"H"))
		
	Else
		
		LayoutContainer = gtk_vpaned_new ()
		
		g_object_set_data(Cast(Any Ptr,LayoutContainer), "ORI", Cast(Any Ptr, @"V"))
		
	Endif
	
	pGlobalTypeWindow9->ListGadgets->addNodeSub(iGadget , Cint(LayoutContainer) ,  28)
	
	gtk_widget_set_size_request(LayoutContainer, iWidth, iHeight)
	
	gtk_fixed_put(GTK_FIXED(vbox), LayoutContainer, iX , iY)
	
	g_object_set_data (Cast(Any Ptr, LayoutContainer), "X_SIZE" , Cast(Any Ptr,Cint(iX)))
	
	g_object_set_data (Cast(Any Ptr, LayoutContainer), "Y_SIZE" , Cast(Any Ptr,Cint(iY)))
	
	g_object_set_data (Cast(Any Ptr, LayoutContainer), "W_SIZE" , Cast(Any Ptr,Cint(iWidth)))
	
	g_object_set_data (Cast(Any Ptr, LayoutContainer), "H_SIZE" , Cast(Any Ptr,Cint(iHeight)))	
	
	tree_add_node(iGadget,pGlobalTypeWindow9->treeNumberGadgets)
	
	Dim As Node Ptr fNode = tree_find_node(iGadget,pGlobalTypeWindow9->treeNumberGadgets)	
	
	g_signal_connect(G_OBJECT(LayoutContainer), "size-allocate", G_CALLBACK (@size_allocate_splitter_signals), @(fNode->x))
	
	g_signal_connect(G_OBJECT(LayoutContainer), "notify::position", G_CALLBACK (@move_splitter), @(fNode->x))
	
	Static As gint iPV = 0
	
	Static As gint iPH = 0
	
	iPV = iHeight
	
	iPH = iWidth
	
	g_object_set_data(Cast(Any Ptr,LayoutContainer), "position_prev_V", Cast(Any Ptr, @iPV))
	
	g_object_set_data(Cast(Any Ptr,LayoutContainer), "position_prev_H", Cast(Any Ptr, @iPH))
	
	g_object_set(LayoutContainer, "position", iPos,  NULL)
	
	pGlobalTypeWindow9->ListWinAndContainers->addNodeSub( Cint(LayoutContainer) , Cint(LayoutContainer) )
	
	gtk_widget_show_all (pGlobalTypeWindow9->CurentHwnd)
	
	pGlobalTypeWindow9->CurentHwnd = LayoutContainer
	
	Return LayoutContainer
	
End Function

Sub AddCallbackSplitterPosition(pFunc As Any Ptr)
	
	f_move_splitter_callback = pFunc
	
End Sub

Sub AddCallbackSplitterResize(pFunc As Any Ptr)
	
	f_size_allocate_splitter_callback = pFunc
	
End Sub

Sub SetSplitterPos(iGadgetSplitter As Long , iPos As Long)
	
	Dim As HWND gID = Gadgetid(iGadgetSplitter)
	
	If gID Then
		
		Dim As Long iW = Cint(g_object_get_data (Cast(Any Ptr, gID), "W_SIZE"))
		
		Dim As Long iH = Cint(g_object_get_data (Cast(Any Ptr, gID), "H_SIZE"))
		
		Dim As String sOri = *Cast(Zstring Ptr ,g_object_get_data(Cast(Any Ptr,gID), "ORI"))
		
		If iPos < 0 Then iPos = 0
		
		If sOri = "H" Then
			
			If iPos > iW Then iPos = iW
			
		Else
			
			If iPos > iH Then iPos = iH
			
		Endif
		
		gtk_paned_set_position(Cast(Any Ptr ,gID) , iPos)
		
	Endif
	
End Sub

Sub SplitterGadgetUnMapGadget(iGadgetSplitter As Long  , iParam As Long)
	
	Dim As Any Ptr pan
	
	Dim As Long iGadget
	
	pan = Gadgetid(iGadgetSplitter)
	
	If pan Then
		
		If iParam = 1 Then
			
			iGadget = Cint(g_object_get_data (Cast(Any Ptr, pan), "ngad1"))
			
			g_object_set_data (Cast(Any Ptr, pan), "gad1" , Cast(Any Ptr,0))
			
			g_object_set_data (Cast(Any Ptr, pan), "ngad1" , Cast(Any Ptr,Cint(0)))
			
			Freegadget(iGadget)
			
		Elseif iParam = 2 Then
			
			iGadget = Cint(g_object_get_data (Cast(Any Ptr, pan), "ngad2"))
			
			g_object_set_data (Cast(Any Ptr, pan), "gad2" , Cast(Any Ptr,0))
			
			g_object_set_data (Cast(Any Ptr, pan), "ngad2" , Cast(Any Ptr,Cint(0)))
			
			Freegadget(iGadget)
			
		Endif
		
	Endif
	
End Sub

Sub SplitterGadgetMapGadget(iGadgetSplitter As Long  , iGadget As Long , iParam As Long)
	
	Dim As HWND gID = Gadgetid(iGadgetSplitter)
	Dim As HWND gGadget = Gadgetid(iGadget)
	
	If gID Andalso gGadget Then
		
		If iParam = 1 Then
			
			gtk_paned_add1 (GTK_PANED (gID), gGadget)
			
			g_object_set_data (Cast(Any Ptr, gID), "gad1" , Cast(Any Ptr,gGadget))
			
			g_object_set_data (Cast(Any Ptr, gID), "ngad1" , Cast(Any Ptr,Cint(iGadget)))
			
		Elseif iParam = 2 Then
			
			gtk_paned_add1 (GTK_PANED (gID), gGadget)
			
			g_object_set_data (Cast(Any Ptr, gID), "gad2" , Cast(Any Ptr,gGadget))
			
			g_object_set_data (Cast(Any Ptr, gID), "ngad2" , Cast(Any Ptr,Cint(iGadget)))
			
		Endif
		
	Endif
	
End Sub

Function GetSplitterPos(iGadget As Long) As Long
	
	Return gtk_paned_get_position(Cast(Any Ptr , Gadgetid(iGadget)))
	
End Function