• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fpdfsdk/formfiller/cffl_checkbox.h"
8 
9 #include <utility>
10 
11 #include "constants/ascii.h"
12 #include "core/fpdfdoc/cpdf_formcontrol.h"
13 #include "fpdfsdk/cpdfsdk_widget.h"
14 #include "fpdfsdk/formfiller/cffl_formfield.h"
15 #include "fpdfsdk/pwl/cpwl_special_button.h"
16 #include "public/fpdf_fwlevent.h"
17 #include "third_party/base/check.h"
18 
CFFL_CheckBox(CFFL_InteractiveFormFiller * pFormFiller,CPDFSDK_Widget * pWidget)19 CFFL_CheckBox::CFFL_CheckBox(CFFL_InteractiveFormFiller* pFormFiller,
20                              CPDFSDK_Widget* pWidget)
21     : CFFL_Button(pFormFiller, pWidget) {}
22 
23 CFFL_CheckBox::~CFFL_CheckBox() = default;
24 
NewPWLWindow(const CPWL_Wnd::CreateParams & cp,std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData)25 std::unique_ptr<CPWL_Wnd> CFFL_CheckBox::NewPWLWindow(
26     const CPWL_Wnd::CreateParams& cp,
27     std::unique_ptr<IPWL_FillerNotify::PerWindowData> pAttachedData) {
28   auto pWnd = std::make_unique<CPWL_CheckBox>(cp, std::move(pAttachedData));
29   pWnd->Realize();
30   pWnd->SetCheck(m_pWidget->IsChecked());
31   return std::move(pWnd);
32 }
33 
OnKeyDown(FWL_VKEYCODE nKeyCode,Mask<FWL_EVENTFLAG> nFlags)34 bool CFFL_CheckBox::OnKeyDown(FWL_VKEYCODE nKeyCode,
35                               Mask<FWL_EVENTFLAG> nFlags) {
36   switch (nKeyCode) {
37     case FWL_VKEY_Return:
38     case FWL_VKEY_Space:
39       return true;
40     default:
41       return CFFL_FormField::OnKeyDown(nKeyCode, nFlags);
42   }
43 }
44 
OnChar(CPDFSDK_Widget * pWidget,uint32_t nChar,Mask<FWL_EVENTFLAG> nFlags)45 bool CFFL_CheckBox::OnChar(CPDFSDK_Widget* pWidget,
46                            uint32_t nChar,
47                            Mask<FWL_EVENTFLAG> nFlags) {
48   switch (nChar) {
49     case pdfium::ascii::kReturn:
50     case pdfium::ascii::kSpace: {
51       CPDFSDK_PageView* pPageView = pWidget->GetPageView();
52       DCHECK(pPageView);
53 
54       ObservedPtr<CPDFSDK_Widget> pObserved(m_pWidget);
55       if (m_pFormFiller->OnButtonUp(pObserved, pPageView, nFlags)) {
56         if (!pObserved)
57           m_pWidget = nullptr;
58         return true;
59       }
60       if (!pObserved) {
61         m_pWidget = nullptr;
62         return true;
63       }
64 
65       CFFL_FormField::OnChar(pWidget, nChar, nFlags);
66 
67       CPWL_CheckBox* pWnd = CreateOrUpdatePWLCheckBox(pPageView);
68       if (pWnd && !pWnd->IsReadOnly()) {
69         ObservedPtr<CPWL_CheckBox> pObservedBox(pWnd);
70         const bool is_checked = pWidget->IsChecked();
71         if (pObservedBox) {
72           pObservedBox->SetCheck(!is_checked);
73         }
74       }
75       return CommitData(pPageView, nFlags);
76     }
77     default:
78       return CFFL_FormField::OnChar(pWidget, nChar, nFlags);
79   }
80 }
81 
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Widget * pWidget,Mask<FWL_EVENTFLAG> nFlags,const CFX_PointF & point)82 bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
83                                 CPDFSDK_Widget* pWidget,
84                                 Mask<FWL_EVENTFLAG> nFlags,
85                                 const CFX_PointF& point) {
86   CFFL_Button::OnLButtonUp(pPageView, pWidget, nFlags, point);
87   if (!IsValid()) {
88     return true;
89   }
90   CPWL_CheckBox* pWnd = CreateOrUpdatePWLCheckBox(pPageView);
91   if (pWnd) {
92     ObservedPtr<CPWL_CheckBox> pObservedBox(pWnd);
93     const bool is_checked = pWidget->IsChecked();
94     if (pObservedBox) {
95       pObservedBox->SetCheck(!is_checked);
96     }
97   }
98   return CommitData(pPageView, nFlags);
99 }
100 
IsDataChanged(const CPDFSDK_PageView * pPageView)101 bool CFFL_CheckBox::IsDataChanged(const CPDFSDK_PageView* pPageView) {
102   CPWL_CheckBox* pWnd = GetPWLCheckBox(pPageView);
103   return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
104 }
105 
SaveData(const CPDFSDK_PageView * pPageView)106 void CFFL_CheckBox::SaveData(const CPDFSDK_PageView* pPageView) {
107   CPWL_CheckBox* pWnd = GetPWLCheckBox(pPageView);
108   if (!pWnd)
109     return;
110 
111   bool bNewChecked = pWnd->IsChecked();
112   ObservedPtr<CPDFSDK_Widget> observed_widget(m_pWidget);
113   ObservedPtr<CFFL_CheckBox> observed_this(this);
114   m_pWidget->SetCheck(bNewChecked);
115   if (!observed_widget)
116     return;
117 
118   m_pWidget->UpdateField();
119   if (!observed_widget || !observed_this)
120     return;
121 
122   SetChangeMark();
123 }
124 
GetPWLCheckBox(const CPDFSDK_PageView * pPageView) const125 CPWL_CheckBox* CFFL_CheckBox::GetPWLCheckBox(
126     const CPDFSDK_PageView* pPageView) const {
127   return static_cast<CPWL_CheckBox*>(GetPWLWindow(pPageView));
128 }
129 
CreateOrUpdatePWLCheckBox(const CPDFSDK_PageView * pPageView)130 CPWL_CheckBox* CFFL_CheckBox::CreateOrUpdatePWLCheckBox(
131     const CPDFSDK_PageView* pPageView) {
132   return static_cast<CPWL_CheckBox*>(CreateOrUpdatePWLWindow(pPageView));
133 }
134