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" |
Dan Sinclair | c411eb9 | 2017-07-25 09:39:30 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/pwl/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 | |
Tom Sepez | bf15730 | 2017-09-15 13:26:32 -0700 | [diff] [blame] | 21 | CPWL_Wnd* CFFL_CheckBox::NewPDFWindow(const CPWL_Wnd::CreateParams& cp) { |
| 22 | auto* pWnd = new CPWL_CheckBox(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | |
Tom Sepez | d0409af | 2017-05-25 15:53:57 -0700 | [diff] [blame] | 48 | CPDFSDK_Annot::ObservedPtr pObserved(m_pWidget.Get()); |
Dan Sinclair | fecd750 | 2017-07-06 11:35:04 -0400 | [diff] [blame] | 49 | if (m_pFormFillEnv->GetInteractiveFormFiller()->OnButtonUp( |
| 50 | &pObserved, pPageView, nFlags)) { |
| 51 | if (!pObserved) |
| 52 | m_pWidget = nullptr; |
| 53 | return true; |
| 54 | } |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 55 | if (!pObserved) { |
| 56 | m_pWidget = nullptr; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 57 | return true; |
tsepez | f8074ce | 2016-09-27 14:29:57 -0700 | [diff] [blame] | 58 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | |
| 60 | CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags); |
Claudio DeSouza | f0947bd | 2017-09-14 21:59:05 +0100 | [diff] [blame] | 61 | |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 62 | CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true); |
Claudio DeSouza | f0947bd | 2017-09-14 21:59:05 +0100 | [diff] [blame] | 63 | if (pWnd) { |
| 64 | CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot); |
| 65 | pWnd->SetCheck(!pWidget->IsChecked()); |
| 66 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | |
Lei Zhang | 6a3fc45 | 2017-06-13 14:04:02 -0700 | [diff] [blame] | 68 | return CommitData(pPageView, nFlags); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | } |
| 70 | default: |
| 71 | return CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags); |
| 72 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 75 | bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView, |
| 76 | CPDFSDK_Annot* pAnnot, |
| 77 | uint32_t nFlags, |
Dan Sinclair | f528eee | 2017-02-14 11:52:07 -0500 | [diff] [blame] | 78 | const CFX_PointF& point) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 81 | if (!IsValid()) |
| 82 | return true; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 83 | |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 84 | CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true); |
| 85 | if (pWnd) { |
| 86 | CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot); |
| 87 | pWnd->SetCheck(!pWidget->IsChecked()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 90 | return CommitData(pPageView, nFlags); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 91 | } |
| 92 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 93 | bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) { |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 94 | CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false); |
Lei Zhang | 96660d6 | 2015-12-14 18:27:25 -0800 | [diff] [blame] | 95 | return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) { |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 99 | CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false); |
| 100 | if (!pWnd) |
| 101 | return; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 102 | |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 103 | bool bNewChecked = pWnd->IsChecked(); |
| 104 | if (bNewChecked) { |
| 105 | CPDF_FormField* pField = m_pWidget->GetFormField(); |
| 106 | for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) { |
| 107 | if (CPDF_FormControl* pCtrl = pField->GetControl(i)) { |
| 108 | if (pCtrl->IsChecked()) { |
| 109 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | } |
Luật Nguyễn | 1886471 | 2017-10-10 12:39:22 +0800 | [diff] [blame] | 114 | CPDFSDK_Widget::ObservedPtr observed_widget(m_pWidget.Get()); |
| 115 | CFFL_CheckBox::ObservedPtr observed_this(this); |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 116 | |
| 117 | m_pWidget->SetCheck(bNewChecked, false); |
Luật Nguyễn | 1886471 | 2017-10-10 12:39:22 +0800 | [diff] [blame] | 118 | if (!observed_widget) |
| 119 | return; |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 120 | m_pWidget->UpdateField(); |
Luật Nguyễn | 1886471 | 2017-10-10 12:39:22 +0800 | [diff] [blame] | 121 | if (!observed_widget || !observed_this) |
| 122 | return; |
Lei Zhang | 77e3fc5 | 2017-06-15 12:37:33 -0700 | [diff] [blame] | 123 | SetChangeMark(); |
| 124 | } |
| 125 | |
| 126 | CPWL_CheckBox* CFFL_CheckBox::GetCheckBox(CPDFSDK_PageView* pPageView, |
| 127 | bool bNew) { |
| 128 | return static_cast<CPWL_CheckBox*>(GetPDFWindow(pPageView, bNew)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 129 | } |