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