#include Once "window9.bi"

Dim Shared As Hbitmap bmpShared

Dim Shared As Long iWidthIm , iHeightIm , iXWinStartDraw , iYWinStartDraw

Dim Shared As HDC cr

Dim Shared As Hwnd hwndDraw

Dim Shared As Double iArrayDot (1) = {2,2}

Dim Shared As Double iArrayDash(1) = {18,6}

Dim Shared As Double iArrayDashDot(3) = {9, 6 , 3 , 6}

Dim Shared As Double iArrayDashDotDot(5) = {9, 3 , 3 , 3 , 3 , 3}

Dim Shared As Byte bFlagWindowStartDraw = 1

Dim Shared As cairo_surface_t Ptr surf

Dim Shared As PangoFontDescription Ptr desc


Function Imagestartdraw(hBit As HBITMAP) As HDC Export
	
	bFlagWindowStartDraw = 0
	
	iWidthIm = gdk_pixbuf_get_width(hBit)
	
	iHeightIm = gdk_pixbuf_get_height(hBit)
	
	bmpShared = hBit
	
	surf = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(bmpShared),CAIRO_FORMAT_ARGB32,iWidthIm,iHeightIm,gdk_pixbuf_get_rowstride(bmpShared))
	
	cr = cairo_create (surf)
	
	cairo_set_antialias(cr,CAIRO_ANTIALIAS_NONE)
	
	'cairo_paint(cr)
	
	Return cr
	
End Function

Function Windowstartdraw(hwnd As Hwnd, iX As Long = 0 , iY As Long = 0 , iWidth As Long = 0 , iHeight As Long = 0 , iAlpha_Flag As Long = 0 , iAlpha_Value As Long = 0) As HDC Export ' параметры iAlpha_Flag и iAlpha_Value не поддерживаются
	
	bFlagWindowStartDraw = 1
	
	If iWidth = 0 Orelse iHeight = 0 Then
		
		iWidthIm = Cint(g_object_get_data (Cast(Any Ptr, hwnd), "W_SIZE"))
		
		iHeightIm = Cint(g_object_get_data (Cast(Any Ptr, hwnd), "H_SIZE"))
		
	Else
		
		iWidthIm = iWidth
		
		iHeightIm = iHeight
		
	Endif
	
	iXWinStartDraw = iX
	
	iYWinStartDraw = iY
	
	hwndDraw = hwnd
	
	bmpShared = gdk_pixbuf_new (GDK_COLORSPACE_RGB, True, 8 , iWidthIm, iHeightIm)
		
	'gdk_pixbuf_fill(bmpShared , &h000000FF)
	
	surf = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(bmpShared),CAIRO_FORMAT_ARGB32,iWidthIm,iHeightIm,gdk_pixbuf_get_rowstride(bmpShared))
	
	cr = cairo_create (surf)
	
	cairo_set_antialias(cr,CAIRO_ANTIALIAS_NONE)
	
	cairo_set_source_rgba (cr, 0 , 0 , 0 , 1)
	
	cairo_paint(cr)
	
	Return cr
	
End Function 

Function Stopdraw() As Integer Export
	
	If bFlagWindowStartDraw = 1 Then
		
		Dim As ListT Ptr pListTemp
		
		Dim As gdkwindow Ptr gdkwin

		pListTemp = Cast(ListT Ptr,pGlobalTypeWindow9->ListWinAndContainers->findNodeFunc(Cint(hwndDraw)))
		
		If pListTemp Then
		
			Dim As Any Ptr mainBox = Cast(hwnd , pListTemp->anyTwoData)
			
			If g_object_get_data (Cast(Any Ptr, pGlobalTypeWindow9->CurentHwnd), "SCROLL") Then
				
				gdkwin = gtk_widget_get_window (mainbox)
				
			Else
			
				gdkwin = gtk_layout_get_bin_window (mainbox)
				
			Endif
		
		Endif
		
		Dim As HDC cr0 = gdk_cairo_create (gdkwin)

		If cr0 Then
			
			gdk_cairo_set_source_pixbuf(cr0, bmpShared , iXWinStartDraw, iYWinStartDraw)
			
			cairo_paint(cr0)
			
			cairo_destroy (cr0)		
			
		Endif
		
		g_object_unref(bmpShared)
		
	Endif
	
	cairo_destroy (cr)
	
	cairo_surface_destroy(surf)
	
	desc  = Cast(PangoFontDescription Ptr , 0)
	
	Return 0
	
End Function

Sub Pixdraw( iX As Long , iY As Long , iColor As Long) Export

	If iX < 0 Orelse iY < 0 Orelse _
	iX >= iWidthIm Orelse iY >= iHeightIm Then
		
		Exit Sub
		
	Endif
	
	cairo_surface_flush(surf)
	
	Dim As Integer iStride = cairo_image_surface_get_stride(surf)
	
	Dim As Ulong Ptr pPixels = Cast(Any Ptr,cairo_image_surface_get_data(surf))	
	
	(Cast(Long Ptr ,(Cint(pPixels) + Cint((iY) * iStride))))[iX] = iColor Or (&hFF Shl 24)
	
	cairo_surface_mark_dirty(surf)	
	
End Sub

Function  Boxdraw (iX As Long , iY As Long , iWidth As Long , iHeight As Long , iColorPen As Long = 0, iColorBG As Long = 0 , iWidthPen As Long = 1 ,iStylePen As Long  = PS_SOLID, iAlpha As Long = 255) As Long Export
	
	Dim As Double rPen , gPen , bPen , rBG , gBG , bBG
	
	rPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColorPen And &h00FF0000) Shr 8) Or (iColorPen And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColorPen And &h0000FF00) Or (iColorPen And &h0000FF00) Shr 8)/&hFFFF		
	
	rBG = Cast(Ushort,((iColorBG And &h000000FF)Shl 8) Or (iColorBG And &h000000FF))/&hFFFF
	
	bBG = Cast(Ushort,((iColorBG And &h00FF0000) Shr 8) Or (iColorBG And &h00FF0000) Shr 16)/&hFFFF
	
	gBG = Cast(Ushort,(iColorBG And &h0000FF00) Or (iColorBG And &h0000FF00) Shr 8)/&hFFFF
	
	Select Case iStylePen
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)	
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	If iStylePen <> PS_NULL Then
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , iAlpha/225)
		
		cairo_rectangle (cr, iX  , iY  , iWidth , iHeight )
		
		cairo_stroke (cr)
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , iAlpha/225)	

		Endif
		
		cairo_rectangle (cr, iX+iWidthPen/2 , iY+iWidthPen/2 , iWidth-iWidthPen , iHeight-iWidthPen)
		
		cairo_fill (cr)			
		
	Else
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , iAlpha/225)	

		Endif
		
		cairo_rectangle (cr, iX , iY , iWidth , iHeight)
		
		cairo_fill (cr)			
		
	Endif	
	
	Return 1
	
End Function

Function  Polygondraw (pPoint As GdkPoint Ptr , iNCount As Long , iFillColor As Long = 0, iBorderColor As Long = 0 , iBorderWidth As Long = 1 ,iBorderStyle As Long  = PS_SOLID) As Long Export 
	
	Dim As Double rPen , gPen , bPen , rBG , gBG , bBG
	
	rPen = Cast(Ushort,((iBorderColor And &h000000FF)Shl 8) Or (iBorderColor And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iBorderColor And &h00FF0000) Shr 8) Or (iBorderColor And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iBorderColor And &h0000FF00) Or (iBorderColor And &h0000FF00) Shr 8)/&hFFFF		
	
	rBG = Cast(Ushort,((iFillColor And &h000000FF)Shl 8) Or (iFillColor And &h000000FF))/&hFFFF
	
	bBG = Cast(Ushort,((iFillColor And &h00FF0000) Shr 8) Or (iFillColor And &h00FF0000) Shr 16)/&hFFFF
	
	gBG = Cast(Ushort,(iFillColor And &h0000FF00) Or (iFillColor And &h0000FF00) Shr 8)/&hFFFF
	
	Select Case iBorderStyle
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)	
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	If iBorderStyle <> PS_NULL Then
		
		cairo_set_line_width (cr, iBorderWidth)
		
		cairo_set_source_rgb (cr, bPen, gPen, rPen )
		
		cairo_move_to (cr, pPoint->x, pPoint->y)
		
		For i As Long = 1 To iNCount - 1
			
			cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)		
			
		Next 
		
		cairo_close_path(cr)
		
		cairo_stroke (cr)	
		
	Endif
	
	cairo_set_source_rgb (cr, bBG, gBG, rBG )
	
	cairo_move_to (cr, pPoint->x, pPoint->y)
	
	For i As Long = 1 To iNCount - 1
		
		cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)		
		
	Next 
	
	cairo_close_path(cr)
	
	cairo_fill (cr)			
	
	Return 1
	
End Function

Function  Polylinedraw (pPoint As GdkPoint Ptr , iNCount As Long , iColorPen As Long = 0 , iWidthPen As Long = 1 ,iStylePen As Long  = PS_SOLID) As Long Export
	
	Dim As Double rPen , gPen , bPen , rBG , gBG , bBG
	
	rPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColorPen And &h00FF0000) Shr 8) Or (iColorPen And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColorPen And &h0000FF00) Or (iColorPen And &h0000FF00) Shr 8)/&hFFFF		
	
	Select Case iStylePen
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)	
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	If iStylePen <> PS_NULL Then
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_set_source_rgb (cr, bPen, gPen, rPen )
		
		cairo_move_to (cr, pPoint->x, pPoint->y)
		
		For i As Long = 1 To iNCount - 1
			
			cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)		
			
		Next 
		
		cairo_stroke (cr)	
		
	Endif		
	
	Return 1
	
End Function


Function Linedraw (iX As Long , iY As Long , iX1 As Long , iY1 As Long , iWidthPen As Long = 1 , iColor As Long = 0 , iStylePen As Long  = PS_SOLID) As Long Export
	
	Dim As Double rPen , gPen , bPen 	
	
	rPen = Cast(Ushort,((iColor And &h000000FF)Shl 8) Or (iColor And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColor And &h00FF0000) Shr 8) Or (iColor And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColor And &h0000FF00) Or (iColor And &h0000FF00) Shr 8)/&hFFFF
	
	Select Case iStylePen
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)	
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	cairo_set_line_width (cr, iWidthPen)
	
	cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND)
	
	cairo_set_source_rgb (cr, bPen, gPen, rPen  )
	
	cairo_move_to (cr, iX, iY)
	
	cairo_line_to (cr, iX1, iY1)
	
	cairo_stroke (cr)			
	
	Return 1
	
End Function

Function  Circledraw (iX As Long , iY As Long , iRadius As Long , iColorPen As Long = 0, iColorBG As Long = 0 , iWidthPen As Long = 1 ,iStylePen As Long  = PS_SOLID, iAlpha As Long = 255) As Long Export
	
	Dim As Double rPen , gPen , bPen , rBG , gBG , bBG
	
	rPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColorPen And &h00FF0000) Shr 8) Or (iColorPen And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColorPen And &h0000FF00) Or (iColorPen And &h0000FF00) Shr 8)/&hFFFF		
	
	rBG = Cast(Ushort,((iColorBG And &h000000FF)Shl 8) Or (iColorBG And &h000000FF))/&hFFFF
	
	bBG = Cast(Ushort,((iColorBG And &h00FF0000) Shr 8) Or (iColorBG And &h00FF0000) Shr 16)/&hFFFF
	
	gBG = Cast(Ushort,(iColorBG And &h0000FF00) Or (iColorBG And &h0000FF00) Shr 8)/&hFFFF
	
	Select Case iStylePen
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	If iStylePen <> PS_NULL Then
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_set_source_rgba (cr, bPen , gPen, rPen, iAlpha/225)
		
		cairo_arc (cr, iX  , iY , iRadius, 0 , 2*M_PI)
		
		cairo_stroke (cr)
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , iAlpha/225)	

		Endif
		
		cairo_arc (cr, iX  , iY , iRadius - iWidthPen/2, 0 , 2*M_PI)
		
		cairo_fill (cr)			
		
	Else
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , iAlpha/225)	

		Endif
		
		cairo_arc (cr, iX  , iY , iRadius, 0 , 2*M_PI)
		
		cairo_fill (cr)			
		
	Endif
	
	Return 1
	
End Function

Function Imagedraw( pImg As HBITMAP, iX As Long , iY As Long , iAlpha As Long = 255) As Long Export
	
	Dim As HBITMAP img = gdk_pixbuf_copy(pImg) 
	
	Dim As Ubyte Ptr pPixels = gdk_pixbuf_get_pixels(img)
	
	Dim As Integer iStride = gdk_pixbuf_get_rowstride(img)
	
	Dim As Long iHeight , iWidth 
	
	iHeight = gdk_pixbuf_get_height(img)
	
	iWidth = gdk_pixbuf_get_width(img)
	
	For y As Long = 0 To iHeight  -1
		
		Dim As Long Ptr row = Cast(Any Ptr,pPixels)
		
		For x As Long = 0 To iWidth-1
			
			Dim As Long iAlphaImg = (row[x] And &hFF000000) Shr 24
			
			If iAlphaImg < iAlpha Then
				
				row[x] = (row[x] And &hFF00FF00) Or ((row[x] And &h00FF0000) Shr 16) Or ((row[x] And &h000000FF) Shl 16)
				
			Else
				
				row[x] = (iAlpha Shl 24) Or (row[x] And &h0000FF00) Or ((row[x] And &h00FF0000) Shr 16) Or ((row[x] And &h000000FF) Shl 16)
				
			Endif
			
		Next
		
		pPixels += iStride
		
	Next		
	
	gdk_cairo_set_source_pixbuf(cr, Img, iX, iY)
	
	cairo_paint(cr)
	
	cairo_fill (cr)
	
	g_object_unref(img)
	
	Return 1
	
End Function

Function Getpix( iX As Long , iY As Long) As Ulong Export

	If iX < 0 Orelse iY < 0 Orelse _
	iX >= iWidthIm Orelse iY >= iHeightIm Then
		
		Return 0
		
	Endif
	
	cairo_surface_flush(surf)
	
	Dim As Integer iStride = cairo_image_surface_get_stride(surf)
	
	Dim As Ulong Ptr pPixels = Cast(Any Ptr,cairo_image_surface_get_data(surf))	
	
	Function = ((Cast(Long Ptr ,(Cint(pPixels) + Cint((iY) * iStride))))[iX]) And &hFFFFFF
	
	cairo_surface_mark_dirty(surf)
	
End Function

Type Int___Pt2D
	
	As Integer x, y
	
End Type

Sub Fillrectdraw( iX  As Long ,  iY As Long , iColor As Long) Export
	
	cairo_surface_flush(surf)
	
	If iColor And &hFF000000 Then
		
	Else
		
		iColor Or = &hFF000000
		
	Endif	
	
	Dim As Long iTop , iTemp , iOldcol , iNcol = iColor
	
	Dim As Integer iStride = cairo_image_surface_get_stride(surf)
	
	Dim As Ulong Ptr pPixels = Cast(Any Ptr,cairo_image_surface_get_data(surf))	
	
	iOldcol = (Cast(Long Ptr ,(Cint(pPixels) + Cint((iY) * iStride))))[iX]	
	
	If iOldcol = iColor Then
		
		cairo_surface_mark_dirty(surf)
		
		Exit Sub
		
	Endif
	
	Redim As Int___Pt2D Stack(3)
	
	Stack(0) = Type(iX, iY)
	
	Do Until iTop = -1
		
		If Stack(iTop).x > iWidthIm-1 Orelse Stack(iTop).x < 0 Orelse Stack(iTop).y > iHeightIm-1 Orelse Stack(iTop).y < 0 Then
			
			iTop -= 1
			
			Continue Do
			
		Endif
		
		iTemp = (Cast(Long Ptr ,(Cint(pPixels) + Cint(Stack(iTop).y * iStride))))[Stack(iTop).x]			
		
		If iOldcol = iTemp Then
			
			(Cast(Long Ptr ,(Cint(pPixels) + Cint(Stack(iTop).y * iStride))))[Stack(iTop).x] = (iNcol Or (&hFF Shl 24))			
			
			If iTop + 3 > Ubound(Stack) Then Redim Preserve Stack(Ubound(Stack)*2)
			
			Stack(iTop+1) = Type(Stack(iTop).x-1, Stack(iTop).y)
			
			Stack(iTop+2) = Type(Stack(iTop).x, Stack(iTop).y+1)
			
			Stack(iTop+3) = Type(Stack(iTop).x, Stack(iTop).y-1)
			
			Stack(iTop).x += 1
			
			iTop += 4
			
		End If
		
		iTop -= 1
		
	Loop
	
	cairo_surface_mark_dirty(surf)	
	
End Sub

Sub Focusdraw( iX As Long , iY As Long , iWidth As Long , iHeight As Long , iColor As Long = 0) Export 
	
	Dim As Double rPen , gPen , bPen 
	
	rPen = Cast(Ushort,((iColor And &h000000FF)Shl 8) Or (iColor And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColor And &h00FF0000) Shr 8) Or (iColor And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColor And &h0000FF00) Or (iColor And &h0000FF00) Shr 8)/&hFFFF
	
	cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
	
	cairo_set_line_width (cr, 1)
	
	cairo_set_source_rgb (cr, bPen, gPen, rPen )
	
	cairo_rectangle (cr, iX  , iY , iWidth , iHeight )
	
	cairo_stroke (cr)	
	
End Sub

Function  Rounddraw (iX As Long , iY As Long , iWidth As Long , iHeight As Long , iColorPen As Long = 0, iColorBG As Long = 0 , iWidthPen As Long = 1 ,iStylePen As Long  = PS_SOLID, iAlpha As Long = 255) As Long Export
	
	Dim As Double rPen , gPen , bPen , rBG , gBG , bBG
	
	rPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColorPen And &h00FF0000) Shr 8) Or (iColorPen And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColorPen And &h0000FF00) Or (iColorPen And &h0000FF00) Shr 8)/&hFFFF		
	
	rBG = Cast(Ushort,((iColorBG And &h000000FF)Shl 8) Or (iColorBG And &h000000FF))/&hFFFF
	
	bBG = Cast(Ushort,((iColorBG And &h00FF0000) Shr 8) Or (iColorBG And &h00FF0000) Shr 16)/&hFFFF
	
	gBG = Cast(Ushort,(iColorBG And &h0000FF00) Or (iColorBG And &h0000FF00) Shr 8)/&hFFFF
	
	Select Case iStylePen
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)	
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	Dim As Double iRadius , iScale ,  dX , dY
	
	If iWidth >= iHeight Then
		
		iRadius = iWidth / 2
		
		iScale = iHeight/iWidth
		
		cairo_scale (cr, 1 , iScale)
		
		dX = iX + iWidth/2
		
		dY = (iY/iHeight)*iWidth + iRadius
		
	Else
		
		iRadius = iHeight / 2
		
		iScale = iWidth/iHeight
		
		cairo_scale (cr, iScale , 1)
		
		dX = (iX/iWidth)*iHeight + iRadius
		
		dY = iY + iHeight/2
		
	Endif
	
	If iStylePen <> PS_NULL Then
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , iAlpha/225)
		
		cairo_arc (cr, dx , dY, iRadius, 0, 2 * M_PI)
		
		cairo_stroke (cr)
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , iAlpha/225)	

		Endif
		
		cairo_arc (cr, dx , dY, iRadius - iWidthPen/2, 0, 2 * M_PI)
		
		cairo_fill (cr)			
		
	Else
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , iAlpha/225)	

		Endif
		
		cairo_arc (cr, dx , dY, iRadius, 0, 2 * M_PI)
		
		cairo_fill (cr)			
		
	Endif
	
	cairo_identity_matrix(cr)	
	
	Return 1
	
End Function

Function  Gradientfilldraw (iX As Long , iY As Long , iWidth As Long , iHeight As Long ,  Rbegin As Long, Gbegin As Long , Bbegin As Long,  REnd As Long, GEnd As Long, BEnd As Long, GOR_VERT As Long = 0) As Long Export
	
	Dim As cairo_pattern_t Ptr pat
	
	If GOR_VERT Then
		
		pat = cairo_pattern_create_linear(iX, iY, iX, iY+iHeight)
		
	Else
		
		pat = cairo_pattern_create_linear(iX, iY, iX+iWidth, iY)
		
	Endif
	
	cairo_pattern_add_color_stop_rgb(pat, 0, Bbegin/&hFFFF, Gbegin/&hFFFF, Rbegin/&hFFFF)
	
	cairo_pattern_add_color_stop_rgb(pat, 1, BEnd/&hFFFF, GEnd/&hFFFF, REnd/&hFFFF)
	
	cairo_rectangle(cr, iX, iY, iWidth, iHeight)
	
	cairo_set_source(cr, pat)
	
	cairo_fill(cr) 
	
	cairo_pattern_destroy(pat)	
	
	Return 1
	
End Function

Function Textdraw( iX As Long , iY As Long , sText As String , ColorBK As Long = 0, ColorText  As Long = 0, iAlpha As Long = 255) As Long Export
	
	Dim As Double rPen , gPen , bPen
	
	Dim As PangoLayout Ptr layout
	
	Dim As PangoFontDescription Ptr desc_temp

	layout = pango_cairo_create_layout (cr)
	
	If desc Then
		
		desc_temp = pango_font_description_copy(desc)
		
		pango_layout_set_font_description (layout, desc_temp)
		
		pango_font_description_free (desc_temp)		
		
	Endif

	pango_layout_set_text (layout, Strptr(sText) , -1)

	Dim As PangoRectangle pr_ink , pr_log

	pango_layout_get_pixel_extents (layout, @pr_ink, @pr_log)
	
	cairo_move_to(cr, iX, iY )

	If ColorBK <> Clng(-1) Then
		
		Dim As Double rPen , gPen , bPen
		
		rPen = Cast(Ushort,((ColorBK And &h000000FF)Shl 8) Or (ColorBK And &h000000FF))/&hFFFF
		
		bPen = Cast(Ushort,((ColorBK And &h00FF0000) Shr 8) Or (ColorBK And &h00FF0000) Shr 16)/&hFFFF
		
		gPen = Cast(Ushort,(ColorBK And &h0000FF00) Or (ColorBK And &h0000FF00) Shr 8)/&hFFFF		
		
		cairo_set_source_rgba(cr, bPen, gPen, rPen, iAlpha/255)
		
		cairo_rectangle (cr, iX , iY , pr_log.Width , pr_log.Height)
		
		cairo_fill (cr)	
		
	Endif
	
	cairo_move_to(cr, iX, iY)
	
	rPen = Cast(Ushort,((ColorText And &h000000FF)Shl 8) Or (ColorText And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((ColorText And &h00FF0000) Shr 8) Or (ColorText And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(ColorText And &h0000FF00) Or (ColorText And &h0000FF00) Shr 8)/&hFFFF
	
	cairo_set_source_rgba(cr, bPen, gPen, rPen, iAlpha/255)
	
	pango_cairo_update_layout (cr, layout)
	
	pango_cairo_show_layout (cr, layout)

	cairo_fill (cr)

	g_object_unref (layout)
	
	Return 0
	
End Function

Sub Fontdraw(iFont As Integer) Export
	
	desc  = Cast(PangoFontDescription Ptr , iFont)

End Sub

Function GetRadPie(iX1 As Double  , iY1 As Double , dX As Double , dY As Double ) As Double 

	Dim As Double dRad

	If iX1 > dX Andalso iY1 > dY Then
		
		dRad = Atn((iY1 - dY ) / (iX1 - dX))
		
	Elseif iX1 < dX Andalso iY1 < dY Then	

		dRad = M_PI + Atn((dY - iY1) / (dX - iX1))
		
	Elseif iX1 > dX Andalso iY1 < dY Then
	
		dRad = (M_PI*2)- Atn(((dY - iY1) / (iX1-dX)))
		
	Elseif iX1 < dX Andalso iY1 > dY Then	
	
		dRad = M_PI - Atn((iY1 - dY ) / (dX - iX1))
		
	Elseif iX1 < dX Andalso iY1 = dY Then
	
		dRad = M_PI
		
	Elseif iX1 > dX Andalso iY1 = dY Then
	
		dRad = 0
		
	Elseif iX1 = dX Andalso iY1 < dY Then
	
		dRad =M_PI + M_PI / 2
		
	Elseif iX1 = dX Andalso iY1 > dY Then	
	
		dRad = M_PI / 2

	Endif	
	
	Return dRad

End Function


Function Piedraw (iX As Long , iY As Long , iWidth As Long , iHeight As Long , iX1 As Long , iY1 As Long , iX2 As Long , iY2 As Long , iColorPen As Long = 0, iColorBG As Long = 0 , iWidthPen As Long = 1 ,iStylePen As Long  = PS_SOLID) As Long Export
	
	Dim As Double rPen , gPen , bPen , rBG , gBG , bBG
	
	rPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
	
	bPen = Cast(Ushort,((iColorPen And &h00FF0000) Shr 8) Or (iColorPen And &h00FF0000) Shr 16)/&hFFFF
	
	gPen = Cast(Ushort,(iColorPen And &h0000FF00) Or (iColorPen And &h0000FF00) Shr 8)/&hFFFF		
	
	rBG = Cast(Ushort,((iColorBG And &h000000FF)Shl 8) Or (iColorBG And &h000000FF))/&hFFFF
	
	bBG = Cast(Ushort,((iColorBG And &h00FF0000) Shr 8) Or (iColorBG And &h00FF0000) Shr 16)/&hFFFF
	
	gBG = Cast(Ushort,(iColorBG And &h0000FF00) Or (iColorBG And &h0000FF00) Shr 8)/&hFFFF
	
	Select Case iStylePen
		
		Case PS_DASH
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDash(0)) , 2 , 0)
		
		Case PS_DOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDot(0)) , 2 , 0)
		
		Case PS_DASHDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDot(0)) , 4 , 0)
		
		Case PS_DASHDOTDOT
		
			cairo_set_dash (cr , Cast(Any Ptr,@iArrayDashDotDot(0)) , 6 , 0)	
		
		Case PS_SOLID
		
			cairo_set_dash (cr ,0 , 0 , 0)
		
	End Select
	
	Dim As Double iRadius , iScale ,  dX , dY 
	
	If iWidth >= iHeight Then
		
		iRadius = iWidth / 2
		
		dX = iX + iWidth/2
		
		dY = iY + iWidth/2
		
	Else
		
		iRadius = iHeight / 2
		
		dX = iX + iHeight/2
		
		dY = iY + iHeight/2
		
	Endif
	
	Dim As Double dRad1 , dRad2

	dRad1 = GetRadPie( iX1, iY1 , dX  , dY)
	
	dRad2 = GetRadPie( iX2, iY2 , dX  , dY )
	
	If iStylePen <> PS_NULL Then
		
		If iColorBG = -1 Then
	
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 0)
			
		Else
		
			cairo_set_source_rgba (cr, bBG, gBG, rBG , 225)	

		Endif
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_arc_negative (cr, dx , dY, iRadius, dRad1, dRad2)
		
		cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_fill (cr)	
		
		cairo_set_source_rgb (cr, bPen, gPen, rPen)
		
		cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_arc_negative (cr, dx , dY, iRadius, dRad1, dRad2)
		
		cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_stroke (cr)	

	Else
		
		cairo_set_source_rgb (cr, bBG, gBG, rBG)
		
		cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_arc_negative (cr, dx , dY, iRadius, dRad1, dRad2)
		
		cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_fill (cr)		
		
	Endif	
	
	Return 1
	
End Function