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 | |
| 7 | #include "fpdfsdk/include/cpdfsdk_interform.h" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <memory> |
| 11 | |
| 12 | #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" |
| 13 | #include "core/fpdfapi/fpdf_parser/include/cfdf_document.h" |
| 14 | #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 15 | #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 16 | #include "core/fpdfapi/fpdf_parser/include/cpdf_stream.h" |
| 17 | #include "core/fpdfdoc/include/cpdf_actionfields.h" |
| 18 | #include "core/fpdfdoc/include/cpdf_interform.h" |
| 19 | #include "core/fxge/include/cfx_graphstatedata.h" |
| 20 | #include "core/fxge/include/cfx_pathdata.h" |
| 21 | #include "core/fxge/include/cfx_renderdevice.h" |
| 22 | #include "fpdfsdk/formfiller/cffl_formfiller.h" |
| 23 | #include "fpdfsdk/fxedit/include/fxet_edit.h" |
| 24 | #include "fpdfsdk/include/cba_annotiterator.h" |
| 25 | #include "fpdfsdk/include/cpdfsdk_annot.h" |
| 26 | #include "fpdfsdk/include/cpdfsdk_widget.h" |
| 27 | #include "fpdfsdk/include/fsdk_actionhandler.h" |
| 28 | #include "fpdfsdk/include/fsdk_define.h" |
| 29 | #include "fpdfsdk/include/fsdk_mgr.h" |
| 30 | #include "fpdfsdk/include/ipdfsdk_annothandler.h" |
| 31 | #include "fpdfsdk/javascript/ijs_context.h" |
| 32 | #include "fpdfsdk/javascript/ijs_runtime.h" |
| 33 | #include "fpdfsdk/pdfwindow/PWL_Utils.h" |
| 34 | #include "third_party/base/stl_util.h" |
| 35 | |
| 36 | #ifdef PDF_ENABLE_XFA |
| 37 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_doc.h" |
| 38 | #include "fpdfsdk/fpdfxfa/include/fpdfxfa_util.h" |
| 39 | #include "fpdfsdk/include/cpdfsdk_xfawidget.h" |
| 40 | #include "xfa/fxfa/include/cxfa_eventparam.h" |
| 41 | #include "xfa/fxfa/include/xfa_ffdocview.h" |
| 42 | #include "xfa/fxfa/include/xfa_ffwidget.h" |
| 43 | #include "xfa/fxfa/include/xfa_ffwidgethandler.h" |
| 44 | #endif // PDF_ENABLE_XFA |
| 45 | |
| 46 | CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_Document* pDocument) |
| 47 | : m_pDocument(pDocument), |
| 48 | m_pInterForm(new CPDF_InterForm(m_pDocument->GetPDFDocument())), |
| 49 | #ifdef PDF_ENABLE_XFA |
| 50 | m_bXfaCalculate(TRUE), |
| 51 | m_bXfaValidationsEnabled(TRUE), |
| 52 | #endif // PDF_ENABLE_XFA |
| 53 | m_bCalculate(TRUE), |
| 54 | m_bBusy(FALSE), |
| 55 | m_iHighlightAlpha(0) { |
| 56 | m_pInterForm->SetFormNotify(this); |
| 57 | for (int i = 0; i < kNumFieldTypes; ++i) |
| 58 | m_bNeedHightlight[i] = FALSE; |
| 59 | } |
| 60 | |
| 61 | CPDFSDK_InterForm::~CPDFSDK_InterForm() { |
| 62 | m_Map.clear(); |
| 63 | #ifdef PDF_ENABLE_XFA |
| 64 | m_XFAMap.clear(); |
| 65 | #endif // PDF_ENABLE_XFA |
| 66 | } |
| 67 | |
| 68 | FX_BOOL CPDFSDK_InterForm::HighlightWidgets() { |
| 69 | return FALSE; |
| 70 | } |
| 71 | |
| 72 | CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget, |
| 73 | FX_BOOL bNext) const { |
| 74 | std::unique_ptr<CBA_AnnotIterator> pIterator( |
jaepark | 9ed9137 | 2016-08-26 16:16:10 -0700 | [diff] [blame^] | 75 | new CBA_AnnotIterator(pWidget->GetPageView(), "Widget")); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 76 | |
| 77 | if (bNext) |
| 78 | return static_cast<CPDFSDK_Widget*>(pIterator->GetNextAnnot(pWidget)); |
| 79 | |
| 80 | return static_cast<CPDFSDK_Widget*>(pIterator->GetPrevAnnot(pWidget)); |
| 81 | } |
| 82 | |
| 83 | CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl, |
| 84 | bool createIfNeeded) const { |
| 85 | if (!pControl || !m_pInterForm) |
| 86 | return nullptr; |
| 87 | |
| 88 | CPDFSDK_Widget* pWidget = nullptr; |
| 89 | const auto it = m_Map.find(pControl); |
| 90 | if (it != m_Map.end()) |
| 91 | pWidget = it->second; |
| 92 | if (pWidget) |
| 93 | return pWidget; |
| 94 | if (!createIfNeeded) |
| 95 | return nullptr; |
| 96 | |
| 97 | CPDF_Dictionary* pControlDict = pControl->GetWidget(); |
| 98 | CPDF_Document* pDocument = m_pDocument->GetPDFDocument(); |
| 99 | CPDFSDK_PageView* pPage = nullptr; |
| 100 | |
| 101 | if (CPDF_Dictionary* pPageDict = pControlDict->GetDictBy("P")) { |
| 102 | int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum()); |
| 103 | if (nPageIndex >= 0) |
| 104 | pPage = m_pDocument->GetPageView(nPageIndex); |
| 105 | } |
| 106 | |
| 107 | if (!pPage) { |
| 108 | int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict); |
| 109 | if (nPageIndex >= 0) |
| 110 | pPage = m_pDocument->GetPageView(nPageIndex); |
| 111 | } |
| 112 | |
| 113 | if (!pPage) |
| 114 | return nullptr; |
| 115 | |
| 116 | return static_cast<CPDFSDK_Widget*>(pPage->GetAnnotByDict(pControlDict)); |
| 117 | } |
| 118 | |
| 119 | void CPDFSDK_InterForm::GetWidgets( |
| 120 | const CFX_WideString& sFieldName, |
| 121 | std::vector<CPDFSDK_Widget*>* widgets) const { |
| 122 | for (int i = 0, sz = m_pInterForm->CountFields(sFieldName); i < sz; ++i) { |
| 123 | CPDF_FormField* pFormField = m_pInterForm->GetField(i, sFieldName); |
| 124 | ASSERT(pFormField); |
| 125 | GetWidgets(pFormField, widgets); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void CPDFSDK_InterForm::GetWidgets( |
| 130 | CPDF_FormField* pField, |
| 131 | std::vector<CPDFSDK_Widget*>* widgets) const { |
| 132 | for (int i = 0, sz = pField->CountControls(); i < sz; ++i) { |
| 133 | CPDF_FormControl* pFormCtrl = pField->GetControl(i); |
| 134 | ASSERT(pFormCtrl); |
| 135 | CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl, true); |
| 136 | if (pWidget) |
| 137 | widgets->push_back(pWidget); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | int CPDFSDK_InterForm::GetPageIndexByAnnotDict( |
| 142 | CPDF_Document* pDocument, |
| 143 | CPDF_Dictionary* pAnnotDict) const { |
| 144 | ASSERT(pAnnotDict); |
| 145 | |
| 146 | for (int i = 0, sz = pDocument->GetPageCount(); i < sz; i++) { |
| 147 | if (CPDF_Dictionary* pPageDict = pDocument->GetPage(i)) { |
| 148 | if (CPDF_Array* pAnnots = pPageDict->GetArrayBy("Annots")) { |
| 149 | for (int j = 0, jsz = pAnnots->GetCount(); j < jsz; j++) { |
| 150 | CPDF_Object* pDict = pAnnots->GetDirectObjectAt(j); |
| 151 | if (pAnnotDict == pDict) |
| 152 | return i; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl, |
| 162 | CPDFSDK_Widget* pWidget) { |
| 163 | m_Map[pControl] = pWidget; |
| 164 | } |
| 165 | |
| 166 | void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) { |
| 167 | m_Map.erase(pControl); |
| 168 | } |
| 169 | |
| 170 | void CPDFSDK_InterForm::EnableCalculate(FX_BOOL bEnabled) { |
| 171 | m_bCalculate = bEnabled; |
| 172 | } |
| 173 | |
| 174 | FX_BOOL CPDFSDK_InterForm::IsCalculateEnabled() const { |
| 175 | return m_bCalculate; |
| 176 | } |
| 177 | |
| 178 | #ifdef PDF_ENABLE_XFA |
| 179 | void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget, |
| 180 | CPDFSDK_XFAWidget* pWidget) { |
| 181 | ASSERT(hWidget); |
| 182 | m_XFAMap[hWidget] = pWidget; |
| 183 | } |
| 184 | |
| 185 | void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) { |
| 186 | ASSERT(hWidget); |
| 187 | m_XFAMap.erase(hWidget); |
| 188 | } |
| 189 | |
| 190 | CPDFSDK_XFAWidget* CPDFSDK_InterForm::GetXFAWidget(CXFA_FFWidget* hWidget) { |
| 191 | ASSERT(hWidget); |
| 192 | auto it = m_XFAMap.find(hWidget); |
| 193 | return it != m_XFAMap.end() ? it->second : nullptr; |
| 194 | } |
| 195 | |
| 196 | void CPDFSDK_InterForm::XfaEnableCalculate(FX_BOOL bEnabled) { |
| 197 | m_bXfaCalculate = bEnabled; |
| 198 | } |
| 199 | FX_BOOL CPDFSDK_InterForm::IsXfaCalculateEnabled() const { |
| 200 | return m_bXfaCalculate; |
| 201 | } |
| 202 | |
| 203 | FX_BOOL CPDFSDK_InterForm::IsXfaValidationsEnabled() { |
| 204 | return m_bXfaValidationsEnabled; |
| 205 | } |
| 206 | void CPDFSDK_InterForm::XfaSetValidationsEnabled(FX_BOOL bEnabled) { |
| 207 | m_bXfaValidationsEnabled = bEnabled; |
| 208 | } |
| 209 | |
| 210 | void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField, |
| 211 | FX_BOOL bSynchronizeElse) { |
| 212 | for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) { |
| 213 | CPDF_FormControl* pFormCtrl = pFormField->GetControl(i); |
| 214 | if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl, false)) |
| 215 | pWidget->Synchronize(bSynchronizeElse); |
| 216 | } |
| 217 | } |
| 218 | #endif // PDF_ENABLE_XFA |
| 219 | |
| 220 | void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { |
| 221 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 222 | ASSERT(pEnv); |
| 223 | if (!pEnv->IsJSInitiated()) |
| 224 | return; |
| 225 | |
| 226 | if (m_bBusy) |
| 227 | return; |
| 228 | |
| 229 | m_bBusy = TRUE; |
| 230 | |
| 231 | if (!IsCalculateEnabled()) { |
| 232 | m_bBusy = FALSE; |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime(); |
| 237 | pRuntime->SetReaderDocument(m_pDocument); |
| 238 | |
| 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, |
| 279 | FX_BOOL& bFormated) { |
| 280 | CFX_WideString sValue = pFormField->GetValue(); |
| 281 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 282 | ASSERT(pEnv); |
| 283 | if (!pEnv->IsJSInitiated()) { |
| 284 | bFormated = FALSE; |
| 285 | return sValue; |
| 286 | } |
| 287 | |
| 288 | IJS_Runtime* pRuntime = m_pDocument->GetJsRuntime(); |
| 289 | pRuntime->SetReaderDocument(m_pDocument); |
| 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 | |
| 298 | bFormated = FALSE; |
| 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; |
| 316 | bFormated = TRUE; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return sValue; |
| 323 | } |
| 324 | |
| 325 | void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField, |
| 326 | const FX_WCHAR* sValue, |
| 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)) { |
| 342 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 343 | CFFL_IFormFiller* pIFormFiller = pEnv->GetIFormFiller(); |
| 344 | UnderlyingPageType* pPage = pWidget->GetUnderlyingPage(); |
| 345 | CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false); |
| 346 | FX_RECT rcBBox = pIFormFiller->GetViewBBox(pPageView, pWidget); |
| 347 | |
| 348 | pEnv->FFI_Invalidate(pPage, rcBBox.left, rcBBox.top, rcBBox.right, |
| 349 | rcBBox.bottom); |
| 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 | |
| 364 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 365 | CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander(); |
| 366 | PDFSDK_FieldAction fa; |
| 367 | fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0); |
| 368 | fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0); |
| 369 | fa.sValue = csValue; |
| 370 | pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::KeyStroke, |
| 371 | m_pDocument, pFormField, fa); |
| 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 | |
| 385 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 386 | CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander(); |
| 387 | PDFSDK_FieldAction fa; |
| 388 | fa.bModifier = pEnv->FFI_IsCTRLKeyDown(0); |
| 389 | fa.bShift = pEnv->FFI_IsSHIFTKeyDown(0); |
| 390 | fa.sValue = csValue; |
| 391 | pActionHandler->DoAction_FieldJavaScript(action, CPDF_AAction::Validate, |
| 392 | m_pDocument, pFormField, fa); |
| 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) { |
| 459 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 460 | |
| 461 | CFX_ByteTextBuf textBuf; |
| 462 | ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude, textBuf); |
| 463 | |
| 464 | uint8_t* pBuffer = textBuf.GetBuffer(); |
| 465 | FX_STRSIZE nBufSize = textBuf.GetLength(); |
| 466 | |
| 467 | if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) |
| 468 | return FALSE; |
| 469 | |
| 470 | pEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str()); |
| 471 | return TRUE; |
| 472 | } |
| 473 | |
| 474 | FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(CFX_WideString csFDFFile, |
| 475 | CFX_WideString csTxtFile) { |
| 476 | return TRUE; |
| 477 | } |
| 478 | |
| 479 | FX_BOOL CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, |
| 480 | FX_STRSIZE& nBufSize) { |
| 481 | CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize); |
| 482 | if (!pFDF) |
| 483 | return TRUE; |
| 484 | |
| 485 | CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictBy("FDF"); |
| 486 | if (!pMainDict) |
| 487 | return FALSE; |
| 488 | |
| 489 | CPDF_Array* pFields = pMainDict->GetArrayBy("Fields"); |
| 490 | if (!pFields) |
| 491 | return FALSE; |
| 492 | |
| 493 | CFX_ByteTextBuf fdfEncodedData; |
| 494 | for (uint32_t i = 0; i < pFields->GetCount(); i++) { |
| 495 | CPDF_Dictionary* pField = pFields->GetDictAt(i); |
| 496 | if (!pField) |
| 497 | continue; |
| 498 | CFX_WideString name; |
| 499 | name = pField->GetUnicodeTextBy("T"); |
| 500 | CFX_ByteString name_b = CFX_ByteString::FromUnicode(name); |
| 501 | CFX_ByteString csBValue = pField->GetStringBy("V"); |
| 502 | CFX_WideString csWValue = PDF_DecodeText(csBValue); |
| 503 | CFX_ByteString csValue_b = CFX_ByteString::FromUnicode(csWValue); |
| 504 | |
| 505 | fdfEncodedData << name_b.GetBuffer(name_b.GetLength()); |
| 506 | name_b.ReleaseBuffer(); |
| 507 | fdfEncodedData << "="; |
| 508 | fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength()); |
| 509 | csValue_b.ReleaseBuffer(); |
| 510 | if (i != pFields->GetCount() - 1) |
| 511 | fdfEncodedData << "&"; |
| 512 | } |
| 513 | |
| 514 | nBufSize = fdfEncodedData.GetLength(); |
| 515 | pBuf = FX_Alloc(uint8_t, nBufSize); |
| 516 | FXSYS_memcpy(pBuf, fdfEncodedData.GetBuffer(), nBufSize); |
| 517 | return TRUE; |
| 518 | } |
| 519 | |
| 520 | FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf( |
| 521 | const std::vector<CPDF_FormField*>& fields, |
| 522 | bool bIncludeOrExclude, |
| 523 | CFX_ByteTextBuf& textBuf) { |
| 524 | std::unique_ptr<CFDF_Document> pFDF(m_pInterForm->ExportToFDF( |
| 525 | m_pDocument->GetPath().AsStringC(), fields, bIncludeOrExclude)); |
| 526 | return pFDF ? pFDF->WriteBuf(textBuf) : FALSE; |
| 527 | } |
| 528 | |
| 529 | CFX_WideString CPDFSDK_InterForm::GetTemporaryFileName( |
| 530 | const CFX_WideString& sFileExt) { |
| 531 | return L""; |
| 532 | } |
| 533 | |
| 534 | FX_BOOL CPDFSDK_InterForm::SubmitForm(const CFX_WideString& sDestination, |
| 535 | FX_BOOL bUrlEncoded) { |
| 536 | if (sDestination.IsEmpty()) |
| 537 | return FALSE; |
| 538 | |
| 539 | if (!m_pDocument || !m_pInterForm) |
| 540 | return FALSE; |
| 541 | |
| 542 | CPDFDoc_Environment* pEnv = m_pDocument->GetEnv(); |
| 543 | CFX_WideString wsPDFFilePath = m_pDocument->GetPath(); |
| 544 | CFDF_Document* pFDFDoc = m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC()); |
| 545 | if (!pFDFDoc) |
| 546 | return FALSE; |
| 547 | |
| 548 | CFX_ByteTextBuf FdfBuffer; |
| 549 | FX_BOOL bRet = pFDFDoc->WriteBuf(FdfBuffer); |
| 550 | delete pFDFDoc; |
| 551 | if (!bRet) |
| 552 | return FALSE; |
| 553 | |
| 554 | uint8_t* pBuffer = FdfBuffer.GetBuffer(); |
| 555 | FX_STRSIZE nBufSize = FdfBuffer.GetLength(); |
| 556 | |
| 557 | if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) |
| 558 | return FALSE; |
| 559 | |
| 560 | pEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str()); |
| 561 | |
| 562 | if (bUrlEncoded) |
| 563 | FX_Free(pBuffer); |
| 564 | |
| 565 | return TRUE; |
| 566 | } |
| 567 | |
| 568 | FX_BOOL CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) { |
| 569 | CFDF_Document* pFDF = |
| 570 | m_pInterForm->ExportToFDF(m_pDocument->GetPath().AsStringC()); |
| 571 | if (!pFDF) |
| 572 | return FALSE; |
| 573 | |
| 574 | FX_BOOL bRet = pFDF->WriteBuf(textBuf); |
| 575 | delete pFDF; |
| 576 | |
| 577 | return bRet; |
| 578 | } |
| 579 | |
| 580 | FX_BOOL CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) { |
| 581 | ASSERT(action.GetDict()); |
| 582 | |
| 583 | CPDF_Dictionary* pActionDict = action.GetDict(); |
| 584 | if (!pActionDict->KeyExist("Fields")) |
| 585 | return m_pInterForm->ResetForm(true); |
| 586 | |
| 587 | CPDF_ActionFields af(&action); |
| 588 | uint32_t dwFlags = action.GetFlags(); |
| 589 | |
| 590 | std::vector<CPDF_Object*> fieldObjects = af.GetAllFields(); |
| 591 | std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects); |
| 592 | return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true); |
| 593 | } |
| 594 | |
| 595 | FX_BOOL CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) { |
| 596 | return FALSE; |
| 597 | } |
| 598 | |
| 599 | std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects( |
| 600 | const std::vector<CPDF_Object*>& objects) const { |
| 601 | std::vector<CPDF_FormField*> fields; |
| 602 | for (CPDF_Object* pObject : objects) { |
| 603 | if (pObject && pObject->IsString()) { |
| 604 | CFX_WideString csName = pObject->GetUnicodeText(); |
| 605 | CPDF_FormField* pField = m_pInterForm->GetField(0, csName); |
| 606 | if (pField) |
| 607 | fields.push_back(pField); |
| 608 | } |
| 609 | } |
| 610 | return fields; |
| 611 | } |
| 612 | |
| 613 | int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField, |
| 614 | const CFX_WideString& csValue) { |
| 615 | int nType = pField->GetFieldType(); |
| 616 | if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD) |
| 617 | return 0; |
| 618 | |
| 619 | if (!OnKeyStrokeCommit(pField, csValue)) |
| 620 | return -1; |
| 621 | |
| 622 | if (!OnValidate(pField, csValue)) |
| 623 | return -1; |
| 624 | |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) { |
| 629 | #ifdef PDF_ENABLE_XFA |
| 630 | SynchronizeField(pField, FALSE); |
| 631 | #endif // PDF_ENABLE_XFA |
| 632 | int nType = pField->GetFieldType(); |
| 633 | if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) { |
| 634 | OnCalculate(pField); |
| 635 | FX_BOOL bFormated = FALSE; |
| 636 | CFX_WideString sValue = OnFormat(pField, bFormated); |
| 637 | ResetFieldAppearance(pField, bFormated ? sValue.c_str() : nullptr, TRUE); |
| 638 | UpdateField(pField); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField, |
| 643 | const CFX_WideString& csValue) { |
| 644 | if (pField->GetFieldType() != FIELDTYPE_LISTBOX) |
| 645 | return 0; |
| 646 | |
| 647 | if (!OnKeyStrokeCommit(pField, csValue)) |
| 648 | return -1; |
| 649 | |
| 650 | if (!OnValidate(pField, csValue)) |
| 651 | return -1; |
| 652 | |
| 653 | return 1; |
| 654 | } |
| 655 | |
| 656 | void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) { |
| 657 | if (pField->GetFieldType() != FIELDTYPE_LISTBOX) |
| 658 | return; |
| 659 | |
| 660 | OnCalculate(pField); |
| 661 | ResetFieldAppearance(pField, nullptr, TRUE); |
| 662 | UpdateField(pField); |
| 663 | } |
| 664 | |
| 665 | void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) { |
| 666 | int nType = pField->GetFieldType(); |
| 667 | if (nType != FIELDTYPE_CHECKBOX && nType != FIELDTYPE_RADIOBUTTON) |
| 668 | return; |
| 669 | |
| 670 | OnCalculate(pField); |
| 671 | UpdateField(pField); |
| 672 | } |
| 673 | |
| 674 | int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) { |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) { |
| 679 | OnCalculate(nullptr); |
| 680 | } |
| 681 | |
| 682 | int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) { |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) { |
| 687 | OnCalculate(nullptr); |
| 688 | } |
| 689 | |
| 690 | FX_BOOL CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) { |
| 691 | if (nFieldType < 1 || nFieldType > kNumFieldTypes) |
| 692 | return FALSE; |
| 693 | return m_bNeedHightlight[nFieldType - 1]; |
| 694 | } |
| 695 | |
| 696 | void CPDFSDK_InterForm::RemoveAllHighLight() { |
| 697 | for (int i = 0; i < kNumFieldTypes; ++i) |
| 698 | m_bNeedHightlight[i] = FALSE; |
| 699 | } |
| 700 | |
| 701 | void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) { |
| 702 | if (nFieldType < 0 || nFieldType > kNumFieldTypes) |
| 703 | return; |
| 704 | switch (nFieldType) { |
| 705 | case 0: { |
| 706 | for (int i = 0; i < kNumFieldTypes; ++i) { |
| 707 | m_aHighlightColor[i] = clr; |
| 708 | m_bNeedHightlight[i] = TRUE; |
| 709 | } |
| 710 | break; |
| 711 | } |
| 712 | default: { |
| 713 | m_aHighlightColor[nFieldType - 1] = clr; |
| 714 | m_bNeedHightlight[nFieldType - 1] = TRUE; |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) { |
| 721 | if (nFieldType < 0 || nFieldType > kNumFieldTypes) |
| 722 | return FXSYS_RGB(255, 255, 255); |
| 723 | if (nFieldType == 0) |
| 724 | return m_aHighlightColor[0]; |
| 725 | return m_aHighlightColor[nFieldType - 1]; |
| 726 | } |