Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.3 KiB

  1. // InPlaceEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "InPlace.h"
  5. #include "cpanel.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define MAX_STR_LEN 255
  12. extern HWND hAdvListCtrl;
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CInPlaceEdit
  15. CInPlaceEdit::CInPlaceEdit(BYTE iItem, BYTE iSubItem):m_iItem(iItem),m_iSubItem(iSubItem)
  16. //,m_bESC(FALSE),m_sInitText(sInitText)
  17. {
  18. m_iItem = iItem;
  19. m_iSubItem = iSubItem;
  20. m_bESC = FALSE;
  21. // _tcscpy(m_sInitText, sInitText);
  22. }
  23. CInPlaceEdit::~CInPlaceEdit()
  24. {
  25. }
  26. BEGIN_MESSAGE_MAP(CInPlaceEdit, CEdit)
  27. //{{AFX_MSG_MAP(CInPlaceEdit)
  28. ON_WM_KILLFOCUS()
  29. ON_WM_CHAR()
  30. ON_WM_CREATE()
  31. ON_WM_MOUSEWHEEL()
  32. //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CInPlaceEdit message handlers
  36. /*
  37. BOOL CInPlaceEdit::PreTranslateMessage(MSG* pMsg)
  38. {
  39. if( pMsg->message == WM_KEYDOWN )
  40. {
  41. if(pMsg->wParam == VK_RETURN
  42. || pMsg->wParam == VK_DELETE
  43. || pMsg->wParam == VK_ESCAPE
  44. || GetKeyState( VK_CONTROL)
  45. )
  46. {
  47. ::TranslateMessage(pMsg);
  48. ::DispatchMessage(pMsg);
  49. return TRUE; // DO NOT process further
  50. }
  51. }
  52. return CEdit::PreTranslateMessage(pMsg);
  53. }
  54. */
  55. BOOL CInPlaceEdit::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
  56. {
  57. OnKillFocus(this);
  58. return TRUE;
  59. }
  60. void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
  61. {
  62. CEdit::OnKillFocus(pNewWnd);
  63. if (LineLength())
  64. {
  65. ::GetWindowText(this->GetSafeHwnd(), m_sInitText, MAX_STR_LEN);
  66. // No point sending the message if the text hasn't changed!
  67. // OR if there's nothing to add!
  68. // Send Notification to parent of ListView ctrl
  69. LV_DISPINFO *lpDispinfo = new (LV_DISPINFO);
  70. ASSERT (lpDispinfo);
  71. lpDispinfo->hdr.hwndFrom = GetParent()->m_hWnd;
  72. lpDispinfo->hdr.idFrom = GetDlgCtrlID();
  73. lpDispinfo->hdr.code = LVN_ENDLABELEDIT;
  74. lpDispinfo->item.mask = LVIF_TEXT;
  75. lpDispinfo->item.iItem = m_iItem;
  76. lpDispinfo->item.iSubItem = m_iSubItem;
  77. lpDispinfo->item.pszText = m_bESC ? NULL : m_sInitText;
  78. lpDispinfo->item.cchTextMax = MAX_STR_LEN;
  79. GetParent()->GetParent()->SendMessage( WM_NOTIFY, GetParent()->GetDlgCtrlID(),
  80. (LPARAM)lpDispinfo );
  81. if (lpDispinfo)
  82. delete (lpDispinfo);
  83. }
  84. if (m_sInitText)
  85. delete[] (m_sInitText);
  86. PostMessage(WM_CLOSE);
  87. }
  88. void CInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  89. {
  90. CEdit::OnChar(nChar, nRepCnt, nFlags);
  91. // Get text extent
  92. BYTE nLen = (BYTE)SendMessage(LB_GETTEXTLEN, (WPARAM)0, 0);
  93. if (nLen == 255)
  94. return;
  95. LPTSTR lpStr = new (TCHAR[nLen+1]);
  96. ASSERT (lpStr);
  97. SendMessage(LB_GETTEXT, (WPARAM)0, (LPARAM)(LPCTSTR)lpStr);
  98. // Resize edit control if needed
  99. HDC hDC = this->GetDC()->m_hDC;
  100. SIZE size;
  101. ::GetTextExtentPoint(hDC, lpStr, nLen+1, &size);
  102. ::ReleaseDC(this->m_hWnd, hDC);
  103. if (lpStr)
  104. delete[] (lpStr);
  105. size.cx += 5; // add some extra buffer
  106. // Get client rect
  107. RECT rect, parentrect;
  108. GetClientRect( &rect );
  109. GetParent()->GetClientRect( &parentrect );
  110. // Transform rect to parent coordinates
  111. ClientToScreen( &rect );
  112. GetParent()->ScreenToClient( &rect );
  113. // Check whether control needs to be resized
  114. // and whether there is space to grow
  115. if( size.cx > (rect.right-rect.left) )
  116. {
  117. rect.right = ( size.cx + rect.left < parentrect.right ) ? rect.left + size.cx : parentrect.right;
  118. MoveWindow( &rect );
  119. }
  120. }
  121. int CInPlaceEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
  122. {
  123. if (CEdit::OnCreate(lpCreateStruct) == -1)
  124. return -1;
  125. // Allocate the string buffer
  126. m_sInitText = new (TCHAR[MAX_STR_LEN+1]);
  127. ASSERT (m_sInitText);
  128. GetItemText(hAdvListCtrl, m_iItem, m_iSubItem, m_sInitText, MAX_STR_LEN);
  129. // Set the proper font
  130. // If you don't, the font is a bold version of the dialog font!
  131. ::SendMessage(this->m_hWnd, WM_SETFONT, ::SendMessage(::GetParent(this->m_hWnd), WM_GETFONT, 0, 0), 0);
  132. SendMessage(WM_SETTEXT, 0, (LPARAM)(LPCTSTR)m_sInitText);
  133. SetFocus();
  134. SendMessage(EM_SETSEL, (WPARAM)0, (LPARAM)-1);
  135. SendMessage(EM_LIMITTEXT, (WPARAM)MAX_STR_LEN, 0);
  136. return 0;
  137. }