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