// HEdit.cpp : implementation file
//

#include "stdafx.h"

#include "HEdit.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHEdit

CHEdit::CHEdit()
{
}

CHEdit::~CHEdit()
{
}


BEGIN_MESSAGE_MAP(CHEdit, CEdit)
	//{{AFX_MSG_MAP(CHEdit)
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHEdit message handlers

void CHEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CString strTmp;
	CString strTmpOld;
	if (m_blnInHexMode == 1)
	{
		this->GetWindowText(strTmp);
        if (nChar == 0x08)
		{
		    if ((strTmp.GetLength() % 3) == 0)
			{
               CEdit::OnChar(nChar, nRepCnt, nFlags);			   
			}
			CEdit::OnChar(nChar, nRepCnt, nFlags);
            CEdit::OnChar(nChar, nRepCnt, nFlags);			
		}
		else
		{	
			if (((nChar >= 0x30) && (nChar <= 0x39)) || 
  			    ((nChar >= 'a') && (nChar <= 'f')) ||
			    ((nChar >= 'A') && (nChar <= 'F')))
			{					    
				switch((strTmp.GetLength()) % 3)
				{
				case 0:
                     {
					 strTmp = strTmp + "0";			   			   
					 this->SetWindowText(strTmp);				 
					 this->SetSel(strTmp.GetLength() + 1,strTmp.GetLength() + 1);			
					 CEdit::OnChar(nChar, nRepCnt, nFlags);
					 }
					 break;
				case 2:			
					 {
					 strTmpOld = strTmp.Left(strTmp.GetLength() - 2) + strTmp.Right(1);				 
					 this->SetWindowText(strTmpOld);
					 this->SetSel(strTmpOld.GetLength(),strTmpOld.GetLength());
			
					 CEdit::OnChar(nChar, nRepCnt, nFlags);
					 this->GetWindowText(strTmp);     
					 strTmp = strTmp + " ";
					 this->SetWindowText(strTmp);     
					 this->SetSel(strTmp.GetLength(),strTmp.GetLength());			
					 }
					 break;			
				}     
			}                   
		}
	}
	else
	    CEdit::OnChar(nChar, nRepCnt, nFlags);
}

DWORD CHEdit::SetMode(int InHexMode)
{
    if (InHexMode == 1)
        m_blnInHexMode = 1;
	else
        m_blnInHexMode = 0;

	return ERROR_SUCCESS;
}
