'#Define AnimateCloseMinimize
'#Define PositionFrameNone
#Include Once "fbgfx.bi"

Using fb

Namespace minigui
	Enum Caption
		CaptionClick=100 
		CaptionClose 
		CaptionMinimize
	End Enum

	#Ifndef AllEvent
	Dim Shared AllEvent As EVENT
	#EndIf

	Type Dialog
		x As Integer
		y As Integer
		xDisplay As Integer
		yDisplay As Integer
		Width As Integer
		Height As Integer
		TextCaption As ZString Ptr
		LenText As Integer
		BackGroundCaption As Integer = &hE0E0E0
		ColorCaption As Integer = &h0
		ColorText As Integer = &h0
		ColorMiniFrame As Integer = &h0
		ColorMinimize As Integer = &hFF0000
		ColorEnd As Integer = &hFF0000
		ColorActive As Integer = &hFFFFFF
		ColorNoActive As Integer = &h0
		Image As Any Ptr
		HBorder As Any Ptr
		VBorder As Any Ptr
		MX As Integer
		MY As Integer
		Wx As Integer
		Wy As Integer
		CaptionClickflag As Byte
		MiniFramehideflag As Byte
		TextCaptionhideflag As Byte
		minimizehideflag As Byte
		endhideflag As Byte
		Minflag As Byte
		closeflag As Byte
		r As UByte
		g As UByte
		b As UByte
		a As UByte = 255
		#Ifdef __FB_WIN32__
		#Include Once "windows.bi"
		As HICON icon ' хендл для икнки
		As HWND hw
		#Else
		hw As Integer
		#EndIf

		Declare Constructor( _
		x As Integer,_
		y As Integer,_
		Width As Integer,_
		Height As Integer,_
		TextCaption  As ZString Ptr, _
		ColorCaption As Integer = 0, _
		BackGround As Integer = &hE0E0E0, _
		ColorText As Integer = &h0 _
		)
		Declare Destructor()
		Declare Sub ScreenCreate()
		Declare Sub DrawFrame()
		Declare Sub Update()
		Declare Function MouseCaptionClick() As Integer
		Declare Sub FrameClick()
		Declare Sub GetColor(Col As Integer)
		Declare Sub SetColorCaption(Col As Integer)
		Declare Sub SetColorMiniFrame(ColorMiniFrame As Integer)
		Declare Sub SetColorText(ColorText As Integer)
		Declare Sub SetTextCaption(Text As ZString Ptr)
		Declare Sub SetColorCaptionBg(BackGround As Integer)
		Declare Sub SetColorFrameActive(ColorActive As Integer)
		Declare Sub SetColorFrameNoActive(ColorNoActive As Integer)
		Declare Sub SetColorEndButton(ColorEnd As Integer)
		Declare Sub SetColorMinimizeButton(ColorMinimize As Integer)
		Declare Sub SetPos(x As Integer , y As Integer)
		Declare Sub SetText(Text As ZString Ptr)
		Declare Sub SetSize(Width As Integer , Height As Integer)
		Declare Sub HideEndButton(endhideflag As Byte)
		Declare Sub HideMinimizeButton(minimizehideflag As Byte)
		Declare Sub HideTextCaption(TextCaptionhideflag As Byte)
		Declare Sub HideMiniFrame(MiniFramehideflag As Byte)
	End Type

	Constructor Dialog( _
	x As Integer,_
	y As Integer,_
	Width As Integer,_
	Height As Integer,_
	TextCaption  As ZString Ptr, _
	ColorCaption As Integer = 0, _
	BackGround As Integer = &hE0E0E0, _
	ColorText As Integer = &h0 _
	)
	this.xDisplay = x
	this.yDisplay = y
	this.width = Width
	this.height = Height
	this.TextCaption = TextCaption
	This.ColorCaption=ColorCaption
	This.BackGroundCaption=BackGroundCaption
	This.ColorText=ColorText
	LenText = Len(*TextCaption)
	ScreenCreate()
	End Constructor

	Destructor Dialog()
	If Image<>0 Then ImageDestroy(Image)
	If VBorder<>0 Then ImageDestroy(VBorder)
	If HBorder<>0 Then ImageDestroy(HBorder)
	#Ifdef __FB_WIN32__
	If Icon<>0 Then DestroyIcon(icon)
	#EndIf
	End Destructor

	Sub Dialog.ScreenCreate()
		If Width <>0 And Height<>0 Then
			ScreenRes Width,Height,32,2,8
			ScreenSet 1
			ScreenControl(GET_WINDOW_HANDLE,Cast(Integer,hw))
			#Ifdef __FB_WIN32__
			If icon<>0 Then DestroyIcon(icon)
			icon=LoadIcon(GetModuleHandle(0),MAKEINTRESOURCE(100))
			SendMessage(hw,WM_SETICON,ICON_BIG,Cast(Lparam,icon))
			#EndIf
			If xDisplay+yDisplay<>0 Then
				ScreenControl  SET_WINDOW_POS,xDisplay , yDisplay
			EndIf
			Image = ImageCreate(Width,Height)
			VBorder = ImageCreate(4,height)
			HBorder = ImageCreate(Width-8,4)
			DrawFrame()
		EndIf
	End Sub

	Sub Dialog.SetSize(Width As Integer,Height As Integer)
		this.Width=Width
		this.Height=Height
		ScreenCreate()
	End Sub

	Sub Dialog.GetColor(Col As Integer)
		r = CUByte(HiWord(Col))
		g = CUByte(LoWord(HiByte(Col)))
		b = CUByte(LoWord(LoByte(Col)))
	End Sub

	Sub Dialog.SetColorCaption(ColorCaption As Integer)
		this.ColorCaption = ColorCaption
		DrawFrame()
	End Sub

	Sub Dialog.SetColorText(ColorText As Integer)
		this.ColorText = ColorText
		DrawFrame()
	End Sub

	Sub Dialog.SetColorMiniFrame(ColorMiniFrame As Integer)
		this.ColorMiniFrame = ColorMiniFrame
		DrawFrame()
	End Sub

	Sub Dialog.SetColorCaptionBg(BackGroundCaption As Integer)
		this.BackGroundCaption = BackGroundCaption
		DrawFrame()
	End Sub

	Sub Dialog.SetColorFrameActive(ColorActive As Integer)
		this.ColorActive = ColorActive
		DrawFrame()
	End Sub

	Sub Dialog.SetColorFrameNoActive(ColorNoActive As Integer)
		this.ColorNoActive = ColorNoActive
		DrawFrame()
	End Sub

	Sub Dialog.SetColorEndButton(ColorEnd As Integer)
		this.ColorEnd = ColorEnd
		DrawFrame()
	End Sub

	Sub Dialog.SetColorMinimizeButton(ColorMinimize As Integer)
		this.ColorMinimize = ColorMinimize
		DrawFrame()
	End Sub

	Sub Dialog.HideEndButton(endhideflag As Byte)
		this.endhideflag = endhideflag
		DrawFrame()
	End Sub

	Sub Dialog.HideMinimizeButton(minimizehideflag As Byte)
		this.minimizehideflag = minimizehideflag
		DrawFrame()
	End Sub

	Sub Dialog.HideTextCaption(TextCaptionhideflag As Byte)
		this.TextCaptionhideflag = TextCaptionhideflag
		DrawFrame()
	End Sub

	Sub Dialog.HideMiniFrame(MiniFramehideflag As Byte)
		this.MiniFramehideflag = MiniFramehideflag
		DrawFrame()
	End Sub

	Sub Dialog.SetTextCaption(Text As ZString Ptr)
		TextCaption = Text
		LenText = Len(*text)
		DrawFrame()
	End Sub

	Function Dialog.MouseCaptionClick() As Integer
		If Mx >=0 And Mx <= Width -55 And My>0 And My<=30 Then
			Return CaptionClick
		ElseIf Mx >=Width-29 And Mx<=Width And My>0 And My<=20 Then
			Return CaptionClose
		ElseIf Mx >=Width-55 And Mx<=Width-30 And My>0 And My<=20 Then
			Return CaptionMinimize
		EndIf
	End Function

	Sub Dialog.DrawFrame()
		GetColor(ColorCaption)
		For i As Integer = 0 To 27
			Line Image,(-1,i)-Step(Width,0),RGBA(r,g,b,a+1+(i*(255\(27))))
		Next
		Line Image,(0,27)-Step(Width-1,height-1),BackGroundCaption,BF
		Line HBorder,(0,0)-(Width-8,4),RGBA(r,g,b,255),BF
		Line VBorder,(0,0)-(4,height),RGBA(r,g,b,255),BF

		If endhideflag = 0 Then
			GetColor(ColorNoActive)
			Line Image,(Width-29,1)-(Width-3,20),RGBA(r,g,b,255),B
			GetColor(ColorEnd)
			Draw String Image, (Width-18,6),"x",RGBA(r,g,b,255)
		EndIf

		#Ifdef __FB_WIN32__
		If minimizehideflag = 0 Then
			GetColor(ColorNoActive)
			Line Image, (Width-55,1)-(Width-29,20),RGBA(r,g,b,255),B
			GetColor(ColorMinimize)
			Draw String Image, (Width-44,7),"-",RGBA(r,g,b,255)
		EndIf
		#EndIf

		If MiniFramehideflag=0 Then
			GetColor(ColorMiniFrame)
			Line Image,((Width - LenText*8)\2-2,6)-Step(LenText*8+2,15),RGBA(r,g,b,40),BF
		EndIf
		If TextCaptionhideflag=0 Then
			GetColor(ColorText)
			Draw String Image,((Width - LenText*8)\2,10),*TextCaption,RGBA(this.r,this.g,this.b,255)
		EndIf
	End Sub

	Sub Dialog.Update()
		Line (0,0)-Step(Width-1,26),BackGroundCaption,BF
		Line (0,y+27)-Step(3,height-1),BackGroundCaption,BF
		Line (Width-4,y+27)-Step(Width-1,height-1),BackGroundCaption,BF
		Line (0,height-4)-Step(Width-1,height-1),BackGroundCaption,BF
		Put (x,y),Image,Alpha
		Put (4,height-4),HBorder,Alpha
		Put (Width-4,27),VBorder,Alpha
		Put (0,27),VBorder,Alpha
		#Ifndef AnimateCloseMinimize
		If Minflag=1 Then
			#Ifdef __FB_WIN32__
			Line (Width-55,1)-(Width-29,20),ColorActive,B
			#EndIf
		ElseIf closeflag = 1 Then
			Line (Width-29,1)-(Width-3,20),ColorActive,B
		EndIf
		#EndIf
	End Sub

	Sub Dialog.FrameClick()
		Select Case AllEvent.type
			Case EVENT_MOUSE_BUTTON_PRESS
				Dim As Integer Click=MouseCaptionClick()
				Select Case Click
					#Ifndef PositionFrameNone
					Case CaptionClick
						CaptionClickflag = 1
						#EndIf
					Case CaptionMinimize
						#Ifdef __FB_WIN32__
						ShowWindow(hw,SW_MINIMIZE)
						#Else
						CaptionClickflag = 1
						#EndIf
					Case CaptionClose
					    uFMOD_StopSong()
						End
				End Select
			Case EVENT_MOUSE_MOVE
				Mx=AllEvent.x:My=AllEvent.y
				#Ifndef PositionFrameNone
				If CaptionClickflag = 1 Then
					ScreenControl  GET_WINDOW_POS, wX, wY
					ScreenControl  SET_WINDOW_POS, wX+AllEvent.dx, wY+AllEvent.dy
				EndIf
				#EndIf
				#Ifndef AnimateCloseMinimize
				If My>0 And My<=30 Then
					If MouseCaptionClick()=CaptionMinimize Then
						Minflag = 1
					Else
						Minflag = 0
					EndIf
					If MouseCaptionClick()=CaptionClose Then
						closeflag = 1
					Else
						closeflag = 0
					EndIf
				EndIf
			Case EVENT_MOUSE_EXIT
				closeflag = 0
				Minflag = 0
				#EndIf
			Case EVENT_MOUSE_BUTTON_RELEASE
				CaptionClickflag = 0
		End Select
	End Sub
End Namespace

