XTPPropertyGridItemExt.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:13k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPPropertyGridItemExt.cpp
  2. //
  3. // This file is a part of the XTREME PROPERTYGRID MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "StdAfx.h"
  21. #include "Common/XTPVC80Helpers.h"
  22. #include "Common/XTPVC50Helpers.h"
  23. #include "Common/XTPResourceManager.h"
  24. #include "XTPPropertyGridInplaceEdit.h"
  25. #include "XTPPropertyGridInplaceButton.h"
  26. #include "XTPPropertyGridInplaceList.h"
  27. #include "XTPPropertyGridItem.h"
  28. #include "XTPPropertyGridItemExt.h"
  29. #include "XTPPropertyGrid.h"
  30. #include "XTPPropertyGridDefines.h"
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. //////////////////////////////////////////////////////////////////////////
  37. BEGIN_MESSAGE_MAP(CXTPPropertyGridInplaceMonthCal, CWnd)
  38. ON_NOTIFY_REFLECT(MCN_SELECT, OnSelect)
  39. END_MESSAGE_MAP()
  40. void CXTPPropertyGridInplaceMonthCal::OnSelect(NMHDR*, LRESULT*)
  41. {
  42. OnAccept();
  43. }
  44. void CXTPPropertyGridInplaceMonthCal::OnCancel()
  45. {
  46. if (m_pItem)
  47. {
  48. m_pItem->OnCancelEdit();
  49. }
  50. DestroyWindow();
  51. }
  52. void CXTPPropertyGridInplaceMonthCal::OnAccept()
  53. {
  54. SYSTEMTIME sysTime;
  55. ::SendMessage(m_hWnd, MCM_GETCURSEL, 0, (LPARAM) &sysTime);
  56. sysTime.wHour = sysTime.wMinute = sysTime.wSecond = sysTime.wMilliseconds = 0;
  57. COleDateTime dtSelected(sysTime);
  58. CString str = m_pItem->Format(dtSelected);
  59. CXTPPropertyGridItemDate* pItem = m_pItem;
  60. m_pItem = NULL;
  61. if (pItem->OnAfterEdit(str))
  62. {
  63. pItem->OnValueChanged(str);
  64. }
  65. PostMessage(WM_CLOSE);
  66. }
  67. void CXTPPropertyGridInplaceMonthCal::PostNcDestroy()
  68. {
  69. delete this;
  70. }
  71. BOOL CXTPPropertyGridInplaceMonthCal::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  72. {
  73. if (message == WM_KILLFOCUS)
  74. {
  75. CWnd* pWnd = CWnd::FromHandle((HWND)wParam);
  76. if (pWnd && IsChild(pWnd))
  77. {
  78. return CWnd::OnWndMsg(message, wParam, lParam, pResult);
  79. }
  80. OnCancel();
  81. return TRUE;
  82. }
  83. if (message == WM_KEYDOWN && wParam == VK_ESCAPE)
  84. {
  85. OnCancel();
  86. return TRUE;
  87. }
  88. if (message == WM_KEYDOWN && wParam == VK_RETURN)
  89. {
  90. OnAccept();
  91. return TRUE;
  92. }
  93. return CWnd::OnWndMsg(message, wParam, lParam, pResult);
  94. }
  95. IMPLEMENT_DYNAMIC(CXTPPropertyGridItemDate, CXTPPropertyGridItem)
  96. CXTPPropertyGridItemDate::CXTPPropertyGridItemDate(LPCTSTR strCaption, const COleDateTime& oleDate, COleDateTime* pBindDate)
  97. : CXTPPropertyGridItem(strCaption)
  98. {
  99. m_pBindDate = pBindDate;
  100. Init(oleDate);
  101. }
  102. CXTPPropertyGridItemDate::CXTPPropertyGridItemDate(UINT nID, const COleDateTime& oleDate, COleDateTime* pBindDate)
  103. : CXTPPropertyGridItem(nID)
  104. {
  105. m_pBindDate = pBindDate;
  106. Init(oleDate);
  107. }
  108. void CXTPPropertyGridItemDate::Init(const COleDateTime& oleDate)
  109. {
  110. m_nFlags = xtpGridItemHasComboButton | xtpGridItemHasEdit;
  111. m_strFormat = _T("%d/%m/%Y");
  112. m_strValue = _T("00/00/0000");
  113. m_strNullValue.Empty();
  114. SetMask(_T("00/00/0000"), _T("__/__/____"));
  115. SetDate(oleDate);
  116. EnableAutomation();
  117. m_strDefaultValue = m_strValue;
  118. }
  119. BOOL CXTPPropertyGridItemDate::ParseDateTime(COleDateTime& dt, LPCTSTR strValue)
  120. {
  121. SYSTEMTIME sysTime;
  122. ZeroMemory(&sysTime, sizeof(SYSTEMTIME));
  123. WORD* ptrDate[3] = {0, 0, 0};
  124. int nResult[3] = {0, 0, 0};
  125. CString strFormat(m_strFormat);
  126. strFormat.MakeLower();
  127. int nIndex = -1, i = 0;
  128. for (i = 0; i < 3; i++)
  129. {
  130. nIndex = FIND_S(strFormat, _T('%'), nIndex + 1);
  131. if (nIndex == -1 || nIndex == strFormat.GetLength() - 1)
  132. return FALSE;
  133. switch (strFormat[nIndex + 1])
  134. {
  135. case _T('d'):
  136. ptrDate[i] = &sysTime.wDay;
  137. break;
  138. case _T('y'):
  139. ptrDate[i] = &sysTime.wYear;
  140. break;
  141. case _T('m'):
  142. ptrDate[i] = &sysTime.wMonth;
  143. break;
  144. default:
  145. return FALSE;
  146. }
  147. strFormat.SetAt(nIndex + 1, _T('d'));
  148. }
  149. if (SCANF_S(strValue, strFormat, &nResult[0], &nResult[1], &nResult[2]) != 3)
  150. return FALSE;
  151. for (i = 0; i < 3; i++)
  152. {
  153. if (!ptrDate[i])
  154. return FALSE;
  155. *ptrDate[i] = (WORD)nResult[i];
  156. }
  157. dt = sysTime;
  158. return dt.GetStatus() == COleDateTime::valid;
  159. }
  160. void CXTPPropertyGridItemDate::SetValue(CString strValue)
  161. {
  162. COleDateTime dt;
  163. TRY
  164. {
  165. if (ParseDateTime(dt, strValue))
  166. {
  167. SetDate(dt);
  168. }
  169. else if (dt.ParseDateTime(strValue, VAR_DATEVALUEONLY, LANG_RUSSIAN))
  170. {
  171. SetDate(dt);
  172. }
  173. else if (!m_strNullValue.IsEmpty())
  174. {
  175. COleDateTime dtNull;
  176. dtNull.SetStatus(COleDateTime::null);
  177. SetDate(dtNull);
  178. }
  179. }
  180. CATCH(COleException, e)
  181. {
  182. }
  183. END_CATCH
  184. }
  185. void CXTPPropertyGridItemDate::SetDateFormat(LPCTSTR strFormat)
  186. {
  187. m_strFormat = strFormat;
  188. SetDate(m_oleDate);
  189. }
  190. CString CXTPPropertyGridItemDate::Format(const COleDateTime& oleDate)
  191. {
  192. if (oleDate.GetStatus() != COleDateTime::valid)
  193. return _T("");
  194. #if _MSC_VER > 1310
  195. if (oleDate.GetYear() <= 1900) // Visual Studio 2005 bug.
  196. {
  197. CString strDate;
  198. if (m_strFormat == _T("%d/%m/%Y"))
  199. {
  200. strDate.Format(_T("%.2i/%.2i/%.4i"), oleDate.GetDay(), oleDate.GetMonth(), oleDate.GetYear());
  201. return strDate;
  202. }
  203. if (m_strFormat == _T("%m/%d/%Y"))
  204. {
  205. strDate.Format(_T("%.2i/%.2i/%.4i"), oleDate.GetMonth(), oleDate.GetDay(), oleDate.GetYear());
  206. return strDate;
  207. }
  208. }
  209. #endif
  210. return oleDate.Format(m_strFormat);
  211. }
  212. void CXTPPropertyGridItemDate::SetDate(const COleDateTime& oleDate)
  213. {
  214. if (oleDate.GetStatus() != COleDateTime::valid && !m_strNullValue.IsEmpty())
  215. {
  216. m_oleDate = oleDate;
  217. if (m_pBindDate) *m_pBindDate = oleDate;
  218. CXTPPropertyGridItem::SetValue(m_strNullValue);
  219. return;
  220. }
  221. ASSERT(oleDate.GetStatus() == COleDateTime::valid);
  222. m_oleDate = oleDate;
  223. if (m_pBindDate) *m_pBindDate = oleDate;
  224. CString strValue = Format(oleDate);
  225. CXTPPropertyGridItem::SetValue(strValue);
  226. }
  227. void CXTPPropertyGridItemDate::BindToDate(COleDateTime* pBindDate)
  228. {
  229. m_pBindDate = pBindDate;
  230. if (m_pBindDate)
  231. {
  232. *m_pBindDate = m_oleDate;
  233. }
  234. }
  235. void CXTPPropertyGridItemDate::OnBeforeInsert()
  236. {
  237. if (m_pBindDate && *m_pBindDate != m_oleDate)
  238. {
  239. SetDate(*m_pBindDate);
  240. }
  241. }
  242. #if _MSC_VER < 1200 // MFC 5.0
  243. BOOL CXTPPropertyGridItemDate::GetAsSystemTime(SYSTEMTIME& sysTime)
  244. {
  245. BOOL bRetVal = FALSE;
  246. if (m_oleDate.GetStatus() == COleDateTime::valid)
  247. {
  248. struct tm tmTemp;
  249. if (CXTPDateTimeHelper::TmFromOleDate(m_oleDate, tmTemp))
  250. {
  251. sysTime.wYear = (WORD) tmTemp.tm_year;
  252. sysTime.wMonth = (WORD) tmTemp.tm_mon;
  253. sysTime.wDayOfWeek = (WORD) tmTemp.tm_wday;
  254. sysTime.wDay = (WORD) tmTemp.tm_mday;
  255. sysTime.wMinute = sysTime.wMilliseconds = sysTime.wSecond = sysTime.wHour = 0;
  256. bRetVal = TRUE;
  257. }
  258. }
  259. return bRetVal;
  260. }
  261. #else
  262. BOOL CXTPPropertyGridItemDate::GetAsSystemTime(SYSTEMTIME& sysTime)
  263. {
  264. BOOL bRetVal = FALSE;
  265. if (m_oleDate.GetStatus() == COleDateTime::valid)
  266. {
  267. bRetVal = m_oleDate.GetAsSystemTime(sysTime);
  268. sysTime.wMinute = sysTime.wMilliseconds = sysTime.wSecond = sysTime.wHour = 0;
  269. }
  270. return bRetVal;
  271. }
  272. #endif
  273. void CXTPPropertyGridItemDate::OnInplaceButtonDown(CXTPPropertyGridInplaceButton* pButton)
  274. {
  275. if (m_pGrid->SendNotifyMessage(XTP_PGN_INPLACEBUTTONDOWN, (LPARAM)pButton) == TRUE)
  276. return;
  277. if (!OnRequestEdit())
  278. return;
  279. CXTPPropertyGridInplaceMonthCal*pManthCtrl = new CXTPPropertyGridInplaceMonthCal(this);
  280. #if _MSC_VER < 1200 // MFC 5.0
  281. INITCOMMONCONTROLSEX icex;
  282. icex.dwSize = sizeof(icex);
  283. icex.dwICC = ICC_DATE_CLASSES;
  284. VERIFY(InitCommonControlsEx(&icex));
  285. #else
  286. VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTL_DATE_REG));
  287. #endif // _MSC_VER < 1200
  288. CRect rect(0, 0, 0, 0);
  289. pManthCtrl->CreateEx(WS_EX_TOPMOST | (m_pGrid->GetExStyle() & WS_EX_LAYOUTRTL), MONTHCAL_CLASS, NULL, WS_BORDER | WS_POPUP | WS_VISIBLE, rect, m_pGrid, 0);
  290. SYSTEMTIME sysTime;
  291. if (GetAsSystemTime(sysTime))
  292. {
  293. pManthCtrl->SendMessage(MCM_SETCURSEL, 0, (LPARAM) &sysTime);
  294. }
  295. if (pManthCtrl->GetMinReqRect(rect))
  296. {
  297. CRect rcItem = GetItemRect();
  298. rect.SetRect(rcItem.right - rect.Width(), rcItem.bottom, rcItem.right, rcItem.bottom + rect.Height());
  299. m_pGrid->ClientToScreen(&rect);
  300. CRect rcWork = XTPMultiMonitor()->GetWorkArea(rect);
  301. if (rect.bottom > rcWork.bottom && rect.top > rcWork.CenterPoint().y)
  302. {
  303. rect.OffsetRect(0, - rect.Height() - rcItem.Height() - 1);
  304. }
  305. if (rect.left < rcWork.left) rect.OffsetRect(rcWork.left - rect.left, 0);
  306. if (rect.right > rcWork.right) rect.OffsetRect(rcWork.right - rect.right, 0);
  307. pManthCtrl->SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER | SWP_NOACTIVATE);
  308. }
  309. pManthCtrl->SetOwner(m_pGrid);
  310. pManthCtrl->SetFocus();
  311. }
  312. long CXTPPropertyGridItemDate::GetDay()
  313. {
  314. return m_oleDate.GetDay();
  315. }
  316. void CXTPPropertyGridItemDate::SetDay(long nDay)
  317. {
  318. COleDateTime oleDate(GetYear(), GetMonth(), nDay, 0, 0, 0);
  319. SetDate(oleDate);
  320. }
  321. long CXTPPropertyGridItemDate::GetMonth()
  322. {
  323. return m_oleDate.GetMonth();
  324. }
  325. void CXTPPropertyGridItemDate::SetMonth(long nMonth)
  326. {
  327. COleDateTime oleDate(GetYear(), nMonth, GetDay(), 0, 0, 0);
  328. SetDate(oleDate);
  329. }
  330. long CXTPPropertyGridItemDate::GetYear()
  331. {
  332. return m_oleDate.GetYear();
  333. }
  334. void CXTPPropertyGridItemDate::SetYear(long nYear)
  335. {
  336. COleDateTime oleDate(nYear, GetMonth(), GetDay(), 0, 0, 0);
  337. SetDate(oleDate);
  338. }
  339. void CXTPPropertyGridItemDate::AllowNullDate(LPCTSTR lpszNullValue)
  340. {
  341. m_strNullValue = lpszNullValue;
  342. }
  343. //////////////////////////////////////////////////////////////////////////
  344. // CXTPPropertyGridItemMultilineString
  345. CXTPPropertyGridItemMultilineString::CXTPPropertyGridItemMultilineString(LPCTSTR lpszCaption, LPCTSTR strValue, CString* pBindString)
  346. : CXTPPropertyGridItem(lpszCaption, strValue, pBindString)
  347. {
  348. m_nFlags = xtpGridItemHasComboButton | xtpGridItemHasEdit;
  349. m_nDropDownItemCount = 8;
  350. }
  351. CXTPPropertyGridItemMultilineString::CXTPPropertyGridItemMultilineString(UINT nID, LPCTSTR strValue, CString* pBindString)
  352. : CXTPPropertyGridItem(nID, strValue, pBindString)
  353. {
  354. m_nFlags = xtpGridItemHasComboButton | xtpGridItemHasEdit;
  355. m_nDropDownItemCount = 8;
  356. }
  357. void CXTPPropertyGridItemMultilineString::OnInplaceButtonDown(CXTPPropertyGridInplaceButton* pButton)
  358. {
  359. if (GetReadOnly())
  360. return;
  361. if (m_pGrid->SendNotifyMessage(XTP_PGN_INPLACEBUTTONDOWN, (LPARAM)pButton) == TRUE)
  362. return;
  363. if ((pButton->GetID() == XTP_ID_PROPERTYGRID_COMBOBUTTON) && OnRequestEdit())
  364. {
  365. CXTPPropertyGridInplaceMultilineEdit* pEdit = new CXTPPropertyGridInplaceMultilineEdit();
  366. pEdit->Create(this, GetItemRect());
  367. m_pGrid->Invalidate(FALSE);
  368. }
  369. }
  370. #ifdef __AFXCTL_H__
  371. //////////////////////////////////////////////////////////////////////////
  372. // CXTPPropertyGridItemPicture
  373. CXTPPropertyGridItemPicture::CXTPPropertyGridItemPicture(LPCTSTR strCaption)
  374. : CXTPPropertyGridItem(strCaption)
  375. {
  376. m_nFlags = xtpGridItemHasExpandButton;
  377. m_nPreviewWidth = 20;
  378. }
  379. BOOL CXTPPropertyGridItemPicture::OnDrawItemValue(CDC& dc, CRect rcValue)
  380. {
  381. COLORREF clr = dc.GetTextColor();
  382. CRect rcSample(rcValue.left - 2, rcValue.top + 1, rcValue.left - 2 + m_nPreviewWidth, rcValue.bottom - 1);
  383. m_olePicture.Render(&dc, rcSample, rcSample);
  384. dc.Draw3dRect(rcSample, clr, clr);
  385. CRect rcText(rcValue);
  386. rcText.left += m_nPreviewWidth + 5;
  387. short type = m_olePicture.GetType();
  388. CString str = type == PICTYPE_ICON ? _T("(Icon)") :
  389. type == PICTYPE_BITMAP ? _T("(Bitmap)") : _T("None");
  390. dc.DrawText(str, rcText, DT_SINGLELINE | DT_VCENTER);
  391. return TRUE;
  392. }
  393. void CXTPPropertyGridItemPicture::OnInplaceButtonDown(CXTPPropertyGridInplaceButton* pButton)
  394. {
  395. if (m_pGrid->SendNotifyMessage(XTP_PGN_INPLACEBUTTONDOWN, (LPARAM)pButton) == TRUE)
  396. return;
  397. if (!OnRequestEdit())
  398. return;
  399. CString strFilter = _T("All Picture Types|*.bmp;*.cur;*.dib;*.emf;*.ico;*.wmf;*.gif;*.jpg|Bitmaps (*.bmp;*.dib;*.gif;*.jpg)|*.bmp;*.dib;*.gif;*.jpg |Icons/Cursors (*.ico;*.cur)|*.ico;*.cur|Metafiles (*.wmf;*.emf)|*.wmf;*.emf|All files (*.*)|*.*||");
  400. CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, strFilter);
  401. if (dlg.DoModal() == IDOK)
  402. {
  403. CString strPath(dlg.GetPathName());
  404. if (OnAfterEdit(strPath))
  405. {
  406. SetPicturePath(strPath);
  407. }
  408. }
  409. else
  410. {
  411. OnCancelEdit();
  412. }
  413. }
  414. void CXTPPropertyGridItemPicture::SetPicturePath(LPCTSTR lpszPath)
  415. {
  416. m_strPicturePath = lpszPath;
  417. LPPICTUREDISP pPict = NULL;
  418. if (OleLoadPicturePath((LPOLESTR)XTP_CT2CW(m_strPicturePath), NULL, 0, 0, IID_IPictureDisp, (LPVOID*)&pPict) == S_OK)
  419. {
  420. m_olePicture.SetPictureDispatch(pPict);
  421. pPict->Release();
  422. OnValueChanged(_T(""));
  423. ((CWnd*)m_pGrid)->Invalidate(FALSE);
  424. }
  425. else if (m_olePicture.GetPictureDispatch())
  426. {
  427. m_olePicture.SetPictureDispatch(NULL);
  428. OnValueChanged(_T(""));
  429. ((CWnd*)m_pGrid)->Invalidate(FALSE);
  430. }
  431. }
  432. #endif