blob: 2863a56001727318221c49579ecd51f0441a5107 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairedbb3192016-03-21 09:08:24 -04007#include "fpdfsdk/formfiller/cffl_checkbox.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008
dsinclair735606d2016-10-05 15:47:02 -07009#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070010#include "fpdfsdk/cpdfsdk_widget.h"
Dan Sinclairedbb3192016-03-21 09:08:24 -040011#include "fpdfsdk/formfiller/cffl_formfiller.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040012#include "fpdfsdk/pwl/cpwl_special_button.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080013#include "public/fpdf_fwlevent.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
dsinclair735606d2016-10-05 15:47:02 -070015CFFL_CheckBox::CFFL_CheckBox(CPDFSDK_FormFillEnvironment* pApp,
16 CPDFSDK_Widget* pWidget)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017 : CFFL_Button(pApp, pWidget) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019CFFL_CheckBox::~CFFL_CheckBox() {}
20
Tom Sepezbf157302017-09-15 13:26:32 -070021CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const CPWL_Wnd::CreateParams& cp) {
22 auto* pWnd = new CPWL_CheckBox();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 pWnd->Create(cp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 pWnd->SetCheck(m_pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 return pWnd;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026}
27
tsepez4cf55152016-11-02 14:37:54 -070028bool CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot,
29 uint32_t nKeyCode,
30 uint32_t nFlags) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 switch (nKeyCode) {
32 case FWL_VKEY_Return:
33 case FWL_VKEY_Space:
tsepez4cf55152016-11-02 14:37:54 -070034 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 default:
36 return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);
37 }
38}
tsepez4cf55152016-11-02 14:37:54 -070039bool CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot,
40 uint32_t nChar,
41 uint32_t nFlags) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 switch (nChar) {
43 case FWL_VKEY_Return:
44 case FWL_VKEY_Space: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
Lei Zhang96660d62015-12-14 18:27:25 -080046 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047
Tom Sepezd0409af2017-05-25 15:53:57 -070048 CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget.Get());
Dan Sinclairfecd7502017-07-06 11:35:04 -040049 if (m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
50 &pObserved, pPageView, nFlags)) {
51 if (!pObserved)
52 m_pWidget = nullptr;
53 return true;
54 }
tsepezf8074ce2016-09-27 14:29:57 -070055 if (!pObserved) {
56 m_pWidget = nullptr;
tsepez4cf55152016-11-02 14:37:54 -070057 return true;
tsepezf8074ce2016-09-27 14:29:57 -070058 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059
60 CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
Claudio DeSouzaf0947bd2017-09-14 21:59:05 +010061
Lei Zhang77e3fc52017-06-15 12:37:33 -070062 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
Claudio DeSouzaf0947bd2017-09-14 21:59:05 +010063 if (pWnd) {
64 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
65 pWnd->SetCheck(!pWidget->IsChecked());
66 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067
Lei Zhang6a3fc452017-06-13 14:04:02 -070068 return CommitData(pPageView, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 }
70 default:
71 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
72 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073}
74
tsepez4cf55152016-11-02 14:37:54 -070075bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
76 CPDFSDK_Annot* pAnnot,
77 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -050078 const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Lei Zhang77e3fc52017-06-15 12:37:33 -070081 if (!IsValid())
82 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070083
Lei Zhang77e3fc52017-06-15 12:37:33 -070084 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
85 if (pWnd) {
86 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
87 pWnd->SetCheck(!pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 }
89
Lei Zhang77e3fc52017-06-15 12:37:33 -070090 return CommitData(pPageView, nFlags);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
tsepez4cf55152016-11-02 14:37:54 -070093bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
Lei Zhang77e3fc52017-06-15 12:37:33 -070094 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
Lei Zhang96660d62015-12-14 18:27:25 -080095 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
Lei Zhang77e3fc52017-06-15 12:37:33 -070099 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
100 if (!pWnd)
101 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102
Lei Zhang77e3fc52017-06-15 12:37:33 -0700103 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;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 }
111 }
112 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 }
Lei Zhang77e3fc52017-06-15 12:37:33 -0700114
115 m_pWidget->SetCheck(bNewChecked, false);
116 m_pWidget->UpdateField();
117 SetChangeMark();
118}
119
120CPWL_CheckBox* CFFL_CheckBox::GetCheckBox(CPDFSDK_PageView* pPageView,
121 bool bNew) {
122 return static_cast<CPWL_CheckBox*>(GetPDFWindow(pPageView, bNew));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}