#include Once "window9.bi"

Constructor GlobalTypeWindow9()
	
	ListGadgets = New LinList
	
	ListWinAndContainers = New LinList
	
	ListIncBin = New LinList
	
	if ListGadgets = 0 orelse ListWinAndContainers = 0 orelse ListIncBin = 0 then
		
		print "Allocated memory error in Constructor GlobalTypeWindow9!"
		
	EndIf
	
End Constructor

pGlobalTypeWindow9 = New GlobalTypeWindow9

if pGlobalTypeWindow9 = 0 then
	
	print "Allocated memory error pGlobalTypeWindow9!"
	
EndIf

Sub LinList.addNodeSub(g As Integer , w As Integer , cl As Integer = 0 , iAnyData As Integer = 0) ' (1-gadget, 2-hwnd , 3 -class) ;  (1-hwnd , 2-container , 3 - flag {0 - window ; 1 - another data})
	
	Dim newNode As ListT Ptr
	
	newNode = New ListT
	
	if newNode then
		
		newNode->anyOneData = g
		
		newNode->anyTwoData = w
		
		newNode->anyThreeData = cl	
		
		newNode->anyFourData = iAnyData
		
		If firstNode = 0 Then
			
			firstNode = newNode
			
		Else
			
			lastNode->nextNode = newNode
			
		End If
		
		lastNode = newNode
		
	else
		
		print "Allocated memory error in addNodeSub!"
		
	endif
	
End Sub

Sub LinList.removeList(delList As LinList Ptr)
	
	Dim seekNode As ListT Ptr
	
	Dim seekNextNode As ListT Ptr
	
	seekNode = firstNode	
	
	Do While seekNode
		
		seekNextNode = seekNode->nextNode
		
		If seekNode Then
			
			Delete seekNode
			
		Endif
		
		seekNode = seekNextNode
		
	Loop
	
	if delList then Delete delList	
	
End Sub

Sub LinList.removeNodeSub(rNode As Integer)
	
	Dim seekNode As ListT Ptr
	
	Dim seekOldNode As ListT Ptr
	
	seekNode = firstNode
	
	seekOldNode = firstNode
	
	Do While seekNode
		
		If seekNode->anyOneData = rNode  Then
			
			Exit Do
			
		End If
		
		seekOldNode = seekNode
		
		seekNode = seekNode->nextNode
		
	Loop
	
	If seekNode = 0 Then
		
		Exit Sub
		
	End If
	
	If seekNode->nextNode <> 0 Then
		
		If firstNode = seekNode Then
			
			firstNode = seekNode->nextNode
			
		Else
			
			seekOldNode->nextNode = seekNode->nextNode
			
		End If
		
	Else
		
		If firstNode = seekNode Then
			
			firstNode = 0
			
			lastNode = 0
			
		Else
			
			seekOldNode->nextNode = 0
			
			lastNode = seekOldNode
			
		End If		
		
	Endif
	
	if seekNode then Delete seekNode
	
End Sub


Function LinList.findNodeFunc(fNode As Integer) As ListT Ptr
	
	Dim seekNode As ListT Ptr
	
	seekNode = firstNode
	
	Do While seekNode
		
		If seekNode->anyOneData = fNode Then
			
			Return seekNode
			
		End If
		
		seekNode = seekNode->nextNode
		
	Loop
	
	Return 0	
	
End Function

Function Getcontainerfromwindow ( Gwindow As HWND ) As HWND Export
	
	Dim As ListT Ptr pTemp = pGlobalTypeWindow9->ListWinAndContainers->findNodeFunc(Cast(Integer,Gwindow))
	
	If pTemp <> 0 Then
		
		Return Cast(hwnd , pTemp->anyTwoData)
		
	Endif
	
End Function

Function Gadgetid ( iGadget As Integer ) As HWND Export
	
	Dim As ListT Ptr pTemp = pGlobalTypeWindow9->ListGadgets->findNodeFunc(iGadget)
	
	If pTemp <> 0 Then
		
		Return Cast(hwnd , pTemp->anyTwoData)
		
	Endif	
	
End Function

Function Id_in_number ( hWin As Hwnd ) As Long Export
	
	If hWin = 0 Then Return 0
	
	Dim As ListT Ptr pTemp = pGlobalTypeWindow9->ListGadgets->firstnode
	
	While pTemp
		
		If pTemp->anyTwoData = hWin Then
			
			Function = pTemp->anyOneData
			
			Exit While
			
		Endif
		
		pTemp = pTemp->nextNode
		
	Wend
	
End Function

Function Getclassgadget ( iGadget As Integer ) As Long Export
	
	Dim As ListT Ptr pTemp = pGlobalTypeWindow9->ListGadgets->findNodeFunc(iGadget)
	
	If pTemp <> 0 Then
		
		Return Cast(Long , pTemp->anyThreeData)
		
	Endif	
	
End Function

Dim Shared As Long iOptionsScroll

iOptionsScroll = GTK_POLICY_AUTOMATIC

Sub Setoptionswindowscroll (iParam As Long) Export
	
	iOptionsScroll = iParam
	
End Sub

Function Openwindow (sName As String , x As Long , y As Long , width_ As Long , height_ As Long , style As Long = WS_OVERLAPPEDWINDOW Or WS_VISIBLE , style2 As Long = 0 , User_data_Sub As Any Ptr = 0 , Byval hParentHwnd As HWND = 0) As HWND Export
	
	Dim As Hwnd mainBox
	
	pGlobalTypeWindow9->OptGroup = 0 ' reset group Optionsgadget
	
	If style And WS_OVERLAPPEDWINDOW Then
		
		pGlobalTypeWindow9->CurentHwnd = gtk_window_new (GTK_WINDOW_TOPLEVEL)
		
		'#ifndef __USE_GTK3__
		'
		'gtk_window_set_policy(Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd) ,1,1,1)
		'
		'#endif
		
	Elseif style And WS_POPUP Then
		
		pGlobalTypeWindow9->CurentHwnd = gtk_window_new (GTK_WINDOW_POPUP)
		
	Endif
	
	If style And WS_MAXIMIZE Then
		
		gtk_window_maximize(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd))
		
	Elseif style And WS_MINIMIZE Then
		
		gtk_window_iconify(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd))
		
	Endif
	
	If hParentHwnd Then
		
		gtk_window_set_transient_for(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd) , Cast(Any Ptr,hParentHwnd))
		
	Endif
	
	gtk_window_set_title(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd),Strptr(sName))
	
	gtk_window_set_default_size(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd), width_, height_)
	
	gtk_window_set_position(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd), GTK_WIN_POS_NONE)
	
	gtk_window_move(GTK_WINDOW(pGlobalTypeWindow9->CurentHwnd), x , y )
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "delete-event", G_CALLBACK (@closeWindowFunc), 0)
	
	gtk_widget_add_events(Cast(Any Ptr ,pGlobalTypeWindow9->CurentHwnd), _
	GDK_POINTER_MOTION_MASK Or GDK_POINTER_MOTION_HINT_MASK _ 
	Or GDK_FOCUS_CHANGE_MASK Or GDK_SCROLL_MASK Or GDK_KEY_PRESS_MASK _ 
	Or GDK_KEY_RELEASE_MASK Or GDK_BUTTON_PRESS_MASK Or GDK_BUTTON_RELEASE_MASK)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "motion-notify-event", G_CALLBACK (@mouseMoveWindowFunc), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "focus-in-event", G_CALLBACK (@focusWindowFunc), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "size-allocate", G_CALLBACK (@resizeWindowSub), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "scroll-event", G_CALLBACK (@mouseWheelWindowSub), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "key-press-event", G_CALLBACK (@KeyPressWindowSub), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "key-release-event", G_CALLBACK (@KeyReleaseWindowSub), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "button-press-event", G_CALLBACK (@MousePressWindowSub), 0)	
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "button-release-event", G_CALLBACK (@MousePressWindowSub), 0)
	
	g_signal_connect(G_OBJECT(pGlobalTypeWindow9->CurentHwnd), "configure-event", G_CALLBACK (@configureWindowSub), 0)
	
	If (style And WS_HSCROLL) Orelse (style And WS_VSCROLL) Then
		
		mainBox = gtk_fixed_new()
		
		Var scrolled = gtk_scrolled_window_new(NULL,NULL)
		
		If (style And WS_HSCROLL) Andalso (style And WS_VSCROLL) Then
			
			gtk_scrolled_window_set_policy(_
			GTK_SCROLLED_WINDOW(scrolled),_
			iOptionsScroll,_
			iOptionsScroll)
			
		Elseif (style And WS_HSCROLL) Then
			
			gtk_scrolled_window_set_policy(_
			GTK_SCROLLED_WINDOW(scrolled),_
			iOptionsScroll,_
			GTK_POLICY_NEVER)
			
		Elseif (style And WS_VSCROLL) Then
			
			gtk_scrolled_window_set_policy(_
			GTK_SCROLLED_WINDOW(scrolled),_
			GTK_POLICY_NEVER,_
			iOptionsScroll)			
			
		Endif
		
		gtk_scrolled_window_add_with_viewport(_
		GTK_SCROLLED_WINDOW(scrolled),_
		mainbox)
		
		gtk_container_add(GTK_CONTAINER(pGlobalTypeWindow9->CurentHwnd),scrolled)
		
		gtk_widget_show(scrolled)
		
		g_object_set_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "SCROLL" , Cast(Any Ptr,Cint(1)))
		
	Else
		
		mainBox = gtk_layout_new(0,0)
		
		gtk_container_add (GTK_CONTAINER(pGlobalTypeWindow9->CurentHwnd), mainBox)
		
	Endif
	
	pGlobalTypeWindow9->ListWinAndContainers->addNodeSub( Cint(pGlobalTypeWindow9->CurentHwnd) , Cint(mainBox) )
	
	gtk_widget_show(mainBox)
	
	If User_data_Sub Then
		
		Dim pInitSubUser As Sub Cdecl(hwnd As hwnd) = User_data_Sub
		
		pInitSubUser(pGlobalTypeWindow9->CurentHwnd)		
		
	Endif
	
	If style And WS_VISIBLE Then
		
		gtk_widget_show(pGlobalTypeWindow9->CurentHwnd)
		
	Endif
	
	Dim pWinSizeAC As TWinSizeAfterCreating Ptr = New TWinSizeAfterCreating
	
	if pWinSizeAC then
		
		pWinSizeAC->hw = pGlobalTypeWindow9->CurentHwnd
		
		pWinSizeAC->iWidth = width_
		
		pWinSizeAC->iHeight = height_
		
		g_timeout_add(20 , @TimerSizeWinAfterCreatingSub , pWinSizeAC)
		
	else
		
		print "Allocated memory error in Openwindow!"
		
	endif
	
	Dim As GtkStyle Ptr iStyle
	
	iStyle = gtk_widget_Get_style(pGlobalTypeWindow9->CurentHwnd)
	
	pGlobalTypeWindow9->fontDefault = iStyle->font_desc
	
	pGlobalTypeWindow9->fontCurent = iStyle->font_desc
	
	g_object_set_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "X_SIZE" , Cast(Any Ptr,Cint(x)))
	
	g_object_set_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "Y_SIZE" , Cast(Any Ptr,Cint(y)))
	
	g_object_set_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "W_SIZE" , Cast(Any Ptr,Cint(width_)))
	
	g_object_set_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "H_SIZE" , Cast(Any Ptr,Cint(height_)))		
	
	Updateinfoxserver()	
	
	Return pGlobalTypeWindow9->CurentHwnd
	
End Function