Home | History | Annotate | Download | only in fxfa
      1 // Copyright 2017 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/fxfa/cxfa_ffnumericedit.h"
      8 
      9 #include <utility>
     10 
     11 #include "xfa/fwl/cfwl_edit.h"
     12 #include "xfa/fwl/cfwl_eventvalidate.h"
     13 #include "xfa/fwl/cfwl_notedriver.h"
     14 #include "xfa/fxfa/cxfa_ffdoc.h"
     15 #include "xfa/fxfa/parser/cxfa_localevalue.h"
     16 #include "xfa/fxfa/parser/cxfa_node.h"
     17 #include "xfa/fxfa/parser/xfa_utils.h"
     18 
     19 CXFA_FFNumericEdit::CXFA_FFNumericEdit(CXFA_Node* pNode)
     20     : CXFA_FFTextEdit(pNode) {}
     21 
     22 CXFA_FFNumericEdit::~CXFA_FFNumericEdit() {}
     23 
     24 bool CXFA_FFNumericEdit::LoadWidget() {
     25   auto pNewEdit = pdfium::MakeUnique<CFWL_Edit>(
     26       GetFWLApp(), pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr);
     27   CFWL_Edit* pWidget = pNewEdit.get();
     28   m_pNormalWidget = std::move(pNewEdit);
     29   m_pNormalWidget->SetLayoutItem(this);
     30 
     31   CFWL_NoteDriver* pNoteDriver =
     32       m_pNormalWidget->GetOwnerApp()->GetNoteDriver();
     33   pNoteDriver->RegisterEventTarget(m_pNormalWidget.get(),
     34                                    m_pNormalWidget.get());
     35   m_pOldDelegate = m_pNormalWidget->GetDelegate();
     36   m_pNormalWidget->SetDelegate(this);
     37   m_pNormalWidget->LockUpdate();
     38 
     39   pWidget->SetText(m_pNode->GetWidgetAcc()->GetValue(XFA_VALUEPICTURE_Display));
     40   UpdateWidgetProperty();
     41   m_pNormalWidget->UnlockUpdate();
     42   return CXFA_FFField::LoadWidget();
     43 }
     44 
     45 void CXFA_FFNumericEdit::UpdateWidgetProperty() {
     46   CFWL_Edit* pWidget = static_cast<CFWL_Edit*>(m_pNormalWidget.get());
     47   if (!pWidget)
     48     return;
     49 
     50   uint32_t dwExtendedStyle =
     51       FWL_STYLEEXT_EDT_ShowScrollbarFocus | FWL_STYLEEXT_EDT_OuterScrollbar |
     52       FWL_STYLEEXT_EDT_Validate | FWL_STYLEEXT_EDT_Number;
     53   dwExtendedStyle |= UpdateUIProperty();
     54   if (!m_pNode->GetWidgetAcc()->IsHorizontalScrollPolicyOff())
     55     dwExtendedStyle |= FWL_STYLEEXT_EDT_AutoHScroll;
     56 
     57   Optional<int32_t> numCells = m_pNode->GetWidgetAcc()->GetNumberOfCells();
     58   if (numCells && *numCells > 0) {
     59     dwExtendedStyle |= FWL_STYLEEXT_EDT_CombText;
     60     pWidget->SetLimit(*numCells);
     61   }
     62   dwExtendedStyle |= GetAlignment();
     63   if (!m_pNode->IsOpenAccess() || !GetDoc()->GetXFADoc()->IsInteractive())
     64     dwExtendedStyle |= FWL_STYLEEXT_EDT_ReadOnly;
     65 
     66   m_pNormalWidget->ModifyStylesEx(dwExtendedStyle, 0xFFFFFFFF);
     67 }
     68 
     69 void CXFA_FFNumericEdit::OnProcessEvent(CFWL_Event* pEvent) {
     70   if (pEvent->GetType() == CFWL_Event::Type::Validate) {
     71     CFWL_EventValidate* event = static_cast<CFWL_EventValidate*>(pEvent);
     72     event->bValidate = OnValidate(m_pNormalWidget.get(), event->wsInsert);
     73     return;
     74   }
     75   CXFA_FFTextEdit::OnProcessEvent(pEvent);
     76 }
     77 
     78 bool CXFA_FFNumericEdit::OnValidate(CFWL_Widget* pWidget, WideString& wsText) {
     79   WideString wsPattern =
     80       m_pNode->GetWidgetAcc()->GetPictureContent(XFA_VALUEPICTURE_Edit);
     81   if (!wsPattern.IsEmpty())
     82     return true;
     83 
     84   WideString wsFormat;
     85   CXFA_LocaleValue widgetValue = XFA_GetLocaleValue(m_pNode.Get());
     86   widgetValue.GetNumericFormat(wsFormat,
     87                                m_pNode->GetWidgetAcc()->GetLeadDigits(),
     88                                m_pNode->GetWidgetAcc()->GetFracDigits());
     89   return widgetValue.ValidateNumericTemp(wsText, wsFormat,
     90                                          m_pNode->GetLocale());
     91 }
     92