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