C++
  1  /*******************************************************************************
  2  	Author						: Aravindan Premkumar
  3  	Unregistered Copyright 2003	: Aravindan Premkumar
  4  	All Rights Reserved
  5  	
  6  	This piece of code does not have any registered copyright and is free to be 
  7  	used as necessary. The user is free to modify as per the requirements. As a
  8  	fellow developer, all that I expect and request for is to be given the 
  9  	credit for intially developing this reusable code by not removing my name as 
 10  	the author.
 11  *******************************************************************************/
 12  
 13  #include "stdafx.h"
 14  #include "InPlaceEdit.h"
 15  
 16  #ifdef _DEBUG
 17  #define new DEBUG_NEW
 18  #undef THIS_FILE
 19  static char THIS_FILE[] = __FILE__;
 20  #endif
 21  
 22  #define CTRL_C	0x3
 23  #define CTRL_V	0x16
 24  #define CTRL_X	0x18
 25  
 26  /////////////////////////////////////////////////////////////////////////////
 27  // CInPlaceEdit
 28  
 29  CInPlaceEdit* CInPlaceEdit::m_pInPlaceEdit = NULL;  
 30  
 31  CInPlaceEdit::CInPlaceEdit()
 32  {
 33  	m_iRowIndex= -1;
 34  	m_iColumnIndex = -1;
 35  	m_bESC = FALSE;
 36  	m_strValidChars.Empty();
 37  }
 38  
 39  CInPlaceEdit::~CInPlaceEdit()
 40  {
 41  }
 42  
 43  BEGIN_MESSAGE_MAP(CInPlaceEdit, CEdit)
 44  	//{{AFX_MSG_MAP(CInPlaceEdit)
 45  	ON_WM_KILLFOCUS()
 46  	ON_WM_CHAR()
 47  	ON_MESSAGE(WM_PASTE, OnPaste)
 48  	ON_WM_CREATE()
 49  	//}}AFX_MSG_MAP
 50  END_MESSAGE_MAP()
 51  
 52  /////////////////////////////////////////////////////////////////////////////
 53  // CInPlaceEdit message handlers
 54  
 55  void CInPlaceEdit::OnPaste(WPARAM /*wParam*/, LPARAM /*lParam*/)
 56  {
 57  	if (m_strValidChars.IsEmpty())
 58  	{
 59  		return;	
 60  	}
 61  
 62      CString strFromClipboard;
 63  
 64  	// get the text from clipboard
 65  	if(OpenClipboard()) {
 66  		HANDLE l_hData = GetClipboardData(CF_TEXT);
 67  		if(NULL == l_hData) {
 68  			return;
 69  		}
 70  		
 71  		char *l_pBuffer = (char*)GlobalLock(l_hData);
 72  		if(NULL != l_pBuffer) {
 73  			strFromClipboard = l_pBuffer;
 74  		}
 75  
 76  		GlobalUnlock(l_hData);
 77  		CloseClipboard();
 78  	}
 79  
 80  	// Validate the characters before pasting 
 81  	for(int iCounter_ = 0; iCounter_ < strFromClipboard.GetLength(); iCounter_++)
 82  	{
 83  		if (-1 == m_strValidChars.Find(strFromClipboard.GetAt(iCounter_)))
 84  		{
 85  			return;
 86  		}
 87  	}
 88  		
 89  	//let the individual control handle other processing
 90  	CEdit::Default();	
 91  }
 92  
 93  void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd) 
 94  {
 95  	CEdit::OnKillFocus(pNewWnd);
 96  	
 97  	// TODO: Add your message handler code here
 98  
 99  	// Get the text in the edit ctrl
100  	CString strEdit;
101  	GetWindowText(strEdit);
102  
103  	// Send Notification to parent of edit ctrl
104  	LV_DISPINFO dispinfo;
105  	dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
106  	dispinfo.hdr.idFrom = GetDlgCtrlID();
107  	dispinfo.hdr.code = LVN_ENDLABELEDIT;
108  
109  	dispinfo.item.mask = LVIF_TEXT;
110  	dispinfo.item.iItem = m_iRowIndex;
111  	dispinfo.item.iSubItem = m_iColumnIndex;
112  	dispinfo.item.pszText = m_bESC ? LPTSTR((LPCTSTR)m_strWindowText) : LPTSTR((LPCTSTR)strEdit);
113  	dispinfo.item.cchTextMax = m_bESC ? m_strWindowText.GetLength() : strEdit.GetLength();
114  	
115  	GetParent()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
116  
117  	PostMessage(WM_CLOSE);
118  }
119  
120  void CInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
121  {
122  	// TODO: Add your message handler code here and/or call default
123  
124   	if ((m_strValidChars.IsEmpty()) || ((-1 != m_strValidChars.Find(static_cast<TCHAR> (nChar))) || 
125  		(nChar == VK_BACK) || (nChar == CTRL_C) || (nChar == CTRL_V) || (nChar == CTRL_X)))
126  	{
127  		CEdit::OnChar(nChar, nRepCnt, nFlags);
128  	}
129  	else
130  	{
131  		MessageBeep(MB_ICONEXCLAMATION);
132  		return;
133  	}
134  }
135  
136  BOOL CInPlaceEdit::PreTranslateMessage(MSG* pMsg) 
137  {
138  	// TODO: Add your specialized code here and/or call the base class
139  	if (WM_KEYDOWN == pMsg->message && (VK_ESCAPE == pMsg->wParam || VK_RETURN == pMsg->wParam))
140  	{
141  		if (VK_ESCAPE == pMsg->wParam)
142  		{
143  			m_bESC = TRUE;
144  		}
145  
146  		GetParent()->SetFocus();
147  		return TRUE;
148  	}
149  
150  	return CEdit::PreTranslateMessage(pMsg);
151  }
152  
153  int CInPlaceEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
154  {
155  	if (CEdit::OnCreate(lpCreateStruct) == -1)
156  		return -1;
157  	
158  	// TODO: Add your specialized creation code here
159  	// Set the proper font
160  	CFont* pFont = GetParent()->GetFont();
161  	SetFont(pFont);
162  
163  	ShowWindow(SW_SHOW);
164  	SetWindowText(m_strWindowText);
165  	SetSel(0, -1);
166  	SetFocus();
167  	
168  	  
169  	return 0;
170  }
171  
172  CInPlaceEdit* CInPlaceEdit::GetInstance()
173  {
174  	if(m_pInPlaceEdit == NULL)
175  	{
176  		m_pInPlaceEdit = new CInPlaceEdit;
177  	}
178  	return m_pInPlaceEdit;
179  }
180  
181  void CInPlaceEdit::DeleteInstance()
182  {
183  	delete m_pInPlaceEdit;
184  	m_pInPlaceEdit = NULL;
185  }
186  
187  BOOL CInPlaceEdit::ShowEditCtrl(DWORD dwStyle, const RECT &rCellRect, CWnd* pParentWnd, 
188  								UINT uiResourceID, int iRowIndex, int iColumnIndex,
189  								CString& strValidChars, CString& rstrCurSelection)
190  {
191  	m_iRowIndex = iRowIndex;
192  	m_iColumnIndex = iColumnIndex;
193  	m_strValidChars = strValidChars;
194  	m_strWindowText = rstrCurSelection;
195  	m_bESC = FALSE;
196  
197  	if (NULL == m_pInPlaceEdit->m_hWnd) 
198  	{
199  		return m_pInPlaceEdit->Create(dwStyle, rCellRect, pParentWnd, uiResourceID); 
200  	}	
201  
202  	return TRUE;
203  }