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_datetimepicker.h"
8
9 #include <memory>
10 #include <utility>
11
12 #include "third_party/base/ptr_util.h"
13 #include "xfa/fwl/cfwl_event.h"
14 #include "xfa/fwl/cfwl_eventselectchanged.h"
15 #include "xfa/fwl/cfwl_messagemouse.h"
16 #include "xfa/fwl/cfwl_messagesetfocus.h"
17 #include "xfa/fwl/cfwl_notedriver.h"
18 #include "xfa/fwl/cfwl_themebackground.h"
19 #include "xfa/fwl/cfwl_widgetmgr.h"
20 #include "xfa/fwl/ifwl_themeprovider.h"
21
22 namespace {
23
24 const int kDateTimePickerHeight = 20;
25
26 } // namespace
CFWL_DateTimePicker(const CFWL_App * app)27 CFWL_DateTimePicker::CFWL_DateTimePicker(const CFWL_App* app)
28 : CFWL_Widget(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr) {
29 m_pProperties->m_dwStyleExes = FWL_STYLEEXT_DTP_ShortDateFormat;
30
31 auto monthProp = pdfium::MakeUnique<CFWL_WidgetProperties>();
32 monthProp->m_dwStyles = FWL_WGTSTYLE_Popup | FWL_WGTSTYLE_Border;
33 monthProp->m_dwStates = FWL_WGTSTATE_Invisible;
34 monthProp->m_pParent = this;
35 monthProp->m_pThemeProvider = m_pProperties->m_pThemeProvider;
36 m_pMonthCal = pdfium::MakeUnique<CFWL_MonthCalendar>(
37 m_pOwnerApp.Get(), std::move(monthProp), this);
38
39 m_pMonthCal->SetWidgetRect(
40 CFX_RectF(0, 0, m_pMonthCal->GetAutosizedWidgetRect().Size()));
41
42 auto editProp = pdfium::MakeUnique<CFWL_WidgetProperties>();
43 editProp->m_pParent = this;
44 editProp->m_pThemeProvider = m_pProperties->m_pThemeProvider;
45
46 m_pEdit = pdfium::MakeUnique<CFWL_DateTimeEdit>(m_pOwnerApp.Get(),
47 std::move(editProp), this);
48 RegisterEventTarget(m_pMonthCal.get());
49 RegisterEventTarget(m_pEdit.get());
50 }
51
~CFWL_DateTimePicker()52 CFWL_DateTimePicker::~CFWL_DateTimePicker() {
53 UnregisterEventTarget();
54 }
55
GetClassID() const56 FWL_Type CFWL_DateTimePicker::GetClassID() const {
57 return FWL_Type::DateTimePicker;
58 }
59
Update()60 void CFWL_DateTimePicker::Update() {
61 if (m_iLock)
62 return;
63
64 if (!m_pProperties->m_pThemeProvider)
65 m_pProperties->m_pThemeProvider = GetAvailableTheme();
66 m_pEdit->SetThemeProvider(m_pProperties->m_pThemeProvider.Get());
67 m_rtClient = GetClientRect();
68 m_pEdit->SetWidgetRect(m_rtClient);
69 ResetEditAlignment();
70 m_pEdit->Update();
71 if (!m_pMonthCal->GetThemeProvider())
72 m_pMonthCal->SetThemeProvider(m_pProperties->m_pThemeProvider.Get());
73
74 IFWL_ThemeProvider* theme = GetAvailableTheme();
75 if (!theme)
76 return;
77
78 m_fBtn = theme->GetScrollBarWidth();
79 CFX_RectF rtMonthCal = m_pMonthCal->GetAutosizedWidgetRect();
80 CFX_RectF rtPopUp(rtMonthCal.left, rtMonthCal.top + kDateTimePickerHeight,
81 rtMonthCal.width, rtMonthCal.height);
82 m_pMonthCal->SetWidgetRect(rtPopUp);
83 m_pMonthCal->Update();
84 }
85
HitTest(const CFX_PointF & point)86 FWL_WidgetHit CFWL_DateTimePicker::HitTest(const CFX_PointF& point) {
87 CFX_RectF rect(0, 0, m_pProperties->m_rtWidget.width,
88 m_pProperties->m_rtWidget.height);
89 if (rect.Contains(point))
90 return FWL_WidgetHit::Edit;
91 if (NeedsToShowButton())
92 rect.width += m_fBtn;
93 if (rect.Contains(point))
94 return FWL_WidgetHit::Client;
95 if (IsMonthCalendarVisible()) {
96 if (m_pMonthCal->GetWidgetRect().Contains(point))
97 return FWL_WidgetHit::Client;
98 }
99 return FWL_WidgetHit::Unknown;
100 }
101
DrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)102 void CFWL_DateTimePicker::DrawWidget(CXFA_Graphics* pGraphics,
103 const CFX_Matrix& matrix) {
104 if (!pGraphics)
105 return;
106
107 IFWL_ThemeProvider* pTheme = m_pProperties->m_pThemeProvider.Get();
108 if (!pTheme)
109 return;
110
111 if (HasBorder())
112 DrawBorder(pGraphics, CFWL_Part::Border, pTheme, matrix);
113 if (!m_rtBtn.IsEmpty())
114 DrawDropDownButton(pGraphics, pTheme, &matrix);
115
116 if (m_pEdit) {
117 CFX_RectF rtEdit = m_pEdit->GetWidgetRect();
118
119 CFX_Matrix mt(1, 0, 0, 1, rtEdit.left, rtEdit.top);
120 mt.Concat(matrix);
121 m_pEdit->DrawWidget(pGraphics, mt);
122 }
123 if (!IsMonthCalendarVisible())
124 return;
125
126 CFX_RectF rtMonth = m_pMonthCal->GetWidgetRect();
127 CFX_Matrix mt(1, 0, 0, 1, rtMonth.left, rtMonth.top);
128 mt.Concat(matrix);
129 m_pMonthCal->DrawWidget(pGraphics, mt);
130 }
131
SetThemeProvider(IFWL_ThemeProvider * pTP)132 void CFWL_DateTimePicker::SetThemeProvider(IFWL_ThemeProvider* pTP) {
133 m_pProperties->m_pThemeProvider = pTP;
134 m_pMonthCal->SetThemeProvider(pTP);
135 }
136
GetCurSel(int32_t & iYear,int32_t & iMonth,int32_t & iDay)137 void CFWL_DateTimePicker::GetCurSel(int32_t& iYear,
138 int32_t& iMonth,
139 int32_t& iDay) {
140 iYear = m_iYear;
141 iMonth = m_iMonth;
142 iDay = m_iDay;
143 }
144
SetCurSel(int32_t iYear,int32_t iMonth,int32_t iDay)145 void CFWL_DateTimePicker::SetCurSel(int32_t iYear,
146 int32_t iMonth,
147 int32_t iDay) {
148 if (iYear <= 0 || iYear >= 3000)
149 return;
150 if (iMonth <= 0 || iMonth >= 13)
151 return;
152 if (iDay <= 0 || iDay >= 32)
153 return;
154
155 m_iYear = iYear;
156 m_iMonth = iMonth;
157 m_iDay = iDay;
158 m_pMonthCal->SetSelect(iYear, iMonth, iDay);
159 }
160
SetEditText(const WideString & wsText)161 void CFWL_DateTimePicker::SetEditText(const WideString& wsText) {
162 if (!m_pEdit)
163 return;
164
165 m_pEdit->SetText(wsText);
166 RepaintRect(m_rtClient);
167
168 CFWL_Event ev(CFWL_Event::Type::EditChanged);
169 DispatchEvent(&ev);
170 }
171
GetEditText() const172 WideString CFWL_DateTimePicker::GetEditText() const {
173 return m_pEdit ? m_pEdit->GetText() : WideString();
174 }
175
GetEditTextLength() const176 int32_t CFWL_DateTimePicker::GetEditTextLength() const {
177 return m_pEdit ? m_pEdit->GetTextLength() : 0;
178 }
179
GetBBox() const180 CFX_RectF CFWL_DateTimePicker::GetBBox() const {
181 CFX_RectF rect = m_pProperties->m_rtWidget;
182 if (NeedsToShowButton())
183 rect.width += m_fBtn;
184 if (!IsMonthCalendarVisible())
185 return rect;
186
187 CFX_RectF rtMonth = m_pMonthCal->GetWidgetRect();
188 rtMonth.Offset(m_pProperties->m_rtWidget.left, m_pProperties->m_rtWidget.top);
189 rect.Union(rtMonth);
190 return rect;
191 }
192
ModifyEditStylesEx(uint32_t dwStylesExAdded,uint32_t dwStylesExRemoved)193 void CFWL_DateTimePicker::ModifyEditStylesEx(uint32_t dwStylesExAdded,
194 uint32_t dwStylesExRemoved) {
195 m_pEdit->ModifyStylesEx(dwStylesExAdded, dwStylesExRemoved);
196 }
197
DrawDropDownButton(CXFA_Graphics * pGraphics,IFWL_ThemeProvider * pTheme,const CFX_Matrix * pMatrix)198 void CFWL_DateTimePicker::DrawDropDownButton(CXFA_Graphics* pGraphics,
199 IFWL_ThemeProvider* pTheme,
200 const CFX_Matrix* pMatrix) {
201 CFWL_ThemeBackground param;
202 param.m_pWidget = this;
203 param.m_iPart = CFWL_Part::DropDownButton;
204 param.m_dwStates = m_iBtnState;
205 param.m_pGraphics = pGraphics;
206 param.m_rtPart = m_rtBtn;
207 if (pMatrix)
208 param.m_matrix.Concat(*pMatrix);
209 pTheme->DrawBackground(param);
210 }
211
FormatDateString(int32_t iYear,int32_t iMonth,int32_t iDay)212 WideString CFWL_DateTimePicker::FormatDateString(int32_t iYear,
213 int32_t iMonth,
214 int32_t iDay) {
215 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_DTP_ShortDateFormat)
216 return WideString::Format(L"%d-%d-%d", iYear, iMonth, iDay);
217
218 return WideString::Format(L"%d Year %d Month %d Day", iYear, iMonth, iDay);
219 }
220
ShowMonthCalendar(bool bActivate)221 void CFWL_DateTimePicker::ShowMonthCalendar(bool bActivate) {
222 if (IsMonthCalendarVisible() == bActivate)
223 return;
224
225 if (bActivate) {
226 CFX_RectF rtMonthCal = m_pMonthCal->GetAutosizedWidgetRect();
227 float fPopupMin = rtMonthCal.height;
228 float fPopupMax = rtMonthCal.height;
229 CFX_RectF rtAnchor(m_pProperties->m_rtWidget);
230 rtAnchor.width = rtMonthCal.width;
231 rtMonthCal.left = m_rtClient.left;
232 rtMonthCal.top = rtAnchor.Height();
233 GetPopupPos(fPopupMin, fPopupMax, rtAnchor, &rtMonthCal);
234 m_pMonthCal->SetWidgetRect(rtMonthCal);
235 if (m_iYear > 0 && m_iMonth > 0 && m_iDay > 0)
236 m_pMonthCal->SetSelect(m_iYear, m_iMonth, m_iDay);
237 m_pMonthCal->Update();
238 }
239 if (bActivate)
240 m_pMonthCal->RemoveStates(FWL_WGTSTATE_Invisible);
241 else
242 m_pMonthCal->SetStates(FWL_WGTSTATE_Invisible);
243
244 if (bActivate) {
245 CFWL_MessageSetFocus msg(m_pEdit.get(), m_pMonthCal.get());
246 m_pEdit->GetDelegate()->OnProcessMessage(&msg);
247 }
248
249 CFX_RectF rtInvalidate(0, 0, m_pProperties->m_rtWidget.width,
250 m_pProperties->m_rtWidget.height);
251
252 CFX_RectF rtCal = m_pMonthCal->GetWidgetRect();
253 rtInvalidate.Union(rtCal);
254 rtInvalidate.Inflate(2, 2);
255 RepaintRect(rtInvalidate);
256 }
257
IsMonthCalendarVisible() const258 bool CFWL_DateTimePicker::IsMonthCalendarVisible() const {
259 return m_pMonthCal && m_pMonthCal->IsVisible();
260 }
261
ResetEditAlignment()262 void CFWL_DateTimePicker::ResetEditAlignment() {
263 if (!m_pEdit)
264 return;
265
266 uint32_t dwAdd = 0;
267 switch (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_DTP_EditHAlignMask) {
268 case FWL_STYLEEXT_DTP_EditHCenter: {
269 dwAdd |= FWL_STYLEEXT_EDT_HCenter;
270 break;
271 }
272 case FWL_STYLEEXT_DTP_EditHFar: {
273 dwAdd |= FWL_STYLEEXT_EDT_HFar;
274 break;
275 }
276 default: {
277 dwAdd |= FWL_STYLEEXT_EDT_HNear;
278 break;
279 }
280 }
281 switch (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_DTP_EditVAlignMask) {
282 case FWL_STYLEEXT_DTP_EditVCenter: {
283 dwAdd |= FWL_STYLEEXT_EDT_VCenter;
284 break;
285 }
286 case FWL_STYLEEXT_DTP_EditVFar: {
287 dwAdd |= FWL_STYLEEXT_EDT_VFar;
288 break;
289 }
290 default: {
291 dwAdd |= FWL_STYLEEXT_EDT_VNear;
292 break;
293 }
294 }
295 if (m_pProperties->m_dwStyleExes & FWL_STYLEEXT_DTP_EditJustified)
296 dwAdd |= FWL_STYLEEXT_EDT_Justified;
297
298 m_pEdit->ModifyStylesEx(dwAdd, FWL_STYLEEXT_EDT_HAlignMask |
299 FWL_STYLEEXT_EDT_HAlignModeMask |
300 FWL_STYLEEXT_EDT_VAlignMask);
301 }
302
ProcessSelChanged(int32_t iYear,int32_t iMonth,int32_t iDay)303 void CFWL_DateTimePicker::ProcessSelChanged(int32_t iYear,
304 int32_t iMonth,
305 int32_t iDay) {
306 m_iYear = iYear;
307 m_iMonth = iMonth;
308 m_iDay = iDay;
309
310 WideString wsText = FormatDateString(m_iYear, m_iMonth, m_iDay);
311 m_pEdit->SetText(wsText);
312 m_pEdit->Update();
313 RepaintRect(m_rtClient);
314
315 CFWL_EventSelectChanged ev(this);
316 ev.iYear = m_iYear;
317 ev.iMonth = m_iMonth;
318 ev.iDay = m_iDay;
319 DispatchEvent(&ev);
320 }
321
NeedsToShowButton() const322 bool CFWL_DateTimePicker::NeedsToShowButton() const {
323 return m_pProperties->m_dwStates & FWL_WGTSTATE_Focused ||
324 m_pMonthCal->GetStates() & FWL_WGTSTATE_Focused ||
325 m_pEdit->GetStates() & FWL_WGTSTATE_Focused;
326 }
327
OnProcessMessage(CFWL_Message * pMessage)328 void CFWL_DateTimePicker::OnProcessMessage(CFWL_Message* pMessage) {
329 if (!pMessage)
330 return;
331
332 switch (pMessage->GetType()) {
333 case CFWL_Message::Type::SetFocus:
334 OnFocusChanged(pMessage, true);
335 break;
336 case CFWL_Message::Type::KillFocus:
337 OnFocusChanged(pMessage, false);
338 break;
339 case CFWL_Message::Type::Mouse: {
340 CFWL_MessageMouse* pMouse = static_cast<CFWL_MessageMouse*>(pMessage);
341 switch (pMouse->m_dwCmd) {
342 case FWL_MouseCommand::LeftButtonDown:
343 OnLButtonDown(pMouse);
344 break;
345 case FWL_MouseCommand::LeftButtonUp:
346 OnLButtonUp(pMouse);
347 break;
348 case FWL_MouseCommand::Move:
349 OnMouseMove(pMouse);
350 break;
351 case FWL_MouseCommand::Leave:
352 OnMouseLeave(pMouse);
353 break;
354 default:
355 break;
356 }
357 break;
358 }
359 case CFWL_Message::Type::Key: {
360 if (m_pEdit->GetStates() & FWL_WGTSTATE_Focused) {
361 m_pEdit->GetDelegate()->OnProcessMessage(pMessage);
362 return;
363 }
364 break;
365 }
366 default:
367 break;
368 }
369 // Dst target could be |this|, continue only if not destroyed by above.
370 if (pMessage->GetDstTarget())
371 CFWL_Widget::OnProcessMessage(pMessage);
372 }
373
OnDrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)374 void CFWL_DateTimePicker::OnDrawWidget(CXFA_Graphics* pGraphics,
375 const CFX_Matrix& matrix) {
376 DrawWidget(pGraphics, matrix);
377 }
378
OnFocusChanged(CFWL_Message * pMsg,bool bSet)379 void CFWL_DateTimePicker::OnFocusChanged(CFWL_Message* pMsg, bool bSet) {
380 if (!pMsg)
381 return;
382
383 CFX_RectF rtInvalidate(m_rtBtn);
384 if (bSet) {
385 m_pProperties->m_dwStates |= FWL_WGTSTATE_Focused;
386 if (m_pEdit && !(m_pEdit->GetStylesEx() & FWL_STYLEEXT_EDT_ReadOnly)) {
387 m_rtBtn = CFX_RectF(m_pProperties->m_rtWidget.width, 0, m_fBtn,
388 m_pProperties->m_rtWidget.height - 1);
389 }
390 rtInvalidate = m_rtBtn;
391 pMsg->SetDstTarget(m_pEdit.get());
392 m_pEdit->GetDelegate()->OnProcessMessage(pMsg);
393 } else {
394 m_pProperties->m_dwStates &= ~FWL_WGTSTATE_Focused;
395 m_rtBtn = CFX_RectF();
396 if (IsMonthCalendarVisible())
397 ShowMonthCalendar(false);
398 if (m_pEdit->GetStates() & FWL_WGTSTATE_Focused) {
399 pMsg->SetSrcTarget(m_pEdit.get());
400 m_pEdit->GetDelegate()->OnProcessMessage(pMsg);
401 }
402 }
403 rtInvalidate.Inflate(2, 2);
404 RepaintRect(rtInvalidate);
405 }
406
OnLButtonDown(CFWL_MessageMouse * pMsg)407 void CFWL_DateTimePicker::OnLButtonDown(CFWL_MessageMouse* pMsg) {
408 if (!pMsg)
409 return;
410 if (!m_rtBtn.Contains(pMsg->m_pos))
411 return;
412
413 if (IsMonthCalendarVisible()) {
414 ShowMonthCalendar(false);
415 return;
416 }
417 ShowMonthCalendar(true);
418
419 m_bLBtnDown = true;
420 RepaintRect(m_rtClient);
421 }
422
OnLButtonUp(CFWL_MessageMouse * pMsg)423 void CFWL_DateTimePicker::OnLButtonUp(CFWL_MessageMouse* pMsg) {
424 if (!pMsg)
425 return;
426
427 m_bLBtnDown = false;
428 if (m_rtBtn.Contains(pMsg->m_pos))
429 m_iBtnState = CFWL_PartState_Hovered;
430 else
431 m_iBtnState = CFWL_PartState_Normal;
432 RepaintRect(m_rtBtn);
433 }
434
OnMouseMove(CFWL_MessageMouse * pMsg)435 void CFWL_DateTimePicker::OnMouseMove(CFWL_MessageMouse* pMsg) {
436 if (!m_rtBtn.Contains(pMsg->m_pos))
437 m_iBtnState = CFWL_PartState_Normal;
438 RepaintRect(m_rtBtn);
439 }
440
OnMouseLeave(CFWL_MessageMouse * pMsg)441 void CFWL_DateTimePicker::OnMouseLeave(CFWL_MessageMouse* pMsg) {
442 if (!pMsg)
443 return;
444 m_iBtnState = CFWL_PartState_Normal;
445 RepaintRect(m_rtBtn);
446 }
447
GetPopupPos(float fMinHeight,float fMaxHeight,const CFX_RectF & rtAnchor,CFX_RectF * pPopupRect)448 void CFWL_DateTimePicker::GetPopupPos(float fMinHeight,
449 float fMaxHeight,
450 const CFX_RectF& rtAnchor,
451 CFX_RectF* pPopupRect) {
452 m_pWidgetMgr->GetAdapterPopupPos(this, fMinHeight, fMaxHeight, rtAnchor,
453 pPopupRect);
454 }
455
ClearText()456 void CFWL_DateTimePicker::ClearText() {
457 m_pEdit->ClearText();
458 }
459
SelectAll()460 void CFWL_DateTimePicker::SelectAll() {
461 m_pEdit->SelectAll();
462 }
463
ClearSelection()464 void CFWL_DateTimePicker::ClearSelection() {
465 m_pEdit->ClearSelection();
466 }
467
Copy()468 Optional<WideString> CFWL_DateTimePicker::Copy() {
469 return m_pEdit->Copy();
470 }
471
Cut()472 Optional<WideString> CFWL_DateTimePicker::Cut() {
473 return m_pEdit->Cut();
474 }
475
Paste(const WideString & wsPaste)476 bool CFWL_DateTimePicker::Paste(const WideString& wsPaste) {
477 return m_pEdit->Paste(wsPaste);
478 }
479
Undo()480 bool CFWL_DateTimePicker::Undo() {
481 return m_pEdit->Undo();
482 }
483
Redo()484 bool CFWL_DateTimePicker::Redo() {
485 return m_pEdit->Redo();
486 }
487
CanUndo()488 bool CFWL_DateTimePicker::CanUndo() {
489 return m_pEdit->CanUndo();
490 }
491
CanRedo()492 bool CFWL_DateTimePicker::CanRedo() {
493 return m_pEdit->CanRedo();
494 }
495