// EditFtpFolder.cpp : implementation file
//

#include "stdafx.h"
#include "EditFtpFolder.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEditFtpFolder

CEditFtpFolder::CEditFtpFolder()
{
	m_bNeedUpdate = FALSE;
}

CEditFtpFolder::~CEditFtpFolder()
{
}


BEGIN_MESSAGE_MAP(CEditFtpFolder, CEdit)
	//{{AFX_MSG_MAP(CEditFtpFolder)
	ON_WM_CHAR()
	ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditFtpFolder message handlers

void CEditFtpFolder::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	/*8-backspace  3-ctrl+c  22-ctrl+v  24-ctrl+x  26-ctrl+z*/
	if (nChar != 8 && nChar != 3 /*&& nChar != 22 */&& nChar != 24 && nChar != 26)
	{
		//if (nChar == '&' || nChar == '_'  || nChar == '%' || nChar == '\"' || nChar == '\\')
		if (nChar == '\\' || nChar == '/'  || nChar == ':' || nChar == '*' || nChar == '?' || nChar == '\"' || nChar == '<' || nChar == '>' || nChar == '|')
			return;
	}
	m_bNeedUpdate = TRUE;
	if(nChar == 8)
		m_bNeedUpdate = FALSE;
	CEdit::OnChar(nChar, nRepCnt, nFlags);
}

void CEditFtpFolder::OnUpdate()
{	
	// TODO: Add your control notification handler code here
	if(m_bNeedUpdate)
	{
		m_bNeedUpdate = FALSE;
		CString strText = _T("");
		GetWindowText(strText);
		strText.TrimLeft();
		strText.TrimRight();

		TCHAR tzText[MAX_MAC_LENGTH] = {0};
		int nIndex = 0;
		for (int i = 0; i < strText.GetLength() && nIndex < MAX_MAC_LENGTH; i++)
		{
			TCHAR nChar = strText.GetAt(i);
			if (nChar == '\\' || nChar == '/'  || nChar == ':' || nChar == '*' || nChar == '?' || nChar == '\"' || nChar == '<' || nChar == '>' || nChar == '|')
			{
				continue;
			}
			tzText[nIndex++] = nChar;
		}
		tzText[nIndex] = _T('\0');
		SetWindowText(tzText);
		SendMessage(EM_SETSEL, nIndex, nIndex); // 光标设置到最后(如果在中段插入字符则会显得不好用)
	}
}

void CEditFtpFolder::SetWindowText(LPCTSTR lpszString, BOOL bNeedUpdate/* = FALSE*/)
{
	m_bNeedUpdate = bNeedUpdate;
	CEdit::SetWindowText(lpszString);
}