1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "xfa/fwl/cfwl_monthcalendar.h"
8
9 #include <algorithm>
10 #include <memory>
11 #include <utility>
12
13 #include "third_party/base/ptr_util.h"
14 #include "third_party/base/stl_util.h"
15 #include "xfa/fde/cfde_textout.h"
16 #include "xfa/fwl/cfwl_datetimepicker.h"
17 #include "xfa/fwl/cfwl_messagemouse.h"
18 #include "xfa/fwl/cfwl_notedriver.h"
19 #include "xfa/fwl/cfwl_themebackground.h"
20 #include "xfa/fwl/cfwl_themetext.h"
21 #include "xfa/fwl/ifwl_themeprovider.h"
22
23 #define MONTHCAL_HSEP_HEIGHT 1
24 #define MONTHCAL_HMARGIN 3
25 #define MONTHCAL_VMARGIN 2
26 #define MONTHCAL_ROWS 9
27 #define MONTHCAL_COLUMNS 7
28 #define MONTHCAL_HEADER_BTN_VMARGIN 7
29 #define MONTHCAL_HEADER_BTN_HMARGIN 5
30
31 namespace {
32
GetAbbreviatedDayOfWeek(int day)33 WideString GetAbbreviatedDayOfWeek(int day) {
34 switch (day) {
35 case 0:
36 return L"Sun";
37 case 1:
38 return L"Mon";
39 case 2:
40 return L"Tue";
41 case 3:
42 return L"Wed";
43 case 4:
44 return L"Thu";
45 case 5:
46 return L"Fri";
47 case 6:
48 return L"Sat";
49 default:
50 NOTREACHED();
51 return L"";
52 }
53 }
54
GetMonth(int month)55 WideString GetMonth(int month) {
56 switch (month) {
57 case 0:
58 return L"January";
59 case 1:
60 return L"February";
61 case 2:
62 return L"March";
63 case 3:
64 return L"April";
65 case 4:
66 return L"May";
67 case 5:
68 return L"June";
69 case 6:
70 return L"July";
71 case 7:
72 return L"August";
73 case 8:
74 return L"September";
75 case 9:
76 return L"October";
77 case 10:
78 return L"November";
79 case 11:
80 return L"December";
81 default:
82 NOTREACHED();
83 return L"";
84 }
85 }
86
87 } // namespace
88
CFWL_MonthCalendar(const CFWL_App * app,std::unique_ptr<CFWL_WidgetProperties> properties,CFWL_Widget * pOuter)89 CFWL_MonthCalendar::CFWL_MonthCalendar(
90 const CFWL_App* app,
91 std::unique_ptr<CFWL_WidgetProperties> properties,
92 CFWL_Widget* pOuter)
93 : CFWL_Widget(app, std::move(properties), pOuter) {}
94
95 CFWL_MonthCalendar::~CFWL_MonthCalendar() = default;
96
GetClassID() const97 FWL_Type CFWL_MonthCalendar::GetClassID() const {
98 return FWL_Type::MonthCalendar;
99 }
100
GetAutosizedWidgetRect()101 CFX_RectF CFWL_MonthCalendar::GetAutosizedWidgetRect() {
102 CFX_SizeF fs = CalcSize();
103 CFX_RectF rect(0, 0, fs.width, fs.height);
104 InflateWidgetRect(rect);
105 return rect;
106 }
107
Update()108 void CFWL_MonthCalendar::Update() {
109 if (IsLocked())
110 return;
111 if (!m_pProperties->m_pThemeProvider)
112 m_pProperties->m_pThemeProvider = GetAvailableTheme();
113
114 GetCapValue();
115 if (!m_bInitialized) {
116 InitDate();
117 m_bInitialized = true;
118 }
119
120 ClearDateItem();
121 ResetDateItem();
122 Layout();
123 }
124
DrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)125 void CFWL_MonthCalendar::DrawWidget(CXFA_Graphics* pGraphics,
126 const CFX_Matrix& matrix) {
127 if (!pGraphics)
128 return;
129
130 if (!m_pProperties->m_pThemeProvider)
131 m_pProperties->m_pThemeProvider = GetAvailableTheme();
132
133 IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider.Get();
134 if (HasBorder())
135 DrawBorder(pGraphics, CFWL_Part::Border, pTheme, matrix);
136
137 DrawBackground(pGraphics, pTheme, &matrix);
138 DrawHeadBK(pGraphics, pTheme, &matrix);
139 DrawLButton(pGraphics, pTheme, &matrix);
140 DrawRButton(pGraphics, pTheme, &matrix);
141 DrawSeparator(pGraphics, pTheme, &matrix);
142 DrawDatesInBK(pGraphics, pTheme, &matrix);
143 DrawDatesInCircle(pGraphics, pTheme, &matrix);
144 DrawCaption(pGraphics, pTheme, &matrix);
145 DrawWeek(pGraphics, pTheme, &matrix);
146 DrawDatesIn(pGraphics, pTheme, &matrix);
147 DrawDatesOut(pGraphics, pTheme, &matrix);
148 DrawToday(pGraphics, pTheme, &matrix);
149 }
150
SetSelect(int32_t iYear,int32_t iMonth,int32_t iDay)151 void CFWL_MonthCalendar::SetSelect(int32_t iYear,
152 int32_t iMonth,
153 int32_t iDay) {
154 ChangeToMonth(iYear, iMonth);
155 AddSelDay(iDay);
156 }
157
DrawBackground(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)158 void CFWL_MonthCalendar::DrawBackground(CXFA_Graphics* pGraphics,
159 IFWL_ThemeProvider* pTheme,
160 const CFX_Matrix* pMatrix) {
161 CFWL_ThemeBackground params;
162 params.m_pWidget = this;
163 params.m_iPart = CFWL_Part::Background;
164 params.m_pGraphics = pGraphics;
165 params.m_dwStates = CFWL_PartState_Normal;
166 params.m_rtPart = m_rtClient;
167 if (pMatrix)
168 params.m_matrix.Concat(*pMatrix);
169 pTheme->DrawBackground(params);
170 }
171
DrawHeadBK(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)172 void CFWL_MonthCalendar::DrawHeadBK(CXFA_Graphics* pGraphics,
173 IFWL_ThemeProvider* pTheme,
174 const CFX_Matrix* pMatrix) {
175 CFWL_ThemeBackground params;
176 params.m_pWidget = this;
177 params.m_iPart = CFWL_Part::Header;
178 params.m_pGraphics = pGraphics;
179 params.m_dwStates = CFWL_PartState_Normal;
180 params.m_rtPart = m_rtHead;
181 if (pMatrix)
182 params.m_matrix.Concat(*pMatrix);
183 pTheme->DrawBackground(params);
184 }
185
DrawLButton(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)186 void CFWL_MonthCalendar::DrawLButton(CXFA_Graphics* pGraphics,
187 IFWL_ThemeProvider* pTheme,
188 const CFX_Matrix* pMatrix) {
189 CFWL_ThemeBackground params;
190 params.m_pWidget = this;
191 params.m_iPart = CFWL_Part::LBtn;
192 params.m_pGraphics = pGraphics;
193 params.m_dwStates = m_iLBtnPartStates;
194 params.m_rtPart = m_rtLBtn;
195 if (pMatrix)
196 params.m_matrix.Concat(*pMatrix);
197 pTheme->DrawBackground(params);
198 }
199
DrawRButton(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)200 void CFWL_MonthCalendar::DrawRButton(CXFA_Graphics* pGraphics,
201 IFWL_ThemeProvider* pTheme,
202 const CFX_Matrix* pMatrix) {
203 CFWL_ThemeBackground params;
204 params.m_pWidget = this;
205 params.m_iPart = CFWL_Part::RBtn;
206 params.m_pGraphics = pGraphics;
207 params.m_dwStates = m_iRBtnPartStates;
208 params.m_rtPart = m_rtRBtn;
209 if (pMatrix)
210 params.m_matrix.Concat(*pMatrix);
211 pTheme->DrawBackground(params);
212 }
213
DrawCaption(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)214 void CFWL_MonthCalendar::DrawCaption(CXFA_Graphics* pGraphics,
215 IFWL_ThemeProvider* pTheme,
216 const CFX_Matrix* pMatrix) {
217 CFWL_ThemeText textParam;
218 textParam.m_pWidget = this;
219 textParam.m_iPart = CFWL_Part::Caption;
220 textParam.m_dwStates = CFWL_PartState_Normal;
221 textParam.m_pGraphics = pGraphics;
222 textParam.m_wsText = GetHeadText(m_iCurYear, m_iCurMonth);
223 m_szHead = CalcTextSize(textParam.m_wsText,
224 m_pProperties->m_pThemeProvider.Get(), false);
225 CalcHeadSize();
226 textParam.m_rtPart = m_rtHeadText;
227 textParam.m_dwTTOStyles.single_line_ = true;
228 textParam.m_iTTOAlign = FDE_TextAlignment::kCenter;
229 if (pMatrix)
230 textParam.m_matrix.Concat(*pMatrix);
231 pTheme->DrawText(textParam);
232 }
233
DrawSeparator(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)234 void CFWL_MonthCalendar::DrawSeparator(CXFA_Graphics* pGraphics,
235 IFWL_ThemeProvider* pTheme,
236 const CFX_Matrix* pMatrix) {
237 CFWL_ThemeBackground params;
238 params.m_pWidget = this;
239 params.m_iPart = CFWL_Part::HSeparator;
240 params.m_pGraphics = pGraphics;
241 params.m_dwStates = CFWL_PartState_Normal;
242 params.m_rtPart = m_rtHSep;
243 if (pMatrix)
244 params.m_matrix.Concat(*pMatrix);
245 pTheme->DrawBackground(params);
246 }
247
DrawDatesInBK(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)248 void CFWL_MonthCalendar::DrawDatesInBK(CXFA_Graphics* pGraphics,
249 IFWL_ThemeProvider* pTheme,
250 const CFX_Matrix* pMatrix) {
251 CFWL_ThemeBackground params;
252 params.m_pWidget = this;
253 params.m_iPart = CFWL_Part::DateInBK;
254 params.m_pGraphics = pGraphics;
255 if (pMatrix)
256 params.m_matrix.Concat(*pMatrix);
257
258 int32_t iCount = pdfium::CollectionSize<int32_t>(m_arrDates);
259 for (int32_t j = 0; j < iCount; j++) {
260 DATEINFO* pDataInfo = m_arrDates[j].get();
261 if (pDataInfo->dwStates & FWL_ITEMSTATE_MCD_Selected) {
262 params.m_dwStates |= CFWL_PartState_Selected;
263 if (pDataInfo->dwStates & FWL_ITEMSTATE_MCD_Flag) {
264 params.m_dwStates |= CFWL_PartState_Flagged;
265 }
266 } else if (j == m_iHovered - 1) {
267 params.m_dwStates |= CFWL_PartState_Hovered;
268 } else if (pDataInfo->dwStates & FWL_ITEMSTATE_MCD_Flag) {
269 params.m_dwStates = CFWL_PartState_Flagged;
270 pTheme->DrawBackground(params);
271 }
272 params.m_rtPart = pDataInfo->rect;
273 pTheme->DrawBackground(params);
274 params.m_dwStates = 0;
275 }
276 }
277
DrawWeek(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)278 void CFWL_MonthCalendar::DrawWeek(CXFA_Graphics* pGraphics,
279 IFWL_ThemeProvider* pTheme,
280 const CFX_Matrix* pMatrix) {
281 CFWL_ThemeText params;
282 params.m_pWidget = this;
283 params.m_iPart = CFWL_Part::Week;
284 params.m_pGraphics = pGraphics;
285 params.m_dwStates = CFWL_PartState_Normal;
286 params.m_iTTOAlign = FDE_TextAlignment::kCenter;
287 params.m_dwTTOStyles.single_line_ = true;
288
289 CFX_RectF rtDayOfWeek;
290 if (pMatrix)
291 params.m_matrix.Concat(*pMatrix);
292
293 for (int32_t i = 0; i < 7; ++i) {
294 rtDayOfWeek =
295 CFX_RectF(m_rtWeek.left + i * (m_szCell.width + MONTHCAL_HMARGIN * 2),
296 m_rtWeek.top, m_szCell);
297
298 params.m_rtPart = rtDayOfWeek;
299 params.m_wsText = GetAbbreviatedDayOfWeek(i);
300 pTheme->DrawText(params);
301 }
302 }
303
DrawToday(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)304 void CFWL_MonthCalendar::DrawToday(CXFA_Graphics* pGraphics,
305 IFWL_ThemeProvider* pTheme,
306 const CFX_Matrix* pMatrix) {
307 CFWL_ThemeText params;
308 params.m_pWidget = this;
309 params.m_iPart = CFWL_Part::Today;
310 params.m_pGraphics = pGraphics;
311 params.m_dwStates = CFWL_PartState_Normal;
312 params.m_iTTOAlign = FDE_TextAlignment::kCenterLeft;
313 params.m_wsText = GetTodayText(m_iYear, m_iMonth, m_iDay);
314
315 m_szToday = CalcTextSize(params.m_wsText,
316 m_pProperties->m_pThemeProvider.Get(), false);
317 CalcTodaySize();
318 params.m_rtPart = m_rtToday;
319 params.m_dwTTOStyles.single_line_ = true;
320
321 if (pMatrix)
322 params.m_matrix.Concat(*pMatrix);
323 pTheme->DrawText(params);
324 }
325
DrawDatesIn(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)326 void CFWL_MonthCalendar::DrawDatesIn(CXFA_Graphics* pGraphics,
327 IFWL_ThemeProvider* pTheme,
328 const CFX_Matrix* pMatrix) {
329 CFWL_ThemeText params;
330 params.m_pWidget = this;
331 params.m_iPart = CFWL_Part::DatesIn;
332 params.m_pGraphics = pGraphics;
333 params.m_dwStates = CFWL_PartState_Normal;
334 params.m_iTTOAlign = FDE_TextAlignment::kCenter;
335 if (pMatrix)
336 params.m_matrix.Concat(*pMatrix);
337
338 int32_t iCount = pdfium::CollectionSize<int32_t>(m_arrDates);
339 for (int32_t j = 0; j < iCount; j++) {
340 DATEINFO* pDataInfo = m_arrDates[j].get();
341 params.m_wsText = pDataInfo->wsDay;
342 params.m_rtPart = pDataInfo->rect;
343 params.m_dwStates = pDataInfo->dwStates;
344 if (j + 1 == m_iHovered)
345 params.m_dwStates |= CFWL_PartState_Hovered;
346
347 params.m_dwTTOStyles.single_line_ = true;
348 pTheme->DrawText(params);
349 }
350 }
351
DrawDatesOut(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)352 void CFWL_MonthCalendar::DrawDatesOut(CXFA_Graphics* pGraphics,
353 IFWL_ThemeProvider* pTheme,
354 const CFX_Matrix* pMatrix) {
355 CFWL_ThemeText params;
356 params.m_pWidget = this;
357 params.m_iPart = CFWL_Part::DatesOut;
358 params.m_pGraphics = pGraphics;
359 params.m_dwStates = CFWL_PartState_Normal;
360 params.m_iTTOAlign = FDE_TextAlignment::kCenter;
361 if (pMatrix)
362 params.m_matrix.Concat(*pMatrix);
363 pTheme->DrawText(params);
364 }
365
DrawDatesInCircle(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)366 void CFWL_MonthCalendar::DrawDatesInCircle(CXFA_Graphics* pGraphics,
367 IFWL_ThemeProvider* pTheme,
368 const CFX_Matrix* pMatrix) {
369 if (m_iMonth != m_iCurMonth || m_iYear != m_iCurYear)
370 return;
371
372 if (m_iDay < 1 || m_iDay > pdfium::CollectionSize<int32_t>(m_arrDates))
373 return;
374
375 DATEINFO* pDate = m_arrDates[m_iDay - 1].get();
376 if (!pDate)
377 return;
378
379 CFWL_ThemeBackground params;
380 params.m_pWidget = this;
381 params.m_iPart = CFWL_Part::DateInCircle;
382 params.m_pGraphics = pGraphics;
383 params.m_rtPart = pDate->rect;
384 params.m_dwStates = CFWL_PartState_Normal;
385 if (pMatrix)
386 params.m_matrix.Concat(*pMatrix);
387 pTheme->DrawBackground(params);
388 }
389
CalcSize()390 CFX_SizeF CFWL_MonthCalendar::CalcSize() {
391 IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider.Get();
392 if (!pTheme)
393 return CFX_SizeF();
394
395 float fMaxWeekW = 0.0f;
396 float fMaxWeekH = 0.0f;
397 for (int i = 0; i < 7; ++i) {
398 CFX_SizeF sz = CalcTextSize(GetAbbreviatedDayOfWeek(i), pTheme, false);
399 fMaxWeekW = (fMaxWeekW >= sz.width) ? fMaxWeekW : sz.width;
400 fMaxWeekH = (fMaxWeekH >= sz.height) ? fMaxWeekH : sz.height;
401 }
402
403 float fDayMaxW = 0.0f;
404 float fDayMaxH = 0.0f;
405 for (int day = 10; day <= 31; day++) {
406 CFX_SizeF sz = CalcTextSize(WideString::Format(L"%d", day), pTheme, false);
407 fDayMaxW = (fDayMaxW >= sz.width) ? fDayMaxW : sz.width;
408 fDayMaxH = (fDayMaxH >= sz.height) ? fDayMaxH : sz.height;
409 }
410 m_szCell.width =
411 static_cast<int>(0.5 + (fMaxWeekW >= fDayMaxW ? fMaxWeekW : fDayMaxW));
412 m_szCell.height = fMaxWeekH >= fDayMaxH ? fMaxWeekH : fDayMaxH;
413
414 CFX_SizeF fs;
415 fs.width = m_szCell.width * MONTHCAL_COLUMNS +
416 MONTHCAL_HMARGIN * MONTHCAL_COLUMNS * 2 +
417 MONTHCAL_HEADER_BTN_HMARGIN * 2;
418
419 float fMonthMaxW = 0.0f;
420 float fMonthMaxH = 0.0f;
421 for (int i = 0; i < 12; ++i) {
422 CFX_SizeF sz = CalcTextSize(GetMonth(i), pTheme, false);
423 fMonthMaxW = (fMonthMaxW >= sz.width) ? fMonthMaxW : sz.width;
424 fMonthMaxH = (fMonthMaxH >= sz.height) ? fMonthMaxH : sz.height;
425 }
426
427 CFX_SizeF szYear =
428 CalcTextSize(GetHeadText(m_iYear, m_iMonth), pTheme, false);
429 fMonthMaxH = std::max(fMonthMaxH, szYear.height);
430 m_szHead = CFX_SizeF(fMonthMaxW + szYear.width, fMonthMaxH);
431 fMonthMaxW =
432 m_szHead.width + MONTHCAL_HEADER_BTN_HMARGIN * 2 + m_szCell.width * 2;
433 fs.width = std::max(fs.width, fMonthMaxW);
434
435 m_wsToday = GetTodayText(m_iYear, m_iMonth, m_iDay);
436 m_szToday = CalcTextSize(m_wsToday, pTheme, false);
437 m_szToday.height = (m_szToday.height >= m_szCell.height) ? m_szToday.height
438 : m_szCell.height;
439 fs.height = m_szCell.width + m_szCell.height * (MONTHCAL_ROWS - 2) +
440 m_szToday.height + MONTHCAL_VMARGIN * MONTHCAL_ROWS * 2 +
441 MONTHCAL_HEADER_BTN_VMARGIN * 4;
442 return fs;
443 }
444
CalcHeadSize()445 void CFWL_MonthCalendar::CalcHeadSize() {
446 float fHeadHMargin = (m_rtClient.width - m_szHead.width) / 2;
447 float fHeadVMargin = (m_szCell.width - m_szHead.height) / 2;
448 m_rtHeadText = CFX_RectF(m_rtClient.left + fHeadHMargin,
449 m_rtClient.top + MONTHCAL_HEADER_BTN_VMARGIN +
450 MONTHCAL_VMARGIN + fHeadVMargin,
451 m_szHead);
452 }
453
CalcTodaySize()454 void CFWL_MonthCalendar::CalcTodaySize() {
455 m_rtTodayFlag = CFX_RectF(
456 m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN + MONTHCAL_HMARGIN,
457 m_rtDates.bottom() + MONTHCAL_HEADER_BTN_VMARGIN + MONTHCAL_VMARGIN,
458 m_szCell.width, m_szToday.height);
459 m_rtToday = CFX_RectF(
460 m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN + m_szCell.width +
461 MONTHCAL_HMARGIN * 2,
462 m_rtDates.bottom() + MONTHCAL_HEADER_BTN_VMARGIN + MONTHCAL_VMARGIN,
463 m_szToday);
464 }
465
Layout()466 void CFWL_MonthCalendar::Layout() {
467 m_rtClient = GetClientRect();
468
469 m_rtHead = CFX_RectF(
470 m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN, m_rtClient.top,
471 m_rtClient.width - MONTHCAL_HEADER_BTN_HMARGIN * 2,
472 m_szCell.width + (MONTHCAL_HEADER_BTN_VMARGIN + MONTHCAL_VMARGIN) * 2);
473 m_rtWeek = CFX_RectF(m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN,
474 m_rtHead.bottom(),
475 m_rtClient.width - MONTHCAL_HEADER_BTN_HMARGIN * 2,
476 m_szCell.height + MONTHCAL_VMARGIN * 2);
477 m_rtLBtn = CFX_RectF(m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN,
478 m_rtClient.top + MONTHCAL_HEADER_BTN_VMARGIN,
479 m_szCell.width, m_szCell.width);
480 m_rtRBtn = CFX_RectF(m_rtClient.left + m_rtClient.width -
481 MONTHCAL_HEADER_BTN_HMARGIN - m_szCell.width,
482 m_rtClient.top + MONTHCAL_HEADER_BTN_VMARGIN,
483 m_szCell.width, m_szCell.width);
484 m_rtHSep = CFX_RectF(
485 m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN + MONTHCAL_HMARGIN,
486 m_rtWeek.bottom() - MONTHCAL_VMARGIN,
487 m_rtClient.width - (MONTHCAL_HEADER_BTN_HMARGIN + MONTHCAL_HMARGIN) * 2,
488 MONTHCAL_HSEP_HEIGHT);
489 m_rtDates = CFX_RectF(m_rtClient.left + MONTHCAL_HEADER_BTN_HMARGIN,
490 m_rtWeek.bottom(),
491 m_rtClient.width - MONTHCAL_HEADER_BTN_HMARGIN * 2,
492 m_szCell.height * (MONTHCAL_ROWS - 3) +
493 MONTHCAL_VMARGIN * (MONTHCAL_ROWS - 3) * 2);
494
495 CalDateItem();
496 }
497
CalDateItem()498 void CFWL_MonthCalendar::CalDateItem() {
499 bool bNewWeek = false;
500 int32_t iWeekOfMonth = 0;
501 float fLeft = m_rtDates.left;
502 float fTop = m_rtDates.top;
503 for (const auto& pDateInfo : m_arrDates) {
504 if (bNewWeek) {
505 iWeekOfMonth++;
506 bNewWeek = false;
507 }
508 pDateInfo->rect = CFX_RectF(
509 fLeft +
510 pDateInfo->iDayOfWeek * (m_szCell.width + (MONTHCAL_HMARGIN * 2)),
511 fTop + iWeekOfMonth * (m_szCell.height + (MONTHCAL_VMARGIN * 2)),
512 m_szCell.width + (MONTHCAL_HMARGIN * 2),
513 m_szCell.height + (MONTHCAL_VMARGIN * 2));
514 if (pDateInfo->iDayOfWeek >= 6)
515 bNewWeek = true;
516 }
517 }
518
GetCapValue()519 void CFWL_MonthCalendar::GetCapValue() {
520 if (!m_pProperties->m_pThemeProvider)
521 m_pProperties->m_pThemeProvider = GetAvailableTheme();
522 }
523
InitDate()524 void CFWL_MonthCalendar::InitDate() {
525 CFX_DateTime now = CFX_DateTime::Now();
526
527 m_iYear = now.GetYear();
528 m_iMonth = now.GetMonth();
529 m_iDay = now.GetDay();
530 m_iCurYear = m_iYear;
531 m_iCurMonth = m_iMonth;
532
533 m_wsToday = GetTodayText(m_iYear, m_iMonth, m_iDay);
534 m_wsHead = GetHeadText(m_iCurYear, m_iCurMonth);
535 m_dtMin = DATE(1500, 12, 1);
536 m_dtMax = DATE(2200, 1, 1);
537 }
538
ClearDateItem()539 void CFWL_MonthCalendar::ClearDateItem() {
540 m_arrDates.clear();
541 }
542
ResetDateItem()543 void CFWL_MonthCalendar::ResetDateItem() {
544 int32_t iDays = FX_DaysInMonth(m_iCurYear, m_iCurMonth);
545 int32_t iDayOfWeek =
546 CFX_DateTime(m_iCurYear, m_iCurMonth, 1, 0, 0, 0, 0).GetDayOfWeek();
547 for (int32_t i = 0; i < iDays; i++) {
548 if (iDayOfWeek >= 7)
549 iDayOfWeek = 0;
550
551 uint32_t dwStates = 0;
552 if (m_iYear == m_iCurYear && m_iMonth == m_iCurMonth && m_iDay == (i + 1))
553 dwStates |= FWL_ITEMSTATE_MCD_Flag;
554 if (pdfium::ContainsValue(m_arrSelDays, i + 1))
555 dwStates |= FWL_ITEMSTATE_MCD_Selected;
556
557 CFX_RectF rtDate;
558 m_arrDates.push_back(pdfium::MakeUnique<DATEINFO>(
559 i + 1, iDayOfWeek, dwStates, rtDate, WideString::Format(L"%d", i + 1)));
560 iDayOfWeek++;
561 }
562 }
563
NextMonth()564 void CFWL_MonthCalendar::NextMonth() {
565 int32_t iYear = m_iCurYear;
566 int32_t iMonth = m_iCurMonth;
567 if (iMonth >= 12) {
568 iMonth = 1;
569 iYear++;
570 } else {
571 iMonth++;
572 }
573 DATE dt(m_iCurYear, m_iCurMonth, 1);
574 if (!(dt < m_dtMax))
575 return;
576
577 m_iCurYear = iYear, m_iCurMonth = iMonth;
578 ChangeToMonth(m_iCurYear, m_iCurMonth);
579 }
580
PrevMonth()581 void CFWL_MonthCalendar::PrevMonth() {
582 int32_t iYear = m_iCurYear;
583 int32_t iMonth = m_iCurMonth;
584 if (iMonth <= 1) {
585 iMonth = 12;
586 iYear--;
587 } else {
588 iMonth--;
589 }
590
591 DATE dt(m_iCurYear, m_iCurMonth, 1);
592 if (!(dt > m_dtMin))
593 return;
594
595 m_iCurYear = iYear, m_iCurMonth = iMonth;
596 ChangeToMonth(m_iCurYear, m_iCurMonth);
597 }
598
ChangeToMonth(int32_t iYear,int32_t iMonth)599 void CFWL_MonthCalendar::ChangeToMonth(int32_t iYear, int32_t iMonth) {
600 m_iCurYear = iYear;
601 m_iCurMonth = iMonth;
602 m_iHovered = -1;
603
604 ClearDateItem();
605 ResetDateItem();
606 CalDateItem();
607 m_wsHead = GetHeadText(m_iCurYear, m_iCurMonth);
608 }
609
RemoveSelDay()610 void CFWL_MonthCalendar::RemoveSelDay() {
611 int32_t iDatesCount = pdfium::CollectionSize<int32_t>(m_arrDates);
612 for (int32_t iSelDay : m_arrSelDays) {
613 if (iSelDay <= iDatesCount)
614 m_arrDates[iSelDay - 1]->dwStates &= ~FWL_ITEMSTATE_MCD_Selected;
615 }
616 m_arrSelDays.clear();
617 }
618
AddSelDay(int32_t iDay)619 void CFWL_MonthCalendar::AddSelDay(int32_t iDay) {
620 ASSERT(iDay > 0);
621 if (!pdfium::ContainsValue(m_arrSelDays, iDay))
622 return;
623
624 RemoveSelDay();
625 if (iDay <= pdfium::CollectionSize<int32_t>(m_arrDates))
626 m_arrDates[iDay - 1]->dwStates |= FWL_ITEMSTATE_MCD_Selected;
627
628 m_arrSelDays.push_back(iDay);
629 }
630
JumpToToday()631 void CFWL_MonthCalendar::JumpToToday() {
632 if (m_iYear != m_iCurYear || m_iMonth != m_iCurMonth) {
633 m_iCurYear = m_iYear;
634 m_iCurMonth = m_iMonth;
635 ChangeToMonth(m_iYear, m_iMonth);
636 AddSelDay(m_iDay);
637 return;
638 }
639
640 if (!pdfium::ContainsValue(m_arrSelDays, m_iDay))
641 AddSelDay(m_iDay);
642 }
643
GetHeadText(int32_t iYear,int32_t iMonth)644 WideString CFWL_MonthCalendar::GetHeadText(int32_t iYear, int32_t iMonth) {
645 ASSERT(iMonth > 0);
646 ASSERT(iMonth < 13);
647
648 static const wchar_t* const pMonth[] = {L"January", L"February", L"March",
649 L"April", L"May", L"June",
650 L"July", L"August", L"September",
651 L"October", L"November", L"December"};
652 return WideString::Format(L"%ls, %d", pMonth[iMonth - 1], iYear);
653 }
654
GetTodayText(int32_t iYear,int32_t iMonth,int32_t iDay)655 WideString CFWL_MonthCalendar::GetTodayText(int32_t iYear,
656 int32_t iMonth,
657 int32_t iDay) {
658 return WideString::Format(L"Today, %d/%d/%d", iDay, iMonth, iYear);
659 }
660
GetDayAtPoint(const CFX_PointF & point) const661 int32_t CFWL_MonthCalendar::GetDayAtPoint(const CFX_PointF& point) const {
662 int i = 1; // one-based day values.
663 for (const auto& pDateInfo : m_arrDates) {
664 if (pDateInfo->rect.Contains(point))
665 return i;
666 ++i;
667 }
668 return -1;
669 }
670
GetDayRect(int32_t iDay)671 CFX_RectF CFWL_MonthCalendar::GetDayRect(int32_t iDay) {
672 if (iDay <= 0 || iDay > pdfium::CollectionSize<int32_t>(m_arrDates))
673 return CFX_RectF();
674
675 DATEINFO* pDateInfo = m_arrDates[iDay - 1].get();
676 return pDateInfo ? pDateInfo->rect : CFX_RectF();
677 }
678
OnProcessMessage(CFWL_Message * pMessage)679 void CFWL_MonthCalendar::OnProcessMessage(CFWL_Message* pMessage) {
680 if (!pMessage)
681 return;
682
683 switch (pMessage->GetType()) {
684 case CFWL_Message::Type::SetFocus:
685 case CFWL_Message::Type::KillFocus:
686 GetOuter()->GetDelegate()->OnProcessMessage(pMessage);
687 break;
688 case CFWL_Message::Type::Key:
689 break;
690 case CFWL_Message::Type::Mouse: {
691 CFWL_MessageMouse* pMouse = static_cast<CFWL_MessageMouse*>(pMessage);
692 switch (pMouse->m_dwCmd) {
693 case FWL_MouseCommand::LeftButtonDown:
694 OnLButtonDown(pMouse);
695 break;
696 case FWL_MouseCommand::LeftButtonUp:
697 OnLButtonUp(pMouse);
698 break;
699 case FWL_MouseCommand::Move:
700 OnMouseMove(pMouse);
701 break;
702 case FWL_MouseCommand::Leave:
703 OnMouseLeave(pMouse);
704 break;
705 default:
706 break;
707 }
708 break;
709 }
710 default:
711 break;
712 }
713 // Dst target could be |this|, continue only if not destroyed by above.
714 if (pMessage->GetDstTarget())
715 CFWL_Widget::OnProcessMessage(pMessage);
716 }
717
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)718 void CFWL_MonthCalendar::OnDrawWidget(CXFA_Graphics* pGraphics,
719 const CFX_Matrix& matrix) {
720 DrawWidget(pGraphics, matrix);
721 }
722
OnLButtonDown(CFWL_MessageMouse * pMsg)723 void CFWL_MonthCalendar::OnLButtonDown(CFWL_MessageMouse* pMsg) {
724 if (m_rtLBtn.Contains(pMsg->m_pos)) {
725 m_iLBtnPartStates = CFWL_PartState_Pressed;
726 PrevMonth();
727 RepaintRect(m_rtClient);
728 } else if (m_rtRBtn.Contains(pMsg->m_pos)) {
729 m_iRBtnPartStates |= CFWL_PartState_Pressed;
730 NextMonth();
731 RepaintRect(m_rtClient);
732 } else if (m_rtToday.Contains(pMsg->m_pos)) {
733 JumpToToday();
734 RepaintRect(m_rtClient);
735 }
736 }
737
OnLButtonUp(CFWL_MessageMouse * pMsg)738 void CFWL_MonthCalendar::OnLButtonUp(CFWL_MessageMouse* pMsg) {
739 if (m_rtLBtn.Contains(pMsg->m_pos)) {
740 m_iLBtnPartStates = 0;
741 RepaintRect(m_rtLBtn);
742 return;
743 }
744 if (m_rtRBtn.Contains(pMsg->m_pos)) {
745 m_iRBtnPartStates = 0;
746 RepaintRect(m_rtRBtn);
747 return;
748 }
749 if (m_rtToday.Contains(pMsg->m_pos))
750 return;
751
752 int32_t iOldSel = 0;
753 if (!m_arrSelDays.empty())
754 iOldSel = m_arrSelDays[0];
755
756 int32_t iCurSel = GetDayAtPoint(pMsg->m_pos);
757 if (iCurSel > 0) {
758 DATEINFO* lpDatesInfo = m_arrDates[iCurSel - 1].get();
759 CFX_RectF rtInvalidate(lpDatesInfo->rect);
760 if (iOldSel > 0 && iOldSel <= pdfium::CollectionSize<int32_t>(m_arrDates)) {
761 lpDatesInfo = m_arrDates[iOldSel - 1].get();
762 rtInvalidate.Union(lpDatesInfo->rect);
763 }
764 AddSelDay(iCurSel);
765 CFWL_DateTimePicker* pDateTime =
766 static_cast<CFWL_DateTimePicker*>(m_pOuter);
767 pDateTime->ProcessSelChanged(m_iCurYear, m_iCurMonth, iCurSel);
768 pDateTime->ShowMonthCalendar(false);
769 }
770 }
771
OnMouseMove(CFWL_MessageMouse * pMsg)772 void CFWL_MonthCalendar::OnMouseMove(CFWL_MessageMouse* pMsg) {
773 bool bRepaint = false;
774 CFX_RectF rtInvalidate;
775 if (m_rtDates.Contains(pMsg->m_pos)) {
776 int32_t iHover = GetDayAtPoint(pMsg->m_pos);
777 bRepaint = m_iHovered != iHover;
778 if (bRepaint) {
779 if (m_iHovered > 0)
780 rtInvalidate = GetDayRect(m_iHovered);
781 if (iHover > 0) {
782 CFX_RectF rtDay = GetDayRect(iHover);
783 if (rtInvalidate.IsEmpty())
784 rtInvalidate = rtDay;
785 else
786 rtInvalidate.Union(rtDay);
787 }
788 }
789 m_iHovered = iHover;
790 } else {
791 bRepaint = m_iHovered > 0;
792 if (bRepaint)
793 rtInvalidate = GetDayRect(m_iHovered);
794
795 m_iHovered = -1;
796 }
797 if (bRepaint && !rtInvalidate.IsEmpty())
798 RepaintRect(rtInvalidate);
799 }
800
OnMouseLeave(CFWL_MessageMouse * pMsg)801 void CFWL_MonthCalendar::OnMouseLeave(CFWL_MessageMouse* pMsg) {
802 if (m_iHovered <= 0)
803 return;
804
805 CFX_RectF rtInvalidate = GetDayRect(m_iHovered);
806 m_iHovered = -1;
807 if (!rtInvalidate.IsEmpty())
808 RepaintRect(rtInvalidate);
809 }
810
DATEINFO(int32_t day,int32_t dayofweek,uint32_t dwSt,CFX_RectF rc,const WideString & wsday)811 CFWL_MonthCalendar::DATEINFO::DATEINFO(int32_t day,
812 int32_t dayofweek,
813 uint32_t dwSt,
814 CFX_RectF rc,
815 const WideString& wsday)
816 : iDay(day),
817 iDayOfWeek(dayofweek),
818 dwStates(dwSt),
819 rect(rc),
820 wsDay(wsday) {}
821
~DATEINFO()822 CFWL_MonthCalendar::DATEINFO::~DATEINFO() {}
823