PropertyGridExtItemTime.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "stdafx.h"
  2. #include "PropertyGridInplaceEdit.h"
  3. #include "PropertyGridInplaceButton.h"
  4. #include "PropertyGridInplaceList.h"
  5. #include "PropertyGridItem.h"
  6. #include "PropertyGridExtItemTime.h"
  7. #include "PropertyGrid.h"
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CInplaceTimeEdit
  10. /////////////////////////////////////////////////////////////////////////////
  11. CInplaceTimeEdit::CInplaceTimeEdit(BOOL bHasSecond)
  12. : CTimeEdit(bHasSecond)
  13. , m_pItem(0)
  14. , m_pGrid(0)
  15. {
  16. }
  17. CInplaceTimeEdit::~CInplaceTimeEdit()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CInplaceTimeEdit, CTimeEdit)
  21. //{{AFX_MSG_MAP(CInplaceTimeEdit)
  22. ON_WM_CTLCOLOR_REFLECT()
  23. ON_CONTROL_REFLECT(EN_KILLFOCUS, OnEnKillfocus)
  24. ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetfocus)
  25. ON_WM_LBUTTONDBLCLK()
  26. ON_WM_GETDLGCODE()
  27. ON_WM_KEYDOWN()
  28. ON_WM_CHAR()
  29. //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31. void CInplaceTimeEdit::SetValue(CString strValue)
  32. {
  33. m_strValue = strValue;
  34. }
  35. void CInplaceTimeEdit::Create(CPropertyGridExtItemTime* pItem, CRect rect)
  36. {
  37. ASSERT(pItem && pItem->GetGrid());
  38. m_pGrid = pItem->GetGrid();
  39. m_pItem = pItem;
  40. if (!m_hWnd)
  41. {
  42. CTimeEdit::Create(WS_CHILD|ES_AUTOHSCROLL, rect, m_pGrid, 0);
  43. }
  44. SetWindowText(m_strValue);
  45. SetWindowPos(0, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER|SWP_SHOWWINDOW);
  46. }
  47. HBRUSH CInplaceTimeEdit::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
  48. {
  49. pDC->SetTextColor(GetStyle() & ES_READONLY ? GetSysColor(COLOR_GRAYTEXT): ((CPropertyGridView*)m_pGrid)->m_clrFore);
  50. COLORREF clr = ((CPropertyGridView*)m_pGrid)->m_clrBack;
  51. if (clr != m_clrBack || !m_brBack.GetSafeHandle())
  52. {
  53. m_brBack.DeleteObject();
  54. m_brBack.CreateSolidBrush(clr);
  55. m_clrBack = clr;
  56. }
  57. pDC->SetBkColor(m_clrBack);
  58. return m_brBack;
  59. }
  60. void CInplaceTimeEdit::OnEnSetfocus()
  61. {
  62. ASSERT(m_pItem && m_pGrid);
  63. m_pGrid->Invalidate(FALSE);
  64. }
  65. void CInplaceTimeEdit::OnEnKillfocus()
  66. {
  67. if (m_pItem)
  68. {
  69. m_pItem->OnValidateEdit();
  70. m_pGrid->Invalidate(FALSE);
  71. }
  72. }
  73. UINT CInplaceTimeEdit::OnGetDlgCode()
  74. {
  75. return DLGC_WANTALLKEYS;
  76. }
  77. void CInplaceTimeEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  78. {
  79. if (nChar == VK_TAB) return;
  80. if (nChar == VK_ESCAPE || nChar == VK_RETURN)
  81. {
  82. m_pGrid->SetFocus();
  83. return;
  84. }
  85. CTimeEdit::OnChar(nChar, nRepCnt, nFlags);
  86. }
  87. void CInplaceTimeEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  88. {
  89. if (nChar == VK_TAB)
  90. {
  91. CWnd* pParent = m_pGrid->GetParent();
  92. if (!pParent || !pParent->GetParent())
  93. {
  94. ASSERT(FALSE);
  95. return;
  96. }
  97. CWnd* pWndNext = pParent->GetParent()->GetNextDlgTabItem( pParent, ::GetKeyState(VK_SHIFT) < 0 );
  98. if (pWndNext != NULL)
  99. {
  100. pWndNext->SetFocus();
  101. }
  102. return;
  103. }
  104. else if (nChar == VK_ESCAPE)
  105. {
  106. SetWindowText(m_strValue);
  107. return;
  108. }
  109. else if (nChar == VK_RETURN)
  110. {
  111. return;
  112. }
  113. else if (nChar == VK_DOWN || nChar == VK_UP)
  114. {
  115. if (SelectConstraint(nChar == VK_DOWN? +1: -1, FALSE))
  116. {
  117. MaskSelectAll();
  118. return;
  119. }
  120. }
  121. CTimeEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  122. }
  123. BOOL CInplaceTimeEdit::SelectConstraint(int nDirection, BOOL bCircle)
  124. {
  125. CPGItemConstraints* pList = m_pItem->GetConstraints();
  126. if (pList->IsEmpty())
  127. return FALSE;
  128. CString str;
  129. GetWindowText(str);
  130. int nIndex = pList->FindConstraint( str );
  131. nIndex+= nDirection;
  132. if (nIndex >= pList->GetCount()) nIndex = bCircle ? 0 : (ULONG)pList->GetCount() - 1;
  133. if (nIndex < 0) nIndex = bCircle ? (ULONG)pList->GetCount() - 1 : 0;
  134. POSITION pos = pList->FindIndex(nIndex);
  135. SetWindowText( pList->GetAt(pos) );
  136. return TRUE;
  137. }
  138. void CInplaceTimeEdit::OnLButtonDblClk(UINT nFlags, CPoint point)
  139. {
  140. if (m_pItem != NULL && !m_pItem->GetReadOnly() && SelectConstraint(+1, TRUE))
  141. {
  142. m_pGrid->SetFocus();
  143. return;
  144. }
  145. CTimeEdit::OnLButtonDblClk(nFlags, point);
  146. }
  147. void CInplaceTimeEdit::DestroyItem()
  148. {
  149. // reset variables to defaults.
  150. m_pItem = NULL;
  151. m_pGrid = NULL;
  152. m_strValue.Empty();
  153. m_brBack.DeleteObject();
  154. // destroy the window.
  155. DestroyWindow( );
  156. }
  157. /////////////////////////////////////////////////////////////////////////////
  158. // CPropertyGridExtItemTime
  159. /////////////////////////////////////////////////////////////////////////////
  160. CPropertyGridExtItemTime::CPropertyGridExtItemTime(CString strCaption, COleDateTime dtTime /* = 当前时间 */, BOOL bHasSecond /* = FALSE */ )
  161. : CPropertyGridItem(strCaption)
  162. , m_wndTimeEdit(bHasSecond)
  163. {
  164. m_wndTimeEdit.m_pItem = this;
  165. m_pBindTime = NULL;
  166. m_nFlags = pgitemHasEdit;
  167. m_bHasSecond = bHasSecond;
  168. SetTime(dtTime);
  169. }
  170. CPropertyGridExtItemTime::CPropertyGridExtItemTime(UINT nID, COleDateTime dtTime /* = 当前时间 */, BOOL bHasSecond /* = FALSE */ )
  171. : CPropertyGridItem(nID)
  172. , m_wndTimeEdit(bHasSecond)
  173. {
  174. m_wndTimeEdit.m_pItem = this;
  175. m_pBindTime = NULL;
  176. m_nFlags = pgitemHasEdit;
  177. m_bHasSecond = bHasSecond;
  178. SetTime(dtTime);
  179. }
  180. CPropertyGridExtItemTime::~CPropertyGridExtItemTime()
  181. {
  182. m_wndTimeEdit.DestroyItem();
  183. }
  184. void CPropertyGridExtItemTime::SetTime(COleDateTime dtTime)
  185. {
  186. // 如果参数dtTime不合法,则使用缺省构造函数产生的时间做为属性值
  187. if (dtTime.GetStatus() == COleDateTime::valid)
  188. m_dtTime = dtTime;
  189. else
  190. m_dtTime = COleDateTime();
  191. CPropertyGridItem::SetValue( CTimeEdit::TimeToString(m_dtTime, m_bHasSecond) );
  192. if (m_pBindTime)
  193. *m_pBindTime = m_dtTime;
  194. }
  195. COleDateTime CPropertyGridExtItemTime::GetTime()
  196. {
  197. return m_dtTime;
  198. }
  199. void CPropertyGridExtItemTime::SetValue(CString strValue)
  200. {
  201. SetTime(CTimeEdit::StringToOleDateTime(strValue));
  202. }
  203. void CPropertyGridExtItemTime::OnValidateEdit()
  204. {
  205. if (m_wndTimeEdit.GetSafeHwnd())
  206. {
  207. m_wndTimeEdit.ShowWindow(SW_HIDE);
  208. CString strValue;
  209. m_wndTimeEdit.GetWindowText(strValue);
  210. if (m_strValue != strValue)
  211. {
  212. // 如果输入的时间合法,则更新属性值,否则发出错误提示音
  213. COleDateTime dt= CDateEdit::StringToOleDateTime(strValue);
  214. if (dt.GetStatus() == COleDateTime::valid)
  215. {
  216. OnValueChanged(strValue);
  217. m_pGrid->Invalidate(FALSE);
  218. }
  219. else
  220. {
  221. ::MessageBeep( (UINT)-1 );
  222. // 内置编辑控件恢复原来的属性值
  223. SetEditText(m_strValue);
  224. }
  225. }
  226. }
  227. }
  228. void CPropertyGridExtItemTime::SetEditText(CString str)
  229. {
  230. if (!m_pGrid) return;
  231. if (m_wndTimeEdit.GetSafeHwnd())
  232. m_wndTimeEdit.SetWindowText(str);
  233. }
  234. void CPropertyGridExtItemTime::OnSelect()
  235. {
  236. ASSERT(m_bVisible);
  237. if (!m_bReadOnly && (m_nFlags & (pgitemHasComboButton | pgitemHasExpandButton)))
  238. {
  239. GetInplaceButton().Create(this, GetItemRect());
  240. }
  241. if (m_nFlags & pgitemHasEdit)
  242. {
  243. m_wndTimeEdit.SetValue(m_strValue);
  244. m_wndTimeEdit.Create(this, GetValueRect());
  245. m_wndTimeEdit.SetReadOnly(m_bReadOnly);
  246. m_wndTimeEdit.SetFont(GetGrid()->GetFont());
  247. m_wndTimeEdit.SetMargins(3, 0);
  248. }
  249. }
  250. BOOL CPropertyGridExtItemTime::OnChar(UINT nChar)
  251. {
  252. if (m_nFlags & pgitemHasEdit)
  253. {
  254. OnSelect();
  255. m_wndTimeEdit.SetFocus();
  256. m_wndTimeEdit.MaskSelectAll();
  257. if (nChar != VK_TAB) m_wndTimeEdit.SendMessage(WM_CHAR, nChar);
  258. return TRUE;
  259. }
  260. return FALSE;
  261. }
  262. void CPropertyGridExtItemTime::OnLButtonDblClk()
  263. {
  264. if (HasChilds())
  265. {
  266. if(m_bExpanded) Collapse(); else Expand();
  267. }
  268. else
  269. {
  270. OnSelect();
  271. if (m_nFlags & pgitemHasEdit)
  272. {
  273. if (!GetReadOnly() && m_wndTimeEdit.SelectConstraint(+1, TRUE))
  274. {
  275. OnValidateEdit();
  276. }
  277. else
  278. {
  279. m_wndTimeEdit.SetFocus();
  280. m_wndTimeEdit.MaskSelectAll();
  281. }
  282. }
  283. else if (!GetReadOnly())
  284. {
  285. CPGItemConstraints* pList = GetConstraints();
  286. if (pList->IsEmpty())
  287. return ;
  288. int nIndex = pList->FindConstraint(m_strValue);
  289. nIndex += +1;
  290. if (nIndex >= pList->GetCount()) nIndex = 0;
  291. POSITION pos = pList->FindIndex(nIndex);
  292. OnValueChanged(pList->GetAt(pos));
  293. }
  294. }
  295. }
  296. void CPropertyGridExtItemTime::BindToTime(COleDateTime* pBindTime)
  297. {
  298. m_pBindTime = pBindTime;
  299. if (m_pBindTime)
  300. SetTime(*m_pBindTime);
  301. }