InPlaceEdit.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // InPlaceEdit.cpp : implementation file
  2. //
  3. // Written by Chris Maunder (chrismaunder@codeguru.com)
  4. // Copyright (c) 1998.
  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 is included. If
  13. // the source code in this file is used in any commercial application
  14. // then acknowledgement must be made to the author of this file
  15. // (in whatever form you wish).
  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. // Expect bugs!
  22. //
  23. // Please use and enjoy. Please let me know of any bugs/mods/improvements
  24. // that you have found/implemented and I will fix/incorporate them into this
  25. // file.
  26. //
  27. // Modifed 10 May 1998 Uses GVN_ notifications instead of LVN_,
  28. // Sends notification messages to the parent,
  29. // instead of the parent's parent.
  30. //
  31. // 15 May 1998 There was a problem when editing with the in-place editor,
  32. // there arises a general protection fault in user.exe, with a
  33. // few qualifications:
  34. // (1) This only happens with owner-drawn buttons;
  35. // (2) This only happens in Win95
  36. // (3) This only happens if the handler for the button does not
  37. // create a new window (even an AfxMessageBox will avoid the
  38. // crash)
  39. // (4) This will not happen if Spy++ is running.
  40. // PreTranslateMessage was added to route messages correctly.
  41. // (Matt Weagle found and fixed this problem)
  42. // 26 Jul 1998 Removed the ES_MULTILINE style - that fixed a few probs!
  43. // 6 Aug 1998 Added nID to the constructor param list
  44. // 6 Sep 1998 Space no longer clears selection when starting edit (Franco Bez)
  45. // 10 Apr 1999 Enter, Tab and Esc key prob fixed (Koay Kah Hoe)
  46. // Workaround for bizzare "shrinking window" problem in CE
  47. //
  48. /////////////////////////////////////////////////////////////////////////////
  49. #include "stdafx.h"
  50. #include "TCHAR.h"
  51. #include "InPlaceEdit.h"
  52. #include "GridCtrl.h"
  53. #ifdef _DEBUG
  54. #define new DEBUG_NEW
  55. #undef THIS_FILE
  56. static char THIS_FILE[] = __FILE__;
  57. #endif
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CInPlaceEdit
  60. CInPlaceEdit::CInPlaceEdit(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID,
  61. int nRow, int nColumn, CString sInitText,
  62. UINT nFirstChar)
  63. {
  64. m_sInitText = sInitText;
  65. m_nRow = nRow;
  66. m_nColumn = nColumn;
  67. m_nLastChar = 0;
  68. m_bExitOnArrows = (nFirstChar != VK_LBUTTON); // If mouse click brought us here,
  69. // then no exit on arrows
  70. m_Rect = rect; // For bizarre CE bug.
  71. DWORD dwEditStyle = WS_BORDER|WS_CHILD|WS_VISIBLE| ES_AUTOHSCROLL //|ES_MULTILINE
  72. | dwStyle;
  73. if (!Create(dwEditStyle, rect, pParent, nID)) return;
  74. SetFont(pParent->GetFont());
  75. SetWindowText(sInitText);
  76. SetFocus();
  77. switch (nFirstChar){
  78. case VK_LBUTTON:
  79. case VK_RETURN: SetSel((int)_tcslen(m_sInitText), -1); return;
  80. case VK_BACK: SetSel((int)_tcslen(m_sInitText), -1); break;
  81. case VK_TAB:
  82. case VK_DOWN:
  83. case VK_UP:
  84. case VK_RIGHT:
  85. case VK_LEFT:
  86. case VK_NEXT:
  87. case VK_PRIOR:
  88. case VK_HOME:
  89. case VK_SPACE:
  90. case VK_END: SetSel(0,-1); return;
  91. default: SetSel(0,-1);
  92. }
  93. SendMessage(WM_CHAR, nFirstChar);
  94. }
  95. CInPlaceEdit::~CInPlaceEdit()
  96. {
  97. }
  98. BEGIN_MESSAGE_MAP(CInPlaceEdit, CEdit)
  99. //{{AFX_MSG_MAP(CInPlaceEdit)
  100. ON_WM_KILLFOCUS()
  101. ON_WM_CHAR()
  102. ON_WM_KEYDOWN()
  103. ON_WM_GETDLGCODE()
  104. ON_WM_CREATE()
  105. //}}AFX_MSG_MAP
  106. END_MESSAGE_MAP()
  107. ////////////////////////////////////////////////////////////////////////////
  108. // CInPlaceEdit message handlers
  109. // If an arrow key (or associated) is pressed, then exit if
  110. // a) The Ctrl key was down, or
  111. // b) m_bExitOnArrows == TRUE
  112. void CInPlaceEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  113. {
  114. if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
  115. nChar == VK_DOWN || nChar == VK_UP ||
  116. nChar == VK_RIGHT || nChar == VK_LEFT) &&
  117. (m_bExitOnArrows || GetKeyState(VK_CONTROL) < 0))
  118. {
  119. m_nLastChar = nChar;
  120. GetParent()->SetFocus();
  121. return;
  122. }
  123. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  124. }
  125. // As soon as this edit loses focus, kill it.
  126. void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
  127. {
  128. CEdit::OnKillFocus(pNewWnd);
  129. EndEdit();
  130. }
  131. void CInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  132. {
  133. if (nChar == VK_TAB || nChar == VK_RETURN)
  134. {
  135. m_nLastChar = nChar;
  136. GetParent()->SetFocus(); // This will destroy this window
  137. return;
  138. }
  139. if (nChar == VK_ESCAPE)
  140. {
  141. SetWindowText(m_sInitText); // restore previous text
  142. m_nLastChar = nChar;
  143. GetParent()->SetFocus();
  144. return;
  145. }
  146. CEdit::OnChar(nChar, nRepCnt, nFlags);
  147. // Resize edit control if needed
  148. // Get text extent
  149. CString str;
  150. GetWindowText( str );
  151. // add some extra buffer
  152. str += _T(" ");
  153. CWindowDC dc(this);
  154. CFont *pFontDC = dc.SelectObject(GetFont());
  155. CSize size = dc.GetTextExtent( str );
  156. dc.SelectObject( pFontDC );
  157. // Get client rect
  158. CRect ParentRect;
  159. GetParent()->GetClientRect( &ParentRect );
  160. // Check whether control needs to be resized
  161. // and whether there is space to grow
  162. if (size.cx > m_Rect.Width())
  163. {
  164. if( size.cx + m_Rect.left < ParentRect.right )
  165. m_Rect.right = m_Rect.left + size.cx;
  166. else
  167. m_Rect.right = ParentRect.right;
  168. MoveWindow( &m_Rect );
  169. }
  170. }
  171. UINT CInPlaceEdit::OnGetDlgCode()
  172. {
  173. return DLGC_WANTALLKEYS;
  174. }
  175. ////////////////////////////////////////////////////////////////////////////
  176. // CInPlaceEdit overrides
  177. // Stoopid win95 accelerator key problem workaround - Matt Weagle.
  178. BOOL CInPlaceEdit::PreTranslateMessage(MSG* pMsg)
  179. {
  180. // Catch the Alt key so we don't choke if focus is going to an owner drawn button
  181. if (pMsg->message == WM_SYSCHAR)
  182. return TRUE;
  183. return CWnd::PreTranslateMessage(pMsg);
  184. }
  185. // Auto delete
  186. void CInPlaceEdit::PostNcDestroy()
  187. {
  188. CEdit::PostNcDestroy();
  189. delete this;
  190. }
  191. ////////////////////////////////////////////////////////////////////////////
  192. // CInPlaceEdit implementation
  193. void CInPlaceEdit::EndEdit()
  194. {
  195. CString str;
  196. GetWindowText(str);
  197. // Send Notification to parent
  198. GV_DISPINFO dispinfo;
  199. dispinfo.hdr.hwndFrom = GetSafeHwnd();
  200. dispinfo.hdr.idFrom = GetDlgCtrlID();
  201. dispinfo.hdr.code = GVN_ENDLABELEDIT;
  202. dispinfo.item.mask = LVIF_TEXT|LVIF_PARAM;
  203. dispinfo.item.row = m_nRow;
  204. dispinfo.item.col = m_nColumn;
  205. dispinfo.item.szText = str;
  206. dispinfo.item.lParam = (LPARAM) m_nLastChar;
  207. CWnd* pOwner = GetOwner();
  208. if (pOwner)
  209. pOwner->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo );
  210. // Close this window (PostNcDestroy will delete this)
  211. PostMessage(WM_CLOSE, 0, 0);
  212. }