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(); |
| 239 | pRuntime->SetReaderDocument(m_pFormFillEnv->GetSDKDocument()); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 240 | |
| 241 | int nSize = m_pInterForm->CountFieldsInCalculationOrder(); |
| 242 | for (int i = 0; i < nSize; i++) { |
| 243 | CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i); |
| 244 | if (!pField) |
| 245 | continue; |
| 246 | |
| 247 | int nType = pField->GetFieldType(); |
| 248 | if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD) |
| 249 | continue; |
| 250 | |
| 251 | CPDF_AAction aAction = pField->GetAdditionalAction(); |
| 252 | if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Calculate)) |
| 253 | continue; |
| 254 | |
| 255 | CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate); |
| 256 | if (!action.GetDict()) |
| 257 | continue; |
| 258 | |
| 259 | CFX_WideString csJS = action.GetJavaScript(); |
| 260 | if (csJS.IsEmpty()) |
| 261 | continue; |
| 262 | |
| 263 | IJS_Context* pContext = pRuntime->NewContext(); |
| 264 | CFX_WideString sOldValue = pField->GetValue(); |
| 265 | CFX_WideString sValue = sOldValue; |
| 266 | FX_BOOL bRC = TRUE; |
| 267 | pContext->OnField_Calculate(pFormField, pField, sValue, bRC); |
| 268 | |
| 269 | CFX_WideString sInfo; |
| 270 | FX_BOOL bRet = pContext->RunScript(csJS, &sInfo); |
| 271 | pRuntime->ReleaseContext(pContext); |
| 272 | |
| 273 | if (bRet && bRC && sValue.Compare(sOldValue) != 0) |
| 274 | pField->SetValue(sValue, TRUE); |
| 275 | } |
| 276 | |
| 277 | m_bBusy = FALSE; |
| 278 | } |
| 279 | |
| 280 | CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, |
tsepez | 8c2a8cd | 2016-09-07 15:29:11 -0700 | [diff] [blame] | 281 | FX_BOOL& bFormatted) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 282 | CFX_WideString sValue = pFormField->GetValue(); |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 283 | if (!m_pFormFillEnv->IsJSInitiated()) { |
tsepez | 8c2a8cd | 2016-09-07 15:29:11 -0700 | [diff] [blame] | 284 | bFormatted = FALSE; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 285 | return sValue; |
| 286 | } |
| 287 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 288 | IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime(); |
| 289 | pRuntime->SetReaderDocument(m_pFormFillEnv->GetSDKDocument()); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX && |
| 292 | pFormField->CountSelectedItems() > 0) { |
| 293 | int index = pFormField->GetSelectedIndex(0); |
| 294 | if (index >= 0) |
| 295 | sValue = pFormField->GetOptionLabel(index); |
| 296 | } |
| 297 | |
tsepez | 8c2a8cd | 2016-09-07 15:29:11 -0700 | [diff] [blame] | 298 | bFormatted = FALSE; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 299 | |
| 300 | CPDF_AAction aAction = pFormField->GetAdditionalAction(); |
| 301 | if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) { |
| 302 | CPDF_Action action = aAction.GetAction(CPDF_AAction::Format); |
| 303 | if (action.GetDict()) { |
| 304 | CFX_WideString script = action.GetJavaScript(); |
| 305 | if (!script.IsEmpty()) { |
| 306 | CFX_WideString Value = sValue; |
| 307 | |
| 308 | IJS_Context* pContext = pRuntime->NewContext(); |
| 309 | pContext->OnField_Format(pFormField, Value, TRUE); |
| 310 | CFX_WideString sInfo; |
| 311 | FX_BOOL bRet = pContext->RunScript(script, &sInfo); |
| 312 | pRuntime->ReleaseContext(pContext); |
| 313 | |
| 314 | if (bRet) { |
| 315 | sValue = Value; |
tsepez | 8c2a8cd | 2016-09-07 15:29:11 -0700 | [diff] [blame] | 316 | bFormatted = TRUE; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return sValue; |
| 323 | } |
| 324 | |
| 325 | void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField, |
tsepez | a31da74 | 2016-09-08 11:28:14 -0700 | [diff] [blame] | 326 | const CFX_WideString* sValue, |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 327 | FX_BOOL bValueChanged) { |
| 328 | for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { |
| 329 | CPDF_FormControl* pFormCtrl = pFormField->GetControl(i); |
| 330 | ASSERT(pFormCtrl); |
| 331 | if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl, false)) |
| 332 | pWidget->ResetAppearance(sValue, bValueChanged); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) { |
| 337 | for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { |
| 338 | CPDF_FormControl* pFormCtrl = pFormField->GetControl(i); |
| 339 | ASSERT(pFormCtrl); |
| 340 | |
| 341 | if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl, false)) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 342 | UnderlyingPageType* pPage = pWidget->GetUnderlyingPage(); |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 343 | CPDFSDK_PageView* pPageView = |
| 344 | m_pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false); |
| 345 | FX_RECT rcBBox = m_pFormFillEnv->GetInteractiveFormFiller()->GetViewBBox( |
| 346 | pPageView, pWidget); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 347 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 348 | m_pFormFillEnv->Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right, |
| 349 | rcBBox.bottom); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | FX_BOOL CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField, |
| 355 | const CFX_WideString& csValue) { |
| 356 | CPDF_AAction aAction = pFormField->GetAdditionalAction(); |
| 357 | if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke)) |
| 358 | return TRUE; |
| 359 | |
| 360 | CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke); |
| 361 | if (!action.GetDict()) |
| 362 | return TRUE; |
| 363 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 364 | CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHander(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 365 | PDFSDK_FieldAction fa; |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 366 | fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(0); |
| 367 | fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(0); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 368 | fa.sValue = csValue; |
| 369 | pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke, |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 370 | m_pFormFillEnv->GetSDKDocument(), |
| 371 | pFormField, fa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 372 | return fa.bRC; |
| 373 | } |
| 374 | |
| 375 | FX_BOOL CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField, |
| 376 | const CFX_WideString& csValue) { |
| 377 | CPDF_AAction aAction = pFormField->GetAdditionalAction(); |
| 378 | if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate)) |
| 379 | return TRUE; |
| 380 | |
| 381 | CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate); |
| 382 | if (!action.GetDict()) |
| 383 | return TRUE; |
| 384 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 385 | CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHander(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 386 | PDFSDK_FieldAction fa; |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 387 | fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(0); |
| 388 | fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(0); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 389 | fa.sValue = csValue; |
| 390 | pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate, |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 391 | m_pFormFillEnv->GetSDKDocument(), |
| 392 | pFormField, fa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 393 | return fa.bRC; |
| 394 | } |
| 395 | |
| 396 | FX_BOOL CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) { |
| 397 | ASSERT(action.GetDict()); |
| 398 | |
| 399 | CPDF_ActionFields af(&action); |
| 400 | std::vector<CPDF_Object*> fieldObjects = af.GetAllFields(); |
| 401 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
| 402 | |
| 403 | bool bHide = action.GetHideStatus(); |
| 404 | FX_BOOL bChanged = FALSE; |
| 405 | |
| 406 | for (CPDF_FormField* pField : fields) { |
| 407 | for (int i = 0, sz = pField->CountControls(); i < sz; ++i) { |
| 408 | CPDF_FormControl* pControl = pField->GetControl(i); |
| 409 | ASSERT(pControl); |
| 410 | |
| 411 | if (CPDFSDK_Widget* pWidget = GetWidget(pControl, false)) { |
| 412 | uint32_t nFlags = pWidget->GetFlags(); |
| 413 | nFlags &= ~ANNOTFLAG_INVISIBLE; |
| 414 | nFlags &= ~ANNOTFLAG_NOVIEW; |
| 415 | if (bHide) |
| 416 | nFlags |= ANNOTFLAG_HIDDEN; |
| 417 | else |
| 418 | nFlags &= ~ANNOTFLAG_HIDDEN; |
| 419 | pWidget->SetFlags(nFlags); |
| 420 | pWidget->GetPageView()->UpdateView(pWidget); |
| 421 | bChanged = TRUE; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return bChanged; |
| 427 | } |
| 428 | |
| 429 | FX_BOOL CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) { |
| 430 | CFX_WideString sDestination = action.GetFilePath(); |
| 431 | if (sDestination.IsEmpty()) |
| 432 | return FALSE; |
| 433 | |
| 434 | CPDF_Dictionary* pActionDict = action.GetDict(); |
| 435 | if (pActionDict->KeyExist("Fields")) { |
| 436 | CPDF_ActionFields af(&action); |
| 437 | uint32_t dwFlags = action.GetFlags(); |
| 438 | std::vector<CPDF_Object*> fieldObjects = af.GetAllFields(); |
| 439 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
| 440 | if (!fields.empty()) { |
| 441 | bool bIncludeOrExclude = !(dwFlags & 0x01); |
| 442 | if (m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude)) |
| 443 | return FALSE; |
| 444 | |
| 445 | return SubmitFields(sDestination, fields, bIncludeOrExclude, false); |
| 446 | } |
| 447 | } |
| 448 | if (m_pInterForm->CheckRequiredFields(nullptr, true)) |
| 449 | return FALSE; |
| 450 | |
| 451 | return SubmitForm(sDestination, FALSE); |
| 452 | } |
| 453 | |
| 454 | FX_BOOL CPDFSDK_InterForm::SubmitFields( |
| 455 | const CFX_WideString& csDestination, |
| 456 | const std::vector<CPDF_FormField*>& fields, |
| 457 | bool bIncludeOrExclude, |
| 458 | bool bUrlEncoded) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 459 | CFX_ByteTextBuf textBuf; |
| 460 | ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf); |
| 461 | |
| 462 | uint8_t* pBuffer = textBuf.GetBuffer(); |
| 463 | FX_STRSIZE nBufSize = textBuf.GetLength(); |
| 464 | |
| 465 | if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) |
| 466 | return FALSE; |
| 467 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 468 | m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str()); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 469 | return TRUE; |
| 470 | } |
| 471 | |
| 472 | FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile, |
| 473 | CFX_WideString csTxtFile) { |
| 474 | return TRUE; |
| 475 | } |
| 476 | |
| 477 | FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, |
| 478 | FX_STRSIZE& nBufSize) { |
| 479 | CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize); |
| 480 | if (!pFDF) |
| 481 | return TRUE; |
| 482 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 483 | CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 484 | if (!pMainDict) |
| 485 | return FALSE; |
| 486 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 487 | CPDF_Array* pFields = pMainDict->GetArrayFor("Fields"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 488 | if (!pFields) |
| 489 | return FALSE; |
| 490 | |
| 491 | CFX_ByteTextBuf fdfEncodedData; |
| 492 | for (uint32_t i = 0; i < pFields->GetCount(); i++) { |
| 493 | CPDF_Dictionary* pField = pFields->GetDictAt(i); |
| 494 | if (!pField) |
| 495 | continue; |
| 496 | CFX_WideString name; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 497 | name = pField->GetUnicodeTextFor("T"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 498 | CFX_ByteString name_b = CFX_ByteString::FromUnicode(name); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 499 | CFX_ByteString csBValue = pField->GetStringFor("V"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 500 | CFX_WideString csWValue = PDF_DecodeText(csBValue); |
| 501 | CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue); |
| 502 | |
| 503 | fdfEncodedData << name_b.GetBuffer(name_b.GetLength()); |
| 504 | name_b.ReleaseBuffer(); |
| 505 | fdfEncodedData << "="; |
| 506 | fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength()); |
| 507 | csValue_b.ReleaseBuffer(); |
| 508 | if (i != pFields->GetCount() - 1) |
| 509 | fdfEncodedData << "&"; |
| 510 | } |
| 511 | |
| 512 | nBufSize = fdfEncodedData.GetLength(); |
| 513 | pBuf = FX_Alloc(uint8_t, nBufSize); |
| 514 | FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize); |
| 515 | return TRUE; |
| 516 | } |
| 517 | |
| 518 | FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf( |
| 519 | const std::vector<CPDF_FormField*>& fields, |
| 520 | bool bIncludeOrExclude, |
| 521 | CFX_ByteTextBuf& textBuf) { |
| 522 | std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF( |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 523 | m_pFormFillEnv->GetSDKDocument()->GetPath().AsStringC(), fields, |
| 524 | bIncludeOrExclude, false)); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 525 | return pFDF ? pFDF->WriteBuf(textBuf) : FALSE; |
| 526 | } |
| 527 | |
| 528 | CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName( |
| 529 | const CFX_WideString& sFileExt) { |
| 530 | return L""; |
| 531 | } |
| 532 | |
| 533 | FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, |
| 534 | FX_BOOL bUrlEncoded) { |
| 535 | if (sDestination.IsEmpty()) |
| 536 | return FALSE; |
| 537 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 538 | if (!m_pFormFillEnv || !m_pFormFillEnv->GetSDKDocument() || !m_pInterForm) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 539 | return FALSE; |
| 540 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 541 | CFX_WideString wsPDFFilePath = m_pFormFillEnv->GetSDKDocument()->GetPath(); |
thestig | 7c292e0 | 2016-09-28 14:14:26 -0700 | [diff] [blame] | 542 | CFDF_Document* pFDFDoc = |
| 543 | m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC(), false); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 544 | if (!pFDFDoc) |
| 545 | return FALSE; |
| 546 | |
| 547 | CFX_ByteTextBuf FdfBuffer; |
| 548 | FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer); |
| 549 | delete pFDFDoc; |
| 550 | if (!bRet) |
| 551 | return FALSE; |
| 552 | |
| 553 | uint8_t* pBuffer = FdfBuffer.GetBuffer(); |
| 554 | FX_STRSIZE nBufSize = FdfBuffer.GetLength(); |
| 555 | |
| 556 | if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) |
| 557 | return FALSE; |
| 558 | |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 559 | m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str()); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 560 | |
| 561 | if (bUrlEncoded) |
| 562 | FX_Free(pBuffer); |
| 563 | |
| 564 | return TRUE; |
| 565 | } |
| 566 | |
| 567 | FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) { |
dsinclair | 690c033 | 2016-10-11 09:13:01 -0700 | [diff] [blame^] | 568 | CFDF_Document* pFDF = m_pInterForm->ExportToFDF( |
| 569 | m_pFormFillEnv->GetSDKDocument()->GetPath().AsStringC(), false); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 570 | if (!pFDF) |
| 571 | return FALSE; |
| 572 | |
| 573 | FX_BOOL bRet = pFDF->WriteBuf(textBuf); |
| 574 | delete pFDF; |
| 575 | |
| 576 | return bRet; |
| 577 | } |
| 578 | |
| 579 | FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) { |
| 580 | ASSERT(action.GetDict()); |
| 581 | |
| 582 | CPDF_Dictionary* pActionDict = action.GetDict(); |
| 583 | if (!pActionDict->KeyExist("Fields")) |
| 584 | return m_pInterForm->ResetForm(true); |
| 585 | |
| 586 | CPDF_ActionFields af(&action); |
| 587 | uint32_t dwFlags = action.GetFlags(); |
| 588 | |
| 589 | std::vector<CPDF_Object*> fieldObjects = af.GetAllFields(); |
| 590 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
| 591 | return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true); |
| 592 | } |
| 593 | |
| 594 | FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) { |
| 595 | return FALSE; |
| 596 | } |
| 597 | |
| 598 | std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects( |
| 599 | const std::vector<CPDF_Object*>& objects) const { |
| 600 | std::vector<CPDF_FormField*> fields; |
| 601 | for (CPDF_Object* pObject : objects) { |
| 602 | if (pObject && pObject->IsString()) { |
| 603 | CFX_WideString csName = pObject->GetUnicodeText(); |
| 604 | CPDF_FormField* pField = m_pInterForm->GetField(0, csName); |
| 605 | if (pField) |
| 606 | fields.push_back(pField); |
| 607 | } |
| 608 | } |
| 609 | return fields; |
| 610 | } |
| 611 | |
| 612 | int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField, |
| 613 | const CFX_WideString& csValue) { |
| 614 | int nType = pField->GetFieldType(); |
| 615 | if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD) |
| 616 | return 0; |
| 617 | |
| 618 | if (!OnKeyStrokeCommit(pField, csValue)) |
| 619 | return -1; |
| 620 | |
| 621 | if (!OnValidate(pField, csValue)) |
| 622 | return -1; |
| 623 | |
| 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) { |
| 628 | #ifdef PDF_ENABLE_XFA |
| 629 | SynchronizeField(pField, FALSE); |
| 630 | #endif // PDF_ENABLE_XFA |
| 631 | int nType = pField->GetFieldType(); |
| 632 | if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) { |
| 633 | OnCalculate(pField); |
tsepez | 8c2a8cd | 2016-09-07 15:29:11 -0700 | [diff] [blame] | 634 | FX_BOOL bFormatted = FALSE; |
| 635 | CFX_WideString sValue = OnFormat(pField, bFormatted); |
tsepez | a31da74 | 2016-09-08 11:28:14 -0700 | [diff] [blame] | 636 | ResetFieldAppearance(pField, bFormatted ? &sValue : nullptr, TRUE); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 637 | UpdateField(pField); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField, |
| 642 | const CFX_WideString& csValue) { |
| 643 | if (pField->GetFieldType() != FIELDTYPE_LISTBOX) |
| 644 | return 0; |
| 645 | |
| 646 | if (!OnKeyStrokeCommit(pField, csValue)) |
| 647 | return -1; |
| 648 | |
| 649 | if (!OnValidate(pField, csValue)) |
| 650 | return -1; |
| 651 | |
| 652 | return 1; |
| 653 | } |
| 654 | |
| 655 | void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) { |
| 656 | if (pField->GetFieldType() != FIELDTYPE_LISTBOX) |
| 657 | return; |
| 658 | |
| 659 | OnCalculate(pField); |
| 660 | ResetFieldAppearance(pField, nullptr, TRUE); |
| 661 | UpdateField(pField); |
| 662 | } |
| 663 | |
| 664 | void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) { |
| 665 | int nType = pField->GetFieldType(); |
| 666 | if (nType != FIELDTYPE_CHECKBOX && nType != FIELDTYPE_RADIOBUTTON) |
| 667 | return; |
| 668 | |
| 669 | OnCalculate(pField); |
| 670 | UpdateField(pField); |
| 671 | } |
| 672 | |
| 673 | int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) { |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) { |
| 678 | OnCalculate(nullptr); |
| 679 | } |
| 680 | |
| 681 | int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) { |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) { |
| 686 | OnCalculate(nullptr); |
| 687 | } |
| 688 | |
| 689 | FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) { |
| 690 | if (nFieldType < 1 || nFieldType > kNumFieldTypes) |
| 691 | return FALSE; |
| 692 | return m_bNeedHightlight[nFieldType - 1]; |
| 693 | } |
| 694 | |
| 695 | void CPDFSDK_InterForm::RemoveAllHighLight() { |
| 696 | for (int i = 0; i < kNumFieldTypes; ++i) |
| 697 | m_bNeedHightlight[i] = FALSE; |
| 698 | } |
| 699 | |
| 700 | void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) { |
| 701 | if (nFieldType < 0 || nFieldType > kNumFieldTypes) |
| 702 | return; |
| 703 | switch (nFieldType) { |
| 704 | case 0: { |
| 705 | for (int i = 0; i < kNumFieldTypes; ++i) { |
| 706 | m_aHighlightColor[i] = clr; |
| 707 | m_bNeedHightlight[i] = TRUE; |
| 708 | } |
| 709 | break; |
| 710 | } |
| 711 | default: { |
| 712 | m_aHighlightColor[nFieldType - 1] = clr; |
| 713 | m_bNeedHightlight[nFieldType - 1] = TRUE; |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) { |
| 720 | if (nFieldType < 0 || nFieldType > kNumFieldTypes) |
| 721 | return FXSYS_RGB(255, 255, 255); |
| 722 | if (nFieldType == 0) |
| 723 | return m_aHighlightColor[0]; |
| 724 | return m_aHighlightColor[nFieldType - 1]; |
| 725 | } |