// EditEnter.cpp : implementation file // #include "stdafx.h" #include "SerialTester.h" #include "MainDlg.h" #include "EditEnter.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CEditEnter CEditEnter::CEditEnter() { } CEditEnter::~CEditEnter() { } BEGIN_MESSAGE_MAP(CEditEnter, CEdit) //{{AFX_MSG_MAP(CEditEnter) ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus) ON_WM_CHAR() ON_WM_GETDLGCODE() // ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CEditEnter message handlers void CEditEnter::OnKillfocus() { m_uLines = 0; } void CEditEnter::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { WORD line; line = HIWORD(this->CharFromPos(this->GetCaretPos())); switch(nChar) { case VK_RETURN: GetParent()->SendMessage(UWM_EDIT_COMPLETE, GetDlgCtrlID(), (LPARAM)this); if(line == this->GetLineCount() - 1){ CEdit::OnChar(nChar, nRepCnt, nFlags); } break; default: CEdit::OnChar(nChar, nRepCnt, nFlags); break; } } UINT CEditEnter::OnGetDlgCode() { return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS; } void CEditEnter::SetLines() { m_uLines = this->GetLineCount(); } int CEditEnter::GetLines() { this->SetLines(); return m_uLines; } //------------------------------------------------------------------------ // // Description // ----------- // This function replaces the Windows function CharFromPos. // CharFromPos does not work well for shift arrowing over very narrow // text. // // Returns // ------- // See CharFromPos documentation. //------------------------------------------------------------------------ int CEditEnter::CharFromPosFix(CPoint pt) { CString text; LPCTSTR pText; int lineIndex = 0; // Get the text, a pointer to the line of text and the length. GetWindowText(text); int length = text.GetLength(); pText = (LPCTSTR)text; if (GetStyle() & ES_MULTILINE) { lineIndex = LineIndex(); pText += (lineIndex * sizeof(TCHAR)); length -= lineIndex; } // Select the font in. CDC *pCDC; pCDC = GetDC(); CFont *pOldFont = pCDC->SelectObject(GetFont()); // Find the correct position in the text using a binary search. WORD low = 0; WORD high = length; WORD mid; SIZE size; while (low < (high - 1)) { mid = (low + high) / 2; GetTextExtentPoint32(*pCDC, pText, mid, &size); if (pt.x <= size.cx) high = mid; else low = mid; } // Restore stuff and compute return value. pCDC->SelectObject(pOldFont); ReleaseDC(pCDC); --high; // Go to 0 based; high += lineIndex; // Add in previous lines. return (high + (LineFromChar(high) << 16)); } CString CEditEnter::GetTextAt(int line_index) { CString ret; long buf_size = LineLength(LineIndex(line_index))+1; LPTSTR line_buf = ret.GetBuffer(buf_size); int cc = GetLine((line_index==-1) ? (LineFromChar()) : (line_index), line_buf, buf_size); ASSERT(cc>=0 && cc