#Include "window9.bi"

Dim Shared As bool bool_
Dim Shared As HWND hwnd,hwnd1
Dim Shared As string DirectoryTemplate
Dim Shared RAEDIT As HWND
RAEDIT=Cast(HWND,ValInt(Command))
DirectoryTemplate=GetCurentDir & "\Template\"

Sub LoadingList()
	Var handle=Read_File(DirectoryTemplate & "template.ini")
	If handle<>-1 Then
		While E_O_F(handle)=0
			Var st=Read_String(handle)
			If st<>"" Then
				Var search=InStr(st," ")
				AddListBoxItem(6,Mid(st,search+1),ValInt(Mid(st,1,search-1)))
			EndIf
		Wend
	EndIf
End Sub
Sub EventList(flag As Integer)
	If flag=0 Then
		Var handle=Read_File(DirectoryTemplate & GetListBoxText(6,GetItemListBox(6)) & ".bas")
		If handle<>-1 Then
			SetGadgetText(7,PeekS(Read_DataS(handle,Size_File(handle))))
			SetToolTipText(7,100,GetGadgetText(7,7))
		EndIf
		Close_File(handle)
	Else
		Var handle=Create_File(DirectoryTemplate & "template.ini")
		If handle<>-1 Then
			Var count=CountItemListBox(6)
			If count<>0 Then
				For countL As Integer=0 To count-1
					Write_StringN(handle,countL & " " & GetListBoxText(6,countL) )
				Next
				Close_File(handle)
			EndIf
		EndIf
	EndIf
End Sub
Sub UP_DOWN(flag As bool)
	Var count=CountItemListBox(6)
	Var item =	GetItemListBox(6)
	If flag=1 Then
		If item=count-1 Then Exit Sub
		AddListBoxItem(6,GetListBoxText(6,item),item+2)
		DeleteListBoxItem(6,item)
		SetItemListBox(6,item+1)
	Else
		If item=0 Then Exit Sub
		item=item-1
		AddListBoxItem(6,GetListBoxText(6,item),item+2)
		DeleteListBoxItem(6,item)
	EndIf
End Sub
Sub ADD_Edit()
	Var GGT12=GetGadgetText(12,7)
	Var GGT11=GetGadgetText(11,7)
	Var COUNT_=CountItemListBox(6)
	If GGT12<>"" AND GGT12<>"Enter Name..." Then
		If bool_=0 Then
			For a As Integer=0 To CountItemListBox(6)-1
				If LCase(GGT12)=LCase(GetListBoxText(6,a)) Then
					MessBox("Error", "Duplicate name")
					Exit Sub
				EndIf
			Next
		EndIf
		If bool_=0 Then
			AddListBoxItem(6,GGT12)
		EndIf
		SetGadgetText(7,GGT11)
		SetToolTipText(7,100,GGT11)
		SetItemListBox(6,COUNT_)
		Var handle=Create_File(DirectoryTemplate & GGT12 & ".bas")
		If handle<>-1 Then
			Write_String(handle,GGT11)
			Close_File(handle)
			Close_Window(hwnd1)
			Exit Sub
		EndIf
	EndIf
	MessBox("Error", "The name of the wrong")
End Sub
Sub open_window_0()
	hwnd=OpenWindow("TempLate",10,10,245,450) : CenterWindow(hwnd)
	ButtonGadget(1,11,9,32,32,"Add")
	ButtonGadget(2,51,9,32,32,"Edit")
	ButtonGadget(3,143,9,40,32,"Up")
	ButtonGadget(4,186,9,40,32,"Down")
	ButtonGadget(5,92,9,32,32,"Del")
	ListBoxGadget(6,8,54,215,200,WS_VSCROLL Or LBS_SORT Or LBS_NOTIFY)
	EditorGadget(7,8,270,215,100,,ES_READONLY ):GadgetToolTip(7,"",100)
	SetGadgetColor(6,&hC6E8E3,&h34572,3)
	SetGadgetColor(7,&hC6E8E3,&h1C6114,3)
	CheckBoxGadget(8,9,380,199,24,"To make over all windows")
End Sub
Sub open_window_1(ByVal default_1 As String="",ByVal default_2 As String="")
	hwnd1=OpenWindow("ADD\EDIT",500,450,400,400) : CenterWindow(hwnd1)
	SetWindowTop(hwnd1,GetGadgetState(8))
	ButtonGadget(9,250,325,50,25,"Ok")
	ButtonGadget(10,315,325,50,25,"Cancel")
	EditorGadget(11,1,1,382,320,default_1)
	SetGadgetFont(11,Cast(Integer,LoadFont("Courier New",12)))
	StringGadget(12,22,328,200,20,default_2,,WS_EX_CLIENTEDGE)
	SetGadgetFont(12,Cast(Integer,LoadFont("Courier New",12)))
	SetGadgetColor(11,&hF5FEEB,&h80,3)
	SetGadgetColor(12,&hF5FEEB,&hff0000,3)
End Sub

Sub BEGINTEMPLATE
	Dim As Integer event
	open_window_0()
	LoadingList()
	Do
		event=WindowEvent()
		If Event=EventClose Then
			If EventHwnd=hwnd Then
				EventList(1)
				Close_Window(hwnd)
				Exit Sub
			Else
				Close_Window(hwnd1)
			EndIf
		EndIf
		If Event=EventGadget Then
			Select Case EventNumber
				Case 1
					open_window_1("Enter Code...","Enter Name...")
					bool_=0
				Case 2 'edit
					open_window_1(GetGadgetText(7,7),GetListBoxText(6,GetItemListBox(6)))
					bool_=1
				Case 3
					UP_DOWN(0)
				Case 4
					UP_DOWN(1)
				Case 5
					SetCurentDir(DirectoryTemplate)
					DeleteFile(GetListBoxText(6,GetItemListBox(6)) & ".bas")
					DeleteListBoxItem(6,GetItemListBox(6))
					SetGadgetText(7,"")
				Case 6
					If HIWORD(eventwParam)=2 Then 'LBN_DBLCLK
						Dim As String tteemm=GetGadgetText(7,7)
						'SetFocus(RaEDIt)
						SendMessage(RaEDIt,EM_REPLACESEL,1,Cast(LPARAM,StrPtr(tteemm)))
					Else
						EventList(0)
					EndIf

				Case 8
					SetWindowTop(hwnd,GetGadgetState(8))
				Case 9
					ADD_Edit()
				Case 10
					Close_Window(hwnd1)
			End Select
		EndIf
	Loop
End Sub

BEGINTEMPLATE

