void CBCGMaskEdit::OnLButtonUp(UINT nFlags, CPoint point) { if (m_bSelectByGroup) { // ----------------- // Calc group bounds // ----------------- int nGroupStart, nGroupEnd; CEdit::GetSel(nGroupStart, nGroupEnd); GetGroupBounds(nGroupStart, nGroupEnd, nGroupStart, TRUE); if (nGroupStart == -1) { CEdit::GetSel(nGroupStart, nGroupEnd); GetGroupBounds(nGroupStart, nGroupEnd, nGroupStart, FALSE); } // ----------------- // Correct selection // ----------------- int nStart, nEnd; CEdit::GetSel(nStart, nEnd); int nNewStart = max(nStart, nGroupStart); int nNewEnd = min(nEnd, nGroupEnd); // additional nNewStart = min(nNewStart, nGroupEnd); nNewEnd = max(nNewEnd ,nGroupStart); if ((nNewEnd != nEnd) || (nNewStart != nStart)) { CEdit::SetSel(nNewStart, nNewEnd); } } CEdit::OnLButtonUp(nFlags, point); }
LRESULT CBCGPMaskEdit::OnClear(WPARAM, LPARAM) { m_bPasteProcessing = TRUE; int nBeginOld, nEndOld; CBCGPEdit::GetSel(nBeginOld, nEndOld); Default (); CString strNew; CWnd::GetWindowText(strNew); if (!SetValue(strNew, TRUE)) { MessageBeep((UINT)-1); } CWnd::SetWindowText(m_str); if (m_bSelectByGroup) { GetGroupBounds(nBeginOld, nEndOld, nBeginOld, TRUE); } CBCGPEdit::SetSel(nBeginOld, nBeginOld); m_bPasteProcessing = FALSE; return 0; }
void CBCGMaskEdit::OnSetFocusR() { if (m_bSelectByGroup) { int nBegin, nEnd; GetGroupBounds(nBegin, nEnd, 0, TRUE); if (nBegin == -1) { } CEdit::SetSel(nBegin, nEnd); } else { CEdit::SetSel(0, -1); } }
void CBCGMaskEdit::OnUpdateR() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CEdit::OnInitDialog() // function to send the EM_SETEVENTMASK message to the control // with the ENM_UPDATE flag ORed into the lParam mask. // TODO: Add your control notification handler code here if (!m_bUpdateInProgress) { m_bUpdateInProgress = TRUE; CString str; CWnd::GetWindowText(str); if (m_str != str && !m_bPasteProcessing) { // work incorrect for Paste when m_bSetMaskedCharsOnly==TRUE if (!SetValue(str, TRUE/*!m_bSetMaskedCharsOnly*/)) { Undo(); EmptyUndoBuffer(); MessageBeep((UINT)-1); CWnd::GetWindowText(str); } if (str != m_str) // str may be empty { CWnd::SetWindowText(m_str); if (m_bSelectByGroup) { int nBegin, nEnd; GetGroupBounds(nBegin, nEnd, 0, TRUE); CEdit::SetSel(nBegin, nEnd); } else { CEdit::SetSel(0, -1); } } } m_bUpdateInProgress = FALSE; } }
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); } } }
void CBCGMaskEdit::OnCharPrintchar(UINT nChar, UINT nRepCnt, UINT nFlags) { ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty()); ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength()); ASSERT(_istprint((BCG_TCHAR) nChar) != FALSE); // ----------------------------------------------- // Processing ES_UPPERCASE and ES_LOWERCASE styles // ----------------------------------------------- DWORD dwStyle = GetStyle (); if (dwStyle & ES_UPPERCASE) { nChar = _totupper((BCG_TCHAR) nChar); } else if (dwStyle & ES_LOWERCASE) { nChar = _totlower((BCG_TCHAR) nChar); } 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; } TCHAR chChar = (TCHAR) nChar; // ----------------- // No selected chars // ----------------- if (nStartPos == nEndPos) { // Use m_strMask if (!m_strMask.IsEmpty()) { // ---------------------------------------------- // Automaticaly move the cursor to the next group // ---------------------------------------------- if (nEndPos==nGroupEnd || // at the end of group nStartPos < nGroupStart || nStartPos > nGroupEnd) // not in the middle of a group { // no space for new char if (nEndPos >= m_str.GetLength()-1) { MessageBeep((UINT)-1); return; } // can search next group else if (nEndPos < m_str.GetLength()-1) { GetGroupBounds(nGroupStart, nGroupEnd, nEndPos+1, TRUE); } // if next group was found if ((nGroupStart != -1) && (nGroupStart > nEndPos)) { CEdit::SetSel(nGroupStart, nGroupStart); nStartPos = nGroupStart; nEndPos = nGroupStart; } // no more groups else { MessageBeep((UINT)-1); return; } } // ---------------------- // Check char in position // ---------------------- if (!CheckChar(chChar, nStartPos)) { MessageBeep((UINT)-1); return; } // --------------------------------- // Replace char in Editbox and m_str // --------------------------------- CEdit::SetSel(nStartPos, nEndPos+1); CEdit::ReplaceSel(CString(chChar), TRUE); m_str.SetAt(nEndPos, chChar); CEdit::SetSel(nEndPos+1, nEndPos+1); // ---------------------------------------------- // Automaticaly move the cursor to the next group // ---------------------------------------------- CEdit::GetSel(nStartPos, nEndPos); if (nEndPos==nGroupEnd) // at the end of group { // can search next group if (nEndPos < m_str.GetLength()-1) { GetGroupBounds(nGroupStart, nGroupEnd, nEndPos+1, TRUE); } // if next group was found if ((nGroupStart != -1) && (nGroupStart > nEndPos)) { CEdit::SetSel(nGroupStart, nGroupStart); nStartPos = nGroupStart; nEndPos = nGroupStart; } } } // Don't use m_strMask else { // ---------------------- // Check char in position // ---------------------- if (!CheckChar(chChar, nStartPos)) { MessageBeep((UINT)-1); return; } // Don't use m_chMask CEdit::OnChar(nChar, nRepCnt, nFlags); } } // ------------------------------- // Have one or more chars selected // ------------------------------- else { // ---------------------- // Check char in position // ---------------------- if (!CheckChar(chChar, nStartPos)) { MessageBeep((UINT)-1); return; } // ---------------------------------- // Replace chars in Editbox and m_str // ---------------------------------- 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 + 1); strReplace += CString(m_chMaskInputTemplate, nRange - 1); ASSERT(strReplace.GetLength() > 0); strReplace.SetAt(0, chChar); } // ------------------------------------------- // 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]); } CEdit::SetSel(nStartPos+1, nStartPos+1); } else { // Don't use m_chMaskInputTemplate CEdit::OnChar(nChar, nRepCnt, nFlags); } } }
void CBCGMaskEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // -------------------------------------- // Make sure the mask has entry positions // -------------------------------------- int nGroupStart, nGroupEnd; GetGroupBounds(nGroupStart, nGroupEnd); if (nGroupStart == -1) { // mask has no entry positions MessageBeep((UINT)-1); return; } switch (nChar) { case VK_END: { // ---------------------- // Calc last group bounds // ---------------------- int nGroupStart, nGroupEnd; CEdit::GetSel(nGroupStart, nGroupEnd); ASSERT(nGroupStart != -1); GetGroupBounds(nGroupStart, nGroupEnd, nGroupEnd, TRUE); if (nGroupStart == -1) { GetGroupBounds(nGroupStart, nGroupEnd, m_str.GetLength(), FALSE); } ASSERT(nGroupStart != -1); CEdit::SetSel(nGroupEnd, nGroupEnd); return; } case VK_HOME: { // ----------------------- // Calc first group bounds // ----------------------- int nGroupStart, nGroupEnd; CEdit::GetSel(nGroupStart, nGroupEnd); ASSERT(nGroupStart != -1); GetGroupBounds(nGroupStart, nGroupEnd, nGroupStart, FALSE); if (nGroupStart == -1) { GetGroupBounds(nGroupStart, nGroupEnd, 0, TRUE); } ASSERT(nGroupStart != -1); CEdit::SetSel(nGroupStart, nGroupStart); return; } case VK_UP: case VK_LEFT: { // ----------------------- // Calc first group bounds // ----------------------- int nGroupStart, nGroupEnd; CEdit::GetSel(nGroupStart, nGroupEnd); ASSERT(nGroupStart != -1); GetGroupBounds(nGroupStart, nGroupEnd, nGroupStart, FALSE); if (nGroupStart == -1) { GetGroupBounds(nGroupStart, nGroupEnd, 0, TRUE); } ASSERT(nGroupStart != -1); if (::GetKeyState(VK_SHIFT)&0x80) { int nStart, nEnd; CEdit::GetSel(nStart, nEnd); if (m_bSelectByGroup) { int nNewStart = max(nStart-1, nGroupStart); // additional nNewStart = min(nNewStart, nGroupEnd); CEdit::SetSel(nNewStart, nEnd); } else { CEdit::SetSel(nStart-1, nEnd); } return; } else if (::GetKeyState(VK_CONTROL)&0x80) { // move to the previous group int nStart, nEnd; CEdit::GetSel(nStart, nEnd); ASSERT(nStart != -1); if (nStart > 1) // can search previous group { GetGroupBounds(nGroupStart, nGroupEnd, nStart-1, FALSE); } if ((nGroupStart != -1) && // if previous group was found (nGroupStart != nStart || nGroupEnd != nEnd)) // and it's not the same { CEdit::SetSel(nGroupStart, nGroupEnd); } else // no more groups { MessageBeep((UINT)-1); } return; } else { int nStart, nEnd; CEdit::GetSel(nStart, nEnd); // move to the previous group if ((nStart==nEnd) && (nStart==nGroupStart)) { if (nStart > 1) // can search previous group { GetGroupBounds(nGroupStart, nGroupEnd, nStart-1, FALSE); } if ((nGroupStart != -1) && (nGroupEnd < nStart)) // if previous group was found { CEdit::SetSel(nGroupEnd, nGroupEnd); } else // no more groups { MessageBeep((UINT)-1); } } else { int nNewStart = max(nStart-1, nGroupStart); // additional nNewStart = min(nNewStart, nGroupEnd); CEdit::SetSel(nNewStart, nNewStart); } return; } } case VK_DOWN: case VK_RIGHT: { // ---------------------- // Calc last group bounds // ---------------------- int nGroupStart, nGroupEnd; CEdit::GetSel(nGroupStart, nGroupEnd); ASSERT(nGroupStart != -1); GetGroupBounds(nGroupStart, nGroupEnd, nGroupEnd, TRUE); if (nGroupStart == -1) { GetGroupBounds(nGroupStart, nGroupEnd, m_str.GetLength(), FALSE); } ASSERT(nGroupStart != -1); if (::GetKeyState(VK_SHIFT)&0x80) { int nStart, nEnd; CEdit::GetSel(nStart, nEnd); if (m_bSelectByGroup) { int nNewEnd = min(nEnd+1, nGroupEnd); // additional nNewEnd = max(nNewEnd, nGroupStart); CEdit::SetSel(nStart, nNewEnd); } else { CEdit::SetSel(nStart, nEnd+1); } return; } else if (::GetKeyState(VK_CONTROL)&0x80) { // move to the next group int nStart, nEnd; CEdit::GetSel(nStart, nEnd); ASSERT(nStart != -1); if (nEnd < m_str.GetLength()-1) // can search next group { GetGroupBounds(nGroupStart, nGroupEnd, nEnd+1, TRUE); } if ((nGroupStart != -1) && // if previous group was found (nGroupStart != nStart || nGroupEnd != nEnd)) // and it's not the same { CEdit::SetSel(nGroupStart, nGroupEnd); } else // no more groups { MessageBeep((UINT)-1); } return; } else { int nStart, nEnd; CEdit::GetSel(nStart, nEnd); // move to the next group if ((nStart==nEnd) && (nEnd==nGroupEnd)) { if (nEnd < m_str.GetLength()-1) // can search next group { GetGroupBounds(nGroupStart, nGroupEnd, nStart+1, TRUE); } if ((nGroupStart != -1) && (nGroupStart > nEnd)) // if next group was found { CEdit::SetSel(nGroupStart, nGroupStart); } else // no more groups { MessageBeep((UINT)-1); } } else { int nNewEnd = min(nEnd+1, nGroupEnd); // additional nNewEnd = max(nNewEnd, nGroupStart); CEdit::SetSel(nNewEnd, nNewEnd); } return; } } case VK_BACK: { // Special processing OnCharBackspace(nChar, nRepCnt, nFlags); return; } case VK_DELETE: { if (::GetKeyState(VK_SHIFT)&0x80) { break; } // Special processing OnCharDelete(nChar, nRepCnt, nFlags); return; } case VK_INSERT: { if ((::GetKeyState(VK_CONTROL)&0x80) || (::GetKeyState(VK_SHIFT)&0x80)) { break; } if (!m_strMask.IsEmpty()) { return; } break; } } CEdit::OnKeyDown(nChar, nRepCnt, nFlags); }
LRESULT CBCGMaskEdit::OnPaste (WPARAM, LPARAM) { m_bPasteProcessing = TRUE; int nBeginOld, nEndOld; CEdit::GetSel(nBeginOld, nEndOld); Default (); int nBegin, nEnd; CEdit::GetSel(nBegin, nEnd); nEnd = max (nBegin, nEnd); CString str; CWnd::GetWindowText(str); CString strPaste = str.Mid (nBeginOld, nEnd - nBeginOld); CString strOld; int nLeft = nBeginOld; if (m_bSetMaskedCharsOnly) { strOld = GetMaskedValue(); if (!m_strMask.IsEmpty ()) { for (int iChar = 0; iChar < m_strInputTemplate.GetLength() && iChar < nBeginOld; iChar++) { if (m_strInputTemplate[iChar] != _T('_')) { nLeft--; } } } } else { strOld = GetValue(); } CString strNew = strOld.Left (nLeft) + strPaste; BOOL bOverwrite = !m_strMask.IsEmpty (); int nRight = nLeft + (bOverwrite ? strPaste.GetLength () : 0); if (nRight < strOld.GetLength ()) { strNew += strOld.Mid (nRight); } if (!SetValue(strNew, !m_bSetMaskedCharsOnly)) { MessageBeep((UINT)-1); } CWnd::SetWindowText(m_str); if (m_bSelectByGroup) { GetGroupBounds(nBeginOld, nEndOld, nBeginOld, TRUE); } CEdit::SetSel(nBeginOld, nBeginOld); m_bPasteProcessing = FALSE; return 0L; }