//============================================================================== // File: PopupEdit.cpp // // Description: A subclassed edit control that works as a popup edit // similar to the one used by the ListView control // // Revisions: 12/24/1999 - created // //============================================================================== // Copyright(C) 1999, Tomas Restrepo. All rights reserved //============================================================================== #include "stdafx.h" #include "PopupEdit.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // message use to notify the window of the text received // a pointer to the string is in lParam // const UINT PopupEdit::PE_TEXTIN = ::RegisterWindowMessage ( _T("PE_TEXTIN_{04247C64-0BBE-424d-8459-FC2A1D7039AE}") ); const UINT PopupEdit::PeExtraSpace = 20; // extra space left blank on the control ///////////////////////////////////////////////////////////////////////////// // PopupEdit PopupEdit::PopupEdit() : m_OriginalRect ( 0, 0, 0, 0 ) { } PopupEdit::~PopupEdit() { } BEGIN_MESSAGE_MAP(PopupEdit, CEdit) //{{AFX_MSG_MAP(PopupEdit) ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus) ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // PopupEdit message handlers BOOL PopupEdit::Create ( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID ) { // // adjust the styles: We need to trap return and // make sure we auto scroll horizontally. // we also need to make sure we start invisible // dwStyle |= WS_CHILD | ES_WANTRETURN | ES_AUTOHSCROLL; dwStyle &= ~WS_VISIBLE; return CEdit::Create ( dwStyle, rect, pParentWnd, nID ); } //========================================================== // PopupEdit::Show() // Description // Makes the edit control visible at the specified // location // // Parameters // rc - rectangle where the control is shown // InitialText - initial text on the control //========================================================== void PopupEdit::Show ( const CRect& rc, LPCTSTR InitialText /* = _T("") */ ) { m_OriginalRect = rc; SetWindowText ( InitialText ); SetSel ( 0, -1 ); MoveWindow ( &rc ); ResizeToFit ( ); ShowWindow ( SW_SHOW ); SetFocus ( ); } void PopupEdit::OnKillfocus ( ) { SetWindowText ( _T("") ); ShowWindow ( SW_HIDE ); } BOOL PopupEdit::PreTranslateMessage(MSG* pMsg) { if ( ::GetFocus ( ) == m_hWnd ) { if ( pMsg->message == WM_KEYDOWN ) { if ( (UINT)(pMsg->wParam) == VK_RETURN ) { CString Text; GetWindowText ( Text ); CWnd* pParentWnd = GetParent ( ); ASSERT ( pParentWnd != NULL ); pParentWnd->SendMessage ( PE_TEXTIN, 0, (LPARAM)(LPCTSTR)Text ); ShowWindow ( SW_HIDE ); return TRUE; } else if ( (UINT)(pMsg->wParam) == VK_ESCAPE ) { ShowWindow ( SW_HIDE ); return TRUE; } } } return CEdit::PreTranslateMessage(pMsg); } //========================================================== // PopupEdit::ResizeToFit() // Description // Resizes the control to fit the text contained //========================================================== void PopupEdit::ResizeToFit ( void ) { CRect ParentRect(0,0,0,0); CRect WndRect(0,0,0,0); CRect ClientRect(0,0,0,0); CWnd* pParentWnd = GetParent ( ); ASSERT ( pParentWnd != NULL ); pParentWnd->GetClientRect ( &ParentRect ); GetWindowRect ( &WndRect ); pParentWnd->ScreenToClient ( &WndRect ); GetClientRect ( &ClientRect ); ClientToScreen ( &ClientRect ); pParentWnd->ScreenToClient ( &ClientRect ); // // save the border width, we need it // later to calculate the window rect // int BorderWidth = WndRect.Width ( ) - ClientRect.Width ( ); CDC* pDC = GetDC ( ); pParentWnd->SendMessage ( WM_CTLCOLOREDIT, (WPARAM)pDC->m_hAttribDC, (LPARAM)m_hWnd ); // // get the space needed to // print out the new string // CString Text; GetWindowText ( Text ); pDC->DrawText ( Text, &ClientRect, DT_CALCRECT | DT_EXPANDTABS | DT_LEFT | DT_NOCLIP | DT_NOPREFIX ); // // calculate the new window rect // if ( Text != _T("") ) { WndRect.right = WndRect.left + ClientRect.Width ( ) + BorderWidth + PeExtraSpace; WndRect.right = max ( WndRect.right, m_OriginalRect.right ); WndRect.right = min ( WndRect.right, ParentRect.right ); } else { // // if Text == "", then DrawText() doesn't modify the // rectangle, so we need to set it correctly // WndRect.right = m_OriginalRect.right; } MoveWindow ( &WndRect ); } //========================================================== // PopupEdit::OnUpdate() // Description // Resize the control if needed when the text changes // //========================================================== void PopupEdit::OnUpdate() { if ( GetStyle ( ) & PE_AUTOHEXPAND ) ResizeToFit ( ); }