blob: 64981f053c03b22d6500154b1927f4d63d7dd64d [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 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
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
21CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp,
22 CPDFSDK_PageView* pPageView) {
23 CPWL_CheckBox* pWnd = new CPWL_CheckBox();
24 pWnd->Create(cp);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 pWnd->SetCheck(m_pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 return pWnd;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029FX_BOOL CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot,
dsinclair72177da2016-09-15 12:07:23 -070030 uint32_t nKeyCode,
31 uint32_t nFlags) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 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}
40FX_BOOL CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot,
dsinclair72177da2016-09-15 12:07:23 -070041 uint32_t nChar,
42 uint32_t nFlags) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 switch (nChar) {
44 case FWL_VKEY_Return:
45 case FWL_VKEY_Space: {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
Lei Zhang96660d62015-12-14 18:27:25 -080047 ASSERT(pPageView);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048
49 FX_BOOL bReset = FALSE;
50 FX_BOOL bExit = FALSE;
tsepezf8074ce2016-09-27 14:29:57 -070051 CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget);
52 m_pEnv->GetInteractiveFormFiller()->OnButtonUp(&pObserved, pPageView,
dsinclairb94d7c92016-09-21 12:07:00 -070053 bReset, bExit, nFlags);
tsepezf8074ce2016-09-27 14:29:57 -070054 if (!pObserved) {
55 m_pWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 return TRUE;
tsepezf8074ce2016-09-27 14:29:57 -070057 }
58 if (bReset || bExit)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 return TRUE;
60
61 CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 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 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071}
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073FX_BOOL CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView,
74 CPDFSDK_Annot* pAnnot,
dsinclair72177da2016-09-15 12:07:23 -070075 uint32_t nFlags,
Tom Sepez281a9ea2016-02-26 14:24:28 -080076 const CFX_FloatPoint& point) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 if (IsValid()) {
80 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, TRUE)) {
81 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
82 pWnd->SetCheck(!pWidget->IsChecked());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 if (!CommitData(pPageView, nFlags))
86 return FALSE;
87 }
88
89 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092FX_BOOL CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
Lei Zhang96660d62015-12-14 18:27:25 -080093 CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE);
94 return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, FALSE)) {
Wei Li97da9762016-03-11 17:00:48 -080099 bool bNewChecked = pWnd->IsChecked();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (bNewChecked) {
102 CPDF_FormField* pField = m_pWidget->GetFormField();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 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 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111
Wei Li97da9762016-03-11 17:00:48 -0800112 m_pWidget->SetCheck(bNewChecked, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 m_pWidget->UpdateField();
114 SetChangeMark();
115 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}