//	Zinc Application Framework - Z_PRINT.HPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#if !defined(Z_PRINT_HPP)
#define Z_PRINT_HPP
#include <z_dlgwin.hpp>
#if defined(ZAF_MSWINDOWS)
#include <w_gdidsp.hpp>
#else
#include <z_dsp.hpp>
#endif

// Define the print setup event.
const ZafEventType S_PRINT_SETUP = -5000;

typedef int ZafPrinterType;
const ZafPrinterType ZAF_NATIVE_PRINTER = 0;
const ZafPrinterType ZAF_POSTSCRIPT_1_PRINTER = 1;
const ZafPrinterType ZAF_POSTSCRIPT_2_PRINTER = 2;
const ZafPrinterType ZAF_PCL_PRINTER = 3;

typedef int ZafPaperOrientation;
const ZafPaperOrientation ZAF_PORTRAIT_ORIENTATION = 0;
const ZafPaperOrientation ZAF_LANDSCAPE_ORIENTATION = 1;

struct ZAF_EXPORT ZafPrintJobStruct
{
	// --- Data Memebers ---
	bool collate;
	int copies, startPage, endPage, minPage, maxPage;
};

#if defined (ZAF_MSWINDOWS)
class ZAF_EXPORT ZafPrinter : public ZafGdiDisplay
#else
class ZAF_EXPORT ZafPrinter : public ZafDisplay
#endif
{
	friend class ZafPrintDialog;
public:
	ZafPrinter(void);
	virtual ~ZafPrinter(void);

	// --- Drawing members ---
#if defined(ZAF_MSWINDOWS)
	// These members are inherited from ZafGdiDisplay.
#else
#	if defined(ZAF_MACINTOSH)
	virtual ZafError ConvertToOSBitmap(ZafBitmapStruct &bitmap);
	virtual ZafError ConvertToOSIcon(ZafIconStruct &icon);
	virtual ZafError DestroyOSBitmap(ZafBitmapStruct &bitmap);
	virtual ZafError DestroyOSIcon(ZafIconStruct &icon);
#	endif
	virtual ZafError Bitmap(ZafCoordinate column, ZafCoordinate line, ZafBitmapStruct &bitmap);
	virtual ZafError Icon(ZafCoordinate column, ZafCoordinate line, ZafIconStruct &icon);
	virtual ZafError Mouse(ZafCoordinate column, ZafCoordinate line, ZafMouseStruct &mouse);
	virtual ZafError Text(ZafCoordinate left, ZafCoordinate top, const ZafIChar *text, int length = -1,
		int hotKeyIndex = -1, bool fill = false);
	virtual ZafError Text(ZafCoordinate left, ZafCoordinate top, ZafCoordinate right, ZafCoordinate bottom,
		const ZafIChar *text, int length = -1,
		ZafHzJustify hzJustify = ZAF_HZ_LEFT,
		ZafVtJustify vtJustify = ZAF_VT_CENTER,
		int hotKeyIndex = -1, bool fill = false);
	virtual ZafRegionStruct TextSize(const ZafIChar *text, int length = -1);
	virtual ZafError Ellipse(ZafCoordinate left, ZafCoordinate top, ZafCoordinate right, ZafCoordinate bottom,
		float startAngle, float endAngle, int width = ZAF_HAIR_LINE, bool fill = false);
	virtual ZafError Line(ZafCoordinate column1, ZafCoordinate line1, ZafCoordinate column2, ZafCoordinate line2,
		int width = ZAF_HAIR_LINE);
	virtual ZafError Pixel(ZafCoordinate column, ZafCoordinate line,
		ZafLogicalColor color = ZAF_CLR_DEFAULT, ZafLogicalColor mono = ZAF_MONO_DEFAULT);
	virtual ZafError Polygon(int numPoints, const ZafCoordinate *polygonPoints,
		int width = ZAF_HAIR_LINE, bool fill = false, bool close = false);
	virtual ZafError Rectangle(ZafCoordinate left, ZafCoordinate top, ZafCoordinate right, ZafCoordinate bottom,
		int width = ZAF_HAIR_LINE, bool fill = false);
#endif
	virtual ZafError RegionCopy(const ZafRegionStruct &oldRegion,
		int newColumn, int newLine);
	virtual ZafError RectangleXORDiff(const ZafRegionStruct *oldRegion,
		const ZafRegionStruct *newRegion);
	virtual ZafCoordinate TextBlock(ZafCoordinate left, ZafCoordinate top, ZafCoordinate right, ZafCoordinate bottom,
		int &charactersPrinted, const ZafIChar *text, int length = -1,
		ZafHzJustify hzJustify = ZAF_HZ_LEFT,
		ZafVtJustify vtJustify = ZAF_VT_TOP,
		bool fill = false);
	virtual ZafCoordinate TextLine(ZafCoordinate left, ZafCoordinate top, ZafCoordinate right, const ZafIChar *text,
		int length = -1, ZafHzJustify hzJustify = ZAF_HZ_LEFT, bool fill = false);

#if defined(ZAF_MSWINDOWS)
	// These members are inherited from ZafGdiDisplay.
#else
	// --- Table support ---
	virtual ZafLogicalColor AddColor(ZafLogicalColor index, ZafUInt8 red, ZafUInt8 green, ZafUInt8 blue);
	virtual ZafLogicalFont AddFont(ZafLogicalFont index, char *fontFamily, int pointSize,
		ZafFontWeight weight = ZAF_FNT_WEIGHT_NORMAL,
		ZafFontSlant slant = ZAF_FNT_SLANT_NORMAL,
		ZafFontPitch pitch = ZAF_FNT_PITCH_DEFAULT);
	virtual ZafError DestroyColor(ZafLogicalColor color);
	virtual ZafError DestroyFont(ZafLogicalFont font);
	virtual ZafError ColorInfo(ZafLogicalColor index, ZafUInt8 &red, ZafUInt8 &green, ZafUInt8 &blue);
	virtual ZafError FontInfo(ZafLogicalFont index, char *fontFamily, int bufferSize,
		int *pointSize, ZafFontWeight *weight = ZAF_NULLP(ZafFontWeight),
		ZafFontSlant *slant = ZAF_NULLP(ZafFontSlant),
		ZafFontPitch *pitch = ZAF_NULLP(ZafFontPitch),
		ZafCoordinate *averageCharWidth = ZAF_NULLP(ZafCoordinate),
		ZafCoordinate *height = ZAF_NULLP(ZafCoordinate),
		ZafCoordinate *leading = ZAF_NULLP(ZafCoordinate));
#endif

	// --- Class identification ---
	static ZafClassNameChar ZAF_FARDATA displayType[];
	virtual ZafClassName DisplayType(void) const { return (displayType); }

	// --- Support members ---
#if defined(ZAF_MSWINDOWS)
	// These members are inherited from ZafGdiDisplay.
#else
	virtual ZafError BeginDraw(OSDisplayContext displayContext,
		OSDrawContext drawContext, const ZafRegionStruct &draw,
		const ZafRegionStruct &clip);
	virtual ZafError EndDraw(void);
	virtual OSDisplayContext DisplayContext(void) const;
	virtual OSDrawContext DrawContext(void) const;
	virtual OSDisplayContext RestoreDisplayContext(void);
	virtual OSDrawContext RestoreDrawContext(void);
	virtual OSDrawContext SetDrawContext(OSDrawContext context);
#endif
	virtual ZafRegionStruct SetClipRegion(const ZafRegionStruct &region);
	virtual OSDisplayContext SetDisplayContext(OSDisplayContext context);

	// --- Printer members ---
	virtual bool JobSetup(ZafPrintJobStruct &printJob);
	virtual bool PrintSetup(void);
	virtual ZafPrinterType PrinterType(void) const;
	virtual ZafPaperOrientation PaperOrientation(void) const;
	virtual ZafCoordinate PaperWidth(void) const;
	virtual ZafCoordinate PaperHeight(void) const;
	virtual ZafCoordinate PageWidth(void) const;
	virtual ZafCoordinate PageHeight(void) const;
	virtual void Margins(ZafCoordinate &leftMargin, ZafCoordinate &topMargin,
		ZafCoordinate &rightMargin, ZafCoordinate &bottomMargin) const;
	virtual ZafPrinterType SetPrinterType(ZafPrinterType printerType);
	virtual ZafPaperOrientation SetPaperOrientation(ZafPaperOrientation paperOrientation);
	virtual ZafCoordinate SetPaperWidth(ZafCoordinate paperWidth);
	virtual ZafCoordinate SetPaperHeight(ZafCoordinate paperHeight);
	virtual ZafCoordinate SetPageWidth(ZafCoordinate paperWidth);
	virtual ZafCoordinate SetPageHeight(ZafCoordinate paperHeight);
	virtual void SetMargins(ZafCoordinate leftMargin, ZafCoordinate topMargin,
		ZafCoordinate rightMargin, ZafCoordinate bottomMargin);

	// --- Printer Control ---
	ZafError AbortJob(void);
	ZafError BeginJob(ZafIChar *jobName = ZAF_NULLP(ZafIChar));
	ZafError BeginPage(void);
	ZafError EndJob(void);
	ZafError EndPage(void);

#if defined(ZAF_MACINTOSH)
	THPrint MacPrintJob(void) { return (printJob); }
#endif

protected:
	int marginLeft;
	int marginTop;
	int marginRight;
	int marginBottom;
	int pageWidth;
	int pageHeight;
	int paperWidth;
	int paperHeight;
	ZafPrinterType printerType;
	ZafPaperOrientation paperOrientation;

#if defined(ZAF_MSWINDOWS)
	// These members are inherited from ZafGdiDisplay.
#elif defined(ZAF_MOTIF) || defined(ZAF_INDEPENDENT) || defined(ZAF_CURSES)
	struct POSTSCRIPTFONT
	{
		char typeFace[32];
		short pointSize;
	};
	// Logical mapping tables.
	char lineTable[ZAF_MAXLINES][32];
	char patternTable[ZAF_MAXPATTERNS][64];
	unsigned long colorTable[ZAF_MAXCOLORS];
	POSTSCRIPTFONT fontTable[ZAF_MAXFONTS];
	char modeTable[ZAF_MAXMODES][32];
	ZafIChar printfileName[256];
	ZafIChar printCommand[1024];

#	if defined(ZAF_INDEPENDENT)
	ZafError BeginJobInitialize(ZafIChar *jobName);
#	endif
#else
	// Logical mapping tables.
	OSLineStyle lineTable[ZAF_MAXLINES];
	OSFillPattern patternTable[ZAF_MAXPATTERNS];
	OSColor colorTable[ZAF_MAXCOLORS];
	OSFont fontTable[ZAF_MAXFONTS];
	OSMode modeTable[ZAF_MAXMODES];

#	if defined(ZAF_MACINTOSH)
	void OSGetPrinterInfo(void);
#	endif
#endif

private:
#if defined(ZAF_MSWINDOWS)
	HGLOBAL hDevMode;
	HGLOBAL hDevNames;
	int nonprintableLeft;
	int nonprintableTop;

	int DriverMode(DWORD field, int newValue = 0);
#elif defined(ZAF_MOTIF) || defined(ZAF_INDEPENDENT) || defined(ZAF_CURSES)
	char PSFilePath[256];	// XX Should use ZafPath ?
	FILE *outfile;			// XX Should use ZafFile ?
	void EmitForeground();
	void EmitBackground();
	void EmitLineAttributes(int width = 0);
	void EmitImage(ZafCoordinate x, ZafCoordinate y,
		double xscale, double yscale, int width, int height,
		ZafLogicalColor *array);
#elif defined(ZAF_MACINTOSH)
	Boolean usedColor[ZAF_MAXCOLORS];
	Boolean usedFont[ZAF_MAXFONTS];
	CTabHandle pixMapColorTable;
	GrafPtr restorePort;
	int virtualCount;
	RgnHandle restoreClipRgn;

	TPPrPort printerPort;
	THPrint printJob;
#endif
};

class ZAF_EXPORT ZafPrintSetupDialog : public ZafDialogWindow
{
	public:
		ZafPrintSetupDialog(int left, int top, ZafPrinter *printer);
		ZafEventType Event(const ZafEventStruct &event);

	protected:
		ZafPrinter *printer;
};

class ZAF_EXPORT ZafPrintDialog : public ZafDialogWindow
{
public:
	ZafPrintDialog(int left, int top, ZafPrinter *printer, ZafPrintJobStruct *printJob);
	virtual ZafDialogEvent Control(ZafQFlags flags = Q_NORMAL);
	ZafEventType Event(const ZafEventStruct &event);

	// --- Attributes ---
	// get
	bool AllowModifyCollate(void) const;
	bool AllowModifyCopies(void) const;
	bool AllowModifyRange(void) const;
	// set
	bool SetAllowModifyCollate(bool allowModifyCollate);
	bool SetAllowModifyCopies(bool allowModifyCopies);
	bool SetAllowModifyRange(bool allowModifyRange);

protected:
	ZafPrinter *printer;
	ZafPrintJobStruct *printJob;
	bool allowModifyCollate;
	bool allowModifyCopies;
	bool allowModifyRange;
};

inline bool ZafPrintDialog::AllowModifyCollate(void) const { return (allowModifyCollate); }
inline bool ZafPrintDialog::AllowModifyCopies(void) const { return (allowModifyCopies); }
inline bool ZafPrintDialog::AllowModifyRange(void) const { return (allowModifyRange); }
inline bool ZafPrintDialog::SetAllowModifyCollate(bool setAllowModifyCollate)
{ allowModifyCollate = setAllowModifyCollate; return (allowModifyCollate); }
inline bool ZafPrintDialog::SetAllowModifyCopies(bool setAllowModifyCopies)
{ allowModifyCopies = setAllowModifyCopies; return (allowModifyCopies); }
inline bool ZafPrintDialog::SetAllowModifyRange(bool setAllowModifyRange)
{ allowModifyRange = setAllowModifyRange; return (allowModifyRange); }

#endif // Z_PRINT_HPP
