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

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

#if defined(ZAF_INDEPENDENT)
#	if !defined(ZAF_METAWINDOW)
#		include <ugl.h>
#	endif

#endif

#if defined(ZAF_MSWINDOWS)
class ZAF_EXPORT ZafScreenDisplay : public ZafGdiDisplay
#else
class ZAF_EXPORT ZafScreenDisplay : public ZafDisplay
#endif
{
public:
#if !defined(ZAF_MSWINDOWS)
	// Logical mapping tables.
	OSLineStyle lineTable[ZAF_MAXLINES];
	OSFillPattern patternTable[ZAF_MAXPATTERNS];
	OSColor colorTable[ZAF_MAXCOLORS];
	OSMono monoTable[ZAF_MAXCOLORS];
	OSFont fontTable[ZAF_MAXFONTS];
	OSMode modeTable[ZAF_MAXMODES];
#endif

	ZafScreenDisplay(int &argc, char **argv);
	virtual ~ZafScreenDisplay(void);

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

#if !defined(ZAF_MSWINDOWS)
	// --- Drawing members ---
	// bitmap
	virtual ZafError Bitmap(ZafCoordinate column, ZafCoordinate line, ZafBitmapStruct &bitmap);
	virtual ZafError ConvertToOSBitmap(ZafBitmapStruct &bitmap);
	virtual ZafError ConvertToZafBitmap(ZafBitmapStruct &bitmap);
	virtual ZafError DestroyOSBitmap(ZafBitmapStruct &bitmap);
	// icon
	virtual ZafError Icon(ZafCoordinate column, ZafCoordinate line, ZafIconStruct &icon);
	virtual ZafError ConvertToOSIcon(ZafIconStruct &icon);
	virtual ZafError ConvertToZafIcon(ZafIconStruct &icon);
	virtual ZafError DestroyOSIcon(ZafIconStruct &icon);
	// mouse
	virtual ZafError Mouse(ZafCoordinate column, ZafCoordinate line, ZafMouseStruct &mouse);
	virtual ZafError ConvertToOSMouse(ZafMouseStruct &mouse);
	virtual ZafError ConvertToZafMouse(ZafMouseStruct &mouse);
	virtual ZafError DestroyOSMouse(ZafMouseStruct &mouse);
	// text
	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);
	// graphic primitives
	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);
	virtual ZafError RegionCopy(const ZafRegionStruct &oldRegion,
		int newColumn, int newLine);
	virtual ZafError RectangleXORDiff(const ZafRegionStruct *oldRegion,
		const ZafRegionStruct *newRegion);

	// --- Drawing support ---
	// get
	virtual ZafLogicalColor Background(void) const;
	virtual ZafRegionStruct ClipRegion(void) const;
	virtual ZafLogicalFont Font(void) const;
	virtual ZafLogicalColor Foreground(void) const;
	virtual ZafLogicalMode Mode(void) const;
	virtual ZafLogicalColor MonoBackground(void) const;
	virtual ZafLogicalColor MonoForeground(void) const;
	virtual ZafPaletteStruct Palette(void) const;
	virtual ZafLogicalFillPattern FillPattern(void) const;
	virtual ZafLogicalLineStyle LineStyle(void) const;
	// set
	virtual ZafLogicalColor SetBackground(ZafLogicalColor color);
	virtual ZafRegionStruct SetClipRegion(const ZafRegionStruct &region);
	virtual ZafLogicalFont SetFont(ZafLogicalFont font);
	virtual ZafLogicalColor SetForeground(ZafLogicalColor color);
	virtual ZafLogicalLineStyle SetLineStyle(ZafLogicalLineStyle line);
	virtual ZafLogicalMode SetMode(ZafLogicalMode mode);
	virtual ZafLogicalColor SetMonoBackground(ZafLogicalColor color);
	virtual ZafLogicalColor SetMonoForeground(ZafLogicalColor color);
	virtual ZafPaletteStruct SetPalette(ZafPaletteStruct palette);
	virtual ZafLogicalFillPattern SetFillPattern(ZafLogicalFillPattern pattern);

	// --- 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));

	// --- ScreenOS specific support members ---
	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 OSDisplayContext SetDisplayContext(OSDisplayContext context);
	virtual OSDrawContext SetDrawContext(OSDrawContext context);

private:
#	if defined(ZAF_MOTIF)
public:
	XtAppContext xAppContext;
	int xScreenNumber;

	Widget topLevelShell;
	Display *xDisplay;
	GC xGC;
	Drawable xDrawable;

	XIC ic;

	bool ZafArrayToOSPixmap(int width, int height, ZafLogicalColor *array, Pixmap *handle, Pixmap *mask);
	bool OSPixmapToZafArray(int *width, int *height, ZafLogicalColor **array, Pixmap handle, Pixmap mask);
	XFontSet MtfGetFontSet(XmFontList fontList) const;
	XFontStruct *MtfGetFont(XmFontList fontList) const;
	ZafLogicalColor MtfGetColor(char *color);
#	elif defined(ZAF_MACINTOSH)
	Boolean usedColor[ZAF_MAXCOLORS];
	Boolean usedFont[ZAF_MAXFONTS];
	CTabHandle pixMapColorTable;
	GrafPtr restorePort;
	int virtualCount;
	RgnHandle drawRgn;
	RgnHandle restoreClipRgn;
#	elif defined(ZAF_INDEPENDENT) || defined(ZAF_CURSES)
public:
	static ZafPaletteStruct *backgroundPalette;
	static ZafPaletteStruct *xorPalette;
	static ZafPaletteStruct *markPalette;

#		if defined(ZAF_INDEPENDENT) || defined(ZAF_CURSES)
#			if defined(ZAF_GRAPHICS_MODE)
#				if defined(ZAF_METAWINDOW)
	struct ZAF_EXPORT ZafDisplayImage
	{
		ZafRegionStruct region, clip;
		ZafLogicalColor *image;
		grafPort *screen;
		grafPort *backup;
	};
	ZafDisplayImage displayImage[ZAF_MAXIMAGES];
	grafPort *screenPort;
#				else
	UGL_DEVICE_ID devID;
	UGL_GC_ID gc;

	struct ZAF_EXPORT ZafCursorImage
	{
		UGL_CURSOR_ID cursorID;
		ZafRegionStruct clip;
		bool movePending;
		int newX, newY;
	};
	ZafCursorImage cursorImage[ZAF_MAXIMAGES];
				#endif
#			elif defined(ZAF_TEXT_MODE)
	struct ZAF_EXPORT ZafDisplayImage
	{
		ZafRegionStruct region;
		ZafLogicalColor *image;
		ZafScreenCell *screen;
		ZafScreenCell *backup;
	};
	ZafDisplayImage displayImage[ZAF_MAXIMAGES];

	ZafScreenCell *screenBuffer;
	ZafLogicalMode isMono;
	ZafIChar *codeSet;
	bool borderFlags;

	static ZafIChar tCornerUL[];
	static ZafIChar tCornerUR[];
	static ZafIChar tCornerLL[];
	static ZafIChar tCornerLR[];
	static ZafIChar tHorizontal[];
	static ZafIChar tVertical[];

	ZafLogicalColor MapColor(const ZafPaletteStruct *palette);
#			endif
#		endif

	ZafRegionList drawRegionList;

	bool initialized;
	signed char virtualCount;
	ZafRegionStruct viewPort;

//	virtual void SetViewPort(ZafRegionStruct *viewPort);
	virtual void SetDrawRegions(ZafRegionList *drawList,
		bool destroyDrawList = true);

	bool destroyDrawList;
	// ADVANCED functions for mouse and cursor --- DO NOT USE! ---
	char stopDevice;
	virtual int DeviceMove(ZafDeviceImageType imageType, int newColumn, int newLine, ZafRegionStruct &clip);
	virtual int DeviceSet(ZafDeviceImageType imageType, int column, int line, int width, int height, ZafLogicalColor *image, ZafRegionStruct &clip);
private:
	ZafRegionStruct masterClip;
#	endif
#endif
};

#endif // Z_SCRDSP_HPP
