EGPropertyGridItemEdit.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 4k
Category:

Graph program

Development Platform:

Visual C++

  1. // PropTreeItemEdit.cpp : implementation file
  2. //
  3. //  Copyright (C) 1998-2001 Scott Ramsay
  4. // sramsay@gonavi.com
  5. // http://www.gonavi.com
  6. //
  7. //  This material is provided "as is", with absolutely no warranty expressed
  8. //  or implied. Any use is at your own risk.
  9. // 
  10. //  Permission to use or copy this software for any purpose is hereby granted 
  11. //  without fee, provided the above notices are retained on all copies.
  12. //  Permission to modify the code and to distribute modified code is granted,
  13. //  provided the above notices are retained, and a notice that the code was
  14. //  modified is included with the above copyright notice.
  15. // 
  16. // If you use this code, drop me an email.  I'd like to know if you find the code
  17. // useful.
  18. #include "stdafx.h"
  19. #include "EGPropertyGrid.h"
  20. #include "EGPropertyGridItemEdit.h"
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CEGPropertyGridItemEdit
  28. CEGPropertyGridItemEdit::CEGPropertyGridItemEdit() :
  29. m_sEdit(_T("")),
  30. m_nFormat(ValueFormatText),
  31. m_bPassword(FALSE),
  32. m_fValue(0.0f)
  33. {
  34. }
  35. CEGPropertyGridItemEdit::~CEGPropertyGridItemEdit()
  36. {
  37. }
  38. BEGIN_MESSAGE_MAP(CEGPropertyGridItemEdit, CEdit)
  39. //{{AFX_MSG_MAP(CEGPropertyGridItemEdit)
  40. ON_WM_GETDLGCODE()
  41. ON_WM_KEYDOWN()
  42. ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CEGPropertyGridItemEdit message handlers
  47. void CEGPropertyGridItemEdit::DrawAttribute(CDC* pDC, const RECT& rc)
  48. {
  49. ASSERT(m_pProp!=NULL);
  50. pDC->SelectObject(IsReadOnly() ? m_pProp->GetNormalFont() : m_pProp->GetBoldFont());
  51. pDC->SetTextColor(RGB(0,0,0));
  52. pDC->SetBkMode(TRANSPARENT);
  53. CRect r = rc;
  54. TCHAR ch;
  55. // can't use GetPasswordChar(), because window may not be created yet
  56. ch = (m_bPassword) ? '*' : '';
  57. if (ch)
  58. {
  59. CString s;
  60. s = m_sEdit;
  61. for (LONG i=0; i<s.GetLength();i++)
  62. s.SetAt(i, ch);
  63. pDC->DrawText(s, r, DT_SINGLELINE|DT_VCENTER);
  64. }
  65. else
  66. {
  67. pDC->DrawText(m_sEdit, r, DT_SINGLELINE|DT_VCENTER);
  68. }
  69. }
  70. void CEGPropertyGridItemEdit::SetAsPassword(BOOL bPassword)
  71. {
  72. m_bPassword = bPassword;
  73. }
  74. void CEGPropertyGridItemEdit::SetValueFormat(ValueFormat nFormat)
  75. {
  76. m_nFormat = nFormat;
  77. }
  78. LPARAM CEGPropertyGridItemEdit::GetItemValue()
  79. {
  80. switch (m_nFormat)
  81. {
  82. case ValueFormatNumber:
  83. return _ttoi(m_sEdit);
  84. case ValueFormatFloatPointer:
  85. _stscanf(m_sEdit, _T("%f"), &m_fValue);
  86. return (LPARAM)&m_fValue;
  87. }
  88. return (LPARAM)(LPCTSTR)m_sEdit;
  89. }
  90. void CEGPropertyGridItemEdit::SetItemValue(LPARAM lParam)
  91. {
  92. switch (m_nFormat)
  93. {
  94. case ValueFormatNumber:
  95. m_sEdit.Format(_T("%d"), lParam);
  96. return;
  97. case ValueFormatFloatPointer:
  98. {
  99. TCHAR tmp[MAX_PATH];
  100. m_fValue = *(float*)lParam;
  101. _stprintf(tmp, _T("%f"), m_fValue);
  102. m_sEdit = tmp;
  103. }
  104. return;
  105. }
  106. if (lParam==0L)
  107. {
  108. TRACE0("CEGPropertyGridItemEdit::SetItemValue - Invalid lParam valuen");
  109. return;
  110. }
  111. m_sEdit = (LPCTSTR)lParam;
  112. }
  113. void CEGPropertyGridItemEdit::OnMove()
  114. {
  115. if (IsWindow(m_hWnd))
  116. SetWindowPos(NULL, m_rc.left, m_rc.top, m_rc.Width(), m_rc.Height(), SWP_NOZORDER|SWP_NOACTIVATE);
  117. }
  118. void CEGPropertyGridItemEdit::OnRefresh()
  119. {
  120. if (IsWindow(m_hWnd))
  121. SetWindowText(m_sEdit);
  122. }
  123. void CEGPropertyGridItemEdit::OnCommit()
  124. {
  125. // hide edit control
  126. ShowWindow(SW_HIDE);
  127. // store edit text for GetItemValue
  128. GetWindowText(m_sEdit);
  129. }
  130. void CEGPropertyGridItemEdit::OnActivate()
  131. {
  132. // Check if the edit control needs creation
  133. if (!IsWindow(m_hWnd))
  134. {
  135. DWORD dwStyle;
  136. dwStyle = WS_CHILD|ES_AUTOHSCROLL;
  137. Create(dwStyle, m_rc, m_pProp->GetCtrlParent(), GetCtrlID());
  138. SendMessage(WM_SETFONT, (WPARAM)m_pProp->GetNormalFont()->m_hObject);
  139. }
  140. SetPasswordChar((TCHAR)(m_bPassword ? '*' : 0));
  141. SetWindowText(m_sEdit);
  142. SetSel(0, -1);
  143. SetWindowPos(NULL, m_rc.left, m_rc.top, m_rc.Width(), m_rc.Height(), SWP_NOZORDER|SWP_SHOWWINDOW);
  144. SetFocus();
  145. }
  146. UINT CEGPropertyGridItemEdit::OnGetDlgCode() 
  147. {
  148. return CEdit::OnGetDlgCode()|DLGC_WANTALLKEYS;
  149. }
  150. void CEGPropertyGridItemEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  151. {
  152. if (nChar==VK_RETURN)
  153. CommitChanges();
  154. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  155. }
  156. void CEGPropertyGridItemEdit::OnKillfocus() 
  157. {
  158. CommitChanges();
  159. }