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