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/pdfwindow/PWL_SpecialButton.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 PWL_CREATEPARAM & cp,CPDFSDK_PageView * pPageView)21 CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp,
22 CPDFSDK_PageView* pPageView) {
23 CPWL_CheckBox* pWnd = new CPWL_CheckBox();
24 pWnd->Create(cp);
25 pWnd->SetCheck(m_pWidget->IsChecked());
26 return pWnd;
27 }
28
OnKeyDown(CPDFSDK_Annot * pAnnot,uint32_t nKeyCode,uint32_t nFlags)29 bool CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot,
30 uint32_t nKeyCode,
31 uint32_t nFlags) {
32 switch (nKeyCode) {
33 case FWL_VKEY_Return:
34 case FWL_VKEY_Space:
35 return true;
36 default:
37 return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);
38 }
39 }
OnChar(CPDFSDK_Annot * pAnnot,uint32_t nChar,uint32_t nFlags)40 bool CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot,
41 uint32_t nChar,
42 uint32_t nFlags) {
43 switch (nChar) {
44 case FWL_VKEY_Return:
45 case FWL_VKEY_Space: {
46 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
47 ASSERT(pPageView);
48
49 bool bReset = false;
50 bool bExit = false;
51 CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget);
52 m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
53 &pObserved, pPageView, bReset, bExit, nFlags);
54 if (!pObserved) {
55 m_pWidget = nullptr;
56 return true;
57 }
58 if (bReset || bExit)
59 return true;
60
61 CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
62 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true))
63 pWnd->SetCheck(!pWnd->IsChecked());
64
65 CommitData(pPageView, nFlags);
66 return true;
67 }
68 default:
69 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
70 }
71 }
72
OnLButtonUp(CPDFSDK_PageView * pPageView,CPDFSDK_Annot * pAnnot,uint32_t nFlags,const CFX_PointF & point)73 bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
74 CPDFSDK_Annot* pAnnot,
75 uint32_t nFlags,
76 const CFX_PointF& point) {
77 CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
78
79 if (IsValid()) {
80 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) {
81 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
82 pWnd->SetCheck(!pWidget->IsChecked());
83 }
84
85 if (!CommitData(pPageView, nFlags))
86 return false;
87 }
88
89 return true;
90 }
91
IsDataChanged(CPDFSDK_PageView * pPageView)92 bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
93 CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false);
94 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
95 }
96
SaveData(CPDFSDK_PageView * pPageView)97 void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
98 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false)) {
99 bool bNewChecked = pWnd->IsChecked();
100
101 if (bNewChecked) {
102 CPDF_FormField* pField = m_pWidget->GetFormField();
103 for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
104 if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
105 if (pCtrl->IsChecked()) {
106 break;
107 }
108 }
109 }
110 }
111
112 m_pWidget->SetCheck(bNewChecked, false);
113 m_pWidget->UpdateField();
114 SetChangeMark();
115 }
116 }
117