• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fpdfsdk/formfiller/cffl_checkbox.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfdoc/cpdf_formcontrol.h"
12 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
13 #include "fpdfsdk/cpdfsdk_widget.h"
14 #include "fpdfsdk/formfiller/cffl_formfiller.h"
15 #include "fpdfsdk/pwl/cpwl_special_button.h"
16 #include "public/fpdf_fwlevent.h"
17 #include "third_party/base/ptr_util.h"
18 
CFFL_CheckBox(CPDFSDK_FormFillEnvironment * pApp,CPDFSDK_Widget * pWidget)19 CFFL_CheckBox::CFFL_CheckBox(CPDFSDK_FormFillEnvironment* pApp,
20                              CPDFSDK_Widget* pWidget)
21     : CFFL_Button(pApp, pWidget) {}
22 
~CFFL_CheckBox()23 CFFL_CheckBox::~CFFL_CheckBox() {}
24 
NewPWLWindow(const CPWL_Wnd::CreateParams & cp,std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData)25 std::unique_ptr<CPWL_Wnd> CFFL_CheckBox::NewPWLWindow(
26     const CPWL_Wnd::CreateParams& cp,
27     std::unique_ptr<IPWL_SystemHandler::PerWindowData> pAttachedData) {
28   auto pWnd = pdfium::MakeUnique<CPWL_CheckBox>(cp, std::move(pAttachedData));
29   pWnd->Realize();
30   pWnd->SetCheck(m_pWidget->IsChecked());
31   return std::move(pWnd);
32 }
33 
OnKeyDown(uint32_t nKeyCode,uint32_t nFlags)34 bool CFFL_CheckBox::OnKeyDown(uint32_t nKeyCode, uint32_t nFlags) {
35   switch (nKeyCode) {
36     case FWL_VKEY_Return:
37     case FWL_VKEY_Space:
38       return true;
39     default:
40       return CFFL_FormFiller::OnKeyDown(nKeyCode, nFlags);
41   }
42 }
OnChar(CPDFSDK_Annot * pAnnot,uint32_t nChar,uint32_t nFlags)43 bool CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot,
44                            uint32_t nChar,
45                            uint32_t nFlags) {
46   switch (nChar) {
47     case FWL_VKEY_Return:
48     case FWL_VKEY_Space: {
49       CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
50       ASSERT(pPageView);
51 
52       ObservedPtr<CPDFSDK_Annot> pObserved(m_pWidget.Get());
53       if (m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
54               &pObserved, pPageView, nFlags)) {
55         if (!pObserved)
56           m_pWidget = nullptr;
57         return true;
58       }
59       if (!pObserved) {
60         m_pWidget = nullptr;
61         return true;
62       }
63 
64       CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
65 
66       CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
67       if (pWnd) {
68         CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
69         pWnd->SetCheck(!pWidget->IsChecked());
70       }
71 
72       return CommitData(pPageView, nFlags);
73     }
74     default:
75       return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
76   }
77 }
78 
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,uint32_t nFlags,const CFX_PointF & point)79 bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
80                                 CPDFSDK_Annot* pAnnot,
81                                 uint32_t nFlags,
82                                 const CFX_PointF& point) {
83   CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
84 
85   if (!IsValid())
86     return true;
87 
88   CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
89   if (pWnd) {
90     CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
91     pWnd->SetCheck(!pWidget->IsChecked());
92   }
93 
94   return CommitData(pPageView, nFlags);
95 }
96 
IsDataChanged(CPDFSDK_PageView * pPageView)97 bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
98   CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
99   return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
100 }
101 
SaveData(CPDFSDK_PageView * pPageView)102 void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
103   CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
104   if (!pWnd)
105     return;
106 
107   bool bNewChecked = pWnd->IsChecked();
108   if (bNewChecked) {
109     CPDF_FormField* pField = m_pWidget->GetFormField();
110     for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
111       if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
112         if (pCtrl->IsChecked()) {
113           break;
114         }
115       }
116     }
117   }
118   ObservedPtr<CPDFSDK_Widget> observed_widget(m_pWidget.Get());
119   ObservedPtr<CFFL_CheckBox> observed_this(this);
120   m_pWidget->SetCheck(bNewChecked, NotificationOption::kDoNotNotify);
121   if (!observed_widget)
122     return;
123 
124   m_pWidget->UpdateField();
125   if (!observed_widget || !observed_this)
126     return;
127 
128   SetChangeMark();
129 }
130 
GetCheckBox(CPDFSDK_PageView * pPageView,bool bNew)131 CPWL_CheckBox* CFFL_CheckBox::GetCheckBox(CPDFSDK_PageView* pPageView,
132                                           bool bNew) {
133   return static_cast<CPWL_CheckBox*>(GetPWLWindow(pPageView, bNew));
134 }
135