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

#include <z_error.hpp>
#include <z_text.hpp>
#include <z_win.hpp>
#include <z_border.hpp>
#include <z_button.hpp>
#include <z_title.hpp>
#include <z_sys.hpp>
#include <z_string.hpp>
#include <z_icon1.hpp>

#define ZAF_ERROR_ICON_INFO
#include "img_def.cpp"

static ZafPaletteMap ZAF_FARDATA titleErrorPaletteMapTable[] =
{
	{ ZAF_PM_ANY_TYPE, ZAF_PM_ANY_STATE, { ZAF_LINE_SOLID, ZAF_PTN_SOLID_FILL, ZAF_CLR_WHITE, ZAF_CLR_RED, ZAF_MONO_WHITE, ZAF_MONO_BLACK, ZAF_FNT_SYSTEM } }
};

static ZafPaletteMap ZAF_FARDATA textErrorPaletteMapTable[] =
{
	{ ZAF_PM_LIGHT_SHADOW, ZAF_PM_ANY_STATE, { ZAF_LINE_SOLID, ZAF_PTN_SOLID_FILL, ZAF_CLR_LIGHTGRAY, ZAF_CLR_WHITE, ZAF_MONO_BLACK, ZAF_MONO_WHITE, ZAF_FNT_DIALOG } },
	{ ZAF_PM_DARK_SHADOW, ZAF_PM_ANY_STATE, { ZAF_LINE_SOLID, ZAF_PTN_SOLID_FILL, ZAF_CLR_DARKGRAY, ZAF_CLR_BLACK, ZAF_MONO_BLACK, ZAF_MONO_WHITE, ZAF_FNT_DIALOG } },
	{ ZAF_PM_ANY_TYPE, ZAF_PM_ANY_STATE, { ZAF_LINE_SOLID, ZAF_PTN_SOLID_FILL, ZAF_CLR_BLACK, ZAF_CLR_LIGHTGRAY, ZAF_MONO_WHITE, ZAF_MONO_BLACK, ZAF_FNT_DIALOG } }
};

// ----- ZafErrorSystem ----------------------------------------------------

ZafDialogEvent ZafErrorSystem::ErrorMessage(ZafWindowObject *,
	const ZafIChar *title, ZafDialogFlags msgFlags, const ZafIChar *message)
{
	// Beep the system.
	Beep();
	if (!message)
		return (0);
	ZafEventManager *eventManager = zafEventManager;
	ZafWindowManager *windowManager = zafWindowManager;

	const ZafEventType ERROR_OK = 9900;
	const ZafEventType ERROR_CANCEL = 9901;

	ZafBorder *border = new ZafBorder;
	ZafSystemButton *sysButton = new ZafSystemButton(ZAF_EMPTY_SYSTEM_BUTTON);
	sysButton->Add(new ZafPopUpItem(messages->GetMessage(ZAF_MSG_MOVE), ZAF_MOVE_OPTION));
	sysButton->Add(new ZafPopUpItem(messages->GetMessage(ZAF_MSG_CLOSE), ZAF_CLOSE_OPTION));

	const ZafIChar *titleText;
	if (*title)
		titleText = title;
	else 
		titleText = messages->GetMessage(ZAF_MSG_TITLE);
	ZafTitle *titleBar = new ZafTitle(titleText);

	titleBar->SetUserPaletteData(new ZafPaletteData(titleErrorPaletteMapTable, true));

	ZafDisplay *display = windowManager->display;
	int width = 43;
	int height = 6;
	const ZafIChar *text = message;
#if defined(ZAF_GRAPHICS_MODE)
	int textWidth = 30 * display->cellWidth;
#elif defined(ZAF_TEXT_MODE)
	int textWidth = 30;
#endif
	display->SetFont(ZAF_FNT_DIALOG);
	int textHeight = display->TextSize(text).Height() / textWidth + 1;
#if defined(ZAF_TEXT_MODE)
		height += textHeight;
#elif defined(ZAF_GRAPHICS_MODE)
		height += (textHeight * (display->TextSize(message).Height() + display->preSpace + display->postSpace) / display->cellHeight) + 1;
#endif

	// Check for newLine characters.
	for (ZafIChar *newLine = strchr((ZafIChar *)text, '\n'); newLine;
			newLine = strchr(++newLine, '\n'))
		height++;

	int left = (display->columns / display->cellWidth - width) / 2;
	int top = (display->lines / display->cellHeight - height) / 2;
	ZafWindow *window = new ZafWindow(left, top, width, height);
	window->SetModal(true);
	window->SetSizeable(false);
	window->SetDestroyable(false);
	window->Add(border);
	window->Add(sysButton);
	window->Add(titleBar);
#if defined(ZAF_TEXT_MODE)
	ZafText *textField = new ZafText(2, 1, 30, height - 6, new ZafStringData(message));
	textField->SetUserPaletteData(new ZafPaletteData(textErrorPaletteMapTable, true));
#elif defined(ZAF_GRAPHICS_MODE)
	ZafText *textField = new ZafText(8, 1, 30, height - 5, new ZafStringData(message));
	ZafIcon *icon = new ZafIcon(2, 1, ZAF_NULLP(ZafIChar), new ZafIconData(_error_ZafIcon));
	icon->SetBordered(false);
	icon->SetDisabled(true);
	icon->SetNoncurrent(true);
	window->Add(icon);
#endif
	textField->SetBordered(false);
	textField->SetNoncurrent(true);
	textField->SetUserPaletteData(new ZafPaletteData(textErrorPaletteMapTable, true));
	textField->SetViewOnly(true);
	window->Add(textField);

	int buttonCount = 0;
	if (msgFlags & ZAF_DIALOG_CANCEL)
		++buttonCount;
	if (msgFlags & ZAF_DIALOG_OK)
		++buttonCount;
	if (msgFlags & ZAF_DIALOG_HELP)
		++buttonCount;

	int buttonWidth = 12;

	int buttonLeft = (width / 2) - (buttonCount * buttonWidth + buttonCount - 1) / 2;

	if (msgFlags & ZAF_DIALOG_CANCEL)
	{
		ZafButton *button = new ZafButton(buttonLeft, height - 3, buttonWidth, 1,
				ZAF_ITEXT("Cancel"));
		button->SetSendMessageWhenSelected(true);
		button->SetValue(ERROR_CANCEL);
		window->Add(button);
		buttonLeft += buttonWidth + 1;
	}
	if (msgFlags & ZAF_DIALOG_OK)
	{
		ZafButton *button = new ZafButton(buttonLeft, height - 3, buttonWidth, 1,
				ZAF_ITEXT("Ok"));
		button->SetSendMessageWhenSelected(true);
		button->SetValue(ERROR_OK);
		window->Add(button);
		buttonLeft += buttonWidth + 1;
	}
	if (msgFlags & ZAF_DIALOG_HELP)
	{
		ZafButton *button = new ZafButton(buttonLeft, height - 3, buttonWidth, 1,
				ZAF_ITEXT("Help"));
		button->SetSendMessageWhenSelected(true);
		window->Add(button);
	}

	windowManager->Add(window);

	// Run the window in a modal fashion.
	ZafEventStruct event;
	ZafEventType ccode = 0;
	do
	{
		eventManager->Get(event, Q_NORMAL);
		ccode = event.type;
		if (ccode == ERROR_OK || ccode == ERROR_CANCEL)
			windowManager->Subtract(window);
		else
			ccode = windowManager->Event(event);
	} while (ccode != ERROR_OK && windowManager->Index(window) != -1);
	windowManager->Subtract(window);
	delete window;
	return ((ccode == ERROR_OK) ? S_DLG_OK : S_DLG_CANCEL);
}
