John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | edbb319 | 2016-03-21 09:08:24 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/formfiller/cffl_checkbox.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 8 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 9 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 10 | #include "fpdfsdk/cpdfsdk_widget.h" |
Dan Sinclair | edbb319 | 2016-03-21 09:08:24 -0400 | [diff] [blame] | 11 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
Lei Zhang | 633a3b7 | 2017-06-02 15:27:22 -0700 | [diff] [blame] | 12 | #include "fpdfsdk/pdfwindow/cpwl_special_button.h" |
Lei Zhang | c2fb35f | 2016-01-05 16:46:58 -0800 | [diff] [blame] | 13 | #include "public/fpdf_fwlevent.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 14 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 15 | CFFL_CheckBox::CFFL_CheckBox(CPDFSDK_FormFillEnvironment* pApp, |
| 16 | CPDFSDK_Widget* pWidget) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | : CFFL_Button(pApp, pWidget) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 19 | CFFL_CheckBox::~CFFL_CheckBox() {} |
| 20 | |
Lei Zhang | dd29c25 | 2017-06-01 23:55:51 -0700 | [diff] [blame] | 21 | CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const PWL_CREATEPARAM& cp) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 22 | CPWL_CheckBox* pWnd = new CPWL_CheckBox(); |
| 23 | pWnd->Create(cp); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | pWnd->SetCheck(m_pWidget->IsChecked()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 25 | return pWnd; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | } |
| 27 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 28 | bool CFFL_CheckBox::OnKeyDown(CPDFSDK_Annot* pAnnot, |
| 29 | uint32_t nKeyCode, |
| 30 | uint32_t nFlags) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | switch (nKeyCode) { |
| 32 | case FWL_VKEY_Return: |
| 33 | case FWL_VKEY_Space: |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 34 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 35 | default: |
| 36 | return CFFL_FormFiller::OnKeyDown(pAnnot, nKeyCode, nFlags); |
| 37 | } |
| 38 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 39 | bool CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot, |
| 40 | uint32_t nChar, |
| 41 | uint32_t nFlags) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | switch (nChar) { |
| 43 | case FWL_VKEY_Return: |
| 44 | case FWL_VKEY_Space: { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | CPDFSDK_PageView* pPageView = pAnnot->GetPageView(); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 46 | ASSERT(pPageView); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 48 | bool bReset = false; |
| 49 | bool bExit = false; |
Tom Sepez | d0409af | 2017-05-25 15:53:57 -0700 | [diff] [blame] | 50 | CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget.Get()); |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 51 | m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp( |
| 52 | &pObserved, pPageView, bReset, bExit, nFlags); |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 53 | if (!pObserved) { |
| 54 | m_pWidget = nullptr; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 55 | return true; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 56 | } |
| 57 | if (bReset || bExit) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 58 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | |
| 60 | CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 61 | if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | pWnd->SetCheck(!pWnd->IsChecked()); |
| 63 | |
| 64 | CommitData(pPageView, nFlags); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 65 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | } |
| 67 | default: |
| 68 | return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags); |
| 69 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 72 | bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView, |
| 73 | CPDFSDK_Annot* pAnnot, |
| 74 | uint32_t nFlags, |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 75 | const CFX_PointF& point) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 77 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | if (IsValid()) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 79 | if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 80 | CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot; |
| 81 | pWnd->SetCheck(!pWidget->IsChecked()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 83 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | if (!CommitData(pPageView, nFlags)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 85 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 88 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 91 | bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) { |
| 92 | CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 93 | return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 97 | if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false)) { |
Wei Li | 97da976 | 2016-03-11 17:00:48 -0800 | [diff] [blame] | 98 | bool bNewChecked = pWnd->IsChecked(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 99 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | if (bNewChecked) { |
| 101 | CPDF_FormField* pField = m_pWidget->GetFormField(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | |
Wei Li | 97da976 | 2016-03-11 17:00:48 -0800 | [diff] [blame] | 111 | m_pWidget->SetCheck(bNewChecked, false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | m_pWidget->UpdateField(); |
| 113 | SetChangeMark(); |
| 114 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 115 | } |