blob: 334adb68eddd06ec456d91a278943d52d1ee4be6 [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
Dan Sinclairedbb3192016-03-21 09:08:24 -04009#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclair79db6092016-09-14 07:27:21 -070010#include "fpdfsdk/include/cpdfsdk_environment.h"
jaepark611adb82016-08-17 11:34:36 -070011#include "fpdfsdk/include/cpdfsdk_widget.h"
dan sinclair89e904b2016-03-23 19:29:15 -040012#include "fpdfsdk/pdfwindow/PWL_SpecialButton.h"
Lei Zhangc2fb35f2016-01-05 16:46:58 -080013#include "public/fpdf_fwlevent.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
dsinclair79db6092016-09-14 07:27:21 -070015CFFL_CheckBox::CFFL_CheckBox(CPDFSDK_Environment* pApp, CPDFSDK_Widget* pWidget)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016 : CFFL_Button(pApp, pWidget) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018CFFL_CheckBox::~CFFL_CheckBox() {}
19
20CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp,
21 CPDFSDK_PageView* pPageView) {
22 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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028FX_BOOL CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot,
dsinclair72177da2016-09-15 12:07:23 -070029 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:
34 return TRUE;
35 default:
36 return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags);
37 }
38}
39FX_BOOL CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot,
dsinclair72177da2016-09-15 12:07:23 -070040 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
48 FX_BOOL bReset = FALSE;
49 FX_BOOL bExit = FALSE;
tsepezf8074ce2016-09-27 14:29:57 -070050 CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget);
51 m_pEnv->GetInteractiveFormFiller()->OnButtonUp(&pObserved, pPageView,
dsinclairb94d7c92016-09-21 12:07:00 -070052 bReset, bExit, nFlags);
tsepezf8074ce2016-09-27 14:29:57 -070053 if (!pObserved) {
54 m_pWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return TRUE;
tsepezf8074ce2016-09-27 14:29:57 -070056 }
57 if (bReset || bExit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 return TRUE;
59
60 CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE))
62 pWnd->SetCheck(!pWnd->IsChecked());
63
64 CommitData(pPageView, nFlags);
65 return TRUE;
66 }
67 default:
68 return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
69 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070}
71
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072FX_BOOL CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
73 CPDFSDK_Annot* pAnnot,
dsinclair72177da2016-09-15 12:07:23 -070074 uint32_t nFlags,
Tom Sepez281a9ea2016-02-26 14:24:28 -080075 const CFX_FloatPoint& 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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 if (IsValid()) {
79 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE)) {
80 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
81 pWnd->SetCheck(!pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070083
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 if (!CommitData(pPageView, nFlags))
85 return FALSE;
86 }
87
88 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091FX_BOOL CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -080092 CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE);
93 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE)) {
Wei Li97da9762016-03-11 17:00:48 -080098 bool bNewChecked = pWnd->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 if (bNewChecked) {
101 CPDF_FormField* pField = m_pWidget->GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
103 if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
104 if (pCtrl->IsChecked()) {
105 break;
106 }
107 }
108 }
109 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110
Wei Li97da9762016-03-11 17:00:48 -0800111 m_pWidget->SetCheck(bNewChecked, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 m_pWidget->UpdateField();
113 SetChangeMark();
114 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}