// LogEdit.cpp : implementation file
//

#include "stdafx.h"
#include "LogEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*************************************************************************
 FILE :		  LogEdit.cpp Version 1.0

 Author :	  Ranojay Sen  (sen_ranojay@email.com)

 Description: CLogEdit is a CEdit derived class and runs on MS-Windows

			  CLogEdit class can be used very easily for the 
			  purpose of logging data by any application or any 
			  Hardware. The automatic scrolling and updating the
			  Cursor position to the end of the line are the main 
			  features of this class. this code can be modified by 
			  profesional programmers to serve their specific needs
			  

Copyright(c) 2008
by  Ranojay Sen  (sen_ranojay@email.com)

This code may be used in compiled form in any way you desire. This
file may be redistributed unmodified by any means PROVIDING it is 
not sold for profit without the authors written consent, and 
providing that this notice and the authors name is included. If 
the source code in this file is used in any commercial application 
then a simple email to the author would be nice.

This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability if it causes any damage.

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

/////////////////////////////////////////////////////////////////////////////
// CLogEdit

CLogEdit::CLogEdit()
{
	m_maxLineCount = 1000;
	flag=1;
	PromptStr="Prompt::";
}

CLogEdit::~CLogEdit()
{
}


BEGIN_MESSAGE_MAP(CLogEdit, CEdit)
	//{{AFX_MSG_MAP(CLogEdit)
	ON_WM_SETFOCUS()
	ON_WM_CHAR()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLogEdit message handlers

int CLogEdit::Trace(TCHAR *szString, ...)
{
	va_list args;
	va_start(args, szString);

	int nrs = _Trace(szString, args);

	va_end(args);
	return nrs;
}

int CLogEdit::_Trace(TCHAR *szFormat, va_list args)
{
	int nrs;
	DWORD dwWrite=0;
	SYSTEMTIME sysTime={0};
	TCHAR szHead[1024*10] = {0,};
	TCHAR szBuffer[1024*10] = {0,};

	GetLocalTime(&sysTime);

	nrs = _vsntprintf(szBuffer, sizeof szBuffer, szFormat, args);

	if(szBuffer[nrs-1] == 0x0D || szBuffer[nrs-1] == 0x0A)
	{
		szBuffer[nrs-1]	= 0x0D;
		szBuffer[nrs]	= 0x0A;
		szBuffer[++nrs]	= 0;
	}

	dwWrite = wsprintf(szHead, TEXT("[%d-%02d-%02d %02d:%02d:%02d] %s"), sysTime.wYear, sysTime.wMonth, sysTime.wDay
				, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, szBuffer);

	InsertLines(szHead, FALSE);

	return nrs;
}

void CLogEdit::RemoveTopLine(CString& message)
{
	INT32 pos = message.Find(L"\r\n", 0);
	if(pos >= 0) {
		message = message.Right(message.GetLength() - (pos + 2)); 
	}
}

int CLogEdit::GetLineCountProxy(CString message)
{
	INT32 iStart = 0;
	INT32 pos;
	INT32 count = 0;

	do {
		pos = message.Find(L"\r\n", iStart);
		count++;
		if(pos >= 0)
			iStart = pos + 1;
	} while(pos >= 0);

	//TRACE("LINE COUNT : %d\n", count);

	return count;
}

void CLogEdit::InsertLines(CString Line,BOOL st)
{
	CString wndtext;
	//GetWindowText ( wndtext );
	wndtext = m_strLogs;
	int text_length = wndtext.GetLength() ;
	if( text_length <=1) 
	{
		if(!st)
			wndtext = wndtext + Line ;
	else
		wndtext = wndtext + PromptStr + Line ;
	}
	else
	{
		if(!st)
			wndtext = wndtext +"\r\n"+  Line ;
	else
		wndtext = wndtext + "\r\n"+PromptStr + Line ;
	}

	int cnt = GetLineCountProxy(wndtext);
	if(cnt > m_maxLineCount)
		RemoveTopLine(wndtext);
	m_strLogs = wndtext;
	CWnd *pParent = AfxGetMainWnd();
	if(pParent)
		pParent->PostMessage(WM_TBD_UPDATE_LOG, 0, 0);
//	SetWindowText ( wndtext );
		
//	LineScroll ( GetLineCount(), 0x0 );
//	UpdateCaretPos();

}

void CLogEdit::UpdateLogsToEdit()
{
	SetWindowText(m_strLogs);
	LineScroll (GetLineCount(), 0x0);
}

void CLogEdit::UpdateCaretPos()
{
	for ( int i = 0 ; i < GetLineCount() ; i++ )
		SendMessage( WM_KEYDOWN, VK_DOWN , 0x0 ) ;
	for ( int i = 0 ; i < LineLength( GetLineCount() - 1 ) ; i++ )
		SendMessage( WM_KEYDOWN, VK_RIGHT , 0x0 ) ;
}



void CLogEdit::OnSetFocus(CWnd* pOldWnd) 
{
	CEdit::OnSetFocus(pOldWnd);
	
	if(flag==1)
	{
		flag = 0;
		//InsertLines("Log Edit Demo Version 1.0  Feb-2008",FALSE);
	}	
	UpdateCaretPos();
}


void CLogEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	UpdateCaretPos();
	return;
//	CEdit::OnChar(nChar, nRepCnt, nFlags);
}


void CLogEdit::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	UpdateCaretPos();	
}

void CLogEdit::OnLButtonDown(UINT nFlags, CPoint point) 
{
	UpdateCaretPos();	

}
