#include once "window9.bi"

Type ListNode10
	declare constructor()
	id as Integer
	valueI As Integer
	valueS As String
	prev_node as ListNode10 Ptr
	next_node as ListNode10 Ptr
End type

type List10
	declare constructor()
	Declare function AddLastIn10 ( as Integer ) as UInteger
	Declare Function AddLastS10(As String ) as UInteger
	declare sub Remove10( as Integer )
	Declare sub RemoveColor( as Integer )
	declare function FindByID10( as Integer ) as ListNode10 Ptr
	Declare Function Setgurrent(As HWND) As Integer
	Declare Function Getgurrent() As HWND
	first_node as ListNode10 Ptr
	last_node as ListNode10 Ptr
	Gurrent As HWND
end Type

constructor ListNode10()
	this.id = 0
	this.prev_node = 0
	this.next_node = 0
End constructor

Constructor List10()
	this.first_node = 0
	this.last_node = 0
End constructor

Dim Shared As Integer LOLILOLILOLI
Dim Shared As List10 Ptr LISALIST,CLASSLIST,ColorList

function List10.AddLastIn10( valueI  as Integer ) as uinteger
	dim new_node as ListNode10 Ptr
	new_node = New ListNode10
	new_node->valueI = valueI
	if this.first_node = 0 then
		new_node->id = valueI
		this.first_node = new_node
	else
		new_node->id = valueI
		this.last_node->next_node = new_node
		new_node->prev_node = this.last_node
	end if
	this.last_node = new_node
	return new_node->id
end Function

function List10.AddLastS10( valueS  as String ) as uinteger
	dim new_node as ListNode10 Ptr
	new_node = New ListNode10
	new_node->valueS = valueS
	if this.first_node = 0 then
		new_node->id = 1
		this.first_node = new_node
	else
		new_node->id = this.last_node->id + 1
		this.last_node->next_node = new_node
		new_node->prev_node = this.last_node
	end if
	this.last_node = new_node
	return new_node->id
end Function

sub List10.Remove10( ida as Integer )
	dim del_node as ListNode10 Ptr
	del_node = this.FindByID10( ida )

	if del_node = 0 Then
		exit sub
	end If

	if this.first_node = del_node Then
		this.first_node = del_node->next_node->next_node
	Else
		del_node->prev_node->next_node = del_node->next_node->next_node
	end if

	if this.last_node->prev_node = del_node Then
		this.last_node = del_node->prev_node
	Else
		del_node->next_node->next_node->prev_node = del_node->prev_node
	end If

	Delete del_node->next_node
	Delete del_node

End Sub

sub List10.RemoveColor( ida as Integer )
	dim del_node as ListNode10 Ptr
	del_node = this.FindByID10( ida )
	if del_node = 0 Then
		exit sub
	end If
	if this.first_node = del_node Then
		this.first_node = del_node->next_node->next_node->next_node
	Else
		del_node->prev_node->next_node = del_node->next_node->next_node->next_node
	end if

	if this.last_node->prev_node->prev_node = del_node Then
		this.last_node = del_node->prev_node
	Else
		del_node->next_node->next_node->next_node->prev_node = del_node->prev_node
	end If

	Delete del_node->next_node->next_node
	Delete del_node->next_node
	Delete del_node
End Sub

function List10.FindByID10( id as Integer ) as ListNode10 Ptr
	dim seek_node as ListNode10 Ptr
	seek_node = this.first_node
	do while seek_node
		if seek_node->id = id Then
			return seek_node
		end if
		seek_node = seek_node->next_node
	loop
	return 0
end Function

Function List10.getgurrent() As HWND
	Return Gurrent
End Function

Function List10.Setgurrent(ByVal hw As HWND) As Integer
	Gurrent=hw
	Return 1
End Function

Function OpenWindow Alias "OpenWindow" (ByVal name_ As String, ByVal x As Integer,ByVal y As Integer,ByVal Width_ As Integer,ByVal height_ As Integer,ByVal stil As Integer=268435456 Or 13565952,ByVal style2 As Integer=0) As HWND Export
	Static doba As Integer
	doba= doba+1
	If LOLILOLILOLI=0 Then
		LISALIST= New List10
		CLASSLIST=New List10
		ColorList=New List10
		LOLILOLILOLI=1
	EndIf
	CLASSLIST->AddLastS10("CLASSCLASS_"+Str(Time())+ Str(doba))
	Dim wc As wndclassex
	with wc
		.cbSize = SIZEOF( WNDCLASSEX )
		.style = CS_HREDRAW or CS_VREDRAW
		.lpfnWndProc = Cast(WNDPROC,GetFbGuiWinProc())
		.hInstance =  getmodulehandle(0)
		.hbrBackground =CPtr(HGDIOBJ, 16)
		.lpszClassName = StrPtr(CLASSLIST->FindByID10(doba)->VALUES)
		.hIcon = LoadIcon( NULL,IDI_APPLICATION )
		.hIconSm = .hIcon
		.hCursor = LoadCursor( NULL,IDC_ARROW)
	end With
	if(RegisterClassEx(@wc) = FALSE) Then
		Var e=GetLastError()
		? "Not Registered class , Error = " & e
		Return 0
	end If
	Var hwn= CreateWindowEx( style2,StrPtr(CLASSLIST->FindByID10(doba)->VALUES)  , name_, stil, x, y, Width_, Height_, 0, 0, 0, 0 )
	LISALIST->Setgurrent(hwn)
	Return hwn
End Function

Function GETGURRENTAL Alias "GETGURRENTAL" () As HWND Export
	Return LISALIST->Getgurrent()
End Function

Function UseGadgetList Alias "UseGadgetList" (hw As HWND) As Integer Export
	Return LISALIST->Setgurrent(hw)
End Function

Function ADDIN9999 Alias "ADDIN9999" (byval gadget as integer, byval hhh as HWND) As Integer Export
	FreeGadget(gadget)
	LISALIST->AddLastIn10(gadget)
	LISALIST->AddLastIn10(Cast(Integer,hhh))
	Return 1
End Function

Function ColorAdd Alias "ColorAdd" (byval hwnd as HWND,ByVal colorBKD_ as Integer,ByVal colorText_ as Integer ) As Integer Export
	ColorList->AddLastIn10(Cast(Integer,hwnd))
	ColorList->AddLastIn10(colorBKD_)
	ColorList->AddLastIn10(colorText_)
	Return 1
End Function

Function GadgetID Alias "GadgetID"(ByVal gadget As integer) As HWND Export
	If LISALIST->FindByID10(Cast(Integer,gadget))<>0 Then
		If LISALIST->FindByID10(Cast(Integer,gadget))->next_node<>0 Then
			Return Cast(HWND,LISALIST->FindByID10(Cast(Integer,gadget))->next_node->VALUEI)
		EndIf
	Else
		Return 0
	EndIf
End Function

Function GetGadgetColor Alias "GetGadgetColor" (byval hwnd as Integer ,ByVal flag as Byte ) As Integer Export
	Dim szClass As ZString *256
	Dim hwndTemp As HWND
	GetClassName(GadgetID(hwnd),@szClass,256)
	If szClass = "ExplorerGadget" then
		hwndTemp = *cast(HWND ptr,GetProp(GadgetID(hwnd),"addparam60000"))
	ElseIf flag < 3 Then
		hwndTemp = GadgetID(hwnd)
	EndIf
	Select Case flag
		Case 1
			If ColorList->FindByID10(CInt(hwndTemp))<>0 Then
				Return ColorList->FindByID10(CInt(hwndTemp))->next_node->VALUEI
			EndIf
		Case 2
			If ColorList->FindByID10(CInt(hwndTemp))<>0 Then
				Return ColorList->FindByID10(CInt(hwndTemp))->next_node->next_node->VALUEI
			EndIf
		Case 3
			If ColorList->FindByID10(hwnd)<>0 Then
				Return ColorList->FindByID10(hwnd)->next_node->VALUEI
			Else
				ERR_(1)
			EndIf
		Case 4
			If ColorList->FindByID10(hwnd)<>0 Then
				Return ColorList->FindByID10(hwnd)->next_node->next_node->VALUEI
			Else
				ERR_(1)
			EndIf
	End Select
End Function

Function SetGadgetColor Alias "SetGadgetColor" (byval hwnd as Integer,ByVal colorBKD_ as Integer,ByVal colorText_ as Integer, ByVal flag as Byte) As Integer Export
	Var return_DSGC=DSGC(GadgetID(hwnd),colorBKD_ ,colorText_ ,flag )
	Dim hwndTemp As HWND
	If return_DSGC=4 Then
		hwndTemp = *cast(HWND ptr,GetProp(GadgetID(hwnd),"addparam60000"))
	Else
		hwndTemp = GadgetID(hwnd)
	EndIf
	If ColorList->FindByID10(Cast(Integer,hwndTemp))<>0 Then
		If ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node<>0 Then
			Select Case flag
				Case 1
					ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node->VALUEI=colorBKD_
				Case 2
					If ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node->next_node<>0 Then
						ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node->next_node->VALUEI=colorText_
					endif
				Case 3
					ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node->VALUEI=colorBKD_
					If ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node->next_node<>0 Then
						ColorList->FindByID10(Cast(Integer,hwndTemp))->next_node->next_node->VALUEI=colorText_
					EndIf
			End Select
		EndIf
	EndIf
	If return_DSGC=2 Then SendMessage(getparent(hwndTemp),WM_CTLCOLORSCROLLBAR,0,Cast(lparam,hwndTemp))
	Return 1
End Function

Function ID_In_Number Alias "ID_In_Number"(ByVal ID As HWND) As Integer Export
	If LISALIST->FindByID10(cast(integer,ID))<>0 Then
		If LISALIST->FindByID10(cast(integer,ID))->prev_node<>0 Then
			Return LISALIST->FindByID10(cast(integer,ID))->prev_node->VALUEI
		EndIf
	EndIf
End Function

Sub RemoveStatusbar(gadget As Integer)
	Dim As Integer Ptr iArray = GetProp(GadgetID(gadget),"STATUSBARARRAY")
	If iArray <> 0 Then
		Delete iArray
		RemoveProp(GadgetID(gadget),"STATUSBARARRAY")
		RemoveProp(GadgetID(gadget),"STATUSBARNUMBERFIELDS")
	EndIf
	RemoveProp(GadgetID(gadget),"STATUSBARWINDOW")
End Sub

Function FreeGadget (ByVal gadget As integer) As Byte Export
	RemoveStatusbar(gadget)
	Dim As HWND hwnd = GadgetID(gadget)
	If hwnd<>0 Then
		ColorList->RemoveColor(Cast(Integer,hwnd))
	EndIf	
	Dim sBuf As String = GetClassName_(hwnd)
	If sBuf = "ExplorerGadget" Then
		Dim freeexplorerproc As Function (pEG As Any Ptr) As bool
		freeexplorerproc = GetProp(hwnd,"EXPLORERLISTFREEPROC")
		If freeexplorerproc<>0 Then
			freeexplorerproc(GetProp(hwnd,"addparam60000"))
			RemoveProp(hwnd,"EXPLORERLISTFREEPROC")
			RemoveProp(hwnd,"EXPLORERGADGETLISTVIEWWINDOW")
		EndIf
	EndIf	
	RemoveProp(hwnd,"addparam60000")
	Function = DestroyWindow(hwnd)
	LISALIST->Remove10(gadget)
End Function
