#include Once "window9.bi"

Dim Shared As GtkPrintSettings Ptr settings

Dim Shared As GtkPageSetup Ptr pPageSetup

Dim Shared As Long iNumPages , iWidthContext , iHeightContext , iNumCopies

Dim Shared As Long iLeftMargin, iRightMargin , iTopMargin , iBottomMargin

Dim Shared As Long iOrientation , iPaperSizeW = 210 , iPaperSizeH = 297 , iPaperSizeCombo

Dim Shared As Long iFlagFontColor = 1 , iColorTextFont , iColorBgFont , iFlagPreview

Dim Shared As PangoFontDescription Ptr desc

Dim Shared As HDC hdc

Redim Shared As HBITMAP hBmps(0)

Namespace window9
	
	Private Sub DialogPrint(Byref iLeft As Integer , Byref iRight As Integer, Byref iTop As Integer, Byref iBottom As Integer)
		
		Var hw = Openwindow("Options Print" , 10,10,415,320,WS_OVERLAPPEDWINDOW Or WS_TABSTOPENABLE)
		
		Centerwindow(hw)
		
		Hidewindow(hw , 0)
		
		Setwindowtop(hw , 1)
		
		Textgadget(1,40,10,100,30,"ORIENTATION:")
		
		Comboboxgadget(2,10,40,170,30)
		
		Addcomboboxitem(2,"portrait",-1)
		
		Addcomboboxitem(2,"landscape",-1)
		
		Addcomboboxitem(2,"portrait reverse",-1)
		
		Addcomboboxitem(2,"landscape reverse",-1)
		
		Setitemcombobox(2 , iOrientation)
		
		Textgadget(3,270,10,100,30,"SIZE PAPER:")
		
		Comboboxgadget(4,230,40,170,30)
		
		Addcomboboxitem(4,"A4 (210 x 297)",-1)
		
		Addcomboboxitem(4,"A5 (148 × 210)",-1)
		
		Addcomboboxitem(4,"Custom...",-1)
		
		Setitemcombobox(4 , iPaperSizeCombo)
		
		Textgadget(5,240,70,100,30,"width:")
		
		Textgadget(6,340,70,100,30,"height:")
		
		Spingadget(7,230,100,70,30,1000,1,iPaperSizeW)
		
		Spingadget(8,330,100,70,30,1000,1,iPaperSizeH)
		
		If iPaperSizeCombo < 2 Then
			
			Disablegadget(7 , 1)
			
			Disablegadget(8 , 1)
			
		Endif
		
		Textgadget(9,40,100,100,30,"MARGINS:")
		
		Textgadget(10,10,130,60,30,"left:")
		
		Textgadget(11,10,170,60,30,"right:")
		
		Textgadget(12,10,210,60,30,"top:")
		
		Textgadget(13,10,250,60,30,"bottom:")
		
		Spingadget(14,70,130,70,30,1000,0,iLeft)
		
		Spingadget(15,70,170,70,30,1000,0,iRight)
		
		Spingadget(16,70,210,70,30,1000,0,iTop)
		
		Spingadget(17,70,250,70,30,1000,0,iBottom)
		
		Buttongadget(18,300,210,100,30,"Ok")
		
		Buttongadget(19,300,250,100,30,"Cancel")
		
		Dim As Long Event
		
		Do
			
			Event = Waitevent()
			
			Select Case Event
					
				Case Eventgadget
					
					Select Case Eventnumber
							
						Case 4
							
							If Getitemcombobox(4) < 2 Then
								
								Disablegadget(7 , 1)
								
								Disablegadget(8 , 1)
								
							Else
								
								Disablegadget(7 , 0)
								
								Disablegadget(8 , 0)							
								
							Endif
							
						Case 19
							
							Exit Do
							
						Case 18
							
							iOrientation = Getitemcombobox(2)
							
							iPaperSizeCombo = Getitemcombobox(4)
							
							Select Case iPaperSizeCombo
									
								Case 0
									
									iPaperSizeW = 210
									
									iPaperSizeH = 297								
									
								Case 1
									
									iPaperSizeW = 148
									
									iPaperSizeH = 210								
									
								Case 2
									
									iPaperSizeW = Getgadgetstate(7)
									
									iPaperSizeH = Getgadgetstate(8)
									
							End Select
							
							iLeftMargin = Getgadgetstate(14)
							
							iRightMargin = Getgadgetstate(15)
							
							iTopMargin = Getgadgetstate(16)
							
							iBottomMargin = Getgadgetstate(17)
							
							iLeft = iLeftMargin
							
							iRight = iRightMargin
							
							iTop = iTopMargin
							
							iBottom = iBottomMargin
							
							Exit Do
							
					End Select
					
				Case Eventclose
					
					Exit Do
					
			End Select
			
		Loop
		
	End Sub
	
	Private Sub begin_print Cdecl(operation As GtkPrintOperation Ptr,context As GtkPrintContext Ptr, user_data As gpointer)
		
		If *Cast(Long Ptr , user_data) Then
			
			If pPageSetup = 0 Then
				
				pPageSetup = gtk_page_setup_new()
				
				gtk_page_setup_set_left_margin(pPageSetup , iLeftMargin , GTK_UNIT_POINTS)
				
				gtk_page_setup_set_right_margin(pPageSetup , iRightMargin , GTK_UNIT_POINTS)
				
				gtk_page_setup_set_top_margin(pPageSetup , iTopMargin , GTK_UNIT_POINTS)
				
				gtk_page_setup_set_bottom_margin(pPageSetup , iBottomMargin , GTK_UNIT_POINTS)
				
				gtk_page_setup_set_orientation(pPageSetup , iOrientation )
				
				Dim As GtkPaperSize Ptr pps = gtk_paper_size_new_custom("custom", "custom", iPaperSizeW , iPaperSizeH , GTK_UNIT_MM)
				
				gtk_page_setup_set_paper_size(pPageSetup , pps)
				
			Endif
			
			gtk_print_operation_set_default_page_setup(operation, pPageSetup)	
			
			gtk_print_operation_set_n_pages (operation, *Cast(Long Ptr , user_data))
			
		Else
			
			iWidthContext = gtk_print_context_get_width(context)
			
			iHeightContext = gtk_print_context_get_height(context)
			
			Dim As GtkPrintSettings Ptr p =  gtk_print_operation_get_print_settings(operation)	
			
			If p Then settings = gtk_print_settings_copy(p)
			
			iNumCopies = gtk_print_settings_get_n_copies(p)
			
			gtk_print_operation_cancel (operation)
			
		Endif
		
	End Sub
	
	Private Sub end_print Cdecl(operation As GtkPrintOperation Ptr,context As GtkPrintContext Ptr, user_data As gpointer)
		
	End Sub
	
	Private Function preview Cdecl(operation As GtkPrintOperation Ptr,_
	preview_ As GtkPrintOperationPreview Ptr,_
	context As GtkPrintContext Ptr,_
	parent As GtkWindow Ptr,_
	user_data As gpointer) As gboolean
		
		iFlagPreview = 1
		
		Return False
		
	End Function
	
	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
		
		cr = gtk_print_context_get_cairo_context(context)
		
		gdk_cairo_set_source_pixbuf(cr,hBmps(page_nr),0,0)
		
		cairo_paint(cr)
		
	End Sub
	
	Function Startprinter(Byref Scale As SinglePoint Ptr=0, Byval Flagstart As Integer=1) As HDC
		
		Dim As GtkPrintOperation Ptr pPrint
		
		Dim As GtkPrintOperationResult res
		
		pPrint = gtk_print_operation_new()
		
		If settings Then
			
			gtk_print_operation_set_print_settings (pPrint, settings)
			
		Endif
		
		g_signal_connect (pPrint, "begin_print", G_CALLBACK (@begin_print), @iNumPages)
		
		g_signal_connect (pPrint, "draw_page", G_CALLBACK (@draw_page), 0)
		
		g_signal_connect (pPrint, "end-print", G_CALLBACK (@end_print), 0)
		
		g_signal_connect (pPrint, "preview", G_CALLBACK (@preview), 0)
		
		res = gtk_print_operation_run (pPrint, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG , 0 , 0)
		
		If res = GTK_PRINT_OPERATION_RESULT_APPLY Then
			
			g_object_unref (pPrint)
			
			Return Cast(HDC , 1)
			
		Else
			
			? "Error Print"
			
		Endif
		
	End Function
	
	Sub Stopprinter()
		
		Dim As GtkPrintOperation Ptr pPrint
		
		Dim As GtkPrintOperationResult res
		
		pPrint = gtk_print_operation_new()
		
		g_signal_connect (pPrint, "begin_print", G_CALLBACK (@begin_print), @iNumPages)
		
		g_signal_connect (pPrint, "draw_page", G_CALLBACK (@draw_page), 0)
		
		g_signal_connect (pPrint, "end-print", G_CALLBACK (@end_print), 0)		
		
		If settings Then gtk_print_operation_set_print_settings (pPrint, settings)
		
		gtk_print_operation_set_job_name(pPrint, Time)
		
		If iFlagPreview Then
			
			res = gtk_print_operation_run (pPrint, GTK_PRINT_OPERATION_ACTION_PREVIEW , 0 , 0)
			
		Else
			
			res = gtk_print_operation_run (pPrint, GTK_PRINT_OPERATION_ACTION_PRINT , 0 , 0)
			
		Endif
		
		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
		
		If pPageSetup Then 
			
			g_object_unref(pPageSetup)
			
			pPageSetup = 0
			
		Endif
		
		g_object_unref (pPrint)
		
		If Ubound(hBmps) <> -1 Then
			
			For i As Long = 0 To Ubound(hBmps)
				
				If hBmps(i) Then Free_image(hBmps(i))
				
			Next
			
			Redim As HBITMAP hBmps(0)
			
			hBmps(0) = 0
			
		Endif
		
		If desc Then pango_font_description_free (desc)
		
		iNumPages = 0
		
		iNumCopies = 0
		
		iColorTextFont = 0
		
		iColorBgFont = 0
		
		iFlagFontColor = 1
		
		iFlagPreview = 0
		
	End Sub
	
	Sub Pagestart()
		
		Dim As HBITMAP bmp = Create_image(iWidthContext+1 , iHeightContext+1)
		
		If hBmps(0) = 0 Then
			
			hBmps(0) = bmp
			
		Else
			
			Redim Preserve As HBITMAP hBmps(Ubound(hBmps)+1)
			
			hBmps(Ubound(hBmps)) = bmp
			
		Endif
		
		iNumPages+=1
		
		hdc = Imagestartdraw(bmp)
		
		Boxdraw(0,0,iWidthContext+1 ,iHeightContext+1,&hffffff,&hffffff) 
		
	End Sub
	
	Sub Pageend()
		
		Stopdraw()
		
	End Sub
	
	Sub Documentstart()
		
	End Sub
	
	Sub Documentend()
		
	End Sub
	
	Function Getcountdoc() As Integer
		
		Return iNumCopies
		
	End Function
	
	Sub Getfullsize(Byref X As Integer, Byref Y As Integer)
		
		X = iWidthContext
		
		Y = iHeightContext
		
	End Sub
	
	Sub Getrealsize(Byref X As Integer, Byref Y As Integer)
		
		X = iWidthContext - (iLeftMargin + iRightMargin)
		
		Y = iHeightContext - (iTopMargin + iBottomMargin)
		
	End Sub
	
	Sub Printimage(Byval bitmap As HBITMAP,Byval x As Integer, Byval y As Integer)
		
		Imagedraw(bitmap , x , y)
		
	End Sub
	
	Sub Framepage(Byref iLeft As Integer, Byref iRight As Integer, Byref iTop As Integer, Byref iBottom As Integer)
		
		DialogPrint(iLeft , iRight , iTop , iBottom)
		
	End Sub
	
	Sub Fontprinter(Byval Font As window9.FontPrint Ptr=0)
		
		If desc Then pango_font_description_free (desc)
		
		If Font Then
			
			Dim As String sFont = Font->Name_ & " "
			
			If Font->BOLD Then sFont &= "Bold "
			
			If Font->Italic Then sFont &= "Italic "
			
			sFont &= Font->size
			
			desc = pango_font_description_from_string (Strptr(sFont))
			
		Else
			
			desc = 0
			
		Endif
		
	End Sub
	
	Sub Colorprinter(Byval iColorText As Integer=0, Byval iColorBG As Integer=-1, Byval iFlag As Integer=1)
		
		iColorTextFont = iColorText
		
		iColorBgFont = iColorBg
		
		iFlagFontColor = iFlag
		
	End Sub
	
	Sub Getlenstring(Byref s As String, Byref X As Integer, Byref Y As Integer)
		
		Dim As PangoLayout Ptr layout
		
		Dim As PangoFontDescription Ptr desc_temp
		
		layout = pango_cairo_create_layout (hdc)
		
		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(s) , -1)
		
		Dim As PangoRectangle pr_ink , pr_log
		
		pango_layout_get_pixel_extents (layout, @pr_ink, @pr_log)
		
		X = pr_log.Width
		
		Y = pr_log.Height
		
		g_object_unref (layout)
		
	End Sub
	
	Sub Printtext(Byref sText0 As String, Byval iLeft As Integer=0, Byval iTop As Integer=0,_
	Byval iRight As Integer=0, Byval iBottom As Integer=0, Byval iFlag As Integer=0)
		
		Dim As PangoLayout Ptr layout
		
		Dim As PangoFontDescription Ptr desc_temp
		
		layout = pango_cairo_create_layout (hdc)
		
		Dim As String sText = sText0
		
		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
		
		Dim As Integer iW , iH
		
		If iRight = 0 Then iRight = iWidthContext - (iLeftMargin + iRightMargin)
		
		If iBottom = 0 Then iBottom = iHeightContext - (iTopMargin + iBottomMargin)
		
		iW = Abs(iRight - iLeft)
		
		iH = Abs(iBottom - iTop)
		
		If iW = 0 Orelse (iFlag And DT_NOCLIP) Then
			
			iW = iWidthContext - (iLeftMargin + iRightMargin)
			
		Endif
		
		If iH = 0 Orelse (iFlag And DT_NOCLIP) Then
			
			iH = iHeightContext - (iTopMargin + iBottomMargin)
			
		Endif
		
		If (iFlag And DT_NOCLIP) Then
			
			pango_layout_set_width(layout , -1)
			
		Elseif Not Cast(Boolean,(iFlag And DT_WORDBREAK)) Then
			
			Dim As extWstring ws = sText
			
			For i As Long = 1 To Len(ws)
				
				Dim As String s = Mid(ws , 1 , i)
				
				pango_layout_set_text (layout, Strptr(s) , -1)
				
				Dim As PangoRectangle pr_ink , pr_log
				
				pango_layout_get_pixel_extents (layout, @pr_ink, @pr_log)
				
				If pr_log.Width > iW Then
					
					sText = Left(ws , i-1)
					
					Exit For
					
				Endif
				
			Next
			
			pango_layout_set_width(layout , iW * PANGO_SCALE)
			
		Else
			
			If (iFlag And DT_WORDBREAK) Then
				
				pango_layout_set_width(layout , iW * PANGO_SCALE)
				
				pango_layout_set_wrap(layout , PANGO_WRAP_WORD_CHAR)
				
				Dim As extWstring ws = sText
				
				For i As Long = 1 To Len(ws)
					
					Dim As String s = Mid(ws , 1 , i)
					
					pango_layout_set_text (layout, Strptr(s) , -1)
					
					Dim As PangoRectangle pr_ink , pr_log
					
					pango_layout_get_pixel_extents (layout, @pr_ink, @pr_log)
					 
					If pr_log.Height > iH Then
						
						sText = Left(ws , i-1)
						
						Exit For
						
					Endif
					
				Next
				
			Endif
			
		Endif
		
		If iFlag And DT_RIGHT Then
			
			pango_layout_set_alignment (layout , PANGO_ALIGN_RIGHT)
			
		Elseif iFlag And DT_LEFT Then
			
			pango_layout_set_alignment (layout , PANGO_ALIGN_LEFT)
			
		Elseif iFlag And DT_CENTER Then
			
			pango_layout_set_alignment (layout , PANGO_ALIGN_CENTER)
			
		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(hdc, iLeft, iTop )
		
		If iFlagFontColor > 1 Then
			
			Dim As Double rPen , gPen , bPen
			
			rPen = Cast(Ushort,((iColorBgFont And &h000000FF)Shl 8) Or (iColorBgFont And &h000000FF))/&hFFFF
			
			bPen = Cast(Ushort,((iColorBgFont And &h00FF0000) Shr 8) Or (iColorBgFont And &h00FF0000) Shr 16)/&hFFFF
			
			gPen = Cast(Ushort,(iColorBgFont And &h0000FF00) Or (iColorBgFont And &h0000FF00) Shr 8)/&hFFFF		
			
			cairo_set_source_rgb(hdc, bPen, gPen, rPen)
			
			cairo_rectangle (hdc, iLeft, iTop , iW , pr_log.Height)
			
			cairo_fill (hdc)	
			'
		Endif
		
		cairo_move_to(hdc, iLeft, iTop)
		
		Dim As Double rPen , gPen , bPen
		
		rPen = Cast(Ushort,((iColorTextFont And &h000000FF)Shl 8) Or (iColorTextFont And &h000000FF))/&hFFFF
		
		bPen = Cast(Ushort,((iColorTextFont And &h00FF0000) Shr 8) Or (iColorTextFont And &h00FF0000) Shr 16)/&hFFFF
		
		gPen = Cast(Ushort,(iColorTextFont And &h0000FF00) Or (iColorTextFont And &h0000FF00) Shr 8)/&hFFFF
		
		cairo_set_source_rgb(hdc, bPen, gPen, rPen)
		
		pango_cairo_update_layout (hdc, layout)
		
		pango_cairo_show_layout (hdc, layout)
		
		cairo_fill (hdc)
		
		g_object_unref (layout)	
		
	End Sub
	
End Namespace