#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 Byte bFlagWindowStartDraw = 1

Dim Shared As cairo_surface_t Ptr surf

Function Imagestartdrawa(Byref hBit As Any Ptr,Byval iColorFlag As Integer=0,Byval iColor As Integer=&hFFFFFFFF) As Any Ptr
	
	bFlagWindowStartDraw = 0
	
	iWidthIm = Image_width(hBit)
	
	iHeightIm = Image_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)
	
	If iColorFlag = 1 Then
		
		Dim As Double  rBG , gBG , bBG , aBG
		
		aBG = Cast(Ushort,((iColor And &hFF000000) Shr 16) Or (iColor And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColor And &h00FF0000) Shr 8) Or (iColor And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColor And &h0000FF00) Or (iColor And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColor And &h000000FF)Shl 8) Or (iColor And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba(cr , bBG , gBG , rBG , aBG)
		
		cairo_paint (cr)
		
	Endif
	
	Return cr
	
End Function

Function Windowstartdrawa(Byval hwnd As HWND,Byval x As Integer=0,Byval y As Integer=0,Byval w As Integer=0, Byval h As Integer=0,Byval iColorFlag As Integer=0,Byval iColor As Integer=&hFFFFFFFF) As Any Ptr
	
	bFlagWindowStartDraw = 1
	
	If w = 0 Orelse h = 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 = w
		
		iHeightIm = h
		
	Endif
	
	iXWinStartDraw = x
	
	iYWinStartDraw = y
	
	hwndDraw = hwnd
	
	bmpShared = gdk_pixbuf_new (GDK_COLORSPACE_RGB, True, 8 , iWidthIm, iHeightIm)
	
	gdk_pixbuf_fill(bmpShared , &hFFFFFF00)
	
	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)
	
	If iColorFlag = 1 Then
		
		Dim As Double  rBG , gBG , bBG , aBG
		
		aBG = Cast(Ushort,((iColor And &hFF000000) Shr 16) Or (iColor And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColor And &h00FF0000) Shr 8) Or (iColor And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColor And &h0000FF00) Or (iColor And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColor And &h000000FF)Shl 8) Or (iColor And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba(cr , bBG , gBG , rBG , aBG)
		
		cairo_paint (cr)
		
	Endif
	
	Return cr
	
End Function 

Function Stopdrawa() As Integer
	
	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)
	
	Return 0
	
End Function

Sub Setpixa( iX As Long , iY As Long , iColor As Long = &hff000000)
	
	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 And &hFF00FF00) Or ((iColor And &h00FF0000) Shr 16) Or ((iColor And &h000000FF) Shl 16) 
	
	cairo_surface_mark_dirty(surf)	
	
End Sub

Function Getpixa(Byval iX As Single,Byval iY As Single) As Integer
	
	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))	
	
	Dim As Long iColor = ((Cast(Long Ptr ,(Cint(pPixels) + Cint(iY * iStride))))[iX]) And &hFFFFFFFF
	
	Function = (iColor And &hFF00FF00) Or ((iColor And &h00FF0000) Shr 16) Or ((iColor And &h000000FF) Shl 16)
	
	cairo_surface_mark_dirty(surf)
	
End Function

Type BrushA	
	
	x0 As Long
	
	x1 As Long
	
	y0 As Long
	
	y1 As Long	
	
	image As Any Ptr
	
	image_tileX As Any Ptr
	
	image_tileY As Any Ptr
	
	image_tileXY As Any Ptr
	
	iWrap As Integer
	
End Type

Function Createbrusha(Byval x0 As Single=0,Byval y0 As Single=0,Byval x1 As Single=0,Byval y1 As Single=0,_
Byval iColor1 As Integer=&hFF00FF00, Byval iColor2 As Integer=&hFF0000FF,Byval GpImage As Any Ptr=0,Byval WrapMode As Integer=3 ) As Any Ptr
	
	Dim brush As BRUSHA Ptr = New BrushA
	
	if brush = 0 then
		
		print "Allocated memory error in Createbrusha!"
		
		return 0
		
	EndIf
	
	brush->iWrap = WrapMode
	
	If GpImage = 0 Then
		
		brush->x0 = x0
		
		brush->x1 = x1
		
		brush->y0 = y0
		
		brush->y1 = y1
		
		Dim As Long w , h
		
		w = Abs(x1 - x0)+1
		
		h = Abs(y1 - y0)+1
		
		brush->image = gdk_pixbuf_new (GDK_COLORSPACE_RGB, True, 8 , w , h)
		
		Dim As Single iColorAlpha1 , iColorRed1 , iColorGreen1 , iColorBlue1 , _
		iColorAlpha2 , iColorRed2 , iColorGreen2 , iColorBlue2
		
		Dim As cairo_surface_t Ptr surf
		
		Dim As HDC cr
		
		surf = cairo_image_surface_create_for_data(gdk_pixbuf_get_pixels(brush->image),CAIRO_FORMAT_ARGB32,w,h,gdk_pixbuf_get_rowstride(brush->image))
		
		cr = cairo_create (surf)
		
		iColorAlpha1 = Cast(Ushort,((iColor1 And &hFF000000) Shr 16) Or (iColor1 And &hFF000000) Shr 24)/&hFFFF
		
		iColorRed1 = Cast(Ushort,((iColor1 And &h00FF0000) Shr 8) Or (iColor1 And &h00FF0000) Shr 16)/&hFFFF
		
		iColorGreen1 = Cast(Ushort,(iColor1 And &h0000FF00) Or (iColor1 And &h0000FF00) Shr 8)/&hFFFF
		
		iColorBlue1 = Cast(Ushort,((iColor1 And &h000000FF)Shl 8) Or (iColor1 And &h000000FF))/&hFFFF
		
		iColorAlpha2 = Cast(Ushort,((iColor2 And &hFF000000) Shr 16) Or (iColor2 And &hFF000000) Shr 24)/&hFFFF
		
		iColorRed2 = Cast(Ushort,((iColor2 And &h00FF0000) Shr 8) Or (iColor2 And &h00FF0000) Shr 16)/&hFFFF
		
		iColorGreen2 = Cast(Ushort,(iColor2 And &h0000FF00) Or (iColor2 And &h0000FF00) Shr 8)/&hFFFF
		
		iColorBlue2 = Cast(Ushort,((iColor2 And &h000000FF)Shl 8) Or (iColor2 And &h000000FF))/&hFFFF
		
		Dim As cairo_pattern_t Ptr pat
		
		pat = cairo_pattern_create_linear(0, 0, w, h)
		
		cairo_pattern_add_color_stop_rgba(pat, 0, iColorRed1, iColorGreen1, iColorBlue1 , iColorAlpha1)
		
		cairo_pattern_add_color_stop_rgba(pat, 1, iColorRed2, iColorGreen2, iColorBlue2 , iColorAlpha2)
		
		cairo_rectangle(cr, 0, 0, w, h)
		
		cairo_set_source(cr, pat)
		
		cairo_fill(cr) 
		
		cairo_pattern_destroy(pat)
		
		cairo_destroy (cr)
		
		cairo_surface_destroy(surf)
		
	Else
		
		Dim As Long w , h
		
		brush->image = gdk_pixbuf_copy(GpImage)
		
		w = gdk_pixbuf_get_width(brush->image)
		
		h = gdk_pixbuf_get_height(brush->image)
		
		brush->x0 = 0
		
		brush->x1 = w - 1
		
		brush->y0 = 0
		
		brush->y1 = h - 1
		
	Endif
	
	If WrapMode = 1 Then
		
		brush->image_tileX = gdk_pixbuf_flip(brush->image , True)
		
	Elseif WrapMode = 2 Then
		
		brush->image_tileY = gdk_pixbuf_flip(brush->image , False)
		
	Elseif WrapMode = 3 Then
		
		brush->image_tileX = gdk_pixbuf_flip(brush->image , True)
		
		brush->image_tileY = gdk_pixbuf_flip(brush->image , False)
		
		brush->image_tileXY = gdk_pixbuf_flip(brush->image_tileY , True)
		
	Elseif WrapMode = 4 Then
		
		gdk_pixbuf_fill(brush->image , &hFFFFFF00)
		
	Endif
	
	Return brush
	
End Function

Sub Freebrusha(Byval Brush As Any Ptr)
	
	if Brush then
		
		Dim As BRUSHA Ptr pBrush = Brush
		
		If pBrush->image Then g_object_unref(pBrush->image)
		
		If pBrush->image_tileX Then g_object_unref(pBrush->image_tileX)
		
		If pBrush->image_tileY Then g_object_unref(pBrush->image_tileY)
		
		If pBrush->image_tileXY Then g_object_unref(pBrush->image_tileXY)
		
		Delete pBrush
		
	endif
	
End Sub

Function getImageFromBrushPoiter(pBrush As BRUSHA Ptr , iFlagX As Long  , iFlagY As Long) As Any Ptr
	
	Select Case pBrush->iWrap
			
		Case 0 , 4
			
			Return pBrush->image
			
		Case 1
			
			If iFlagX = 0 Then
				
				Return pBrush->image
				
			Else
				
				Return pBrush->image_tileX
				
			Endif
			
		Case 2
			
			If iFlagY = 0 Then
				
				Return pBrush->image
				
			Else
				
				Return pBrush->image_tileY
				
			Endif
			
		Case 3
			
			If iFlagX = 0 Andalso iFlagY = 0 Then
				
				Return pBrush->image
				
			Elseif iFlagX = 1  Andalso iFlagY = 1 Then
				
				Return pBrush->image_tileXY
				
			Elseif iFlagX = 1 Then
				
				Return pBrush->image_tileX
				
			Elseif iFlagY = 1 Then
				
				Return pBrush->image_tileY
				
			Endif
			
	End Select
	
End Function

Sub Boxdrawa(Byval iX As Single , Byval iY As Single , Byval iWidth As Single ,_
Byval iHeight As Single , Byval iColorPen As Integer=&hff000000 , Byval iFlagcolorBK As Integer=1 ,_
Byval iColorBk As Integer=&hff000000 , Byval brushPen As Any Ptr=0 , Byval brushBk As Any Ptr=0 , Byval iWidthPen As Single=1)
	
	If iWidth = 0 Orelse iHeight = 0 Then Exit Sub
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	Dim As Long iX1 = iWidth + iX - 1  , iY1 = iHeight + iY - 1 , iHalfWidthPen = iWidthPen\2
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_rectangle (cr, iX  , iY  , iWidth , iHeight )
		
		cairo_stroke (cr)
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img
		
		cairo_set_line_width (cr, iWidthPen)		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_rectangle (cr , iX  , iY  , iWidth , iHeight)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif
	
	If iFlagcolorBK = 1 Then
		
		aBG = Cast(Ushort,((iColorBk And &hFF000000) Shr 16) Or (iColorBk And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColorBk And &h00FF0000) Shr 8) Or (iColorBk And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColorBk And &h0000FF00) Or (iColorBk And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColorBk And &h000000FF)Shl 8) Or (iColorBk And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		cairo_rectangle (cr, iX , iY , iWidth , iHeight)
		
		cairo_fill (cr)	
		
	Elseif iFlagcolorBK = 2 Andalso brushBk Then
		
		Dim As BRUSHA Ptr pBrush = brushBk
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 - iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 - iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX + iHalfWidthPen , iStartY + iHalfWidthPen)
				
				cairo_rectangle (cr , iX  , iY , iWidth , iHeight)
				
				cairo_fill (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)
		
	Endif
	
End Sub

Sub Circledrawa(Byval iX As Single , Byval iY As Single , Byval iRadius As Single,_
Byval iColorPen As Integer=&hff000000 , Byval iFlagcolorBK As Integer=1, _
Byval iColorBk As Integer=&hff000000 , Byval brushPen As Any Ptr=0,_
Byval brushBk As Any Ptr=0 , Byval iWidthPen As Single=1)	
	
	If iRadius = 0 Then Exit Sub
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	Dim As Long iWidth = iRadius*2 , iHeight = iWidth
	
	Dim As Long iHalfWidthPen = iWidthPen\2
	
	Dim As Long iXL = iX - iRadius - iHalfWidthPen , iXL1 = iX + iRadius + iHalfWidthPen ,_
	iYL = iY - iRadius - iHalfWidthPen , iYL1 = iY + iRadius + iHalfWidthPen
	
	cairo_set_line_width (cr, iWidthPen)
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_arc (cr, iX  , iY , iRadius, 0 , 2*M_PI)
		
		cairo_stroke (cr)
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iXL1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iYL1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_arc (cr, iX  , iY , iRadius, 0 , 2*M_PI)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif
	
	If iFlagcolorBK = 1 Then
		
		aBG = Cast(Ushort,((iColorBk And &hFF000000) Shr 16) Or (iColorBk And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColorBk And &h00FF0000) Shr 8) Or (iColorBk And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColorBk And &h0000FF00) Or (iColorBk And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColorBk And &h000000FF)Shl 8) Or (iColorBk And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		cairo_arc (cr, iX  , iY , iRadius, 0 , 2*M_PI)
		
		cairo_fill (cr)	
		
	Elseif iFlagcolorBK = 2 Andalso brushBk Then
		
		Dim As BRUSHA Ptr pBrush = brushBk
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iXL1 - iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iYL1 - iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX + iHalfWidthPen , iStartY + iHalfWidthPen)
				
				cairo_arc (cr, iX  , iY , iRadius, 0 , 2*M_PI)
				
				cairo_fill (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)
		
	Endif
	
End Sub

Sub Rounddrawa(Byval iX As Single , Byval iY As Single , Byval iWidth As Single , Byval iHeight As Single , _
Byval iColorPen As Integer=&hff000000 , Byval iFlagcolorBK As Integer=1 , Byval iColorBk As Integer=&hff000000 , _
Byval brushPen As Any Ptr=0 , Byval brushBk As Any Ptr=0 , Byval iWidthPen As Single=1)
	
	If iWidth = 0 Orelse iHeight = 0 Then Exit Sub
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	Dim As Double iRadius , iScale
	
	Dim As Long iHalfWidthPen = iWidthPen\2  , dX , dY  , iMaxFill
	
	If iWidth >= iHeight Then
		
		iRadius = iWidth / 2
		
		iScale = iHeight/iWidth
		
		cairo_scale (cr, 1 , iScale)
		
		dX = iX + iRadius
		
		dY = (iY/iHeight)*iWidth + iRadius
		
		iMaxFill = iWidth + iX
		
	Else
		
		iRadius = iHeight / 2
		
		iScale = iWidth/iHeight
		
		cairo_scale (cr, iScale , 1)
		
		dX = (iX/iWidth)*iHeight + iRadius
		
		dY = iY + iRadius
		
		iMaxFill = iHeight + iY
		
	Endif
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_line_width (cr, iWidthPen)
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_arc (cr, dX  , dY , iRadius, 0 , 2*M_PI)
		
		cairo_stroke (cr)
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img
		
		cairo_set_line_width (cr, iWidthPen)		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iMaxFill
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iMaxFill
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_arc (cr, dX  , dY , iRadius, 0 , 2*M_PI)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif
	
	If iFlagcolorBK = 1 Then
		
		aBG = Cast(Ushort,((iColorBk And &hFF000000) Shr 16) Or (iColorBk And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColorBk And &h00FF0000) Shr 8) Or (iColorBk And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColorBk And &h0000FF00) Or (iColorBk And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColorBk And &h000000FF)Shl 8) Or (iColorBk And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		cairo_arc (cr, dX , dY, iRadius, 0, 2 * M_PI)
		
		cairo_fill (cr)	
		
	Elseif iFlagcolorBK = 2 Andalso brushBk Then
		
		Dim As BRUSHA Ptr pBrush = brushBk
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iMaxFill
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iMaxFill
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX + iHalfWidthPen , iStartY + iHalfWidthPen)
				
				cairo_arc (cr, dX , dY, iRadius, 0, 2 * M_PI)
				
				cairo_fill (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)
		
	Endif
	
	cairo_identity_matrix(cr)
	
End Sub

Sub Linedrawa(Byval iX As Single , Byval iY As Single , Byval iX1 As Single , Byval iY1 As Single ,_
Byval iWidthPen As Single=1 , Byval iColorBk As Integer=&hff000000 , Byval brushPen As Any Ptr=0)
	
	If iX = iX1 Andalso iY  = iY1 Then Exit Sub
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	Dim As Long iHalfWidthPen = iWidthPen\2
	
	cairo_set_line_width (cr, iWidthPen)
	
	If brushPen = 0 Then
		
		aBG = Cast(Ushort,((iColorBk And &hFF000000) Shr 16) Or (iColorBk And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColorBk And &h00FF0000) Shr 8) Or (iColorBk And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColorBk And &h0000FF00) Or (iColorBk And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColorBk And &h000000FF)Shl 8) Or (iColorBk And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		cairo_move_to (cr, iX, iY)
		
		cairo_line_to (cr, iX1, iY1)
		
		cairo_stroke (cr)
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img
		
		cairo_set_line_width (cr, iWidthPen)		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY
		
		Dim As Long iStartX , iStartY
		
		Dim As Long iXL , iXL1 , iYL , iYL1
		
		If iX > iX1 Then
			
			iXL = iX1
			
			iXL1 = iX
			
		Else
			
			iXL = iX
			
			iXL1 = iX1	
			
		Endif
		
		If iY > iY1 Then
			
			iYL = iY1
			
			iYL1 = iY
			
		Else
			
			iYL = iY
			
			iYL1 = iY1		
			
		Endif
		
		cairo_push_group(cr)
		
		While iStartX < iXL1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iYL1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_move_to (cr, iX, iY)
				
				cairo_line_to (cr, iX1, iY1)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif
	
End Sub

Sub Imagedrawa(Byval GpImage As Any Ptr,Byval iX As Single, Byval iY As Single,Byval iWidth0 As Single=0, Byval iHeight0 As Single=0)
	
	Dim As Long iHeight , iWidth , iFlagScale
	
	Dim As Any Ptr img
	
	If iWidth0 Then
		
		iWidth = iWidth0
		
		iFlagScale = 1
		
	Else
		
		iWidth = gdk_pixbuf_get_width(GpImage)
		
	Endif
	
	If iHeight0 Then
		
		iHeight = iHeight0
		
		iFlagScale = 1
		
	Else
		
		iHeight = gdk_pixbuf_get_height(GpImage)
		
	Endif
	
	If iFlagScale Then
		
		img = gdk_pixbuf_scale_simple(GpImage , iWidth , iHeight , GDK_INTERP_BILINEAR)
		
	Else
		
		img = gdk_pixbuf_copy(GpImage)
		
	Endif
	
	Dim As Ubyte Ptr pPixels = gdk_pixbuf_get_pixels(img)
	
	Dim As Integer iStride = gdk_pixbuf_get_rowstride(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
			
			row[x] = (row[x] And &hFF00FF00) Or ((row[x] And &h00FF0000) Shr 16) Or ((row[x] And &h000000FF) Shl 16)
			
		Next
		
		pPixels += iStride
		
	Next	
	
	gdk_cairo_set_source_pixbuf(cr, Img, iX, iY)
	
	cairo_paint(cr)
	
	cairo_fill (cr)
	
	g_object_unref(img)
	
End Sub

Sub Modedrawa(Byval mode As Integer)
	
	cairo_set_antialias(cr,mode)
	
End Sub

Type Int___Pt2D
	
	As Integer x, y
	
End Type

Sub Fillrectdrawa(Byval iX As Integer, Byval iY As Integer, Byval iColor As Integer)
	
	cairo_surface_flush(surf)
	
	iColor = (iColor And &hFF00FF00) Or ((iColor And &h00FF0000) Shr 16) Or ((iColor And &h000000FF) Shl 16)
	
	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 			
			
			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 Bezierdrawa(Byval x0 As Single,Byval y0 As Single,Byval x1 As Single,Byval y1 As Single,_
Byval x2 As Single,Byval y2 As Single,Byval x3 As Single,Byval y3 As Single,_
Byval iColorPen As Integer=&hff000000,Byval brushPen As Any Ptr=0,Byval iWidthPen As Single=1)
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	cairo_set_line_width (cr, iWidthPen)
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_move_to(cr ,x0,y0)
		
		cairo_curve_to (cr, x1,y1,x2,y2,x3,y3)
		
		cairo_stroke (cr)
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img	
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		Dim As Long iX1  , iY1 , iHalfWidthPen = iWidthPen\2
		
		iX1 = Iif (x0 > x1 , x0 , x1)
		
		iX1 = Iif (x2 > iX1 , x2 , iX1)
		
		iX1 = Iif (x3 > iX1 , x3 , iX1)
		
		iY1 = Iif (y0 > y1 , y0 , y1)
		
		iY1 = Iif (y2 > iY1 , y2 , iY1)
		
		iY1 = Iif (y3 > iY1 , y3 , iY1)		
		
		cairo_push_group(cr)
		
		While iStartX < iX1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_move_to(cr ,x0,y0)
				
				cairo_curve_to (cr, x1,y1,x2,y2,x3,y3)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif	
	
End Sub

Sub Arcdrawa(Byval iX As Single , Byval iY As Single , Byval iWidth As Single , Byval iHeight As Single,_
Byval startAngle As Single , Byval sweepAngle As Single , Byval iColorPen As Integer=&hff000000 , _
Byval brushPen As Any Ptr=0 , Byval iWidthPen As Single=1)
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	cairo_set_line_width (cr, iWidthPen)
	
	Dim As Double iRadius , 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 = startAngle*(M_PI/180)
	
	dRad2 = (startAngle+sweepAngle)*(M_PI/180)
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_arc (cr, dx , dY, iRadius, dRad1, dRad2)
		
		cairo_stroke (cr)	
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iX1 = iWidth + iX - 1  , iY1 = iHeight + iY - 1 , iHalfWidthPen = iWidthPen\2
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_arc (cr, dx , dY, iRadius, dRad1, dRad2)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)
		
	Endif
	
End Sub

Sub Piedrawa(Byval iX As Single , Byval iY As Single , Byval iWidth As Single , Byval iHeight As Single ,_
Byval startAngle As Single , Byval sweepAngle As Single , Byval iColorPen As Integer=&hff000000 , _
Byval iFlagcolorBK As Integer=1 , Byval iColorBk As Integer=&hff000000 , Byval brushPen As Any Ptr=0 , _
Byval brushBk As Any Ptr=0 , Byval iWidthPen As Single=1)
	
	If iWidth = 0 Orelse iHeight = 0 Then Exit Sub
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	Dim As Long iX1 = iWidth + iX - 1  , iY1 = iHeight + iY - 1 , iHalfWidthPen = iWidthPen\2
	
	Dim As Double dRad1 , dRad2
	
	Dim As Double iRadius , 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	
	
	dRad1 = startAngle*(M_PI/180)
	
	dRad2 = (startAngle+sweepAngle)*(M_PI/180)
	
	cairo_set_line_width (cr, iWidthPen)
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_arc (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_arc (cr, dx , dY, iRadius, dRad1, dRad2)
		
		cairo_arc (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_stroke (cr)
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_arc (cr, dX, dY, 0.1, 0, 2*M_PI)
				
				cairo_arc (cr, dx , dY, iRadius, dRad1, dRad2)
				
				cairo_arc (cr, dX, dY, 0.1, 0, 2*M_PI)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif
	
	If iFlagcolorBK = 1 Then
		
		aBG = Cast(Ushort,((iColorBk And &hFF000000) Shr 16) Or (iColorBk And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColorBk And &h00FF0000) Shr 8) Or (iColorBk And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColorBk And &h0000FF00) Or (iColorBk And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColorBk And &h000000FF)Shl 8) Or (iColorBk And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		cairo_arc (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_arc (cr, dx , dY, iRadius, dRad1, dRad2)
		
		cairo_arc (cr, dX, dY, 0.1, 0, 2*M_PI)
		
		cairo_fill (cr)	
		
	Elseif iFlagcolorBK = 2 Andalso brushBk Then
		
		Dim As BRUSHA Ptr pBrush = brushBk
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 - iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 - iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX + iHalfWidthPen , iStartY + iHalfWidthPen)
				
				cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
				
				cairo_arc (cr, dx , dY, iRadius, dRad1, dRad2)
				
				cairo_arc_negative (cr, dX, dY, 0.1, 0, 2*M_PI)
				
				cairo_fill (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)
		
	Endif	
	
End Sub

Sub Polygondrawa(Byval Points As Any Ptr , Byval iCountPoints As Integer , Byval iColorPen As Integer=&hff000000 , _
Byval iFlagcolorBK As Integer=1 , Byval iColorBk As Integer=&hff000000 , Byval brushPen As Any Ptr=0 , _
Byval brushBk As Any Ptr=0 , Byval iWidthPen As Single=1 , Byval iFillmode As Integer=0)
	
	If Points = 0 Then Exit Sub
	
	Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
	
	Dim As Long iX1 , iY1 , iHalfWidthPen = iWidthPen\2
	
	cairo_set_line_width (cr, iWidthPen)
	
	Dim As GdkPoint Ptr pPoint = Points , pTemp = Points
	
	For i As Long = 0 To iCountPoints -1
		
		If pTemp->x > iX1 Then
			
			iX1 = pTemp->x
			
		Endif
		
		If pTemp->y > iY1 Then
			
			iY1 = pTemp->y
			
		Endif
		
		pTemp+=1
		
	Next
	
	iX1 += iWidthPen*2
	
	iY1 += iWidthPen*2
	
	If brushPen = 0 Then
		
		aPen = Cast(Ushort,((iColorPen And &hFF000000) Shr 16) Or (iColorPen And &hFF000000) Shr 24)/&hFFFF
		
		rPen = 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
		
		bPen = Cast(Ushort,((iColorPen And &h000000FF)Shl 8) Or (iColorPen And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bPen, gPen, rPen , aPen)
		
		cairo_move_to (cr, pPoint->x, pPoint->y)
		
		For i As Long = 1 To iCountPoints - 1
			
			cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)		
			
		Next 
		
		cairo_close_path(cr)
		
		cairo_stroke (cr)	
		
	Else
		
		Dim As BRUSHA Ptr pBrush = brushPen
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 + iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 + iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX - iHalfWidthPen , iStartY - iHalfWidthPen)
				
				cairo_move_to (cr, pPoint->x, pPoint->y)
				
				For i As Long = 1 To iCountPoints - 1
					
					cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)		
					
				Next 
				
				cairo_close_path(cr)
				
				cairo_stroke (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)		
		
	Endif
	
	If iFlagcolorBK = 1 Then
		
		aBG = Cast(Ushort,((iColorBk And &hFF000000) Shr 16) Or (iColorBk And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColorBk And &h00FF0000) Shr 8) Or (iColorBk And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColorBk And &h0000FF00) Or (iColorBk And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColorBk And &h000000FF)Shl 8) Or (iColorBk And &h000000FF))/&hFFFF
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		cairo_move_to (cr, pPoint->x, pPoint->y)
		
		For i As Long = 1 To iCountPoints - 1
			
			cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)		
			
		Next 
		
		cairo_close_path(cr)
		
		cairo_fill (cr)	
		
	Elseif iFlagcolorBK = 2 Andalso brushBk Then
		
		Dim As BRUSHA Ptr pBrush = brushBk
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1 - iHalfWidthPen
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1 - iHalfWidthPen
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX + iHalfWidthPen , iStartY + iHalfWidthPen)
				
				cairo_move_to (cr, pPoint->x, pPoint->y)
				
				For i As Long = 1 To iCountPoints - 1
					
					cairo_line_to (cr, (pPoint+ i)->x, (pPoint+ i)->y)
					
				Next 
				
				cairo_close_path(cr)
				
				cairo_fill (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_paint (cr)
		
	Endif	
	
End Sub

Function Createfontdrawa(Byval sName As String="Sans" , Byval iSize As Integer=10 , Byval iStyle As Integer=0 , Byval iUnit As Integer=6) As Any Ptr
	
	Dim As String sFont = Trim(sName) & " "
	
	If iStyle And 1 Then sFont &= "Bold "
	
	If iStyle And 2 Then sFont &= "Italic "
	
	Select Case iUnit
			
		Case 1 , 2
			
			Dim As Long iPoint = (iSize / 96)*72
			
			sFont &= iPoint
			
		Case 3
			
			sFont &= iSize
			
		Case 4
			
			Dim As Long iPoint = iSize*72
			
			sFont &= iPoint			
			
		Case 5
			
			Dim As Long iPoint = (iSize*72)/300
			
			sFont &= iPoint			
			
		Case 6
			
			Dim As Long iPoint = (iSize / 25.4) * 72 
			
			sFont &= iPoint
			
	End Select
	
	Return pango_font_description_from_string(sFont)
	
End Function

Sub Freefontdrawa(Byval GpFont As Any Ptr)
	
	If GpFont Then
		
		pango_font_description_free (GpFont)
		
	Endif
	
End Sub

Sub Textdrawa(Byref sText As String , Byval iX As Integer , Byval iY As Integer , Byval GpFont As Any Ptr=0 ,_
Byval iColor As Integer=&hFFFFFFFF , Byval brush As Any Ptr=0 , Byval iMode As Integer=0)
	
	Dim As PangoLayout Ptr layout
	
	Dim As PangoFontDescription Ptr desc_temp
	
	layout = pango_cairo_create_layout (cr)
	
	If GpFont = 0 Then
		
		Dim As Long iPoint = (12 / 25.4) * 72
		
		desc_temp = pango_font_description_from_string("Sans " & iPoint)
		
		If desc_temp Then
			
			pango_layout_set_font_description (layout, desc_temp)
			
			pango_font_description_free (desc_temp)
			
		Endif
		
	Else
		
		desc_temp = pango_font_description_copy(GpFont)
		
		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)
	
	If brush = 0 Then
		
		Dim As Double rPen , gPen , bPen , aPen , rBG , gBG , bBG , aBG
		
		aBG = Cast(Ushort,((iColor And &hFF000000) Shr 16) Or (iColor And &hFF000000) Shr 24)/&hFFFF
		
		rBG = Cast(Ushort,((iColor And &h00FF0000) Shr 8) Or (iColor And &h00FF0000) Shr 16)/&hFFFF
		
		gBG = Cast(Ushort,(iColor And &h0000FF00) Or (iColor And &h0000FF00) Shr 8)/&hFFFF
		
		bBG = Cast(Ushort,((iColor And &h000000FF)Shl 8) Or (iColor And &h000000FF))/&hFFFF
		
		cairo_move_to(cr, iX, iY )
		
		cairo_set_source_rgba (cr, bBG, gBG, rBG , aBG)
		
		pango_cairo_update_layout (cr, layout)
		
		pango_cairo_show_layout (cr, layout)
		
		cairo_fill (cr)
		
	Else
		
		Dim As Long iX1 = pr_log.Width + iX - 1  , iY1 = pr_log.Height + iY - 1
		
		Dim As BRUSHA Ptr pBrush = brush
		
		Dim As Any Ptr img		
		
		Dim As Long iWbr = Abs(pBrush->x1 - pBrush->x0)+1 , iHbr = Abs(pBrush->y1 - pBrush->y0)+1 , iFlagX , iFlagY	
		
		Dim As Long iStartX , iStartY
		
		cairo_push_group(cr)
		
		While iStartX < iX1
			
			Dim As Long iTempY = iStartY
			
			While iStartY < iY1
				
				img = getImageFromBrushPoiter(pBrush , iFlagX , iFlagY)
				
				gdk_cairo_set_source_pixbuf(cr, Img, iStartX , iStartY)
				
				cairo_rectangle (cr, iX , iY , pr_log.Width , pr_log.Height)
				
				cairo_fill (cr)
				
				iFlagY Xor=1
				
				iStartY+=iHbr
				
			Wend
			
			iFlagY = 0
			
			iFlagX Xor=1
			
			iStartX+=iWbr
			
			iStartY = iTempY
			
		Wend
		
		cairo_pop_group_to_source(cr)
		
		cairo_move_to(cr, iX, iY )
		
		pango_cairo_update_layout (cr, layout)
		
		pango_cairo_show_layout (cr, layout)
		
		cairo_fill (cr)
		
	Endif
	
	g_object_unref (layout)
	
End Sub