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