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