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