#include Once "window9.bi"

Dim Shared As String Ptr pStr

Redim Shared As Long mIndex(10)

Dim Shared As Long iCounter_mIndex

Dim Shared As Long iColorText , iColorBg

Private Sub begin_print Cdecl(operation As GtkPrintOperation Ptr,context As GtkPrintContext Ptr, user_data As gpointer)
	
End Sub

Private Function paginate Cdecl(operation As GtkPrintOperation Ptr,context As GtkPrintContext Ptr, user_data As gpointer) As gboolean
	
	Dim As cairo_t Ptr cr
	
	Dim As PangoLayout Ptr layout
	
	Dim As PangoFontDescription Ptr desc
	
	Dim As Double dWidth, dHeight
	
	Dim As window9.FONTPRINT Ptr ft = user_data
	
	dWidth = gtk_print_context_get_width(context)
	
	dHeight = gtk_print_context_get_height(context)
	
	cr = gtk_print_context_get_cairo_context(context)
	
	cairo_move_to(cr , 0 , 0)
	
	layout = gtk_print_context_create_pango_layout (context)
	
	pango_layout_set_width(layout , dWidth * PANGO_SCALE)
	
	pango_layout_set_height(layout , dHeight * PANGO_SCALE)
	
	Dim As String sText
	
	If iCounter_mIndex = 0 Then
		
		sText = *pStr
		
	Else
	
		Dim As Long iPos
		
		For i As Long = 0 To iCounter_mIndex
			
			iPos += mIndex(i)
			
		Next
		
		sText = Mid(*pStr , iPos+1)
		
	Endif
	
	If Len(sText) = 0 Then
		
		gtk_print_operation_set_n_pages (operation, iCounter_mIndex)
		
		Return True		
		
	Endif
	
	pango_layout_set_text (layout, Strptr(sText) , -1)

	Dim As String sFont = ft->Name_ & " "
	
	If ft->BOLD Then sFont &= "Bold "
	
	If ft->Italic Then sFont &= "Italic "
	
	sFont &= ft->size
	
	desc = pango_font_description_from_string (Strptr(sFont))
	
	pango_layout_set_font_description (layout, desc)
	
	pango_layout_set_wrap(layout , PANGO_WRAP_WORD_CHAR)
	
	pango_font_description_free (desc)
	
	pango_cairo_show_layout (cr, layout)

	Dim As Gint iFind , iLineIndex

	Do
		
		Dim As PangoLayoutLine Ptr pline = pango_layout_get_line_readonly(layout , iLineIndex)
		
		If pline Then
			
			iLineIndex += 1
			
			If pLine->runs Andalso Pline->runs->Data Then
				
				iFind = Pline->start_index
				
				If iFind + Pline->length = Len(sText) Then
					
					gtk_print_operation_set_n_pages (operation, iCounter_mIndex+1)
					
					Return True	
					
				Endif
				
			Endif
			
		Else
			
			Exit Do
			
		Endif
		
	Loop

	g_object_unref (layout)
	
	If iFind Then
		
		Dim As Long j = Instr(iFind+1 , sText , Chr(10))
		
		If j Then iFind = j+1
		
		iCounter_mIndex +=1
		
		If iCounter_mIndex > Ubound(mIndex) Then
			
			Redim Preserve As Long mIndex(iCounter_mIndex+10)
			
		Endif
		
		mIndex(iCounter_mIndex) = iFind
		
	Else
	
		gtk_print_operation_set_n_pages (operation, iCounter_mIndex+1)
		
		Return True
		
	Endif
	
End Function

Private Sub end_print Cdecl(operation As GtkPrintOperation Ptr,context As GtkPrintContext Ptr, user_data As gpointer)
	
End Sub

Private Sub draw_page Cdecl(operation As GtkPrintOperation Ptr,context As GtkPrintContext Ptr, page_nr As gint ,user_data As gpointer )
	
	Dim As cairo_t Ptr cr
	
	Dim As PangoLayout Ptr layout
	
	Dim As PangoFontDescription Ptr desc
	
	Dim As Double dWidth, dHeight
	
	Dim As window9.FONTPRINT Ptr ft = user_data
	
	dWidth = gtk_print_context_get_width(context)
	
	dHeight = gtk_print_context_get_height(context)
	
	cr = gtk_print_context_get_cairo_context(context)
	
	cairo_move_to(cr , 0 , 0)
	
	layout = gtk_print_context_create_pango_layout (context)
	
	pango_layout_set_width(layout , dWidth* PANGO_SCALE)
	
	pango_layout_set_height(layout , dHeight* PANGO_SCALE)
	
	pango_layout_set_wrap(layout , PANGO_WRAP_WORD_CHAR)	
	
	Dim As String sText
	
	If page_nr = 0 Then
		
		sText = *pStr
		
	Else
	
		Dim As Long iPos
		
		For i As Long = 0 To page_nr
			
			iPos += mIndex(i)
			
		Next
		
		sText = Mid(*pStr , iPos)
		
	Endif

	pango_layout_set_text (layout, Strptr(sText) , -1)
	
	Dim As String sFont = ft->Name_ & " "
	
	If ft->BOLD Then sFont &= "Bold "
	
	If ft->Italic Then sFont &= "Italic "
	
	sFont &= ft->size
	
	desc = pango_font_description_from_string (Strptr(sFont))
	
	pango_layout_set_font_description (layout, desc)
	
	pango_font_description_free (desc)

	Dim As PangoRectangle pr_ink , pr_log

	pango_layout_get_pixel_extents (layout, @pr_ink, @pr_log)

	If iColorBg Then
		
		Dim As Double rPen , gPen , bPen
		
		rPen = Cast(Ushort,((iColorBg And &h000000FF)Shl 8) Or (iColorBg And &h000000FF))/&hFFFF
		
		bPen = Cast(Ushort,((iColorBg And &h00FF0000) Shr 8) Or (iColorBg And &h00FF0000) Shr 16)/&hFFFF
		
		gPen = Cast(Ushort,(iColorBg And &h0000FF00) Or (iColorBg And &h0000FF00) Shr 8)/&hFFFF		
		
		cairo_set_source_rgb(cr, rPen, gPen, bPen)
		
		cairo_rectangle (cr, 0 , 0 , pr_log.Width , pr_log.Height)
		
		cairo_fill (cr)
		
	Endif

	If iColorText Then
		
		Dim As Double rPen , gPen , bPen
		
		rPen = Cast(Ushort,((iColorText And &h000000FF)Shl 8) Or (iColorText And &h000000FF))/&hFFFF
		
		bPen = Cast(Ushort,((iColorText And &h00FF0000) Shr 8) Or (iColorText And &h00FF0000) Shr 16)/&hFFFF
		
		gPen = Cast(Ushort,(iColorText And &h0000FF00) Or (iColorText And &h0000FF00) Shr 8)/&hFFFF
		
		cairo_set_source_rgb(cr, rPen, gPen, bPen)	
		
	Endif

	pango_cairo_show_layout (cr, layout)
	
	g_object_unref (layout)
	
End Sub

Sub Textprinter(Byref sSourceText As String, Byval Font As window9.FontPrint Ptr=0, Byval color_BK As COLORREF=0, Byval color_T As COLORREF=0)	
	
	If Len(sSourceText) = 0 Then
		 
		 pStr = 0
		 
		 Exit Sub
		 
	Endif
	
	Dim As String sPrintText = Rtrim(sSourceText , Any Chr(10) & Chr(13))
	
	pStr = @sPrintText
	
	iColorText = color_T
	
	iColorBg = color_BK
	
	Dim As GtkPrintOperation Ptr pPrint
	
	Dim As GtkPrintOperationResult res
	
	Static As GtkPrintSettings Ptr settings
	
	pPrint = gtk_print_operation_new()
	
	If settings Then
		
		gtk_print_operation_set_print_settings (pPrint, settings)
		
	Endif
	
	Redim As Long mIndex(10)

	iCounter_mIndex = 0
	
	g_signal_connect (pPrint, "begin_print", G_CALLBACK (@begin_print), 0)
	
	g_signal_connect (pPrint, "paginate", G_CALLBACK (@paginate), font)
	
	g_signal_connect (pPrint, "draw_page", G_CALLBACK (@draw_page), font)
	
	g_signal_connect (pPrint, "end-print", G_CALLBACK (@end_print), 0)
	
	res = gtk_print_operation_run (pPrint, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG , 0 , 0)
	
	If res = GTK_PRINT_OPERATION_RESULT_APPLY Then
		
		If settings Then g_object_unref (settings)
		
		settings = g_object_ref(gtk_print_operation_get_print_settings(pPrint))
		
	Else
		
		? "Error Print"
		
	Endif
	
	g_object_unref (pPrint)
	
End Sub