/*

CLASS {NATIVE_Printers|_CSharp},
	UNTESTED, NATIVE:

IMPORT: {AL4_drawing}, {Printer}, {Canvas} .

NATIVE: "using System.Drawing.Printing;" .
NATIVE: "///copy NATIVE_Printers.cs" .

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using AL4;
using AL4_project;

namespace AL4
{

public class AL4Print
{

public static List<AL4PrintDocument> All_print_docs 
	   = new List<AL4PrintDocument>();
public static List<Graphics> All_print_graphics = new List<Graphics>();
    
/*

FUNCTION Create_printer_object ==> INT RESULT 
	| Handle_printer,
	NATIVE :
@	"	AL4PrintDocument p  = new AL4PrintDocument();"
	"	if (AL4Print.All_print_docs.Count == 0)"
	"		AL4Print.All_print_docs.Add(null);"
	"	for (int j = 1; j < AL4Print.All_print_docs.Count; j++)"
	"		if (AL4Print.All_print_docs[j] == null)"
	"		{"
	"			AL4Print.All_print_docs[j] = p;"
	"			return j ;"
	"		}"
	"	int i = AL4Print.All_print_docs.Count;"
	"	AL4Print.All_print_docs.Add(p);"
	"	return i;" .
	
FUNCTION Release_printer_canvas(
	INT Printer_handle, INT Canvas_handle), NATIVE :
	Destroy_canvas(Canvas_handle)
	NATIVE
@	"	//AL4PrintDocument p = AL4Print.All_print_docs[(int)X_Printer_handle];"
	"	AL4Print.All_print_docs[(int)X_Printer_handle] = null;" .

FUNCTION Get_list_of_printers(
	INT Printer_handle) ==> STR, NATIVE :
@	"	foreach (string s in "
	"		System.Drawing.Printing.PrinterSettings.InstalledPrinters)"
	"	{"
	"		RESULT += "#QU"\n\r"#QU" + s;"
	"	}"
	"	RESULT = RESULT.Trim();" .
	
FUNCTION Select_printer_index(
	INT Printer_handle, INT Printer_index, STR Name_of_printer), NATIVE :
@	"	AL4PrintDocument p = AL4Print.All_print_docs[(int)X_Printer_handle];"
	"	int i = 0;"
	"	foreach (string s in "
	"		System.Drawing.Printing.PrinterSettings.InstalledPrinters)"
	"	{"
	"		if (i == X_Printer_index)"
	"		{"
	"			p.PrinterSettings.PrinterName = s;"
	"			return;"
	"		}"
	"		i++;"
	"	}"
	"	p.PrinterSettings.PrinterName = X_Name_of_printer;" .
	
FUNCTION Begin_print_document(
	{Printer} Printer|_device, 
	STR Title|_document,
	INT Copies|_count) ==> BOOL RESULT|_false_already_finished,
	NATIVE :
@	"	C_Printer C_P = (C_Printer)(X_Printer.Obj);"	
	"	AL4PrintDocument p = "
	"			AL4Print.All_print_docs[(int)C_P.F_printer_handle];"
	"	if (p == null) return false;"     
	"	p.Page_index = 0;"
	"	p.DocumentName = X_Title;"
	"	p.PrinterSettings.Copies = (short)X_Copies;"
	"	AL4Print a4p = new AL4Print();"
	"	a4p.printer_obj = C_P.hard_link;"
	"	p.PrintPage += new PrintPageEventHandler(a4p.Print_page);"
	"	try"
	"	{"
	"		p.Print();"
	"	}"
	"	catch(Exception)"
	"	{ }" 
	"	RESULT = false;" .
	
*/

public Ref_obj printer_obj;
	
public void Print_page(object sender, PrintPageEventArgs e)
{
	AL4PrintDocument a4pd = (AL4PrintDocument)sender;
	C_Printer C_P = (C_Printer)printer_obj.Obj;
	while (All_print_graphics.Count <= C_P.F_printer_handle)
		All_print_graphics.Add(null);
	All_print_graphics[(int)C_P.F_printer_handle] = e.Graphics;
	try
	{
		//++++++++++++++
		
		//--------------
		bool last = C_P.Do.M_call_internal_print_page(a4pd.Page_index++);
		e.HasMorePages = ! last;
	}
	finally
	{
		All_print_graphics[(int)C_P.F_printer_handle] = null;
	}
}

public static Graphics extract_graphics_4printer(int printer_handle)
{
	return All_print_graphics[printer_handle];
}

/*
	
FUNCTION End_print_document(INT Printer_handle), NATIVE :
@	"" .
	
FUNCTION New_page(INT Printer_handle), NATIVE :
@	"" .
	
FUNCTION Supports_copies ==> BOOL : RESULT = TRUE .


*/

} // end of class

public class AL4PrintDocument : PrintDocument
{
	public int Page_index;
}	

/*

FUNCTION Set_paper_landscape(
{Canvas} C|anvas_of_printer, BOOL Is_landscape), NATIVE
    :
	CASE C.Printer_handle == 0 ? ==> ;
	NATIVE @     
    "AL4PrintDocument p = "
	"			AL4Print.All_print_docs["
	"				(int)((C_Canvas)X_C.Obj).F_Printer_handle];"
	"	p.PrinterSettings.DefaultPageSettings.Landscape = X_Is_landscape;" .

END
*/
	
} // end of namespace
