/*
注意：インクルードファイルではありません。
イベント処理の実装コード生成プログラム。
*/

Imports System
Imports System.IO
Imports System.Text
Imports ActiveBasic
Imports ActiveBasic.CType

Function EventNameToMemberVariableName(eventName As String) As String
	'先頭1字を小文字化
	Dim t = New StringBuilder(eventName)
	t[0] = ToLower(t[0])
	Return t.ToString
End Function

Sub OutputEventHandlerCode(out As TextWriter, eventName As String, argBase As String, comment As String)
	Dim eventMember = EventNameToMemberVariableName(eventName)
	Dim handlerType = argBase & "Handler"
	Dim argsType = argBase & "Args"
	out.WriteLine("Public")
	out.WriteLine(Ex"\t/*!")
	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを追加する")
	out.WriteLine(Ex"\t*/")
	out.WriteLine(Ex"\tSub Add" & eventName & "(h As " & handlerType & ")")
	out.WriteLine(Ex"\t\tIf IsNothing(" & eventMember & ") Then")
	out.WriteLine(Ex"\t\t\t" & eventMember & " = New " & handlerType)
	out.WriteLine(Ex"\t\tEnd If")
	out.WriteLine(Ex"\t\t" & eventMember & " += h")
	out.WriteLine(Ex"\tEnd Sub")
	out.WriteLine(Ex"\t/*!")
	out.WriteLine(Ex"\t@brief " & eventName & "イベントハンドラを削除する")
	out.WriteLine(Ex"\t*/")
	out.WriteLine(Ex"\tSub Remove" & eventName & "(h As " & handlerType & ")")
	out.WriteLine(Ex"\t\tIf Not IsNothing(" & eventMember & ") Then")
	out.WriteLine(Ex"\t\t\t" & eventMember & " -= h")
	out.WriteLine(Ex"\t\tEnd If")
	out.WriteLine(Ex"\tEnd Sub")
	out.WriteLine("Protected")
	out.WriteLine(Ex"\t/*!")
	out.WriteLine(Ex"\t@brief " & comment)
	out.WriteLine(Ex"\t*/")
	out.WriteLine(Ex"\tVirtual Sub On" & eventName & "(e As " & argsType & ")")
	out.WriteLine(Ex"\t\tIf Not IsNothing(" & eventMember & ") Then")
	out.WriteLine(Ex"\t\t\t" & eventMember & "(This, e)")
	out.WriteLine(Ex"\t\tEnd If")
	out.WriteLine(Ex"\tEnd Sub")
	out.WriteLine("Private")
	out.WriteLine(Ex"\t" & eventMember & " As " & handlerType)
	out.WriteLine()
End Sub

'OutputEventHandlerCode("PaintDC", "PaintDCEventHandler", 
'	"ウィンドウの描画が必要なときに呼び出されます。")

Sub MakeControlEvent(t As String)
	Dim event As String, handler As String, comment As String
	Dim n As DWord, i As DWord
	/*Using*/ Dim in = New StreamReader(t + "EventList.txt")
		/*Using*/ Dim out = New StreamWriter(t + "Event.sbp")
			Do
				Dim s = in.ReadLine
				If IsNothing(s) Then
					Exit Do
				End If
				If s[0] = Asc("'") Then Continue
				Dim a = ActiveBasic.Strings.Detail.Split(s, 9) 'Tab
				If a.Count >= 3 Then
					OutputEventHandlerCode(out, a[0], a[1], a[2])
				End If
			Loop
		in.Close() 'End Using
	out.Close() 'End Using
End Sub

MakeControlEvent("Control")
End
