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

对话框与窗口

开发平台:

Visual C++

  1. // XTPCalendarWeekView.cpp: implementation of the CXTPCalendarWeekView class.
  2. //
  3. // This file is a part of the XTREME CALENDAR 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 "XTPCalendarWeekView.h"
  22. #include "XTPCalendarPaintManager.h"
  23. #include "XTPCalendarWeekViewEvent.h"
  24. #include "XTPCalendarWeekViewDay.h"
  25. #include "XTPCalendarControl.h"
  26. #include "XTPCalendarTheme.h"
  27. #include "XTPCalendarNotifications.h"
  28. #ifdef _DEBUG
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #define new DEBUG_NEW
  32. #endif
  33. //////////////////////////////////////////////////////////////////////
  34. // Construction/Destruction
  35. //////////////////////////////////////////////////////////////////////
  36. IMPLEMENT_DYNAMIC(CXTPCalendarWeekView, CXTPCalendarView)
  37. CXTPCalendarWeekView::CXTPCalendarWeekView(CXTPCalendarControl* pCalendarControl) :
  38. TBase(pCalendarControl, xtpCalendarWeekView)
  39. {
  40. m_LayoutX.m_nGridColumns = 2;
  41. m_nCurrPos = 180;//365;
  42. m_LayoutX.m_nDayHeaderHeight = 18;
  43. m_LayoutX.m_nDayWidth = 1;
  44. m_LayoutX.m_nDayHeidht = 1;
  45. m_nFirstDayOfWeekIndex = 0;     // 1 = Sunday, 2 = Monday ...
  46. COleDateTime dtNow = CXTPCalendarUtils::GetCurrentTime();
  47. SetBeginDate(dtNow);
  48. AdjustFirstDayOfWeek();
  49. SetBeginDate(dtNow);
  50. //---------------------------------------------------------------------------
  51. for (int i = 0; i < 7; i++)
  52. {
  53. CXTPCalendarWeekViewDay* pWDayView = new CXTPCalendarWeekViewDay(this);
  54. m_arDays.Add(pWDayView);
  55. }
  56. }
  57. CXTPCalendarWeekView::~CXTPCalendarWeekView()
  58. {
  59. }
  60. /////////////////////////////////////////////////////////////////////////////////////////////////
  61. void CXTPCalendarWeekView::ShowDay(const COleDateTime& date, BOOL bSelect)
  62. {
  63. CXTPCalendarControl::CUpdateContext updateContext(m_pControl, xtpCalendarUpdateRedrawIfNeed);
  64. CXTPCalendarControl::CViewChangedContext viewChanged(this);
  65. CXTPCalendarControl::CUpdateContext updateContext2(m_pControl, xtpCalendarUpdateAll);
  66. SetBeginDate(date);
  67. Populate();
  68. if (bSelect)
  69. SelectDay(CXTPCalendarUtils::ResetTime(date));
  70. }
  71. void CXTPCalendarWeekView::Populate()
  72. {
  73. AdjustFirstDayOfWeek();
  74. TBase::Populate();
  75. }
  76. void CXTPCalendarWeekView::AdjustLayout(CDC* pDC, const CRect& rcView, BOOL bCallPostAdjustLayout)
  77. {
  78. if (!GetPaintManager() || !GetPaintManager()->GetWeekViewPart() ||
  79. !GetPaintManager()->GetWeekViewEventPart() || !pDC)
  80. {
  81. ASSERT(FALSE);
  82. return;
  83. }
  84. TBase::AdjustLayout(pDC, rcView, FALSE);
  85. AdjustFirstDayOfWeek();
  86. CXTPCalendarViewPart* pWVPart = GetPaintManager()->GetWeekViewPart();
  87. m_LayoutX.m_nDayHeaderHeight = pWVPart->GetTextExtent(pDC, _T("A")).cy + 7;
  88. m_Layout.m_nRowHeight = GetPaintManager()->GetWeekViewEventPart()->GetTextExtent(pDC, _T("(")).cy + 6;
  89. // Adjust Week's Grid
  90. CRect rcView2 = rcView;
  91. rcView2.DeflateRect(-1, -1, -1, -1);
  92. int nRows = 6/m_LayoutX.m_nGridColumns;
  93. ASSERT(6 % m_LayoutX.m_nGridColumns == 0);
  94. m_LayoutX.m_nDayWidth = rcView2.Width() / m_LayoutX.m_nGridColumns;
  95. m_LayoutX.m_nDayHeidht = rcView2.Height() / nRows;
  96. CalculateHeaderFormat(pDC, m_LayoutX.m_nDayWidth, &pWVPart->GetTextFont());
  97. int nDayIndex = 0;
  98. CRect rc(0, 0, 0, 0);
  99. for (int nDayCol = 0; nDayCol < m_LayoutX.m_nGridColumns; nDayCol++)
  100. {
  101. for (int nDayRow = 0; nDayRow < nRows; nDayRow++)
  102. {
  103. CXTPCalendarWeekViewDay* pDay = GetViewDay(nDayIndex);
  104. ASSERT(pDay);
  105. if (!pDay)
  106. {
  107. continue;
  108. }
  109. COleDateTime dtDayDate = GetViewDayDate(nDayIndex);
  110. rc.SetRect( rcView2.left + m_LayoutX.m_nDayWidth * nDayCol,
  111. rcView2.top + m_LayoutX.m_nDayHeidht* nDayRow,
  112. rcView2.left + m_LayoutX.m_nDayWidth + m_LayoutX.m_nDayWidth * nDayCol,
  113. rcView2.top + m_LayoutX.m_nDayHeidht + m_LayoutX.m_nDayHeidht * nDayRow);
  114. int nWday = dtDayDate.GetDayOfWeek();
  115. ASSERT(nWday != 1);
  116. if (nWday == 7)
  117. {
  118. CRect rc2 = rc;
  119. rc2.bottom = rc.top + rc.Height()/2;
  120. pDay->AdjustLayout(pDC, rc2);
  121. nDayIndex++;
  122. pDay = GetViewDay(nDayIndex);
  123. rc.top = rc2.bottom;
  124. }
  125. ASSERT(pDay);
  126. if (pDay)
  127. {
  128. pDay->AdjustLayout(pDC, rc);
  129. }
  130. nDayIndex++;
  131. }
  132. }
  133. //---------------------------------------------------------------------------
  134. if (bCallPostAdjustLayout)
  135. {
  136. OnPostAdjustLayout();
  137. }
  138. }
  139. void CXTPCalendarWeekView::AdjustLayout2(CDC* pDC, const CRect& rcView, BOOL bCallPostAdjustLayout)
  140. {
  141. if (!GetTheme())
  142. {
  143. ASSERT(FALSE);
  144. return;
  145. }
  146. TBase::AdjustLayout(pDC, rcView, FALSE);
  147. AdjustFirstDayOfWeek();
  148. XTP_SAFE_CALL2(GetTheme(), GetWeekViewPart(), AdjustLayout(pDC, rcView, bCallPostAdjustLayout));
  149. }
  150. void CXTPCalendarWeekView::AdjustFirstDayOfWeek()
  151. {
  152. if (!GetCalendarControl())
  153. {
  154. return;
  155. }
  156. int nFDPrev = m_nFirstDayOfWeekIndex;
  157. //- Adjust First day of Week ------------------------------------------
  158. m_nFirstDayOfWeekIndex = GetCalendarControl()->GetFirstDayOfWeek();
  159. if (m_nFirstDayOfWeekIndex == 1)
  160. {
  161. m_nFirstDayOfWeekIndex = 2;
  162. }
  163. int nDShift = m_nFirstDayOfWeekIndex - nFDPrev;
  164. COleDateTime dtBeginDate_prev = GetBeginDate();
  165. COleDateTime dtBeginDate = dtBeginDate_prev + COleDateTimeSpan(nDShift);
  166. SetBeginDate(dtBeginDate);
  167. dtBeginDate = GetBeginDate();
  168. //  if (!CXTPCalendarUtils::IsEqual(dtBeginDate_prev, dtBeginDate))
  169. //  {
  170. //      CXTPCalendarControl::CViewChangedContext viewChanged(this);
  171. //  }
  172. }
  173. COleDateTime CXTPCalendarWeekView::ShiftDateToCell_00(COleDateTime dtDate) const
  174. {
  175. int nShift = (dtDate.GetDayOfWeek() - m_nFirstDayOfWeekIndex + 7) % 7;
  176. if (nShift)
  177. {
  178. COleDateTimeSpan spShift(nShift, 0, 0, 0);
  179. dtDate -= spShift;
  180. }
  181. return dtDate;
  182. }
  183. void CXTPCalendarWeekView::SetBeginDate(COleDateTime dtNewBeginDate)
  184. {
  185. m_dtBeginDate = ShiftDateToCell_00(CXTPCalendarUtils::ResetTime(dtNewBeginDate));
  186. }
  187. void CXTPCalendarWeekView::Draw(CDC* pDC)
  188. {
  189. XTP_SAFE_CALL2(GetPaintManager(), GetWeekViewPart(), OnDraw(pDC, this));
  190. // Draw events
  191. int nDays = m_arDays.GetCount();
  192. for (int nDay = 0; nDay < nDays; nDay++)
  193. {
  194. XTP_SAFE_CALL1(m_arDays.GetAt(nDay), Draw(pDC));
  195. }
  196. }
  197. void CXTPCalendarWeekView::Draw2(CDC* pDC)
  198. {
  199. if (!GetTheme())
  200. {
  201. ASSERT(FALSE);
  202. return;
  203. }
  204. GetTheme()->GetWeekViewPart()->Draw(pDC);
  205. }
  206. void CXTPCalendarWeekView::ScrollV(int /*nPos*/, int nPos_raw)
  207. {
  208. if (nPos_raw == m_nCurrPos || m_bScrollV_Disabled)
  209. return;
  210. CXTPCalendarControl::CViewChangedContext viewChanged(this);
  211. if (IsEditingSubject())
  212. {
  213. EndEditSubject(xtpCalendarEditSubjectCommit, FALSE);
  214. }
  215. int nShift = nPos_raw - m_nCurrPos;
  216. COleDateTimeSpan spWeeks(nShift * 7, 0, 0, 0);
  217. m_dtBeginDate += spWeeks;
  218. m_nCurrPos = nPos_raw;
  219. if (GetSelection())
  220. {
  221. m_selectedBlock.dtBegin += spWeeks;
  222. m_selectedBlock.dtEnd += spWeeks;
  223. CSelectionChangedContext selChanged(this, xtpCalendarSelectionDays);
  224. }
  225. UnselectAllEvents();
  226. Populate();
  227. }
  228. BOOL CXTPCalendarWeekView::GetScrollBarInfoV(SCROLLINFO* pSI)
  229. {
  230. ASSERT(pSI);
  231. if (!pSI || m_bScrollV_Disabled)
  232. {
  233. return FALSE;
  234. }
  235. pSI->nPos = m_nCurrPos;
  236. pSI->nMax = 360;//730;
  237. pSI->nPage = 1;
  238. return TRUE;
  239. }
  240. COleDateTime CXTPCalendarWeekView::GetViewDayDate(int nIndex)
  241. {
  242. ASSERT(nIndex >= 0 && nIndex < 7);
  243. COleDateTimeSpan spDays(nIndex, 0, 0, 0);
  244. COleDateTime dtDayDate = m_dtBeginDate + spDays;
  245. return dtDayDate;
  246. }
  247. void CXTPCalendarWeekView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  248. {
  249. CSelectionChangedContext selChanged(this);
  250. TBase::OnKeyDown(nChar, nRepCnt, nFlags);
  251. if (nChar == VK_LEFT || nChar == VK_RIGHT || nChar == VK_UP || nChar == VK_DOWN ||
  252. nChar == VK_PRIOR || nChar == VK_NEXT)
  253. {
  254. CXTPCalendarControl::CUpdateContext updateContext(m_pControl, xtpCalendarUpdateRedraw);
  255. BOOL bVKShift = !!(GetKeyState(VK_SHIFT) & 0x8000);
  256. COleDateTimeSpan spDay(1, 0, 0, 0);
  257. COleDateTimeSpan spWeek(7, 0, 0, 0);
  258. int nDVCount = GetViewDayCount();
  259. BOOL bSelReseted = !m_selectedBlock.dtEnd.GetStatus() == COleDateTime::valid;
  260. COleDateTime dtLastSelDay = GetLastSelectedDate();
  261. int nLastSelDayIdx = (dtLastSelDay.GetDayOfWeek() - m_nFirstDayOfWeekIndex + 7) % 7;
  262. COleDateTime dtNewSelDay = dtLastSelDay;
  263. int nSatDayIdx = 7 - m_nFirstDayOfWeekIndex;
  264. int nNewIdx = nLastSelDayIdx + 3;
  265. int nHalfShift = nNewIdx > nSatDayIdx && nLastSelDayIdx <= nSatDayIdx ||
  266.  nNewIdx > nSatDayIdx + 7 && nLastSelDayIdx <= nSatDayIdx + 7 ? 4 : 3;
  267. COleDateTimeSpan spHalfWeekPlus(nHalfShift, 0, 0, 0);
  268. COleDateTimeSpan spHalfWeekMinus(7 - nHalfShift, 0, 0, 0);
  269. CXTPDrawHelpers::KeyToLayout(m_pControl, nChar);
  270. if (nChar == VK_LEFT)
  271. {
  272. dtNewSelDay -= spHalfWeekMinus;
  273. }
  274. else
  275. if (nChar == VK_RIGHT)
  276. {
  277. dtNewSelDay += spHalfWeekPlus;
  278. }
  279. else
  280. if (nChar == VK_UP)
  281. {
  282. dtNewSelDay -= spDay;
  283. }
  284. else
  285. if (nChar == VK_DOWN)
  286. {
  287. dtNewSelDay += spDay;
  288. }
  289. else
  290. if (nChar == VK_PRIOR)
  291. {
  292. dtNewSelDay -= spWeek;
  293. }
  294. else
  295. if (nChar == VK_NEXT)
  296. {
  297. dtNewSelDay += spWeek;
  298. }
  299. //---------------------------------------------------------------------------
  300. COleDateTime dtFirstDay = GetViewDayDate(0);
  301. COleDateTime dtLastDay = GetViewDayDate(nDVCount-1);
  302. BOOL bNeedScroll = dtNewSelDay < dtFirstDay || dtNewSelDay > dtLastDay;
  303. if (bNeedScroll && !m_bScrollV_Disabled)
  304. {
  305. if (dtNewSelDay < dtFirstDay)
  306. {
  307. ScrollV(m_nCurrPos-1, m_nCurrPos-1);
  308. bSelReseted = TRUE;
  309. CXTPCalendarControl::CUpdateContext updateContext1(m_pControl, xtpCalendarUpdateScrollBar);
  310. }
  311. if (dtNewSelDay > dtLastDay)
  312. {
  313. ScrollV(m_nCurrPos + 1, m_nCurrPos + 1);
  314. bSelReseted = TRUE;
  315. CXTPCalendarControl::CUpdateContext updateContext1(m_pControl, xtpCalendarUpdateScrollBar);
  316. }
  317. }
  318. //---------------------------------------------------------------------------
  319. if (!bNeedScroll || !m_bScrollV_Disabled)
  320. {
  321. if (bVKShift && !bSelReseted)
  322. {
  323. SelectDays(dtNewSelDay);
  324. }
  325. else
  326. {
  327. SelectDay(dtNewSelDay);
  328. }
  329. }
  330. }
  331. }