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