InPlaceEdit.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // InPlaceEdit.cpp : implementation file
  2. //
  3. // Adapted by Chris Maunder <cmaunder@mail.com>
  4. // Copyright (c) 1998-2001. All Rights Reserved.
  5. //
  6. // The code contained in this file is based on the original
  7. // CInPlaceEdit from http://www.codeguru.com/listview/edit_subitems.shtml
  8. //
  9. // This code may be used in compiled form in any way you desire. This
  10. // file may be redistributed unmodified by any means PROVIDING it is
  11. // not sold for profit without the authors written consent, and
  12. // providing that this notice and the authors name and all copyright
  13. // notices remains intact.
  14. //
  15. // An email letting me know how you are using it would be nice as well.
  16. //
  17. // This file is provided "as is" with no expressed or implied warranty.
  18. // The author accepts no liability for any damage/loss of business that
  19. // this product may cause.
  20. //
  21. // For use with CGridCtrl v2.10+
  22. //
  23. // History:
  24. // 10 May 1998 Uses GVN_ notifications instead of LVN_,
  25. // Sends notification messages to the parent,
  26. // instead of the parent's parent.
  27. // 15 May 1998 There was a problem when editing with the in-place editor,
  28. // there arises a general protection fault in user.exe, with a
  29. // few qualifications:
  30. // (1) This only happens with owner-drawn buttons;
  31. // (2) This only happens in Win95
  32. // (3) This only happens if the handler for the button does not
  33. // create a new window (even an AfxMessageBox will avoid the
  34. // crash)
  35. // (4) This will not happen if Spy++ is running.
  36. // PreTranslateMessage was added to route messages correctly.
  37. // (Matt Weagle found and fixed this problem)
  38. // 26 Jul 1998 Removed the ES_MULTILINE style - that fixed a few probs!
  39. // 6 Aug 1998 Added nID to the constructor param list
  40. // 6 Sep 1998 Space no longer clears selection when starting edit (Franco Bez)
  41. // 10 Apr 1999 Enter, Tab and Esc key prob fixed (Koay Kah Hoe)
  42. // Workaround for bizzare "shrinking window" problem in CE
  43. //
  44. /////////////////////////////////////////////////////////////////////////////
  45. #include "stdafx.h"
  46. #include "TCHAR.h"
  47. #include "InPlaceEdit.h"
  48. #include "GridCtrl.h"
  49. #ifdef _DEBUG
  50. #define new DEBUG_NEW
  51. #undef THIS_FILE
  52. static char THIS_FILE[] = __FILE__;
  53. #endif
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CInPlaceEdit
  56. CInPlaceEdit::CInPlaceEdit(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID,
  57. int nRow, int nColumn, CString sInitText,
  58. UINT nFirstChar)
  59. {
  60. m_sInitText = sInitText;
  61. m_nRow = nRow;
  62. m_nColumn = nColumn;
  63. m_nLastChar = 0;
  64. m_bExitOnArrows = (nFirstChar != VK_LBUTTON); // If mouse click brought us here,
  65. // then no exit on arrows
  66. m_Rect = rect; // For bizarre CE bug.
  67. DWORD dwEditStyle = WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL //|ES_MULTILINE
  68. | dwStyle;
  69. if (!Create(dwEditStyle, rect, pParent, nID)) return;
  70. SetFont(pParent->GetFont());
  71. SetWindowText(sInitText);
  72. SetFocus();
  73. switch (nFirstChar){
  74. case VK_LBUTTON:
  75. case VK_RETURN: SetSel((int)_tcslen(m_sInitText), -1); return;
  76. case VK_BACK: SetSel((int)_tcslen(m_sInitText), -1); break;
  77. case VK_TAB:
  78. case VK_DOWN:
  79. case VK_UP:
  80. case VK_RIGHT:
  81. case VK_LEFT:
  82. case VK_NEXT:
  83. case VK_PRIOR:
  84. case VK_HOME:
  85. case VK_SPACE:
  86. case VK_END: SetSel(0, -1); return;
  87. default: SetSel(0, -1);
  88. }
  89. // Added by KiteFly. When entering DBCS chars into cells the first char was being lost
  90. // SenMessage changed to PostMessage (John Lagerquist)
  91. if (nFirstChar < 0x80)
  92. PostMessage(WM_CHAR, nFirstChar);
  93. else
  94. PostMessage(WM_IME_CHAR, nFirstChar);
  95. }
  96. CInPlaceEdit::~CInPlaceEdit()
  97. {
  98. }
  99. BEGIN_MESSAGE_MAP(CInPlaceEdit, CEdit)
  100. //{{AFX_MSG_MAP(CInPlaceEdit)
  101. ON_WM_KILLFOCUS()
  102. ON_WM_CHAR()
  103. ON_WM_KEYDOWN()
  104. ON_WM_GETDLGCODE()
  105. ON_WM_CREATE()
  106. //}}AFX_MSG_MAP
  107. END_MESSAGE_MAP()
  108. ////////////////////////////////////////////////////////////////////////////
  109. // CInPlaceEdit message handlers
  110. // If an arrow key (or associated) is pressed, then exit if
  111. // a) The Ctrl key was down, or
  112. // b) m_bExitOnArrows == TRUE
  113. void CInPlaceEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  114. {
  115. if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
  116. nChar == VK_DOWN || nChar == VK_UP ||
  117. nChar == VK_RIGHT || nChar == VK_LEFT) &&
  118. (m_bExitOnArrows || GetKeyState(VK_CONTROL) < 0))
  119. {
  120. m_nLastChar = nChar;
  121. GetParent()->SetFocus();
  122. return;
  123. }
  124. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  125. }
  126. // As soon as this edit loses focus, kill it.
  127. void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
  128. {
  129. CEdit::OnKillFocus(pNewWnd);
  130. EndEdit();
  131. }
  132. void CInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  133. {
  134. if (nChar == VK_TAB || nChar == VK_RETURN)
  135. {
  136. m_nLastChar = nChar;
  137. GetParent()->SetFocus(); // This will destroy this window
  138. return;
  139. }
  140. if (nChar == VK_ESCAPE)
  141. {
  142. SetWindowText(m_sInitText); // restore previous text
  143. m_nLastChar = nChar;
  144. GetParent()->SetFocus();
  145. return;
  146. }
  147. CEdit::OnChar(nChar, nRepCnt, nFlags);
  148. // Resize edit control if needed
  149. // Get text extent
  150. CString str;
  151. GetWindowText(str);
  152. // add some extra buffer
  153. str += _T(" ");
  154. CWindowDC dc(this);
  155. CFont *pFontDC = dc.SelectObject(GetFont());
  156. CSize size = dc.GetTextExtent(str);
  157. dc.SelectObject(pFontDC);
  158. // Get client rect
  159. CRect ParentRect;
  160. GetParent()->GetClientRect(&ParentRect);
  161. // Check whether control needs to be resized
  162. // and whether there is space to grow
  163. if (size.cx > m_Rect.Width())
  164. {
  165. if (size.cx + m_Rect.left < ParentRect.right)
  166. m_Rect.right = m_Rect.left + size.cx;
  167. else
  168. m_Rect.right = ParentRect.right;
  169. MoveWindow(&m_Rect);
  170. }
  171. }
  172. UINT CInPlaceEdit::OnGetDlgCode()
  173. {
  174. return DLGC_WANTALLKEYS;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////
  177. // CInPlaceEdit overrides
  178. // Stoopid win95 accelerator key problem workaround - Matt Weagle.
  179. BOOL CInPlaceEdit::PreTranslateMessage(MSG* pMsg)
  180. {
  181. // Catch the Alt key so we don't choke if focus is going to an owner drawn button
  182. if (pMsg->message == WM_SYSCHAR)
  183. return TRUE;
  184. return CWnd::PreTranslateMessage(pMsg);
  185. }
  186. // Auto delete
  187. void CInPlaceEdit::PostNcDestroy()
  188. {
  189. CEdit::PostNcDestroy();
  190. delete this;
  191. }
  192. ////////////////////////////////////////////////////////////////////////////
  193. // CInPlaceEdit implementation
  194. void CInPlaceEdit::EndEdit()
  195. {
  196. CString str;
  197. // EFW - BUG FIX - Clicking on a grid scroll bar in a derived class
  198. // that validates input can cause this to get called multiple times
  199. // causing assertions because the edit control goes away the first time.
  200. static BOOL bAlreadyEnding = FALSE;
  201. if (bAlreadyEnding)
  202. return;
  203. bAlreadyEnding = TRUE;
  204. GetWindowText(str);
  205. // Send Notification to parent
  206. GV_DISPINFO dispinfo;
  207. dispinfo.hdr.hwndFrom = GetSafeHwnd();
  208. dispinfo.hdr.idFrom = GetDlgCtrlID();
  209. dispinfo.hdr.code = GVN_ENDLABELEDIT;
  210. dispinfo.item.mask = LVIF_TEXT | LVIF_PARAM;
  211. dispinfo.item.row = m_nRow;
  212. dispinfo.item.col = m_nColumn;
  213. dispinfo.item.strText = str;
  214. dispinfo.item.lParam = (LPARAM)m_nLastChar;
  215. CWnd* pOwner = GetOwner();
  216. if (pOwner)
  217. pOwner->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo);
  218. // Close this window (PostNcDestroy will delete this)
  219. if (IsWindow(GetSafeHwnd()))
  220. SendMessage(WM_CLOSE, 0, 0);
  221. bAlreadyEnding = FALSE;
  222. }