blob: 1a3ccca547b8235614744340c992c9a436f91bfa [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
Lei Zhangdd29c252017-06-01 23:55:51 -070021CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 CPWL_CheckBox* pWnd = new CPWL_CheckBox();
23 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);
Lei Zhang77e3fc52017-06-15 12:37:33 -070061 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
62 if (pWnd)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 pWnd->SetCheck(!pWnd->IsChecked());
64
Lei Zhang6a3fc452017-06-13 14:04:02 -070065 return CommitData(pPageView, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 }
67 default:
68 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
69 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070}
71
tsepez4cf55152016-11-02 14:37:54 -070072bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
73 CPDFSDK_Annot* pAnnot,
74 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -050075 const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Lei Zhang77e3fc52017-06-15 12:37:33 -070078 if (!IsValid())
79 return true;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070080
Lei Zhang77e3fc52017-06-15 12:37:33 -070081 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
82 if (pWnd) {
83 CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
84 pWnd->SetCheck(!pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 }
86
Lei Zhang77e3fc52017-06-15 12:37:33 -070087 return CommitData(pPageView, nFlags);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
tsepez4cf55152016-11-02 14:37:54 -070090bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
Lei Zhang77e3fc52017-06-15 12:37:33 -070091 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
Lei Zhang96660d62015-12-14 18:27:25 -080092 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
Lei Zhang77e3fc52017-06-15 12:37:33 -070096 CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
97 if (!pWnd)
98 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Lei Zhang77e3fc52017-06-15 12:37:33 -0700100 bool bNewChecked = pWnd->IsChecked();
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;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 }
108 }
109 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 }
Lei Zhang77e3fc52017-06-15 12:37:33 -0700111
112 m_pWidget->SetCheck(bNewChecked, false);
113 m_pWidget->UpdateField();
114 SetChangeMark();
115}
116
117CPWL_CheckBox* CFFL_CheckBox::GetCheckBox(CPDFSDK_PageView* pPageView,
118 bool bNew) {
119 return static_cast<CPWL_CheckBox*>(GetPDFWindow(pPageView, bNew));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}