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

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.

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


// LogPane.cpp : implementation file
//

#include "stdafx.h"
#include "SCTS3DevMan.h"
#include "LogPane.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLogPane dialog


CLogPane::CLogPane(char *_filename,long _dataPos,long _dataSize,CWnd* pParent /*=NULL*/)
	: CDialog(CLogPane::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLogPane)
	//}}AFX_DATA_INIT
	filename=strdup(_filename);
	dataPos=_dataPos;
	dataSize=_dataSize;
}


void CLogPane::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLogPane)
	DDX_Control(pDX, IDC_LOG, m_xmlCtrl);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CLogPane message handlers
#define INDENT "    "

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;
}

BOOL CLogPane::OnInitDialog() 
{
	CDialog::OnInitDialog();
	FILE *fp;
	char *data;
	int indentLevel=0;
	CString indent="";
	CString m_xml="";
	char *pos1;
	char *pos2;
	int len=0;
	int cnt;
	BOOL isTag=FALSE;
	fp=fopen(filename,"rb");
	if (!fp)
	{
		m_xmlCtrl.SetWindowText("Bad file name.");
		return TRUE;
	}
	data=(char *)malloc(dataSize+1);
	if (!data)
	{
		m_xmlCtrl.SetWindowText("File too large,");
		fclose(fp);
		return TRUE;
	}
	fseek(fp,dataPos,SEEK_CUR);
	fread(data,sizeof(char),dataSize,fp);
	fclose(fp);
	data[dataSize]=0;
	pos1=pos2=data;
	
	/* Format XML */
	while(pos1[(len=strcspn(pos1,"<\n\\"))])
	{
	  	if (pos1[len]=='\\')
		{
			pos1[len]=0;
			m_xml+=pos1;
			m_xml+="\\\\";
			pos1+=len+1;
			continue;
		}
		indent="";
		for (cnt=indentLevel;cnt;cnt--)
			indent+=INDENT;
		if (pos1[len]=='\n')
		{
			pos1[len]=0;
			if (!isTag) m_xml+="\\par"+indent+INDENT;
			m_xml+=pos1;
			m_xml+="\\par"+indent+INDENT;
			pos1+=len+1;
			isTag=TRUE;
			continue;
		}
		if (pos1[len]=='<')
		{
			pos1[len]=0;
			m_xml+=pos1;
			pos1+=len;
			/* Handle CDATA section */
			if (!strncmp(pos1+1,"![CDATA[",8))
			{
				char *pos3;
				m_xml+="\\par"+indent+"\\b0\\cf2<![CDATA[\\cf0";
				pos2=pos1+len+9;
				pos1=strstr(pos2,"]]>");
				pos1[0]='\n';
				pos1[1]=0;
				pos1+=3;
				while ((pos3=strchr(pos2,'\n')))
				{
					*pos3=0;
					m_xml+="\\par"+indent+pos2;
					pos2=pos3+1;
				}
				m_xml+="\\cf2]]>\\cf0";
				continue;
			}
			*pos1='<';
			pos2=strchr(pos1,'>');
			*pos2=0;
			if(*(pos2-1)=='/' || *(pos2-1)=='?' || *(pos1+1)=='!')
			{
				indentLevel--;
			}
			if (isTag)
				m_xml+="\\par"+indent;
			if (pos1[1]=='/' && indentLevel)
				indentLevel--;
			else
			{
				indentLevel++;
				m_xml+=INDENT;
			}
			m_xml+="\\b0\\cf2<";
			if (*(pos1+1)=='!')
				m_xml+="\\cf4";
			else if (*(pos2-1)!='?')
				m_xml+="\\cf3";
			m_xml+=pos1+1;
			m_xml+="\\cf2>\\cf0\\b ";
			pos1=pos2+1;
			isTag=(*pos1=='<');
			indent="";
		}
	}
	m_xmlCtrl.SetWindowText("");
	CString rtfPrefix, rtfPostfix;
	rtfPrefix = "{\\rtf1\\ansi\\deff0\\deftab720{\\fonttbl{\\f0\\fswiss "
		"Arial;}}\n{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;"
		"\\red0\\green0\\blue255;\\red160\\green64\\blue0;\\red180\\green180\\blue180;}\\fs20";
	rtfPostfix = "\n\\par }";
	CString rtfString = rtfPrefix + m_xml + rtfPostfix;
	EDITSTREAM es = {(DWORD)&rtfString, 0, EditStreamCallBack};
	m_xmlCtrl.StreamIn(SF_RTF | SFF_SELECTION, es);
	free(data);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CLogPane::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	if (m_xmlCtrl.m_hWnd)
	{
		RECT rect;
		GetClientRect(&rect);
		m_xmlCtrl.MoveWindow(&rect);
	}
}

BOOL CLogPane::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);
}
