xref: /aosp_15_r20/external/pdfium/xfa/fxfa/cxfa_ffnumericedit.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 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/fxfa/cxfa_ffnumericedit.h"
8 
9 #include "third_party/base/check.h"
10 #include "xfa/fwl/cfwl_edit.h"
11 #include "xfa/fwl/cfwl_eventvalidate.h"
12 #include "xfa/fwl/cfwl_notedriver.h"
13 #include "xfa/fxfa/cxfa_ffdoc.h"
14 #include "xfa/fxfa/parser/cxfa_localevalue.h"
15 #include "xfa/fxfa/parser/cxfa_node.h"
16 #include "xfa/fxfa/parser/xfa_utils.h"
17 
CXFA_FFNumericEdit(CXFA_Node * pNode)18 CXFA_FFNumericEdit::CXFA_FFNumericEdit(CXFA_Node* pNode)
19     : CXFA_FFTextEdit(pNode) {}
20 
21 CXFA_FFNumericEdit::~CXFA_FFNumericEdit() = default;
22 
LoadWidget()23 bool CXFA_FFNumericEdit::LoadWidget() {
24   DCHECK(!IsLoaded());
25 
26   CFWL_Edit* pWidget = cppgc::MakeGarbageCollected<CFWL_Edit>(
27       GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp(),
28       CFWL_Widget::Properties(), nullptr);
29   SetNormalWidget(pWidget);
30   pWidget->SetAdapterIface(this);
31 
32   CFWL_NoteDriver* pNoteDriver = pWidget->GetFWLApp()->GetNoteDriver();
33   pNoteDriver->RegisterEventTarget(pWidget, pWidget);
34   m_pOldDelegate = pWidget->GetDelegate();
35   pWidget->SetDelegate(this);
36 
37   {
38     CFWL_Widget::ScopedUpdateLock update_lock(pWidget);
39     pWidget->SetText(m_pNode->GetValue(XFA_ValuePicture::kDisplay));
40     UpdateWidgetProperty();
41   }
42 
43   return CXFA_FFField::LoadWidget();
44 }
45 
UpdateWidgetProperty()46 void CXFA_FFNumericEdit::UpdateWidgetProperty() {
47   CFWL_Edit* pWidget = static_cast<CFWL_Edit*>(GetNormalWidget());
48   if (!pWidget)
49     return;
50 
51   uint32_t dwExtendedStyle =
52       FWL_STYLEEXT_EDT_ShowScrollbarFocus | FWL_STYLEEXT_EDT_OuterScrollbar |
53       FWL_STYLEEXT_EDT_Validate | FWL_STYLEEXT_EDT_Number;
54   dwExtendedStyle |= UpdateUIProperty();
55   if (!m_pNode->IsHorizontalScrollPolicyOff())
56     dwExtendedStyle |= FWL_STYLEEXT_EDT_AutoHScroll;
57 
58   absl::optional<int32_t> numCells = m_pNode->GetNumberOfCells();
59   if (numCells.has_value() && numCells.value() > 0) {
60     dwExtendedStyle |= FWL_STYLEEXT_EDT_CombText;
61     pWidget->SetLimit(numCells.value());
62   }
63   dwExtendedStyle |= GetAlignment();
64   if (!m_pNode->IsOpenAccess() || !GetDoc()->GetXFADoc()->IsInteractive())
65     dwExtendedStyle |= FWL_STYLEEXT_EDT_ReadOnly;
66 
67   GetNormalWidget()->ModifyStyleExts(dwExtendedStyle, 0xFFFFFFFF);
68 }
69 
OnProcessEvent(CFWL_Event * pEvent)70 void CXFA_FFNumericEdit::OnProcessEvent(CFWL_Event* pEvent) {
71   if (pEvent->GetType() == CFWL_Event::Type::Validate) {
72     CFWL_EventValidate* event = static_cast<CFWL_EventValidate*>(pEvent);
73     event->SetValidate(OnValidate(GetNormalWidget(), event->GetInsert()));
74     return;
75   }
76   CXFA_FFTextEdit::OnProcessEvent(pEvent);
77 }
78 
OnValidate(CFWL_Widget * pWidget,const WideString & wsText)79 bool CXFA_FFNumericEdit::OnValidate(CFWL_Widget* pWidget,
80                                     const WideString& wsText) {
81   WideString wsPattern = m_pNode->GetPictureContent(XFA_ValuePicture::kEdit);
82   if (!wsPattern.IsEmpty())
83     return true;
84 
85   WideString wsFormat;
86   CXFA_LocaleValue widgetValue = XFA_GetLocaleValue(m_pNode.Get());
87   widgetValue.GetNumericFormat(wsFormat, m_pNode->GetLeadDigits(),
88                                m_pNode->GetFracDigits());
89   return widgetValue.ValidateNumericTemp(wsText, wsFormat,
90                                          m_pNode->GetLocale());
91 }
92