Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfwl_datetimepicker.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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 "xfa/fwl/cfwl_app.h"
10#include "xfa/fwl/cfwl_event.h"
11#include "xfa/fwl/cfwl_eventselectchanged.h"
12#include "xfa/fwl/cfwl_messagemouse.h"
13#include "xfa/fwl/cfwl_messagesetfocus.h"
14#include "xfa/fwl/cfwl_notedriver.h"
15#include "xfa/fwl/cfwl_themebackground.h"
16#include "xfa/fwl/cfwl_widgetmgr.h"
17#include "xfa/fwl/ifwl_themeprovider.h"
18
19namespace pdfium {
20
21namespace {
22
23constexpr int kDateTimePickerHeight = 20;
24
25} // namespace
26
27CFWL_DateTimePicker::CFWL_DateTimePicker(CFWL_App* app)
28 : CFWL_Widget(app,
30 nullptr),
31 m_pEdit(cppgc::MakeGarbageCollected<CFWL_DateTimeEdit>(
32 app->GetHeap()->GetAllocationHandle(),
33 app,
34 Properties(),
35 this)),
36 m_pMonthCal(cppgc::MakeGarbageCollected<CFWL_MonthCalendar>(
37 app->GetHeap()->GetAllocationHandle(),
38 app,
41 this)) {
42 m_pMonthCal->SetWidgetRect(
43 CFX_RectF(0, 0, m_pMonthCal->GetAutosizedWidgetRect().Size()));
44
45 CFWL_NoteDriver* pNoteDriver = GetFWLApp()->GetNoteDriver();
46 pNoteDriver->RegisterEventTarget(this, m_pMonthCal);
47 pNoteDriver->RegisterEventTarget(this, m_pEdit);
48}
49
50CFWL_DateTimePicker::~CFWL_DateTimePicker() = default;
51
52void CFWL_DateTimePicker::PreFinalize() {
55}
56
57void CFWL_DateTimePicker::Trace(cppgc::Visitor* visitor) const {
58 CFWL_Widget::Trace(visitor);
59 visitor->Trace(m_pEdit);
60 visitor->Trace(m_pMonthCal);
61}
62
63FWL_Type CFWL_DateTimePicker::GetClassID() const {
65}
66
67void CFWL_DateTimePicker::Update() {
68 if (IsLocked())
69 return;
70
71 m_ClientRect = GetClientRect();
72 m_pEdit->SetWidgetRect(m_ClientRect);
73 ResetEditAlignment();
74 m_pEdit->Update();
75
76 m_fBtn = GetThemeProvider()->GetScrollBarWidth();
77 CFX_RectF rtMonthCal = m_pMonthCal->GetAutosizedWidgetRect();
78 CFX_RectF rtPopUp(rtMonthCal.left, rtMonthCal.top + kDateTimePickerHeight,
79 rtMonthCal.width, rtMonthCal.height);
80 m_pMonthCal->SetWidgetRect(rtPopUp);
81 m_pMonthCal->Update();
82}
83
84FWL_WidgetHit CFWL_DateTimePicker::HitTest(const CFX_PointF& point) {
86 if (rect.Contains(point))
88 if (NeedsToShowButton())
89 rect.width += m_fBtn;
90 if (rect.Contains(point))
93 if (m_pMonthCal->GetWidgetRect().Contains(point))
95 }
97}
98
99void CFWL_DateTimePicker::DrawWidget(CFGAS_GEGraphics* pGraphics,
100 const CFX_Matrix& matrix) {
101 if (!pGraphics)
102 return;
103
104 if (HasBorder())
105 DrawBorder(pGraphics, CFWL_ThemePart::Part::kBorder, matrix);
106
107 if (!m_BtnRect.IsEmpty())
108 DrawDropDownButton(pGraphics, matrix);
109
110 if (m_pEdit) {
111 CFX_RectF rtEdit = m_pEdit->GetWidgetRect();
112 CFX_Matrix mt(1, 0, 0, 1, rtEdit.left, rtEdit.top);
113 mt.Concat(matrix);
114 m_pEdit->DrawWidget(pGraphics, mt);
115 }
117 return;
118
119 CFX_RectF rtMonth = m_pMonthCal->GetWidgetRect();
120 CFX_Matrix mt(1, 0, 0, 1, rtMonth.left, rtMonth.top);
121 mt.Concat(matrix);
122 m_pMonthCal->DrawWidget(pGraphics, mt);
123}
124
125void CFWL_DateTimePicker::GetCurSel(int32_t& iYear,
126 int32_t& iMonth,
127 int32_t& iDay) {
128 iYear = m_iYear;
129 iMonth = m_iMonth;
130 iDay = m_iDay;
131}
132
133void CFWL_DateTimePicker::SetCurSel(int32_t iYear,
134 int32_t iMonth,
135 int32_t iDay) {
136 if (iYear <= 0 || iYear >= 3000)
137 return;
138 if (iMonth <= 0 || iMonth >= 13)
139 return;
140 if (iDay <= 0 || iDay >= 32)
141 return;
142
143 m_iYear = iYear;
144 m_iMonth = iMonth;
145 m_iDay = iDay;
146 m_pMonthCal->SetSelect(iYear, iMonth, iDay);
147}
148
149void CFWL_DateTimePicker::SetEditText(const WideString& wsText) {
150 if (!m_pEdit)
151 return;
152
153 m_pEdit->SetText(wsText);
154 RepaintRect(m_ClientRect);
155
157 DispatchEvent(&ev);
158}
159
160WideString CFWL_DateTimePicker::GetEditText() const {
161 return m_pEdit ? m_pEdit->GetText() : WideString();
162}
163
164size_t CFWL_DateTimePicker::GetEditTextLength() const {
165 return m_pEdit ? m_pEdit->GetTextLength() : 0;
166}
167
168CFX_RectF CFWL_DateTimePicker::GetBBox() const {
169 CFX_RectF rect = m_WidgetRect;
170 if (NeedsToShowButton())
171 rect.width += m_fBtn;
173 return rect;
174
175 CFX_RectF rtMonth = m_pMonthCal->GetWidgetRect();
177 rect.Union(rtMonth);
178 return rect;
179}
180
181void CFWL_DateTimePicker::ModifyEditStyleExts(uint32_t dwStyleExtsAdded,
182 uint32_t dwStyleExtsRemoved) {
183 m_pEdit->ModifyStyleExts(dwStyleExtsAdded, dwStyleExtsRemoved);
184}
185
186void CFWL_DateTimePicker::DrawDropDownButton(CFGAS_GEGraphics* pGraphics,
187 const CFX_Matrix& mtMatrix) {
188 CFWL_ThemeBackground param(CFWL_ThemePart::Part::kDropDownButton, this,
189 pGraphics);
190 param.m_dwStates = m_iBtnState;
191 param.m_PartRect = m_BtnRect;
192 param.m_matrix = mtMatrix;
193 GetThemeProvider()->DrawBackground(param);
194}
195
196WideString CFWL_DateTimePicker::FormatDateString(int32_t iYear,
197 int32_t iMonth,
198 int32_t iDay) {
200 return WideString::Format(L"%d-%d-%d", iYear, iMonth, iDay);
201
202 return WideString::Format(L"%d Year %d Month %d Day", iYear, iMonth, iDay);
203}
204
205void CFWL_DateTimePicker::ShowMonthCalendar() {
207 return;
208
209 CFX_RectF rtMonthCal = m_pMonthCal->GetAutosizedWidgetRect();
210 float fPopupMin = rtMonthCal.height;
211 float fPopupMax = rtMonthCal.height;
212 CFX_RectF rtAnchor = m_WidgetRect;
213 rtAnchor.width = rtMonthCal.width;
214 rtMonthCal.left = m_ClientRect.left;
215 rtMonthCal.top = rtAnchor.Height();
216 GetPopupPos(fPopupMin, fPopupMax, rtAnchor, &rtMonthCal);
217 m_pMonthCal->SetWidgetRect(rtMonthCal);
218 if (m_iYear > 0 && m_iMonth > 0 && m_iDay > 0)
219 m_pMonthCal->SetSelect(m_iYear, m_iMonth, m_iDay);
220 m_pMonthCal->Update();
221 m_pMonthCal->RemoveStates(FWL_STATE_WGT_Invisible);
222
223 CFWL_MessageSetFocus msg(m_pMonthCal);
224 m_pEdit->GetDelegate()->OnProcessMessage(&msg);
225 RepaintInflatedMonthCalRect();
226}
227
228void CFWL_DateTimePicker::HideMonthCalendar() {
230 return;
231
232 m_pMonthCal->SetStates(FWL_STATE_WGT_Invisible);
233 RepaintInflatedMonthCalRect();
234}
235
236void CFWL_DateTimePicker::RepaintInflatedMonthCalRect() {
238 CFX_RectF rtCal = m_pMonthCal->GetWidgetRect();
239 rtInvalidate.Union(rtCal);
240 rtInvalidate.Inflate(2, 2);
241 RepaintRect(rtInvalidate);
242}
243
244bool CFWL_DateTimePicker::IsMonthCalendarVisible() const {
245 return m_pMonthCal && m_pMonthCal->IsVisible();
246}
247
248void CFWL_DateTimePicker::ResetEditAlignment() {
249 if (!m_pEdit)
250 return;
251
252 uint32_t dwAdd = 0;
256 break;
257 }
259 dwAdd |= FWL_STYLEEXT_EDT_HFar;
260 break;
261 }
262 default: {
263 dwAdd |= FWL_STYLEEXT_EDT_HNear;
264 break;
265 }
266 }
270 break;
271 }
273 dwAdd |= FWL_STYLEEXT_EDT_VFar;
274 break;
275 }
276 default: {
277 dwAdd |= FWL_STYLEEXT_EDT_VNear;
278 break;
279 }
280 }
283
284 m_pEdit->ModifyStyleExts(dwAdd, FWL_STYLEEXT_EDT_HAlignMask |
287}
288
289void CFWL_DateTimePicker::ProcessSelChanged(int32_t iYear,
290 int32_t iMonth,
291 int32_t iDay) {
292 m_iYear = iYear;
293 m_iMonth = iMonth;
294 m_iDay = iDay;
295 m_pEdit->SetText(FormatDateString(m_iYear, m_iMonth, m_iDay));
296 m_pEdit->Update();
297 RepaintRect(m_ClientRect);
298
299 CFWL_EventSelectChanged ev(this, m_iYear, m_iMonth, m_iDay);
300 DispatchEvent(&ev);
301}
302
303bool CFWL_DateTimePicker::NeedsToShowButton() const {
304 return m_Properties.m_dwStates & FWL_STATE_WGT_Focused ||
305 m_pMonthCal->GetStates() & FWL_STATE_WGT_Focused ||
306 m_pEdit->GetStates() & FWL_STATE_WGT_Focused;
307}
308
309void CFWL_DateTimePicker::OnProcessMessage(CFWL_Message* pMessage) {
310 switch (pMessage->GetType()) {
312 OnFocusGained(pMessage);
313 break;
315 OnFocusLost(pMessage);
316 break;
318 CFWL_MessageMouse* pMouse = static_cast<CFWL_MessageMouse*>(pMessage);
319 switch (pMouse->m_dwCmd) {
320 case CFWL_MessageMouse::MouseCommand::kLeftButtonDown:
321 OnLButtonDown(pMouse);
322 break;
323 case CFWL_MessageMouse::MouseCommand::kLeftButtonUp:
324 OnLButtonUp(pMouse);
325 break;
326 case CFWL_MessageMouse::MouseCommand::kMove:
327 OnMouseMove(pMouse);
328 break;
329 case CFWL_MessageMouse::MouseCommand::kLeave:
330 OnMouseLeave(pMouse);
331 break;
332 default:
333 break;
334 }
335 break;
336 }
338 if (m_pEdit->GetStates() & FWL_STATE_WGT_Focused) {
339 m_pEdit->GetDelegate()->OnProcessMessage(pMessage);
340 return;
341 }
342 break;
343 }
344 default:
345 break;
346 }
347 // Dst target could be |this|, continue only if not destroyed by above.
348 if (pMessage->GetDstTarget())
350}
351
352void CFWL_DateTimePicker::OnDrawWidget(CFGAS_GEGraphics* pGraphics,
353 const CFX_Matrix& matrix) {
354 DrawWidget(pGraphics, matrix);
355}
356
357void CFWL_DateTimePicker::OnFocusGained(CFWL_Message* pMsg) {
359 if (m_pEdit && !(m_pEdit->GetStyleExts() & FWL_STYLEEXT_EDT_ReadOnly)) {
360 m_BtnRect =
362 }
363 CFX_RectF rtInvalidate(m_BtnRect);
364 pMsg->SetDstTarget(m_pEdit);
365 m_pEdit->GetDelegate()->OnProcessMessage(pMsg);
366 rtInvalidate.Inflate(2, 2);
367 RepaintRect(rtInvalidate);
368}
369
370void CFWL_DateTimePicker::OnFocusLost(CFWL_Message* pMsg) {
371 CFX_RectF rtInvalidate(m_BtnRect);
373 m_BtnRect = CFX_RectF();
375 if (m_pEdit->GetStates() & FWL_STATE_WGT_Focused)
376 m_pEdit->GetDelegate()->OnProcessMessage(pMsg);
377 rtInvalidate.Inflate(2, 2);
378 RepaintRect(rtInvalidate);
379}
380
381void CFWL_DateTimePicker::OnLButtonDown(CFWL_MessageMouse* pMsg) {
382 if (!pMsg)
383 return;
384 if (!m_BtnRect.Contains(pMsg->m_pos))
385 return;
386
389 return;
390 }
392 m_bLBtnDown = true;
393 RepaintRect(m_ClientRect);
394}
395
396void CFWL_DateTimePicker::OnLButtonUp(CFWL_MessageMouse* pMsg) {
397 if (!pMsg)
398 return;
399
400 m_bLBtnDown = false;
401 if (m_BtnRect.Contains(pMsg->m_pos))
402 m_iBtnState = CFWL_PartState::kHovered;
403 else
404 m_iBtnState = CFWL_PartState::kNormal;
405 RepaintRect(m_BtnRect);
406}
407
408void CFWL_DateTimePicker::OnMouseMove(CFWL_MessageMouse* pMsg) {
409 if (!m_BtnRect.Contains(pMsg->m_pos))
410 m_iBtnState = CFWL_PartState::kNormal;
411 RepaintRect(m_BtnRect);
412}
413
414void CFWL_DateTimePicker::OnMouseLeave(CFWL_MessageMouse* pMsg) {
415 if (!pMsg)
416 return;
417 m_iBtnState = CFWL_PartState::kNormal;
418 RepaintRect(m_BtnRect);
419}
420
421void CFWL_DateTimePicker::GetPopupPos(float fMinHeight,
422 float fMaxHeight,
423 const CFX_RectF& rtAnchor,
424 CFX_RectF* pPopupRect) {
425 GetWidgetMgr()->GetAdapterPopupPos(this, fMinHeight, fMaxHeight, rtAnchor,
426 pPopupRect);
427}
428
429void CFWL_DateTimePicker::ClearText() {
430 m_pEdit->ClearText();
431}
432
433void CFWL_DateTimePicker::SelectAll() {
434 m_pEdit->SelectAll();
435}
436
437void CFWL_DateTimePicker::ClearSelection() {
438 m_pEdit->ClearSelection();
439}
440
441std::optional<WideString> CFWL_DateTimePicker::Copy() {
442 return m_pEdit->Copy();
443}
444
445std::optional<WideString> CFWL_DateTimePicker::Cut() {
446 return m_pEdit->Cut();
447}
448
449bool CFWL_DateTimePicker::Paste(const WideString& wsPaste) {
450 return m_pEdit->Paste(wsPaste);
451}
452
453bool CFWL_DateTimePicker::Undo() {
454 return m_pEdit->Undo();
455}
456
457bool CFWL_DateTimePicker::Redo() {
458 return m_pEdit->Redo();
459}
460
461bool CFWL_DateTimePicker::CanUndo() {
462 return m_pEdit->CanUndo();
463}
464
465bool CFWL_DateTimePicker::CanRedo() {
466 return m_pEdit->CanRedo();
467}
468
469} // namespace pdfium
#define FWL_STYLEEXT_DTP_EditVCenter
#define FWL_STYLEEXT_DTP_EditJustified
#define FWL_STYLEEXT_DTP_ShortDateFormat
#define FWL_STYLEEXT_DTP_EditHCenter
#define FWL_STYLEEXT_DTP_EditVFar
#define FWL_STYLEEXT_DTP_EditHAlignMask
#define FWL_STYLEEXT_DTP_EditVAlignMask
#define FWL_STYLEEXT_DTP_EditHFar
#define FWL_STYLEEXT_EDT_HFar
Definition cfwl_edit.h:34
#define FWL_STYLEEXT_EDT_VAlignMask
Definition cfwl_edit.h:40
#define FWL_STYLEEXT_EDT_HAlignModeMask
Definition cfwl_edit.h:41
#define FWL_STYLEEXT_EDT_HAlignMask
Definition cfwl_edit.h:39
#define FWL_STYLEEXT_EDT_VNear
Definition cfwl_edit.h:35
#define FWL_STYLEEXT_EDT_VCenter
Definition cfwl_edit.h:36
#define FWL_STYLEEXT_EDT_ReadOnly
Definition cfwl_edit.h:23
#define FWL_STYLEEXT_EDT_VFar
Definition cfwl_edit.h:37
#define FWL_STYLEEXT_EDT_HNear
Definition cfwl_edit.h:32
#define FWL_STYLEEXT_EDT_Justified
Definition cfwl_edit.h:38
#define FWL_STYLEEXT_EDT_HCenter
Definition cfwl_edit.h:33
#define FWL_STYLE_WGT_Popup
Definition cfwl_widget.h:35
#define FWL_STATE_WGT_Invisible
Definition cfwl_widget.h:45
#define FWL_STATE_WGT_Focused
Definition cfwl_widget.h:44
#define FWL_STYLE_WGT_Border
Definition cfwl_widget.h:38
CFX_Matrix & operator=(const CFX_Matrix &other)=default
constexpr CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void Concat(const CFX_Matrix &right)
CFX_RectF(const CFX_RectF &other)=default
void Offset(float dx, float dy)
constexpr CFX_RectF()=default
bool IsEmpty() const
void Inflate(float x, float y)
CFX_RectF & operator=(const CFX_RectF &other)=default
float Height() const
bool Contains(const PointType &p) const
constexpr CFX_RectF(float dst_left, float dst_top, float dst_width, float dst_height)
void Union(const CFX_RectF &rt)
static WideString Format(const wchar_t *pFormat,...)
std::optional< WideString > Cut()
void ModifyEditStyleExts(uint32_t dwStyleExtsAdded, uint32_t dwStyleExtsRemoved)
void SetEditText(const WideString &wsText)
void GetCurSel(int32_t &iYear, int32_t &iMonth, int32_t &iDay)
void DrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
void ProcessSelChanged(int32_t iYear, int32_t iMonth, int32_t iDay)
void Trace(cppgc::Visitor *visitor) const override
void OnDrawWidget(CFGAS_GEGraphics *pGraphics, const CFX_Matrix &matrix) override
FWL_WidgetHit HitTest(const CFX_PointF &point) override
FWL_Type GetClassID() const override
void SetCurSel(int32_t iYear, int32_t iMonth, int32_t iDay)
bool Paste(const WideString &wsPaste)
std::optional< WideString > Copy()
void OnProcessMessage(CFWL_Message *pMessage) override
CFWL_Event(Type type)
Type GetType() const
CFWL_Widget * GetDstTarget() const
void OnProcessMessage(CFWL_Message *pMessage) override
void RepaintRect(const CFX_RectF &pRect)
bool HasBorder() const
void DispatchEvent(CFWL_Event *pEvent)
CFWL_Widget(CFWL_App *app, const Properties &properties, CFWL_Widget *pOuter)
void DrawBorder(CFGAS_GEGraphics *pGraphics, CFWL_ThemePart::Part iPartBorder, const CFX_Matrix &pMatrix)
virtual void PreFinalize()
virtual CFX_RectF GetClientRect()
CFWL_App * GetFWLApp() const
IFWL_ThemeProvider * GetThemeProvider() const
bool IsLocked() const
CFWL_WidgetMgr * GetWidgetMgr() const
Properties m_Properties
CFX_PTemplate< float > CFX_PointF
fxcrt::WideString WideString
Definition widestring.h:207