jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 1 | // Copyright 2016 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. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_interform.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
| 10 | #include <memory> |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 11 | #include <sstream> |
| 12 | #include <string> |
Tom Sepez | e005dc3 | 2018-06-19 17:33:32 +0000 | [diff] [blame] | 13 | #include <utility> |
thestig | 7c292e0 | 2016-09-28 14:14:26 -0700 | [diff] [blame] | 14 | #include <vector> |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 15 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 16 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cfdf_document.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_document.h" |
| 20 | #include "core/fpdfapi/parser/cpdf_stream.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 21 | #include "core/fpdfdoc/cpdf_actionfields.h" |
| 22 | #include "core/fpdfdoc/cpdf_interform.h" |
Tom Sepez | c22d671 | 2018-06-05 22:33:31 +0000 | [diff] [blame] | 23 | #include "core/fxcrt/autorestorer.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 24 | #include "core/fxge/cfx_graphstatedata.h" |
| 25 | #include "core/fxge/cfx_pathdata.h" |
Dan Sinclair | 7d12532 | 2018-03-28 18:49:34 +0000 | [diff] [blame] | 26 | #include "fpdfsdk/cpdfsdk_actionhandler.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 27 | #include "fpdfsdk/cpdfsdk_annot.h" |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 28 | #include "fpdfsdk/cpdfsdk_annotiterator.h" |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 29 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 30 | #include "fpdfsdk/cpdfsdk_helpers.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 31 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 32 | #include "fpdfsdk/cpdfsdk_widget.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 33 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 34 | #include "fpdfsdk/ipdfsdk_annothandler.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 35 | #include "fxjs/ijs_event_context.h" |
| 36 | #include "fxjs/ijs_runtime.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 37 | #include "third_party/base/stl_util.h" |
| 38 | |
| 39 | #ifdef PDF_ENABLE_XFA |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 40 | #include "fpdfsdk/cpdfsdk_xfawidget.h" |
dsinclair | 521b750 | 2016-11-02 13:02:28 -0700 | [diff] [blame] | 41 | #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h" |
dsinclair | 4d29e78 | 2016-10-04 14:02:47 -0700 | [diff] [blame] | 42 | #include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 43 | #include "xfa/fxfa/cxfa_eventparam.h" |
Dan Sinclair | 80c4878 | 2017-03-23 12:11:20 -0400 | [diff] [blame] | 44 | #include "xfa/fxfa/cxfa_ffdocview.h" |
| 45 | #include "xfa/fxfa/cxfa_ffwidget.h" |
| 46 | #include "xfa/fxfa/cxfa_ffwidgethandler.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 47 | #endif // PDF_ENABLE_XFA |
| 48 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | |
Lei Zhang | 4f7479a | 2018-03-21 13:37:06 +0000 | [diff] [blame] | 51 | constexpr uint32_t kWhiteBGR = FXSYS_BGR(255, 255, 255); |
| 52 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 53 | bool IsFormFieldTypeComboOrText(FormFieldType fieldType) { |
| 54 | switch (fieldType) { |
| 55 | case FormFieldType::kComboBox: |
| 56 | case FormFieldType::kTextField: |
| 57 | return true; |
| 58 | default: |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
Ryan Harrison | 2056fac | 2018-01-16 16:06:45 +0000 | [diff] [blame] | 63 | #ifdef PDF_ENABLE_XFA |
| 64 | bool IsFormFieldTypeXFA(FormFieldType fieldType) { |
| 65 | switch (fieldType) { |
| 66 | case FormFieldType::kXFA: |
| 67 | case FormFieldType::kXFA_CheckBox: |
| 68 | case FormFieldType::kXFA_ComboBox: |
| 69 | case FormFieldType::kXFA_ImageField: |
| 70 | case FormFieldType::kXFA_ListBox: |
| 71 | case FormFieldType::kXFA_PushButton: |
| 72 | case FormFieldType::kXFA_Signature: |
| 73 | case FormFieldType::kXFA_TextField: |
| 74 | return true; |
| 75 | default: |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | #endif // PDF_ENABLE_XFA |
| 80 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 81 | } // namespace |
| 82 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame] | 83 | CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv) |
| 84 | : m_pFormFillEnv(pFormFillEnv), |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 85 | m_pInterForm( |
| 86 | pdfium::MakeUnique<CPDF_InterForm>(m_pFormFillEnv->GetPDFDocument())), |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 87 | #ifdef PDF_ENABLE_XFA |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 88 | m_bXfaCalculate(true), |
| 89 | m_bXfaValidationsEnabled(true), |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 90 | #endif // PDF_ENABLE_XFA |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 91 | m_bCalculate(true), |
| 92 | m_bBusy(false), |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 93 | m_HighlightAlpha(0) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 94 | m_pInterForm->SetFormNotify(this); |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 95 | RemoveAllHighLights(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | CPDFSDK_InterForm::~CPDFSDK_InterForm() { |
| 99 | m_Map.clear(); |
| 100 | #ifdef PDF_ENABLE_XFA |
| 101 | m_XFAMap.clear(); |
| 102 | #endif // PDF_ENABLE_XFA |
| 103 | } |
| 104 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 105 | bool CPDFSDK_InterForm::HighlightWidgets() { |
| 106 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 110 | bool bNext) const { |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 111 | auto pIterator = pdfium::MakeUnique<CPDFSDK_AnnotIterator>( |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 112 | pWidget->GetPageView(), CPDF_Annot::Subtype::WIDGET); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 113 | |
Tom Sepez | 4ef943b | 2018-07-26 19:06:06 +0000 | [diff] [blame] | 114 | return ToCPDFSDKWidget(bNext ? pIterator->GetNextAnnot(pWidget) |
| 115 | : pIterator->GetPrevAnnot(pWidget)); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
dsinclair | c5267c5 | 2016-11-04 15:35:12 -0700 | [diff] [blame] | 118 | CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 119 | if (!pControl || !m_pInterForm) |
| 120 | return nullptr; |
| 121 | |
| 122 | CPDFSDK_Widget* pWidget = nullptr; |
| 123 | const auto it = m_Map.find(pControl); |
| 124 | if (it != m_Map.end()) |
| 125 | pWidget = it->second; |
| 126 | if (pWidget) |
| 127 | return pWidget; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 128 | |
| 129 | CPDF_Dictionary* pControlDict = pControl->GetWidget(); |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 130 | CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 131 | CPDFSDK_PageView* pPage = nullptr; |
| 132 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 133 | if (CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 134 | int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum()); |
| 135 | if (nPageIndex >= 0) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 136 | pPage = m_pFormFillEnv->GetPageView(nPageIndex); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (!pPage) { |
| 140 | int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict); |
| 141 | if (nPageIndex >= 0) |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 142 | pPage = m_pFormFillEnv->GetPageView(nPageIndex); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Tom Sepez | 4ef943b | 2018-07-26 19:06:06 +0000 | [diff] [blame] | 145 | return pPage ? ToCPDFSDKWidget(pPage->GetAnnotByDict(pControlDict)) : nullptr; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void CPDFSDK_InterForm::GetWidgets( |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 149 | const WideString& sFieldName, |
tsepez | 8fa8279 | 2017-01-11 09:32:33 -0800 | [diff] [blame] | 150 | std::vector<CPDFSDK_Annot::ObservedPtr>* widgets) const { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 151 | for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) { |
| 152 | CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName); |
| 153 | ASSERT(pFormField); |
| 154 | GetWidgets(pFormField, widgets); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void CPDFSDK_InterForm::GetWidgets( |
| 159 | CPDF_FormField* pField, |
tsepez | 8fa8279 | 2017-01-11 09:32:33 -0800 | [diff] [blame] | 160 | std::vector<CPDFSDK_Annot::ObservedPtr>* widgets) const { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 161 | for (int i = 0, sz = pField->CountControls(); i < sz; ++i) { |
| 162 | CPDF_FormControl* pFormCtrl = pField->GetControl(i); |
| 163 | ASSERT(pFormCtrl); |
dsinclair | c5267c5 | 2016-11-04 15:35:12 -0700 | [diff] [blame] | 164 | CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 165 | if (pWidget) |
tsepez | 8fa8279 | 2017-01-11 09:32:33 -0800 | [diff] [blame] | 166 | widgets->emplace_back(pWidget); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | int CPDFSDK_InterForm::GetPageIndexByAnnotDict( |
| 171 | CPDF_Document* pDocument, |
| 172 | CPDF_Dictionary* pAnnotDict) const { |
| 173 | ASSERT(pAnnotDict); |
| 174 | |
| 175 | for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) { |
Tom Sepez | 967aa07 | 2018-05-08 13:40:20 +0000 | [diff] [blame] | 176 | if (CPDF_Dictionary* pPageDict = pDocument->GetPageDictionary(i)) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 177 | if (CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots")) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 178 | for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) { |
| 179 | CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j); |
| 180 | if (pAnnotDict == pDict) |
| 181 | return i; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl, |
| 191 | CPDFSDK_Widget* pWidget) { |
| 192 | m_Map[pControl] = pWidget; |
| 193 | } |
| 194 | |
| 195 | void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) { |
| 196 | m_Map.erase(pControl); |
| 197 | } |
| 198 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 199 | void CPDFSDK_InterForm::EnableCalculate(bool bEnabled) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 200 | m_bCalculate = bEnabled; |
| 201 | } |
| 202 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 203 | bool CPDFSDK_InterForm::IsCalculateEnabled() const { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 204 | return m_bCalculate; |
| 205 | } |
| 206 | |
| 207 | #ifdef PDF_ENABLE_XFA |
| 208 | void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget, |
| 209 | CPDFSDK_XFAWidget* pWidget) { |
| 210 | ASSERT(hWidget); |
| 211 | m_XFAMap[hWidget] = pWidget; |
| 212 | } |
| 213 | |
| 214 | void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) { |
| 215 | ASSERT(hWidget); |
| 216 | m_XFAMap.erase(hWidget); |
| 217 | } |
| 218 | |
| 219 | CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) { |
| 220 | ASSERT(hWidget); |
| 221 | auto it = m_XFAMap.find(hWidget); |
| 222 | return it != m_XFAMap.end() ? it->second : nullptr; |
| 223 | } |
| 224 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 225 | void CPDFSDK_InterForm::XfaEnableCalculate(bool bEnabled) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 226 | m_bXfaCalculate = bEnabled; |
| 227 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 228 | bool CPDFSDK_InterForm::IsXfaCalculateEnabled() const { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 229 | return m_bXfaCalculate; |
| 230 | } |
| 231 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 232 | bool CPDFSDK_InterForm::IsXfaValidationsEnabled() { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 233 | return m_bXfaValidationsEnabled; |
| 234 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 235 | void CPDFSDK_InterForm::XfaSetValidationsEnabled(bool bEnabled) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 236 | m_bXfaValidationsEnabled = bEnabled; |
| 237 | } |
| 238 | |
Dan Sinclair | ce047a6 | 2018-01-30 18:15:02 +0000 | [diff] [blame] | 239 | void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 240 | for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { |
| 241 | CPDF_FormControl* pFormCtrl = pFormField->GetControl(i); |
dsinclair | c5267c5 | 2016-11-04 15:35:12 -0700 | [diff] [blame] | 242 | if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) |
Dan Sinclair | ce047a6 | 2018-01-30 18:15:02 +0000 | [diff] [blame] | 243 | pWidget->Synchronize(false); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | #endif // PDF_ENABLE_XFA |
| 247 | |
| 248 | void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 249 | if (!m_pFormFillEnv->IsJSPlatformPresent()) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 250 | return; |
| 251 | |
| 252 | if (m_bBusy) |
| 253 | return; |
| 254 | |
Tom Sepez | c22d671 | 2018-06-05 22:33:31 +0000 | [diff] [blame] | 255 | AutoRestorer<bool> restorer(&m_bBusy); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 256 | m_bBusy = true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 257 | |
Tom Sepez | c22d671 | 2018-06-05 22:33:31 +0000 | [diff] [blame] | 258 | if (!IsCalculateEnabled()) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 259 | return; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 260 | |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 261 | IJS_Runtime* pRuntime = m_pFormFillEnv->GetIJSRuntime(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 262 | int nSize = m_pInterForm->CountFieldsInCalculationOrder(); |
| 263 | for (int i = 0; i < nSize; i++) { |
| 264 | CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i); |
| 265 | if (!pField) |
| 266 | continue; |
| 267 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 268 | FormFieldType fieldType = pField->GetFieldType(); |
| 269 | if (!IsFormFieldTypeComboOrText(fieldType)) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 270 | continue; |
| 271 | |
| 272 | CPDF_AAction aAction = pField->GetAdditionalAction(); |
| 273 | if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Calculate)) |
| 274 | continue; |
| 275 | |
| 276 | CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate); |
| 277 | if (!action.GetDict()) |
| 278 | continue; |
| 279 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 280 | WideString csJS = action.GetJavaScript(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 281 | if (csJS.IsEmpty()) |
| 282 | continue; |
| 283 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 284 | WideString sOldValue = pField->GetValue(); |
| 285 | WideString sValue = sOldValue; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 286 | bool bRC = true; |
Tom Sepez | c22d671 | 2018-06-05 22:33:31 +0000 | [diff] [blame] | 287 | IJS_Runtime::ScopedEventContext pContext(pRuntime); |
Tom Sepez | 3d81315 | 2018-06-21 17:28:24 +0000 | [diff] [blame] | 288 | pContext->OnField_Calculate(pFormField, pField, &sValue, &bRC); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 289 | |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 290 | Optional<IJS_Runtime::JS_Error> err = pContext->RunScript(csJS); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 291 | if (!err && bRC && sValue.Compare(sOldValue) != 0) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 292 | pField->SetValue(sValue, true); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 293 | } |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 296 | WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, |
| 297 | bool& bFormatted) { |
| 298 | WideString sValue = pFormField->GetValue(); |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 299 | if (!m_pFormFillEnv->IsJSPlatformPresent()) { |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 300 | bFormatted = false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 301 | return sValue; |
| 302 | } |
| 303 | |
Tom Sepez | f5ca90c | 2018-02-01 02:15:44 +0000 | [diff] [blame] | 304 | IJS_Runtime* pRuntime = m_pFormFillEnv->GetIJSRuntime(); |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 305 | if (pFormField->GetFieldType() == FormFieldType::kComboBox && |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 306 | pFormField->CountSelectedItems() > 0) { |
| 307 | int index = pFormField->GetSelectedIndex(0); |
| 308 | if (index >= 0) |
| 309 | sValue = pFormField->GetOptionLabel(index); |
| 310 | } |
| 311 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 312 | bFormatted = false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 313 | |
| 314 | CPDF_AAction aAction = pFormField->GetAdditionalAction(); |
| 315 | if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) { |
| 316 | CPDF_Action action = aAction.GetAction(CPDF_AAction::Format); |
| 317 | if (action.GetDict()) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 318 | WideString script = action.GetJavaScript(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 319 | if (!script.IsEmpty()) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 320 | WideString Value = sValue; |
Tom Sepez | c22d671 | 2018-06-05 22:33:31 +0000 | [diff] [blame] | 321 | IJS_Runtime::ScopedEventContext pContext(pRuntime); |
Tom Sepez | 3d81315 | 2018-06-21 17:28:24 +0000 | [diff] [blame] | 322 | pContext->OnField_Format(pFormField, &Value, true); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 323 | Optional<IJS_Runtime::JS_Error> err = pContext->RunScript(script); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 324 | if (!err) { |
Tom Sepez | e005dc3 | 2018-06-19 17:33:32 +0000 | [diff] [blame] | 325 | sValue = std::move(Value); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 326 | bFormatted = true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 331 | return sValue; |
| 332 | } |
| 333 | |
| 334 | void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 335 | const WideString* sValue, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 336 | bool bValueChanged) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 337 | for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { |
| 338 | CPDF_FormControl* pFormCtrl = pFormField->GetControl(i); |
| 339 | ASSERT(pFormCtrl); |
dsinclair | c5267c5 | 2016-11-04 15:35:12 -0700 | [diff] [blame] | 340 | if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl)) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 341 | pWidget->ResetAppearance(sValue, bValueChanged); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) { |
Lei Zhang | 375c276 | 2017-03-10 14:37:14 -0800 | [diff] [blame] | 346 | auto* formfiller = m_pFormFillEnv->GetInteractiveFormFiller(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 347 | for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { |
| 348 | CPDF_FormControl* pFormCtrl = pFormField->GetControl(i); |
| 349 | ASSERT(pFormCtrl); |
| 350 | |
Lei Zhang | 77f9bff | 2017-08-29 11:34:12 -0700 | [diff] [blame] | 351 | CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl); |
| 352 | if (!pWidget) |
| 353 | continue; |
| 354 | |
Tom Sepez | 101535f | 2018-06-12 13:36:05 +0000 | [diff] [blame] | 355 | IPDF_Page* pPage = pWidget->GetPage(); |
Lei Zhang | 77f9bff | 2017-08-29 11:34:12 -0700 | [diff] [blame] | 356 | FX_RECT rect = formfiller->GetViewBBox( |
| 357 | m_pFormFillEnv->GetPageView(pPage, false), pWidget); |
| 358 | m_pFormFillEnv->Invalidate(pPage, rect); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 362 | bool CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 363 | const WideString& csValue) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 364 | CPDF_AAction aAction = pFormField->GetAdditionalAction(); |
| 365 | if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 366 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 367 | |
| 368 | CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke); |
| 369 | if (!action.GetDict()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 370 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 371 | |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 372 | CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHandler(); |
Dan Sinclair | 7d12532 | 2018-03-28 18:49:34 +0000 | [diff] [blame] | 373 | CPDFSDK_FieldAction fa; |
Lei Zhang | 60fa2fc | 2017-07-21 17:42:19 -0700 | [diff] [blame] | 374 | fa.bModifier = false; |
| 375 | fa.bShift = false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 376 | fa.sValue = csValue; |
Tom Sepez | cc20513 | 2017-05-16 14:01:47 -0700 | [diff] [blame] | 377 | pActionHandler->DoAction_FieldJavaScript( |
Dan Sinclair | 8cdea72 | 2018-01-30 18:56:50 +0000 | [diff] [blame] | 378 | action, CPDF_AAction::KeyStroke, m_pFormFillEnv.Get(), pFormField, &fa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 379 | return fa.bRC; |
| 380 | } |
| 381 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 382 | bool CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 383 | const WideString& csValue) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 384 | CPDF_AAction aAction = pFormField->GetAdditionalAction(); |
| 385 | if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 386 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 387 | |
| 388 | CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate); |
| 389 | if (!action.GetDict()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 390 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 391 | |
Lei Zhang | cddc8ed | 2017-06-20 17:26:44 -0700 | [diff] [blame] | 392 | CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHandler(); |
Dan Sinclair | 7d12532 | 2018-03-28 18:49:34 +0000 | [diff] [blame] | 393 | CPDFSDK_FieldAction fa; |
Lei Zhang | 60fa2fc | 2017-07-21 17:42:19 -0700 | [diff] [blame] | 394 | fa.bModifier = false; |
| 395 | fa.bShift = false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 396 | fa.sValue = csValue; |
Tom Sepez | cc20513 | 2017-05-16 14:01:47 -0700 | [diff] [blame] | 397 | pActionHandler->DoAction_FieldJavaScript( |
Dan Sinclair | 8cdea72 | 2018-01-30 18:56:50 +0000 | [diff] [blame] | 398 | action, CPDF_AAction::Validate, m_pFormFillEnv.Get(), pFormField, &fa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 399 | return fa.bRC; |
| 400 | } |
| 401 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 402 | bool CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 403 | ASSERT(action.GetDict()); |
| 404 | |
| 405 | CPDF_ActionFields af(&action); |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 406 | std::vector<const CPDF_Object*> fieldObjects = af.GetAllFields(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 407 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
| 408 | |
| 409 | bool bHide = action.GetHideStatus(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 410 | bool bChanged = false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 411 | |
| 412 | for (CPDF_FormField* pField : fields) { |
| 413 | for (int i = 0, sz = pField->CountControls(); i < sz; ++i) { |
| 414 | CPDF_FormControl* pControl = pField->GetControl(i); |
| 415 | ASSERT(pControl); |
| 416 | |
dsinclair | c5267c5 | 2016-11-04 15:35:12 -0700 | [diff] [blame] | 417 | if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 418 | uint32_t nFlags = pWidget->GetFlags(); |
| 419 | nFlags &= ~ANNOTFLAG_INVISIBLE; |
| 420 | nFlags &= ~ANNOTFLAG_NOVIEW; |
| 421 | if (bHide) |
| 422 | nFlags |= ANNOTFLAG_HIDDEN; |
| 423 | else |
| 424 | nFlags &= ~ANNOTFLAG_HIDDEN; |
| 425 | pWidget->SetFlags(nFlags); |
| 426 | pWidget->GetPageView()->UpdateView(pWidget); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 427 | bChanged = true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return bChanged; |
| 433 | } |
| 434 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 435 | bool CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 436 | WideString sDestination = action.GetFilePath(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 437 | if (sDestination.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 438 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 439 | |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 440 | const CPDF_Dictionary* pActionDict = action.GetDict(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 441 | if (pActionDict->KeyExist("Fields")) { |
| 442 | CPDF_ActionFields af(&action); |
| 443 | uint32_t dwFlags = action.GetFlags(); |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 444 | std::vector<const CPDF_Object*> fieldObjects = af.GetAllFields(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 445 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
| 446 | if (!fields.empty()) { |
| 447 | bool bIncludeOrExclude = !(dwFlags & 0x01); |
Nicolas Pena | a478dc5 | 2017-01-23 15:48:51 -0500 | [diff] [blame] | 448 | if (!m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 449 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 450 | |
| 451 | return SubmitFields(sDestination, fields, bIncludeOrExclude, false); |
| 452 | } |
| 453 | } |
Nicolas Pena | a478dc5 | 2017-01-23 15:48:51 -0500 | [diff] [blame] | 454 | if (!m_pInterForm->CheckRequiredFields(nullptr, true)) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 455 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 456 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 457 | return SubmitForm(sDestination, false); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 460 | bool CPDFSDK_InterForm::SubmitFields(const WideString& csDestination, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 461 | const std::vector<CPDF_FormField*>& fields, |
| 462 | bool bIncludeOrExclude, |
| 463 | bool bUrlEncoded) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 464 | ByteString textBuf = ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude); |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 465 | size_t nBufSize = textBuf.GetLength(); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 466 | if (nBufSize == 0) |
Henrique Nakashima | aea80dc | 2017-08-01 19:47:24 -0400 | [diff] [blame] | 467 | return false; |
Henrique Nakashima | aea80dc | 2017-08-01 19:47:24 -0400 | [diff] [blame] | 468 | |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 469 | uint8_t* pLocalBuffer = FX_Alloc(uint8_t, nBufSize); |
| 470 | memcpy(pLocalBuffer, textBuf.c_str(), nBufSize); |
| 471 | uint8_t* pBuffer = pLocalBuffer; |
Henrique Nakashima | aea80dc | 2017-08-01 19:47:24 -0400 | [diff] [blame] | 472 | |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 473 | if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) { |
| 474 | FX_Free(pLocalBuffer); |
| 475 | return false; |
| 476 | } |
| 477 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 478 | m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 479 | |
| 480 | if (pBuffer != pLocalBuffer) |
| 481 | FX_Free(pBuffer); |
| 482 | |
| 483 | FX_Free(pLocalBuffer); |
| 484 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 485 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 488 | bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, size_t& nBufSize) { |
tsepez | 05e0169 | 2016-11-28 17:30:09 -0800 | [diff] [blame] | 489 | std::unique_ptr<CFDF_Document> pFDF = |
| 490 | CFDF_Document::ParseMemory(pBuf, nBufSize); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 491 | if (!pFDF) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 492 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 493 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 494 | CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 495 | if (!pMainDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 496 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 497 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 498 | CPDF_Array* pFields = pMainDict->GetArrayFor("Fields"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 499 | if (!pFields) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 500 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 501 | |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 502 | std::ostringstream fdfEncodedData; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 503 | for (uint32_t i = 0; i < pFields->GetCount(); i++) { |
| 504 | CPDF_Dictionary* pField = pFields->GetDictAt(i); |
| 505 | if (!pField) |
| 506 | continue; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 507 | WideString name; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 508 | name = pField->GetUnicodeTextFor("T"); |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 509 | ByteString name_b = name.ToDefANSI(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 510 | ByteString csBValue = pField->GetStringFor("V"); |
| 511 | WideString csWValue = PDF_DecodeText(csBValue); |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 512 | ByteString csValue_b = csWValue.ToDefANSI(); |
Tom Sepez | e84e618 | 2018-06-20 14:20:23 +0000 | [diff] [blame] | 513 | fdfEncodedData << name_b << "=" << csValue_b; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 514 | if (i != pFields->GetCount() - 1) |
| 515 | fdfEncodedData << "&"; |
| 516 | } |
| 517 | |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 518 | nBufSize = fdfEncodedData.tellp(); |
Henrique Nakashima | aa1c787 | 2018-01-30 19:12:10 +0000 | [diff] [blame] | 519 | if (nBufSize <= 0) |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 520 | return false; |
| 521 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 522 | pBuf = FX_Alloc(uint8_t, nBufSize); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 523 | memcpy(pBuf, fdfEncodedData.str().c_str(), nBufSize); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 524 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 527 | ByteString CPDFSDK_InterForm::ExportFieldsToFDFTextBuf( |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 528 | const std::vector<CPDF_FormField*>& fields, |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 529 | bool bIncludeOrExclude) { |
Tom Sepez | 690d456 | 2017-05-18 11:42:46 -0700 | [diff] [blame] | 530 | std::unique_ptr<CFDF_Document> pFDF = m_pInterForm->ExportToFDF( |
| 531 | m_pFormFillEnv->JS_docGetFilePath(), fields, bIncludeOrExclude, false); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 532 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 533 | return pFDF ? pFDF->WriteToString() : ByteString(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 536 | bool CPDFSDK_InterForm::SubmitForm(const WideString& sDestination, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 537 | bool bUrlEncoded) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 538 | if (sDestination.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 539 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 540 | |
dsinclair | 7cbe68e | 2016-10-12 11:56:23 -0700 | [diff] [blame] | 541 | if (!m_pFormFillEnv || !m_pInterForm) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 542 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 543 | |
Tom Sepez | 690d456 | 2017-05-18 11:42:46 -0700 | [diff] [blame] | 544 | std::unique_ptr<CFDF_Document> pFDFDoc = |
| 545 | m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 546 | if (!pFDFDoc) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 547 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 548 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 549 | ByteString fdfBuffer = pFDFDoc->WriteToString(); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 550 | |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 551 | if (fdfBuffer.IsEmpty()) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 552 | return false; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 553 | |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 554 | uint8_t* pLocalBuffer = FX_Alloc(uint8_t, fdfBuffer.GetLength()); |
| 555 | memcpy(pLocalBuffer, fdfBuffer.c_str(), fdfBuffer.GetLength()); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 556 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 557 | uint8_t* pBuffer = pLocalBuffer; |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 558 | size_t nBufSize = fdfBuffer.GetLength(); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 559 | if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) { |
| 560 | FX_Free(pLocalBuffer); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 561 | return false; |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 562 | } |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 563 | |
Tom Sepez | 35939f8 | 2018-04-17 21:23:58 +0000 | [diff] [blame] | 564 | m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 565 | |
| 566 | if (pBuffer != pLocalBuffer) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 567 | FX_Free(pBuffer); |
| 568 | |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 569 | FX_Free(pLocalBuffer); |
| 570 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 571 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 574 | ByteString CPDFSDK_InterForm::ExportFormToFDFTextBuf() { |
Tom Sepez | 690d456 | 2017-05-18 11:42:46 -0700 | [diff] [blame] | 575 | std::unique_ptr<CFDF_Document> pFDF = |
| 576 | m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false); |
Henrique Nakashima | 5c09f4c | 2017-08-04 12:28:52 -0400 | [diff] [blame] | 577 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 578 | return pFDF ? pFDF->WriteToString() : ByteString(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
dan sinclair | 7544a4b | 2018-03-08 15:19:50 +0000 | [diff] [blame] | 581 | void CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 582 | ASSERT(action.GetDict()); |
| 583 | |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 584 | const CPDF_Dictionary* pActionDict = action.GetDict(); |
dan sinclair | 7544a4b | 2018-03-08 15:19:50 +0000 | [diff] [blame] | 585 | if (!pActionDict->KeyExist("Fields")) { |
| 586 | m_pInterForm->ResetForm(true); |
| 587 | return; |
| 588 | } |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 589 | |
| 590 | CPDF_ActionFields af(&action); |
| 591 | uint32_t dwFlags = action.GetFlags(); |
| 592 | |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 593 | std::vector<const CPDF_Object*> fieldObjects = af.GetAllFields(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 594 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
dan sinclair | 7544a4b | 2018-03-08 15:19:50 +0000 | [diff] [blame] | 595 | m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 598 | std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects( |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 599 | const std::vector<const CPDF_Object*>& objects) const { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 600 | std::vector<CPDF_FormField*> fields; |
Lei Zhang | 5cee3f2 | 2018-05-25 21:48:49 +0000 | [diff] [blame] | 601 | for (const CPDF_Object* pObject : objects) { |
Dan Sinclair | ce047a6 | 2018-01-30 18:15:02 +0000 | [diff] [blame] | 602 | if (!pObject || !pObject->IsString()) |
| 603 | continue; |
| 604 | |
| 605 | WideString csName = pObject->GetUnicodeText(); |
| 606 | CPDF_FormField* pField = m_pInterForm->GetField(0, csName); |
| 607 | if (pField) |
| 608 | fields.push_back(pField); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 609 | } |
| 610 | return fields; |
| 611 | } |
| 612 | |
dan sinclair | 507fb4e | 2018-03-08 15:14:09 +0000 | [diff] [blame] | 613 | bool CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField, |
| 614 | const WideString& csValue) { |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 615 | FormFieldType fieldType = pField->GetFieldType(); |
| 616 | if (!IsFormFieldTypeComboOrText(fieldType)) |
dan sinclair | 507fb4e | 2018-03-08 15:14:09 +0000 | [diff] [blame] | 617 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 618 | if (!OnKeyStrokeCommit(pField, csValue)) |
dan sinclair | 507fb4e | 2018-03-08 15:14:09 +0000 | [diff] [blame] | 619 | return false; |
| 620 | return OnValidate(pField, csValue); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) { |
| 624 | #ifdef PDF_ENABLE_XFA |
Dan Sinclair | ce047a6 | 2018-01-30 18:15:02 +0000 | [diff] [blame] | 625 | SynchronizeField(pField); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 626 | #endif // PDF_ENABLE_XFA |
Dan Sinclair | ce047a6 | 2018-01-30 18:15:02 +0000 | [diff] [blame] | 627 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 628 | FormFieldType fieldType = pField->GetFieldType(); |
Dan Sinclair | ce047a6 | 2018-01-30 18:15:02 +0000 | [diff] [blame] | 629 | if (!IsFormFieldTypeComboOrText(fieldType)) |
| 630 | return; |
| 631 | |
| 632 | OnCalculate(pField); |
| 633 | bool bFormatted = false; |
| 634 | WideString sValue = OnFormat(pField, bFormatted); |
| 635 | ResetFieldAppearance(pField, bFormatted ? &sValue : nullptr, true); |
| 636 | UpdateField(pField); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 637 | } |
| 638 | |
dan sinclair | 507fb4e | 2018-03-08 15:14:09 +0000 | [diff] [blame] | 639 | bool CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField, |
| 640 | const WideString& csValue) { |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 641 | if (pField->GetFieldType() != FormFieldType::kListBox) |
dan sinclair | 507fb4e | 2018-03-08 15:14:09 +0000 | [diff] [blame] | 642 | return true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 643 | if (!OnKeyStrokeCommit(pField, csValue)) |
dan sinclair | 507fb4e | 2018-03-08 15:14:09 +0000 | [diff] [blame] | 644 | return false; |
| 645 | return OnValidate(pField, csValue); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) { |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 649 | if (pField->GetFieldType() != FormFieldType::kListBox) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 650 | return; |
| 651 | |
| 652 | OnCalculate(pField); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 653 | ResetFieldAppearance(pField, nullptr, true); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 654 | UpdateField(pField); |
| 655 | } |
| 656 | |
| 657 | void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) { |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 658 | FormFieldType fieldType = pField->GetFieldType(); |
| 659 | if (fieldType != FormFieldType::kCheckBox && |
| 660 | fieldType != FormFieldType::kRadioButton) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 661 | return; |
| 662 | |
| 663 | OnCalculate(pField); |
| 664 | UpdateField(pField); |
| 665 | } |
| 666 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 667 | void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) { |
| 668 | OnCalculate(nullptr); |
| 669 | } |
| 670 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 671 | bool CPDFSDK_InterForm::IsNeedHighLight(FormFieldType fieldType) { |
| 672 | if (fieldType == FormFieldType::kUnknown) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 673 | return false; |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 674 | |
Ryan Harrison | 2056fac | 2018-01-16 16:06:45 +0000 | [diff] [blame] | 675 | #ifdef PDF_ENABLE_XFA |
| 676 | // For the XFA fields, we need to return if the specific field type has |
| 677 | // highlight enabled or if the general XFA field type has it enabled. |
| 678 | if (IsFormFieldTypeXFA(fieldType)) { |
| 679 | if (!m_NeedsHighlight[static_cast<size_t>(fieldType)]) |
| 680 | return m_NeedsHighlight[static_cast<size_t>(FormFieldType::kXFA)]; |
| 681 | } |
| 682 | #endif // PDF_ENABLE_XFA |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 683 | return m_NeedsHighlight[static_cast<size_t>(fieldType)]; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 686 | void CPDFSDK_InterForm::RemoveAllHighLights() { |
| 687 | std::fill(m_HighlightColor, m_HighlightColor + kFormFieldTypeCount, |
Lei Zhang | 4f7479a | 2018-03-21 13:37:06 +0000 | [diff] [blame] | 688 | kWhiteBGR); |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 689 | std::fill(m_NeedsHighlight, m_NeedsHighlight + kFormFieldTypeCount, false); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 692 | void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, |
| 693 | FormFieldType fieldType) { |
| 694 | if (fieldType == FormFieldType::kUnknown) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 695 | return; |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 696 | |
| 697 | m_HighlightColor[static_cast<size_t>(fieldType)] = clr; |
| 698 | m_NeedsHighlight[static_cast<size_t>(fieldType)] = true; |
| 699 | } |
| 700 | |
| 701 | void CPDFSDK_InterForm::SetAllHighlightColors(FX_COLORREF clr) { |
| 702 | for (auto type : kFormFieldTypes) { |
| 703 | m_HighlightColor[static_cast<size_t>(type)] = clr; |
| 704 | m_NeedsHighlight[static_cast<size_t>(type)] = true; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 708 | FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(FormFieldType fieldType) { |
| 709 | if (fieldType == FormFieldType::kUnknown) |
Lei Zhang | 4f7479a | 2018-03-21 13:37:06 +0000 | [diff] [blame] | 710 | return kWhiteBGR; |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 711 | |
Ryan Harrison | 2056fac | 2018-01-16 16:06:45 +0000 | [diff] [blame] | 712 | #ifdef PDF_ENABLE_XFA |
| 713 | // For the XFA fields, we need to return the specific field type highlight |
| 714 | // colour or the general XFA field type colour if present. |
| 715 | if (IsFormFieldTypeXFA(fieldType)) { |
| 716 | if (!m_NeedsHighlight[static_cast<size_t>(fieldType)] && |
| 717 | m_NeedsHighlight[static_cast<size_t>(FormFieldType::kXFA)]) { |
| 718 | return m_HighlightColor[static_cast<size_t>(FormFieldType::kXFA)]; |
| 719 | } |
| 720 | } |
| 721 | #endif // PDF_ENABLE_XFA |
Ryan Harrison | 9baf31f | 2018-01-12 18:36:30 +0000 | [diff] [blame] | 722 | return m_HighlightColor[static_cast<size_t>(fieldType)]; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 723 | } |