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