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

#include <w_app.hpp>
#include <z_scrdsp.hpp>

#define ZAF_SCREEN_DISPLAY_INFO
#include "gbl_def.cpp"

// Data used for initializing the color table. (see constructor)
static COLORREF ZAF_FARDATA zafColors[] =
{
	RGB(0x00, 0x00, 0x00),	// 0  - ZAF_CLR_BLACK
	RGB(0x00, 0x00, 0x80),	// 1  - ZAF_CLR_BLUE
	RGB(0x00, 0x80, 0x00),	// 2  - ZAF_CLR_GREEN
	RGB(0x00, 0x80, 0x80),	// 3  - ZAF_CLR_CYAN
	RGB(0x80, 0x00, 0x00),	// 4  - ZAF_CLR_RED
	RGB(0x80, 0x00, 0x80),	// 5  - ZAF_CLR_MAGENTA
	RGB(0x80, 0x80, 0x00),	// 6  - ZAF_CLR_BROWN
	RGB(0xC0, 0xC0, 0xC0),	// 7  - ZAF_CLR_LIGHTGRAY
	RGB(0x80, 0x80, 0x80),	// 8  - ZAF_CLR_DARKGRAY
	RGB(0x00, 0x00, 0xFF),	// 9  - ZAF_CLR_LIGHTBLUE
	RGB(0x00, 0xFF, 0x00),	// 10 - ZAF_CLR_LIGHTGREEN
	RGB(0x00, 0xFF, 0xFF),	// 11 - ZAF_CLR_LIGHTCYAN
	RGB(0xFF, 0x00, 0x00),	// 12 - ZAF_CLR_LIGHTRED
	RGB(0xFF, 0x00, 0xFF),	// 13 - ZAF_CLR_LIGHTMAGENTA
	RGB(0xFF, 0xFF, 0x00),	// 14 - ZAF_CLR_YELLOW
	RGB(0xFF, 0xFF, 0xFF),	// 15 - ZAF_CLR_WHITE
	RGB(0xE0, 0xE0, 0xE0),	// 16 - COLOR_3DHILIGHT (Windows Only)
};

// Data used for initializing the pattern table. (see constructor)
static WORD zafSolidData[8] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF };
static WORD zafInterleaveData[8] = { 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA };

ZafScreenDisplay::ZafScreenDisplay(int &, char **) :
	ZafGdiDisplay()
{
	// Initialize tables.
	int index;
	for (index = 0; index < ZAF_MAXCOLORS; index++)
		colorTable[index] = 0xFFFFFFFFL;
	for (index = 0; index < ZAF_MAXFONTS; index++)
		fontTable[index] = ZAF_NULLH(OSFont);

	// Initialize the font table.
	fontTable[ZAF_FNT_SYSTEM] = (HFONT)GetStockObject(SYSTEM_FONT);
	fontTable[ZAF_FNT_APPLICATION] = fontTable[ZAF_FNT_SYSTEM];
	fontTable[ZAF_FNT_FIXED] = (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
#if defined(ZAF_WIN32) && defined(ZAF_UNICODE)
	LOGFONTA logSmallFont;
	SystemParametersInfoA(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &logSmallFont, 0);
	fontTable[ZAF_FNT_SMALL] = CreateFontIndirectA(&logSmallFont);
#else
	LOGFONT	logSmallFont;
	SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &logSmallFont, 0);
	fontTable[ZAF_FNT_SMALL] = CreateFontIndirect(&logSmallFont);
#endif
	if (ZafMSWindowsApp::windowsPlatform == ZAF_WIN32_WINDOWS)
		fontTable[ZAF_FNT_DIALOG] = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
	else
		fontTable[ZAF_FNT_DIALOG] = fontTable[ZAF_FNT_SMALL];

	// Get a screen device context for initializing members.
	hDC = GetDC(0);

	// Determine if the display driver is a palette device.
	paletteDevice = (GetDeviceCaps(hDC, RASTERCAPS) & RC_PALETTE) ? true : false;

	// Map Zinc's reserved colors into the Windows default color palette.
	HPALETTE defaultPalette = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
	for (int i = 0; i < sizeof(zafColors) / sizeof(COLORREF); i++)
	{
		int index = GetNearestPaletteIndex(defaultPalette, zafColors[i]);
		PALETTEENTRY paletteEntry;
		GetPaletteEntries(defaultPalette, index, 1, &paletteEntry);
		colorTable[i] = RGB(paletteEntry.peRed, paletteEntry.peGreen, paletteEntry.peBlue);
	}

	CreateLogicalPalette();

	// Get the text metrics of the system font.
#if defined(ZAF_WIN32) && defined(ZAF_UNICODE)
	TEXTMETRICA textMetrics;
	HFONT oldFont = (HFONT)SelectObject(hDC, fontTable[ZAF_FNT_DIALOG]);
//	HFONT oldFont = (HFONT)SelectObject(hDC, fontTable[ZAF_FNT_SYSTEM]);
	GetTextMetricsA(hDC, &textMetrics);
#else
	TEXTMETRIC textMetrics;
	HFONT oldFont = (HFONT)SelectObject(hDC, fontTable[ZAF_FNT_DIALOG]);
//	HFONT oldFont = (HFONT)SelectObject(hDC, fontTable[ZAF_FNT_SYSTEM]);
	GetTextMetrics(hDC, &textMetrics);
#endif

	columns = GetDeviceCaps(hDC, HORZRES);
	lines = GetDeviceCaps(hDC, VERTRES);
	pixelsPerInchX = GetDeviceCaps(hDC, LOGPIXELSX);
	pixelsPerInchY = GetDeviceCaps(hDC, LOGPIXELSY);

/* START BLOCK COMMENT
**		// Initialize cell width and cell height.
**		// The "correct" method.
**		int baseX = (LOWORD(GetTextExtent(hDC, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52)) / 26 + 1) / 2;
**		int baseY = textMetrics.tmHeight;
**		cellWidth = baseX;
**		preSpace = postSpace = 2 * baseY / 8;
**		cellHeight = 14 * baseY / 8 + preSpace + postSpace;
END BLOCK COMMENT */

	preSpace = 2;
	postSpace = 2;
/* START BLOCK COMMENT
**		cellWidth = textMetrics.tmAveCharWidth + 1;
**		cellHeight =
**			textMetrics.tmHeight + 					// Text height
**			textMetrics.tmInternalLeading + 		// Space above text
**			textMetrics.tmExternalLeading + 		// Space below text
**			ZAF_BORDER_SIZE * 2 +	   				// border space
**			preSpace +								// preSpace (above the border)
**			postSpace;								// postSpace (below the border)
END BLOCK COMMENT */
	ZafRegionStruct sizeRegion = TextSize(ZAF_ITEXT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
	cellWidth = (sizeRegion.Width() + 19) / 39;
	cellHeight = (sizeRegion.Height() * 15 + 6) / 13 +
		2 * (ZAF_TEXT_MARGIN_SIZE + ZAF_BORDER_SIZE + ZAF_FOCUS_SIZE) +
		preSpace + postSpace;

	miniNumeratorX = 1;
	miniDenominatorX = 10;
	miniNumeratorY = 1;
	miniDenominatorY = 10;

	// Release screen device context.
	SelectObject(hDC, oldFont);
	ReleaseDC(0, hDC);
	hDC = 0;

	// Initialize the line style table.
	lineTable[ZAF_LINE_SOLID] = PS_SOLID;
	lineTable[ZAF_LINE_DOTTED] = PS_DOT;
	lineTable[ZAF_LINE_DASHED] = PS_DASH;
	lineTable[ZAF_LINE_DASHDOTTED] = PS_DASHDOT;

	// Initialize the pattern table.
	patternTable[ZAF_PTN_SOLID_FILL] = CreateBitmap(8, 8, 1, 1, zafSolidData);
	patternTable[ZAF_PTN_INTERLEAVE_FILL] = CreateBitmap(8, 8, 1, 1, zafInterleaveData);

	// Initialize the mix mode table.
	modeTable[ZAF_MODE_COPY].binary = R2_COPYPEN;
	modeTable[ZAF_MODE_COPY].ternary = SRCCOPY;
	modeTable[ZAF_MODE_XOR].binary = R2_XORPEN;
	modeTable[ZAF_MODE_XOR].ternary = SRCINVERT;
	mode = ZAF_MODE_COPY;

	// Initialize the Zinc palette.
	palette.lineStyle = ZAF_LINE_SOLID;
	palette.fillPattern = ZAF_PTN_SOLID_FILL;
	palette.colorForeground = ZAF_CLR_BLACK;
	palette.colorBackground = ZAF_CLR_WHITE;
	palette.monoForeground = ZAF_MONO_BLACK;
	palette.monoBackground = ZAF_MONO_WHITE;
	palette.font = ZAF_FNT_DIALOG;
	palette.osPalette.colorForeground = COLOR_WINDOWTEXT;
	palette.osPalette.colorBackground = COLOR_WINDOW;

	// Initialize the clip region.
	clipRegion.Assign(0, 0, columns, lines, ZAF_PIXEL);

	// Initialize zafDisplay.
	zafDisplay = this;
}

ZafScreenDisplay::~ZafScreenDisplay(void)
{
	// Clean up.
	DeleteObject(patternTable[ZAF_PTN_SOLID_FILL]);
	DeleteObject(patternTable[ZAF_PTN_INTERLEAVE_FILL]);
	DeleteObject(fontTable[ZAF_FNT_SMALL]);
	DeleteObject(logicalPalette);
}

