PropertyGridInPlaceEdit.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // InPlaceEdit.cpp : implementation file
  2. //
  3. // Adapted by Chris Maunder <cmaunder@mail.com>
  4. // Copyright (c) 1998-2002. All Rights Reserved.
  5. //
  6. // The code contained in this file is based on the original
  7. // CPropertyGridInPlaceEdit 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 "PropertyGridInPlaceEdit.h"
  48. #ifdef _DEBUG
  49. #define new DEBUG_NEW
  50. #undef THIS_FILE
  51. static char THIS_FILE[] = __FILE__;
  52. #endif
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CPropertyGridInPlaceEdit
  55. CPropertyGridInPlaceEdit::CPropertyGridInPlaceEdit(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID, string sInitText)
  56. {
  57. m_sInitText = sInitText.c_str();
  58. m_bExitOnArrows = FALSE;
  59. m_Rect = rect; // For bizarre CE bug.
  60. DWORD dwEditStyle = /*WS_BORDER|*/WS_CHILD|/*WS_VISIBLE|*/ES_AUTOHSCROLL|dwStyle;
  61. if (!Create(dwEditStyle, rect, pParent, nID))
  62. return;
  63. m_clrBack = GetSysColor(COLOR_WINDOW);
  64. m_clrText = GetSysColor(COLOR_WINDOWTEXT);
  65. m_Brush.CreateSolidBrush(m_clrBack);
  66. SetFont(pParent->GetFont());
  67. SetWindowText(m_sInitText);
  68. SetFocus();
  69. SetSel(0, -1);
  70. SetSel(-1, 0);
  71. }
  72. CPropertyGridInPlaceEdit::~CPropertyGridInPlaceEdit()
  73. {
  74. }
  75. void CPropertyGridInPlaceEdit::SetColors(COLORREF clrBack, COLORREF clrText)
  76. {
  77. m_clrBack = clrBack;
  78. m_clrText = clrText;
  79. m_Brush.DeleteObject();
  80. m_Brush.CreateSolidBrush(m_clrBack);
  81. }
  82. BEGIN_MESSAGE_MAP(CPropertyGridInPlaceEdit, CEdit)
  83. //{{AFX_MSG_MAP(CPropertyGridInPlaceEdit)
  84. ON_WM_KILLFOCUS()
  85. ON_WM_CHAR()
  86. ON_WM_KEYDOWN()
  87. ON_WM_GETDLGCODE()
  88. ON_WM_CREATE()
  89. ON_WM_CTLCOLOR_REFLECT( )
  90. //}}AFX_MSG_MAP
  91. END_MESSAGE_MAP()
  92. ////////////////////////////////////////////////////////////////////////////
  93. // CPropertyGridInPlaceEdit message handlers
  94. // If an arrow key (or associated) is pressed, then exit if
  95. // a) The Ctrl key was down, or
  96. // b) m_bExitOnArrows == TRUE
  97. void CPropertyGridInPlaceEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  98. {
  99. if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
  100. nChar == VK_DOWN || nChar == VK_UP ||
  101. nChar == VK_RIGHT || nChar == VK_LEFT) &&
  102. (m_bExitOnArrows || GetKeyState(VK_CONTROL) < 0))
  103. {
  104. GetParent()->SetFocus();
  105. return;
  106. }
  107. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  108. }
  109. // As soon as this edit loses focus, kill it.
  110. void CPropertyGridInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
  111. {
  112. CEdit::OnKillFocus(pNewWnd);
  113. EndEdit();
  114. }
  115. void CPropertyGridInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  116. {
  117. if (nChar == VK_TAB || nChar == VK_RETURN)
  118. {
  119. GetParent()->SetFocus(); // This will destroy this window
  120. return;
  121. }
  122. if (nChar == VK_ESCAPE)
  123. {
  124. CancelEdit();
  125. return;
  126. }
  127. CEdit::OnChar(nChar, nRepCnt, nFlags);
  128. //// Resize edit control if needed
  129. //
  130. //// Get text extent
  131. //CString str;
  132. //GetWindowText( str );
  133. //// add some extra buffer
  134. //str += _T(" ");
  135. //
  136. //CWindowDC dc(this);
  137. //CFont *pFontDC = dc.SelectObject(GetFont());
  138. //CSize size = dc.GetTextExtent( str );
  139. //dc.SelectObject( pFontDC );
  140. //
  141. //// Get client rect
  142. //CRect ParentRect;
  143. //GetParent()->GetClientRect( &ParentRect );
  144. //
  145. //// Check whether control needs to be resized
  146. //// and whether there is space to grow
  147. //if (size.cx > m_Rect.Width())
  148. //{
  149. // if( size.cx + m_Rect.left < ParentRect.right )
  150. // m_Rect.right = m_Rect.left + size.cx;
  151. // else
  152. // m_Rect.right = ParentRect.right;
  153. // MoveWindow( &m_Rect );
  154. //}
  155. }
  156. UINT CPropertyGridInPlaceEdit::OnGetDlgCode()
  157. {
  158. return DLGC_WANTALLKEYS;
  159. }
  160. ////////////////////////////////////////////////////////////////////////////
  161. // CPropertyGridInPlaceEdit overrides
  162. // Stoopid win95 accelerator key problem workaround - Matt Weagle.
  163. BOOL CPropertyGridInPlaceEdit::PreTranslateMessage(MSG* pMsg)
  164. {
  165. // Catch the Alt key so we don't choke if focus is going to an owner drawn button
  166. if (pMsg->message == WM_SYSCHAR)
  167. return TRUE;
  168. return CEdit::PreTranslateMessage(pMsg);
  169. }
  170. ////////////////////////////////////////////////////////////////////////////
  171. // CPropertyGridInPlaceEdit implementation
  172. void CPropertyGridInPlaceEdit::CancelEdit()
  173. {
  174. // restore previous text
  175. if (IsWindow(GetSafeHwnd()))
  176. {
  177. SetWindowText(m_sInitText);
  178. SendMessage(WM_CLOSE, 0, 0);
  179. }
  180. }
  181. void CPropertyGridInPlaceEdit::EndEdit()
  182. {
  183. CString str;
  184. // EFW - BUG FIX - Clicking on a grid scroll bar in a derived class
  185. // that validates input can cause this to get called multiple times
  186. // causing assertions because the edit control goes away the first time.
  187. static BOOL bAlreadyEnding = FALSE;
  188. if(bAlreadyEnding)
  189. return;
  190. bAlreadyEnding = TRUE;
  191. GetWindowText(str);
  192. CWnd* pOwner = GetOwner();
  193. if (pOwner)
  194. pOwner->SendMessage(WM_PG_ENDLABELEDIT, (WPARAM) LPCTSTR(str), NULL );
  195. // Close this window (PostNcDestroy will delete this)
  196. if (IsWindow(GetSafeHwnd()))
  197. SendMessage(WM_CLOSE, 0, 0);
  198. bAlreadyEnding = FALSE;
  199. }
  200. HBRUSH CPropertyGridInPlaceEdit::CtlColor ( CDC* pDC, UINT nCtlColor )
  201. {
  202. pDC->SetTextColor(m_clrText);
  203. pDC->SetBkColor(m_clrBack);
  204. return m_Brush;
  205. }