#include Once "window9.bi"

Type THWNDPRINTER
	
	bmp As HBITMAP
	
	Xpr As Integer = 0

	Ypr As Integer = 0
	
End Type

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

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 Double dWidth, dHeight
	
	Dim As Integer Xpr , Ypr , x1 , y1
	
	Dim As THWNDPRINTER Ptr pHW = Cast(THWNDPRINTER Ptr , user_data)
	
	dWidth = gtk_print_context_get_width(context)
	
	dHeight = gtk_print_context_get_height(context)
	
	cr = gtk_print_context_get_cairo_context(context)
	
	If pHW->Xpr = -1 Then
		
		Xpr = ((dWidth - Image_width(pHW->bmp))\2)
		
	Else
	
		Xpr = pHW->Xpr
		
	Endif
	
	If pHW->Ypr = -1 Then
		
		Ypr = ((dHeight - Image_height(pHW->bmp))\2)
		
	Else
	
		Ypr = pHW->Ypr
		
	Endif	
	
	gdk_cairo_set_source_pixbuf(cr,pHW->bmp,Xpr,Ypr)
	
	cairo_paint(cr)
	
End Sub

Function Hwndprinter(Byval hwnd As Any Ptr, Byval Xpr As Integer=0, Byval Ypr As Integer=0, Byval X As Integer=0,_
 Byval Y As Integer=0, Byval X1 As Integer=0, Byval Y1 As Integer=0) As Integer	
	
	If hwnd = 0 Then
		 
		 Return 0
		
	Endif
	
	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

	Dim As HBITMAP bmp
	
	Dim As Any Ptr pWin
	
	If hwnd = gdk_get_default_root_window() Then
		
		pWin = hwnd
		
	Else
		
		gtk_widget_grab_focus(hwnd)
		
		Updateinfoxserver()
		
		pWin = gtk_widget_get_window(hwnd)
		
	Endif
	
	If pWin = 0 Then
		
		pWin = hwnd
		
	Endif
	
	If X1 = 0 Then
		
		x1 = gdk_window_get_width(pWin)
		
	Endif
	
	If Y1 = 0 Then
		
		y1 = gdk_window_get_height(pWin)
		
	Endif	
	
	bmp = Createcopyimagerect(pWin , X , Y , x1 , y1 , 1)
	
	If bmp Then
		
		Dim p As THWNDPRINTER
		
		p.bmp = bmp
		
		p.Xpr = Xpr
		
		p.Ypr = Ypr
		
		g_signal_connect (pPrint, "begin_print", G_CALLBACK (@begin_print), 0)
		
		g_signal_connect (pPrint, "draw_page", G_CALLBACK (@draw_page), @p)
		
		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))
			
			Return 1
			
		Else
			
			? "Error Print"
			
		Endif
		
		gdk_pixbuf_unref(bmp)
		
		g_object_unref (pPrint)
	
	Endif
	
End Function