/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

// ErrorLogPane.cpp : implementation file
//

#include "stdafx.h"
#include "Scts3DevMan.h"
#include "ErrorLogPane.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CErrorLogPane dialog

static DWORD CALLBACK EditStreamCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb, 
					LONG *pcb)
{
	CString *pstr = (CString *)dwCookie;

	if( pstr->GetLength() < cb )
	{
		*pcb = pstr->GetLength();
		memcpy(pbBuff, (LPCSTR)*pstr, *pcb );
		pstr->Empty();
	}
	else
	{
		*pcb = cb;
		memcpy(pbBuff, (LPCSTR)*pstr, *pcb );
		*pstr = pstr->Right( pstr->GetLength() - cb );
	}
	return 0;
}

CErrorLogPane::CErrorLogPane(SctsErrorLog *_errorLog,CWnd* pParent /*=NULL*/)
	: CDialog(CErrorLogPane::IDD, pParent),errorLog(_errorLog)
{
	//{{AFX_DATA_INIT(CErrorLogPane)
	//}}AFX_DATA_INIT
}


void CErrorLogPane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CErrorLogPane)
	DDX_Control(pDX, IDC_ERRORRTF, m_errorRTF);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CErrorLogPane, CDialog)
	//{{AFX_MSG_MAP(CErrorLogPane)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CErrorLogPane message handlers

BOOL CErrorLogPane::OnInitDialog() 
{
	int level=0;
	char buffer[10];
	SctsTree<SctsErrorLog::Node *>::Item *nodeItem,*nextNodeItem;
	SctsErrorLog::NodeEntry *nodeEntry;
	CDialog::OnInitDialog();
	CString rtf;
	
	/* RTF prefix */
	rtf="{\\rtf1\\ansi\\deff0\\deftab720"
		"{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;\\red160\\green64\\blue0;}"
		"\\fs24\\b Error Log \\b0\\fs18";

	nodeItem=errorLog->nodeTree.GetRootItem();
	do
	{
		if (!level) rtf+="\\line ";
		if (nodeItem!=errorLog->nodeTree.GetRootItem())
		{
			itoa(level,buffer,10);
			rtf+="\\par\\b * \\li";
			rtf+=buffer;
			rtf+=" ";
			rtf+=nodeItem->data->name;
			rtf+="\\b0 ";
			if (nodeItem->data->desc)
			{
				rtf+="\\cf2\\line ";
				rtf+=nodeItem->data->desc;
				rtf+="\\cf0 ";
			}
			level+=300;
		}
		if ((nodeEntry=nodeItem->data->errors))
		{
			itoa(level,buffer,10);
			rtf+="\\par\\li";
			rtf+=buffer;
			rtf+=" ";
			for (nodeEntry=nodeItem->data->errors;nodeEntry;nodeEntry=nodeEntry->next)
			{
				rtf+=nodeEntry->error;
				if (nodeEntry->next) rtf+="\\line ";
			}
		}

		if ((nextNodeItem=errorLog->nodeTree.GetFirstChildItem(nodeItem)))
		{
			nodeItem=nextNodeItem;
			continue;
		}
		if (level) level-=300;
		if ((nextNodeItem=errorLog->nodeTree.GetNextSiblingItem(nodeItem)))
		{
			nodeItem=nextNodeItem;
			continue;
		}
		do
		{
			if (level) level-=300;
			nextNodeItem=errorLog->nodeTree.GetParentItem(nodeItem);
			if (nextNodeItem)
			{
				if ((nodeItem=errorLog->nodeTree.GetNextSiblingItem(nextNodeItem)))
					nextNodeItem=0;
				else
					nodeItem=nextNodeItem;
			}
			else
				nodeItem=0;
		}
		while (nextNodeItem);
	} while (nodeItem);
	
	/* RTF postfix */
	rtf+="\n\\par }";
	EDITSTREAM es = {(DWORD)&rtf, 0, EditStreamCallBack};
	m_errorRTF.SetTargetDevice(NULL,0);
	m_errorRTF.StreamIn(SF_RTF | SFF_SELECTION, es);
	m_errorRTF.SetSel(0,0);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CErrorLogPane::OnSize(UINT nType, int cx, int cy) 
{
	if (m_errorRTF.m_hWnd)
	{
		RECT rect;
		GetClientRect(&rect);
		m_errorRTF.MoveWindow(&rect);
		m_errorRTF.RedrawWindow();
	}
}

BOOL CErrorLogPane::PreTranslateMessage(MSG* pMsg) 
{
	/* Disable ESC  and Enter */
	if (pMsg->message==WM_KEYDOWN && (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN)) return TRUE;
	
	return CDialog::PreTranslateMessage(pMsg);
}
