blob: a81458e92df0f6930ed4a12356e87d19f4ddfb58 [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"
Lei Zhang633a3b72017-06-02 15:27:22 -070012#include "fpdfsdk/pdfwindow/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
tsepez4cf55152016-11-02 14:37:54 -070048 bool bReset = false;
49 bool bExit = false;
Tom Sepezd0409af2017-05-25 15:53:57 -070050 CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget.Get());
dsinclair8779fa82016-10-12 12:05:44 -070051 m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp(
52 &pObserved, pPageView, bReset, bExit, nFlags);
tsepezf8074ce2016-09-27 14:29:57 -070053 if (!pObserved) {
54 m_pWidget = nullptr;
tsepez4cf55152016-11-02 14:37:54 -070055 return true;
tsepezf8074ce2016-09-27 14:29:57 -070056 }
57 if (bReset || bExit)
tsepez4cf55152016-11-02 14:37:54 -070058 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059
60 CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
tsepez4cf55152016-11-02 14:37:54 -070061 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true))
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 pWnd->SetCheck(!pWnd->IsChecked());
63
Lei Zhang6a3fc452017-06-13 14:04:02 -070064 return CommitData(pPageView, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 }
66 default:
67 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
68 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069}
70
tsepez4cf55152016-11-02 14:37:54 -070071bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
72 CPDFSDK_Annot* pAnnot,
73 uint32_t nFlags,
Dan Sinclairf528eee2017-02-14 11:52:07 -050074 const CFX_PointF& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 if (IsValid()) {
tsepez4cf55152016-11-02 14:37:54 -070078 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
80 pWnd->SetCheck(!pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070082
Lei Zhang6a3fc452017-06-13 14:04:02 -070083 return CommitData(pPageView, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 }
85
tsepez4cf55152016-11-02 14:37:54 -070086 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
tsepez4cf55152016-11-02 14:37:54 -070089bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
90 CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false);
Lei Zhang96660d62015-12-14 18:27:25 -080091 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
tsepez4cf55152016-11-02 14:37:54 -070095 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false)) {
Wei Li97da9762016-03-11 17:00:48 -080096 bool bNewChecked = pWnd->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 if (bNewChecked) {
99 CPDF_FormField* pField = m_pWidget->GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
101 if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
102 if (pCtrl->IsChecked()) {
103 break;
104 }
105 }
106 }
107 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
Wei Li97da9762016-03-11 17:00:48 -0800109 m_pWidget->SetCheck(bNewChecked, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 m_pWidget->UpdateField();
111 SetChangeMark();
112 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113}