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