BOOL CBCGMaskEdit::CheckChar(TCHAR chChar, int nPos) // returns TRUE if the symbol is valid { ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty()); ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength()); ASSERT(_istprint((BCG_TCHAR) (UINT) chChar) != FALSE); ASSERT(nPos >= 0); // -------------- // Don't use mask // -------------- if (m_strMask.IsEmpty()) { return IsValidChar(chChar); } else { ASSERT(nPos < m_strMask.GetLength()); } // -------- // Use mask // -------- ASSERT(m_str.IsEmpty() == m_strMask.IsEmpty()); ASSERT(m_str.GetLength() == m_strMask.GetLength()); if (m_strInputTemplate[nPos] == _T('_')) { return IsValidChar(chChar) && IsMaskedChar(chChar, m_strMask[nPos]); } else { return FALSE; } }
BOOL CBCGPMaskEdit::CheckChar(TCHAR chChar, int nPos) // returns TRUE if the symbol is valid { ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty()); ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength()); ASSERT(_istprint(chChar) != FALSE); ASSERT(nPos >= 0); // -------------- // Don't use mask // -------------- if (m_strMask.IsEmpty()) { // Use valid string characters if (!m_strValid.IsEmpty()) { return (m_strValid.Find(chChar) != -1); } // Don't use valid string characters else { return TRUE; } } else { ASSERT(nPos < m_strMask.GetLength()); } // -------- // Use mask // -------- ASSERT(m_str.GetLength() == m_strMask.GetLength()); if (m_strInputTemplate[nPos] == _T('_')) { BOOL bIsMaskedChar = IsMaskedChar(chChar, m_strMask[nPos]); // Use valid string characters if (!m_strValid.IsEmpty()) { return bIsMaskedChar && (m_strValid.Find(chChar) != -1); } // Don't use valid string characters else { return bIsMaskedChar; } } else { return FALSE; } }
const CString CBCGMaskEdit::GetMaskedValue(BOOL bWithSpaces) const { ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty()); ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength()); // -------------- // Don't use mask // -------------- if (m_strMask.IsEmpty()) { return m_str; } // -------- // Use mask // -------- ASSERT(m_str.IsEmpty() == m_strMask.IsEmpty()); ASSERT(m_str.GetLength() == m_strMask.GetLength()); CString strResult; for (int iChar=0; iChar < m_strInputTemplate.GetLength(); iChar++) { if (m_strInputTemplate[iChar] == _T('_')) { TCHAR ch = m_str[iChar]; if (ch == m_chMaskInputTemplate) { if (bWithSpaces) { strResult += ch; } } else { ASSERT(IsValidChar(ch)); ASSERT(IsMaskedChar(ch, m_strMask[iChar])); strResult += ch; } } } return strResult; }
void CBCGMaskEdit::OnCharBackspace(UINT nChar, UINT nRepCnt, UINT nFlags) { ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty()); ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength()); int nStartPos, nEndPos; CEdit::GetSel(nStartPos, nEndPos); ASSERT(nStartPos>=0); ASSERT(nEndPos>=0); ASSERT(nEndPos>=nStartPos); // ----------------- // Calc group bounds // ----------------- int nGroupStart, nGroupEnd; GetGroupBounds(nGroupStart, nGroupEnd, nStartPos); // ------------ // Out of range // ------------ if ((nStartPos<0) && (nEndPos > m_str.GetLength()) || (nStartPos < nGroupStart) || (nStartPos > nGroupEnd) || (nEndPos < nGroupStart) || (nEndPos > nGroupEnd)) { MessageBeep((UINT)-1); CEdit::SetSel(nGroupStart, nGroupEnd); return; } // ----------------- // No selected chars // ----------------- if (nStartPos == nEndPos) { // Use m_strMask if (!m_strMask.IsEmpty()) { // -------------------------------------------------- // Automaticaly move the cursor to the previous group // -------------------------------------------------- if (nEndPos==nGroupStart) // at the start of group { // can search previous group if (nEndPos > 1) { GetGroupBounds(nGroupStart, nGroupEnd, nEndPos-1, FALSE); } // if previous group was found if ((nGroupStart != -1) && (nGroupEnd < nEndPos)) { CEdit::SetSel(nGroupEnd, nGroupEnd); return; } // no more group else { MessageBeep((UINT)-1); return; } } // --------------------------------------------------- // Calc the number of literals with the same mask char // --------------------------------------------------- ASSERT(nStartPos > 0); ASSERT(nEndPos > 0); ASSERT(nGroupEnd <= m_strInputTemplate.GetLength()); int nSameMaskCharsNum = 1; int nIndex = nStartPos-1; // an index of the char to delete TCHAR chMaskChar = m_strMask[nIndex]; BOOL bScanMore = TRUE; while (bScanMore && (nIndex + nSameMaskCharsNum < nGroupEnd)) { if (m_strMask[nIndex + nSameMaskCharsNum] == chMaskChar) { nSameMaskCharsNum++; } else { bScanMore = FALSE; } } // --------------------------------- // Validate new string (dispensable) // --------------------------------- int i = nIndex; for (; (i + nSameMaskCharsNum < nGroupEnd); i++) { if (m_str[i] != m_chMaskInputTemplate) // allow m_chMaskInputTemplate { if (!IsValidChar(m_str[i]) || !IsMaskedChar(m_str[i], m_strMask[i])) { MessageBeep((UINT)-1); return; } } } // ----------------------- // Form the shifted string // ----------------------- ASSERT(nIndex >= nGroupStart); ASSERT(nIndex + nSameMaskCharsNum <= nGroupEnd); CString strReplace = m_str.Mid(nIndex, nSameMaskCharsNum); if (nSameMaskCharsNum > 0) { strReplace = strReplace.Right(nSameMaskCharsNum - 1); strReplace += m_chMaskInputTemplate; } // ------------------------------------------- // Replace the content with the shifted string // ------------------------------------------- CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum); CEdit::ReplaceSel(strReplace, TRUE); CEdit::SetSel(nIndex, nIndex); for(i=0; i < strReplace.GetLength(); i++) { m_str.SetAt(nIndex+i, strReplace[i]); } } else // Don't use m_chMaskInputTemplate - delete symbol { CWnd::OnKeyDown(nChar, nRepCnt, nFlags); } } // -------------------------------- // Have one or more chars selected // -------------------------------- else { if (!m_strInputTemplate.IsEmpty()) // Use m_strInputTemplate { // --------------------------------------------------- // Calc the number of literals with the same mask char // --------------------------------------------------- ASSERT(nStartPos >= 0); ASSERT(nEndPos > 0); ASSERT(nStartPos <= m_strInputTemplate.GetLength()); int nSameMaskCharsNum = 1; int nIndex = nStartPos; // an index of the first selected char TCHAR chMaskChar = m_strMask[nIndex]; BOOL bScanMore = TRUE; while (bScanMore && (nIndex + nSameMaskCharsNum < nGroupEnd)) { if (m_strMask[nIndex + nSameMaskCharsNum] == chMaskChar) { nSameMaskCharsNum++; } else { bScanMore = FALSE; } } // Make sure the selection has the same mask char if (nEndPos - nStartPos > nSameMaskCharsNum) { MessageBeep((UINT)-1); CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum); return; } // ------------------------------- // Form the shifted replace string // ------------------------------- ASSERT(nIndex >= nGroupStart); ASSERT(nIndex + nSameMaskCharsNum <= nGroupEnd); CString strReplace = m_str.Mid(nIndex, nSameMaskCharsNum); if (nSameMaskCharsNum > 0) { ASSERT(nStartPos <= m_strInputTemplate.GetLength()); ASSERT(nEndPos <= m_strInputTemplate.GetLength()); int nRange = nEndPos - nStartPos; ASSERT(nRange>0); strReplace = strReplace.Right(nSameMaskCharsNum - nRange); strReplace += CString(m_chMaskInputTemplate, nRange); } // ------------------------------------------- // Replace the content with the shifted string // ------------------------------------------- CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum); CEdit::ReplaceSel(strReplace, TRUE); CEdit::SetSel(nIndex, nIndex); for(int i=0; i < strReplace.GetLength(); i++) { m_str.SetAt(nIndex+i, strReplace[i]); } } else { // Don't use m_chMaskInputTemplate - delete symbols CWnd::OnKeyDown(nChar, nRepCnt, nFlags); } } }
BOOL CBCGMaskEdit::SetValue(LPCTSTR lpszString, BOOL bWithDelimiters) { ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty()); ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength()); ASSERT(lpszString != NULL); // ------------------------------------------------ // Make sure the string is not longer than the mask // ------------------------------------------------ CString strSource = lpszString; if (!m_strMask.IsEmpty()) { if (bWithDelimiters) { if (strSource.GetLength() > m_strMask.GetLength()) { return FALSE; } } else { // Count _T('_') in m_strInputTemplate int nCount = 0; for (int i = 0; i < m_strInputTemplate.GetLength(); i++) { if (m_strInputTemplate[i] == _T('_')) { nCount++; } } if (strSource.GetLength() > nCount) { return FALSE; } } } // ----------------------------------- // Use mask, validate against the mask // ----------------------------------- if (!m_strMask.IsEmpty()) { ASSERT(m_str.IsEmpty() == m_strMask.IsEmpty()); ASSERT(m_str.GetLength() == m_strMask.GetLength()); CString strResult = m_strInputTemplate; // Replace '_' with default char for (int i=0; i<strResult.GetLength(); i++) { if (m_strInputTemplate[i] == _T('_')) { strResult.SetAt(i, m_chMaskInputTemplate); } } int iSrcChar = 0; int iDstChar = 0; while ((iSrcChar<strSource.GetLength()) && (iDstChar<m_strInputTemplate.GetLength())) { // iDstChar - character entry position ("_" char) if (m_strInputTemplate[iDstChar] == _T('_')) { TCHAR chChar = strSource[iSrcChar]; if (chChar != m_chMaskInputTemplate) // allow m_chMaskInputTemplate { if (!IsValidChar(chChar) || !IsMaskedChar(chChar, m_strMask[iDstChar])) { return FALSE; } } strResult.SetAt(iDstChar, chChar); iSrcChar++; iDstChar++; } // iDstChar - delimeter else { if (bWithDelimiters) { if (m_strInputTemplate[iDstChar] != strSource[iSrcChar]) { return FALSE; } iSrcChar++; iDstChar++; } else { iDstChar++; } } } m_str = strResult; } // -------------- // Don't use mask // -------------- else { m_str = strSource; } return TRUE; }