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.

7327 lines
211 KiB

  1. #include "ctlspriv.h"
  2. #include "scdttime.h"
  3. #include "monthcal.h"
  4. #include "prshti.h" // for StrDup_AtoW
  5. // TODO
  6. //
  7. // #6329: When Min/Max range is set, then dates before the min
  8. // or after the max are painted in the normal date color. They
  9. // should be painted with MCSC_TRAILINGTEXT color. (Or we should
  10. // add a new color to cover this case.) Feature requested by Jobi George
  11. //
  12. // 9577: We want a DAYSTATE like structure for the background
  13. // color of dates. For highlighting. Perhaps a COLORSTATE per
  14. // registered background color.
  15. //
  16. // private message
  17. #define MCMP_WINDOWPOSCHANGED (MCM_FIRST - 1) // MCM_FIRST is way over WM_USER
  18. #define DTMP_WINDOWPOSCHANGED (DTM_FIRST - 1) // DTM_FIRST is way over WM_USER
  19. // MONTHCAL
  20. LRESULT CALLBACK MonthCalWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  21. LRESULT MCNcCreateHandler(HWND hwnd);
  22. LRESULT MCCreateHandler(MONTHCAL *pmc, HWND hwnd, LPCREATESTRUCT lpcs);
  23. LRESULT MCOnStyleChanging(MONTHCAL *pmc, UINT gwl, LPSTYLESTRUCT pinfo);
  24. LRESULT MCOnStyleChanged(MONTHCAL *pmc, UINT gwl, LPSTYLESTRUCT pinfo);
  25. void MCCalcSizes(MONTHCAL *pmc);
  26. void MCHandleSetFont(MONTHCAL *pmc, HFONT hfont, BOOL fRedraw);
  27. void MCPaint(MONTHCAL *pmc, HDC hdc);
  28. void MCPaintMonth(MONTHCAL *pmc, HDC hdc, RECT *prc, int iMonth, int iYear, int iIndex,
  29. BOOL fDrawPrev, BOOL fDrawNext, HBRUSH hbrSelect);
  30. void MCNcDestroyHandler(HWND hwnd, MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  31. void MCRecomputeSizing(MONTHCAL *pmc, RECT *prect);
  32. LRESULT MCSizeHandler(MONTHCAL *pmc, RECT *prc);
  33. void MCUpdateMonthNamePos(MONTHCAL *pmc);
  34. void MCUpdateStartEndDates(MONTHCAL *pmc, SYSTEMTIME *pstStart);
  35. void MCGetRcForDay(MONTHCAL *pmc, int iMonth, int iDay, RECT *prc);
  36. void MCGetRcForMonth(MONTHCAL *pmc, int iMonth, RECT *prc);
  37. void MCUpdateToday(MONTHCAL *pmc);
  38. void MCUpdateRcDayCur(MONTHCAL *pmc, SYSTEMTIME *pst);
  39. void MCUpdateDayState(MONTHCAL *pmc);
  40. int MCGetOffsetForYrMo(MONTHCAL *pmc, int iYear, int iMonth);
  41. int MCIsSelectedDayMoYr(MONTHCAL *pmc, int iDay, int iMonth, int iYear);
  42. BOOL MCIsBoldOffsetDay(MONTHCAL *pmc, int nDay, int iIndex);
  43. BOOL FGetOffsetForPt(MONTHCAL *pmc, POINT pt, int *piOffset);
  44. BOOL FGetRowColForRelPt(MONTHCAL *pmc, POINT ptRel, int *piRow, int *piCol);
  45. BOOL FGetDateForPt(MONTHCAL *pmc, POINT pt, SYSTEMTIME *pst,
  46. int* piDay, int* piCol, int* piRow, LPRECT prcMonth);
  47. LRESULT MCContextMenu(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  48. LRESULT MCLButtonDown(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  49. LRESULT MCLButtonUp(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  50. LRESULT MCMouseMove(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  51. LRESULT MCHandleTimer(MONTHCAL *pmc, WPARAM wParam);
  52. LRESULT MCHandleKeydown(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  53. LRESULT MCHandleChar(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam);
  54. int MCIncrStartMonth(MONTHCAL *pmc, int nDelta, BOOL fDelayDayChange);
  55. void MCGetTitleRcsForOffset(MONTHCAL* pmc, int iOffset, LPRECT prcMonth, LPRECT prcYear);
  56. BOOL MCSetDate(MONTHCAL *pmc, SYSTEMTIME *pst);
  57. void MCNotifySelChange(MONTHCAL *pmc, UINT uMsg);
  58. void MCInvalidateDates(MONTHCAL *pmc, SYSTEMTIME *pst1, SYSTEMTIME *pst2);
  59. void MCInvalidateMonthDays(MONTHCAL *pmc);
  60. void MCSetToday(MONTHCAL* pmc, SYSTEMTIME* pst);
  61. void MCGetTodayBtnRect(MONTHCAL *pmc, RECT *prc);
  62. void GetYrMoForOffset(MONTHCAL *pmc, int iOffset, int *piYear, int *piMonth);
  63. BOOL FScrollIntoView(MONTHCAL *pmc);
  64. void MCFreeCalendarInfo(PCALENDARTYPE pct);
  65. void MCGetCalendarInfo(PCALENDARTYPE pct);
  66. BOOL MCIsDateStringRTL(TCHAR tch);
  67. // DATEPICK
  68. LRESULT CALLBACK DatePickWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  69. LRESULT DPNcCreateHandler(HWND hwnd);
  70. LRESULT DPCreateHandler(DATEPICK *pdp, HWND hwnd, LPCREATESTRUCT lpcs);
  71. LRESULT DPOnStyleChanging(DATEPICK *pdp, UINT gwl, LPSTYLESTRUCT pinfo);
  72. LRESULT DPOnStyleChanged(DATEPICK *pdp, UINT gwl, LPSTYLESTRUCT pinfo);
  73. void DPHandleLocaleChange(DATEPICK *pdp);
  74. void DPDestroyHandler(HWND hwnd, DATEPICK *pdp, WPARAM wParam, LPARAM lParam);
  75. void DPHandleSetFont(DATEPICK *pdp, HFONT hfont, BOOL fRedraw);
  76. void DPPaint(DATEPICK *pdp, HDC hdc);
  77. void DPLBD_MonthCal(DATEPICK *pdp, BOOL fLButtonDown);
  78. LRESULT DPLButtonDown(DATEPICK *pdp, WPARAM wParam, LPARAM lParam);
  79. LRESULT DPLButtonUp(DATEPICK *pdp, WPARAM wParam, LPARAM lParam);
  80. void DPRecomputeSizing(DATEPICK *pdp, RECT *prect);
  81. LRESULT DPHandleKeydown(DATEPICK *pdp, WPARAM wParam, LPARAM lParam);
  82. LRESULT DPHandleChar(DATEPICK *pdp, WPARAM wParam, LPARAM lParam);
  83. void DPNotifyDateChange(DATEPICK *pdp);
  84. BOOL DPSetDate(DATEPICK *pdp, SYSTEMTIME *pst, BOOL fMungeDate);
  85. void DPDrawDropdownButton(DATEPICK *pdp, HDC hdc, BOOL fPressed);
  86. void SECGetSystemtime(LPSUBEDITCONTROL psec, LPSYSTEMTIME pst);
  87. static TCHAR const g_rgchMCName[] = MONTHCAL_CLASS;
  88. static TCHAR const g_rgchDTPName[] = DATETIMEPICK_CLASS;
  89. // MONTHCAL globals
  90. #define g_szTextExtentDef TEXT("0000")
  91. #define g_szNumFmt TEXT("%d")
  92. //
  93. // Epoch = the beginning of the universe (the earliest date we support)
  94. // Armageddon = the end of the universe (the latest date we support)
  95. //
  96. // Epoch is 14-sep-1752 because that's when the Gregorian calendar
  97. // kicked in. The day before 14-sep-1752 was 2-sep-1752 (in British
  98. // and US history; other countries switched at other times).
  99. //
  100. // Armageddon is 31-dec-9999 because we assume four digits for years
  101. // is enough. (Oh no, the Y10K problem...)
  102. //
  103. const SYSTEMTIME c_stEpoch = { 1752, 9, 0, 14, 0, 0, 0, 0 };
  104. const SYSTEMTIME c_stArmageddon = { 9999, 12, 0, 31, 23, 59, 59, 999 };
  105. BOOL InitDateClasses(HINSTANCE hinst)
  106. {
  107. WNDCLASS wc;
  108. BOOL fRegistered = FALSE;
  109. wc.style = CS_GLOBALCLASS;
  110. wc.lpfnWndProc = MonthCalWndProc;
  111. wc.cbClsExtra = 0;
  112. wc.cbWndExtra = sizeof(LPVOID);
  113. wc.hInstance = hinst;
  114. wc.hIcon = NULL;
  115. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  116. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  117. wc.lpszMenuName = NULL;
  118. wc.lpszClassName = g_rgchMCName;
  119. if (RegisterClass(&wc) || (GetLastError() == ERROR_CLASS_ALREADY_EXISTS))
  120. {
  121. wc.lpfnWndProc = DatePickWndProc;
  122. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  123. wc.lpszClassName = g_rgchDTPName;
  124. fRegistered = (RegisterClass(&wc) || (GetLastError() == ERROR_CLASS_ALREADY_EXISTS));
  125. }
  126. return fRegistered;
  127. }
  128. ////////////////////////////////////////////////////////////////////////////
  129. //
  130. // MonthCal stuff
  131. //
  132. ////////////////////////////////////////////////////////////////////////////
  133. ////////////////////////////////////
  134. //
  135. // MCInsert/RemoveMarkers
  136. //
  137. // QuickSummary: Convert the string "MMMM yyyy" into "\1MMMM\3 \2yyyy\4".
  138. //
  139. // In order to lay out the month/year info in the header, we have to be
  140. // able to extract the month and year out of the formatted string so we
  141. // know what their rectangles are. We do this by wrapping the month and
  142. // year inserts with markers so we can extract them after formatting.
  143. //
  144. // Since \1 through \4 are control characters, they won't conflict with
  145. // displayable characters in the actual format string. And just to play it
  146. // safe, if we actually see a format character, we erase it from the string.
  147. //
  148. // MCInsertMarkers inserts the markers into the output string so we can
  149. // extract the substrings later. Quotation marks are funky since you can
  150. // write a format of "'The' mm'''th month of' yyyy". Note that a simple
  151. // even-odd test works for detecting whether we are inside or outside
  152. // quotation marks, even in the nested quotation mark case.
  153. //
  154. void MCInsertMarkers(LPTSTR pszOut, LPCTSTR pszIn)
  155. {
  156. BOOL fInQuote = FALSE;
  157. UINT flSeen = 0;
  158. UINT flThis;
  159. for (;;)
  160. {
  161. TCHAR ch = *pszIn;
  162. switch (ch) {
  163. // At end of string, terminate the output buffer and go home
  164. case TEXT('\0'):
  165. *pszOut = TEXT('\0');
  166. return;
  167. case TEXT('m'):
  168. case TEXT('M'):
  169. flThis = IMM_MONTHSTART;
  170. goto CheckMarker;
  171. case TEXT('y'):
  172. flThis = IMM_YEARSTART;
  173. goto CheckMarker;
  174. CheckMarker:
  175. // If inside a quotation mark or we've already done this guy,
  176. // then just treat it as a regular character.
  177. if (fInQuote || (flSeen & flThis))
  178. goto CopyChar;
  179. flSeen |= flThis;
  180. *pszOut++ = (TCHAR)flThis;
  181. // Don't need to use CharNext because we know *pszIn is "m" "M" or "y"
  182. for ( ; *pszIn == ch; pszIn++)
  183. {
  184. *pszOut++ = ch;
  185. }
  186. *pszOut++ = (TCHAR)(flThis + DMM_STARTEND);
  187. // Restart the loop so we re-parse the character at *pszIn
  188. continue;
  189. // Toggle the quotation mark gizmo if we see one, and then just
  190. // copy it.
  191. case '\'':
  192. fInQuote ^= TRUE;
  193. goto CopyChar;
  194. //
  195. // Don't let these sneak into the output format or it
  196. // will confuse us.
  197. //
  198. case IMM_MONTHSTART:
  199. case IMM_YEARSTART:
  200. case IMM_MONTHEND:
  201. case IMM_YEAREND:
  202. break;
  203. default:
  204. CopyChar:
  205. *pszOut++ = ch;
  206. break;
  207. }
  208. pszIn++; // We handled the DBCS case already
  209. }
  210. // NOTREACHED
  211. }
  212. //
  213. // MCRemoveMarkers hunts down the marker characters and strips them out,
  214. // recording their locations in the optional MONTHMETRICS (as character
  215. // indices).
  216. //
  217. void MCRemoveMarkers(LPTSTR pszBuf, PMONTHMETRICS pmm)
  218. {
  219. int iWrite, iRead;
  220. //
  221. // If by some horrid error we can't find our markers, just pretend
  222. // they were at the start of the string.
  223. //
  224. if (pmm) {
  225. pmm->rgi[IMM_MONTHSTART] = 0;
  226. pmm->rgi[IMM_YEARSTART ] = 0;
  227. pmm->rgi[IMM_MONTHEND ] = 0;
  228. pmm->rgi[IMM_YEAREND ] = 0;
  229. }
  230. iWrite = iRead = 0;
  231. for (;;)
  232. {
  233. TCHAR ch = pszBuf[iRead];
  234. switch (ch)
  235. {
  236. // At end of string, terminate the output buffer and go home
  237. case TEXT('\0'):
  238. pszBuf[iWrite] = TEXT('\0');
  239. return;
  240. // If we find a marker, eat it and remember its location
  241. case IMM_MONTHSTART:
  242. case IMM_YEARSTART:
  243. case IMM_MONTHEND:
  244. case IMM_YEAREND:
  245. if (pmm)
  246. pmm->rgi[ch] = iWrite;
  247. break;
  248. // Otherwise, just copy it to the output
  249. default:
  250. pszBuf[iWrite++] = ch;
  251. break;
  252. }
  253. iRead++;
  254. }
  255. // NOTREACHED
  256. }
  257. ////////////////////////////////////
  258. //
  259. // Like LocalizedLoadString, except that we get the string from
  260. // LOCAL_USER_DEFAULT instead of GetUserDefaultUILanguage().
  261. //
  262. // LOCALE_USER_DEFAULT is the same as GetUserDefaultLCID(), and
  263. // LANGIDFROMLCID(GetUserDefaultLCID()) is the same as GetUserDefaultLangID().
  264. //
  265. // So we pass GetUserDefaultLangID() as the language.
  266. //
  267. int MCLoadString(UINT uID, LPWSTR lpBuffer, int nBufferMax)
  268. {
  269. return CCLoadStringEx(uID, lpBuffer, nBufferMax, GetUserDefaultLangID());
  270. }
  271. ////////////////////////////////////
  272. //
  273. // Get the localized calendar info
  274. //
  275. BOOL UpdateLocaleInfo(MONTHCAL* pmc, LPLOCALEINFO pli)
  276. {
  277. int i;
  278. TCHAR szBuf[64];
  279. int cch;
  280. LPTSTR pc = szBuf;
  281. //
  282. // Get information about the calendar (e.g., is it supported?)
  283. //
  284. MCGetCalendarInfo(&pmc->ct);
  285. //
  286. // Check if the calendar title is an RTL string
  287. //
  288. GetDateFormat(pmc->ct.lcid, 0, NULL, TEXT("MMMM"), szBuf, ARRAYSIZE(szBuf));
  289. pmc->fHeaderRTL = (WORD) MCIsDateStringRTL(szBuf[0]);
  290. //
  291. // get the short date format and sniff it to see if it displays the year
  292. // or month first
  293. //
  294. MCLoadString(IDS_MONTHFMT, pli->szMonthFmt, ARRAYSIZE(pli->szMonthFmt));
  295. //
  296. // Try to get the MONTHYEAR format from NLS. If not supported by NLS,
  297. // then use the hard-coded value in our resources. Note that we
  298. // subtract 4 from the buffer size because we may insert up to four
  299. // marker characters.
  300. //
  301. COMPILETIME_ASSERT(ARRAYSIZE(szBuf) >= ARRAYSIZE(pli->szMonthYearFmt));
  302. szBuf[0] = TEXT('\0');
  303. GetLocaleInfo(pmc->ct.lcid, LOCALE_SYEARMONTH,
  304. szBuf, ARRAYSIZE(pli->szMonthYearFmt) - CCH_MARKERS);
  305. if (!szBuf[0]) {
  306. MCLoadString(IDS_MONTHYEARFMT, szBuf, ARRAYSIZE(pli->szMonthYearFmt) - CCH_MARKERS);
  307. }
  308. MCInsertMarkers(pli->szMonthYearFmt, szBuf);
  309. //
  310. // REARCHITECT this code needs to change to use CAL_ values when we
  311. // want to support multiple calendars.
  312. //
  313. //
  314. // Get the month names
  315. //
  316. for (i = 0; i < 12; i++)
  317. {
  318. cch = GetLocaleInfo(pmc->ct.lcid, LOCALE_SMONTHNAME1 + i,
  319. pli->rgszMonth[i], CCHMAXMONTH);
  320. if (cch == 0)
  321. // the calendar is pretty useless without month names...
  322. return(FALSE);
  323. }
  324. //
  325. // Get the days of the week
  326. //
  327. for (i = 0; i < 7; i++)
  328. {
  329. cch = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i,
  330. pli->rgszDay[i], CCHMAXABBREVDAY);
  331. if (cch == 0)
  332. // the calendar is pretty useless without day names...
  333. return(FALSE);
  334. }
  335. //
  336. // If we haven't already set what the first day of the week is, get the
  337. // localized setting.
  338. //
  339. if (!pmc->fFirstDowSet)
  340. {
  341. cch = GetLocaleInfo(pmc->ct.lcid, LOCALE_IFIRSTDAYOFWEEK, szBuf, ARRAYSIZE(szBuf));
  342. if (cch > 0)
  343. pli->dowStartWeek = szBuf[0] - TEXT('0');
  344. }
  345. //
  346. // Get the first week of the year
  347. //
  348. cch = GetLocaleInfo(pmc->ct.lcid, LOCALE_IFIRSTWEEKOFYEAR, szBuf, ARRAYSIZE(szBuf));
  349. if (cch > 0)
  350. pli->firstWeek = szBuf[0] - TEXT('0');
  351. // Set up pointers
  352. for (i = 0; i < 12; i++)
  353. pli->rgpszMonth[i] = pli->rgszMonth[i];
  354. for (i = 0; i < 7; i++)
  355. pli->rgpszDay[i] = pli->rgszDay[i];
  356. // Get static strings
  357. MCLoadString(IDS_TODAY, pli->szToday, ARRAYSIZE(pli->szToday));
  358. MCLoadString(IDS_GOTOTODAY, pli->szGoToToday, ARRAYSIZE(pli->szGoToToday));
  359. // if we've been initialized
  360. if (pmc->hinstance)
  361. {
  362. SYSTEMTIME st;
  363. CopyDate(pmc->stMonthFirst, st);
  364. MCUpdateStartEndDates(pmc, &st);
  365. }
  366. return(TRUE);
  367. }
  368. void MCReloadMenus(MONTHCAL *pmc)
  369. {
  370. int i;
  371. if (pmc->hmenuCtxt)
  372. DestroyMenu(pmc->hmenuCtxt);
  373. if (pmc->hmenuMonth)
  374. DestroyMenu(pmc->hmenuMonth);
  375. pmc->hmenuCtxt = CreatePopupMenu();
  376. if (pmc->hmenuCtxt)
  377. AppendMenu(pmc->hmenuCtxt, MF_STRING, 1, pmc->li.szGoToToday);
  378. pmc->hmenuMonth = CreatePopupMenu();
  379. if (pmc->hmenuMonth)
  380. {
  381. for (i = 0; i < 12; i++)
  382. AppendMenu(pmc->hmenuMonth, MF_STRING, i + 1, pmc->li.rgszMonth[i]);
  383. }
  384. }
  385. BOOL MCHandleEraseBkgnd(MONTHCAL* pmc, HDC hdc)
  386. {
  387. RECT rc;
  388. GetClipBox(hdc, &rc);
  389. FillRectClr(hdc, &rc, pmc->clr[MCSC_BACKGROUND]);
  390. return TRUE;
  391. }
  392. LRESULT MCHandleHitTest(MONTHCAL* pmc, PMCHITTESTINFO phti)
  393. {
  394. int iMonth;
  395. RECT rc;
  396. if (!phti || phti->cbSize != sizeof(MCHITTESTINFO))
  397. return -1;
  398. phti->uHit = MCHT_NOWHERE;
  399. MCGetTodayBtnRect(pmc, &rc);
  400. if (PtInRect(&rc, phti->pt) && MonthCal_ShowToday(pmc))
  401. {
  402. phti->uHit = MCHT_TODAYLINK;
  403. }
  404. else if (pmc->fSpinPrev = (WORD) PtInRect(&pmc->rcPrev, phti->pt))
  405. {
  406. phti->uHit = MCHT_TITLEBTNPREV;
  407. }
  408. else if (PtInRect(&pmc->rcNext, phti->pt))
  409. {
  410. phti->uHit = MCHT_TITLEBTNNEXT;
  411. }
  412. else if (FGetOffsetForPt(pmc, phti->pt, &iMonth))
  413. {
  414. RECT rcMonth; // bounding rect for month containg phti->pt
  415. POINT ptRel; // relative point in a month
  416. int month;
  417. int year;
  418. MCGetRcForMonth(pmc, iMonth, &rcMonth);
  419. ptRel.x = phti->pt.x - rcMonth.left;
  420. ptRel.y = phti->pt.y - rcMonth.top;
  421. GetYrMoForOffset(pmc, iMonth, &year, &month);
  422. phti->st.wMonth = (WORD) month;
  423. phti->st.wYear = (WORD) year;
  424. //
  425. // if calendar is showing week numbers and the point lies in the
  426. // the week numbers, get the date for day immediately to the right
  427. // of the week number containing the point
  428. //
  429. if (MonthCal_ShowWeekNumbers(pmc) && PtInRect(&pmc->rcWeekNum, ptRel))
  430. {
  431. phti->uHit |= MCHT_CALENDARWEEKNUM;
  432. phti->pt.x += pmc->rcDayNum.left;
  433. FGetDateForPt(pmc, phti->pt, &phti->st, NULL, NULL, NULL, NULL);
  434. }
  435. //
  436. // if the point lies in the days of the week header, then return
  437. // the day of the week containing the point
  438. //
  439. else if (PtInRect(&pmc->rcDow, ptRel))
  440. {
  441. int iRow;
  442. int iCol;
  443. phti->uHit |= MCHT_CALENDARDAY;
  444. ptRel.y = pmc->rcDayNum.top;
  445. FGetRowColForRelPt(pmc, ptRel, &iRow, &iCol);
  446. phti->st.wDayOfWeek = (WORD) iCol;
  447. }
  448. //
  449. // if the point lies in the actually calendar part, then return the
  450. // date containg the point
  451. //
  452. else if (PtInRect(&pmc->rcDayNum, ptRel))
  453. {
  454. int iDay;
  455. // we're in the calendar part!
  456. phti->uHit |= MCHT_CALENDAR;
  457. if (FGetDateForPt(pmc, phti->pt, &phti->st, &iDay, NULL, NULL, NULL))
  458. {
  459. phti->uHit |= MCHT_CALENDARDATE;
  460. // if it was beyond the bounds of the days we're showing
  461. // and also FGetDateForPt returns TRUE, then we're on the boundary
  462. // of the displayed months
  463. if (iDay <= 0)
  464. {
  465. phti->uHit |= MCHT_PREV;
  466. }
  467. else if (iDay > pmc->rgcDay[iMonth + 1])
  468. {
  469. phti->uHit |= MCHT_NEXT;
  470. }
  471. }
  472. }
  473. else
  474. {
  475. RECT rcMonthTitle;
  476. RECT rcYearTitle;
  477. // otherwise we're in the title
  478. phti->uHit |= MCHT_TITLE;
  479. MCGetTitleRcsForOffset(pmc, iMonth, &rcMonthTitle, &rcYearTitle);
  480. if (PtInRect(&rcMonthTitle, phti->pt))
  481. {
  482. phti->uHit |= MCHT_TITLEMONTH;
  483. }
  484. else if (PtInRect(&rcYearTitle, phti->pt))
  485. {
  486. phti->uHit |= MCHT_TITLEYEAR;
  487. }
  488. }
  489. }
  490. DebugMsg(TF_MONTHCAL, TEXT("mc: Hittest returns : %d %d %d %d)"),
  491. (int)phti->st.wDay,
  492. (int)phti->st.wMonth,
  493. (int)phti->st.wYear,
  494. (int)phti->st.wDayOfWeek
  495. );
  496. return phti->uHit;
  497. }
  498. void MonthCal_OnPaint(MONTHCAL *pmc, HDC hdc)
  499. {
  500. if (hdc)
  501. {
  502. MCPaint(pmc, hdc);
  503. }
  504. else
  505. {
  506. PAINTSTRUCT ps;
  507. hdc = BeginPaint(pmc->ci.hwnd, &ps);
  508. MCPaint(pmc, hdc);
  509. EndPaint(pmc->ci.hwnd, &ps);
  510. }
  511. }
  512. BOOL MCGetDateFormatWithTempYear(PCALENDARTYPE pct, SYSTEMTIME *pst, LPCTSTR pszFormat, UINT uYear, LPTSTR pszBuf, UINT cchBuf)
  513. {
  514. BOOL fRc;
  515. WORD wYear = pst->wYear;
  516. pst->wYear = (WORD)uYear;
  517. fRc = GetDateFormat(pct->lcid, 0, pst, pszFormat, pszBuf, cchBuf);
  518. if (!fRc)
  519. {
  520. // AIGH! I hate Feburary 29. In case we are Feb 29 1996 and the
  521. // user changes to a non-leap year, force the day to something valid
  522. // in February 1997 (or whatever year the user finally picked).
  523. //
  524. // We can't blindly smash the day to 1 because the era might change
  525. // in the middle of the month.
  526. WORD wDay = pst->wDay;
  527. ASSERT(pst->wDay == 29);
  528. pst->wDay = 28;
  529. fRc = GetDateFormat(pct->lcid, 0, pst, pszFormat, pszBuf, cchBuf);
  530. pst->wDay = wDay;
  531. }
  532. pst->wYear = wYear;
  533. return fRc;
  534. }
  535. void MCUpdateEditYear(MONTHCAL *pmc)
  536. {
  537. TCHAR rgch[64];
  538. ASSERT(pmc->hwndEdit);
  539. EVAL(MCGetDateFormatWithTempYear(&pmc->ct, &pmc->st, TEXT("yyyy"), pmc->st.wYear, rgch, ARRAYSIZE(rgch)));
  540. SendMessage(pmc->hwndEdit, WM_SETTEXT, 0, (LPARAM)rgch);
  541. }
  542. LRESULT CALLBACK MonthCalWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  543. {
  544. MONTHCAL *pmc;
  545. LRESULT lres = 0;
  546. if (uMsg == WM_NCCREATE)
  547. return(MCNcCreateHandler(hwnd));
  548. pmc = MonthCal_GetPtr(hwnd);
  549. if (pmc == NULL)
  550. return(DefWindowProc(hwnd, uMsg, wParam, lParam));
  551. // Dispatch the various messages we can receive
  552. switch (uMsg)
  553. {
  554. case WM_CREATE:
  555. lres = MCCreateHandler(pmc, hwnd, (LPCREATESTRUCT)lParam);
  556. break;
  557. HANDLE_MSG(pmc, WM_ERASEBKGND, MCHandleEraseBkgnd);
  558. case WM_PRINTCLIENT:
  559. case WM_PAINT:
  560. MonthCal_OnPaint(pmc, (HDC)wParam);
  561. return(0);
  562. case WM_KEYDOWN:
  563. MCHandleKeydown(pmc, wParam, lParam);
  564. break;
  565. case WM_KEYUP:
  566. switch (wParam)
  567. {
  568. case VK_CONTROL:
  569. pmc->fControl = FALSE;
  570. break;
  571. case VK_SHIFT:
  572. pmc->fShift = FALSE;
  573. break;
  574. }
  575. break;
  576. case WM_CONTEXTMENU:
  577. MCContextMenu(pmc, wParam, lParam);
  578. break;
  579. case WM_LBUTTONDOWN:
  580. MCLButtonDown(pmc, wParam, lParam);
  581. break;
  582. case WM_LBUTTONUP:
  583. MCLButtonUp(pmc, wParam, lParam);
  584. break;
  585. case WM_MOUSEMOVE:
  586. MCMouseMove(pmc, wParam, lParam);
  587. break;
  588. case WM_GETFONT:
  589. lres = (LRESULT)pmc->hfont;
  590. break;
  591. case WM_SETFONT:
  592. MCHandleSetFont(pmc, (HFONT)wParam, (BOOL)LOWORD(lParam));
  593. MCSizeHandler(pmc, &pmc->rc);
  594. MCUpdateMonthNamePos(pmc);
  595. break;
  596. case WM_TIMER:
  597. MCHandleTimer(pmc, wParam);
  598. break;
  599. case WM_NCDESTROY:
  600. MCNcDestroyHandler(hwnd, pmc, wParam, lParam);
  601. break;
  602. case WM_ENABLE:
  603. {
  604. BOOL fEnable = wParam ? TRUE:FALSE;
  605. if (pmc->fEnabled != fEnable)
  606. {
  607. pmc->fEnabled = (WORD) fEnable;
  608. InvalidateRect(pmc->ci.hwnd, NULL, TRUE);
  609. }
  610. break;
  611. }
  612. case MCMP_WINDOWPOSCHANGED:
  613. case WM_SIZE:
  614. {
  615. RECT rc;
  616. if (uMsg==MCMP_WINDOWPOSCHANGED)
  617. {
  618. GetClientRect(pmc->ci.hwnd, &rc);
  619. }
  620. else
  621. {
  622. rc.left = 0;
  623. rc.top = 0;
  624. rc.right = GET_X_LPARAM(lParam);
  625. rc.bottom = GET_Y_LPARAM(lParam);
  626. }
  627. lres = MCSizeHandler(pmc, &rc);
  628. break;
  629. }
  630. case WM_CANCELMODE:
  631. PostMessage(pmc->ci.hwnd, WM_LBUTTONUP, 0, 0xFFFFFFFF);
  632. break;
  633. case WM_SYSCOLORCHANGE:
  634. InitGlobalColors();
  635. break;
  636. case WM_WININICHANGE:
  637. InitGlobalMetrics(wParam);
  638. if (lParam == 0 ||
  639. !lstrcmpi((LPTSTR)lParam, TEXT("Intl"))
  640. )
  641. {
  642. UpdateLocaleInfo(pmc, &pmc->li);
  643. MCReloadMenus(pmc);
  644. InvalidateRect(hwnd, NULL, TRUE);
  645. wParam = 0; // force MCCalcSizes to happen
  646. }
  647. if (wParam == 0 || wParam == SPI_SETNONCLIENTMETRICS)
  648. {
  649. MCCalcSizes(pmc);
  650. PostMessage(pmc->ci.hwnd, MCMP_WINDOWPOSCHANGED, 0, 0);
  651. }
  652. break;
  653. case WM_THEMECHANGED:
  654. if (pmc->hThemeScroll)
  655. {
  656. CloseThemeData(pmc->hThemeScroll);
  657. }
  658. pmc->hThemeScroll = OpenThemeData(pmc->ci.hwnd, L"ScrollBar");
  659. InvalidateRect(pmc->ci.hwnd, NULL, TRUE);
  660. break;
  661. case WM_NOTIFYFORMAT:
  662. return CIHandleNotifyFormat(&pmc->ci, lParam);
  663. break;
  664. case WM_STYLECHANGING:
  665. lres = MCOnStyleChanging(pmc, (UINT) wParam, (LPSTYLESTRUCT)lParam);
  666. break;
  667. case WM_STYLECHANGED:
  668. lres = MCOnStyleChanged(pmc, (UINT) wParam, (LPSTYLESTRUCT)lParam);
  669. break;
  670. case WM_NOTIFY: {
  671. LPNMHDR pnm = (LPNMHDR)lParam;
  672. switch (pnm->code)
  673. {
  674. case UDN_DELTAPOS:
  675. if (pnm->hwndFrom == pmc->hwndUD)
  676. {
  677. // A notification from the UpDown control buddied
  678. // with the currently popped up monthcal, adjust the
  679. // edit box appropriately. We use UDN_DELTAPOS instad
  680. // of WM_VSCROLL because we care only about the delta and
  681. // not the absolute number. The absolute number causes us
  682. // problems in localized calendars.
  683. LPNM_UPDOWN pnmdp = (LPNM_UPDOWN)lParam;
  684. UINT yr = pmc->st.wYear + pnmdp->iDelta;
  685. UINT yrMin, yrMax;
  686. int delta;
  687. yrMin = pmc->stMin.wYear;
  688. if (yr < yrMin)
  689. yr = yrMin;
  690. yrMax = pmc->stMax.wYear;
  691. if (yr > yrMax)
  692. yr = yrMax;
  693. delta = yr - pmc->st.wYear;
  694. pmc->st.wYear = (WORD)yr;
  695. if (delta) {
  696. MCIncrStartMonth(pmc, delta * 12, FALSE);
  697. MCNotifySelChange(pmc,MCN_SELCHANGE);
  698. }
  699. }
  700. break;
  701. }
  702. } // WM_NOTIFY switch
  703. break;
  704. case WM_VSCROLL:
  705. // this must be coming from our UpDown control buddied
  706. // with the currently popped up monthcal, adjust the
  707. // edit box appropriately
  708. // We must do this on WM_VSCROLL rather than UDN_DELTAPOS
  709. // since we need to fix the selection after the updown mangled it
  710. MCUpdateEditYear(pmc);
  711. break;
  712. //
  713. // MONTHCAL specific messages
  714. //
  715. // MCM_GETCURSEL wParam=void lParam=LPSYSTEMTIME
  716. // sets *lParam to the currently selected SYSTEMTIME
  717. // returns TRUE on success, FALSE on error (such as multi-select MONTHCAL)
  718. case MCM_GETCURSEL:
  719. if (!MonthCal_IsMultiSelect(pmc))
  720. {
  721. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  722. if (pst)
  723. {
  724. ZeroMemory(pst, SIZEOF(SYSTEMTIME));
  725. // Bug#94368 raymondc v6. Need to zero out the time fields instead of
  726. // setting them to garbage. This confuses MFC.
  727. *pst = pmc->st;
  728. pst->wDayOfWeek = (DowFromDate(pst)+1) % 7; // this returns 0==sun
  729. lres = 1;
  730. }
  731. }
  732. break;
  733. // MCM_SETCURSEL wParam=void lParam=LPSYSTEMTIME
  734. // sets the currently selected SYSTEMTIME to *lParam
  735. // returns TRUE on success, FALSE on error (such as multi-select MONTHCAL or bad parameters)
  736. case MCM_SETCURSEL:
  737. {
  738. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  739. if (MonthCal_IsMultiSelect(pmc) ||
  740. !IsValidDate(pst))
  741. {
  742. break;
  743. }
  744. if (0 == CmpDate(pst, &pmc->st))
  745. {
  746. // if no change, just return
  747. lres = 1;
  748. break;
  749. }
  750. pmc->rcDayOld = pmc->rcDayCur;
  751. pmc->fNoNotify = TRUE;
  752. lres = MCSetDate(pmc, pst);
  753. pmc->fNoNotify = FALSE;
  754. if (lres)
  755. {
  756. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayOld, FALSE); // erase old highlight
  757. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayCur, FALSE); // draw new highlight
  758. }
  759. UpdateWindow(pmc->ci.hwnd);
  760. break;
  761. }
  762. // MCM_GETMAXSELCOUNT wParam=void lParam=void
  763. // returns the max number of selected days allowed
  764. case MCM_GETMAXSELCOUNT:
  765. lres = (LRESULT)(MonthCal_IsMultiSelect(pmc) ? pmc->cSelMax : 1);
  766. break;
  767. // MCM_SETMAXSELCOUNT wParam=int lParam=void
  768. // sets the maximum selectable date range to wParam days
  769. // returns TRUE on success, FALSE on error (such as single-select MONTHCAL)
  770. case MCM_SETMAXSELCOUNT:
  771. if (!MonthCal_IsMultiSelect(pmc) || (int)wParam < 1)
  772. break;
  773. pmc->cSelMax = (int)wParam;
  774. lres = 1;
  775. break;
  776. // MCM_GETSELRANGE wParam=void lParam=LPSYSTEMTIME[2]
  777. // sets *lParam to the first date of the range, *(lParam+1) to the second date
  778. // returns TRUE on success, FALSE otherwise (such as single-select MONTHCAL)
  779. case MCM_GETSELRANGE:
  780. {
  781. LPSYSTEMTIME pst;
  782. pst = (LPSYSTEMTIME)lParam;
  783. if (!pst)
  784. break;
  785. ZeroMemory(pst, 2*SIZEOF(SYSTEMTIME));
  786. if (!MonthCal_IsMultiSelect(pmc))
  787. break;
  788. *pst = pmc->st;
  789. pst->wDayOfWeek = (DowFromDate(pst)+1) % 7; // this returns 0==sun
  790. pst++;
  791. *pst = pmc->stEndSel;
  792. pst->wDayOfWeek = (DowFromDate(pst)+1) % 7; // this returns 0==sun
  793. lres = 1;
  794. break;
  795. }
  796. // MCM_SETSELRANGE wParam=void lParam=LPSYSTEMTIME[2]
  797. // sets the currently selected day range to *lparam to *(lParam+1)
  798. // returns TRUE on success, FALSE otherwise (such as single-select MONTHCAL or bad params)
  799. case MCM_SETSELRANGE:
  800. {
  801. LPSYSTEMTIME pstStart = (LPSYSTEMTIME)lParam;
  802. LPSYSTEMTIME pstEnd = &pstStart[1];
  803. SYSTEMTIME stStart;
  804. SYSTEMTIME stEnd;
  805. if (!MonthCal_IsMultiSelect(pmc) ||
  806. !IsValidDate(pstStart) ||
  807. !IsValidDate(pstEnd))
  808. break;
  809. // IE3 shipped without validating the time portion of this message.
  810. // Make sure our stored systemtimes are always valid (so we will
  811. // always give out valid systemtime structs).
  812. //
  813. if (!IsValidTime(pstStart))
  814. CopyTime(pmc->st, *pstStart);
  815. if (!IsValidTime(pstEnd))
  816. CopyTime(pmc->stEndSel, *pstEnd);
  817. if (CmpDate(pstStart, pstEnd) > 0)
  818. {
  819. stEnd = *pstStart;
  820. stStart = *pstEnd;
  821. pstStart = &stStart;
  822. pstEnd = &stEnd;
  823. }
  824. if (CmpDate(pstStart, &pmc->stMin) < 0)
  825. break;
  826. if (CmpDate(pstEnd, &pmc->stMax) > 0)
  827. break;
  828. if (DaysBetweenDates(pstStart, pstEnd) >= pmc->cSelMax)
  829. break;
  830. if (0 == CmpDate(pstStart, &pmc->st) &&
  831. 0 == CmpDate(pstEnd, &pmc->stEndSel))
  832. {
  833. // if no change, just return
  834. lres = 1;
  835. break;
  836. }
  837. pmc->stStartPrev = pmc->st;
  838. pmc->stEndPrev = pmc->stEndSel;
  839. pmc->fNoNotify = TRUE;
  840. lres = MCSetDate(pmc, pstEnd);
  841. if (lres)
  842. {
  843. pmc->st = *pstStart;
  844. pmc->stEndSel = *pstEnd;
  845. MCInvalidateDates(pmc, &pmc->stStartPrev, &pmc->stEndPrev);
  846. MCInvalidateDates(pmc, &pmc->st, &pmc->stEndSel);
  847. UpdateWindow(pmc->ci.hwnd);
  848. }
  849. pmc->fNoNotify = FALSE;
  850. break;
  851. }
  852. // MCM_GETMONTHRANGE wParam=GMR_flags lParam=LPSYSTEMTIME[2]
  853. // if GMR_VISIBLE, returns the range of selectable (non-grayed) displayed
  854. // days. if GMR_DAYSTATE, returns the range of every (incl grayed) days.
  855. // returns the number of months the above range spans.
  856. case MCM_GETMONTHRANGE:
  857. {
  858. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  859. if (pst)
  860. {
  861. ZeroMemory(pst, 2*SIZEOF(SYSTEMTIME));
  862. if (wParam == GMR_VISIBLE)
  863. {
  864. pst[0] = pmc->stMonthFirst;
  865. pst[1] = pmc->stMonthLast;
  866. }
  867. else if (wParam == GMR_DAYSTATE)
  868. {
  869. pst[0] = pmc->stViewFirst;
  870. pst[1] = pmc->stViewLast;
  871. }
  872. }
  873. lres = (LRESULT)pmc->nMonths;
  874. if (wParam == GMR_DAYSTATE)
  875. lres += 2;
  876. break;
  877. }
  878. // MCM_SETDAYSTATE wParam=int lParam=LPDAYSTATE
  879. // updates the MONTHCAL's DAYSTATE, only for MONTHCALs with DAYSTATE enabled
  880. // the range of months represented in the DAYSTATE array passed in lParam
  881. // should match that of the MONTHCAL
  882. // wParam count of items in DAYSTATE array
  883. // lParam pointer to array of DAYSTATE items
  884. // returns FALSE if not DAYSTATE enabled or if an error occurs, TRUE otherwise
  885. case MCM_SETDAYSTATE:
  886. {
  887. MONTHDAYSTATE *pmds = (MONTHDAYSTATE *)lParam;
  888. int i;
  889. if (!MonthCal_IsDayState(pmc) ||
  890. (int)wParam != (pmc->nMonths + 2))
  891. break;
  892. for (i = 0; i < (int)wParam; i++)
  893. {
  894. pmc->rgdayState[i] = *pmds;
  895. pmds++;
  896. }
  897. MCInvalidateMonthDays(pmc);
  898. lres = 1;
  899. break;
  900. }
  901. // MCM_GETMINREQRECT wParam=void lParam=LPRECT
  902. // sets *lParam to the minimum size required to display one month in full.
  903. // Note: this is dependent upon the currently selected font.
  904. // Apps can take the returned size and double the width to get two calendars
  905. // displayed.
  906. case MCM_GETMINREQRECT:
  907. {
  908. LPRECT prc = (LPRECT)lParam;
  909. prc->left = 0;
  910. prc->top = 0;
  911. prc->right = pmc->dxMonth;
  912. prc->bottom = pmc->dyMonth;
  913. if (MonthCal_ShowToday(pmc))
  914. {
  915. prc->bottom += pmc->dyToday;
  916. }
  917. AdjustWindowRect(prc, pmc->ci.style, FALSE);
  918. // This is a bogus message, lParam should really be LPSIZE.
  919. // Make sure left and top are 0 (AdjustWindowRect will make these negative).
  920. prc->right -= prc->left;
  921. prc->bottom -= prc->top;
  922. prc->left = 0;
  923. prc->top = 0;
  924. lres = 1;
  925. break;
  926. }
  927. // MCM_GETMAXTODAYWIDTH wParam=void lParam=LPDWORD
  928. // sets *lParam to the width of the "today" string, so apps
  929. // can figure out how big to make the calendar (max of MCM_GETMINREQRECT
  930. // and MCM_GETMAXTODAYWIDTH).
  931. case MCM_GETMAXTODAYWIDTH:
  932. {
  933. RECT rc;
  934. rc.left = 0;
  935. rc.top = 0;
  936. rc.right = pmc->dxToday;
  937. rc.bottom = pmc->dyToday;
  938. AdjustWindowRect(&rc, pmc->ci.style, FALSE);
  939. lres = rc.right - rc.left;
  940. break;
  941. }
  942. case MCM_HITTEST:
  943. return MCHandleHitTest(pmc, (PMCHITTESTINFO)lParam);
  944. case MCM_SETCOLOR:
  945. if (wParam < MCSC_COLORCOUNT)
  946. {
  947. COLORREF clr = pmc->clr[wParam];
  948. pmc->clr[wParam] = (COLORREF)lParam;
  949. InvalidateRect(hwnd, NULL, wParam == MCSC_BACKGROUND);
  950. return clr;
  951. }
  952. return -1;
  953. case MCM_GETCOLOR:
  954. if (wParam < MCSC_COLORCOUNT)
  955. return pmc->clr[wParam];
  956. return -1;
  957. case MCM_SETFIRSTDAYOFWEEK:
  958. {
  959. lres = MAKELONG(pmc->li.dowStartWeek, (BOOL)pmc->fFirstDowSet);
  960. if (lParam == (LPARAM)-1) {
  961. pmc->fFirstDowSet = FALSE;
  962. } else if (lParam < 7) {
  963. pmc->fFirstDowSet = TRUE;
  964. pmc->li.dowStartWeek = (TCHAR)lParam;
  965. }
  966. UpdateLocaleInfo(pmc, &pmc->li);
  967. InvalidateRect(hwnd, NULL, FALSE);
  968. return lres;
  969. }
  970. case MCM_GETFIRSTDAYOFWEEK:
  971. return MAKELONG(pmc->li.dowStartWeek, (BOOL)pmc->fFirstDowSet);
  972. case MCM_SETTODAY:
  973. MCSetToday(pmc, (SYSTEMTIME*)lParam);
  974. break;
  975. case MCM_GETTODAY:
  976. if (lParam) {
  977. *((SYSTEMTIME*)lParam) = pmc->stToday;
  978. return TRUE;
  979. }
  980. return FALSE;
  981. case MCM_GETRANGE:
  982. if (lParam)
  983. {
  984. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  985. ZeroMemory(pst, 2*SIZEOF(SYSTEMTIME));
  986. ASSERT(lres == 0);
  987. if (pmc->fMinYrSet)
  988. {
  989. pst[0] = pmc->stMin;
  990. lres = GDTR_MIN;
  991. }
  992. if (pmc->fMaxYrSet)
  993. {
  994. pst[1] = pmc->stMax;
  995. lres |= GDTR_MAX;
  996. }
  997. }
  998. break;
  999. case MCM_SETRANGE:
  1000. if (lParam)
  1001. {
  1002. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  1003. if (((wParam & GDTR_MIN) && !IsValidDate(pst)) ||
  1004. ((wParam & GDTR_MAX) && !IsValidDate(&pst[1])))
  1005. break;
  1006. // IE3 did not validate the time portion of this struct
  1007. // use stToday time fields cuz pmc->stMin/Max may be zero
  1008. if ((wParam & GDTR_MIN) && !IsValidTime(pst))
  1009. CopyTime(pmc->stToday, pst[0]);
  1010. if ((wParam & GDTR_MAX) && !IsValidTime(&pst[1]))
  1011. CopyTime(pmc->stToday, pst[1]);
  1012. if (wParam & GDTR_MIN)
  1013. {
  1014. pmc->stMin = *pst;
  1015. pmc->fMinYrSet = TRUE;
  1016. }
  1017. else
  1018. {
  1019. pmc->stMin = c_stEpoch;
  1020. pmc->fMinYrSet = FALSE;
  1021. }
  1022. pst++;
  1023. if (wParam & GDTR_MAX)
  1024. {
  1025. pmc->stMax = *pst;
  1026. pmc->fMaxYrSet = TRUE;
  1027. }
  1028. else
  1029. {
  1030. pmc->stMax = c_stArmageddon;
  1031. pmc->fMaxYrSet = FALSE;
  1032. }
  1033. if (pmc->fMaxYrSet && pmc->fMinYrSet && CmpDate(&pmc->stMin, &pmc->stMax) > 0)
  1034. {
  1035. SYSTEMTIME stTemp = pmc->stMin;
  1036. pmc->stMin = pmc->stMax;
  1037. pmc->stMax = stTemp;
  1038. }
  1039. lres = TRUE;
  1040. }
  1041. break;
  1042. case MCM_GETMONTHDELTA:
  1043. if (pmc->fMonthDelta)
  1044. lres = pmc->nMonthDelta;
  1045. else
  1046. lres = pmc->nMonths;
  1047. break;
  1048. case MCM_SETMONTHDELTA:
  1049. if (pmc->fMonthDelta)
  1050. lres = pmc->nMonthDelta;
  1051. else
  1052. lres = 0;
  1053. if ((int)wParam==0)
  1054. pmc->fMonthDelta = FALSE;
  1055. else
  1056. {
  1057. pmc->fMonthDelta = TRUE;
  1058. pmc->nMonthDelta = (int)wParam;
  1059. }
  1060. break;
  1061. default:
  1062. if (CCWndProc(&pmc->ci, uMsg, wParam, lParam, &lres))
  1063. return lres;
  1064. lres = DefWindowProc(hwnd, uMsg, wParam, lParam);
  1065. break;
  1066. } /* switch (uMsg) */
  1067. return(lres);
  1068. }
  1069. LRESULT MCNcCreateHandler(HWND hwnd)
  1070. {
  1071. MONTHCAL *pmc;
  1072. // Allocate storage for the dtpick structure
  1073. pmc = (MONTHCAL *)NearAlloc(sizeof(MONTHCAL));
  1074. if (!pmc)
  1075. {
  1076. DebugMsg(DM_WARNING, TEXT("mc: Out Of Near Memory"));
  1077. return(0L);
  1078. }
  1079. MonthCal_SetPtr(hwnd, pmc);
  1080. return(1L);
  1081. }
  1082. void MCInitColorArray(COLORREF* pclr)
  1083. {
  1084. pclr[MCSC_BACKGROUND] = g_clrWindow;
  1085. pclr[MCSC_MONTHBK] = g_clrWindow;
  1086. pclr[MCSC_TEXT] = g_clrWindowText;
  1087. pclr[MCSC_TITLEBK] = GetSysColor(COLOR_ACTIVECAPTION);
  1088. pclr[MCSC_TITLETEXT] = GetSysColor(COLOR_CAPTIONTEXT);
  1089. pclr[MCSC_TRAILINGTEXT] = g_clrGrayText;
  1090. }
  1091. LRESULT MCCreateHandler(MONTHCAL *pmc, HWND hwnd, LPCREATESTRUCT lpcs)
  1092. {
  1093. HFONT hfont;
  1094. SYSTEMTIME st;
  1095. // Validate data
  1096. //
  1097. if (lpcs->style & MCS_INVALIDBITS)
  1098. return(-1);
  1099. CIInitialize(&pmc->ci, hwnd, lpcs);
  1100. UpdateLocaleInfo(pmc, &pmc->li);
  1101. // Initialize our data.
  1102. //
  1103. pmc->hinstance = lpcs->hInstance;
  1104. pmc->fEnabled = !(pmc->ci.style & WS_DISABLED);
  1105. pmc->hbrushToday = CreateSolidBrush(CAL_COLOR_TODAY);
  1106. MCReloadMenus(pmc);
  1107. // Default minimum date is the epoch
  1108. pmc->stMin = c_stEpoch;
  1109. // Default maximum date is armageddon
  1110. pmc->stMax = c_stArmageddon;
  1111. GetLocalTime(&pmc->stToday);
  1112. pmc->st = pmc->stToday;
  1113. if (MonthCal_IsMultiSelect(pmc))
  1114. pmc->stEndSel = pmc->st;
  1115. // make sure the time portions of these are valid. they are never
  1116. // touched after this point
  1117. pmc->stMonthFirst = pmc->st;
  1118. pmc->stMonthLast = pmc->st;
  1119. pmc->stViewFirst = pmc->st;
  1120. pmc->stViewLast = pmc->st;
  1121. pmc->cSelMax = CAL_DEF_SELMAX;
  1122. hfont = NULL;
  1123. if (lpcs->hwndParent)
  1124. hfont = (HFONT)SendMessage(lpcs->hwndParent, WM_GETFONT, 0, 0);
  1125. if (hfont == NULL)
  1126. hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  1127. MCHandleSetFont(pmc, hfont, FALSE);
  1128. CopyDate(pmc->st, st);
  1129. // Can we start at January?
  1130. if (st.wMonth <= (pmc->nViewRows * pmc->nViewCols))
  1131. st.wMonth = 1;
  1132. MCUpdateStartEndDates(pmc, &st);
  1133. pmc->idTimerToday = SetTimer(pmc->ci.hwnd, CAL_TODAYTIMER, CAL_SECTODAYTIMER * 1000, NULL);
  1134. MCInitColorArray(pmc->clr);
  1135. pmc->hThemeScroll = OpenThemeData(pmc->ci.hwnd, L"ScrollBar");
  1136. return(0);
  1137. }
  1138. LRESULT MCOnStyleChanging(MONTHCAL *pmc, UINT gwl, LPSTYLESTRUCT pinfo)
  1139. {
  1140. if (gwl == GWL_STYLE)
  1141. {
  1142. DWORD changeFlags = pmc->ci.style ^ pinfo->styleNew;
  1143. // Don't allow these bits to change
  1144. changeFlags &= MCS_MULTISELECT | MCS_DAYSTATE | MCS_INVALIDBITS;
  1145. pinfo->styleNew ^= changeFlags;
  1146. }
  1147. return(0);
  1148. }
  1149. LRESULT MCOnStyleChanged(MONTHCAL *pmc, UINT gwl, LPSTYLESTRUCT pinfo)
  1150. {
  1151. if (gwl == GWL_STYLE)
  1152. {
  1153. DWORD changeFlags = pmc->ci.style ^ pinfo->styleNew;
  1154. ASSERT(!(changeFlags & (MCS_MULTISELECT|MCS_DAYSTATE|MCS_INVALIDBITS)));
  1155. pmc->ci.style = pinfo->styleNew;
  1156. if (changeFlags & MCS_WEEKNUMBERS)
  1157. {
  1158. MCCalcSizes(pmc);
  1159. MCUpdateRcDayCur(pmc, &pmc->st);
  1160. //MCUpdateToday(pmc);
  1161. }
  1162. // save a touch of code and share the MCUpdateToday
  1163. // call with MCS_WEEKNUMBERS above
  1164. if (changeFlags & (MCS_NOTODAY|MCS_NOTODAYCIRCLE|MCS_WEEKNUMBERS))
  1165. {
  1166. MCUpdateToday(pmc);
  1167. }
  1168. if (changeFlags & (WS_BORDER | WS_CAPTION | WS_THICKFRAME)) {
  1169. // the changing of these bits affect the size of the window
  1170. // but not until after this message is handled
  1171. // so post ourself a message.
  1172. PostMessage(pmc->ci.hwnd, MCMP_WINDOWPOSCHANGED, 0, 0);
  1173. }
  1174. if (changeFlags)
  1175. InvalidateRect(pmc->ci.hwnd, NULL, TRUE);
  1176. }
  1177. else if (gwl == GWL_EXSTYLE)
  1178. {
  1179. if ((pinfo->styleOld ^ pinfo->styleNew) & RTL_MIRRORED_WINDOW)
  1180. {
  1181. MCUpdateMonthNamePos(pmc);
  1182. }
  1183. }
  1184. return(0);
  1185. }
  1186. void MCCalcSizes(MONTHCAL *pmc)
  1187. {
  1188. HDC hdc;
  1189. HFONT hfontOrig;
  1190. int i, dxMax, dyMax, dxExtra;
  1191. RECT rect;
  1192. TCHAR szBuf[128];
  1193. TCHAR szDateFmt[64];
  1194. // get sizing info for bold font...
  1195. hdc = GetDC(pmc->ci.hwnd);
  1196. hfontOrig = SelectObject(hdc, (HGDIOBJ)pmc->hfontBold);
  1197. MGetTextExtent(hdc, g_szTextExtentDef, 2, &dxMax, &dyMax);
  1198. MGetTextExtent(hdc, g_szTextExtentDef, 4, &pmc->dxYearMax, NULL);
  1199. GetDateFormat(pmc->ct.lcid, DATE_SHORTDATE, &pmc->stToday,
  1200. NULL, szDateFmt, ARRAYSIZE(szDateFmt));
  1201. StringCchPrintf(szBuf, ARRAYSIZE(szBuf), TEXT("%s %s"), pmc->li.szToday, szDateFmt);
  1202. MGetTextExtent(hdc, szBuf, -1, &pmc->dxToday, &pmc->dyToday);
  1203. // REARCHITECT raymondc - hard-coded numbers are accessibility-incompatible
  1204. pmc->dyToday += 4;
  1205. //
  1206. // Cache these values so we don't go wacko if the app fails to
  1207. // forward WM_WININCHANGE messages into us and the user changes
  1208. // scrollbar widths. We'll draw with the wrong width, but at
  1209. // least they will be consistently wrong.
  1210. //
  1211. pmc->dxArrowMargin = DX_ARROWMARGIN;
  1212. pmc->dxCalArrow = DX_CALARROW;
  1213. pmc->dyCalArrow = DY_CALARROW;
  1214. //
  1215. // The banner bar consists of
  1216. //
  1217. // margin + scrollbutton + spacer +
  1218. // MonthName yyyy +
  1219. // + spacer + scrollbutton + margin
  1220. //
  1221. // Margin is dxArrowMargin
  1222. //
  1223. // Scrollbutton = dxCalArrow
  1224. //
  1225. // Spacer = border + CXVSCROLL + border
  1226. //
  1227. // The spacer needs to be large enough for us to insert an updown
  1228. // control when it comes time to spin the year. We don't need to
  1229. // cache the spacer anywhere - its value is implicit from the others.
  1230. //
  1231. // The actual width is divided by the number of columns we need
  1232. // (typically 7, but perhaps 8 if we are also displaying week numbers).
  1233. //
  1234. // We round the division down - later, we'll add some random futz
  1235. // to compensate.
  1236. //
  1237. dxExtra = pmc->dxArrowMargin + pmc->dxCalArrow +
  1238. (g_cxBorder + g_cxVScroll + g_cxBorder);
  1239. dxExtra = dxExtra + dxExtra; // left + right
  1240. for (i = 0; i < 12; i++)
  1241. {
  1242. int dxTemp;
  1243. // REARCHITECT raymondc - not localization safe for languages which change
  1244. // month forms based on context
  1245. StringCchPrintf(szBuf, ARRAYSIZE(szBuf), pmc->li.rgszMonth[i], g_szTextExtentDef);
  1246. MGetTextExtent(hdc, szBuf, -1, &dxTemp, NULL);
  1247. dxTemp += dxExtra;
  1248. dxTemp = dxTemp / (CALCOLMAX + (MonthCal_ShowWeekNumbers(pmc) ? 1:0));
  1249. if (dxTemp > dxMax)
  1250. dxMax = dxTemp;
  1251. }
  1252. SelectObject(hdc, (HGDIOBJ)pmc->hfont);
  1253. for (i = 0; i < 7; i++)
  1254. {
  1255. SIZE size;
  1256. MGetTextExtent(hdc, pmc->li.rgszDay[i], -1, (LPINT)&size.cx, (LPINT)&size.cy);
  1257. if (size.cx > dxMax)
  1258. dxMax = size.cx;
  1259. if (size.cy > dyMax)
  1260. dyMax = size.cy;
  1261. }
  1262. if (dyMax < pmc->dyCalArrow / 2)
  1263. dyMax = pmc->dyCalArrow / 2;
  1264. SelectObject(hdc, (HGDIOBJ)hfontOrig);
  1265. ReleaseDC(pmc->ci.hwnd, hdc);
  1266. pmc->dxCol = dxMax + 2;
  1267. pmc->dyRow = dyMax + 2;
  1268. pmc->dxMonth = pmc->dxCol * (CALCOLMAX + (MonthCal_ShowWeekNumbers(pmc) ? 1:0)) + 1;
  1269. pmc->dyMonth = pmc->dyRow * (CALROWMAX + 3) + 1; // we add 2 for the month name and day names
  1270. pmc->dxToday += pmc->dxCol+6+CALBORDER; // +2 for -1 at ends and 4 for shift of circle
  1271. if (pmc->dxMonth > pmc->dxToday)
  1272. pmc->dxToday = pmc->dxMonth;
  1273. // Space for month name (tile bar area of each month)
  1274. pmc->rcMonthName.left = 0;
  1275. pmc->rcMonthName.top = 0;
  1276. pmc->rcMonthName.right = pmc->dxMonth;
  1277. pmc->rcMonthName.bottom = pmc->rcMonthName.top + (pmc->dyRow * 2);
  1278. // Space for day-of-week
  1279. pmc->rcDow.left = 0;
  1280. pmc->rcDow.top = pmc->rcMonthName.bottom;
  1281. pmc->rcDow.right = pmc->dxMonth;
  1282. pmc->rcDow.bottom = pmc->rcDow.top + pmc->dyRow;
  1283. // Space for week numbers
  1284. if (MonthCal_ShowWeekNumbers(pmc))
  1285. {
  1286. pmc->rcWeekNum.left = pmc->rcDow.left;
  1287. pmc->rcWeekNum.top = pmc->rcDow.bottom;
  1288. pmc->rcWeekNum.right = pmc->rcWeekNum.left + pmc->dxCol;
  1289. pmc->rcWeekNum.bottom = pmc->dyMonth;
  1290. pmc->rcDow.left += pmc->dxCol; // shift days of week
  1291. }
  1292. // Space for the day numbers
  1293. pmc->rcDayNum.left = pmc->rcDow.left;
  1294. pmc->rcDayNum.top = pmc->rcDow.bottom;
  1295. pmc->rcDayNum.right = pmc->rcDayNum.left + (CALCOLMAX * pmc->dxCol);
  1296. pmc->rcDayNum.bottom = pmc->dyMonth;
  1297. GetClientRect(pmc->ci.hwnd, &rect);
  1298. MCRecomputeSizing(pmc, &rect);
  1299. }
  1300. void MCHandleSetFont(MONTHCAL *pmc, HFONT hfont, BOOL fRedraw)
  1301. {
  1302. LOGFONT lf;
  1303. HFONT hfontBold;
  1304. if (hfont == NULL)
  1305. hfont = (HFONT)GetStockObject(SYSTEM_FONT);
  1306. GetObject(hfont, sizeof(LOGFONT), (LPVOID)&lf);
  1307. // we want to make sure that the bold days are obviously different
  1308. // from the non-bold days...
  1309. CCAdjustForBold(&lf);
  1310. hfontBold = CreateFontIndirect(&lf);
  1311. if (hfontBold == NULL)
  1312. return;
  1313. if (pmc->hfontBold)
  1314. DeleteObject((HGDIOBJ)pmc->hfontBold);
  1315. pmc->hfont = hfont;
  1316. pmc->hfontBold = hfontBold;
  1317. pmc->ci.uiCodePage = GetCodePageForFont(hfont);
  1318. // calculate the new row and column sizes
  1319. MCCalcSizes(pmc);
  1320. if (fRedraw)
  1321. {
  1322. InvalidateRect(pmc->ci.hwnd, NULL, TRUE);
  1323. UpdateWindow(pmc->ci.hwnd);
  1324. }
  1325. }
  1326. void MCDrawTodayCircle(MONTHCAL *pmc, HDC hdc, RECT *prc)
  1327. {
  1328. FrameRect(hdc, prc, pmc->hbrushToday);
  1329. }
  1330. void MCInvalidateMonthDays(MONTHCAL *pmc)
  1331. {
  1332. InvalidateRect(pmc->ci.hwnd, &pmc->rcCentered, FALSE);
  1333. }
  1334. void MCGetTodayBtnRect(MONTHCAL *pmc, RECT *prc)
  1335. {
  1336. if (pmc->dxToday > pmc->rcCentered.right - pmc->rcCentered.left)
  1337. {
  1338. prc->left = pmc->rc.left + 1;
  1339. prc->right = pmc->rc.right - 1;
  1340. }
  1341. else
  1342. {
  1343. prc->left = pmc->rcCentered.left + 1;
  1344. prc->right = pmc->rcCentered.right - 1;
  1345. }
  1346. prc->top = pmc->rcCentered.bottom - pmc->dyToday;
  1347. prc->bottom = pmc->rcCentered.bottom;
  1348. // center the today rect when we only have 1 col and it will fit in window
  1349. if ((pmc->nViewCols == 1) && (pmc->dxToday <= pmc->rc.right - pmc->rc.left))
  1350. {
  1351. int dx = ((pmc->rcCentered.right - pmc->rcCentered.left) - pmc->dxToday) / 2 - 1;
  1352. prc->left += dx;
  1353. prc->right -= dx;
  1354. }
  1355. }
  1356. void MCPaintArrowBtn(MONTHCAL *pmc, HDC hdc, BOOL fPrev, BOOL fPressed)
  1357. {
  1358. int iStateId;
  1359. LPRECT prc;
  1360. UINT dfcs;
  1361. if (fPrev)
  1362. {
  1363. iStateId = ABS_LEFTNORMAL;
  1364. dfcs = DFCS_SCROLLLEFT;
  1365. prc = &pmc->rcPrev;
  1366. }
  1367. else
  1368. {
  1369. iStateId = ABS_RIGHTNORMAL;
  1370. dfcs = DFCS_SCROLLRIGHT;
  1371. prc = &pmc->rcNext;
  1372. }
  1373. if (pmc->fEnabled)
  1374. {
  1375. if (fPressed)
  1376. {
  1377. iStateId += 2;
  1378. dfcs |= DFCS_PUSHED | DFCS_FLAT;
  1379. }
  1380. }
  1381. else
  1382. {
  1383. iStateId += 3; // Disabled
  1384. dfcs |= DFCS_INACTIVE;
  1385. }
  1386. if (pmc->hThemeScroll)
  1387. {
  1388. DrawThemeBackground(pmc->hThemeScroll, hdc, SBP_ARROWBTN, iStateId, prc, 0);
  1389. }
  1390. else
  1391. {
  1392. DrawFrameControl(hdc, prc, DFC_SCROLL, dfcs);
  1393. }
  1394. }
  1395. void MCPaint(MONTHCAL *pmc, HDC hdc)
  1396. {
  1397. RECT rc, rcT;
  1398. int irow, icol, iMonth, iYear, iIndex, dx, dy;
  1399. HBRUSH hbrSelect;
  1400. HGDIOBJ hgdiOrig, hpenOrig;
  1401. pmc->hpen = CreatePen(PS_SOLID, 0, pmc->clr[MCSC_TEXT]);
  1402. hbrSelect = CreateSolidBrush(pmc->clr[MCSC_TITLEBK]);
  1403. SetBkMode(hdc, TRANSPARENT);
  1404. SetTextColor(hdc, pmc->clr[MCSC_TEXT]);
  1405. hpenOrig = SelectObject(hdc, GetStockObject(BLACK_PEN));
  1406. rc = pmc->rcCentered;
  1407. FillRectClr(hdc, &rc, pmc->clr[MCSC_MONTHBK]);
  1408. SelectObject(hdc, (HGDIOBJ)pmc->hpen);
  1409. // get the place for top left month
  1410. rc.left = pmc->rcCentered.left;
  1411. rc.right = rc.left + pmc->dxMonth;
  1412. rc.top = pmc->rcCentered.top;
  1413. rc.bottom = rc.top + pmc->dyMonth;
  1414. iMonth = pmc->stMonthFirst.wMonth;
  1415. iYear = pmc->stMonthFirst.wYear;
  1416. dx = pmc->dxMonth + CALBORDER;
  1417. dy = pmc->dyMonth + CALBORDER;
  1418. iIndex = 0;
  1419. for (irow = 0; irow < pmc->nViewRows; irow++)
  1420. {
  1421. rcT = rc;
  1422. for (icol = 0; icol < pmc->nViewCols; icol++)
  1423. {
  1424. if (RectVisible(hdc, &rcT))
  1425. {
  1426. MCPaintMonth(pmc, hdc, &rcT, iMonth, iYear, iIndex,
  1427. iIndex == 0,
  1428. iIndex == (pmc->nMonths - 1), hbrSelect);
  1429. }
  1430. rcT.left += dx;
  1431. rcT.right += dx;
  1432. if (++iMonth > 12)
  1433. {
  1434. iMonth = 1;
  1435. iYear++;
  1436. }
  1437. iIndex++;
  1438. }
  1439. rc.top += dy;
  1440. rc.bottom += dy;
  1441. }
  1442. // draw the today stuff
  1443. if (MonthCal_ShowToday(pmc))
  1444. {
  1445. MCGetTodayBtnRect(pmc, &rc);
  1446. if (RectVisible(hdc, &rc))
  1447. {
  1448. TCHAR szDateFmt[32];
  1449. TCHAR szBuf[64];
  1450. rcT.right = rc.left + 2; // a bit extra border space
  1451. if (MonthCal_ShowTodayCircle(pmc)) // this turns on/off the red circle
  1452. {
  1453. rcT.left = rcT.right + 2;
  1454. rcT.right = rcT.left + pmc->dxCol - 2;
  1455. rcT.top = rc.top + 2;
  1456. rcT.bottom = rc.bottom - 2;
  1457. MCDrawTodayCircle(pmc, hdc, &rcT);
  1458. }
  1459. rcT.left = rcT.right + 2;
  1460. rcT.right = rc.right - 2;
  1461. rcT.top = rc.top;
  1462. rcT.bottom = rc.bottom;
  1463. hgdiOrig = SelectObject(hdc, (HGDIOBJ)pmc->hfontBold);
  1464. SetTextColor(hdc, pmc->clr[MCSC_TEXT]);
  1465. GetDateFormat(pmc->ct.lcid, DATE_SHORTDATE, &pmc->stToday,
  1466. NULL, szDateFmt, ARRAYSIZE(szDateFmt));
  1467. StringCchPrintf(szBuf, ARRAYSIZE(szBuf), TEXT("%s %s"), pmc->li.szToday, szDateFmt);
  1468. DrawText(hdc, szBuf, lstrlen(szBuf), &rcT,
  1469. DT_LEFT | DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
  1470. SelectObject(hdc, hgdiOrig);
  1471. }
  1472. }
  1473. // Draw the spin buttons
  1474. if (RectVisible(hdc, &pmc->rcPrev))
  1475. MCPaintArrowBtn(pmc, hdc, TRUE, (pmc->idTimer && pmc->fSpinPrev));
  1476. if (RectVisible(hdc, &pmc->rcNext))
  1477. MCPaintArrowBtn(pmc, hdc, FALSE, (pmc->idTimer && !pmc->fSpinPrev));
  1478. SelectObject(hdc, hpenOrig);
  1479. DeleteObject((HGDIOBJ)hbrSelect);
  1480. DeleteObject((HGDIOBJ)pmc->hpen);
  1481. }
  1482. //
  1483. // MCGetMonthFormat gets the string to display for the month/year
  1484. // in the passed-in SYSTEMTIME. This is tricky because of eras.
  1485. // If pmm is non-NULL, it receives the metrics of the
  1486. // formatted month/year string.
  1487. //
  1488. void MCGetMonthFormat(MONTHCAL *pmc, SYSTEMTIME *pst, LPTSTR rgch, UINT cch, PMONTHMETRICS pmm)
  1489. {
  1490. // For all months, we display the name appropriate to the first
  1491. // day of the month. Note that this means that the title of the
  1492. // month in which the era changes may be confusing. If the era
  1493. // changes in the middle of a month, we name the month after the
  1494. // previous era, even if the current selection belongs to the next
  1495. // era. I hope nobody will mind.
  1496. pst->wDay = 1;
  1497. //
  1498. // Get the string (all marked up), then extract the markers
  1499. // to locate the month and year substrings.
  1500. //
  1501. rgch[0] = TEXT('\0'); // In case something horrible happens
  1502. GetDateFormat(pmc->ct.lcid, 0, pst,
  1503. pmc->li.szMonthYearFmt,
  1504. rgch, cch);
  1505. MCRemoveMarkers(rgch, pmm);
  1506. }
  1507. void MCPaintMonth(MONTHCAL *pmc, HDC hdc, RECT *prc, int iMonth, int iYear, int iIndex,
  1508. BOOL fDrawPrev, BOOL fDrawNext, HBRUSH hbrSelect)
  1509. {
  1510. BOOL fBold, fView, fReset;
  1511. RECT rc, rcT;
  1512. int nDay, cdy, irow, icol, crowShow, nweek, isel;
  1513. TCHAR rgch[64];
  1514. LPTSTR psz;
  1515. HGDIOBJ hfontOrig;
  1516. COLORREF clrGrayText, clrHiliteText, clrOld, clrText;
  1517. SYSTEMTIME st = {0};
  1518. int iIndexSave = iIndex;
  1519. clrText = pmc->clr[MCSC_TEXT];
  1520. clrGrayText = pmc->clr[MCSC_TRAILINGTEXT];
  1521. clrHiliteText = pmc->clr[MCSC_TITLETEXT];
  1522. hfontOrig = SelectObject(hdc, (HGDIOBJ)pmc->hfont);
  1523. SelectObject(hdc, (HGDIOBJ)pmc->hpen);
  1524. //
  1525. // Draw the Month and Year
  1526. //
  1527. // translate the relative coords to window coords
  1528. rc = pmc->rcMonthName;
  1529. rc.left += prc->left;
  1530. rc.right += prc->left;
  1531. rc.top += prc->top;
  1532. rc.bottom += prc->top;
  1533. if (RectVisible(hdc, &rc))
  1534. {
  1535. FillRectClr(hdc, &rc, pmc->clr[MCSC_TITLEBK]);
  1536. SetTextColor(hdc, pmc->clr[MCSC_TITLETEXT]);
  1537. SelectObject(hdc, (HGDIOBJ)pmc->hfontBold);
  1538. st.wYear = (WORD) iYear;
  1539. st.wMonth = (WORD) iMonth;
  1540. MCGetMonthFormat(pmc, &st, rgch, ARRAYSIZE(rgch), NULL);
  1541. DrawText(hdc, rgch, lstrlen(rgch), &rc, DT_CENTER | DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
  1542. #ifdef MARKER_DEBUG
  1543. //
  1544. // When debugging MCInsertMarker and MCRemoveMarker, draw colored
  1545. // bars where we think the markers were.
  1546. //
  1547. { RECT rcT = rc;
  1548. rcT.top = rcT.bottom - 2;
  1549. rcT.left = rc.left + pmc->rgmm[iIndex].rgi[IMM_MONTHSTART];
  1550. rcT.right = rc.left + pmc->rgmm[iIndex].rgi[IMM_MONTHEND];
  1551. FillRectClr(hdc, &rcT, RGB(0xFF, 0, 0));
  1552. rcT.left = rc.left + pmc->rgmm[iIndex].rgi[IMM_YEARSTART];
  1553. rcT.right = rc.left + pmc->rgmm[iIndex].rgi[IMM_YEAREND];
  1554. FillRectClr(hdc, &rcT, RGB(0, 0xFF, 0));
  1555. }
  1556. #endif
  1557. SelectObject(hdc, (HGDIOBJ)pmc->hfont);
  1558. }
  1559. SetTextColor(hdc, pmc->clr[MCSC_TITLEBK]);
  1560. //
  1561. // Draw the days of the month
  1562. //
  1563. // translate the relative coords to window coords
  1564. rc = pmc->rcDow;
  1565. rc.left += prc->left;
  1566. rc.right += prc->left;
  1567. rc.top += prc->top;
  1568. rc.bottom += prc->top;
  1569. if (RectVisible(hdc, &rc))
  1570. {
  1571. MoveToEx(hdc, rc.left + 4, rc.bottom - 1, NULL);
  1572. LineTo(hdc, rc.right - 4, rc.bottom - 1);
  1573. rc.right = rc.left + pmc->dxCol;
  1574. for (icol = 0; icol < CALCOLMAX; icol++)
  1575. {
  1576. psz = pmc->li.rgszDay[(icol + pmc->li.dowStartWeek) % 7];
  1577. DrawText(hdc, psz, lstrlen(psz), &rc, DT_CENTER | DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
  1578. rc.left += pmc->dxCol;
  1579. rc.right += pmc->dxCol;
  1580. }
  1581. }
  1582. // Check to see how many days from the previous month exist in this months calendar
  1583. nDay = pmc->rgnDayUL[iIndex]; // last day in prev month that won't be shown in this month
  1584. cdy = pmc->rgcDay[iIndex]; // # of days in prev month
  1585. // Calculate the number of weeks to display
  1586. if (fDrawNext)
  1587. crowShow = CALROWMAX;
  1588. else
  1589. crowShow = ((cdy - nDay) + pmc->rgcDay[iIndex + 1] + 6/* round up */) / 7;
  1590. if (nDay != cdy)
  1591. {
  1592. // start at previous month
  1593. iMonth--;
  1594. if(iMonth <= 0)
  1595. {
  1596. iMonth = 12;
  1597. iYear--;
  1598. }
  1599. nDay++;
  1600. fView = FALSE;
  1601. }
  1602. else
  1603. {
  1604. // start at this month
  1605. iIndex++; // this month
  1606. nDay = 1;
  1607. cdy = pmc->rgcDay[iIndex];
  1608. fView = TRUE;
  1609. }
  1610. //
  1611. // Draw the week numbers
  1612. //
  1613. if (MonthCal_ShowWeekNumbers(pmc))
  1614. {
  1615. // translate the relative coords to window coords
  1616. rc = pmc->rcWeekNum;
  1617. rc.left += prc->left;
  1618. rc.top += prc->top;
  1619. rc.right += prc->left;
  1620. rc.bottom = rc.top + (pmc->dyRow * crowShow);
  1621. // draw the week numbers
  1622. if (RectVisible(hdc, &rc))
  1623. {
  1624. MoveToEx(hdc, rc.right - 1, rc.top + 4, NULL);
  1625. LineTo(hdc, rc.right - 1, rc.bottom - 4);
  1626. st.wYear = (WORD) iYear;
  1627. st.wMonth = (WORD) iMonth;
  1628. st.wDay = (WORD) nDay;
  1629. nweek = GetWeekNumber(&st, pmc->li.dowStartWeek, pmc->li.firstWeek);
  1630. rc.bottom = rc.top + pmc->dyRow;
  1631. for (irow = 0; irow < crowShow; irow++)
  1632. {
  1633. StringCchPrintf(rgch, ARRAYSIZE(rgch), g_szNumFmt, nweek);
  1634. DrawText(hdc, rgch, (nweek > 9 ? 2 : 1), &rc,
  1635. DT_CENTER | DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
  1636. rc.top += pmc->dyRow;
  1637. rc.bottom += pmc->dyRow;
  1638. IncrSystemTime(&st, &st, 1, INCRSYS_WEEK);
  1639. nweek = GetWeekNumber(&st, pmc->li.dowStartWeek, pmc->li.firstWeek);
  1640. }
  1641. }
  1642. }
  1643. if (!fView)
  1644. SetTextColor(hdc, clrGrayText);
  1645. else
  1646. SetTextColor(hdc, clrText);
  1647. rc = pmc->rcDayNum;
  1648. rc.left += prc->left;
  1649. rc.top += prc->top;
  1650. rc.right = rc.left + pmc->dxCol;
  1651. rc.bottom = rc.top + pmc->dyRow;
  1652. fReset = FALSE;
  1653. fBold = FALSE;
  1654. for (irow = 0; irow < crowShow; irow++)
  1655. {
  1656. rcT = rc;
  1657. for (icol = 0; icol < CALCOLMAX; icol++)
  1658. {
  1659. if ((fView || fDrawPrev) && RectVisible(hdc, &rcT))
  1660. {
  1661. StringCchPrintf(rgch, ARRAYSIZE(rgch), g_szNumFmt, nDay);
  1662. if (MonthCal_IsDayState(pmc))
  1663. {
  1664. // if we're in a dropdown we don't display
  1665. if (MCIsBoldOffsetDay(pmc, nDay, iIndex))
  1666. {
  1667. if (!fBold)
  1668. {
  1669. SelectObject(hdc, (HGDIOBJ)pmc->hfontBold);
  1670. fBold = TRUE;
  1671. }
  1672. }
  1673. else
  1674. {
  1675. if (fBold)
  1676. {
  1677. SelectObject(hdc, (HGDIOBJ)pmc->hfont);
  1678. fBold = FALSE;
  1679. }
  1680. }
  1681. }
  1682. if (isel = MCIsSelectedDayMoYr(pmc, nDay, iMonth, iYear))
  1683. {
  1684. int x1, x2;
  1685. clrOld = SetTextColor(hdc, clrHiliteText);
  1686. fReset = TRUE;
  1687. SelectObject(hdc, GetStockObject(NULL_PEN));
  1688. x1 = 0;
  1689. x2 = 0;
  1690. if (isel & SEL_DOT)
  1691. {
  1692. FillRect(hdc, &rcT, hbrSelect);
  1693. if (isel == SEL_BEGIN)
  1694. {
  1695. x1 = rcT.left + (rcT.right - rcT.left) / 2;
  1696. x2 = rcT.right;
  1697. }
  1698. else if (isel == SEL_END)
  1699. {
  1700. x1 = rcT.left;
  1701. x2 = rcT.left + (rcT.right - rcT.left) / 2;
  1702. }
  1703. }
  1704. else
  1705. {
  1706. x1 = rcT.left;
  1707. x2 = rcT.right;
  1708. }
  1709. if (x1 && x2)
  1710. {
  1711. Rectangle(hdc, x1, rcT.top + 2, x2 + 1, rcT.bottom - 1);
  1712. }
  1713. }
  1714. DrawText(hdc, rgch, (nDay > 9 ? 2 : 1), &rcT,
  1715. DT_CENTER | DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
  1716. if (MonthCal_ShowTodayCircle(pmc) && pmc->fToday && iIndexSave == pmc->iMonthToday &&
  1717. icol == pmc->iColToday && irow == pmc->iRowToday)
  1718. {
  1719. MCDrawTodayCircle(pmc, hdc, &rcT);
  1720. }
  1721. if (fReset)
  1722. {
  1723. SetTextColor(hdc, clrOld);
  1724. fReset = FALSE;
  1725. }
  1726. }
  1727. rcT.left += pmc->dxCol;
  1728. rcT.right += pmc->dxCol;
  1729. nDay++;
  1730. if (nDay > cdy)
  1731. {
  1732. if (!fDrawNext && iIndex > iIndexSave)
  1733. goto doneMonth;
  1734. nDay = 1;
  1735. iIndex++;
  1736. cdy = pmc->rgcDay[iIndex];
  1737. iMonth++;
  1738. if (iMonth > 12)
  1739. {
  1740. iMonth = 1;
  1741. iYear++;
  1742. }
  1743. fView = !fView;
  1744. SetTextColor(hdc, fView ? clrText : clrGrayText);
  1745. fDrawPrev = fDrawNext;
  1746. }
  1747. }
  1748. rc.top += pmc->dyRow;
  1749. rc.bottom += pmc->dyRow;
  1750. }
  1751. doneMonth:
  1752. SelectObject(hdc, hfontOrig);
  1753. return;
  1754. }
  1755. int MCIsSelectedDayMoYr(MONTHCAL *pmc, int iDay, int iMonth, int iYear)
  1756. {
  1757. SYSTEMTIME st;
  1758. int iBegin, iEnd;
  1759. int iret = 0;
  1760. st.wYear = (WORD) iYear;
  1761. st.wMonth = (WORD) iMonth;
  1762. st.wDay = (WORD) iDay;
  1763. iBegin = CmpDate(&st, &pmc->st);
  1764. if (MonthCal_IsMultiSelect(pmc))
  1765. {
  1766. iEnd = CmpDate(&st, &pmc->stEndSel);
  1767. if (iBegin > 0 && iEnd< 0)
  1768. iret = SEL_MID;
  1769. else
  1770. {
  1771. if (iBegin == 0)
  1772. iret |= SEL_BEGIN;
  1773. if (iEnd == 0)
  1774. iret |= SEL_END;
  1775. }
  1776. }
  1777. else if (iBegin == 0)
  1778. {
  1779. iret = SEL_DOT;
  1780. }
  1781. return(iret);
  1782. }
  1783. BOOL MCIsBoldOffsetDay(MONTHCAL *pmc, int nDay, int iIndex)
  1784. {
  1785. return(pmc->rgdayState && (pmc->rgdayState[iIndex] & (1L << (nDay - 1))) != 0);
  1786. }
  1787. void MCNcDestroyHandler(HWND hwnd, MONTHCAL *pmc, WPARAM wParam, LPARAM lParam)
  1788. {
  1789. if (pmc)
  1790. {
  1791. if (pmc->hThemeScroll)
  1792. CloseThemeData(pmc->hThemeScroll);
  1793. if (pmc->hbrushToday)
  1794. DeleteObject((HGDIOBJ)pmc->hbrushToday);
  1795. if (pmc->hfontBold)
  1796. DeleteObject((HGDIOBJ)pmc->hfontBold);
  1797. if (pmc->hmenuCtxt)
  1798. DestroyMenu(pmc->hmenuCtxt);
  1799. if (pmc->hmenuMonth)
  1800. DestroyMenu(pmc->hmenuMonth);
  1801. if (pmc->idTimer)
  1802. KillTimer(pmc->ci.hwnd, pmc->idTimer);
  1803. if (pmc->idTimerToday)
  1804. KillTimer(pmc->ci.hwnd, pmc->idTimerToday);
  1805. MCFreeCalendarInfo(&pmc->ct);
  1806. GlobalFreePtr(pmc);
  1807. }
  1808. // In case rogue messages float through after we have freed the pdtpick, set
  1809. // the handle in the window structure to FFFF and test for this value at
  1810. // the top of the WndProc
  1811. MonthCal_SetPtr(hwnd, NULL);
  1812. // Call DefWindowProc32 to free all little chunks of memory such as szName
  1813. // and rgwScroll.
  1814. DefWindowProc(hwnd, WM_NCDESTROY, wParam, lParam);
  1815. }
  1816. /* Computes the following:
  1817. * nViewCols
  1818. * nViewRows
  1819. * rcCentered
  1820. * rcPrev
  1821. * rcNext
  1822. */
  1823. void MCRecomputeSizing(MONTHCAL *pmc, RECT *prect)
  1824. {
  1825. RECT rc;
  1826. int dx, dy, dCal;
  1827. // Space for entire calendar
  1828. pmc->rc = *prect;
  1829. dx = prect->right - prect->left;
  1830. dy = prect->bottom - prect->top;
  1831. pmc->nViewCols = 1 + (dx - pmc->dxMonth) / (pmc->dxMonth + CALBORDER);
  1832. pmc->nViewRows = 1 + (dy - pmc->dyMonth - pmc->dyToday) / (pmc->dyMonth + CALBORDER);
  1833. // if dx < dxMonth or dy < dyMonth, these can be zero. That's bad...
  1834. if (pmc->nViewCols < 1)
  1835. pmc->nViewCols = 1;
  1836. if (pmc->nViewRows < 1)
  1837. pmc->nViewRows = 1;
  1838. // Make sure we don't display more than CALMONTHMAX months
  1839. while ((pmc->nViewRows * pmc->nViewCols) > CALMONTHMAX)
  1840. {
  1841. if (pmc->nViewRows > pmc->nViewCols)
  1842. pmc->nViewRows--;
  1843. else
  1844. pmc->nViewCols--;
  1845. }
  1846. // RC for the months, centered within the client window
  1847. dCal = pmc->nViewCols * (pmc->dxMonth + CALBORDER) - CALBORDER;
  1848. pmc->rcCentered.left = (dx - dCal) / 2;
  1849. if (pmc->rcCentered.left < 0)
  1850. pmc->rcCentered.left = 0;
  1851. pmc->rcCentered.right = pmc->rcCentered.left + dCal;
  1852. dCal = pmc->nViewRows * (pmc->dyMonth + CALBORDER) - CALBORDER + pmc->dyToday;
  1853. pmc->rcCentered.top = (dy - dCal) / 2;
  1854. if (pmc->rcCentered.top < 0)
  1855. pmc->rcCentered.top = 0;
  1856. pmc->rcCentered.bottom = pmc->rcCentered.top + dCal;
  1857. // Calculate and set RCs for the spin buttons
  1858. rc.top = pmc->rcCentered.top + (pmc->dyRow * 2 - pmc->dyCalArrow) /2;
  1859. rc.bottom = rc.top + pmc->dyCalArrow;
  1860. rc.left = pmc->rcCentered.left + pmc->dxArrowMargin;
  1861. rc.right = rc.left + pmc->dxCalArrow;
  1862. pmc->rcPrev = rc;
  1863. rc.right = pmc->rcCentered.right - pmc->dxArrowMargin;
  1864. rc.left = rc.right - pmc->dxCalArrow;
  1865. pmc->rcNext = rc;
  1866. }
  1867. LRESULT MCSizeHandler(MONTHCAL *pmc, RECT *prc)
  1868. {
  1869. int nMax;
  1870. SYSTEMTIME st;
  1871. int cmo, dmo;
  1872. MCRecomputeSizing(pmc, prc);
  1873. nMax = pmc->nViewRows * pmc->nViewCols;
  1874. // Compute new start date
  1875. CopyDate(pmc->stMonthFirst, st);
  1876. // REARCHITECT: this doesn't consider stEndSel
  1877. cmo = (pmc->stMonthLast.wYear - (int)pmc->st.wYear) * 12 +
  1878. (pmc->stMonthLast.wMonth - (int)pmc->st.wMonth);
  1879. dmo = nMax - pmc->nMonths;
  1880. if (-dmo > cmo)
  1881. {
  1882. // Selected mon/yr not in view
  1883. IncrSystemTime(&st, &st, -(cmo + dmo), INCRSYS_MONTH);
  1884. cmo = 0;
  1885. }
  1886. // If the # of months being displayed has changed, then lets try to
  1887. // start the calendar from January.
  1888. if ((dmo != 0) && (cmo + dmo >= pmc->stMonthFirst.wMonth - 1))
  1889. st.wMonth = 1;
  1890. MCUpdateStartEndDates(pmc, &st);
  1891. InvalidateRect(pmc->ci.hwnd, NULL, TRUE);
  1892. UpdateWindow(pmc->ci.hwnd);
  1893. return(0);
  1894. }
  1895. //
  1896. // For each month being displayed, compute the precise locations of all
  1897. // the gizmos we draw into the month header area.
  1898. //
  1899. void MCUpdateMonthNamePos(MONTHCAL *pmc)
  1900. {
  1901. HDC hdc;
  1902. int iCount;
  1903. SYSTEMTIME st;
  1904. TCHAR rgch[64];
  1905. SIZE size;
  1906. HGDIOBJ hfontOrig;
  1907. hdc = GetDC(pmc->ci.hwnd);
  1908. hfontOrig = SelectObject(hdc, (HGDIOBJ)pmc->hfontBold);
  1909. st = pmc->stMonthFirst;
  1910. for (iCount = 0; iCount < pmc->nMonths; iCount++)
  1911. {
  1912. PMONTHMETRICS pmm = &pmc->rgmm[iCount];
  1913. int i;
  1914. MCGetMonthFormat(pmc, &st, rgch, ARRAYSIZE(rgch), pmm);
  1915. GetTextExtentPoint32(hdc, rgch, lstrlen(rgch), &size);
  1916. pmm->rgi[IMM_START] = (pmc->dxMonth - size.cx) / 2;
  1917. //
  1918. // Now convert the indices into pixels so we can figure out where
  1919. // all the strings ended up.
  1920. //
  1921. for (i = IMM_DATEFIRST; i <= IMM_DATELAST; i++) {
  1922. SIZE sizeT;
  1923. // In case of horrible error, pretend the marker was at the
  1924. // beginning of the string.
  1925. sizeT.cx = 0;
  1926. GetTextExtentPoint32(hdc, rgch, pmm->rgi[i], &sizeT);
  1927. pmm->rgi[i] = pmm->rgi[IMM_START] + sizeT.cx;
  1928. }
  1929. //
  1930. // Now flip the coordinates for RTL.
  1931. //
  1932. if (pmc->fHeaderRTL || IS_WINDOW_RTL_MIRRORED(pmc->ci.hwnd))
  1933. {
  1934. int dxStart, dxEnd;
  1935. // Flip the month...
  1936. dxStart = pmm->rgi[IMM_MONTHSTART] - pmm->rgi[IMM_START];
  1937. dxEnd = pmm->rgi[IMM_MONTHEND ] - pmm->rgi[IMM_START];
  1938. pmm->rgi[IMM_MONTHSTART] = pmm->rgi[IMM_START] + size.cx - dxEnd;
  1939. pmm->rgi[IMM_MONTHEND ] = pmm->rgi[IMM_START] + size.cx - dxStart;
  1940. // Flip the year...
  1941. dxStart = pmm->rgi[IMM_YEARSTART] - pmm->rgi[IMM_START];
  1942. dxEnd = pmm->rgi[IMM_YEAREND ] - pmm->rgi[IMM_START];
  1943. pmm->rgi[IMM_YEARSTART] = pmm->rgi[IMM_START] + size.cx - dxEnd;
  1944. pmm->rgi[IMM_YEAREND ] = pmm->rgi[IMM_START] + size.cx - dxStart;
  1945. }
  1946. // On to the next month
  1947. if(++st.wMonth > 12)
  1948. {
  1949. st.wMonth = 1;
  1950. st.wYear++;
  1951. }
  1952. }
  1953. SelectObject(hdc, hfontOrig);
  1954. ReleaseDC(pmc->ci.hwnd, hdc);
  1955. }
  1956. /*
  1957. * Computes the following, given the number of rows & columns available:
  1958. * stMonthFirst.wMonth
  1959. * stMonthFirst.wYear
  1960. * stMonthLast.wMonth
  1961. * stMonthLast.wYear
  1962. * nMonths
  1963. *
  1964. * Trashes *pstStart
  1965. */
  1966. void MCUpdateStartEndDates(MONTHCAL *pmc, SYSTEMTIME *pstStart)
  1967. {
  1968. int iCount, iMonth, iYear;
  1969. int nMonthsToEdge;
  1970. pmc->nMonths = pmc->nViewRows * pmc->nViewCols;
  1971. // make sure pstStart to pstStart+nMonths is within range
  1972. nMonthsToEdge = ((int)pmc->stMax.wYear - (int)pstStart->wYear) * 12 +
  1973. ((int)pmc->stMax.wMonth - (int)pstStart->wMonth) + 1;
  1974. if (nMonthsToEdge < pmc->nMonths)
  1975. IncrSystemTime(pstStart, pstStart, nMonthsToEdge - pmc->nMonths, INCRSYS_MONTH);
  1976. if (CmpDate(pstStart, &pmc->stMin) < 0)
  1977. {
  1978. CopyDate(pmc->stMin, *pstStart);
  1979. }
  1980. nMonthsToEdge = ((int)pmc->stMax.wYear - (int)pstStart->wYear) * 12 +
  1981. ((int)pmc->stMax.wMonth - (int)pstStart->wMonth) + 1;
  1982. if (nMonthsToEdge < pmc->nMonths)
  1983. pmc->nMonths = nMonthsToEdge;
  1984. pmc->stMonthFirst.wYear = pstStart->wYear;
  1985. pmc->stMonthFirst.wMonth = pstStart->wMonth;
  1986. pmc->stMonthFirst.wDay = 1;
  1987. if (CmpDate(&pmc->stMonthFirst, &pmc->stMin) < 0)
  1988. {
  1989. pmc->stMonthFirst.wDay = pmc->stMin.wDay;
  1990. ASSERT(0==CmpDate(&pmc->stMonthFirst, &pmc->stMin));
  1991. }
  1992. // these ranges are CALMONTHMAX+2 and nMonths <= CALMONTHMAX, so we are safe
  1993. // index 0 corresponds to stViewFirst (DAYSTATE) info
  1994. // index 1..nMonths correspond to stMonthFirst..stMonthLast info
  1995. // index nMonths+1 corresponds to stViewLast (DAYSTATE) info
  1996. //
  1997. iYear = pmc->stMonthFirst.wYear;
  1998. iMonth = pmc->stMonthFirst.wMonth - 1;
  1999. if(iMonth == 0)
  2000. {
  2001. iMonth = 12;
  2002. iYear--;
  2003. }
  2004. for (iCount = 0; iCount <= pmc->nMonths+1; iCount++)
  2005. {
  2006. int cdy, dow, ddow;
  2007. // number of days in this month
  2008. cdy = GetDaysForMonth(iYear, iMonth);
  2009. pmc->rgcDay[iCount] = cdy;
  2010. // move to "this" month
  2011. if(++iMonth > 12)
  2012. {
  2013. iMonth = 1;
  2014. iYear++;
  2015. }
  2016. // last day of this month NOT visible when viewing NEXT month
  2017. dow = GetStartDowForMonth(iYear, iMonth);
  2018. ddow = dow - pmc->li.dowStartWeek;
  2019. if(ddow < 0)
  2020. ddow += CALCOLMAX;
  2021. pmc->rgnDayUL[iCount] = cdy - ddow;
  2022. }
  2023. // we want to always have days visible on the previous month
  2024. if (pmc->rgnDayUL[0] == pmc->rgcDay[0])
  2025. pmc->rgnDayUL[0] -= CALCOLMAX;
  2026. IncrSystemTime(&pmc->stMonthFirst, &pmc->stMonthLast, pmc->nMonths - 1, INCRSYS_MONTH);
  2027. pmc->stMonthLast.wDay = (WORD) pmc->rgcDay[pmc->nMonths];
  2028. if (pmc->fMaxYrSet && CmpDate(&pmc->stMonthLast, &pmc->stMax) > 0)
  2029. {
  2030. pmc->stMonthLast.wDay = pmc->stMax.wDay;
  2031. ASSERT(0==CmpDate(&pmc->stMonthLast, &pmc->stMax));
  2032. }
  2033. pmc->stViewFirst.wYear = pmc->stMonthFirst.wYear;
  2034. pmc->stViewFirst.wMonth = pmc->stMonthFirst.wMonth - 1;
  2035. if (pmc->stViewFirst.wMonth == 0)
  2036. {
  2037. pmc->stViewFirst.wMonth = 12;
  2038. pmc->stViewFirst.wYear--;
  2039. }
  2040. pmc->stViewFirst.wDay = pmc->rgnDayUL[0] + 1;
  2041. pmc->stViewLast.wYear = pmc->stMonthLast.wYear;
  2042. pmc->stViewLast.wMonth = pmc->stMonthLast.wMonth + 1;
  2043. if (pmc->stViewLast.wMonth == 13)
  2044. {
  2045. pmc->stViewLast.wMonth = 1;
  2046. pmc->stViewLast.wYear++;
  2047. }
  2048. // total days - (days in last month + remaining days in previous month)
  2049. pmc->stViewLast.wDay = CALROWMAX * CALCOLMAX -
  2050. (pmc->rgcDay[pmc->nMonths] +
  2051. pmc->rgcDay[pmc->nMonths-1] - pmc->rgnDayUL[pmc->nMonths-1]);
  2052. MCUpdateDayState(pmc);
  2053. MCUpdateRcDayCur(pmc, &pmc->st);
  2054. MCUpdateToday(pmc);
  2055. MCUpdateMonthNamePos(pmc);
  2056. }
  2057. void MCUpdateToday(MONTHCAL *pmc)
  2058. {
  2059. if (MonthCal_ShowTodayCircle(pmc))
  2060. {
  2061. int iMonth;
  2062. iMonth = MCGetOffsetForYrMo(pmc, pmc->stToday.wYear, pmc->stToday.wMonth);
  2063. if (iMonth < 0)
  2064. {
  2065. // today is not visible in the displayed months
  2066. pmc->fToday = FALSE;
  2067. }
  2068. else
  2069. {
  2070. int iDay;
  2071. // today is visible in the displayed months
  2072. pmc->fToday = TRUE;
  2073. iDay = pmc->rgcDay[iMonth] - pmc->rgnDayUL[iMonth] + pmc->stToday.wDay - 1;
  2074. pmc->iMonthToday = iMonth;
  2075. pmc->iRowToday = iDay / CALCOLMAX;
  2076. pmc->iColToday = iDay % CALCOLMAX;
  2077. }
  2078. }
  2079. }
  2080. BOOL FUpdateRcDayCur(MONTHCAL *pmc, POINT pt)
  2081. {
  2082. int iRow, iCol;
  2083. RECT rc;
  2084. SYSTEMTIME st;
  2085. if (!FGetDateForPt(pmc, pt, &st, NULL, &iCol, &iRow, &rc))
  2086. return FALSE;
  2087. if (CmpDate(&st, &pmc->stMin) < 0)
  2088. return FALSE;
  2089. if (CmpDate(&st, &pmc->stMax) > 0)
  2090. return FALSE;
  2091. // calculate the day rc
  2092. pmc->rcDayCur.left = rc.left + pmc->rcDayNum.left + iCol * pmc->dxCol;
  2093. pmc->rcDayCur.top = rc.top + pmc->rcDayNum.top + iRow * pmc->dyRow;
  2094. pmc->rcDayCur.right = pmc->rcDayCur.left + pmc->dxCol;
  2095. pmc->rcDayCur.bottom = pmc->rcDayCur.top + pmc->dyRow;
  2096. return(TRUE);
  2097. }
  2098. void MCUpdateDayState(MONTHCAL *pmc)
  2099. {
  2100. HWND hwndParent;
  2101. if (!MonthCal_IsDayState(pmc))
  2102. return;
  2103. hwndParent = GetParent(pmc->ci.hwnd);
  2104. if (hwndParent)
  2105. {
  2106. int i, mon, yr, cmonths;
  2107. yr = pmc->stViewFirst.wYear;
  2108. mon = pmc->stViewFirst.wMonth;
  2109. cmonths = pmc->nMonths + 2;
  2110. // don't do anything unless we need to
  2111. if (cmonths != pmc->cds || mon != pmc->dsMonth || yr != pmc->dsYear)
  2112. {
  2113. // this is a small enough to not deal with allocating it
  2114. NMDAYSTATE nmds;
  2115. MONTHDAYSTATE buffer[CALMONTHMAX+2];
  2116. ZeroMemory(&nmds, SIZEOF(nmds));
  2117. nmds.stStart.wYear = (WORD) yr;
  2118. nmds.stStart.wMonth = (WORD) mon;
  2119. nmds.stStart.wDay = 1;
  2120. nmds.cDayState = cmonths;
  2121. nmds.prgDayState = buffer;
  2122. CCSendNotify(&pmc->ci, MCN_GETDAYSTATE, &nmds.nmhdr);
  2123. for (i = 0; i < cmonths; i++)
  2124. pmc->rgdayState[i] = nmds.prgDayState[i];
  2125. pmc->cds = cmonths;
  2126. pmc->dsMonth = mon;
  2127. pmc->dsYear = yr;
  2128. }
  2129. }
  2130. }
  2131. void MCNotifySelChange(MONTHCAL *pmc, UINT uMsg)
  2132. {
  2133. HWND hwndParent;
  2134. if (pmc->fNoNotify)
  2135. return;
  2136. hwndParent = GetParent(pmc->ci.hwnd);
  2137. if (hwndParent)
  2138. {
  2139. NMSELCHANGE nmsc;
  2140. ZeroMemory(&nmsc, SIZEOF(nmsc));
  2141. CopyDate(pmc->st, nmsc.stSelStart);
  2142. if (MonthCal_IsMultiSelect(pmc))
  2143. CopyDate(pmc->stEndSel, nmsc.stSelEnd);
  2144. CCSendNotify(&pmc->ci, uMsg, &nmsc.nmhdr);
  2145. }
  2146. }
  2147. void MCUpdateRcDayCur(MONTHCAL *pmc, SYSTEMTIME *pst)
  2148. {
  2149. int iOff;
  2150. iOff = MCGetOffsetForYrMo(pmc, pst->wYear, pst->wMonth);
  2151. if (iOff >= 0)
  2152. MCGetRcForDay(pmc, iOff, pst->wDay, &pmc->rcDayCur);
  2153. }
  2154. // returns zero-based index into DISPLAYED months for month
  2155. // if month is not in DISPLAYED months, then -1 is returned...
  2156. int MCGetOffsetForYrMo(MONTHCAL *pmc, int iYear, int iMonth)
  2157. {
  2158. int iOff;
  2159. iOff = ((int)iYear - pmc->stMonthFirst.wYear) * 12 + (int)iMonth - pmc->stMonthFirst.wMonth;
  2160. if (iOff < 0 || iOff >= pmc->nMonths)
  2161. return(-1);
  2162. return(iOff);
  2163. }
  2164. // iMonth is a zero-based index relative to the DISPLAYED months.
  2165. // iDay is a 1-based index of the day of the month,
  2166. void MCGetRcForDay(MONTHCAL *pmc, int iMonth, int iDay, RECT *prc)
  2167. {
  2168. RECT rc;
  2169. int iPlace, iRow, iCol;
  2170. MCGetRcForMonth(pmc, iMonth, &rc);
  2171. iPlace = pmc->rgcDay[iMonth] - pmc->rgnDayUL[iMonth] + iDay - 1;
  2172. iRow = iPlace / CALCOLMAX;
  2173. iCol = iPlace % CALCOLMAX;
  2174. prc->left = rc.left + pmc->rcDayNum.left + (pmc->dxCol * iCol);
  2175. prc->top = rc.top + pmc->rcDayNum.top + (pmc->dyRow * iRow);
  2176. prc->right = prc->left + pmc->dxCol;
  2177. prc->bottom = prc->top + pmc->dyRow;
  2178. }
  2179. //
  2180. // This routine gets the bounding rect for the iMonth of the displayed months.
  2181. // NOTE: iMonth is a zero-based index relative to the DISPLAYED months,
  2182. // counting along the rows.
  2183. //
  2184. void MCGetRcForMonth(MONTHCAL *pmc, int iMonth, RECT *prc)
  2185. {
  2186. int iRow, iCol, d;
  2187. iRow = iMonth / pmc->nViewCols;
  2188. iCol = iMonth % pmc->nViewCols;
  2189. // intialize the rect to be the bounding rect for the month in the
  2190. // top left corner
  2191. prc->left = pmc->rcCentered.left;
  2192. prc->right = prc->left + pmc->dxMonth;
  2193. prc->top = pmc->rcCentered.top;
  2194. prc->bottom = prc->top + pmc->dyMonth;
  2195. if (iCol) // slide the rect across to the correct column
  2196. {
  2197. d = (pmc->dxMonth + CALBORDER) * iCol;
  2198. prc->left += d;
  2199. prc->right += d;
  2200. }
  2201. if (iRow) // slide the rect down to the correct row
  2202. {
  2203. d = (pmc->dyMonth + CALBORDER) * iRow;
  2204. prc->top += d;
  2205. prc->bottom += d;
  2206. }
  2207. }
  2208. // Changes starting month by nDelta
  2209. // returns number of months actually changed
  2210. int FIncrStartMonth(MONTHCAL *pmc, int nDelta, BOOL fNoCurDayChange)
  2211. {
  2212. SYSTEMTIME stStart;
  2213. int nOldStartYear = pmc->stMonthFirst.wYear;
  2214. int nOldStartMonth = pmc->stMonthFirst.wMonth;
  2215. IncrSystemTime(&pmc->stMonthFirst, &stStart, nDelta, INCRSYS_MONTH);
  2216. // MCUpdateStartEndDates takes stMin/stMax into account
  2217. MCUpdateStartEndDates(pmc, &stStart);
  2218. if (!fNoCurDayChange)
  2219. {
  2220. int cday;
  2221. // REARCHITECT: we arbitrarily set the currently selected day
  2222. // to be in the new stMonthFirst, but given the way the
  2223. // control works, I doubt we ever hit this code. what's it for??
  2224. if (MonthCal_IsMultiSelect(pmc))
  2225. cday = DaysBetweenDates(&pmc->st, &pmc->stEndSel);
  2226. // need to set date for focus here
  2227. pmc->st.wMonth = pmc->stMonthFirst.wMonth;
  2228. pmc->st.wYear = pmc->stMonthFirst.wYear;
  2229. // Check to see if the day is in range, eg, Jan 31 -> Feb 28
  2230. if (pmc->st.wDay > pmc->rgcDay[1])
  2231. pmc->st.wDay = (WORD) pmc->rgcDay[1];
  2232. if (MonthCal_IsMultiSelect(pmc))
  2233. IncrSystemTime(&pmc->st, &pmc->stEndSel, cday, INCRSYS_DAY);
  2234. MCNotifySelChange(pmc, MCN_SELCHANGE);
  2235. MCUpdateRcDayCur(pmc, &pmc->st);
  2236. }
  2237. MCInvalidateMonthDays(pmc);
  2238. return((pmc->stMonthFirst.wYear-nOldStartYear)*12 + (pmc->stMonthFirst.wMonth-nOldStartMonth));
  2239. }
  2240. // FIncrStartMonth with a beep when it doesn't change.
  2241. int MCIncrStartMonth(MONTHCAL *pmc, int nDelta, BOOL fDelayDayChange)
  2242. {
  2243. int cmoSpun;
  2244. // FIncrStartMonth takes stMin/stMax into account
  2245. cmoSpun = FIncrStartMonth(pmc, nDelta, fDelayDayChange);
  2246. if (cmoSpun==0)
  2247. MessageBeep(0);
  2248. return(cmoSpun);
  2249. }
  2250. //
  2251. // Determines in which month the given point lies. In other words, if the
  2252. // calendar control is currently sized to show six months, this routine
  2253. // determines in which which of those six months the point lies. It returns
  2254. // the zero based index of the month, counting along the rows.
  2255. //
  2256. BOOL FGetOffsetForPt(MONTHCAL *pmc, POINT pt, int *piOffset)
  2257. {
  2258. int iRow, iCol, i;
  2259. // check to see if point is within the centered months
  2260. if (!PtInRect(&pmc->rcCentered, pt))
  2261. return(FALSE);
  2262. // calculate the month row and column
  2263. // (we're really fudging a little here, since the point could
  2264. // actually be within the space between months...)
  2265. iCol = (pt.x - pmc->rcCentered.left) / (pmc->dxMonth + CALBORDER);
  2266. iRow = (pt.y - pmc->rcCentered.top) / (pmc->dyMonth + CALBORDER);
  2267. i = iRow * pmc->nViewCols + iCol;
  2268. if (i >= pmc->nMonths)
  2269. return(FALSE);
  2270. *piOffset = i;
  2271. return(TRUE);
  2272. }
  2273. //
  2274. // This routine returns the row and column of day containing the given point
  2275. //
  2276. BOOL FGetRowColForRelPt(MONTHCAL *pmc, POINT ptRel, int *piRow, int *piCol)
  2277. {
  2278. if (!PtInRect(&pmc->rcDayNum, ptRel))
  2279. return(FALSE);
  2280. ptRel.x -= pmc->rcDayNum.left;
  2281. ptRel.y -= pmc->rcDayNum.top;
  2282. *piCol = ptRel.x / pmc->dxCol;
  2283. *piRow = ptRel.y / pmc->dyRow;
  2284. return(TRUE);
  2285. }
  2286. //
  2287. // This routine returns the month and year of the iMonth in the displayed
  2288. // months. NOTE: iMonth is a zero-based index of the displayed months
  2289. //
  2290. void GetYrMoForOffset(MONTHCAL *pmc, int iMonth, int *piYear, int *piMonth)
  2291. {
  2292. SYSTEMTIME st;
  2293. st.wDay = 1;
  2294. st.wMonth = pmc->stMonthFirst.wMonth;
  2295. st.wYear = pmc->stMonthFirst.wYear;
  2296. IncrSystemTime(&st, &st, iMonth, INCRSYS_MONTH);
  2297. *piYear = st.wYear;
  2298. *piMonth = st.wMonth;
  2299. }
  2300. //
  2301. // This routine returns, the day, month, and year of day containing the
  2302. // given point. It will optionally return the day of the month, the row and
  2303. // column in the month, and the bounding rect of the month containing the point.
  2304. // NOTE: the day returned in piDay can be less than 1 (to indicate a day in the
  2305. // previous month) or greater than the number of days in the month (to indicate
  2306. // a day in the next month).
  2307. //
  2308. BOOL FGetDateForPt(MONTHCAL *pmc, POINT pt, SYSTEMTIME *pst, int *piDay,
  2309. int* piCol, int* piRow, LPRECT prcMonth)
  2310. {
  2311. int iOff, iRow, iCol, iDay, iMon, iYear;
  2312. RECT rcMonth;
  2313. if (!FGetOffsetForPt(pmc, pt, &iOff))
  2314. return(FALSE);
  2315. MCGetRcForMonth(pmc, iOff, &rcMonth);
  2316. pt.x -= rcMonth.left;
  2317. pt.y -= rcMonth.top;
  2318. if (!FGetRowColForRelPt(pmc, pt, &iRow, &iCol))
  2319. return(FALSE);
  2320. // get the day containing the point by subtracting the number of days
  2321. // that are visible from the previous month, and then add one, since
  2322. // we are zero-based and the days of the month are 1-based.
  2323. //
  2324. iDay = iRow * CALCOLMAX + iCol - (pmc->rgcDay[iOff] - pmc->rgnDayUL[iOff]) + 1;
  2325. if (piDay)
  2326. *piDay = iDay;
  2327. if (iDay <= 0)
  2328. {
  2329. if (iOff)
  2330. return(FALSE); // dont accept days in prev month unless
  2331. // this happens to be the first month
  2332. iDay += pmc->rgcDay[iOff]; // add the cnt of days in the prev month,
  2333. --iOff; // then incr the month to get day in new month
  2334. }
  2335. else if (iDay > pmc->rgcDay[iOff+1])
  2336. {
  2337. if (iOff < (pmc->nMonths - 1)) // dont accept days in next month unless
  2338. return(FALSE); // this happens to be the last month
  2339. ++iOff; // increment the month, and then sub the
  2340. iDay -= pmc->rgcDay[iOff]; // count of days to get day in new month
  2341. }
  2342. GetYrMoForOffset(pmc, iOff, &iYear, &iMon);
  2343. pst->wDay = (WORD) iDay;
  2344. pst->wMonth = (WORD) iMon;
  2345. pst->wYear = (WORD) iYear;
  2346. if (piCol)
  2347. *piCol = iCol;
  2348. if (piRow)
  2349. *piRow = iRow;
  2350. if (prcMonth)
  2351. *prcMonth = rcMonth;
  2352. return(TRUE);
  2353. }
  2354. BOOL MCSetDate(MONTHCAL *pmc, SYSTEMTIME *pst)
  2355. {
  2356. int nDelta = 0;
  2357. //
  2358. // Can't set date outside of min/max range
  2359. //
  2360. if (CmpDate(pst, &pmc->stMin) < 0)
  2361. return FALSE;
  2362. if (CmpDate(pst, &pmc->stMax) > 0)
  2363. return FALSE;
  2364. //
  2365. // Set new day
  2366. //
  2367. pmc->st = *pst;
  2368. if (MonthCal_IsMultiSelect(pmc))
  2369. pmc->stEndSel = *pst;
  2370. FScrollIntoView(pmc);
  2371. MCNotifySelChange(pmc, MCN_SELCHANGE);
  2372. MCUpdateRcDayCur(pmc, pst);
  2373. return(TRUE);
  2374. }
  2375. void MCSetToday(MONTHCAL* pmc, SYSTEMTIME* pst)
  2376. {
  2377. SYSTEMTIME st;
  2378. RECT rc;
  2379. if (!pst)
  2380. {
  2381. GetLocalTime(&st);
  2382. pmc->fTodaySet = FALSE;
  2383. }
  2384. else
  2385. {
  2386. st = *pst;
  2387. pmc->fTodaySet = TRUE;
  2388. }
  2389. if (CmpDate(&st, &pmc->stToday) != 0)
  2390. {
  2391. MCGetRcForDay(pmc, pmc->iMonthToday, pmc->stToday.wDay, &rc);
  2392. InvalidateRect(pmc->ci.hwnd, &rc, FALSE);
  2393. pmc->stToday = st;
  2394. MCUpdateToday(pmc);
  2395. MCGetRcForDay(pmc, pmc->iMonthToday, pmc->stToday.wDay, &rc);
  2396. InvalidateRect(pmc->ci.hwnd, &rc, FALSE);
  2397. if (MonthCal_ShowToday(pmc))
  2398. {
  2399. MCGetTodayBtnRect(pmc, &rc);
  2400. InvalidateRect(pmc->ci.hwnd, &rc, FALSE);
  2401. }
  2402. UpdateWindow(pmc->ci.hwnd);
  2403. }
  2404. }
  2405. LRESULT MCHandleTimer(MONTHCAL *pmc, WPARAM wParam)
  2406. {
  2407. if (wParam == CAL_IDAUTOSPIN)
  2408. {
  2409. int nDelta = pmc->fMonthDelta ? pmc->nMonthDelta : pmc->nMonths;
  2410. // REARCHITECT: pass last parameter TRUE if multiselect! else you
  2411. // can't multiselect across months
  2412. MCIncrStartMonth(pmc, (pmc->fSpinPrev ? -nDelta : nDelta), FALSE);
  2413. if (pmc->idTimer == 0)
  2414. pmc->idTimer = SetTimer(pmc->ci.hwnd, CAL_IDAUTOSPIN, CAL_MSECAUTOSPIN, NULL);
  2415. pmc->rcDayOld = pmc->rcDayCur;
  2416. UpdateWindow(pmc->ci.hwnd);
  2417. }
  2418. else if (wParam == CAL_TODAYTIMER)
  2419. {
  2420. if (!pmc->fTodaySet)
  2421. MCSetToday(pmc, NULL);
  2422. }
  2423. MCNotifySelChange(pmc, MCN_SELCHANGE); // our date has changed
  2424. return((LRESULT)TRUE);
  2425. }
  2426. void MCInvalidateDates(MONTHCAL *pmc, SYSTEMTIME *pst1, SYSTEMTIME *pst2)
  2427. {
  2428. int iMonth, ioff, icol, irow;
  2429. RECT rc, rcMonth;
  2430. SYSTEMTIME st, stEnd;
  2431. if (CmpDate(pst1, &pmc->stViewLast) > 0 ||
  2432. CmpDate(pst2, &pmc->stViewFirst) < 0)
  2433. return;
  2434. if (CmpDate(pst1, &pmc->stViewFirst) < 0)
  2435. CopyDate(pmc->stViewFirst, st);
  2436. else
  2437. CopyDate(*pst1, st);
  2438. if (CmpDate(pst2, &pmc->stViewLast) > 0)
  2439. CopyDate(pmc->stViewLast, stEnd);
  2440. else
  2441. CopyDate(*pst2, stEnd);
  2442. iMonth = MCGetOffsetForYrMo(pmc, st.wYear, st.wMonth);
  2443. if (iMonth == -1)
  2444. {
  2445. if (st.wMonth == pmc->stViewFirst.wMonth)
  2446. {
  2447. iMonth = 0;
  2448. ioff = st.wDay - pmc->rgnDayUL[0] - 1;
  2449. }
  2450. else
  2451. {
  2452. iMonth = pmc->nMonths - 1;
  2453. ioff = st.wDay + pmc->rgcDay[pmc->nMonths] +
  2454. pmc->rgcDay[iMonth] - pmc->rgnDayUL[iMonth] - 1;
  2455. }
  2456. }
  2457. else
  2458. {
  2459. ioff = st.wDay + (pmc->rgcDay[iMonth] - pmc->rgnDayUL[iMonth]) - 1;
  2460. }
  2461. MCGetRcForMonth(pmc, iMonth, &rcMonth);
  2462. // TODO: make it more efficient...
  2463. while (CmpDate(&st, &stEnd) <= 0)
  2464. {
  2465. irow = ioff / CALCOLMAX;
  2466. icol = ioff % CALCOLMAX;
  2467. rc.left = rcMonth.left + pmc->rcDayNum.left + (pmc->dxCol * icol);
  2468. rc.top = rcMonth.top + pmc->rcDayNum.top + (pmc->dyRow * irow);
  2469. rc.right = rc.left + pmc->dxCol;
  2470. rc.bottom = rc.top + pmc->dyRow;
  2471. InvalidateRect(pmc->ci.hwnd, &rc, FALSE);
  2472. IncrSystemTime(&st, &st, 1, INCRSYS_DAY);
  2473. ioff++;
  2474. if (st.wDay == 1)
  2475. {
  2476. if (st.wMonth != pmc->stMonthFirst.wMonth &&
  2477. st.wMonth != pmc->stViewLast.wMonth)
  2478. {
  2479. iMonth++;
  2480. MCGetRcForMonth(pmc, iMonth, &rcMonth);
  2481. ioff = ioff % CALCOLMAX;
  2482. }
  2483. }
  2484. }
  2485. }
  2486. void MCHandleMultiSelect(MONTHCAL *pmc, SYSTEMTIME *pst)
  2487. {
  2488. int i;
  2489. DWORD cday;
  2490. SYSTEMTIME stStart, stEnd;
  2491. if (!pmc->fMultiSelecting)
  2492. {
  2493. CopyDate(*pst, stStart);
  2494. CopyDate(*pst, stEnd);
  2495. pmc->fMultiSelecting = TRUE;
  2496. pmc->fForwardSelect = TRUE;
  2497. CopyDate(pmc->st, pmc->stStartPrev);
  2498. CopyDate(pmc->stEndSel, pmc->stEndPrev);
  2499. }
  2500. else
  2501. {
  2502. if (pmc->fForwardSelect)
  2503. {
  2504. i = CmpDate(pst, &pmc->st);
  2505. if (i >= 0)
  2506. {
  2507. CopyDate(pmc->st, stStart);
  2508. CopyDate(*pst, stEnd);
  2509. }
  2510. else
  2511. {
  2512. CopyDate(*pst, stStart);
  2513. CopyDate(pmc->st, stEnd);
  2514. pmc->fForwardSelect = FALSE;
  2515. }
  2516. }
  2517. else
  2518. {
  2519. i = CmpDate(pst, &pmc->stEndSel);
  2520. if (i < 0)
  2521. {
  2522. CopyDate(*pst, stStart);
  2523. CopyDate(pmc->stEndSel, stEnd);
  2524. }
  2525. else
  2526. {
  2527. CopyDate(pmc->stEndSel, stStart);
  2528. CopyDate(*pst, stEnd);
  2529. pmc->fForwardSelect = TRUE;
  2530. }
  2531. }
  2532. }
  2533. // check to make sure not exceeding cSelMax
  2534. cday = DaysBetweenDates(&stStart, &stEnd) + 1;
  2535. if (cday > pmc->cSelMax)
  2536. {
  2537. if (pmc->fForwardSelect)
  2538. IncrSystemTime(&stStart, &stEnd, pmc->cSelMax - 1, INCRSYS_DAY);
  2539. else
  2540. IncrSystemTime(&stEnd, &stStart, 1 - pmc->cSelMax, INCRSYS_DAY);
  2541. }
  2542. if (0 == CmpDate(&stStart, &pmc->st) &&
  2543. 0 == CmpDate(&stEnd, &pmc->stEndSel))
  2544. return;
  2545. // TODO: do this more effeciently..
  2546. MCInvalidateDates(pmc, &pmc->st, &pmc->stEndSel);
  2547. MCInvalidateDates(pmc, &stStart, &stEnd);
  2548. CopyDate(stStart, pmc->st);
  2549. CopyDate(stEnd, pmc->stEndSel);
  2550. MCNotifySelChange(pmc, MCN_SELCHANGE);
  2551. UpdateWindow(pmc->ci.hwnd);
  2552. }
  2553. void MCGotoToday(MONTHCAL *pmc)
  2554. {
  2555. pmc->rcDayOld = pmc->rcDayCur;
  2556. // force old selection to get repainted
  2557. if (MonthCal_IsMultiSelect(pmc))
  2558. MCInvalidateDates(pmc, &pmc->st, &pmc->stEndSel);
  2559. else
  2560. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayOld, FALSE);
  2561. MCSetDate(pmc, &pmc->stToday);
  2562. MCNotifySelChange(pmc, MCN_SELECT);
  2563. // force new selection to get repainted
  2564. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayCur, FALSE);
  2565. UpdateWindow(pmc->ci.hwnd);
  2566. }
  2567. LRESULT MCContextMenu(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam)
  2568. {
  2569. POINT pt;
  2570. int click;
  2571. if (!pmc->fEnabled || !MonthCal_ShowToday(pmc))
  2572. return(0);
  2573. // ignore double click since this makes us advance twice
  2574. // since we already had a leftdown before the leftdblclk
  2575. if (!pmc->fCapture)
  2576. {
  2577. pt.x = GET_X_LPARAM(lParam);
  2578. pt.y = GET_Y_LPARAM(lParam);
  2579. //
  2580. // If the context menu was generated from the keyboard,
  2581. // then put it at the focus rectangle.
  2582. //
  2583. if (pt.x == -1 && pt.y == -1)
  2584. {
  2585. pt.x = (pmc->rcDayCur.left + pmc->rcDayCur.right ) / 2;
  2586. pt.y = (pmc->rcDayCur.top + pmc->rcDayCur.bottom) / 2;
  2587. ClientToScreen(pmc->ci.hwnd, &pt);
  2588. }
  2589. click = TrackPopupMenu(pmc->hmenuCtxt,
  2590. TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY,
  2591. pt.x, pt.y, 0, pmc->ci.hwnd, NULL);
  2592. if (click >= 1)
  2593. MCGotoToday(pmc);
  2594. }
  2595. return(0);
  2596. }
  2597. //
  2598. // Computes the bounding rects for the month and the year in the title area of
  2599. // the month.
  2600. //
  2601. void MCGetTitleRcsForOffset(MONTHCAL* pmc, int iOffset, LPRECT prcMonth, LPRECT prcYear)
  2602. {
  2603. RECT rcT;
  2604. RECT rc;
  2605. MCGetRcForMonth(pmc, iOffset, &rc);
  2606. rcT.top = rc.top + (pmc->dyRow / 2);
  2607. rcT.bottom = rcT.top + pmc->dyRow;
  2608. rcT.left = rc.left + pmc->rcMonthName.left + pmc->rgmm[iOffset].rgi[IMM_MONTHSTART];
  2609. rcT.right = rc.left + pmc->rcMonthName.left + pmc->rgmm[iOffset].rgi[IMM_MONTHEND];
  2610. *prcMonth = rcT;
  2611. rcT.left = rc.left + pmc->rcMonthName.left + pmc->rgmm[iOffset].rgi[IMM_YEARSTART];
  2612. rcT.right = rc.left + pmc->rcMonthName.left + pmc->rgmm[iOffset].rgi[IMM_YEAREND];
  2613. *prcYear = rcT;
  2614. }
  2615. LRESULT MCLButtonDown(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam)
  2616. {
  2617. HDC hdc;
  2618. POINT pt;
  2619. SYSTEMTIME st;
  2620. RECT rc, rcCal;
  2621. BOOL fShow;
  2622. MSG msg;
  2623. int offset, imonth, iyear;
  2624. if (!pmc->fEnabled)
  2625. return(0);
  2626. pt.x = GET_X_LPARAM(lParam);
  2627. pt.y = GET_Y_LPARAM(lParam);
  2628. // treat a shift click like an LMouseDown at the prev location and
  2629. // a MouseMove to the new location
  2630. if (MonthCal_IsMultiSelect(pmc) && ((wParam & MK_SHIFT) == MK_SHIFT) && (!PtInRect(&pmc->rcDayCur, pt)))
  2631. {
  2632. SetCapture(pmc->ci.hwnd);
  2633. pmc->fCapture = TRUE;
  2634. pmc->fForwardSelect = (CmpDate(&pmc->stAnchor, &pmc->st) != 0) ? FALSE : TRUE;
  2635. pmc->fMultiSelecting = TRUE;
  2636. hdc = GetDC(pmc->ci.hwnd);
  2637. DrawFocusRect(hdc, &pmc->rcDayCur); // draw focus rect
  2638. pmc->fFocusDrawn = TRUE;
  2639. ReleaseDC(pmc->ci.hwnd, hdc);
  2640. MCMouseMove(pmc, wParam, lParam); // draw the highlight to new date
  2641. return 0;
  2642. }
  2643. // ignore double click since this makes us advance twice
  2644. // since we already had a leftdown before the leftdblclk
  2645. if (!pmc->fCapture)
  2646. {
  2647. SetCapture(pmc->ci.hwnd);
  2648. pmc->fCapture = TRUE;
  2649. // check for spin buttons
  2650. if ((pmc->fSpinPrev = (WORD) PtInRect(&pmc->rcPrev, pt)) || PtInRect(&pmc->rcNext, pt))
  2651. {
  2652. MCHandleTimer(pmc, CAL_IDAUTOSPIN);
  2653. return(0);
  2654. }
  2655. // check for valid day
  2656. pmc->rcDayOld = pmc->rcDayCur; // rcDayCur should always be valid now
  2657. if (MonthCal_IsMultiSelect(pmc))
  2658. {
  2659. // need to cache these values because these are how
  2660. // we determine if the selection has changed and we
  2661. // need to notify the parent
  2662. CopyDate(pmc->st, pmc->stStartPrev);
  2663. CopyDate(pmc->stEndSel, pmc->stEndPrev);
  2664. }
  2665. if (FUpdateRcDayCur(pmc, pt))
  2666. {
  2667. if (MonthCal_IsMultiSelect(pmc))
  2668. {
  2669. if (FGetDateForPt(pmc, pt, &st, NULL, NULL, NULL, NULL))
  2670. MCHandleMultiSelect(pmc, &st);
  2671. }
  2672. hdc = GetDC(pmc->ci.hwnd);
  2673. DrawFocusRect(hdc, &pmc->rcDayCur); // draw focus rect
  2674. pmc->fFocusDrawn = TRUE;
  2675. ReleaseDC(pmc->ci.hwnd, hdc);
  2676. CopyDate(st, pmc->stAnchor); // new Anchor point
  2677. }
  2678. else
  2679. {
  2680. RECT rcMonth, rcYear;
  2681. int delta, year, month;
  2682. // is this a click in the today area...
  2683. if (MonthCal_ShowToday(pmc))
  2684. {
  2685. MCGetTodayBtnRect(pmc, &rc);
  2686. if (PtInRect(&rc, pt))
  2687. {
  2688. CCReleaseCapture(&pmc->ci);
  2689. pmc->fCapture = FALSE;
  2690. MCGotoToday(pmc);
  2691. return(0);
  2692. }
  2693. }
  2694. // figure out if the click was in a month name or a year
  2695. if (!FGetOffsetForPt(pmc, pt, &offset))
  2696. return(0);
  2697. GetYrMoForOffset(pmc, offset, &year, &month);
  2698. // calculate where the month name and year are,
  2699. // so we can figure out if they clicked in them...
  2700. MCGetTitleRcsForOffset(pmc, offset, &rcMonth, &rcYear);
  2701. delta = 0;
  2702. if (PtInRect(&rcMonth, pt))
  2703. {
  2704. CCReleaseCapture(&pmc->ci);
  2705. pmc->fCapture = FALSE;
  2706. ClientToScreen(pmc->ci.hwnd, &pt);
  2707. imonth = TrackPopupMenu(pmc->hmenuMonth,
  2708. TPM_LEFTALIGN | TPM_TOPALIGN |
  2709. TPM_NONOTIFY | TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_RIGHTBUTTON,
  2710. pt.x, pt.y, 0, pmc->ci.hwnd, NULL);
  2711. if (imonth >= 1)
  2712. delta = imonth - month;
  2713. goto ChangeMonth;
  2714. }
  2715. if (PtInRect(&rcYear, pt))
  2716. {
  2717. HWND hwndEdit, hwndUD, hwndFocus;
  2718. int yrMin, yrMax;
  2719. DWORD dwExStyle = 0L;
  2720. CCReleaseCapture(&pmc->ci);
  2721. pmc->fCapture = FALSE;
  2722. //
  2723. // If the year is in a RTL string, then numeric control
  2724. // is to the left.
  2725. //
  2726. if (pmc->fHeaderRTL)
  2727. {
  2728. rcYear.left = (rcYear.right - (pmc->dxYearMax + 6));
  2729. }
  2730. else
  2731. {
  2732. rcYear.right = rcYear.left + pmc->dxYearMax + 6;
  2733. }
  2734. rcYear.top--;
  2735. rcYear.bottom++;
  2736. if(((pmc->fHeaderRTL) && !(IS_WINDOW_RTL_MIRRORED(pmc->ci.hwnd))) ||
  2737. (!(pmc->fHeaderRTL) && (IS_WINDOW_RTL_MIRRORED(pmc->ci.hwnd))))
  2738. {
  2739. // not mirrored force RTL, mirrored force LTR (for mirroring RTLis LTR!!)
  2740. dwExStyle|= WS_EX_RTLREADING;
  2741. }
  2742. hwndEdit = CreateWindowEx(dwExStyle, TEXT("EDIT"), NULL,
  2743. WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY | ES_LEFT | ES_AUTOHSCROLL,
  2744. rcYear.left, rcYear.top, rcYear.right - rcYear.left, rcYear.bottom - rcYear.top,
  2745. pmc->ci.hwnd, (HMENU)0, pmc->hinstance, NULL);
  2746. if (hwndEdit == NULL)
  2747. return(0);
  2748. pmc->hwndEdit = hwndEdit;
  2749. SendMessage(hwndEdit, WM_SETFONT, (WPARAM)pmc->hfontBold, (LPARAM)FALSE);
  2750. SendMessage(hwndEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN,
  2751. (LPARAM)MAKELONG(1, 1));
  2752. MCUpdateEditYear(pmc);
  2753. //
  2754. // Convert from Gregorian to display years.
  2755. //
  2756. year = GregorianToOther(&pmc->ct, year);
  2757. yrMin = GregorianToOther(&pmc->ct, pmc->stMin.wYear);
  2758. yrMax = 9999;
  2759. if (pmc->fMaxYrSet)
  2760. yrMax = GregorianToOther(&pmc->ct, pmc->stMax.wYear);
  2761. hwndUD = CreateUpDownControl(
  2762. WS_CHILD | WS_VISIBLE | WS_BORDER |
  2763. UDS_NOTHOUSANDS | UDS_ARROWKEYS,// | UDS_SETBUDDYINT,
  2764. pmc->fHeaderRTL ? (rcYear.left - 1 - (rcYear.bottom-rcYear.top)): (rcYear.right + 1),
  2765. rcYear.top,
  2766. rcYear.bottom - rcYear.top, rcYear.bottom - rcYear.top, pmc->ci.hwnd,
  2767. 1, pmc->hinstance, hwndEdit, yrMax, yrMin, year);
  2768. if (hwndUD == NULL)
  2769. {
  2770. DestroyWindow(hwndEdit);
  2771. return(0);
  2772. }
  2773. pmc->hwndUD = hwndUD;
  2774. hwndFocus = SetFocus(hwndEdit);
  2775. //
  2776. // Widen the area depending on the string direction.
  2777. //
  2778. if (pmc->fHeaderRTL)
  2779. rcYear.left -= (1 + rcYear.bottom - rcYear.top);
  2780. else
  2781. rcYear.right += 1 + rcYear.bottom - rcYear.top;
  2782. // Use MapWindowRect, It works in a mirrored and unmirrored windows.
  2783. MapWindowRect(pmc->ci.hwnd, NULL, (LPPOINT)&rcYear);
  2784. rcCal = pmc->rc;
  2785. MapWindowRect(pmc->ci.hwnd, NULL, (LPPOINT)&rcCal);
  2786. fShow = TRUE;
  2787. while (fShow && GetFocus() == hwndEdit)
  2788. {
  2789. if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
  2790. {
  2791. // Check for events that cause the calendar to go away
  2792. if (msg.message == WM_KILLFOCUS ||
  2793. (msg.message >= WM_SYSKEYDOWN &&
  2794. msg.message <= WM_SYSDEADCHAR))
  2795. {
  2796. fShow = FALSE;
  2797. }
  2798. else if ((msg.message == WM_LBUTTONDOWN ||
  2799. msg.message == WM_NCLBUTTONDOWN ||
  2800. msg.message == WM_RBUTTONDOWN ||
  2801. msg.message == WM_NCRBUTTONDOWN ||
  2802. msg.message == WM_MBUTTONDOWN ||
  2803. msg.message == WM_NCMBUTTONDOWN) &&
  2804. !PtInRect(&rcYear, msg.pt))
  2805. {
  2806. fShow = FALSE;
  2807. // if its a button down inside the calendar, eat it
  2808. // so the calendar doesn't do anything strange when
  2809. // the user is just trying to get rid of the year edit
  2810. if (PtInRect(&rcCal, msg.pt))
  2811. GetMessage(&msg, NULL, 0, 0);
  2812. break; // do not dispatch
  2813. }
  2814. else if (msg.message == WM_QUIT)
  2815. { // Don't dispatch a WM_QUIT; leave it in the queue
  2816. break; // do not dispatch
  2817. }
  2818. else if (msg.message == WM_CHAR)
  2819. {
  2820. if (msg.wParam == VK_ESCAPE)
  2821. {
  2822. goto NoYearChange;
  2823. }
  2824. else if (msg.wParam == VK_RETURN)
  2825. {
  2826. fShow = FALSE;
  2827. }
  2828. }
  2829. GetMessage(&msg, NULL, 0, 0);
  2830. TranslateMessage(&msg);
  2831. DispatchMessage(&msg);
  2832. }
  2833. else
  2834. WaitMessage();
  2835. }
  2836. iyear = (int) SendMessage(hwndUD, UDM_GETPOS, 0, 0);
  2837. if (HIWORD(iyear) == 0)
  2838. delta = (iyear - year) * 12;
  2839. NoYearChange:
  2840. DestroyWindow(hwndUD);
  2841. DestroyWindow(hwndEdit);
  2842. pmc->hwndUD = NULL;
  2843. pmc->hwndEdit = NULL;
  2844. UpdateWindow(pmc->ci.hwnd);
  2845. if (hwndFocus != NULL)
  2846. SetFocus(hwndFocus);
  2847. }
  2848. ChangeMonth:
  2849. if (delta != 0)
  2850. {
  2851. MCIncrStartMonth(pmc, delta, FALSE);
  2852. MCNotifySelChange(pmc,MCN_SELCHANGE);
  2853. }
  2854. }
  2855. }
  2856. return(0);
  2857. }
  2858. LRESULT MCLButtonUp(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam)
  2859. {
  2860. HDC hdc;
  2861. SYSTEMTIME st;
  2862. POINT pt;
  2863. if (pmc->fCapture)
  2864. {
  2865. CCReleaseCapture(&pmc->ci);
  2866. pmc->fCapture = FALSE;
  2867. if (pmc->idTimer)
  2868. {
  2869. KillTimer(pmc->ci.hwnd, pmc->idTimer);
  2870. pmc->idTimer = 0;
  2871. hdc = GetDC(pmc->ci.hwnd);
  2872. MCPaintArrowBtn(pmc, hdc, pmc->fSpinPrev, FALSE);
  2873. ReleaseDC(pmc->ci.hwnd, hdc);
  2874. return(0);
  2875. }
  2876. if (pmc->fFocusDrawn)
  2877. {
  2878. hdc = GetDC(pmc->ci.hwnd);
  2879. DrawFocusRect(hdc, &pmc->rcDayCur); // erase old focus rect
  2880. pmc->fFocusDrawn = FALSE;
  2881. ReleaseDC(pmc->ci.hwnd, hdc);
  2882. }
  2883. pt.x = GET_X_LPARAM(lParam);
  2884. pt.y = GET_Y_LPARAM(lParam);
  2885. if (MonthCal_IsMultiSelect(pmc))
  2886. {
  2887. FUpdateRcDayCur(pmc, pt);
  2888. if (!EqualRect(&pmc->rcDayOld, &pmc->rcDayCur))
  2889. {
  2890. if (FGetDateForPt(pmc, pt, &st, NULL, NULL, NULL, NULL))
  2891. MCHandleMultiSelect(pmc, &st);
  2892. }
  2893. pmc->fMultiSelecting = FALSE;
  2894. if (0 != CmpDate(&pmc->stStartPrev, &pmc->st) ||
  2895. 0 != CmpDate(&pmc->stEndPrev, &pmc->stEndSel))
  2896. {
  2897. FScrollIntoView(pmc);
  2898. }
  2899. MCNotifySelChange(pmc, MCN_SELECT);
  2900. }
  2901. else
  2902. {
  2903. if (FUpdateRcDayCur(pmc, pt))
  2904. {
  2905. if (!EqualRect(&pmc->rcDayOld, &pmc->rcDayCur) && (FGetDateForPt(pmc, pt, &st, NULL, NULL, NULL, NULL)))
  2906. {
  2907. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayOld, FALSE);
  2908. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayCur, FALSE);
  2909. MCSetDate(pmc, &st);
  2910. }
  2911. MCNotifySelChange(pmc, MCN_SELECT);
  2912. }
  2913. }
  2914. }
  2915. return(0);
  2916. }
  2917. LRESULT MCMouseMove(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam)
  2918. {
  2919. BOOL fPrev;
  2920. HDC hdc;
  2921. POINT pt;
  2922. SYSTEMTIME st;
  2923. if (pmc->fCapture)
  2924. {
  2925. pt.x = GET_X_LPARAM(lParam);
  2926. pt.y = GET_Y_LPARAM(lParam);
  2927. // check spin buttons
  2928. if ((fPrev = PtInRect(&pmc->rcPrev, pt)) || PtInRect(&pmc->rcNext, pt))
  2929. {
  2930. if (pmc->idTimer == 0)
  2931. {
  2932. pmc->fSpinPrev = (WORD) fPrev;
  2933. MCHandleTimer(pmc, CAL_IDAUTOSPIN);
  2934. }
  2935. return(0);
  2936. }
  2937. else
  2938. {
  2939. hdc = GetDC(pmc->ci.hwnd);
  2940. if (pmc->idTimer)
  2941. {
  2942. KillTimer(pmc->ci.hwnd, pmc->idTimer);
  2943. pmc->idTimer = 0;
  2944. MCPaintArrowBtn(pmc, hdc, pmc->fSpinPrev, FALSE);
  2945. }
  2946. }
  2947. // check days
  2948. if (!PtInRect(&pmc->rcDayCur, pt))
  2949. {
  2950. if (pmc->fFocusDrawn)
  2951. DrawFocusRect(hdc, &pmc->rcDayCur); // erase focus rect
  2952. if (pmc->fFocusDrawn = (WORD) FUpdateRcDayCur(pmc, pt))
  2953. {
  2954. // moved into a new valid day
  2955. if (pmc->fMultiSelecting)
  2956. {
  2957. if (FGetDateForPt(pmc, pt, &st, NULL, NULL, NULL, NULL))
  2958. MCHandleMultiSelect(pmc, &st);
  2959. }
  2960. DrawFocusRect(hdc, &pmc->rcDayCur);
  2961. }
  2962. else
  2963. {
  2964. // moved into an invalid position
  2965. pmc->rcDayCur = pmc->rcDayOld;
  2966. }
  2967. }
  2968. else if (!pmc->fFocusDrawn)
  2969. {
  2970. // handle case where we just moved back into rcDayCur from invalid area
  2971. DrawFocusRect(hdc, &pmc->rcDayCur);
  2972. pmc->fFocusDrawn = TRUE;
  2973. }
  2974. ReleaseDC(pmc->ci.hwnd, hdc);
  2975. }
  2976. return(0);
  2977. }
  2978. LRESULT MCHandleKeydown(MONTHCAL *pmc, WPARAM wParam, LPARAM lParam)
  2979. {
  2980. LONG lIncrement;
  2981. int iDirection;
  2982. SYSTEMTIME st;
  2983. BOOL fRet = FALSE;
  2984. HDC hdc = NULL;
  2985. RECT rcCurFocus;
  2986. // REARCHITECT: raymondc ERA - need to invalidate month title when selection
  2987. // moves in/out/within
  2988. switch (wParam)
  2989. {
  2990. case VK_CONTROL:
  2991. pmc->fControl = TRUE; // we'll clear this on WM_KEYUP
  2992. return TRUE;
  2993. break;
  2994. case VK_SHIFT:
  2995. pmc->fShift = TRUE; // we'll clear this on WM_KEYUP
  2996. return TRUE;
  2997. break;
  2998. case VK_LEFT: // goto previous day
  2999. iDirection = -1;
  3000. lIncrement = INCRSYS_DAY;
  3001. break;
  3002. case VK_RIGHT: // goto next day
  3003. iDirection = 1;
  3004. lIncrement = INCRSYS_DAY;
  3005. break;
  3006. case VK_UP: // goto previous week
  3007. iDirection = -1;
  3008. lIncrement = INCRSYS_WEEK;
  3009. break;
  3010. case VK_DOWN: // goto next week
  3011. iDirection = 1;
  3012. lIncrement = INCRSYS_WEEK;
  3013. break;
  3014. case VK_NEXT:
  3015. iDirection = 1;
  3016. if (pmc->fControl) // goto next year
  3017. lIncrement = INCRSYS_YEAR;
  3018. else // goto next month
  3019. lIncrement = INCRSYS_MONTH;
  3020. break;
  3021. case VK_PRIOR:
  3022. iDirection = -1;
  3023. if (pmc->fControl) // goto previous year
  3024. lIncrement = INCRSYS_YEAR;
  3025. else
  3026. lIncrement = INCRSYS_MONTH; // goto next month
  3027. break;
  3028. case VK_HOME:
  3029. if (pmc->fControl) // goto first visible month
  3030. {
  3031. CopyDate(pmc->stMonthFirst, st);
  3032. }
  3033. else // goto first day of current month
  3034. {
  3035. CopyDate(pmc->st, st);
  3036. st.wDay = 1;
  3037. }
  3038. goto setDate;
  3039. break;
  3040. case VK_END:
  3041. if (pmc->fControl) // goto last visible month
  3042. {
  3043. CopyDate(pmc->stMonthLast, st);
  3044. }
  3045. else // goto last day of current month
  3046. {
  3047. CopyDate(pmc->st, st);
  3048. st.wDay = (WORD) GetDaysForMonth(st.wYear, st.wMonth);
  3049. }
  3050. goto setDate;
  3051. break;
  3052. default:
  3053. return FALSE;
  3054. }
  3055. // if we're multiselecting, we need to know which "end" of the selection
  3056. // the user is moving.
  3057. if (pmc->fMultiSelecting && pmc->fForwardSelect)
  3058. CopyDate(pmc->stEndSel, st);
  3059. else
  3060. CopyDate(pmc->st, st);
  3061. IncrSystemTime(&st, &st, iDirection, lIncrement);
  3062. setDate:
  3063. // based on the window style and the shift key state,
  3064. // we'll do a multi-select (or not)
  3065. if (MonthCal_IsMultiSelect(pmc) && pmc->fShift)
  3066. {
  3067. pmc->fForwardSelect = (CmpDate(&pmc->st, &pmc->stAnchor) >= 0) ? TRUE : FALSE;
  3068. pmc->fMultiSelecting = TRUE;
  3069. }
  3070. // otherwise, we'll end multiselect, and set the new anchor
  3071. else
  3072. {
  3073. pmc->fMultiSelecting = FALSE;
  3074. CopyDate(st, pmc->stAnchor);
  3075. }
  3076. if (pmc->fFocusDrawn) // erase the focus rect, but don't clear the bit
  3077. { // so we know to put it back
  3078. hdc = GetDC(pmc->ci.hwnd);
  3079. DrawFocusRect(hdc, &pmc->rcDayCur);
  3080. ReleaseDC(pmc->ci.hwnd, hdc);
  3081. rcCurFocus = pmc->rcDayCur;
  3082. }
  3083. else
  3084. {
  3085. pmc->rcDayOld = pmc->rcDayCur;
  3086. }
  3087. if (MonthCal_IsMultiSelect(pmc))
  3088. {
  3089. int nDelta = 0;
  3090. MCHandleMultiSelect(pmc, &st);
  3091. FScrollIntoView(pmc);
  3092. }
  3093. else if (fRet = MCSetDate(pmc, &st))
  3094. {
  3095. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayOld, FALSE);
  3096. InvalidateRect(pmc->ci.hwnd, &pmc->rcDayCur, FALSE);
  3097. UpdateWindow(pmc->ci.hwnd);
  3098. }
  3099. if (pmc->fFocusDrawn) // put the focus rect back
  3100. {
  3101. pmc->rcDayOld = pmc->rcDayCur;
  3102. pmc->rcDayCur = rcCurFocus;
  3103. hdc = GetDC(pmc->ci.hwnd);
  3104. DrawFocusRect(hdc, &pmc->rcDayCur);
  3105. ReleaseDC(pmc->ci.hwnd, hdc);
  3106. }
  3107. return fRet;
  3108. }
  3109. //
  3110. // Era information is kept in a DPA of LocalAlloc'd strings.
  3111. //
  3112. int MCDPAEnumCallback(LPVOID d, LPVOID p)
  3113. {
  3114. UNREFERENCED_PARAMETER(p);
  3115. if (d)
  3116. LocalFree(d);
  3117. return TRUE;
  3118. }
  3119. void MCDPADestroy(HDPA hdpa)
  3120. {
  3121. if (hdpa)
  3122. DPA_DestroyCallback(hdpa, MCDPAEnumCallback, 0);
  3123. }
  3124. //
  3125. // Collect era information.
  3126. //
  3127. // Since EnumCalendarInfo is not thread-safe, we have to take the critical
  3128. // section.
  3129. HDPA g_hdpaCal;
  3130. BOOL MCEnumCalInfoProc(LPWSTR psz)
  3131. {
  3132. LPWSTR pwszSave = StrDup(psz);
  3133. if (pwszSave) {
  3134. if (DPA_AppendPtr(g_hdpaCal, pwszSave) >= 0) {
  3135. return TRUE;
  3136. }
  3137. LocalFree(pwszSave);
  3138. }
  3139. //
  3140. // Out of memory. Bail.
  3141. //
  3142. MCDPADestroy(g_hdpaCal);
  3143. g_hdpaCal = NULL;
  3144. return FALSE;
  3145. }
  3146. HDPA MCGetCalInfoDPA(CALID calid, CALTYPE calType)
  3147. {
  3148. HDPA hdpa = DPA_Create(4);
  3149. ENTERCRITICAL;
  3150. ASSERT(g_hdpaCal == NULL);
  3151. g_hdpaCal = hdpa;
  3152. EnumCalendarInfoW(MCEnumCalInfoProc, LOCALE_USER_DEFAULT, calid, calType);
  3153. hdpa = g_hdpaCal;
  3154. g_hdpaCal = NULL;
  3155. LEAVECRITICAL;
  3156. return hdpa;
  3157. }
  3158. void MCFreeCalendarInfo(PCALENDARTYPE pct)
  3159. {
  3160. MCDPADestroy(pct->hdpaYears);
  3161. MCDPADestroy(pct->hdpaEras);
  3162. pct->hdpaYears = 0;
  3163. pct->hdpaEras = 0;
  3164. }
  3165. //
  3166. // Get all the era info and validate it so we don't fault when we try to
  3167. // use them.
  3168. //
  3169. BOOL MCGetEraInfo(PCALENDARTYPE pct)
  3170. {
  3171. int i;
  3172. pct->hdpaYears = MCGetCalInfoDPA(pct->calid, CAL_IYEAROFFSETRANGE);
  3173. if (!pct->hdpaYears)
  3174. goto Bad;
  3175. pct->hdpaEras = MCGetCalInfoDPA(pct->calid, CAL_SERASTRING);
  3176. if (!pct->hdpaEras)
  3177. goto Bad;
  3178. // There must be at least one era...
  3179. if (!DPA_GetPtrCount(pct->hdpaEras))
  3180. goto Bad;
  3181. // The number of eras must be equal to the number of era names
  3182. if (DPA_GetPtrCount(pct->hdpaEras) != DPA_GetPtrCount(pct->hdpaYears))
  3183. goto Bad;
  3184. // The era dates must be in descending order.
  3185. for (i = 1; i < DPA_GetPtrCount(pct->hdpaYears); i++)
  3186. {
  3187. if (StrToInt(DPA_FastGetPtr(pct->hdpaYears, i)) >
  3188. StrToInt(DPA_FastGetPtr(pct->hdpaYears, i - 1)))
  3189. goto Bad;
  3190. }
  3191. return TRUE;
  3192. Bad:
  3193. /*
  3194. * Something went wrong, so clean up.
  3195. */
  3196. MCFreeCalendarInfo(pct);
  3197. return FALSE;
  3198. }
  3199. //
  3200. // Check to see if this calendar is not supported currently
  3201. //
  3202. // Return FALSE for Hijri, Hebrew calendars, since these are
  3203. // Lunar calndars. This is hack so that this control behaves well when the calendar
  3204. // is any of the non-supported till we add this support to this control. [samera]
  3205. //
  3206. void MCGetCalendarInfo(PCALENDARTYPE pct)
  3207. {
  3208. TCHAR tchCalendar[32];
  3209. CALTYPE defCalendar = CAL_GREGORIAN;
  3210. if (GetLocaleInfo(LOCALE_USER_DEFAULT,
  3211. LOCALE_ICALENDARTYPE,
  3212. tchCalendar,
  3213. ARRAYSIZE(tchCalendar)))
  3214. {
  3215. defCalendar = StrToInt(tchCalendar);
  3216. }
  3217. //
  3218. // Start with a clean slate. Assume we don't have to do funky
  3219. // offset stuff (dyrOFfset = 0) or era stuff (hdpaEras = NULL),
  3220. // and that we don't need to do locale munging (LOCALE_USER_DEFAULT).
  3221. //
  3222. MCFreeCalendarInfo(pct);
  3223. ZeroMemory(pct, SIZEOF(CALTYPE));
  3224. pct->calid = defCalendar;
  3225. pct->lcid = LOCALE_USER_DEFAULT;
  3226. switch (pct->calid) {
  3227. case CAL_GREGORIAN:
  3228. case CAL_GREGORIAN_US:
  3229. case CAL_GREGORIAN_ME_FRENCH:
  3230. case CAL_GREGORIAN_ARABIC:
  3231. case CAL_GREGORIAN_XLIT_ENGLISH:
  3232. case CAL_GREGORIAN_XLIT_FRENCH:
  3233. break; // Gregorian calendars are just fine
  3234. case CAL_JAPAN:
  3235. case CAL_TAIWAN:
  3236. //
  3237. // These are era calendars. Go get the era info. Get hdpaEras
  3238. // last so we can use it to test whether we have a supported era
  3239. // calendar.
  3240. //
  3241. // If not enough memory to support traditional calendar, then just
  3242. // force Gregorian. Hey, at least we display *something*.
  3243. //
  3244. if (!MCGetEraInfo(pct))
  3245. goto ForceGregorian;
  3246. break;
  3247. case CAL_THAI:
  3248. pct->dyrOffset = BUDDHIST_BIAS; // You Just Have To Know this number
  3249. break;
  3250. case CAL_KOREA:
  3251. pct->dyrOffset = KOREAN_BIAS; // You Just Have To Know this number
  3252. break;
  3253. default:
  3254. //
  3255. // If the calenday isn't supported, then treat it as Gregorian. [samera]
  3256. //
  3257. ForceGregorian:
  3258. pct->calid = CAL_GREGORIAN;
  3259. pct->lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  3260. break;
  3261. }
  3262. }
  3263. //
  3264. // Check whether the date string returned accorind to the
  3265. // current user-locale and calendar setting is Right-To-Left (RTL),
  3266. // and is so the RECTs for month year (@LBUTTONDOWN and NCHITTEST)
  3267. // needs to be adjusted. [samera]
  3268. //
  3269. BOOL MCIsDateStringRTL(TCHAR tch)
  3270. {
  3271. WORD wAttrib=0;
  3272. LCID lcidUserDefault;
  3273. BOOL fRTL = FALSE;
  3274. lcidUserDefault = GetUserDefaultLCID();
  3275. if (lcidUserDefault)
  3276. {
  3277. //
  3278. // Return TRUE if the 1st character is a RTL string.
  3279. // A RTL char followed by a european number will
  3280. // display visually as "european-num RTL-string" since the
  3281. // BiDi layout algorithm of the language-pack will do
  3282. // this. [samera]
  3283. //
  3284. if(GetStringTypeEx(lcidUserDefault,
  3285. CT_CTYPE2,
  3286. &tch,
  3287. 1,
  3288. &wAttrib))
  3289. {
  3290. if(C2_RIGHTTOLEFT == wAttrib)
  3291. {
  3292. fRTL = TRUE;
  3293. }
  3294. }
  3295. }
  3296. return fRTL;
  3297. }
  3298. ////////////////////////////////////////////////////////////////////////////
  3299. //
  3300. // Date/Time Picker
  3301. //
  3302. ////////////////////////////////////////////////////////////////////////////
  3303. //
  3304. // Subedit wrapper for the various weird things we need to get from NLS.
  3305. //
  3306. // SE_YEARALT means that the year field is only two digits wide
  3307. // in the format string, so we need to perform special Y2K enhancements.
  3308. // For these fields, the field is displayed in two-digit format, but
  3309. // when you go to edit the field, it temporarily changes to four-digit
  3310. // format so you can change the century too. Then when you complete the
  3311. // edit, it returns to two-digit format.
  3312. //
  3313. // The SE_YEARLIKE macro detects either SE_YEAR or SE_YEARALT.
  3314. //
  3315. // SE_MONTHALT is just like SE_MONTH except that it is used
  3316. // when the day (dd) comes before the month (mmm). This is
  3317. // important for languages like Russian where "12 October"
  3318. // and "October 12" use different strings for the word
  3319. // "October". There is no way to get the alternate string
  3320. // directly, except by creating a bogus date format that also
  3321. // has the day before the month, then throwing away the day.
  3322. //
  3323. // For example, if the incoming date string is
  3324. //
  3325. // "MMMM dd yyyy"
  3326. //
  3327. // we break it up into
  3328. //
  3329. // "MMMM" SE_MONTH
  3330. // " " SE_STATIC
  3331. // "dd" SE_DAY
  3332. // " " SE_STATIC
  3333. // "yyyy" SE_YEAR
  3334. //
  3335. // However, if the incoming date is
  3336. //
  3337. // "dd MMMM yyyy"
  3338. //
  3339. // we break it up into
  3340. //
  3341. // "dd" SE_DAY
  3342. // " " SE_STATIC
  3343. // "ddMMMM" SE_MONTHALT
  3344. // " " SE_STATIC
  3345. // "yyyy" SE_YEAR
  3346. //
  3347. // The extra "dd" at the beginning of SE_MONTHALT is stripped out below.
  3348. //
  3349. // Y2K weirdness: If we are getting the date format for the year,
  3350. // and the year is being edited (either due to a SUBEDIT_ALL or because
  3351. // it is the active subedit), and the format year is only
  3352. // two digits, then force a four-digit year for editing purposes.
  3353. //
  3354. //
  3355. void SEGetTimeDateFormat(LPSUBEDIT pse, LPSUBEDITCONTROL psec, LPTSTR pszBuf, DWORD cchBuf)
  3356. {
  3357. int cch;
  3358. ASSERT(cchBuf >= 2); // We assume it can hold at least space and a null
  3359. pszBuf[0] = TEXT('\0'); // In case something fails
  3360. if (pse->id == SE_MONTHALT)
  3361. {
  3362. TCHAR tszBuf[DTP_FORMATLENGTH + 3];
  3363. //
  3364. // When we parsed the date string and realized that we needed
  3365. // the Alternate Month format, we created a date format of
  3366. // the form "ddMMM...", where "MMM..." is the
  3367. // the original month format. Here we strip off the digits.
  3368. //
  3369. cch = GetDateFormat(psec->ct.lcid, 0, &psec->st, pse->pv, tszBuf, ARRAYSIZE(tszBuf));
  3370. if (cch >= 2)
  3371. {
  3372. // [msadek] For Hebrew calander, day format "dd" is actually
  3373. // two OR THREE characters. Don't hardcode it as 2.
  3374. int cchDay = GetDateFormat(psec->ct.lcid, 0, &psec->st, TEXT("dd"), NULL, 0);
  3375. StringCchCopy(pszBuf, cchBuf, tszBuf + cchDay - 1);
  3376. }
  3377. }
  3378. else if (pse->id == SE_YEARALT &&
  3379. (psec->iseCur == SUBEDIT_ALL || pse == &psec->pse[psec->iseCur]))
  3380. {
  3381. GetDateFormat(psec->ct.lcid, 0, &psec->st, TEXT("yyyy"), pszBuf, cchBuf);
  3382. }
  3383. else if (SE_DATELIKE(pse->id))
  3384. {
  3385. GetDateFormat(psec->ct.lcid, 0, &psec->st, pse->pv, pszBuf, cchBuf);
  3386. // Change a blank era to a space so the user can see it
  3387. if (pse->id == SE_ERA && pszBuf[0] == TEXT('\0'))
  3388. {
  3389. pszBuf[0] = TEXT(' ');
  3390. pszBuf[1] = TEXT('\0');
  3391. }
  3392. }
  3393. else if (pse->id != SE_APP)
  3394. {
  3395. GetTimeFormat(LOCALE_USER_DEFAULT, 0, &psec->st, pse->pv, pszBuf, cchBuf);
  3396. }
  3397. else
  3398. {
  3399. NMDATETIMEFORMAT nmdtf = { 0 };
  3400. nmdtf.pszFormat = pse->pv;
  3401. SECGetSystemtime(psec, &nmdtf.st);
  3402. nmdtf.pszDisplay = nmdtf.szDisplay;
  3403. CCSendNotify(psec->pci, DTN_FORMAT, &nmdtf.nmhdr);
  3404. StringCchCopy(pszBuf, cchBuf, nmdtf.pszDisplay);
  3405. //
  3406. // If the parent is an ANSI window, and pszDisplay
  3407. // does not equal szDisplay, the then thunk had to
  3408. // allocated memory for pszDisplay. We need to
  3409. // free it here.
  3410. //
  3411. if (!psec->pci->bUnicode && nmdtf.pszDisplay &&
  3412. nmdtf.pszDisplay != nmdtf.szDisplay) {
  3413. LocalFree ((LPSTR)nmdtf.pszDisplay);
  3414. }
  3415. }
  3416. }
  3417. //
  3418. // SUBEDIT stuff for DateTimePicker
  3419. //
  3420. // NOTE: Now that the DatePicker and TimePicker are combined,
  3421. // this could be moved back into the parent structure.
  3422. //
  3423. //
  3424. // Used in an era calendar to get the length of the longest era name.
  3425. // Also leaves a random heigth in psize->cy because that's what the
  3426. // non-era code does, too.
  3427. //
  3428. int SECGetMaxEraLength(PCALENDARTYPE pct, HDC hdc, PSIZE psize)
  3429. {
  3430. int i;
  3431. int wid = 0;
  3432. for (i = 0; i < DPA_GetPtrCount(pct->hdpaEras); i++)
  3433. {
  3434. LPCTSTR ptsz = DPA_FastGetPtr(pct->hdpaEras, i);
  3435. if (GetTextExtentPoint32(hdc, ptsz, lstrlen(ptsz), psize) &&
  3436. psize->cx > wid)
  3437. {
  3438. wid = psize->cx;
  3439. }
  3440. }
  3441. return wid;
  3442. }
  3443. // SECRecomputeSizing needs to calculate the maximum rectangle each subedit can be. Ugh.
  3444. //
  3445. // The size of the SE_YEARALT field changes depending on whether or not it is
  3446. // the current psec->iseCur. Double ugh.
  3447. void SECRecomputeSizing(LPSUBEDITCONTROL psec, LPRECT prc)
  3448. {
  3449. HDC hdc;
  3450. HGDIOBJ hfontOrig;
  3451. int i;
  3452. LPSUBEDIT pse;
  3453. int left = prc->left;
  3454. psec->rc = *prc;
  3455. hdc = GetDC(psec->pci->hwnd);
  3456. hfontOrig = SelectObject(hdc, (HGDIOBJ)psec->hfont);
  3457. for (i=0, pse=psec->pse; i < psec->cse ; i++, pse++)
  3458. {
  3459. TCHAR szTmp[DTP_FORMATLENGTH];
  3460. LPCTSTR sz;
  3461. int min, max;
  3462. SIZE size;
  3463. int wid;
  3464. min = pse->min;
  3465. max = pse->max;
  3466. if (pse->id == SE_STATIC)
  3467. {
  3468. ASSERT(pse->fReadOnly);
  3469. sz = pse->pv;
  3470. }
  3471. else
  3472. {
  3473. sz = szTmp;
  3474. // make some assumptions so we don't loop more than we have to
  3475. switch (pse->id)
  3476. {
  3477. // we only need seven for the text days of the week
  3478. case SE_DAY:
  3479. min = 10; // make them all double-digit
  3480. max = 17;
  3481. break;
  3482. // Assume we only have numeric output with all chars same width
  3483. case SE_MARK:
  3484. min = 11;
  3485. max = 12;
  3486. break;
  3487. case SE_HOUR:
  3488. case SE_YEAR:
  3489. case SE_YEARALT:
  3490. case SE_MINUTE:
  3491. case SE_SECOND:
  3492. min = max;
  3493. break;
  3494. case SE_ERA:
  3495. if (ISERACALENDAR(&psec->ct)) {
  3496. wid = SECGetMaxEraLength(&psec->ct, hdc, &size);
  3497. goto HaveWidth;
  3498. } else {
  3499. min = max = *pse->pval; // current value is good enough
  3500. }
  3501. break;
  3502. }
  3503. }
  3504. // now get max width
  3505. if (pse->id == SE_APP)
  3506. {
  3507. NMDATETIMEFORMATQUERY nmdtfq = {0};
  3508. nmdtfq.pszFormat = pse->pv;
  3509. CCSendNotify(psec->pci, DTN_FORMATQUERY, &nmdtfq.nmhdr);
  3510. size = nmdtfq.szMax;
  3511. wid = nmdtfq.szMax.cx;
  3512. }
  3513. else
  3514. {
  3515. SYSTEMTIME st = psec->st;
  3516. /*
  3517. * SUBTLE - Munge the month/day to January 1. This solves
  3518. * lots of problems, such as "Today is Feb 29 1996, and
  3519. * when we iterate through year = 1997, we get Feb 29 1997,
  3520. * which is invalid." Or "Today is Jan 31 1999, and when
  3521. * we iterate through the months, we get Sep 31 1999, which
  3522. * is invalid."
  3523. *
  3524. * We choose "January 1" because
  3525. *
  3526. * 1. Every year has a "January 1", so the year can vary.
  3527. * 2. Every month has a "first", so the month can vary.
  3528. * 3. Every day up to 31 is valid in January, so the day can vary.
  3529. */
  3530. psec->st.wMonth = psec->st.wDay = 1;
  3531. for (wid = 0 ; min <= max ; min++)
  3532. {
  3533. if (pse->id != SE_STATIC)
  3534. {
  3535. *pse->pval = (WORD) min;
  3536. SEGetTimeDateFormat(pse, psec, szTmp, ARRAYSIZE(szTmp));
  3537. if (szTmp[0] == TEXT('\0'))
  3538. {
  3539. DebugMsg(TF_ERROR, TEXT("SECRecomputeSizing: GetDate/TimeFormat([%s] y=%d m=%d d=%d h=%d m=%d s=%d) = ERROR %d"),
  3540. pse->pv, psec->st.wYear, psec->st.wMonth, psec->st.wDay, psec->st.wHour, psec->st.wMinute, psec->st.wSecond,
  3541. GetLastError());
  3542. }
  3543. }
  3544. if (!GetTextExtentPoint32(hdc, sz, lstrlen(sz), &size))
  3545. {
  3546. size.cx = 0;
  3547. DebugMsg(TF_MONTHCAL,TEXT("SECRecomputeSizing: GetTextExtentPoint32(%s) = ERROR %d"), sz, GetLastError());
  3548. }
  3549. if (size.cx > wid)
  3550. wid = size.cx;
  3551. }
  3552. psec->st = st;
  3553. }
  3554. HaveWidth:
  3555. // now set up subedit's bounding rectangle
  3556. pse->rc.top = prc->top + SECYBORDER;
  3557. pse->rc.bottom = pse->rc.top + size.cy;
  3558. pse->rc.left = left;
  3559. pse->rc.right = left + wid;
  3560. left = pse->rc.right;
  3561. }
  3562. SelectObject(hdc, hfontOrig);
  3563. ReleaseDC(psec->pci->hwnd, hdc);
  3564. }
  3565. // InitSubEditControl parses szFormat into psec, setting the time to pst.
  3566. TCHAR c_szFormats[] = TEXT("gyMdthHmsX");
  3567. BOOL SECParseFormat(DATEPICK* pdp, LPSUBEDITCONTROL psec, LPCTSTR szFormat)
  3568. {
  3569. LPCTSTR pFmt;
  3570. LPTSTR psecFmt;
  3571. int cse, cchExtra;
  3572. int nTmp;
  3573. LPSUBEDIT pse;
  3574. BOOL fDaySeen = FALSE;
  3575. BOOL fForceCentury = FALSE;
  3576. int iLen, i;
  3577. TCHAR tch;
  3578. LPTSTR pFmtTemp;
  3579. TCHAR szFormatTemp[DTP_FORMATLENGTH];
  3580. //
  3581. // We need to force the century if the format is
  3582. // DTS_SHORTDATECENTURYFORMAT.
  3583. //
  3584. if (pdp->fLocale &&
  3585. (pdp->ci.style & DTS_FORMATMASK) == DTS_SHORTDATECENTURYFORMAT)
  3586. {
  3587. fForceCentury = TRUE;
  3588. }
  3589. // [msadek]; If we need to mirror the format and the
  3590. // cleint passed a read only buffer, we will AV (W2k bug# 354533)
  3591. // Let's copy it first
  3592. if (psec->fMirrorSEC)
  3593. {
  3594. StringCchPrintf(szFormatTemp, ARRAYSIZE(szFormatTemp), szFormat);
  3595. szFormat = szFormatTemp;
  3596. }
  3597. // count szFormat sections so we know what to allocate
  3598. pFmt = szFormat;
  3599. cse = 0;
  3600. cchExtra = 0;
  3601. while (*pFmt)
  3602. {
  3603. if (StrChr(c_szFormats, *pFmt)) // format string
  3604. {
  3605. TCHAR c = *pFmt;
  3606. while (c == *pFmt)
  3607. pFmt++;
  3608. cse++;
  3609. // If it was a string Month format, reserve 2 more chars for
  3610. // the possible "dd" leader, in case we need SE_MONTHALT.
  3611. if (c == TEXT('M'))
  3612. cchExtra += 2;
  3613. }
  3614. else if (*pFmt == TEXT('\'')) // quoted static string
  3615. {
  3616. KeepSearching:
  3617. pFmt++;
  3618. while (*pFmt && *pFmt != TEXT('\''))
  3619. pFmt++;
  3620. if (*pFmt) // handle poorly quoted strings
  3621. {
  3622. pFmt++;
  3623. if (*pFmt == TEXT('\'')) // quoted quote, not end of quote
  3624. goto KeepSearching;
  3625. }
  3626. cse++;
  3627. }
  3628. else // static string probably a delimiter
  3629. {
  3630. while (*pFmt && *pFmt!=TEXT('\'') && !StrChr(c_szFormats, *pFmt))
  3631. pFmt++;
  3632. cse++;
  3633. }
  3634. }
  3635. // Allocate space
  3636. nTmp = cse + lstrlen(szFormat) + cchExtra + 1; // number of chars
  3637. nTmp = nTmp * sizeof(TCHAR); // size in BYTES
  3638. nTmp = ROUND_TO_POINTER(nTmp); // round up to POINTER boundary
  3639. psecFmt = (LPTSTR)LocalAlloc(LPTR, nTmp + cse * sizeof(SUBEDIT));
  3640. if (!psecFmt)
  3641. {
  3642. DebugMsg(TF_MONTHCAL, TEXT("SECParseFormat failed to allocate memory"));
  3643. return FALSE; // use whatever we already have
  3644. }
  3645. if (psec->szFormat)
  3646. LocalFree(psec->szFormat);
  3647. psec->szFormat = psecFmt;
  3648. psec->cDelimeter = '\0';
  3649. psec->pse = (LPSUBEDIT)((LPBYTE)psecFmt + nTmp);
  3650. // Fill psec
  3651. psec->iseCur = SUBEDIT_NONE;
  3652. psec->cse = cse;
  3653. pse = psec->pse;
  3654. ZeroMemory(pse, cse * sizeof(SUBEDIT));
  3655. pFmt = szFormat;
  3656. pdp->fHasMark = FALSE;
  3657. //
  3658. // Before start parsing the format string, let's mirror it if requested.
  3659. //
  3660. if (psec->fMirrorSEC)
  3661. {
  3662. pFmtTemp = (LPTSTR)pFmt;
  3663. iLen = lstrlen(pFmtTemp);
  3664. for( i=0 ; i<iLen/2 ; i++ )
  3665. {
  3666. tch = pFmtTemp[i];
  3667. pFmtTemp[i] = pFmtTemp[iLen-i-1];
  3668. pFmtTemp[iLen-i-1] = tch;
  3669. }
  3670. }
  3671. while (*pFmt)
  3672. {
  3673. pse->flDrawText = DT_CENTER;
  3674. if (*pFmt == TEXT('y') || *pFmt == TEXT('g')) // y=year g=era
  3675. {
  3676. TCHAR ch = *pFmt;
  3677. // If the calendar doesn't use eras, then the era field is just
  3678. // for show and can't be changed.
  3679. if (ch == TEXT('g') && !ISERACALENDAR(&psec->ct)) {
  3680. pse->fReadOnly = TRUE;
  3681. }
  3682. pse->id = ch == TEXT('y') ? SE_YEAR : SE_ERA;
  3683. pse->pval = &psec->st.wYear;
  3684. pse->min = c_stEpoch.wYear;
  3685. pse->max = c_stArmageddon.wYear;
  3686. pse->cchMax = 0;
  3687. pse->pv = psecFmt;
  3688. while (*pFmt == ch) {
  3689. pse->cchMax++;
  3690. *psecFmt++ = *pFmt++;
  3691. }
  3692. if (pse->id == SE_YEAR)
  3693. {
  3694. pse->flDrawText = DT_RIGHT;
  3695. if (fForceCentury)
  3696. {
  3697. pse->pv = TEXT("yyyy");
  3698. pse->cchMax = 4;
  3699. }
  3700. else
  3701. {
  3702. switch (pse->cchMax)
  3703. {
  3704. case 1: // "y" is a SE_YEARALT
  3705. case 2: // "yy" is a SE_YEARALT
  3706. pse->id = SE_YEARALT;
  3707. pse->cchMax = 4; // Force four-digit editing
  3708. break;
  3709. case 3: // "yyy" is an alias for "yyyy".
  3710. pse->cchMax = 4;
  3711. break;
  3712. }
  3713. }
  3714. }
  3715. *psecFmt++ = TEXT('\0');
  3716. }
  3717. else if (*pFmt == TEXT('M')) // month
  3718. {
  3719. pse->pv = psecFmt;
  3720. // If the day has been seen, then we need to use the alternate
  3721. // month format, so set up the gratuitous "dd" prefix.
  3722. // See SEGetTimeDateFormat.
  3723. //
  3724. if (fDaySeen) {
  3725. pse->id = SE_MONTHALT;
  3726. *psecFmt++ = TEXT('d');
  3727. *psecFmt++ = TEXT('d');
  3728. } else {
  3729. pse->id = SE_MONTH;
  3730. }
  3731. pse->pval = &psec->st.wMonth;
  3732. pse->min = 1;
  3733. pse->max = 12;
  3734. pse->cchMax = 2;
  3735. while (*pFmt == TEXT('M'))
  3736. *psecFmt++ = *pFmt++;
  3737. if (psecFmt - pse->pv <= 2)
  3738. pse->flDrawText = DT_RIGHT;
  3739. *psecFmt++ = TEXT('\0');
  3740. }
  3741. else if (*pFmt == TEXT('d')) // day or day of week
  3742. {
  3743. fDaySeen = TRUE; // See SEGetTimeDateFormat
  3744. pse->id = SE_DAY;
  3745. pse->pval = &psec->st.wDay;
  3746. pse->min = 1;
  3747. pse->max = GetDaysForMonth(psec->st.wYear, psec->st.wMonth);
  3748. pse->cchMax = 2;
  3749. pse->pv = psecFmt;
  3750. while (*pFmt == TEXT('d'))
  3751. *psecFmt++ = *pFmt++;
  3752. if (psecFmt - pse->pv <= 2)
  3753. pse->flDrawText = DT_RIGHT; // day
  3754. else
  3755. pse->fReadOnly = TRUE; // day of week
  3756. *psecFmt++ = TEXT('\0');
  3757. }
  3758. else if (*pFmt == TEXT('t')) // marker
  3759. {
  3760. pdp->fHasMark = TRUE;
  3761. pse->id = SE_MARK;
  3762. pse->pval = &psec->st.wHour;
  3763. pse->min = 0;
  3764. pse->max = 23;
  3765. pse->cIncrement = 12;
  3766. pse->cchMax = 2;
  3767. pse->pv = psecFmt;
  3768. while (*pFmt == TEXT('t'))
  3769. *psecFmt++ = *pFmt++;
  3770. *psecFmt++ = TEXT('\0');
  3771. }
  3772. else if (*pFmt == TEXT('h')) // (12) hour
  3773. {
  3774. pse->id = SE_HOUR;
  3775. pse->pval = &psec->st.wHour;
  3776. pse->min = 0;
  3777. pse->max = 23;
  3778. pse->cchMax = 2;
  3779. pse->flDrawText = DT_RIGHT;
  3780. pse->pv = psecFmt;
  3781. while (*pFmt == TEXT('h'))
  3782. *psecFmt++ = *pFmt++;
  3783. *psecFmt++ = TEXT('\0');
  3784. }
  3785. else if (*pFmt == TEXT('H')) // (24) hour
  3786. {
  3787. pse->id = SE_HOUR;
  3788. pse->pval = &psec->st.wHour;
  3789. pse->min = 0;
  3790. pse->max = 23;
  3791. pse->cchMax = 2;
  3792. pse->flDrawText = DT_RIGHT;
  3793. pse->pv = psecFmt;
  3794. while (*pFmt == TEXT('H'))
  3795. *psecFmt++ = *pFmt++;
  3796. *psecFmt++ = TEXT('\0');
  3797. }
  3798. else if (*pFmt == TEXT('m')) // minute
  3799. {
  3800. pse->id = SE_MINUTE;
  3801. pse->pval = &psec->st.wMinute;
  3802. pse->min = 0;
  3803. pse->max = 59;
  3804. pse->cchMax = 2;
  3805. pse->flDrawText = DT_RIGHT;
  3806. pse->pv = psecFmt;
  3807. while (*pFmt == TEXT('m'))
  3808. *psecFmt++ = *pFmt++;
  3809. *psecFmt++ = TEXT('\0');
  3810. }
  3811. else if (*pFmt == TEXT('s')) // second
  3812. {
  3813. pse->id = SE_SECOND;
  3814. pse->pval = &psec->st.wSecond;
  3815. pse->min = 0;
  3816. pse->max = 59;
  3817. pse->cchMax = 2;
  3818. pse->flDrawText = DT_RIGHT;
  3819. pse->pv = psecFmt;
  3820. while (*pFmt == TEXT('s'))
  3821. *psecFmt++ = *pFmt++;
  3822. *psecFmt++ = TEXT('\0');
  3823. }
  3824. else if (*pFmt == TEXT('X')) // app specified field
  3825. {
  3826. pse->id = SE_APP;
  3827. pse->pv = psecFmt;
  3828. while (*pFmt == TEXT('X'))
  3829. *psecFmt++ = *pFmt++;
  3830. *psecFmt++ = TEXT('\0');
  3831. }
  3832. else if (*pFmt == TEXT('\'')) // quoted static string
  3833. {
  3834. pse->id = SE_STATIC;
  3835. pse->fReadOnly = TRUE;
  3836. pse->pv = psecFmt;
  3837. SearchSomeMore:
  3838. pFmt++;
  3839. while (*pFmt && *pFmt != TEXT('\''))
  3840. *psecFmt++ = *pFmt++;
  3841. if (*pFmt) // handle poorly quoted strings
  3842. {
  3843. pFmt++;
  3844. if (*pFmt == TEXT('\'')) // quoted quote, not end of quote
  3845. {
  3846. *psecFmt++ = *pFmt;
  3847. goto SearchSomeMore;
  3848. }
  3849. }
  3850. *psecFmt++ = TEXT('\0');
  3851. }
  3852. else // unknown non-editable stuff (most likely a delimeter)
  3853. {
  3854. // FEATURE: even though it's unknown, we should probably pass
  3855. // it off to GetDateFormat so we will be forward compatible
  3856. // with future date formats...
  3857. //
  3858. pse->id = SE_STATIC;
  3859. pse->fReadOnly = TRUE;
  3860. if (!psec->cDelimeter)
  3861. psec->cDelimeter = *pFmt;
  3862. pse->pv = psecFmt;
  3863. while (*pFmt && *pFmt!=TEXT('\'') && !StrChr(c_szFormats, *pFmt))
  3864. *psecFmt++ = *pFmt++;
  3865. *psecFmt++ = TEXT('\0');
  3866. // we'll assume that the first not formatting char is the
  3867. // delimeter...maybe not a great assumption, but it will work
  3868. // most of the time.
  3869. //
  3870. }
  3871. pse++;
  3872. }
  3873. #ifdef DEBUG
  3874. {
  3875. TCHAR sz[200];
  3876. LPTSTR psz;
  3877. psz = sz;
  3878. sz[0]=TEXT('\0');
  3879. pse = psec->pse;
  3880. cse = psec->cse;
  3881. while (cse > 0)
  3882. {
  3883. StringCchPrintf(psz, ARRAYSIZE(sz), TEXT("[%s] "), pse->pv);
  3884. psz = psz + lstrlen(psz);
  3885. cse--;
  3886. pse++;
  3887. }
  3888. DebugMsg(TF_MONTHCAL, TEXT("SECParseFormat: %s"), sz);
  3889. }
  3890. #endif
  3891. //
  3892. // Let restore the original format
  3893. //
  3894. if (psec->fMirrorSEC)
  3895. {
  3896. pFmtTemp = (LPTSTR)szFormat;
  3897. for( i=0 ; i<iLen/2 ; i++ )
  3898. {
  3899. tch = pFmtTemp[i];
  3900. pFmtTemp[i] = pFmtTemp[iLen-i-1];
  3901. pFmtTemp[iLen-i-1] = tch;
  3902. }
  3903. }
  3904. //
  3905. // If this is a time-only DTP control and we need to swap the AM/PM symbol
  3906. // to the other side, then let's do it.
  3907. //
  3908. if (psec->fSwapTimeMarker)
  3909. {
  3910. SUBEDIT se;
  3911. pse = psec->pse;
  3912. cse = psec->cse;
  3913. if ((cse > 1) && (psec->pse[0].id == SE_MARK))
  3914. {
  3915. se = psec->pse[0];
  3916. i = 0;
  3917. while( i < (cse-1) )
  3918. {
  3919. pse[i] = pse[i+1];
  3920. i++;
  3921. }
  3922. pse[psec->cse-1] = se;
  3923. }
  3924. }
  3925. // The subedits have changed, recompute sizes
  3926. SECRecomputeSizing(psec, &psec->rc);
  3927. // We're going to need to redraw this
  3928. InvalidateRect(psec->pci->hwnd, NULL, TRUE);
  3929. // Changing the format also changes the window text.
  3930. NotifyWinEvent(EVENT_OBJECT_VALUECHANGE, pdp->ci.hwnd, OBJID_CLIENT, INDEXID_CONTAINER);
  3931. return TRUE;
  3932. }
  3933. void SECDestroy(LPSUBEDITCONTROL psec)
  3934. {
  3935. if (psec->szFormat)
  3936. {
  3937. LocalFree(psec->szFormat);
  3938. psec->szFormat = NULL;
  3939. }
  3940. }
  3941. void SECSetFont(LPSUBEDITCONTROL psec, HFONT hfont)
  3942. {
  3943. if (hfont == NULL)
  3944. hfont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
  3945. psec->hfont = hfont;
  3946. }
  3947. void InvalidateScrollRect(HWND hwnd, RECT *prc, int xScroll)
  3948. {
  3949. RECT rc;
  3950. if (xScroll)
  3951. {
  3952. rc = *prc;
  3953. OffsetRect(&rc, -xScroll, 0);
  3954. prc = &rc;
  3955. }
  3956. InvalidateRect(hwnd, prc, TRUE);
  3957. }
  3958. void SECSaveResetSubeditEdit(DATEPICK *pdp, BOOL fReset);
  3959. #define SECSaveSubeditEdit(pdp) SECSaveResetSubeditEdit(pdp, FALSE)
  3960. #define SECResetSubeditEdit(pdp) SECSaveResetSubeditEdit(pdp, TRUE)
  3961. // Set the current subedit, scrolling things into view as needed
  3962. void SECSetCurSubed(DATEPICK *pdp, int isubed)
  3963. {
  3964. LPSUBEDITCONTROL psec = &pdp->sec;
  3965. // validate the arguments
  3966. ASSERT(isubed < psec->cse);
  3967. // if the subedit is changing, we need to invalidate stuff
  3968. if (isubed != psec->iseCur)
  3969. {
  3970. int isePre;
  3971. if (psec->iseCur >= 0)
  3972. {
  3973. SECResetSubeditEdit(pdp);
  3974. InvalidateScrollRect(psec->pci->hwnd, &psec->pse[psec->iseCur].rc, psec->xScroll);
  3975. }
  3976. isePre = psec->iseCur;
  3977. psec->iseCur = isubed;
  3978. // For perf reasons, do a full recompute only if SE_YEARALT or
  3979. // SUBEDIT_ALL was involved, since those are the only cases
  3980. // where SE_YEARALT fields change size.
  3981. #define YearAffected(psec, ise) \
  3982. (ise == SUBEDIT_ALL || \
  3983. (ise >= 0 && psec->pse[ise].id == SE_YEARALT))
  3984. if (YearAffected(psec, isePre) || YearAffected(psec, isubed))
  3985. {
  3986. SECRecomputeSizing(psec, &psec->rc);
  3987. InvalidateRect(psec->pci->hwnd, NULL, TRUE);
  3988. }
  3989. #undef YearAffected
  3990. if (psec->iseCur >= 0)
  3991. {
  3992. RECT rc = psec->pse[psec->iseCur].rc;
  3993. OffsetRect(&rc, -psec->xScroll, 0);
  3994. if (rc.left < psec->rc.left)
  3995. {
  3996. psec->xScroll += rc.left - psec->rc.left;
  3997. InvalidateRect(psec->pci->hwnd, NULL, TRUE);
  3998. }
  3999. else if (rc.right > psec->rc.right)
  4000. {
  4001. psec->xScroll += rc.right - psec->rc.right;
  4002. InvalidateRect(psec->pci->hwnd, NULL, TRUE);
  4003. }
  4004. else
  4005. {
  4006. InvalidateRect(psec->pci->hwnd, &rc, TRUE);
  4007. }
  4008. }
  4009. }
  4010. }
  4011. int SECIncrFocus(DATEPICK *pdp, int delta)
  4012. {
  4013. int ise, loop;
  4014. LPSUBEDITCONTROL psec = &pdp->sec;
  4015. ASSERT(-1 == delta || 1 == delta);
  4016. ise = psec->iseCur;
  4017. if (ise < 0 && delta < 0)
  4018. ise = psec->cse;
  4019. for (loop = 0 ; loop < psec->cse ; loop++)
  4020. {
  4021. int oldise = ise;
  4022. ise = (ise + delta + psec->cse) % psec->cse;
  4023. if (ise != oldise+delta && psec->fNone)
  4024. {
  4025. // we wrapped and we allow scrolling into SUBEDIT_NONE state
  4026. break;
  4027. }
  4028. if (!psec->pse[ise].fReadOnly)
  4029. {
  4030. goto Found;
  4031. }
  4032. }
  4033. ise = SUBEDIT_NONE;
  4034. Found:
  4035. SECSetCurSubed(pdp, ise);
  4036. return ise;
  4037. }
  4038. void SECInvalidate(LPSUBEDITCONTROL psec, int id);
  4039. //
  4040. // Given a Gregorian year, get the local name for that year.
  4041. //
  4042. UINT SECGetYearValue(DATEPICK *pdp, UINT uYear)
  4043. {
  4044. UINT uiRc = 0;
  4045. TCHAR rgch[64];
  4046. if (EVAL(MCGetDateFormatWithTempYear(&pdp->sec.ct, &pdp->sec.st, TEXT("yyyy"), uYear, rgch, ARRAYSIZE(rgch)))) {
  4047. uiRc = StrToInt(rgch);
  4048. }
  4049. return uiRc;
  4050. }
  4051. //
  4052. // SECAdjustByEra
  4053. //
  4054. // pct - PCALENDARTYPE structure to use for conversion
  4055. //
  4056. // uInput - the value the user typed (to be interpreted as local calendar)
  4057. //
  4058. // The basic idea is that if you type a year, it is interpreted relative
  4059. // to the era you were in previously. If the number you typed isn't valid
  4060. // for that era, then reject it (by returning the original year unchanged).
  4061. //
  4062. UINT SECAdjustByEra(DATEPICK *pdp, UINT uInput)
  4063. {
  4064. UINT uResult = pdp->sec.st.wYear;
  4065. //
  4066. // Find the delta between the current local year and the current
  4067. // Gregorian year. We don't use any of the era transition dates
  4068. // since they aren't reliable at the boundaries. Just convert it
  4069. // to a display name and re-parse it back.
  4070. //
  4071. UINT uDelta = pdp->sec.st.wYear - SECGetYearValue(pdp, pdp->sec.st.wYear);
  4072. //
  4073. // Apply that delta to the year the user typed in. This converts the
  4074. // local year into a Gregorian year.
  4075. //
  4076. UINT uNewVal = uInput + uDelta;
  4077. // uNewVal is the value we want to change it to. If it's valid for that
  4078. // era, then use it. We detect that it's okay for the era by converting
  4079. // it to a display name and seeing if it matches. It can fail for being
  4080. // too large (past the end of the era) or too small (trying to change to
  4081. // January 1 local year 1 when the era didn't change until March).
  4082. if (SECGetYearValue(pdp, uNewVal) == uInput) {
  4083. uResult = uNewVal;
  4084. }
  4085. return uResult;
  4086. }
  4087. //
  4088. // SECAdjustByType
  4089. //
  4090. // Some field types are special.
  4091. //
  4092. // SE_YEAR or SE_YEARALT if user typed only two digits
  4093. //
  4094. // Use the "implied century for two-digit years" logic
  4095. // as described in GetCalendarInfoA.
  4096. //
  4097. // SE_YEAR or SE_YEARALT if user typed more than two digits
  4098. //
  4099. // Use that number.
  4100. //
  4101. // BONUS FEATURE!
  4102. //
  4103. // Some calendars run parallel to the Gregorian year,
  4104. // but with different years. Use GregorianToOther and
  4105. // OtherToGregorian to convert.
  4106. //
  4107. // The input value is the local year (Gregorian, Buddhist, whatever)
  4108. // but the return value is always the Gregorian year, since that's
  4109. // what SYSTEMTIME uses.
  4110. //
  4111. // SE_HOUR
  4112. //
  4113. // If the clock is in 12-hour format, then preserve AM/PM ness of
  4114. // the hour. For example, if it was 3pm and somebody is changing
  4115. // the hour to 4, use 4pm instead of 4am.
  4116. //
  4117. UINT SECAdjustByType(DATEPICK *pdp, LPSUBEDIT psubed, UINT uNewValue)
  4118. {
  4119. if (SE_YEARLIKE(psubed->id))
  4120. {
  4121. if (uNewValue < 100)
  4122. {
  4123. // Get the preferred century of the preferred calendar
  4124. // (in the localized year, not Gregorian.)
  4125. DWORD dwMax2DigitYear;
  4126. if (!GetCalendarInfoA(pdp->sec.ct.lcid, pdp->sec.ct.calid, CAL_RETURN_NUMBER + CAL_ITWODIGITYEARMAX,
  4127. NULL, 0, &dwMax2DigitYear))
  4128. {
  4129. // default in the absence of all information
  4130. dwMax2DigitYear = GregorianToOther(&pdp->sec.ct, 2029);
  4131. // if the current year in this era is less than 100, then the 2 digits typed
  4132. // may be the real date, so set the max to 99 (i.e., no conversion)
  4133. //
  4134. if (dwMax2DigitYear < 99)
  4135. dwMax2DigitYear = 99;
  4136. }
  4137. //
  4138. // Copy the century of dwMax2DigitYear into uNewValue.
  4139. //
  4140. uNewValue += (dwMax2DigitYear - dwMax2DigitYear % 100);
  4141. //
  4142. // If it exceeds the max, then drop to previous century.
  4143. //
  4144. if (uNewValue > dwMax2DigitYear)
  4145. uNewValue -= 100;
  4146. }
  4147. //
  4148. // Finally, convert back to Gregorian as necessary.
  4149. //
  4150. uNewValue = OtherToGregorian(&pdp->sec.ct, uNewValue);
  4151. //
  4152. // If we are in an Era calendar, then we need to adjust the
  4153. // year relative to the ambient era.
  4154. //
  4155. if (ISERACALENDAR(&pdp->sec.ct)) {
  4156. uNewValue = SECAdjustByEra(pdp, uNewValue);
  4157. }
  4158. } else if (psubed->id == SE_HOUR && psubed->pv[0] == TEXT('h')) {
  4159. if (*psubed->pval >= 12 && uNewValue < 12)
  4160. uNewValue += 12;
  4161. }
  4162. return uNewValue;
  4163. }
  4164. void SECSetSubeditValue(DATEPICK *pdp, LPSUBEDIT psubed, UINT uNewValue, BOOL fForce)
  4165. {
  4166. LPSUBEDITCONTROL psec = &pdp->sec;
  4167. UINT uOldValue;
  4168. uNewValue = SECAdjustByType(pdp, psubed, uNewValue);
  4169. //
  4170. // Must do a full-on range check in addition to the simple psubed->min
  4171. // psubed->max range check because the new value might be valid
  4172. // for our range but not in the global scheme of things. For example,
  4173. // the minimum date is Sep 14 1752, but if today is Jan 1 1995 and
  4174. // the user types "1752", that will pass the simple min/max year test,
  4175. // but it's not a valid date since Jan 1 1752 is out of range.
  4176. //
  4177. uOldValue = *psubed->pval;
  4178. *psubed->pval = (WORD)uNewValue;
  4179. if (uNewValue >= psubed->min && uNewValue <= psubed->max &&
  4180. CmpSystemtime(&pdp->sec.st, &pdp->stMin) >= 0 &&
  4181. CmpSystemtime(&pdp->sec.st, &pdp->stMax) <= 0)
  4182. {
  4183. if (fForce || uNewValue != uOldValue)
  4184. {
  4185. SECInvalidate(psec, SE_APP);
  4186. InvalidateScrollRect(psec->pci->hwnd, &psubed->rc, psec->xScroll);
  4187. DPNotifyDateChange(pdp);
  4188. }
  4189. }
  4190. else
  4191. {
  4192. // Oops, not valid, put the old value back
  4193. *psubed->pval = (WORD)uOldValue;
  4194. }
  4195. }
  4196. // This saves the current pending value and also resets the edit state
  4197. // if fReset is TRUE
  4198. void SECSaveResetSubeditEdit(DATEPICK *pdp, BOOL fReset)
  4199. {
  4200. LPSUBEDITCONTROL psec = &pdp->sec;
  4201. if (psec->iseCur >= 0)
  4202. {
  4203. LPSUBEDIT psubed = &psec->pse[psec->iseCur];
  4204. if (psubed->cchEdit)
  4205. {
  4206. SECSetSubeditValue(pdp, psubed, psubed->valEdit, FALSE);
  4207. }
  4208. if (fReset)
  4209. psubed->cchEdit = 0;
  4210. }
  4211. }
  4212. // SECInvalidate invalidates the display for each subedit affected by a change
  4213. // to ID. NOTE: as a side affect, it recalculates MAX fields for all subedits
  4214. // affected by a change to ID.
  4215. //
  4216. // SE_APP invalidates everything, anything invalidates SE_APP
  4217. // SE_MARK (am/pm) invalidate SE_HOUR, SE_HOUR invalides SE_MARK
  4218. //
  4219. void SECInvalidate(LPSUBEDITCONTROL psec, int id)
  4220. {
  4221. BOOL fAdjustDayMax = (id == SE_MONTH || id == SE_MONTHALT || id == SE_YEAR || id == SE_YEARALT || id == SE_APP || id == SE_ERA);
  4222. LPSUBEDIT pse;
  4223. int i;
  4224. // If we changed any date field and we are in a era-like calendar,
  4225. // then invalidate all, since changing the month, day or year may
  4226. // change the era or vice versa.
  4227. if (ISERACALENDAR(&psec->ct) && SE_DATELIKE(id))
  4228. {
  4229. id = SE_APP;
  4230. }
  4231. for (pse=psec->pse, i=0 ; i < psec->cse ; pse++, i++)
  4232. {
  4233. // we need to invalidate all fields that changed
  4234. if (id == pse->id || pse->id == SE_APP || id == SE_APP || (id == SE_MARK && pse->id == SE_HOUR) || (id == SE_HOUR && pse->id == SE_MARK))
  4235. {
  4236. InvalidateScrollRect(psec->pci->hwnd, &pse->rc, psec->xScroll);
  4237. }
  4238. // the month or year changed, fix max field for SE_DAY
  4239. if (fAdjustDayMax && pse->id == SE_DAY)
  4240. {
  4241. pse->max = GetDaysForMonth(psec->st.wYear, psec->st.wMonth);
  4242. if (*pse->pval > pse->max)
  4243. {
  4244. *pse->pval = (WORD) pse->max;
  4245. }
  4246. SECInvalidate(psec, SE_DAY);
  4247. }
  4248. }
  4249. }
  4250. __inline
  4251. BOOL
  4252. SECGetEraName(LPSUBEDITCONTROL psec, LPSUBEDIT pse, UINT uYear, LPTSTR ptszBuf, UINT cchBuf)
  4253. {
  4254. return MCGetDateFormatWithTempYear(&psec->ct, &psec->st, pse->pv, uYear, ptszBuf, cchBuf);
  4255. }
  4256. //
  4257. // SECIncrementEra increments/decrements the era field. ERAs are strange
  4258. // since they aren't a field unto themselves but are rather an artifact
  4259. // of the other fields. Returns the new year to use.
  4260. //
  4261. UINT SECIncrementEra(LPSUBEDITCONTROL psec, LPSUBEDIT pse, int delta)
  4262. {
  4263. TCHAR rgch[64];
  4264. TCHAR rgch2[64];
  4265. int i;
  4266. int cEras = DPA_GetPtrCount(psec->ct.hdpaEras);
  4267. UINT uNewYear;
  4268. ASSERT(pse->pval == &psec->st.wYear);
  4269. uNewYear = psec->st.wYear;
  4270. //
  4271. // First find the era that encloses the current year.
  4272. // Do this by comparing the era string, because it's possible
  4273. // for the era to change twice within the same calendar year
  4274. // (if an emperor ascends to the throne and then dies the next week)
  4275. // so comparing against hdpaYear won't help.
  4276. //
  4277. SECGetEraName(psec, pse, uNewYear, rgch, ARRAYSIZE(rgch));
  4278. //
  4279. // If the era string is blank, it means we're in the "before the
  4280. // first era" scenario, so we use the "virtual" last element that
  4281. // represents "minus infinity".
  4282. //
  4283. if (rgch[0] == TEXT('\0'))
  4284. {
  4285. i = cEras;
  4286. goto FoundEra;
  4287. }
  4288. for (i = 0; i < cEras; i++)
  4289. {
  4290. if (lstrcmp(rgch, DPA_FastGetPtr(psec->ct.hdpaEras, i)) == 0)
  4291. goto FoundEra;
  4292. }
  4293. //
  4294. // Eek! Couldn't find the era! Just increment/decrement the
  4295. // year instead.
  4296. //
  4297. uNewYear += delta;
  4298. goto Finish;
  4299. FoundEra:
  4300. //
  4301. // The era list is stored backwards, so incrementing the era means
  4302. // decrementing the index (i).
  4303. //
  4304. if (delta > 0) // Incrementing
  4305. {
  4306. //
  4307. // Don't go off the end of the list. Note that if we were in
  4308. // the "virtual era" at minus infinity, this decrement will move
  4309. // us into the first "real" era.
  4310. //
  4311. if (--i < 0)
  4312. goto Finish;
  4313. // Increment to first year of the next era.
  4314. uNewYear = StrToInt(DPA_FastGetPtr(psec->ct.hdpaYears, i));
  4315. }
  4316. else
  4317. {
  4318. //
  4319. // Don't go off the end of the list. Note that this also
  4320. // catches the "virtual era" at minus infinity.
  4321. //
  4322. if (i >= cEras)
  4323. goto Finish;
  4324. //
  4325. // Move to the last year of the previous era. Do this by
  4326. // starting with the first year of the current era and
  4327. // decrementing it if necessary.
  4328. //
  4329. uNewYear = StrToInt(DPA_FastGetPtr(psec->ct.hdpaYears, i));
  4330. }
  4331. //
  4332. // We have a year that might be in the next/prev era. Try it.
  4333. // If we're still in the original era, then inc/dec one more time
  4334. // to get there for good.
  4335. //
  4336. SECGetEraName(psec, pse, uNewYear, rgch2, ARRAYSIZE(rgch2));
  4337. if (lstrcmp(rgch, rgch2) == 0)
  4338. uNewYear += delta;
  4339. Finish:
  4340. if (uNewYear < pse->min)
  4341. uNewYear = pse->min;
  4342. if (uNewYear > pse->max)
  4343. uNewYear = pse->max;
  4344. return uNewYear;
  4345. }
  4346. // SECIncrementSubedit increments currently selected subedit by delta
  4347. // Returns TRUE iff the value changed
  4348. BOOL SECIncrementSubedit(LPSUBEDITCONTROL psec, int delta)
  4349. {
  4350. LPSUBEDIT psubed;
  4351. UINT val;
  4352. if (psec->iseCur < 0)
  4353. return(FALSE);
  4354. psubed = &psec->pse[psec->iseCur];
  4355. if (psubed->id == SE_APP)
  4356. return(FALSE);
  4357. //
  4358. // Only numeric fields should accelerate. Text fields should always
  4359. // increment/decrement by exactly one position.
  4360. //
  4361. if (psubed->flDrawText & DT_CENTER) {
  4362. if (delta < 0) delta = -1;
  4363. if (delta > 0) delta = +1;
  4364. }
  4365. //
  4366. // Incrementing/decrementing ERAs is strange.
  4367. //
  4368. if (psubed->id == SE_ERA)
  4369. {
  4370. val = SECIncrementEra(psec, psubed, delta);
  4371. }
  4372. else
  4373. {
  4374. // delta isn't a REAL delta -- it's a directional thing. Here's the REAL delta:
  4375. if (psubed->cIncrement > 0)
  4376. delta = delta * psubed->cIncrement;
  4377. if(!psubed->pval)
  4378. return (FALSE);
  4379. val = *psubed->pval + delta;
  4380. while (1) {
  4381. if ((int)val < (int)psubed->min)
  4382. {
  4383. // don't wrap years
  4384. if (SE_YEARLIKE(psubed->id)) {
  4385. val = psubed->min;
  4386. break;
  4387. }
  4388. val = psubed->min - val - 1;
  4389. val = psubed->max - val;
  4390. }
  4391. else if (val > psubed->max)
  4392. {
  4393. // don't wrap years
  4394. if (SE_YEARLIKE(psubed->id)) {
  4395. val = psubed->max;
  4396. break;
  4397. }
  4398. val = val - psubed->max - 1;
  4399. val = psubed->min + val;
  4400. } else
  4401. break;
  4402. }
  4403. }
  4404. if (*psubed->pval != val)
  4405. {
  4406. *psubed->pval = (WORD) val;
  4407. SECInvalidate(psec, psubed->id);
  4408. return(TRUE);
  4409. }
  4410. return(FALSE);
  4411. }
  4412. // returns TRUE if a value has changed, FALSE otherwise
  4413. BOOL SECHandleKeydown(DATEPICK *pdp, WPARAM wParam, LPARAM lParam)
  4414. {
  4415. int delta = 1;
  4416. LPSUBEDITCONTROL psec = &pdp->sec;
  4417. switch (wParam)
  4418. {
  4419. case VK_LEFT:
  4420. delta = -1;
  4421. // fall through...
  4422. case VK_RIGHT:
  4423. SECResetSubeditEdit(pdp);
  4424. SECIncrFocus(pdp, delta);
  4425. return(FALSE);
  4426. }
  4427. if (psec->iseCur >= 0 &&
  4428. psec->pse[psec->iseCur].id == SE_APP)
  4429. {
  4430. NMDATETIMEWMKEYDOWN nmdtkd = {0};
  4431. nmdtkd.nVirtKey = (int) wParam;
  4432. nmdtkd.pszFormat = psec->pse[psec->iseCur].pv;
  4433. SECGetSystemtime(psec,&nmdtkd.st);
  4434. CCSendNotify(psec->pci, DTN_WMKEYDOWN, &nmdtkd.nmhdr);
  4435. if (psec->st.wYear != nmdtkd.st.wYear ||
  4436. psec->st.wMonth != nmdtkd.st.wMonth ||
  4437. psec->st.wDay != nmdtkd.st.wDay ||
  4438. psec->st.wHour != nmdtkd.st.wHour ||
  4439. psec->st.wMinute != nmdtkd.st.wMinute ||
  4440. psec->st.wSecond != nmdtkd.st.wSecond) // skip wDayOfWeek and wMilliseconds
  4441. {
  4442. psec->st = nmdtkd.st;
  4443. SECInvalidate(psec, SE_APP);
  4444. return(TRUE);
  4445. }
  4446. }
  4447. else
  4448. {
  4449. MSG msg;
  4450. switch (wParam)
  4451. {
  4452. case VK_DOWN:
  4453. case VK_SUBTRACT:
  4454. delta = -1;
  4455. // fall through...
  4456. case VK_UP:
  4457. case VK_ADD:
  4458. PeekMessage(&msg, NULL, WM_CHAR, WM_CHAR, PM_REMOVE); // eat this message
  4459. SECResetSubeditEdit(pdp);
  4460. return(SECIncrementSubedit(psec, delta));
  4461. break;
  4462. case VK_HOME:
  4463. case VK_END:
  4464. if (psec->iseCur >= 0)
  4465. {
  4466. LPSUBEDIT psubed;
  4467. int valT;
  4468. SECResetSubeditEdit(pdp);
  4469. psubed = &psec->pse[psec->iseCur];
  4470. valT = *psubed->pval;
  4471. *psubed->pval = (wParam == VK_HOME ? psubed->min : psubed->max);
  4472. delta = *psubed->pval - valT;
  4473. if (delta != 0)
  4474. {
  4475. SECInvalidate(psec, psubed->id);
  4476. return(TRUE);
  4477. }
  4478. }
  4479. break;
  4480. }
  4481. }
  4482. return(FALSE);
  4483. }
  4484. // returns TRUE if a value has changed, FALSE otherwise
  4485. // This function performs a DPNotifyDateChange() if applicable.
  4486. BOOL SECHandleChar(DATEPICK *pdp, TCHAR ch)
  4487. {
  4488. LPSUBEDIT psubed;
  4489. UINT uCurDigit; // current digit hit
  4490. UINT uCurSubValue; // current displayed subvalue in edit field
  4491. UINT uCurValue; // current value of the subedit
  4492. LPSUBEDITCONTROL psec = &pdp->sec;
  4493. // NOTE: In almost all cases, uCurSubValue will be the same as uCurValue
  4494. // since most fields don't have shortened displays. However, for years
  4495. // we can display two digits of a 4 digit number, which makes for
  4496. // complications.
  4497. if (psec->iseCur < 0)
  4498. return(FALSE);
  4499. psubed = &psec->pse[psec->iseCur];
  4500. if (psubed->cchMax == 0)
  4501. return(FALSE);
  4502. if (ch == psec->cDelimeter || StrChr(psec->szDelimeters, ch))
  4503. {
  4504. SECResetSubeditEdit(pdp);
  4505. SECIncrFocus(pdp, 1);
  4506. return(FALSE);
  4507. }
  4508. // allow 'a' and 'p' to set the AM/PM fields. we need to do some
  4509. // funky stuff to get this to work right, so here it is.
  4510. else if (psubed->id == SE_MARK)
  4511. {
  4512. if ((ch == TEXT('p') || ch == TEXT('P')) && (*psubed->pval < 12))
  4513. {
  4514. int valNew = *psubed->pval+12;
  4515. ch = (valNew) % 10 + TEXT('0');
  4516. psubed->valEdit = (valNew) / 10;
  4517. psubed->cchEdit = 1;
  4518. }
  4519. else if ((ch == TEXT('a') || ch == TEXT('A')) && (*psubed->pval >= 12))
  4520. {
  4521. int valNew = *psubed->pval-12;
  4522. ch = (valNew) % 10 + TEXT('0');
  4523. psubed->valEdit = (valNew) / 10;
  4524. psubed->cchEdit = 1;
  4525. }
  4526. else
  4527. {
  4528. return(FALSE);
  4529. }
  4530. }
  4531. else if (ch < TEXT('0') || ch > TEXT('9'))
  4532. {
  4533. MessageBeep(MB_ICONHAND);
  4534. return(FALSE);
  4535. }
  4536. else if (psubed->id == SE_ERA)
  4537. {
  4538. // I don't know what to do with this field, so bail out
  4539. return(FALSE);
  4540. }
  4541. uCurDigit = ch - TEXT('0');
  4542. if (psubed->cchEdit)
  4543. uCurSubValue = psubed->valEdit * 10 + uCurDigit;
  4544. else
  4545. uCurSubValue = uCurDigit;
  4546. uCurValue = SECAdjustByType(pdp, psubed, uCurSubValue);
  4547. // Allow bogus values for years since you might need to type
  4548. // in a bogus value on the way to a valid four-digit value.
  4549. if (uCurValue > psubed->max && !SE_YEARLIKE(psubed->id))
  4550. {
  4551. // the number has exceeded the max, so no point in continuing
  4552. psubed->cchEdit = 0;
  4553. // If we're going to exceed the max, then reset the edit
  4554. // and make this the first number instead of beeping
  4555. uCurValue = uCurValue - uCurSubValue + uCurDigit;
  4556. uCurSubValue = uCurDigit;
  4557. }
  4558. // Allow 0 to be valEdit for subedits, even though it may be
  4559. // illegal for that field (e.g., month).
  4560. // This lets people type "09" and get the "expected" result.
  4561. SECInvalidate(psec, psubed->id);
  4562. psubed->valEdit = uCurSubValue;
  4563. psubed->cchEdit++;
  4564. if (psubed->cchEdit == psubed->cchMax)
  4565. psubed->cchEdit = 0;
  4566. if (psubed->cchEdit == 0)
  4567. {
  4568. // SECSetSubeditValue will do the validation
  4569. SECSetSubeditValue(pdp, psubed, uCurSubValue, TRUE);
  4570. return(TRUE);
  4571. }
  4572. if(psubed->valEdit != *psubed->pval)
  4573. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  4574. return(FALSE);
  4575. }
  4576. // SECFormatSubed returns pointer to correct string
  4577. LPTSTR SECFormatSubed(LPSUBEDITCONTROL psec, LPSUBEDIT psubed, LPTSTR szTmp, UINT cch)
  4578. {
  4579. LPTSTR sz;
  4580. if (psubed->id == SE_STATIC)
  4581. {
  4582. sz = (LPTSTR)psubed->pv;
  4583. }
  4584. else
  4585. {
  4586. sz = szTmp;
  4587. SEGetTimeDateFormat(psubed, psec, szTmp, cch);
  4588. }
  4589. return sz;
  4590. }
  4591. // Returns TRUE if this subedit displays as digits (rather than text).
  4592. BOOL SECIsNumeric(LPSUBEDIT psubed)
  4593. {
  4594. switch (psubed->id)
  4595. {
  4596. case SE_ERA: return FALSE; // g never
  4597. case SE_YEAR: return TRUE; // yyyy always digits
  4598. case SE_YEARALT: return TRUE; // yy always digits
  4599. case SE_MONTH: return lstrlen(psubed->pv) <= 2; // MM yes, but not MMM
  4600. case SE_MONTHALT: return lstrlen(psubed->pv) <= 4; // ddMM yes, but not ddMMM
  4601. case SE_DAY: return TRUE; // dd always digits
  4602. case SE_MARK: return FALSE; // tt never
  4603. case SE_HOUR: return TRUE; // hh always digits
  4604. case SE_MINUTE: return TRUE; // mm always digits
  4605. case SE_SECOND: return TRUE; // ss always digits
  4606. case SE_STATIC: return FALSE; // static text
  4607. case SE_APP: return FALSE; // app's job to format this
  4608. }
  4609. return FALSE;
  4610. }
  4611. // SECDrawSubedits draws subedits and updates their bounding rectangles
  4612. void SECDrawSubedits(HDC hdc, LPSUBEDITCONTROL psec, BOOL fFocus, BOOL fEnabled)
  4613. {
  4614. HGDIOBJ hfontOrig;
  4615. int i, iseCur;
  4616. LPTSTR sz;
  4617. TCHAR szTmp[DTP_FORMATLENGTH];
  4618. LPSUBEDIT psubed;
  4619. hfontOrig = SelectObject(hdc, (HGDIOBJ)psec->hfont);
  4620. // Do this cuz the xScroll stuff can send text into visible area that it shouldn't be in
  4621. IntersectClipRect(hdc, psec->rc.left, psec->rc.top, psec->rc.right, psec->rc.bottom);
  4622. SetBkColor(hdc, g_clrHighlight);
  4623. iseCur = psec->iseCur;
  4624. if (!fFocus)
  4625. iseCur = SUBEDIT_NONE;
  4626. for (i = 0, psubed = psec->pse; i < psec->cse; i++, psubed++)
  4627. {
  4628. RECT rc = psubed->rc;
  4629. if (psec->xScroll)
  4630. OffsetRect(&rc, -psec->xScroll, 0);
  4631. if (!fEnabled)
  4632. {
  4633. SetBkMode(hdc, TRANSPARENT);
  4634. SetTextColor(hdc, g_clrGrayText);
  4635. }
  4636. else if (iseCur == i)
  4637. {
  4638. SetBkMode(hdc, OPAQUE);
  4639. SetTextColor(hdc, g_clrHighlightText);
  4640. }
  4641. else
  4642. {
  4643. SetBkMode(hdc, TRANSPARENT);
  4644. SetTextColor(hdc, g_clrWindowText);
  4645. }
  4646. //HACK
  4647. //if subedit control is being edited then we display the
  4648. //value in psubed->valEdit because it is not being updated
  4649. //until psubed->cchMax is reached or SECSave/ResetSubeditEdit is
  4650. //called
  4651. if(i == psec->iseCur && psubed->cchEdit != 0)
  4652. {
  4653. //
  4654. // If the field is numeric, then display it raw including the
  4655. // leading zero. People really want to see that leading zero,
  4656. // so give the public what it wants. (And even if they didn't,
  4657. // we need this special case anyway because the value might not
  4658. // yet be a valid value because the user is still typing it.
  4659. // This is particular true for SE_YEARLIKE fields.)
  4660. //
  4661. if (SECIsNumeric(psubed))
  4662. {
  4663. TCHAR szFormat[10];
  4664. StringCchPrintf(szFormat, ARRAYSIZE(szFormat), TEXT("%%0%dd"), psubed->cchEdit);
  4665. StringCchPrintf(szTmp, ARRAYSIZE(szTmp), szFormat, psubed->valEdit);
  4666. sz = szTmp;
  4667. }
  4668. else
  4669. {
  4670. // The day-of-month might not be valid for the temporary month
  4671. // or year in psubed->valEdit, so force the day-of-month to 1
  4672. // so the month will always come out okay.
  4673. //
  4674. // This is tricky, because if the item being edited is the
  4675. // day-of-month itself, we want to display valEdit, not 1!
  4676. // So we force it to 1, then slam in the valEdit, then do
  4677. // our SECFormatSubed, then restore the original values.
  4678. //
  4679. UINT uTmp = *psubed->pval; //save the original value
  4680. WORD wOldDay = psec->st.wDay;
  4681. psec->st.wDay = 1;
  4682. // Don't change to zero in case user is typing a leading zero
  4683. // into an alphabetic field. (Stranger things have happened.)
  4684. if (psubed->valEdit)
  4685. *psubed->pval = (WORD) psubed->valEdit;
  4686. sz = SECFormatSubed(psec, psubed, szTmp, ARRAYSIZE(szTmp));
  4687. psec->st.wDay = wOldDay;
  4688. *psubed->pval = (WORD) uTmp; //restore the original value
  4689. }
  4690. }
  4691. else
  4692. sz = SECFormatSubed(psec, psubed, szTmp, ARRAYSIZE(szTmp));
  4693. DrawText(hdc, sz, -1, &rc,
  4694. psubed->flDrawText | DT_TOP | DT_NOPREFIX | DT_SINGLELINE);
  4695. }
  4696. // we know no clip region was selected before this function
  4697. SelectClipRgn(hdc, NULL);
  4698. SelectObject(hdc, hfontOrig);
  4699. }
  4700. // DON'T need to worry about xScroll here because pt is offset
  4701. int SECSubeditFromPt(LPSUBEDITCONTROL psec, POINT pt)
  4702. {
  4703. int isubed;
  4704. for (isubed = psec->cse - 1; isubed >= 0; isubed--)
  4705. {
  4706. if (!psec->pse[isubed].fReadOnly &&
  4707. pt.x >= psec->pse[isubed].rc.left)
  4708. {
  4709. break;
  4710. }
  4711. }
  4712. return(isubed);
  4713. }
  4714. void SECGetSystemtime(LPSUBEDITCONTROL psec, LPSYSTEMTIME pst)
  4715. {
  4716. *pst = psec->st;
  4717. // we don't keep doy up to date, set it now (0==sun, 6==sat)
  4718. pst->wDayOfWeek = (DowFromDate(pst)+1) % 7; // this returns 0==sun
  4719. }
  4720. BOOL SECSetSystemtime(DATEPICK *pdp, LPSYSTEMTIME pst)
  4721. {
  4722. pdp->sec.st = *pst;
  4723. return TRUE; // assume something changed
  4724. }
  4725. // SECEdit: Start a free-format edit return result in szOutput.
  4726. BOOL SECEdit(DATEPICK *pdp, LPTSTR szOutput, int cchOutput)
  4727. {
  4728. HWND hwndEdit;
  4729. TCHAR szBuf[DTP_FORMATLENGTH];
  4730. LPTSTR pszBuf;
  4731. int cchBuf;
  4732. int i;
  4733. int isePrev;
  4734. LPSUBEDIT pse;
  4735. BOOL fRet = FALSE;
  4736. LPSUBEDITCONTROL psec = &pdp->sec;
  4737. // Build the string that we hand to the app.
  4738. // For the duration of the string build, set the current subedit
  4739. // to SUBEDIT_ALL so that
  4740. // 1. partial edits are applied before building the string, and
  4741. // 2. SE_YEARALT can format appropriately.
  4742. isePrev = psec->iseCur;
  4743. SECSetCurSubed(pdp, SUBEDIT_ALL);
  4744. pszBuf = szBuf;
  4745. cchBuf = ARRAYSIZE(szBuf);
  4746. //
  4747. // Need to mirror the format since the Edit control will take
  4748. // of the origianl format with RTL mirroring.
  4749. //
  4750. if (psec->fMirrorSEC)
  4751. pse = (psec->pse + (psec->cse - 1));
  4752. else
  4753. pse = psec->pse;
  4754. for (i = 0 ; i < psec->cse ; i++)
  4755. {
  4756. int nTmp;
  4757. if (pse->id == SE_STATIC)
  4758. {
  4759. StringCchCopy(pszBuf, cchBuf, pse->pv);
  4760. }
  4761. else
  4762. {
  4763. SEGetTimeDateFormat(pse, psec, pszBuf, cchBuf);
  4764. }
  4765. nTmp = lstrlen(pszBuf);
  4766. cchBuf -= nTmp;
  4767. pszBuf += nTmp;
  4768. //
  4769. // If this control is mirrored, then read contents backward.
  4770. //
  4771. if (psec->fMirrorSEC)
  4772. pse--;
  4773. else
  4774. pse++;
  4775. }
  4776. SECSetCurSubed(pdp, isePrev);
  4777. hwndEdit = CreateWindowEx(0, TEXT("EDIT"), szBuf, WS_CHILD | ES_AUTOHSCROLL,
  4778. psec->rc.left + 2, psec->rc.top + 2,
  4779. psec->rc.right - psec->rc.left,
  4780. psec->rc.bottom - psec->rc.top,
  4781. psec->pci->hwnd, NULL, HINST_THISDLL, NULL);
  4782. if (hwndEdit)
  4783. {
  4784. RECT rcEdit = psec->rc;
  4785. MapWindowRect(psec->pci->hwnd, NULL, &rcEdit); // ClientToScreen
  4786. pdp->fFreeEditing = TRUE;
  4787. InvalidateRect(psec->pci->hwnd, NULL, TRUE);
  4788. Edit_LimitText(hwndEdit, ARRAYSIZE(szBuf) - 1);
  4789. FORWARD_WM_SETFONT(hwndEdit, psec->hfont, FALSE, SendMessage);
  4790. SetFocus(hwndEdit);
  4791. RescrollEditWindow(hwndEdit);
  4792. ShowWindow(hwndEdit, SW_SHOWNORMAL);
  4793. //
  4794. // The basic idea:
  4795. //
  4796. // Process messages until we receive a cancel message,
  4797. // or an accept message, or some implicit accept-like
  4798. // thing happens (namely, a sent WM_KILLFOCUS).
  4799. //
  4800. // If the accept or cancel was implicit, then leave the
  4801. // cancelling message in the queue for somebody else
  4802. // to process. Otherwise, if the accept/cancel was
  4803. // explicit, eat the message so nobody else gets
  4804. // confused by it.
  4805. //
  4806. for (;;)
  4807. {
  4808. MSG msg;
  4809. BOOL fPeek;
  4810. fPeek = PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  4811. // That PeekMessage may have dispatched a sent WM_KILLFOCUS,
  4812. // in which case the change is considered to have been Accepted.
  4813. // Leave the message we peeked in the queue because the accept
  4814. // was implicit.
  4815. if (GetFocus() != hwndEdit)
  4816. {
  4817. DebugMsg(TF_MONTHCAL, TEXT("SECEdit accept (killfocus)"));
  4818. fRet = TRUE;
  4819. break;
  4820. }
  4821. if (fPeek) {
  4822. //
  4823. // Messages that cause us to cancel implicitly.
  4824. // These messages stay in the queue.
  4825. //
  4826. if (msg.message == WM_SYSCOMMAND ||
  4827. msg.message == WM_SYSCHAR ||
  4828. msg.message == WM_SYSDEADCHAR ||
  4829. msg.message == WM_DEADCHAR ||
  4830. msg.message == WM_SYSKEYDOWN ||
  4831. msg.message == WM_QUIT) {
  4832. DebugMsg(TF_MONTHCAL, TEXT("SECEdit got a message to terminate (%d)"), msg.message);
  4833. fRet = FALSE;
  4834. break;
  4835. }
  4836. //
  4837. // Messages that cause us to accept implicitly.
  4838. // These messages stay in the queue.
  4839. //
  4840. if ((msg.message == WM_LBUTTONDOWN ||
  4841. msg.message == WM_NCLBUTTONDOWN ||
  4842. msg.message == WM_RBUTTONDOWN ||
  4843. msg.message == WM_NCRBUTTONDOWN ||
  4844. msg.message == WM_LBUTTONDBLCLK) &&
  4845. !PtInRect(&rcEdit, msg.pt))
  4846. {
  4847. DebugMsg(TF_MONTHCAL, TEXT("SECEdit got a message to accept (%d)"), msg.message);
  4848. fRet = TRUE;
  4849. break;
  4850. }
  4851. // We are now committed to eating or processing the message
  4852. GetMessage(&msg, NULL, 0, 0);
  4853. //
  4854. // Messages that cause us to cancel explicitly.
  4855. //
  4856. if (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
  4857. {
  4858. DebugMsg(TF_MONTHCAL, TEXT("SECEdit explicit cancel (%d)"), msg.message);
  4859. fRet = FALSE;
  4860. break;
  4861. }
  4862. //
  4863. // Messages that cause us to accept explicitly.
  4864. //
  4865. if (msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN)
  4866. {
  4867. DebugMsg(TF_MONTHCAL, TEXT("SECEdit explicit accept (%d)"), msg.message);
  4868. fRet = TRUE;
  4869. break;
  4870. }
  4871. //
  4872. // All other messages just get dispatched.
  4873. //
  4874. TranslateMessage(&msg);
  4875. DispatchMessage(&msg);
  4876. } else {
  4877. WaitMessage();
  4878. }
  4879. } // for (;;)
  4880. if (fRet)
  4881. {
  4882. Edit_GetText(hwndEdit, szOutput, cchOutput);
  4883. }
  4884. DestroyWindow(hwndEdit);
  4885. pdp->fFreeEditing = FALSE;
  4886. InvalidateRect(psec->pci->hwnd, NULL, TRUE);
  4887. }
  4888. return(fRet);
  4889. }
  4890. //
  4891. // returns true if months were scrolled, false otherwise
  4892. //
  4893. BOOL FScrollIntoView(MONTHCAL *pmc)
  4894. {
  4895. int nDelta = 0;
  4896. SYSTEMTIME stEnd;
  4897. if (MonthCal_IsMultiSelect(pmc))
  4898. CopyDate(pmc->stEndSel, stEnd);
  4899. else
  4900. CopyDate(pmc->st, stEnd);
  4901. //
  4902. // If the month/yr for the new date is not in view, bring it
  4903. // into view
  4904. //
  4905. if ((stEnd.wYear < pmc->stMonthFirst.wYear) ||
  4906. ((stEnd.wYear == pmc->stMonthFirst.wYear) && (stEnd.wMonth < pmc->stMonthFirst.wMonth)))
  4907. {
  4908. nDelta = - (pmc->stMonthFirst.wYear - (int)stEnd.wYear) * 12 - (pmc->stMonthFirst.wMonth - (int)stEnd.wMonth);
  4909. }
  4910. else if ((pmc->st.wYear > pmc->stMonthLast.wYear) ||
  4911. ((pmc->st.wYear == pmc->stMonthLast.wYear) && (pmc->st.wMonth > pmc->stMonthLast.wMonth)))
  4912. {
  4913. nDelta = ((int)pmc->st.wYear - pmc->stMonthLast.wYear) * 12 + ((int)pmc->st.wMonth - pmc->stMonthLast.wMonth);
  4914. }
  4915. if (nDelta)
  4916. return FIncrStartMonth(pmc, nDelta, TRUE /* dont change day */);
  4917. else
  4918. return FALSE;
  4919. }
  4920. //
  4921. // Validates the isubed to make sure we aren't setting it to something
  4922. // bogus. If necessary, we pick a field at random.
  4923. //
  4924. void SECSafeSetCurSubed(DATEPICK *pdp, int ise)
  4925. {
  4926. if (ise >= pdp->sec.cse ||
  4927. (ise >= 0 && pdp->sec.pse[ise].fReadOnly))
  4928. {
  4929. SECSetCurSubed(pdp, SUBEDIT_NONE);
  4930. SECIncrFocus(pdp, 1);
  4931. }
  4932. else
  4933. SECSetCurSubed(pdp, ise);
  4934. }
  4935. LRESULT DTM_OnSetFormat(DATEPICK *pdp, LPCTSTR szFormat)
  4936. {
  4937. // remember the field that has focus so we can restore it later
  4938. //
  4939. int iseCur = pdp->sec.iseCur;
  4940. if (!szFormat || !*szFormat)
  4941. {
  4942. pdp->fLocale = TRUE;
  4943. DPHandleLocaleChange(pdp);
  4944. }
  4945. else
  4946. {
  4947. pdp->fLocale = FALSE;
  4948. SECParseFormat(pdp, &pdp->sec, szFormat);
  4949. }
  4950. // restore focus. it might be cool to do extra validation
  4951. // to see if iseCur is the same type that it used to be,
  4952. // maybe even validating that cse is constant. the case we're
  4953. // really trying to fix is changing "1st" to "2nd" to "3rd",
  4954. // so only a text portion is really changing...
  4955. //
  4956. SECSafeSetCurSubed(pdp, iseCur);
  4957. return((LRESULT)TRUE);
  4958. }
  4959. //
  4960. // DATEPICKER stuff
  4961. //
  4962. LRESULT CALLBACK DatePickWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  4963. {
  4964. DATEPICK *pdp;
  4965. NMHDR nmhdr;
  4966. LRESULT lres = 0;
  4967. if (message == WM_NCCREATE)
  4968. return(DPNcCreateHandler(hwnd));
  4969. pdp = DatePick_GetPtr(hwnd);
  4970. if (pdp == NULL)
  4971. return(DefWindowProc(hwnd, message, wParam, lParam));
  4972. // Dispatch the various messages we can receive
  4973. switch (message)
  4974. {
  4975. case WM_CREATE:
  4976. lres = DPCreateHandler(pdp, hwnd, (LPCREATESTRUCT)lParam);
  4977. break;
  4978. case WM_ERASEBKGND:
  4979. if (!pdp->fEnabled)
  4980. {
  4981. RECT rc;
  4982. HDC hdc = (HDC)wParam;
  4983. GetClipBox(hdc, &rc);
  4984. FillRectClr(hdc, &rc, g_clrBtnFace);
  4985. } else
  4986. goto DoDefault;
  4987. break;
  4988. case WM_NCPAINT:
  4989. if (pdp->hThemeCombo && GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_CLIENTEDGE)
  4990. {
  4991. HRGN hrgn = (wParam != 1) ? (HRGN)wParam : NULL;
  4992. HBRUSH hbr = (HBRUSH)GetClassLongPtr(hwnd, GCLP_HBRBACKGROUND);
  4993. if (CCDrawNonClientTheme(pdp->hThemeCombo, hwnd, hrgn, hbr, 0, CBXS_NORMAL))
  4994. {
  4995. break;
  4996. }
  4997. }
  4998. goto DoDefault;
  4999. case WM_PRINTCLIENT:
  5000. case WM_PAINT:
  5001. {
  5002. PAINTSTRUCT ps;
  5003. HDC hdc;
  5004. hdc = (HDC)wParam;
  5005. if (hdc) {
  5006. DPPaint(pdp, hdc);
  5007. } else {
  5008. hwnd = pdp->ci.hwnd;
  5009. hdc = BeginPaint(hwnd, &ps);
  5010. DPPaint(pdp, hdc);
  5011. EndPaint(hwnd, &ps);
  5012. }
  5013. break;
  5014. }
  5015. case WM_LBUTTONDOWN:
  5016. DPLButtonDown(pdp, wParam, lParam);
  5017. break;
  5018. case WM_NOTIFY:
  5019. switch (((LPNMHDR)lParam)->code)
  5020. {
  5021. case MCN_SELCHANGE:
  5022. case MCN_SELECT:
  5023. {
  5024. LPNMSELECT pnms = (LPNMSELECT)lParam;
  5025. DebugMsg(TF_MONTHCAL,TEXT("MonthCal notified DateTimePick of SELECT"));
  5026. if (!DPSetDate(pdp, &pnms->stSelStart, TRUE))
  5027. {
  5028. DebugMsg(DM_WARNING,TEXT("MonthCal cannot set selected date!"));
  5029. MessageBeep(MB_ICONHAND);
  5030. }
  5031. pdp->fShow = (((LPNMHDR)lParam)->code == MCN_SELCHANGE);
  5032. break;
  5033. }
  5034. case UDN_DELTAPOS:
  5035. if ((int)wParam == DATEPICK_UPDOWN)
  5036. {
  5037. LPNM_UPDOWN pnmdp = (LPNM_UPDOWN)lParam;
  5038. if (!pdp->fFocus)
  5039. SetFocus(pdp->ci.hwnd);
  5040. SECResetSubeditEdit(pdp);
  5041. if (SECIncrementSubedit(&pdp->sec, -pnmdp->iDelta))
  5042. DPNotifyDateChange(pdp);
  5043. }
  5044. break;
  5045. } // WM_NOTIFY switch
  5046. break;
  5047. case WM_GETFONT:
  5048. lres = (LRESULT)pdp->sec.hfont;
  5049. break;
  5050. case WM_SETFONT:
  5051. DPHandleSetFont(pdp, (HFONT)wParam, (BOOL)LOWORD(lParam));
  5052. break;
  5053. case WM_DESTROY:
  5054. DPDestroyHandler(hwnd, pdp, wParam, lParam);
  5055. break;
  5056. case WM_KILLFOCUS:
  5057. case WM_SETFOCUS:
  5058. {
  5059. BOOL fGotFocus = (message == WM_SETFOCUS);
  5060. if (BOOLIFY(fGotFocus) != BOOLIFY(pdp->fFocus))
  5061. {
  5062. pdp->fFocus = (WORD) fGotFocus;
  5063. if (pdp->sec.iseCur >= 0)
  5064. {
  5065. InvalidateScrollRect(pdp->ci.hwnd, &pdp->sec.pse[pdp->sec.iseCur].rc, pdp->sec.xScroll);
  5066. }
  5067. else if (DatePick_ShowCheck(pdp))
  5068. {
  5069. pdp->fCheckFocus = (WORD) fGotFocus;
  5070. InvalidateRect(pdp->ci.hwnd, &pdp->rcCheck, TRUE);
  5071. }
  5072. else if (fGotFocus) // nothing has focus, bring it to something
  5073. {
  5074. SECIncrFocus(pdp, 1);
  5075. }
  5076. CCSendNotify(&pdp->ci, (fGotFocus ? NM_SETFOCUS : NM_KILLFOCUS), &nmhdr);
  5077. }
  5078. if (fGotFocus)
  5079. {
  5080. // Revalidate iseLastActive because the app might've changed
  5081. // the format while we were nonfocus
  5082. SECSafeSetCurSubed(pdp, pdp->iseLastActive);
  5083. }
  5084. else
  5085. {
  5086. pdp->iseLastActive = pdp->sec.iseCur;
  5087. SECSetCurSubed(pdp, SUBEDIT_NONE);
  5088. }
  5089. break;
  5090. }
  5091. case WM_ENABLE:
  5092. {
  5093. BOOL fEnabled = wParam ? TRUE:FALSE;
  5094. if (BOOLIFY(pdp->fEnabled) != fEnabled)
  5095. {
  5096. pdp->fEnabled = (WORD) fEnabled;
  5097. if (pdp->hwndUD)
  5098. EnableWindow(pdp->hwndUD, fEnabled);
  5099. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5100. }
  5101. break;
  5102. }
  5103. case DTMP_WINDOWPOSCHANGED:
  5104. case WM_SIZE:
  5105. {
  5106. RECT rc;
  5107. if (message == DTMP_WINDOWPOSCHANGED)
  5108. {
  5109. GetClientRect(pdp->ci.hwnd, &rc);
  5110. }
  5111. else
  5112. {
  5113. rc.left = 0;
  5114. rc.top = 0;
  5115. rc.right = GET_X_LPARAM(lParam);
  5116. rc.bottom = GET_Y_LPARAM(lParam);
  5117. }
  5118. DPRecomputeSizing(pdp, &rc);
  5119. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5120. UpdateWindow(pdp->ci.hwnd);
  5121. break;
  5122. }
  5123. case WM_GETDLGCODE:
  5124. lres = DLGC_WANTARROWS | DLGC_WANTCHARS;
  5125. break;
  5126. case WM_KEYDOWN:
  5127. if (pdp->fShow)
  5128. {
  5129. SendMessage(pdp->hwndMC, WM_KEYDOWN, wParam, lParam);
  5130. return 0;
  5131. }
  5132. else
  5133. {
  5134. lres = DPHandleKeydown(pdp, wParam, lParam);
  5135. }
  5136. break;
  5137. case WM_KEYUP:
  5138. if (pdp->fShow)
  5139. SendMessage(pdp->hwndMC, WM_KEYUP, wParam, lParam);
  5140. break;
  5141. case WM_SYSKEYDOWN:
  5142. if (wParam == VK_DOWN && !pdp->fUseUpDown)
  5143. {
  5144. DPLBD_MonthCal(pdp, FALSE);
  5145. }
  5146. else
  5147. goto DoDefault;
  5148. break;
  5149. case WM_CHAR:
  5150. lres = DPHandleChar(pdp, wParam, lParam);
  5151. break;
  5152. case WM_SYSCOLORCHANGE:
  5153. InitGlobalColors();
  5154. // Don't need to propagate to pdp->hwndMC because it is its own
  5155. // top-level window.
  5156. break;
  5157. case WM_WININICHANGE:
  5158. if (lParam == 0 || !lstrcmpi((LPTSTR)lParam, TEXT("Intl")))
  5159. {
  5160. DPHandleLocaleChange(pdp);
  5161. }
  5162. // Don't need to propagate to pdp->hwndMC because it is its own
  5163. // top-level window.
  5164. break;
  5165. case WM_THEMECHANGED:
  5166. if (pdp->hThemeCombo)
  5167. {
  5168. CloseThemeData(pdp->hThemeCombo);
  5169. }
  5170. if (pdp->hThemeCheck)
  5171. {
  5172. CloseThemeData(pdp->hThemeCheck);
  5173. }
  5174. pdp->hThemeCombo = OpenThemeData(hwnd, L"Combobox"); // This one kinda looks like a combobox
  5175. pdp->hThemeCheck = OpenThemeData(hwnd, L"Button");
  5176. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5177. break;
  5178. case WM_NOTIFYFORMAT:
  5179. return CIHandleNotifyFormat(&pdp->ci, lParam);
  5180. break;
  5181. // Cannot use WM_SETTEXT to change the text of a DTP
  5182. case WM_SETTEXT:
  5183. return -1;
  5184. case WM_GETTEXT:
  5185. if (!lParam || !wParam) {
  5186. // previously this just failed and returned 0
  5187. // in bogus input. should be safe to convert to
  5188. // gettextlength
  5189. message = WM_GETTEXTLENGTH;
  5190. } else
  5191. (*(LPTSTR)lParam) = 0;
  5192. // fall through
  5193. case WM_GETTEXTLENGTH:
  5194. {
  5195. TCHAR szTmp[DTP_FORMATLENGTH];
  5196. LPSUBEDIT psubed;
  5197. int i;
  5198. TCHAR *pszText = (TCHAR *)lParam;
  5199. UINT nTextLen = 0;
  5200. for (i = 0, psubed = pdp->sec.pse; i < pdp->sec.cse; i++, psubed++)
  5201. {
  5202. LPTSTR sz;
  5203. UINT nLen;
  5204. sz = SECFormatSubed(&pdp->sec, psubed, szTmp, ARRAYSIZE(szTmp));
  5205. nLen = lstrlen(sz);
  5206. if (message == WM_GETTEXT)
  5207. {
  5208. if (nTextLen + nLen >= wParam)
  5209. break;
  5210. StringCchCopy(pszText, wParam, sz);
  5211. pszText += nLen;
  5212. }
  5213. nTextLen += nLen;
  5214. }
  5215. lres = nTextLen;
  5216. }
  5217. break;
  5218. case WM_STYLECHANGING:
  5219. lres = DPOnStyleChanging(pdp, (UINT) wParam, (LPSTYLESTRUCT)lParam);
  5220. break;
  5221. case WM_STYLECHANGED:
  5222. lres = DPOnStyleChanged(pdp, (UINT) wParam, (LPSTYLESTRUCT)lParam);
  5223. break;
  5224. case WM_CONTEXTMENU:
  5225. if (pdp->hwndMC)
  5226. lres = SendMessage(pdp->hwndMC, message, wParam, lParam);
  5227. else
  5228. goto DoDefault;
  5229. break;
  5230. //
  5231. // DATETIMEPICK specific messages
  5232. //
  5233. // DTM_GETSYSTEMTIME wParam=void lParam=LPSYSTEMTIME
  5234. // returns GDT_NONE if no date selected (DTS_SHOWNONE only)
  5235. // returns GDT_VALID and modifies *lParam to be the selected date
  5236. case DTM_GETSYSTEMTIME:
  5237. if (!pdp->fCheck)
  5238. {
  5239. lres = GDT_NONE;
  5240. }
  5241. else
  5242. {
  5243. // If there is an edit pending, save it so the app sees
  5244. // the absolute latest values. This is important for app
  5245. // compat, because IE4 wasn't Y2K compliant and people got
  5246. // away with typing just two digits of the year and hitting
  5247. // ENTER. The "Find Files" dialog would then ask us for the
  5248. // year, and in the Y2K case, we are still waiting for the
  5249. // other two digits (for a four-digit year) and return the
  5250. // wrong year.
  5251. SECSaveSubeditEdit(pdp);
  5252. SECGetSystemtime(&pdp->sec, (SYSTEMTIME *)lParam);
  5253. lres = GDT_VALID;
  5254. }
  5255. break;
  5256. // DTM_SETSYSTEMTIME wParam=GDT_flag lParam=LPSYSTEMTIME
  5257. // if wParam==GDT_NONE, sets datepick to None (DTS_SHOWNONE only)
  5258. // if wParam==GDT_VALID, sets datepick to *lParam
  5259. // returns TRUE on success, FALSE on error (such as bad params)
  5260. case DTM_SETSYSTEMTIME:
  5261. {
  5262. LPSYSTEMTIME pst = ((LPSYSTEMTIME)lParam);
  5263. if ((wParam != GDT_NONE && wParam != GDT_VALID) ||
  5264. (wParam == GDT_NONE && !DatePick_ShowCheck(pdp)) ||
  5265. (wParam == GDT_VALID && !IsValidSystemtime(pst)))
  5266. {
  5267. break;
  5268. }
  5269. // reset subed in place edit
  5270. SECResetSubeditEdit(pdp);
  5271. pdp->fNoNotify = TRUE;
  5272. if (DatePick_ShowCheck(pdp))
  5273. {
  5274. if ((wParam == GDT_NONE) || (pdp->fCheck))
  5275. {
  5276. // let checkbox have focus
  5277. SECSetCurSubed(pdp, SUBEDIT_NONE);
  5278. pdp->fCheckFocus = 1;
  5279. }
  5280. pdp->fCheck = (wParam == GDT_NONE ? 0 : 1);
  5281. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5282. }
  5283. if (wParam == GDT_VALID)
  5284. {
  5285. pdp->fNoNotify = TRUE;
  5286. DPSetDate(pdp, pst, FALSE);
  5287. pdp->fNoNotify = FALSE;
  5288. }
  5289. lres = TRUE;
  5290. pdp->fNoNotify = FALSE;
  5291. break;
  5292. }
  5293. // DTM_GETRANGE wParam=void lParam=LPSYSTEMTIME[2]
  5294. // modifies *lParam to be the minimum ALLOWABLE systemtime (or 0 if no minimum)
  5295. // modifies *(lParam+1) to be the maximum ALLOWABLE systemtime (or 0 if no maximum)
  5296. // returns GDTR_MIN|GDTR_MAX if there is a minimum|maximum limit
  5297. case DTM_GETRANGE:
  5298. {
  5299. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  5300. ZeroMemory(pst, 2*SIZEOF(SYSTEMTIME));
  5301. lres = pdp->gdtr;
  5302. if (lres & GDTR_MIN)
  5303. pst[0] = pdp->stMin;
  5304. if (lres & GDTR_MAX)
  5305. pst[1] = pdp->stMax;
  5306. break;
  5307. }
  5308. // DTM_SETRANGE wParam=GDR_flags lParam=LPSYSTEMTIME[2]
  5309. // if GDTR_MIN, sets the minimum ALLOWABLE systemtime to *lParam, otherwise removes minimum
  5310. // if GDTR_MAX, sets the maximum ALLOWABLE systemtime to *(lParam+1), otherwise removes maximum
  5311. // returns TRUE on success, FALSE on error (such as invalid parameters)
  5312. case DTM_SETRANGE:
  5313. {
  5314. LPSYSTEMTIME pst = (LPSYSTEMTIME)lParam;
  5315. const SYSTEMTIME *pstMin = (wParam & GDTR_MIN) ? pst+0 : &c_stEpoch;
  5316. const SYSTEMTIME *pstMax = (wParam & GDTR_MAX) ? pst+1 : &c_stArmageddon;
  5317. if (!IsValidDate(pstMin) || !IsValidDate(pstMax))
  5318. {
  5319. break;
  5320. }
  5321. // Save the flags so we can tell the app if it asks.
  5322. // We personally don't care.
  5323. pdp->gdtr = (UINT)wParam & (GDTR_MIN | GDTR_MAX);
  5324. if (CmpDate(&pdp->stMin, &pdp->stMax) <= 0)
  5325. {
  5326. pdp->stMin = *pstMin;
  5327. pdp->stMax = *pstMax;
  5328. }
  5329. else
  5330. {
  5331. pdp->stMin = *pstMax;
  5332. pdp->stMax = *pstMin;
  5333. }
  5334. // we might now have an invalid date, if so, try to set the current
  5335. // date and munge it to a max or min value if out of range.
  5336. pdp->fNoNotify = TRUE;
  5337. DPSetDate(pdp, &pdp->sec.st, TRUE);
  5338. pdp->fNoNotify = FALSE;
  5339. lres = TRUE;
  5340. break;
  5341. }
  5342. // DTM_SETFORMAT wParam=void lParam=LPCTSTR
  5343. // Sets the formatting string to a copy of lParam.
  5344. case DTM_SETFORMATA:
  5345. {
  5346. LPCSTR pszFormat = (LPCSTR)lParam;
  5347. LPWSTR pwszFormat = NULL;
  5348. if (pszFormat && *pszFormat)
  5349. {
  5350. pwszFormat = ProduceWFromA(pdp->ci.uiCodePage, pszFormat);
  5351. }
  5352. lres = DTM_OnSetFormat(pdp, pwszFormat);
  5353. if (pwszFormat)
  5354. {
  5355. FreeProducedString(pwszFormat);
  5356. }
  5357. break;
  5358. }
  5359. // DTM_SETFORMAT wParam=void lParam=LPCTSTR
  5360. // Sets the formatting string to a copy of lParam.
  5361. case DTM_SETFORMAT:
  5362. {
  5363. lres = DTM_OnSetFormat(pdp, (LPCTSTR)lParam);
  5364. break;
  5365. }
  5366. case DTM_SETMCCOLOR:
  5367. if (wParam < MCSC_COLORCOUNT)
  5368. {
  5369. COLORREF clr = pdp->clr[wParam];
  5370. pdp->clr[wParam] = (COLORREF)lParam;
  5371. if (pdp->hwndMC)
  5372. SendMessage(pdp->hwndMC, MCM_SETCOLOR, wParam, lParam);
  5373. return clr;
  5374. }
  5375. return -1;
  5376. case DTM_GETMCCOLOR:
  5377. if (wParam < MCSC_COLORCOUNT)
  5378. return pdp->clr[wParam];
  5379. return -1;
  5380. case DTM_GETMONTHCAL:
  5381. return (LRESULT)(UINT_PTR)pdp->hwndMC;
  5382. // wParam -- HFONT, LOWORD(lParam) -- fRedraw
  5383. case DTM_SETMCFONT:
  5384. pdp->hfontMC = (HFONT)wParam;
  5385. if (pdp->hwndMC)
  5386. SendMessage(pdp->hwndMC, WM_SETFONT, wParam, lParam);
  5387. break;
  5388. // returns the font
  5389. case DTM_GETMCFONT:
  5390. return (LRESULT)pdp->hfontMC;
  5391. break;
  5392. default:
  5393. if (CCWndProc(&pdp->ci, message, wParam, lParam, &lres))
  5394. return lres;
  5395. DoDefault:
  5396. lres = DefWindowProc(hwnd, message, wParam, lParam);
  5397. break;
  5398. } /* switch (message) */
  5399. return(lres);
  5400. }
  5401. LRESULT DPNcCreateHandler(HWND hwnd)
  5402. {
  5403. DATEPICK *pdp;
  5404. // Sink the datepick -- we may only want to do this if WS_BORDER is set
  5405. SetWindowBits(hwnd, GWL_EXSTYLE, WS_EX_CLIENTEDGE, WS_EX_CLIENTEDGE);
  5406. // Allocate storage for the dtpick structure
  5407. pdp = (DATEPICK *)LocalAlloc(LPTR, sizeof(DATEPICK));
  5408. if (pdp)
  5409. {
  5410. DatePick_SetPtr(hwnd, pdp);
  5411. pdp->hThemeCombo = OpenThemeData(hwnd, L"Combobox"); // This one kinda looks like a combobox
  5412. pdp->hThemeCheck = OpenThemeData(hwnd, L"Button");
  5413. }
  5414. return((LRESULT)pdp);
  5415. }
  5416. void DPDestroyHandler(HWND hwnd, DATEPICK *pdp, WPARAM wParam, LPARAM lParam)
  5417. {
  5418. if (pdp)
  5419. {
  5420. if (pdp->hThemeCombo)
  5421. {
  5422. CloseThemeData(pdp->hThemeCombo);
  5423. }
  5424. if (pdp->hThemeCheck)
  5425. {
  5426. CloseThemeData(pdp->hThemeCheck);
  5427. }
  5428. SECDestroy(&pdp->sec);
  5429. MCFreeCalendarInfo(&pdp->sec.ct);
  5430. LocalFree(pdp);
  5431. }
  5432. DatePick_SetPtr(hwnd, NULL);
  5433. }
  5434. // set any locale-dependent values
  5435. #define DTS_TIMEFORMATONLY (DTS_TIMEFORMAT & ~DTS_UPDOWN) // remove the UPDOWN bit for testing
  5436. LRESULT DPCreateHandler(DATEPICK *pdp, HWND hwnd, LPCREATESTRUCT lpcs)
  5437. {
  5438. HFONT hfont;
  5439. SYSTEMTIME st;
  5440. LCID lcid;
  5441. // Initialize our data.
  5442. CIInitialize(&pdp->ci, hwnd, lpcs);
  5443. if (pdp->ci.style & DTS_INVALIDBITS)
  5444. return(-1);
  5445. if (pdp->ci.style & DTS_UPDOWN)
  5446. {
  5447. pdp->fUseUpDown = TRUE;
  5448. pdp->hwndUD = CreateWindow(UPDOWN_CLASS, NULL,
  5449. WS_CHILD | WS_VISIBLE | (pdp->ci.style & WS_DISABLED),
  5450. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwnd,
  5451. (HMENU)DATEPICK_UPDOWN, HINST_THISDLL, NULL);
  5452. }
  5453. if (DatePick_ShowCheck(pdp))
  5454. {
  5455. pdp->sec.fNone = TRUE; // ugly: this SEC stuff should be merged back into DATEPICK
  5456. pdp->iseLastActive = SUBEDIT_NONE;
  5457. }
  5458. pdp->fEnabled = !(pdp->ci.style & WS_DISABLED);
  5459. pdp->fCheck = TRUE; // start checked
  5460. // Default minimum date is the epoch
  5461. pdp->stMin = c_stEpoch;
  5462. // Default maximum date is armageddon
  5463. pdp->stMax = c_stArmageddon;
  5464. pdp->gdtr = GDTR_MIN; // We marked MIN as set in IE4, go figure
  5465. //
  5466. // See if the date/time picker supports this calendar. [samera]
  5467. //
  5468. MCGetCalendarInfo(&pdp->sec.ct);
  5469. //
  5470. // If the DTP is RTL mirrored and it's a Time-Only field, then
  5471. // we need to mirror format string so that it's displayed correctly
  5472. // on a RTL mirrored window. In case of Arabic, we need to swap the
  5473. // Time-Marker to the other side (visual left) so that it looks ok.
  5474. // For the hebrew, we need to swap the field (whether it's date or time)
  5475. // bacause unlike Arabic, it doesn't have its own digit so it reads
  5476. // from LeftToRight. [samera]
  5477. //
  5478. lcid = GetUserDefaultLCID();
  5479. pdp->sec.fMirrorSEC = pdp->sec.fSwapTimeMarker = FALSE;
  5480. if (IS_WINDOW_RTL_MIRRORED(hwnd))
  5481. {
  5482. if (pdp->ci.style & DTS_TIMEFORMATONLY)
  5483. {
  5484. pdp->sec.fMirrorSEC = TRUE;
  5485. if ((PRIMARYLANGID(LANGIDFROMLCID(lcid))) == LANG_ARABIC)
  5486. pdp->sec.fSwapTimeMarker = TRUE;
  5487. }
  5488. else if((PRIMARYLANGID(LANGIDFROMLCID(lcid))) == LANG_HEBREW)
  5489. {
  5490. pdp->sec.fMirrorSEC = TRUE;
  5491. }
  5492. }
  5493. // initialize SUBEDITCONTROL
  5494. pdp->sec.pci = &pdp->ci;
  5495. GetLocalTime(&st);
  5496. SECSetSystemtime(pdp, &st);
  5497. SECSetFont(&pdp->sec, NULL);
  5498. pdp->fLocale = TRUE;
  5499. DPHandleLocaleChange(pdp);
  5500. MCLoadString(IDS_DELIMETERS, pdp->sec.szDelimeters, ARRAYSIZE(pdp->sec.szDelimeters));
  5501. hfont = NULL;
  5502. if (lpcs->hwndParent)
  5503. hfont = (HFONT)SendMessage(lpcs->hwndParent, WM_GETFONT, 0, 0);
  5504. DPHandleSetFont(pdp, hfont, FALSE);
  5505. // initialize the colors
  5506. MCInitColorArray(pdp->clr);
  5507. return(0);
  5508. }
  5509. LRESULT DPOnStyleChanging(DATEPICK *pdp, UINT gwl, LPSTYLESTRUCT pinfo)
  5510. {
  5511. if (gwl == GWL_STYLE)
  5512. {
  5513. DWORD changeFlags = pdp->ci.style ^ pinfo->styleNew;
  5514. // Don't allow these bits to change
  5515. changeFlags &= DTS_UPDOWN | DTS_SHOWNONE | DTS_INVALIDBITS;
  5516. pinfo->styleNew ^= changeFlags;
  5517. }
  5518. return(0);
  5519. }
  5520. LRESULT DPOnStyleChanged(DATEPICK *pdp, UINT gwl, LPSTYLESTRUCT pinfo)
  5521. {
  5522. if (gwl == GWL_STYLE)
  5523. {
  5524. DWORD changeFlags = pdp->ci.style ^ pinfo->styleNew;
  5525. ASSERT(!(changeFlags & (DTS_UPDOWN|DTS_SHOWNONE)));
  5526. pdp->ci.style = pinfo->styleNew;
  5527. if (changeFlags & (DTS_SHORTDATEFORMAT|DTS_LONGDATEFORMAT|DTS_TIMEFORMAT|DTS_INVALIDBITS))
  5528. {
  5529. DPHandleLocaleChange(pdp);
  5530. }
  5531. if (changeFlags & (WS_BORDER | WS_CAPTION | WS_THICKFRAME)) {
  5532. // the changing of these bits affect the size of the window
  5533. // but not until after this message is handled
  5534. // so post ourself a message.
  5535. PostMessage(pdp->ci.hwnd, DTMP_WINDOWPOSCHANGED, 0, 0);
  5536. }
  5537. }
  5538. return(0);
  5539. }
  5540. void DPHandleLocaleChange(DATEPICK *pdp)
  5541. {
  5542. //
  5543. // See if the date/time picker supports this new calendar, and refresh
  5544. // era names as appropriate.
  5545. //
  5546. MCGetCalendarInfo(&pdp->sec.ct);
  5547. if (pdp->fLocale)
  5548. {
  5549. TCHAR szFormat[DTP_FORMATLENGTH];
  5550. switch (pdp->ci.style & DTS_FORMATMASK)
  5551. {
  5552. case DTS_TIMEFORMATONLY:
  5553. GetLocaleInfo(pdp->sec.ct.lcid, LOCALE_STIMEFORMAT, szFormat, ARRAYSIZE(szFormat));
  5554. break;
  5555. case DTS_LONGDATEFORMAT:
  5556. GetLocaleInfo(pdp->sec.ct.lcid, LOCALE_SLONGDATE, szFormat, ARRAYSIZE(szFormat));
  5557. break;
  5558. case DTS_SHORTDATEFORMAT:
  5559. case DTS_SHORTDATECENTURYFORMAT:
  5560. GetLocaleInfo(pdp->sec.ct.lcid, LOCALE_SSHORTDATE, szFormat, ARRAYSIZE(szFormat));
  5561. break;
  5562. }
  5563. SECParseFormat(pdp, &pdp->sec, szFormat);
  5564. }
  5565. }
  5566. void DPHandleSetFont(DATEPICK *pdp, HFONT hfont, BOOL fRedraw)
  5567. {
  5568. SECSetFont(&pdp->sec, hfont);
  5569. SECRecomputeSizing(&pdp->sec, &pdp->rc);
  5570. pdp->ci.uiCodePage = GetCodePageForFont(hfont);
  5571. if (fRedraw)
  5572. {
  5573. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5574. UpdateWindow(pdp->ci.hwnd);
  5575. }
  5576. }
  5577. void DPPaint(DATEPICK *pdp, HDC hdc)
  5578. {
  5579. if (DatePick_ShowCheck(pdp))
  5580. {
  5581. if (RectVisible(hdc, &pdp->rcCheck))
  5582. {
  5583. RECT rc = pdp->rcCheck;
  5584. if (pdp->hThemeCheck)
  5585. {
  5586. int iStateId = CBS_UNCHECKEDNORMAL;
  5587. if (pdp->fCheck)
  5588. iStateId = CBS_CHECKEDNORMAL;
  5589. if (pdp->fCheckFocus)
  5590. iStateId += 1; // Hot == Check focus...
  5591. if (!pdp->fEnabled)
  5592. iStateId += 3; // Disabled
  5593. DrawThemeBackground(pdp->hThemeCheck, hdc, BP_CHECKBOX, iStateId, &rc, 0);
  5594. }
  5595. else
  5596. {
  5597. UINT dfcs = DFCS_BUTTONCHECK;
  5598. if (pdp->fCheck)
  5599. dfcs |= DFCS_CHECKED;
  5600. if (!pdp->fEnabled)
  5601. dfcs |= DFCS_INACTIVE;
  5602. InflateRect(&rc, -1 , -1);
  5603. if (pdp->fCheckFocus)
  5604. DrawFocusRect(hdc, &rc);
  5605. DrawFrameControl(hdc, &rc, DFC_BUTTON, dfcs);
  5606. }
  5607. }
  5608. }
  5609. if (!pdp->fFreeEditing)
  5610. SECDrawSubedits(hdc, &pdp->sec, pdp->fFocus, pdp->fCheck ? pdp->fEnabled : FALSE);
  5611. if (!pdp->fUseUpDown && RectVisible(hdc, &pdp->rcBtn))
  5612. DPDrawDropdownButton(pdp, hdc, FALSE);
  5613. }
  5614. void _RecomputeMonthCalRect(DATEPICK *pdp, LPRECT prcCal, LPRECT prcCalT )
  5615. {
  5616. RECT rcCal = *prcCal;
  5617. RECT rcCalT = *prcCalT;
  5618. RECT rcWorkArea;
  5619. MONITORINFO mi = {0};
  5620. HMONITOR hMonitor;
  5621. if (DatePick_RightAlign(pdp))
  5622. {
  5623. rcCal.left = rcCal.right - (rcCalT.right - rcCalT.left);
  5624. }
  5625. else
  5626. {
  5627. rcCal.right = rcCal.left + (rcCalT.right - rcCalT.left);
  5628. }
  5629. rcCal.bottom = rcCal.top + (rcCalT.bottom - rcCalT.top);
  5630. // Get the information about the most appropriate monitor.
  5631. // (This includes both the work area and the monitor size.
  5632. hMonitor = MonitorFromRect(&rcCal, MONITOR_DEFAULTTONEAREST);
  5633. mi.cbSize = sizeof(mi);
  5634. GetMonitorInfo(hMonitor, &mi);
  5635. // we need to know where to fit this rectangle into
  5636. if (GetWindowLong(pdp->ci.hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)
  5637. {
  5638. // if we're topmost, our limits are the screen limits (not the working area)
  5639. rcWorkArea = mi.rcMonitor;
  5640. }
  5641. else
  5642. {
  5643. // otherwise it's the limits of the workarea
  5644. rcWorkArea = mi.rcWork;
  5645. }
  5646. // slide left if off the right side of area
  5647. if (rcCal.right > rcWorkArea.right)
  5648. {
  5649. int nTmp = rcCal.right - rcWorkArea.right;
  5650. rcCal.left -= nTmp;
  5651. rcCal.right -= nTmp;
  5652. }
  5653. // slide right if off the left side of area
  5654. if (rcCal.left < rcWorkArea.left)
  5655. {
  5656. int nTmp = rcWorkArea.left - rcCal.left;
  5657. rcCal.left += nTmp;
  5658. rcCal.right += nTmp;
  5659. }
  5660. // move to top of control if off the bottom side of area
  5661. if (rcCal.bottom > rcWorkArea.bottom)
  5662. {
  5663. RECT rcT = pdp->rc;
  5664. int nTmp = rcCal.bottom - rcCal.top;
  5665. MapWindowRect(pdp->ci.hwnd, NULL, (LPPOINT)&rcT); // 2 ClientToScreen
  5666. rcCal.bottom = rcT.top;
  5667. rcCal.top = rcCal.bottom - nTmp;
  5668. }
  5669. *prcCal = rcCal;
  5670. }
  5671. void DPLBD_MonthCal(DATEPICK *pdp, BOOL fLButtonDown)
  5672. {
  5673. HDC hdc;
  5674. HWND hwndMC;
  5675. RECT rcT, rcCalT;
  5676. RECT rcBtn, rcCal;
  5677. BOOL fBtnDown; // Is the button drawn DOWN or UP
  5678. BOOL fBtnActive; // Is the button still active
  5679. SYSTEMTIME st;
  5680. SYSTEMTIME stOld;
  5681. DWORD dwWidth;
  5682. hdc = GetDC(pdp->ci.hwnd);
  5683. // turn datetimepick on but remove all focus -- the MonthCal will have focus
  5684. if (!pdp->fCheck)
  5685. {
  5686. pdp->fCheck = TRUE;
  5687. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5688. DPNotifyDateChange(pdp);
  5689. }
  5690. if (pdp->fCheckFocus)
  5691. {
  5692. pdp->fCheckFocus = FALSE;
  5693. InvalidateRect(pdp->ci.hwnd, &pdp->rcCheck, TRUE);
  5694. }
  5695. SECSetCurSubed(pdp, SUBEDIT_NONE);
  5696. if (fLButtonDown)
  5697. DPDrawDropdownButton(pdp, hdc, TRUE);
  5698. rcT = pdp->rc;
  5699. MapWindowRect(pdp->ci.hwnd, NULL, &rcT); //2 ClientToScreen
  5700. rcBtn = pdp->rcBtn;
  5701. MapWindowRect(pdp->ci.hwnd, NULL, &rcBtn); //ClientToScreen
  5702. rcCal = rcT; // this size is only temp until
  5703. rcCal.top = rcCal.bottom + 1; // we ask the monthcal how big it
  5704. rcCal.bottom = rcCal.top + 1; // wants to be
  5705. hwndMC = CreateWindow(g_rgchMCName, NULL, WS_POPUP | WS_BORDER,
  5706. rcCal.left, rcCal.top,
  5707. rcCal.right - rcCal.left, rcCal.bottom - rcCal.top,
  5708. pdp->ci.hwnd, NULL, HINST_THISDLL, NULL);
  5709. if (hwndMC == NULL)
  5710. {
  5711. DebugMsg(DM_WARNING, TEXT("DPLBD_MonthCal could not create MONTHCAL"));
  5712. return;
  5713. }
  5714. pdp->hwndMC = hwndMC;
  5715. // set all the colors:
  5716. {
  5717. int i;
  5718. for (i = 0; i < MCSC_COLORCOUNT; i++)
  5719. {
  5720. SendMessage(hwndMC, MCM_SETCOLOR, i, pdp->clr[i]);
  5721. }
  5722. }
  5723. if (pdp->hfontMC)
  5724. SendMessage(hwndMC, WM_SETFONT, (WPARAM)pdp->hfontMC, (LPARAM)FALSE);
  5725. // set min/max dates
  5726. // Relies on HACK! that stMin and stMax are adjacent
  5727. MonthCal_SetRange(hwndMC, GDTR_MIN | GDTR_MAX, &pdp->stMin);
  5728. SendMessage(hwndMC, MCM_GETMINREQRECT, 0, (LPARAM)&rcCalT);
  5729. ASSERT(rcCalT.left == 0 && rcCalT.top == 0);
  5730. dwWidth = (DWORD)SendMessage(hwndMC, MCM_GETMAXTODAYWIDTH, 0, 0);
  5731. if (dwWidth > (DWORD)rcCalT.right)
  5732. rcCalT.right = dwWidth;
  5733. SECGetSystemtime(&pdp->sec, &st);
  5734. SendMessage(hwndMC, MCM_SETCURSEL, 0, (LPARAM)&st);
  5735. _RecomputeMonthCalRect(pdp, &rcCal, &rcCalT);
  5736. MoveWindow(hwndMC, rcCal.left, rcCal.top,
  5737. rcCal.right - rcCal.left, rcCal.bottom - rcCal.top, FALSE);
  5738. CCSendNotify(&pdp->ci, DTN_DROPDOWN, NULL);
  5739. //
  5740. // HACK-- App may have resized the window during DTN_DROPDOWN,
  5741. // so we need to get the new rcCal rect
  5742. //
  5743. {
  5744. MONTHCAL *pmc = MonthCal_GetPtr(hwndMC);
  5745. _RecomputeMonthCalRect(pdp, &rcCal, &pmc->rc);
  5746. MoveWindow(hwndMC, rcCal.left, rcCal.top,
  5747. rcCal.right - rcCal.left, rcCal.bottom - rcCal.top, FALSE);
  5748. #ifdef DEBUG
  5749. if (GetAsyncKeyState(VK_CONTROL) < 0)
  5750. (pmc)->ci.style |= MCS_MULTISELECT;
  5751. #endif
  5752. }
  5753. ShowWindow(hwndMC, SW_SHOWNA);
  5754. pdp->fShow = TRUE;
  5755. fBtnDown = fLButtonDown;
  5756. fBtnActive = fLButtonDown;
  5757. stOld = pdp->sec.st;
  5758. while (pdp->fShow)
  5759. {
  5760. MSG msg;
  5761. pdp->fShow = (WORD) GetMessage(&msg, NULL, 0, 0);
  5762. // Here's how button controls work as far as I can tell:
  5763. // Until the "final button draw up", the button draws down when the
  5764. // mouse is over it and it draws up when the mouse is not over it. This
  5765. // entire time, the control is active.
  5766. //
  5767. // The "final button draw up" occurs at the first opportunity of:
  5768. // the user releases the mouse button OR the user moves into the rect
  5769. // of the control. The control does it's action on a "mouse up".
  5770. if (fBtnActive)
  5771. {
  5772. switch (msg.message) {
  5773. case WM_MOUSEMOVE:
  5774. if (PtInRect(&rcBtn, msg.pt))
  5775. {
  5776. if (!fBtnDown)
  5777. {
  5778. DPDrawDropdownButton(pdp, hdc, TRUE);
  5779. fBtnDown = TRUE;
  5780. }
  5781. }
  5782. else
  5783. {
  5784. if (fBtnDown)
  5785. {
  5786. DPDrawDropdownButton(pdp, hdc, FALSE);
  5787. fBtnDown = FALSE;
  5788. }
  5789. if (PtInRect(&rcCal, msg.pt))
  5790. {
  5791. fBtnActive = FALSE;
  5792. // let MonthCal think it got a button down
  5793. FORWARD_WM_LBUTTONDOWN(hwndMC, FALSE,
  5794. rcCal.left/2 + rcCal.right/2,
  5795. rcCal.top/2 + rcCal.bottom/2,
  5796. 0, SendMessage);
  5797. }
  5798. }
  5799. continue; // the MonthCal doesn't need this message
  5800. case WM_LBUTTONUP:
  5801. if (fBtnDown)
  5802. {
  5803. DPDrawDropdownButton(pdp, hdc, FALSE);
  5804. fBtnDown = FALSE;
  5805. }
  5806. fBtnActive = FALSE;
  5807. continue; // the MonthCal doesn't need this message
  5808. }
  5809. } // if (fBtnActive)
  5810. // Check for events that cause the calendar to go away
  5811. //
  5812. // These events mean "I like it". We allow Alt+Up or Enter
  5813. // to accept the changes. (Alt+Up for compat with combo boxes.)
  5814. //
  5815. if (((msg.message == WM_LBUTTONDOWN ||
  5816. msg.message == WM_NCLBUTTONDOWN ||
  5817. msg.message == WM_LBUTTONDBLCLK) && !PtInRect(&rcCal, msg.pt)) ||
  5818. msg.message == WM_SYSCOMMAND ||
  5819. msg.message == WM_COMMAND ||
  5820. (msg.message == WM_SYSKEYDOWN && msg.wParam == VK_UP) ||
  5821. (msg.message == WM_KEYDOWN && msg.wParam == VK_RETURN) ||
  5822. msg.message == WM_KILLFOCUS)
  5823. {
  5824. DebugMsg(TF_MONTHCAL,TEXT("DPLBD_MonthCal got a message to accept (%d)"), msg.message);
  5825. pdp->fShow = FALSE;
  5826. continue;
  5827. }
  5828. //
  5829. // These events mean "I don't like it".
  5830. //
  5831. else if (((msg.message == WM_RBUTTONDOWN ||
  5832. msg.message == WM_NCRBUTTONDOWN ||
  5833. msg.message == WM_RBUTTONDBLCLK) && !PtInRect(&rcCal, msg.pt)) ||
  5834. (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE))
  5835. {
  5836. DebugMsg(TF_MONTHCAL,TEXT("DPLBD_MonthCal got a message to cancel (%d)"), msg.message);
  5837. pdp->fShow = FALSE;
  5838. pdp->sec.st = stOld;
  5839. DPNotifyDateChange(pdp);
  5840. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5841. continue;
  5842. }
  5843. TranslateMessage(&msg);
  5844. DispatchMessage(&msg);
  5845. } // while(fShow)
  5846. CCSendNotify(&pdp->ci, DTN_CLOSEUP, NULL);
  5847. pdp->hwndMC = NULL;
  5848. DestroyWindow(hwndMC);
  5849. ReleaseDC(pdp->ci.hwnd, hdc);
  5850. }
  5851. void DPHandleSECEdit(DATEPICK *pdp)
  5852. {
  5853. TCHAR szBuf[DTP_FORMATLENGTH];
  5854. if (SECEdit(pdp, szBuf, ARRAYSIZE(szBuf)))
  5855. {
  5856. NMDATETIMESTRING nmdts = {0};
  5857. nmdts.pszUserString = szBuf;
  5858. // just in case the app doesn't parse the string
  5859. nmdts.st = pdp->sec.st;
  5860. nmdts.dwFlags = (pdp->fCheck==1) ? GDT_VALID : GDT_NONE;
  5861. CCSendNotify(&pdp->ci, DTN_USERSTRING, &nmdts.nmhdr);
  5862. // If the app gives us an invalid date, go back to the old date
  5863. if (nmdts.dwFlags == GDT_VALID &&
  5864. !IsValidSystemtime(&nmdts.st))
  5865. {
  5866. nmdts.st = pdp->sec.st;
  5867. }
  5868. if (nmdts.dwFlags == GDT_NONE)
  5869. {
  5870. if (DatePick_ShowCheck(pdp))
  5871. {
  5872. pdp->fCheck = FALSE;
  5873. pdp->fCheckFocus = TRUE;
  5874. SECSetCurSubed(pdp, SUBEDIT_NONE);
  5875. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5876. DPNotifyDateChange(pdp);
  5877. }
  5878. }
  5879. else if (nmdts.dwFlags == GDT_VALID)
  5880. {
  5881. DPSetDate(pdp, &nmdts.st, FALSE);
  5882. }
  5883. }
  5884. }
  5885. LRESULT DPLButtonDown(DATEPICK *pdp, WPARAM wParam, LPARAM lParam)
  5886. {
  5887. POINT pt;
  5888. BOOL fFocus;
  5889. if (!pdp->fEnabled)
  5890. return(0);
  5891. pt.x = GET_X_LPARAM(lParam);
  5892. pt.y = GET_Y_LPARAM(lParam);
  5893. // reset subed char count
  5894. SECResetSubeditEdit(pdp);
  5895. fFocus = pdp->fFocus;
  5896. if (!fFocus)
  5897. SetFocus(pdp->ci.hwnd);
  5898. // display MONTHCAL iif we're not DTS_UPDOWN
  5899. if (!pdp->fUseUpDown && PtInRect(&pdp->rcBtn, pt) && IsWindowVisible(pdp->ci.hwnd))
  5900. {
  5901. DPLBD_MonthCal(pdp, TRUE);
  5902. }
  5903. else if (!pdp->fCapture)
  5904. {
  5905. // Un/check checkbox
  5906. if (DatePick_ShowCheck(pdp) && PtInRect(&pdp->rcCheck, pt))
  5907. {
  5908. pdp->fCheck = !pdp->fCheck;
  5909. pdp->fCheckFocus = 1;
  5910. SECSetCurSubed(pdp, SUBEDIT_NONE);
  5911. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  5912. DPNotifyDateChange(pdp);
  5913. }
  5914. // Select a subedit
  5915. else if (pdp->fCheck)
  5916. {
  5917. if (DatePick_AppCanParse(pdp) && fFocus)
  5918. {
  5919. // First click brings focus to a subedit, second click starts editing
  5920. DPHandleSECEdit(pdp);
  5921. }
  5922. else
  5923. {
  5924. int isubed;
  5925. pt.x += pdp->sec.xScroll;
  5926. isubed = SECSubeditFromPt(&pdp->sec, pt);
  5927. if (isubed >= 0)
  5928. {
  5929. SECSetCurSubed(pdp, isubed);
  5930. if (DatePick_ShowCheck(pdp))
  5931. {
  5932. pdp->fCheckFocus = 0;
  5933. InvalidateRect(pdp->ci.hwnd, &pdp->rcCheck, TRUE);
  5934. }
  5935. }
  5936. }
  5937. }
  5938. }
  5939. return(0);
  5940. }
  5941. void DPRecomputeSizing(DATEPICK *pdp, RECT *prect)
  5942. {
  5943. RECT rcTmp;
  5944. if (DatePick_ShowCheck(pdp))
  5945. {
  5946. pdp->rcCheck.top = prect->top + 1;
  5947. pdp->rcCheck.bottom = prect->bottom - 1;
  5948. pdp->rcCheck.left = prect->left + 1;
  5949. pdp->rcCheck.right = prect->left + (pdp->rcCheck.bottom - pdp->rcCheck.top);
  5950. // occupy at most half the width of the window
  5951. if (pdp->rcCheck.right > prect->left + (prect->right - prect->left)/2)
  5952. {
  5953. pdp->rcCheck.right = prect->left + (prect->right - prect->left)/2;
  5954. }
  5955. }
  5956. else
  5957. {
  5958. pdp->rcCheck.top = prect->top;
  5959. pdp->rcCheck.bottom = prect->top;
  5960. pdp->rcCheck.left = prect->left;
  5961. pdp->rcCheck.right = prect->left + DPXBUFFER - 1;
  5962. }
  5963. pdp->rcBtn = *prect;
  5964. pdp->rcBtn.left = pdp->rcBtn.right - GetSystemMetrics(SM_CXVSCROLL);
  5965. if (pdp->rcBtn.left < pdp->rcCheck.right)
  5966. pdp->rcBtn.left = pdp->rcCheck.right;
  5967. if (pdp->hwndUD)
  5968. MoveWindow(pdp->hwndUD, pdp->rcBtn.left, pdp->rcBtn.top, pdp->rcBtn.right - pdp->rcBtn.left + 1, pdp->rcBtn.bottom - pdp->rcBtn.top + 1, FALSE);
  5969. rcTmp = pdp->rc;
  5970. pdp->rc.top = prect->top;
  5971. pdp->rc.bottom = prect->bottom;
  5972. pdp->rc.left = pdp->rcCheck.right + 1;
  5973. pdp->rc.right = pdp->rcBtn.left - 1;
  5974. SECRecomputeSizing(&pdp->sec, &pdp->rc);
  5975. }
  5976. // deal with control codes
  5977. LRESULT DPHandleKeydown(DATEPICK *pdp, WPARAM wParam, LPARAM lParam)
  5978. {
  5979. int delta = 1;
  5980. if (wParam == VK_F4 && !pdp->fUseUpDown)
  5981. {
  5982. DPLBD_MonthCal(pdp, FALSE);
  5983. }
  5984. else if (DatePick_AppCanParse(pdp) && wParam == VK_F2)
  5985. {
  5986. DPHandleSECEdit(pdp);
  5987. }
  5988. else if (pdp->fCheckFocus)
  5989. {
  5990. switch (wParam)
  5991. {
  5992. case VK_LEFT:
  5993. delta = -1;
  5994. // fall through...
  5995. case VK_RIGHT:
  5996. if (pdp->fCheck)
  5997. {
  5998. if (SUBEDIT_NONE != SECIncrFocus(pdp, delta))
  5999. {
  6000. pdp->fCheckFocus = FALSE;
  6001. InvalidateRect(pdp->ci.hwnd, &pdp->rcCheck, TRUE);
  6002. }
  6003. }
  6004. break;
  6005. }
  6006. }
  6007. else
  6008. {
  6009. switch (wParam)
  6010. {
  6011. case VK_HOME:
  6012. if (GetKeyState(VK_CONTROL) < 0)
  6013. {
  6014. SYSTEMTIME st;
  6015. GetLocalTime(&st);
  6016. DPSetDate(pdp, &st, TRUE);
  6017. break;
  6018. }
  6019. // fall through...
  6020. default:
  6021. if (SECHandleKeydown(pdp, wParam, lParam))
  6022. {
  6023. DPNotifyDateChange(pdp);
  6024. }
  6025. else if (DatePick_ShowCheck(pdp))
  6026. {
  6027. if (pdp->sec.iseCur < 0)
  6028. {
  6029. pdp->fCheckFocus = TRUE;
  6030. InvalidateRect(pdp->ci.hwnd, &pdp->rcCheck, TRUE);
  6031. }
  6032. }
  6033. break;
  6034. }
  6035. }
  6036. return(0);
  6037. }
  6038. // deal with characters
  6039. LRESULT DPHandleChar(DATEPICK *pdp, WPARAM wParam, LPARAM lParam)
  6040. {
  6041. TCHAR ch = (TCHAR)wParam;
  6042. if (pdp->fCheckFocus)
  6043. {
  6044. // this is the only character we care about in this case
  6045. if (ch == TEXT(' '))
  6046. {
  6047. pdp->fCheck = 1-pdp->fCheck;
  6048. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  6049. DPNotifyDateChange(pdp);
  6050. }
  6051. else
  6052. {
  6053. MessageBeep(MB_ICONHAND);
  6054. }
  6055. }
  6056. else
  6057. {
  6058. // let the subedit handle this -- a value can change
  6059. SECHandleChar(pdp, ch);
  6060. }
  6061. return(0);
  6062. }
  6063. void DPNotifyDateChange(DATEPICK *pdp)
  6064. {
  6065. NMDATETIMECHANGE nmdc = {0};
  6066. BOOL fChanged;
  6067. if (pdp->fNoNotify)
  6068. return;
  6069. if (pdp->fCheck == 0)
  6070. {
  6071. nmdc.dwFlags = GDT_NONE;
  6072. }
  6073. else
  6074. {
  6075. // validate date - do it here in only one place
  6076. if (CmpSystemtime(&pdp->sec.st, &pdp->stMin) < 0)
  6077. {
  6078. pdp->sec.st = pdp->stMin;
  6079. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  6080. SECInvalidate(&pdp->sec, SE_APP);
  6081. }
  6082. else if (CmpSystemtime(&pdp->sec.st, &pdp->stMax) > 0)
  6083. {
  6084. pdp->sec.st = pdp->stMax;
  6085. InvalidateRect(pdp->ci.hwnd, NULL, TRUE);
  6086. SECInvalidate(&pdp->sec, SE_APP);
  6087. }
  6088. nmdc.dwFlags = GDT_VALID;
  6089. SECGetSystemtime(&pdp->sec, &nmdc.st);
  6090. }
  6091. fChanged = CmpSystemtime(&pdp->stPrev, &nmdc.st);
  6092. if (fChanged)
  6093. {
  6094. NotifyWinEvent(EVENT_OBJECT_VALUECHANGE, pdp->ci.hwnd, OBJID_CLIENT, INDEXID_CONTAINER);
  6095. }
  6096. //
  6097. // APP COMPAT: IE4 always notified even if the date didn't change.
  6098. // I don't know of any apps that rely on this
  6099. // but I'm not gonna risk it.
  6100. //
  6101. if (fChanged || pdp->ci.iVersion < 5)
  6102. {
  6103. pdp->stPrev = nmdc.st;
  6104. CCSendNotify(&pdp->ci, DTN_DATETIMECHANGE, &nmdc.nmhdr);
  6105. }
  6106. }
  6107. BOOL DPSetDate(DATEPICK *pdp, SYSTEMTIME *pst, BOOL fMungeDate)
  6108. {
  6109. BOOL fChanged = FALSE;
  6110. // make sure that the new date is within the valid range
  6111. if (CmpSystemtime(pst, &pdp->stMin) < 0)
  6112. {
  6113. if (!fMungeDate)
  6114. return(FALSE);
  6115. pst = &pdp->stMin;
  6116. }
  6117. if (CmpSystemtime(&pdp->stMax, pst) < 0)
  6118. {
  6119. if (!fMungeDate)
  6120. return(FALSE);
  6121. pst = &pdp->stMax;
  6122. }
  6123. if (fMungeDate)
  6124. {
  6125. // only copy the date portion
  6126. CopyDate(*pst, pdp->sec.st);
  6127. fChanged = TRUE;
  6128. }
  6129. else
  6130. {
  6131. fChanged = SECSetSystemtime(pdp, pst);
  6132. }
  6133. if (fChanged)
  6134. {
  6135. SECInvalidate(&pdp->sec, SE_APP); // SE_APP invalidates everything
  6136. DPNotifyDateChange(pdp);
  6137. }
  6138. return(TRUE);
  6139. }
  6140. void DPDrawDropdownButton(DATEPICK *pdp, HDC hdc, BOOL fPressed)
  6141. {
  6142. if (pdp->hThemeCombo)
  6143. {
  6144. int iStateId = CBXS_NORMAL;
  6145. if (fPressed)
  6146. iStateId = CBXS_PRESSED;
  6147. if (!pdp->fEnabled)
  6148. iStateId = CBXS_DISABLED;
  6149. DrawThemeBackground(pdp->hThemeCombo, hdc, CP_DROPDOWNBUTTON, iStateId, &pdp->rcBtn, 0);
  6150. }
  6151. else
  6152. {
  6153. UINT dfcs;
  6154. dfcs = DFCS_SCROLLDOWN;
  6155. if (fPressed)
  6156. dfcs |= DFCS_PUSHED | DFCS_FLAT;
  6157. if (!pdp->fEnabled)
  6158. dfcs |= DFCS_INACTIVE;
  6159. DrawFrameControl(hdc, &pdp->rcBtn, DFC_SCROLL, dfcs);
  6160. }
  6161. }