blob: 635655c0337adbbea824bb0afa14c12098d04ac8 [file] [log] [blame]
jaepark611adb82016-08-17 11:34:36 -07001// 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
dsinclair114e46a2016-09-29 17:18:21 -07007#include "fpdfsdk/cpdfsdk_interform.h"
jaepark611adb82016-08-17 11:34:36 -07008
9#include <algorithm>
10#include <memory>
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -040011#include <sstream>
12#include <string>
thestig7c292e02016-09-28 14:14:26 -070013#include <vector>
jaepark611adb82016-08-17 11:34:36 -070014
dsinclair41872fa2016-10-04 11:29:35 -070015#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070016#include "core/fpdfapi/parser/cfdf_document.h"
17#include "core/fpdfapi/parser/cpdf_array.h"
18#include "core/fpdfapi/parser/cpdf_document.h"
19#include "core/fpdfapi/parser/cpdf_stream.h"
dsinclair1727aee2016-09-29 13:12:56 -070020#include "core/fpdfdoc/cpdf_actionfields.h"
21#include "core/fpdfdoc/cpdf_interform.h"
dsinclair74a34fc2016-09-29 16:41:42 -070022#include "core/fxge/cfx_graphstatedata.h"
23#include "core/fxge/cfx_pathdata.h"
24#include "core/fxge/cfx_renderdevice.h"
dsinclair114e46a2016-09-29 17:18:21 -070025#include "fpdfsdk/cba_annotiterator.h"
26#include "fpdfsdk/cpdfsdk_annot.h"
dsinclair735606d2016-10-05 15:47:02 -070027#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070028#include "fpdfsdk/cpdfsdk_pageview.h"
29#include "fpdfsdk/cpdfsdk_widget.h"
jaepark611adb82016-08-17 11:34:36 -070030#include "fpdfsdk/formfiller/cffl_formfiller.h"
dsinclair114e46a2016-09-29 17:18:21 -070031#include "fpdfsdk/fsdk_actionhandler.h"
32#include "fpdfsdk/fsdk_define.h"
dsinclair114e46a2016-09-29 17:18:21 -070033#include "fpdfsdk/ipdfsdk_annothandler.h"
Tom Sepezd6ae2af2017-02-16 11:49:55 -080034#include "fpdfsdk/javascript/ijs_event_context.h"
jaepark611adb82016-08-17 11:34:36 -070035#include "fpdfsdk/javascript/ijs_runtime.h"
jaepark611adb82016-08-17 11:34:36 -070036#include "third_party/base/stl_util.h"
37
38#ifdef PDF_ENABLE_XFA
dsinclair114e46a2016-09-29 17:18:21 -070039#include "fpdfsdk/cpdfsdk_xfawidget.h"
dsinclair521b7502016-11-02 13:02:28 -070040#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070041#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
dsinclair5b493092016-09-29 20:20:24 -070042#include "xfa/fxfa/cxfa_eventparam.h"
Dan Sinclair80c48782017-03-23 12:11:20 -040043#include "xfa/fxfa/cxfa_ffdocview.h"
44#include "xfa/fxfa/cxfa_ffwidget.h"
45#include "xfa/fxfa/cxfa_ffwidgethandler.h"
jaepark611adb82016-08-17 11:34:36 -070046#endif // PDF_ENABLE_XFA
47
dsinclair690c0332016-10-11 09:13:01 -070048CPDFSDK_InterForm::CPDFSDK_InterForm(CPDFSDK_FormFillEnvironment* pFormFillEnv)
49 : m_pFormFillEnv(pFormFillEnv),
Dan Sinclair0bb13332017-03-30 16:12:02 -040050 m_pInterForm(
51 pdfium::MakeUnique<CPDF_InterForm>(m_pFormFillEnv->GetPDFDocument())),
jaepark611adb82016-08-17 11:34:36 -070052#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -070053 m_bXfaCalculate(true),
54 m_bXfaValidationsEnabled(true),
jaepark611adb82016-08-17 11:34:36 -070055#endif // PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -070056 m_bCalculate(true),
57 m_bBusy(false),
jaepark611adb82016-08-17 11:34:36 -070058 m_iHighlightAlpha(0) {
59 m_pInterForm->SetFormNotify(this);
60 for (int i = 0; i < kNumFieldTypes; ++i)
tsepez4cf55152016-11-02 14:37:54 -070061 m_bNeedHightlight[i] = false;
jaepark611adb82016-08-17 11:34:36 -070062}
63
64CPDFSDK_InterForm::~CPDFSDK_InterForm() {
65 m_Map.clear();
66#ifdef PDF_ENABLE_XFA
67 m_XFAMap.clear();
68#endif // PDF_ENABLE_XFA
69}
70
tsepez4cf55152016-11-02 14:37:54 -070071bool CPDFSDK_InterForm::HighlightWidgets() {
72 return false;
jaepark611adb82016-08-17 11:34:36 -070073}
74
75CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget,
tsepez4cf55152016-11-02 14:37:54 -070076 bool bNext) const {
Dan Sinclair0bb13332017-03-30 16:12:02 -040077 auto pIterator = pdfium::MakeUnique<CBA_AnnotIterator>(
78 pWidget->GetPageView(), CPDF_Annot::Subtype::WIDGET);
jaepark611adb82016-08-17 11:34:36 -070079
80 if (bNext)
81 return static_cast<CPDFSDK_Widget*>(pIterator->GetNextAnnot(pWidget));
82
83 return static_cast<CPDFSDK_Widget*>(pIterator->GetPrevAnnot(pWidget));
84}
85
dsinclairc5267c52016-11-04 15:35:12 -070086CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const {
jaepark611adb82016-08-17 11:34:36 -070087 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;
jaepark611adb82016-08-17 11:34:36 -070096
97 CPDF_Dictionary* pControlDict = pControl->GetWidget();
dsinclair7cbe68e2016-10-12 11:56:23 -070098 CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
jaepark611adb82016-08-17 11:34:36 -070099 CPDFSDK_PageView* pPage = nullptr;
100
dsinclair38fd8442016-09-15 10:15:32 -0700101 if (CPDF_Dictionary* pPageDict = pControlDict->GetDictFor("P")) {
jaepark611adb82016-08-17 11:34:36 -0700102 int nPageIndex = pDocument->GetPageIndex(pPageDict->GetObjNum());
103 if (nPageIndex >= 0)
dsinclair7cbe68e2016-10-12 11:56:23 -0700104 pPage = m_pFormFillEnv->GetPageView(nPageIndex);
jaepark611adb82016-08-17 11:34:36 -0700105 }
106
107 if (!pPage) {
108 int nPageIndex = GetPageIndexByAnnotDict(pDocument, pControlDict);
109 if (nPageIndex >= 0)
dsinclair7cbe68e2016-10-12 11:56:23 -0700110 pPage = m_pFormFillEnv->GetPageView(nPageIndex);
jaepark611adb82016-08-17 11:34:36 -0700111 }
112
113 if (!pPage)
114 return nullptr;
115
116 return static_cast<CPDFSDK_Widget*>(pPage->GetAnnotByDict(pControlDict));
117}
118
119void CPDFSDK_InterForm::GetWidgets(
Ryan Harrison275e2602017-09-18 14:23:18 -0400120 const WideString& sFieldName,
tsepez8fa82792017-01-11 09:32:33 -0800121 std::vector<CPDFSDK_Annot::ObservedPtr>* widgets) const {
jaepark611adb82016-08-17 11:34:36 -0700122 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
129void CPDFSDK_InterForm::GetWidgets(
130 CPDF_FormField* pField,
tsepez8fa82792017-01-11 09:32:33 -0800131 std::vector<CPDFSDK_Annot::ObservedPtr>* widgets) const {
jaepark611adb82016-08-17 11:34:36 -0700132 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
133 CPDF_FormControl* pFormCtrl = pField->GetControl(i);
134 ASSERT(pFormCtrl);
dsinclairc5267c52016-11-04 15:35:12 -0700135 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
jaepark611adb82016-08-17 11:34:36 -0700136 if (pWidget)
tsepez8fa82792017-01-11 09:32:33 -0800137 widgets->emplace_back(pWidget);
jaepark611adb82016-08-17 11:34:36 -0700138 }
139}
140
141int 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)) {
dsinclair38fd8442016-09-15 10:15:32 -0700148 if (CPDF_Array* pAnnots = pPageDict->GetArrayFor("Annots")) {
jaepark611adb82016-08-17 11:34:36 -0700149 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
161void CPDFSDK_InterForm::AddMap(CPDF_FormControl* pControl,
162 CPDFSDK_Widget* pWidget) {
163 m_Map[pControl] = pWidget;
164}
165
166void CPDFSDK_InterForm::RemoveMap(CPDF_FormControl* pControl) {
167 m_Map.erase(pControl);
168}
169
tsepez4cf55152016-11-02 14:37:54 -0700170void CPDFSDK_InterForm::EnableCalculate(bool bEnabled) {
jaepark611adb82016-08-17 11:34:36 -0700171 m_bCalculate = bEnabled;
172}
173
tsepez4cf55152016-11-02 14:37:54 -0700174bool CPDFSDK_InterForm::IsCalculateEnabled() const {
jaepark611adb82016-08-17 11:34:36 -0700175 return m_bCalculate;
176}
177
178#ifdef PDF_ENABLE_XFA
179void CPDFSDK_InterForm::AddXFAMap(CXFA_FFWidget* hWidget,
180 CPDFSDK_XFAWidget* pWidget) {
181 ASSERT(hWidget);
182 m_XFAMap[hWidget] = pWidget;
183}
184
185void CPDFSDK_InterForm::RemoveXFAMap(CXFA_FFWidget* hWidget) {
186 ASSERT(hWidget);
187 m_XFAMap.erase(hWidget);
188}
189
190CPDFSDK_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
tsepez4cf55152016-11-02 14:37:54 -0700196void CPDFSDK_InterForm::XfaEnableCalculate(bool bEnabled) {
jaepark611adb82016-08-17 11:34:36 -0700197 m_bXfaCalculate = bEnabled;
198}
tsepez4cf55152016-11-02 14:37:54 -0700199bool CPDFSDK_InterForm::IsXfaCalculateEnabled() const {
jaepark611adb82016-08-17 11:34:36 -0700200 return m_bXfaCalculate;
201}
202
tsepez4cf55152016-11-02 14:37:54 -0700203bool CPDFSDK_InterForm::IsXfaValidationsEnabled() {
jaepark611adb82016-08-17 11:34:36 -0700204 return m_bXfaValidationsEnabled;
205}
tsepez4cf55152016-11-02 14:37:54 -0700206void CPDFSDK_InterForm::XfaSetValidationsEnabled(bool bEnabled) {
jaepark611adb82016-08-17 11:34:36 -0700207 m_bXfaValidationsEnabled = bEnabled;
208}
209
210void CPDFSDK_InterForm::SynchronizeField(CPDF_FormField* pFormField,
tsepez4cf55152016-11-02 14:37:54 -0700211 bool bSynchronizeElse) {
jaepark611adb82016-08-17 11:34:36 -0700212 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
213 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
dsinclairc5267c52016-11-04 15:35:12 -0700214 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
jaepark611adb82016-08-17 11:34:36 -0700215 pWidget->Synchronize(bSynchronizeElse);
216 }
217}
218#endif // PDF_ENABLE_XFA
219
220void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) {
dsinclair690c0332016-10-11 09:13:01 -0700221 if (!m_pFormFillEnv->IsJSInitiated())
jaepark611adb82016-08-17 11:34:36 -0700222 return;
223
224 if (m_bBusy)
225 return;
226
tsepez4cf55152016-11-02 14:37:54 -0700227 m_bBusy = true;
jaepark611adb82016-08-17 11:34:36 -0700228
229 if (!IsCalculateEnabled()) {
tsepez4cf55152016-11-02 14:37:54 -0700230 m_bBusy = false;
jaepark611adb82016-08-17 11:34:36 -0700231 return;
232 }
233
dsinclair690c0332016-10-11 09:13:01 -0700234 IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime();
jaepark611adb82016-08-17 11:34:36 -0700235 int nSize = m_pInterForm->CountFieldsInCalculationOrder();
236 for (int i = 0; i < nSize; i++) {
237 CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i);
238 if (!pField)
239 continue;
240
241 int nType = pField->GetFieldType();
242 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
243 continue;
244
245 CPDF_AAction aAction = pField->GetAdditionalAction();
246 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Calculate))
247 continue;
248
249 CPDF_Action action = aAction.GetAction(CPDF_AAction::Calculate);
250 if (!action.GetDict())
251 continue;
252
Ryan Harrison275e2602017-09-18 14:23:18 -0400253 WideString csJS = action.GetJavaScript();
jaepark611adb82016-08-17 11:34:36 -0700254 if (csJS.IsEmpty())
255 continue;
256
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800257 IJS_EventContext* pContext = pRuntime->NewEventContext();
Ryan Harrison275e2602017-09-18 14:23:18 -0400258 WideString sOldValue = pField->GetValue();
259 WideString sValue = sOldValue;
tsepez4cf55152016-11-02 14:37:54 -0700260 bool bRC = true;
jaepark611adb82016-08-17 11:34:36 -0700261 pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
262
Ryan Harrison275e2602017-09-18 14:23:18 -0400263 WideString sInfo;
tsepez4cf55152016-11-02 14:37:54 -0700264 bool bRet = pContext->RunScript(csJS, &sInfo);
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800265 pRuntime->ReleaseEventContext(pContext);
jaepark611adb82016-08-17 11:34:36 -0700266 if (bRet && bRC && sValue.Compare(sOldValue) != 0)
tsepez4cf55152016-11-02 14:37:54 -0700267 pField->SetValue(sValue, true);
jaepark611adb82016-08-17 11:34:36 -0700268 }
tsepez4cf55152016-11-02 14:37:54 -0700269 m_bBusy = false;
jaepark611adb82016-08-17 11:34:36 -0700270}
271
Ryan Harrison275e2602017-09-18 14:23:18 -0400272WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField,
273 bool& bFormatted) {
274 WideString sValue = pFormField->GetValue();
dsinclair690c0332016-10-11 09:13:01 -0700275 if (!m_pFormFillEnv->IsJSInitiated()) {
tsepez4cf55152016-11-02 14:37:54 -0700276 bFormatted = false;
jaepark611adb82016-08-17 11:34:36 -0700277 return sValue;
278 }
279
dsinclair690c0332016-10-11 09:13:01 -0700280 IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime();
jaepark611adb82016-08-17 11:34:36 -0700281 if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX &&
282 pFormField->CountSelectedItems() > 0) {
283 int index = pFormField->GetSelectedIndex(0);
284 if (index >= 0)
285 sValue = pFormField->GetOptionLabel(index);
286 }
287
tsepez4cf55152016-11-02 14:37:54 -0700288 bFormatted = false;
jaepark611adb82016-08-17 11:34:36 -0700289
290 CPDF_AAction aAction = pFormField->GetAdditionalAction();
291 if (aAction.GetDict() && aAction.ActionExist(CPDF_AAction::Format)) {
292 CPDF_Action action = aAction.GetAction(CPDF_AAction::Format);
293 if (action.GetDict()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400294 WideString script = action.GetJavaScript();
jaepark611adb82016-08-17 11:34:36 -0700295 if (!script.IsEmpty()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400296 WideString Value = sValue;
jaepark611adb82016-08-17 11:34:36 -0700297
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800298 IJS_EventContext* pContext = pRuntime->NewEventContext();
tsepez4cf55152016-11-02 14:37:54 -0700299 pContext->OnField_Format(pFormField, Value, true);
Ryan Harrison275e2602017-09-18 14:23:18 -0400300 WideString sInfo;
tsepez4cf55152016-11-02 14:37:54 -0700301 bool bRet = pContext->RunScript(script, &sInfo);
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800302 pRuntime->ReleaseEventContext(pContext);
jaepark611adb82016-08-17 11:34:36 -0700303 if (bRet) {
304 sValue = Value;
tsepez4cf55152016-11-02 14:37:54 -0700305 bFormatted = true;
jaepark611adb82016-08-17 11:34:36 -0700306 }
307 }
308 }
309 }
jaepark611adb82016-08-17 11:34:36 -0700310 return sValue;
311}
312
313void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
Ryan Harrison275e2602017-09-18 14:23:18 -0400314 const WideString* sValue,
tsepez4cf55152016-11-02 14:37:54 -0700315 bool bValueChanged) {
jaepark611adb82016-08-17 11:34:36 -0700316 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
317 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
318 ASSERT(pFormCtrl);
dsinclairc5267c52016-11-04 15:35:12 -0700319 if (CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl))
jaepark611adb82016-08-17 11:34:36 -0700320 pWidget->ResetAppearance(sValue, bValueChanged);
321 }
322}
323
324void CPDFSDK_InterForm::UpdateField(CPDF_FormField* pFormField) {
Lei Zhang375c2762017-03-10 14:37:14 -0800325 auto* formfiller = m_pFormFillEnv->GetInteractiveFormFiller();
jaepark611adb82016-08-17 11:34:36 -0700326 for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
327 CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
328 ASSERT(pFormCtrl);
329
Lei Zhang77f9bff2017-08-29 11:34:12 -0700330 CPDFSDK_Widget* pWidget = GetWidget(pFormCtrl);
331 if (!pWidget)
332 continue;
333
334 UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
335 FX_RECT rect = formfiller->GetViewBBox(
336 m_pFormFillEnv->GetPageView(pPage, false), pWidget);
337 m_pFormFillEnv->Invalidate(pPage, rect);
jaepark611adb82016-08-17 11:34:36 -0700338 }
339}
340
tsepez4cf55152016-11-02 14:37:54 -0700341bool CPDFSDK_InterForm::OnKeyStrokeCommit(CPDF_FormField* pFormField,
Ryan Harrison275e2602017-09-18 14:23:18 -0400342 const WideString& csValue) {
jaepark611adb82016-08-17 11:34:36 -0700343 CPDF_AAction aAction = pFormField->GetAdditionalAction();
344 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::KeyStroke))
tsepez4cf55152016-11-02 14:37:54 -0700345 return true;
jaepark611adb82016-08-17 11:34:36 -0700346
347 CPDF_Action action = aAction.GetAction(CPDF_AAction::KeyStroke);
348 if (!action.GetDict())
tsepez4cf55152016-11-02 14:37:54 -0700349 return true;
jaepark611adb82016-08-17 11:34:36 -0700350
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700351 CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHandler();
jaepark611adb82016-08-17 11:34:36 -0700352 PDFSDK_FieldAction fa;
Lei Zhang60fa2fc2017-07-21 17:42:19 -0700353 fa.bModifier = false;
354 fa.bShift = false;
jaepark611adb82016-08-17 11:34:36 -0700355 fa.sValue = csValue;
Tom Sepezcc205132017-05-16 14:01:47 -0700356 pActionHandler->DoAction_FieldJavaScript(
357 action, CPDF_AAction::KeyStroke, m_pFormFillEnv.Get(), pFormField, fa);
jaepark611adb82016-08-17 11:34:36 -0700358 return fa.bRC;
359}
360
tsepez4cf55152016-11-02 14:37:54 -0700361bool CPDFSDK_InterForm::OnValidate(CPDF_FormField* pFormField,
Ryan Harrison275e2602017-09-18 14:23:18 -0400362 const WideString& csValue) {
jaepark611adb82016-08-17 11:34:36 -0700363 CPDF_AAction aAction = pFormField->GetAdditionalAction();
364 if (!aAction.GetDict() || !aAction.ActionExist(CPDF_AAction::Validate))
tsepez4cf55152016-11-02 14:37:54 -0700365 return true;
jaepark611adb82016-08-17 11:34:36 -0700366
367 CPDF_Action action = aAction.GetAction(CPDF_AAction::Validate);
368 if (!action.GetDict())
tsepez4cf55152016-11-02 14:37:54 -0700369 return true;
jaepark611adb82016-08-17 11:34:36 -0700370
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700371 CPDFSDK_ActionHandler* pActionHandler = m_pFormFillEnv->GetActionHandler();
jaepark611adb82016-08-17 11:34:36 -0700372 PDFSDK_FieldAction fa;
Lei Zhang60fa2fc2017-07-21 17:42:19 -0700373 fa.bModifier = false;
374 fa.bShift = false;
jaepark611adb82016-08-17 11:34:36 -0700375 fa.sValue = csValue;
Tom Sepezcc205132017-05-16 14:01:47 -0700376 pActionHandler->DoAction_FieldJavaScript(
377 action, CPDF_AAction::Validate, m_pFormFillEnv.Get(), pFormField, fa);
jaepark611adb82016-08-17 11:34:36 -0700378 return fa.bRC;
379}
380
tsepez4cf55152016-11-02 14:37:54 -0700381bool CPDFSDK_InterForm::DoAction_Hide(const CPDF_Action& action) {
jaepark611adb82016-08-17 11:34:36 -0700382 ASSERT(action.GetDict());
383
384 CPDF_ActionFields af(&action);
385 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
386 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
387
388 bool bHide = action.GetHideStatus();
tsepez4cf55152016-11-02 14:37:54 -0700389 bool bChanged = false;
jaepark611adb82016-08-17 11:34:36 -0700390
391 for (CPDF_FormField* pField : fields) {
392 for (int i = 0, sz = pField->CountControls(); i < sz; ++i) {
393 CPDF_FormControl* pControl = pField->GetControl(i);
394 ASSERT(pControl);
395
dsinclairc5267c52016-11-04 15:35:12 -0700396 if (CPDFSDK_Widget* pWidget = GetWidget(pControl)) {
jaepark611adb82016-08-17 11:34:36 -0700397 uint32_t nFlags = pWidget->GetFlags();
398 nFlags &= ~ANNOTFLAG_INVISIBLE;
399 nFlags &= ~ANNOTFLAG_NOVIEW;
400 if (bHide)
401 nFlags |= ANNOTFLAG_HIDDEN;
402 else
403 nFlags &= ~ANNOTFLAG_HIDDEN;
404 pWidget->SetFlags(nFlags);
405 pWidget->GetPageView()->UpdateView(pWidget);
tsepez4cf55152016-11-02 14:37:54 -0700406 bChanged = true;
jaepark611adb82016-08-17 11:34:36 -0700407 }
408 }
409 }
410
411 return bChanged;
412}
413
tsepez4cf55152016-11-02 14:37:54 -0700414bool CPDFSDK_InterForm::DoAction_SubmitForm(const CPDF_Action& action) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400415 WideString sDestination = action.GetFilePath();
jaepark611adb82016-08-17 11:34:36 -0700416 if (sDestination.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700417 return false;
jaepark611adb82016-08-17 11:34:36 -0700418
419 CPDF_Dictionary* pActionDict = action.GetDict();
420 if (pActionDict->KeyExist("Fields")) {
421 CPDF_ActionFields af(&action);
422 uint32_t dwFlags = action.GetFlags();
423 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
424 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
425 if (!fields.empty()) {
426 bool bIncludeOrExclude = !(dwFlags & 0x01);
Nicolas Penaa478dc52017-01-23 15:48:51 -0500427 if (!m_pInterForm->CheckRequiredFields(&fields, bIncludeOrExclude))
tsepez4cf55152016-11-02 14:37:54 -0700428 return false;
jaepark611adb82016-08-17 11:34:36 -0700429
430 return SubmitFields(sDestination, fields, bIncludeOrExclude, false);
431 }
432 }
Nicolas Penaa478dc52017-01-23 15:48:51 -0500433 if (!m_pInterForm->CheckRequiredFields(nullptr, true))
tsepez4cf55152016-11-02 14:37:54 -0700434 return false;
jaepark611adb82016-08-17 11:34:36 -0700435
tsepez4cf55152016-11-02 14:37:54 -0700436 return SubmitForm(sDestination, false);
jaepark611adb82016-08-17 11:34:36 -0700437}
438
Ryan Harrison275e2602017-09-18 14:23:18 -0400439bool CPDFSDK_InterForm::SubmitFields(const WideString& csDestination,
tsepez4cf55152016-11-02 14:37:54 -0700440 const std::vector<CPDF_FormField*>& fields,
441 bool bIncludeOrExclude,
442 bool bUrlEncoded) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400443 ByteString textBuf = ExportFieldsToFDFTextBuf(fields, bIncludeOrExclude);
jaepark611adb82016-08-17 11:34:36 -0700444
Ryan Harrison875e98c2017-09-27 10:53:11 -0400445 size_t nBufSize = textBuf.GetLength();
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400446 if (nBufSize == 0)
Henrique Nakashimaaea80dc2017-08-01 19:47:24 -0400447 return false;
Henrique Nakashimaaea80dc2017-08-01 19:47:24 -0400448
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400449 uint8_t* pLocalBuffer = FX_Alloc(uint8_t, nBufSize);
450 memcpy(pLocalBuffer, textBuf.c_str(), nBufSize);
451 uint8_t* pBuffer = pLocalBuffer;
Henrique Nakashimaaea80dc2017-08-01 19:47:24 -0400452
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400453 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) {
454 FX_Free(pLocalBuffer);
455 return false;
456 }
457
458 m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, csDestination.c_str());
459
460 if (pBuffer != pLocalBuffer)
461 FX_Free(pBuffer);
462
463 FX_Free(pLocalBuffer);
464
tsepez4cf55152016-11-02 14:37:54 -0700465 return true;
jaepark611adb82016-08-17 11:34:36 -0700466}
467
Ryan Harrison875e98c2017-09-27 10:53:11 -0400468bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf, size_t& nBufSize) {
tsepez05e01692016-11-28 17:30:09 -0800469 std::unique_ptr<CFDF_Document> pFDF =
470 CFDF_Document::ParseMemory(pBuf, nBufSize);
jaepark611adb82016-08-17 11:34:36 -0700471 if (!pFDF)
tsepez4cf55152016-11-02 14:37:54 -0700472 return true;
jaepark611adb82016-08-17 11:34:36 -0700473
dsinclair38fd8442016-09-15 10:15:32 -0700474 CPDF_Dictionary* pMainDict = pFDF->GetRoot()->GetDictFor("FDF");
jaepark611adb82016-08-17 11:34:36 -0700475 if (!pMainDict)
tsepez4cf55152016-11-02 14:37:54 -0700476 return false;
jaepark611adb82016-08-17 11:34:36 -0700477
dsinclair38fd8442016-09-15 10:15:32 -0700478 CPDF_Array* pFields = pMainDict->GetArrayFor("Fields");
jaepark611adb82016-08-17 11:34:36 -0700479 if (!pFields)
tsepez4cf55152016-11-02 14:37:54 -0700480 return false;
jaepark611adb82016-08-17 11:34:36 -0700481
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400482 std::ostringstream fdfEncodedData;
jaepark611adb82016-08-17 11:34:36 -0700483 for (uint32_t i = 0; i < pFields->GetCount(); i++) {
484 CPDF_Dictionary* pField = pFields->GetDictAt(i);
485 if (!pField)
486 continue;
Ryan Harrison275e2602017-09-18 14:23:18 -0400487 WideString name;
dsinclair38fd8442016-09-15 10:15:32 -0700488 name = pField->GetUnicodeTextFor("T");
Ryan Harrison275e2602017-09-18 14:23:18 -0400489 ByteString name_b = ByteString::FromUnicode(name);
490 ByteString csBValue = pField->GetStringFor("V");
491 WideString csWValue = PDF_DecodeText(csBValue);
492 ByteString csValue_b = ByteString::FromUnicode(csWValue);
jaepark611adb82016-08-17 11:34:36 -0700493
494 fdfEncodedData << name_b.GetBuffer(name_b.GetLength());
Ryan Harrison0186c182017-08-01 16:20:40 -0400495 name_b.ReleaseBuffer(name_b.GetStringLength());
jaepark611adb82016-08-17 11:34:36 -0700496 fdfEncodedData << "=";
497 fdfEncodedData << csValue_b.GetBuffer(csValue_b.GetLength());
Ryan Harrison0186c182017-08-01 16:20:40 -0400498 csValue_b.ReleaseBuffer(csValue_b.GetStringLength());
jaepark611adb82016-08-17 11:34:36 -0700499 if (i != pFields->GetCount() - 1)
500 fdfEncodedData << "&";
501 }
502
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400503 nBufSize = fdfEncodedData.tellp();
504 if (nBufSize == 0)
505 return false;
506
jaepark611adb82016-08-17 11:34:36 -0700507 pBuf = FX_Alloc(uint8_t, nBufSize);
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400508 memcpy(pBuf, fdfEncodedData.str().c_str(), nBufSize);
tsepez4cf55152016-11-02 14:37:54 -0700509 return true;
jaepark611adb82016-08-17 11:34:36 -0700510}
511
Ryan Harrison275e2602017-09-18 14:23:18 -0400512ByteString CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(
jaepark611adb82016-08-17 11:34:36 -0700513 const std::vector<CPDF_FormField*>& fields,
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400514 bool bIncludeOrExclude) {
Tom Sepez690d4562017-05-18 11:42:46 -0700515 std::unique_ptr<CFDF_Document> pFDF = m_pInterForm->ExportToFDF(
516 m_pFormFillEnv->JS_docGetFilePath(), fields, bIncludeOrExclude, false);
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400517
Ryan Harrison275e2602017-09-18 14:23:18 -0400518 return pFDF ? pFDF->WriteToString() : ByteString();
jaepark611adb82016-08-17 11:34:36 -0700519}
520
Ryan Harrison275e2602017-09-18 14:23:18 -0400521WideString CPDFSDK_InterForm::GetTemporaryFileName(const WideString& sFileExt) {
jaepark611adb82016-08-17 11:34:36 -0700522 return L"";
523}
524
Ryan Harrison275e2602017-09-18 14:23:18 -0400525bool CPDFSDK_InterForm::SubmitForm(const WideString& sDestination,
tsepez4cf55152016-11-02 14:37:54 -0700526 bool bUrlEncoded) {
jaepark611adb82016-08-17 11:34:36 -0700527 if (sDestination.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700528 return false;
jaepark611adb82016-08-17 11:34:36 -0700529
dsinclair7cbe68e2016-10-12 11:56:23 -0700530 if (!m_pFormFillEnv || !m_pInterForm)
tsepez4cf55152016-11-02 14:37:54 -0700531 return false;
jaepark611adb82016-08-17 11:34:36 -0700532
Tom Sepez690d4562017-05-18 11:42:46 -0700533 std::unique_ptr<CFDF_Document> pFDFDoc =
534 m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false);
jaepark611adb82016-08-17 11:34:36 -0700535 if (!pFDFDoc)
tsepez4cf55152016-11-02 14:37:54 -0700536 return false;
jaepark611adb82016-08-17 11:34:36 -0700537
Ryan Harrison275e2602017-09-18 14:23:18 -0400538 ByteString fdfBuffer = pFDFDoc->WriteToString();
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400539
Ryan Harrison875e98c2017-09-27 10:53:11 -0400540 if (fdfBuffer.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700541 return false;
jaepark611adb82016-08-17 11:34:36 -0700542
Ryan Harrison875e98c2017-09-27 10:53:11 -0400543 uint8_t* pLocalBuffer = FX_Alloc(uint8_t, fdfBuffer.GetLength());
544 memcpy(pLocalBuffer, fdfBuffer.c_str(), fdfBuffer.GetLength());
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400545 uint8_t* pBuffer = pLocalBuffer;
546
Ryan Harrison875e98c2017-09-27 10:53:11 -0400547 size_t nBufSize = fdfBuffer.GetLength();
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400548 if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize)) {
549 FX_Free(pLocalBuffer);
tsepez4cf55152016-11-02 14:37:54 -0700550 return false;
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400551 }
jaepark611adb82016-08-17 11:34:36 -0700552
dsinclair690c0332016-10-11 09:13:01 -0700553 m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400554
555 if (pBuffer != pLocalBuffer)
jaepark611adb82016-08-17 11:34:36 -0700556 FX_Free(pBuffer);
557
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400558 FX_Free(pLocalBuffer);
559
tsepez4cf55152016-11-02 14:37:54 -0700560 return true;
jaepark611adb82016-08-17 11:34:36 -0700561}
562
Ryan Harrison275e2602017-09-18 14:23:18 -0400563ByteString CPDFSDK_InterForm::ExportFormToFDFTextBuf() {
Tom Sepez690d4562017-05-18 11:42:46 -0700564 std::unique_ptr<CFDF_Document> pFDF =
565 m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath(), false);
Henrique Nakashima5c09f4c2017-08-04 12:28:52 -0400566
Ryan Harrison275e2602017-09-18 14:23:18 -0400567 return pFDF ? pFDF->WriteToString() : ByteString();
jaepark611adb82016-08-17 11:34:36 -0700568}
569
tsepez4cf55152016-11-02 14:37:54 -0700570bool CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {
jaepark611adb82016-08-17 11:34:36 -0700571 ASSERT(action.GetDict());
572
573 CPDF_Dictionary* pActionDict = action.GetDict();
574 if (!pActionDict->KeyExist("Fields"))
575 return m_pInterForm->ResetForm(true);
576
577 CPDF_ActionFields af(&action);
578 uint32_t dwFlags = action.GetFlags();
579
580 std::vector<CPDF_Object*> fieldObjects = af.GetAllFields();
581 std::vector<CPDF_FormField*> fields = GetFieldFromObjects(fieldObjects);
582 return m_pInterForm->ResetForm(fields, !(dwFlags & 0x01), true);
583}
584
tsepez4cf55152016-11-02 14:37:54 -0700585bool CPDFSDK_InterForm::DoAction_ImportData(const CPDF_Action& action) {
586 return false;
jaepark611adb82016-08-17 11:34:36 -0700587}
588
589std::vector<CPDF_FormField*> CPDFSDK_InterForm::GetFieldFromObjects(
590 const std::vector<CPDF_Object*>& objects) const {
591 std::vector<CPDF_FormField*> fields;
592 for (CPDF_Object* pObject : objects) {
593 if (pObject && pObject->IsString()) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400594 WideString csName = pObject->GetUnicodeText();
jaepark611adb82016-08-17 11:34:36 -0700595 CPDF_FormField* pField = m_pInterForm->GetField(0, csName);
596 if (pField)
597 fields.push_back(pField);
598 }
599 }
600 return fields;
601}
602
603int CPDFSDK_InterForm::BeforeValueChange(CPDF_FormField* pField,
Ryan Harrison275e2602017-09-18 14:23:18 -0400604 const WideString& csValue) {
jaepark611adb82016-08-17 11:34:36 -0700605 int nType = pField->GetFieldType();
606 if (nType != FIELDTYPE_COMBOBOX && nType != FIELDTYPE_TEXTFIELD)
607 return 0;
608
609 if (!OnKeyStrokeCommit(pField, csValue))
610 return -1;
611
612 if (!OnValidate(pField, csValue))
613 return -1;
614
615 return 1;
616}
617
618void CPDFSDK_InterForm::AfterValueChange(CPDF_FormField* pField) {
619#ifdef PDF_ENABLE_XFA
tsepez4cf55152016-11-02 14:37:54 -0700620 SynchronizeField(pField, false);
jaepark611adb82016-08-17 11:34:36 -0700621#endif // PDF_ENABLE_XFA
622 int nType = pField->GetFieldType();
623 if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) {
624 OnCalculate(pField);
tsepez4cf55152016-11-02 14:37:54 -0700625 bool bFormatted = false;
Ryan Harrison275e2602017-09-18 14:23:18 -0400626 WideString sValue = OnFormat(pField, bFormatted);
tsepez4cf55152016-11-02 14:37:54 -0700627 ResetFieldAppearance(pField, bFormatted ? &sValue : nullptr, true);
jaepark611adb82016-08-17 11:34:36 -0700628 UpdateField(pField);
629 }
630}
631
632int CPDFSDK_InterForm::BeforeSelectionChange(CPDF_FormField* pField,
Ryan Harrison275e2602017-09-18 14:23:18 -0400633 const WideString& csValue) {
jaepark611adb82016-08-17 11:34:36 -0700634 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
635 return 0;
636
637 if (!OnKeyStrokeCommit(pField, csValue))
638 return -1;
639
640 if (!OnValidate(pField, csValue))
641 return -1;
642
643 return 1;
644}
645
646void CPDFSDK_InterForm::AfterSelectionChange(CPDF_FormField* pField) {
647 if (pField->GetFieldType() != FIELDTYPE_LISTBOX)
648 return;
649
650 OnCalculate(pField);
tsepez4cf55152016-11-02 14:37:54 -0700651 ResetFieldAppearance(pField, nullptr, true);
jaepark611adb82016-08-17 11:34:36 -0700652 UpdateField(pField);
653}
654
655void CPDFSDK_InterForm::AfterCheckedStatusChange(CPDF_FormField* pField) {
656 int nType = pField->GetFieldType();
657 if (nType != FIELDTYPE_CHECKBOX && nType != FIELDTYPE_RADIOBUTTON)
658 return;
659
660 OnCalculate(pField);
661 UpdateField(pField);
662}
663
664int CPDFSDK_InterForm::BeforeFormReset(CPDF_InterForm* pForm) {
665 return 0;
666}
667
668void CPDFSDK_InterForm::AfterFormReset(CPDF_InterForm* pForm) {
669 OnCalculate(nullptr);
670}
671
672int CPDFSDK_InterForm::BeforeFormImportData(CPDF_InterForm* pForm) {
673 return 0;
674}
675
676void CPDFSDK_InterForm::AfterFormImportData(CPDF_InterForm* pForm) {
677 OnCalculate(nullptr);
678}
679
tsepez4cf55152016-11-02 14:37:54 -0700680bool CPDFSDK_InterForm::IsNeedHighLight(int nFieldType) {
jaepark611adb82016-08-17 11:34:36 -0700681 if (nFieldType < 1 || nFieldType > kNumFieldTypes)
tsepez4cf55152016-11-02 14:37:54 -0700682 return false;
jaepark611adb82016-08-17 11:34:36 -0700683 return m_bNeedHightlight[nFieldType - 1];
684}
685
686void CPDFSDK_InterForm::RemoveAllHighLight() {
687 for (int i = 0; i < kNumFieldTypes; ++i)
tsepez4cf55152016-11-02 14:37:54 -0700688 m_bNeedHightlight[i] = false;
jaepark611adb82016-08-17 11:34:36 -0700689}
690
691void CPDFSDK_InterForm::SetHighlightColor(FX_COLORREF clr, int nFieldType) {
692 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
693 return;
694 switch (nFieldType) {
695 case 0: {
696 for (int i = 0; i < kNumFieldTypes; ++i) {
697 m_aHighlightColor[i] = clr;
tsepez4cf55152016-11-02 14:37:54 -0700698 m_bNeedHightlight[i] = true;
jaepark611adb82016-08-17 11:34:36 -0700699 }
700 break;
701 }
702 default: {
703 m_aHighlightColor[nFieldType - 1] = clr;
tsepez4cf55152016-11-02 14:37:54 -0700704 m_bNeedHightlight[nFieldType - 1] = true;
jaepark611adb82016-08-17 11:34:36 -0700705 break;
706 }
707 }
708}
709
710FX_COLORREF CPDFSDK_InterForm::GetHighlightColor(int nFieldType) {
711 if (nFieldType < 0 || nFieldType > kNumFieldTypes)
712 return FXSYS_RGB(255, 255, 255);
713 if (nFieldType == 0)
714 return m_aHighlightColor[0];
715 return m_aHighlightColor[nFieldType - 1];
716}