blob: cc3d5d53c1464fa3ff787bc27ef44777b2d32cba [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_formfill.h"
8
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08009#include <memory>
Tom Sepez49357702019-08-19 17:17:18 +000010#include <utility>
Tom Sepezab277682016-02-17 10:07:21 -080011#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080012
Tom Sepez7fad4d62019-06-07 16:19:10 +000013#include "core/fpdfapi/page/cpdf_occontext.h"
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_page.h"
Lei Zhang81535612018-10-09 21:15:17 +000015#include "core/fpdfapi/parser/cpdf_dictionary.h"
dsinclair488b7ad2016-10-04 11:55:50 -070016#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair69d9c682016-10-04 12:18:35 -070017#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_formcontrol.h"
19#include "core/fpdfdoc/cpdf_formfield.h"
Lei Zhangc3450652018-10-11 16:54:42 +000020#include "core/fpdfdoc/cpdf_interactiveform.h"
Dan Sinclaira5085d42017-05-11 16:26:50 -040021#include "core/fxge/cfx_defaultrenderdevice.h"
Dan Sinclair7d125322018-03-28 18:49:34 +000022#include "fpdfsdk/cpdfsdk_actionhandler.h"
Tom Sepez49357702019-08-19 17:17:18 +000023#include "fpdfsdk/cpdfsdk_baannothandler.h"
dsinclair735606d2016-10-05 15:47:02 -070024#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000025#include "fpdfsdk/cpdfsdk_helpers.h"
Lei Zhangc3450652018-10-11 16:54:42 +000026#include "fpdfsdk/cpdfsdk_interactiveform.h"
dsinclair114e46a2016-09-29 17:18:21 -070027#include "fpdfsdk/cpdfsdk_pageview.h"
Tom Sepez49357702019-08-19 17:17:18 +000028#include "fpdfsdk/cpdfsdk_widgethandler.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080029#include "public/fpdfview.h"
tsepez36eb4bd2016-10-03 15:24:27 -070030#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Tom Sepez40e9ff32015-11-30 12:39:54 -080032#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070033#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070034#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Tom Sepez49357702019-08-19 17:17:18 +000035#include "fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.h"
Dan Sinclaircdba7472017-03-23 09:17:10 -040036
Ryan Harrisonc3cc2ab2018-06-21 21:09:54 +000037static_assert(static_cast<int>(AlertButton::kDefault) ==
38 JSPLATFORM_ALERT_BUTTON_DEFAULT,
39 "Default alert button types must match");
40static_assert(static_cast<int>(AlertButton::kOK) == JSPLATFORM_ALERT_BUTTON_OK,
41 "OK alert button types must match");
42static_assert(static_cast<int>(AlertButton::kOKCancel) ==
43 JSPLATFORM_ALERT_BUTTON_OKCANCEL,
44 "OKCancel alert button types must match");
45static_assert(static_cast<int>(AlertButton::kYesNo) ==
46 JSPLATFORM_ALERT_BUTTON_YESNO,
47 "YesNo alert button types must match");
48static_assert(static_cast<int>(AlertButton::kYesNoCancel) ==
49 JSPLATFORM_ALERT_BUTTON_YESNOCANCEL,
50 "YesNoCancel alert button types must match");
51
52static_assert(static_cast<int>(AlertIcon::kDefault) ==
53 JSPLATFORM_ALERT_ICON_DEFAULT,
54 "Default alert icon types must match");
55static_assert(static_cast<int>(AlertIcon::kError) ==
56 JSPLATFORM_ALERT_ICON_ERROR,
57 "Error alert icon types must match");
58static_assert(static_cast<int>(AlertIcon::kWarning) ==
59 JSPLATFORM_ALERT_ICON_WARNING,
60 "Warning alert icon types must match");
61static_assert(static_cast<int>(AlertIcon::kQuestion) ==
62 JSPLATFORM_ALERT_ICON_QUESTION,
63 "Question alert icon types must match");
64static_assert(static_cast<int>(AlertIcon::kStatus) ==
65 JSPLATFORM_ALERT_ICON_STATUS,
66 "Status alert icon types must match");
67static_assert(static_cast<int>(AlertIcon::kAsterisk) ==
68 JSPLATFORM_ALERT_ICON_ASTERISK,
69 "Asterisk alert icon types must match");
70
71static_assert(static_cast<int>(AlertReturn::kOK) == JSPLATFORM_ALERT_RETURN_OK,
72 "OK alert return types must match");
73static_assert(static_cast<int>(AlertReturn::kCancel) ==
74 JSPLATFORM_ALERT_RETURN_CANCEL,
75 "Cancel alert return types must match");
76static_assert(static_cast<int>(AlertReturn::kNo) == JSPLATFORM_ALERT_RETURN_NO,
77 "No alert return types must match");
78static_assert(static_cast<int>(AlertReturn::kYes) ==
79 JSPLATFORM_ALERT_RETURN_YES,
80 "Yes alert return types must match");
81
Ryan Harrison854d71c2017-10-18 12:28:14 -040082static_assert(static_cast<int>(FormType::kNone) == FORMTYPE_NONE,
83 "None form types must match");
84static_assert(static_cast<int>(FormType::kAcroForm) == FORMTYPE_ACRO_FORM,
85 "AcroForm form types must match");
86static_assert(static_cast<int>(FormType::kXFAFull) == FORMTYPE_XFA_FULL,
87 "XFA full form types must match");
88static_assert(static_cast<int>(FormType::kXFAForeground) ==
89 FORMTYPE_XFA_FOREGROUND,
90 "XFA foreground form types must match");
Tom Sepez40e9ff32015-11-30 12:39:54 -080091#endif // PDF_ENABLE_XFA
92
Ryan Harrison9baf31f2018-01-12 18:36:30 +000093static_assert(static_cast<int>(FormFieldType::kUnknown) ==
94 FPDF_FORMFIELD_UNKNOWN,
95 "Unknown form field types must match");
96static_assert(static_cast<int>(FormFieldType::kPushButton) ==
97 FPDF_FORMFIELD_PUSHBUTTON,
98 "PushButton form field types must match");
99static_assert(static_cast<int>(FormFieldType::kCheckBox) ==
100 FPDF_FORMFIELD_CHECKBOX,
101 "CheckBox form field types must match");
102static_assert(static_cast<int>(FormFieldType::kRadioButton) ==
103 FPDF_FORMFIELD_RADIOBUTTON,
104 "RadioButton form field types must match");
105static_assert(static_cast<int>(FormFieldType::kComboBox) ==
106 FPDF_FORMFIELD_COMBOBOX,
107 "ComboBox form field types must match");
108static_assert(static_cast<int>(FormFieldType::kListBox) ==
109 FPDF_FORMFIELD_LISTBOX,
110 "ListBox form field types must match");
111static_assert(static_cast<int>(FormFieldType::kTextField) ==
112 FPDF_FORMFIELD_TEXTFIELD,
113 "TextField form field types must match");
114static_assert(static_cast<int>(FormFieldType::kSignature) ==
115 FPDF_FORMFIELD_SIGNATURE,
116 "Signature form field types must match");
117#ifdef PDF_ENABLE_XFA
118static_assert(static_cast<int>(FormFieldType::kXFA) == FPDF_FORMFIELD_XFA,
119 "XFA form field types must match");
Ryan Harrison2056fac2018-01-16 16:06:45 +0000120static_assert(static_cast<int>(FormFieldType::kXFA_CheckBox) ==
121 FPDF_FORMFIELD_XFA_CHECKBOX,
122 "XFA CheckBox form field types must match");
123static_assert(static_cast<int>(FormFieldType::kXFA_ComboBox) ==
124 FPDF_FORMFIELD_XFA_COMBOBOX,
125 "XFA ComboBox form field types must match");
126static_assert(static_cast<int>(FormFieldType::kXFA_ImageField) ==
127 FPDF_FORMFIELD_XFA_IMAGEFIELD,
128 "XFA ImageField form field types must match");
129static_assert(static_cast<int>(FormFieldType::kXFA_ListBox) ==
130 FPDF_FORMFIELD_XFA_LISTBOX,
131 "XFA ListBox form field types must match");
132static_assert(static_cast<int>(FormFieldType::kXFA_PushButton) ==
133 FPDF_FORMFIELD_XFA_PUSHBUTTON,
134 "XFA PushButton form field types must match");
135static_assert(static_cast<int>(FormFieldType::kXFA_Signature) ==
136 FPDF_FORMFIELD_XFA_SIGNATURE,
137 "XFA Signature form field types must match");
138static_assert(static_cast<int>(FormFieldType::kXFA_TextField) ==
139 FPDF_FORMFIELD_XFA_TEXTFIELD,
140 "XFA TextField form field types must match");
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000141#endif // PDF_ENABLE_XFA
142static_assert(kFormFieldTypeCount == FPDF_FORMFIELD_COUNT,
143 "Number of form field types must match");
144
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000145static_assert(static_cast<int>(CPDF_AAction::kCloseDocument) ==
Tom Sepez9ead8112018-10-19 16:56:58 +0000146 FPDFDOC_AACTION_WC,
147 "CloseDocument action must match");
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000148static_assert(static_cast<int>(CPDF_AAction::kSaveDocument) ==
Tom Sepez9ead8112018-10-19 16:56:58 +0000149 FPDFDOC_AACTION_WS,
150 "SaveDocument action must match");
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000151static_assert(static_cast<int>(CPDF_AAction::kDocumentSaved) ==
Tom Sepez9ead8112018-10-19 16:56:58 +0000152 FPDFDOC_AACTION_DS,
153 "DocumentSaved action must match");
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000154static_assert(static_cast<int>(CPDF_AAction::kPrintDocument) ==
Tom Sepez9ead8112018-10-19 16:56:58 +0000155 FPDFDOC_AACTION_WP,
156 "PrintDocument action must match");
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000157static_assert(static_cast<int>(CPDF_AAction::kDocumentPrinted) ==
Tom Sepez9ead8112018-10-19 16:56:58 +0000158 FPDFDOC_AACTION_DP,
159 "DocumentPrinted action must match");
160
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700161namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
Tom Sepezdae73f52018-06-13 17:54:57 +0000164 FPDF_PAGE fpdf_page) {
165 IPDF_Page* pPage = IPDFPageFromFPDFPage(fpdf_page);
Tom Sepez540c4362015-11-24 13:33:57 -0800166 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -0700168
dsinclairf3fbe832016-10-11 13:08:04 -0700169 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000170 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclair7cbe68e2016-10-12 11:56:23 -0700171 return pFormFillEnv ? pFormFillEnv->GetPageView(pPage, true) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700172}
Bo Xufdc00a72014-10-28 23:03:33 -0700173
thestigbefa4502016-05-26 20:15:19 -0700174void FFLCommon(FPDF_FORMHANDLE hHandle,
175 FPDF_BITMAP bitmap,
176 FPDF_RECORDER recorder,
Tom Sepezdae73f52018-06-13 17:54:57 +0000177 FPDF_PAGE fpdf_page,
thestigbefa4502016-05-26 20:15:19 -0700178 int start_x,
179 int start_y,
180 int size_x,
181 int size_y,
182 int rotate,
183 int flags) {
184 if (!hHandle)
185 return;
186
Tom Sepezdae73f52018-06-13 17:54:57 +0000187 IPDF_Page* pPage = IPDFPageFromFPDFPage(fpdf_page);
thestigbefa4502016-05-26 20:15:19 -0700188 if (!pPage)
189 return;
190
Tom Sepezca0273392018-05-25 23:37:50 +0000191 CPDF_Document* pPDFDoc = pPage->GetDocument();
Tom Sepezdae73f52018-06-13 17:54:57 +0000192 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, fpdf_page);
thestigbefa4502016-05-26 20:15:19 -0700193
Lei Zhangc4242b22018-04-12 15:50:49 +0000194 const FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
195 CFX_Matrix matrix = pPage->GetDisplayMatrix(rect, rotate);
thestigbefa4502016-05-26 20:15:19 -0700196
Dan Sinclaira5085d42017-05-11 16:26:50 -0400197 auto pDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
thestigbefa4502016-05-26 20:15:19 -0700198#ifdef _SKIA_SUPPORT_
199 pDevice->AttachRecorder(static_cast<SkPictureRecorder*>(recorder));
200#endif
Tom Sepezdae73f52018-06-13 17:54:57 +0000201
Tom Sepez525147a2018-05-03 17:19:53 +0000202 RetainPtr<CFX_DIBitmap> holder(CFXDIBitmapFromFPDFBitmap(bitmap));
Jing Wangfd281922019-04-11 21:20:11 +0000203 pDevice->Attach(holder, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Tom Sepez1629f602017-04-21 14:11:26 -0700204 {
205 CFX_RenderDevice::StateRestorer restorer(pDevice.get());
Lei Zhangc4242b22018-04-12 15:50:49 +0000206 pDevice->SetClip_Rect(rect);
thestigbefa4502016-05-26 20:15:19 -0700207
Tom Sepez1629f602017-04-21 14:11:26 -0700208 CPDF_RenderOptions options;
Lei Zhangb8b52b42018-11-08 19:11:22 +0000209 options.GetOptions().bClearType = !!(flags & FPDF_LCD_TEXT);
thestigbefa4502016-05-26 20:15:19 -0700210
Tom Sepez1629f602017-04-21 14:11:26 -0700211 // Grayscale output
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400212 if (flags & FPDF_GRAYSCALE)
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400213 options.SetColorMode(CPDF_RenderOptions::kGray);
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400214
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400215 options.SetDrawAnnots(flags & FPDF_ANNOT);
Dan Sinclair921fe6b2017-10-05 11:14:12 -0400216 options.SetOCContext(
217 pdfium::MakeRetain<CPDF_OCContext>(pPDFDoc, CPDF_OCContext::View));
Tom Sepezca0273392018-05-25 23:37:50 +0000218
Tom Sepez45a55242018-05-25 23:57:13 +0000219 if (pPageView)
Lei Zhang09ee0872018-10-09 19:13:55 +0000220 pPageView->PageView_OnDraw(pDevice.get(), matrix, &options, rect);
Tom Sepez1629f602017-04-21 14:11:26 -0700221 }
Tom Sepezdae73f52018-06-13 17:54:57 +0000222
caryclark8f875502016-12-06 13:49:34 -0800223#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -0400224 pDevice->Flush(true);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700225 holder->UnPreMultiply();
caryclark687fbde2016-11-22 12:44:25 -0800226#endif
thestigbefa4502016-05-26 20:15:19 -0700227}
228
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700229} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -0700230
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400231FPDF_EXPORT int FPDF_CALLCONV
232FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
233 FPDF_PAGE page,
234 double page_x,
235 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700236 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 if (pPage) {
Lei Zhangf7e69762019-02-13 19:09:15 +0000238 CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
239 if (!pForm)
240 return -1;
241
242 CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
243 CPDF_FormControl* pFormCtrl = pPDFForm->GetControlAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400244 pPage,
245 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500246 nullptr);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700247 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 CPDF_FormField* pFormField = pFormCtrl->GetField();
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000250 return pFormField ? static_cast<int>(pFormField->GetFieldType()) : -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 }
252
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800253#ifdef PDF_ENABLE_XFA
Tom Sepez101535f2018-06-12 13:36:05 +0000254 CPDFXFA_Page* pXFAPage = ToXFAPage(IPDFPageFromFPDFPage(page));
Tom Sepezbcb7a692019-08-09 21:21:09 +0000255 if (pXFAPage) {
256 return pXFAPage->HasFormFieldAtPoint(
257 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800259#endif // PDF_ENABLE_XFA
Tom Sepezbcb7a692019-08-09 21:21:09 +0000260
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800261 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400264FPDF_EXPORT int FPDF_CALLCONV
265FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
266 FPDF_PAGE page,
267 double page_x,
268 double page_y) {
Lei Zhangf7e69762019-02-13 19:09:15 +0000269 CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
270 if (!pForm)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700271 return -1;
Lei Zhangf7e69762019-02-13 19:09:15 +0000272
Tom Sepezdb0be962015-10-16 14:00:21 -0700273 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
274 if (!pPage)
275 return -1;
Lei Zhangf7e69762019-02-13 19:09:15 +0000276
277 CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
Lei Zhangbdf72c32015-08-14 19:24:08 -0700278 int z_order = -1;
Lei Zhangf7e69762019-02-13 19:09:15 +0000279 pPDFForm->GetControlAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400280 pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
Ryan Harrison6cec70a2018-06-05 14:06:10 +0000281 &z_order);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700282 return z_order;
283}
284
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400285FPDF_EXPORT FPDF_FORMHANDLE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
287 FPDF_FORMFILLINFO* formInfo) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800288#ifdef PDF_ENABLE_XFA
Lei Zhangf672baf2019-07-29 17:31:28 +0000289 constexpr int kRequiredVersion = 2;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800290#else // PDF_ENABLE_XFA
Lei Zhangf672baf2019-07-29 17:31:28 +0000291 constexpr int kRequiredVersion = 1;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800292#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800293 if (!formInfo || formInfo->version != kRequiredVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295
Tom Sepezfe06d512018-05-01 17:25:25 +0000296 auto* pDocument = CPDFDocumentFromFPDFDocument(document);
Tom Sepez540c4362015-11-24 13:33:57 -0800297 if (!pDocument)
298 return nullptr;
299
Tom Sepez40e9ff32015-11-30 12:39:54 -0800300#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700301 // If the CPDFXFA_Context has a FormFillEnvironment already then we've done
dsinclair8779fa82016-10-12 12:05:44 -0700302 // this and can just return the old Env. Otherwise, we'll end up setting a new
303 // environment into the XFADocument and, that could get weird.
Tom Sepezfe06d512018-05-01 17:25:25 +0000304 auto* pContext = static_cast<CPDFXFA_Context*>(pDocument->GetExtension());
Tom Sepez2563fc32018-08-01 19:13:06 +0000305 if (pContext && pContext->GetFormFillEnv()) {
306 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
307 pContext->GetFormFillEnv());
308 }
dsinclaira939bfe2016-09-22 13:18:45 -0700309#endif
310
Tom Sepez49357702019-08-19 17:17:18 +0000311 std::unique_ptr<IPDFSDK_AnnotHandler> pXFAHandler;
312#ifdef PDF_ENABLE_XFA
313 pXFAHandler = pdfium::MakeUnique<CPDFXFA_WidgetHandler>();
314#endif // PDF_ENABLE_XFA
315
316 auto pFormFillEnv = pdfium::MakeUnique<CPDFSDK_FormFillEnvironment>(
317 pDocument, formInfo,
318 pdfium::MakeUnique<CPDFSDK_AnnotHandlerMgr>(
319 pdfium::MakeUnique<CPDFSDK_BAAnnotHandler>(),
320 pdfium::MakeUnique<CPDFSDK_WidgetHandler>(), std::move(pXFAHandler)));
dsinclaira939bfe2016-09-22 13:18:45 -0700321
322#ifdef PDF_ENABLE_XFA
Tom Sepez3f3c39d2018-05-01 17:46:34 +0000323 if (pContext)
324 pContext->SetFormFillEnv(pFormFillEnv.get());
Tom Sepez40e9ff32015-11-30 12:39:54 -0800325#endif // PDF_ENABLE_XFA
dsinclaira939bfe2016-09-22 13:18:45 -0700326
Tom Sepez2563fc32018-08-01 19:13:06 +0000327 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
328 pFormFillEnv.release()); // Caller takes ownership.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329}
330
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400331FPDF_EXPORT void FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
Daniel Hosseinian9b87a7d2020-01-08 22:50:39 +0000333 if (!hHandle)
Lei Zhang1fed5a22017-06-01 11:52:22 -0700334 return;
dsinclaira939bfe2016-09-22 13:18:45 -0700335
Daniel Hosseinian9b87a7d2020-01-08 22:50:39 +0000336 // Take back ownership of the form fill environment. This is the inverse of
337 // FPDFDOC_InitFormFillEnvironment() above.
338 std::unique_ptr<CPDFSDK_FormFillEnvironment> pFormFillEnv(
339 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle));
340
Tom Sepez40e9ff32015-11-30 12:39:54 -0800341#ifdef PDF_ENABLE_XFA
dsinclair21161052016-10-05 15:46:15 -0700342 // Reset the focused annotations and remove the SDK document from the
343 // XFA document.
dsinclair7cbe68e2016-10-12 11:56:23 -0700344 pFormFillEnv->ClearAllFocusedAnnots();
dsinclairf3fbe832016-10-11 13:08:04 -0700345 // If the document was closed first, it's possible the XFA document
346 // is now a nullptr.
Tom Sepezd48bd292019-08-14 19:48:55 +0000347 auto* pContext =
348 static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
349 if (pContext)
350 pContext->SetFormFillEnv(nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800351#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352}
353
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400354FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
355 FPDF_PAGE page,
356 int modifier,
357 double page_x,
358 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
360 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700361 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500362 return pPageView->OnMouseMove(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700363}
364
Lei Zhang63b01262017-08-31 08:54:46 -0700365FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnFocus(FPDF_FORMHANDLE hHandle,
366 FPDF_PAGE page,
367 int modifier,
368 double page_x,
369 double page_y) {
370 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
371 if (!pPageView)
372 return false;
373 return pPageView->OnFocus(CFX_PointF(page_x, page_y), modifier);
374}
375
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400376FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
377 FPDF_PAGE page,
378 int modifier,
379 double page_x,
380 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
382 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700383 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000384#ifdef PDF_ENABLE_CLICK_LOGGING
385 fprintf(stderr, "mousedown,left,%d,%d\n", static_cast<int>(round(page_x)),
386 static_cast<int>(round(page_y)));
387#endif // PDF_ENABLE_CLICK_LOGGING
Dan Sinclairf528eee2017-02-14 11:52:07 -0500388 return pPageView->OnLButtonDown(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389}
390
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400391FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
392 FPDF_PAGE page,
393 int modifier,
394 double page_x,
395 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
397 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700398 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000399#ifdef PDF_ENABLE_CLICK_LOGGING
400 fprintf(stderr, "mouseup,left,%d,%d\n", static_cast<int>(round(page_x)),
401 static_cast<int>(round(page_y)));
402#endif // PDF_ENABLE_CLICK_LOGGING
Lei Zhang1fed5a22017-06-01 11:52:22 -0700403 return pPageView->OnLButtonUp(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700404}
405
Lei Zhanga37989f2018-10-17 20:32:41 +0000406FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
407FORM_OnLButtonDoubleClick(FPDF_FORMHANDLE hHandle,
408 FPDF_PAGE page,
409 int modifier,
410 double page_x,
411 double page_y) {
412 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
413 if (!pPageView)
414 return false;
415#ifdef PDF_ENABLE_CLICK_LOGGING
416 fprintf(stderr, "mousedown,doubleleft,%d,%d\n",
417 static_cast<int>(round(page_x)), static_cast<int>(round(page_y)));
418#endif // PDF_ENABLE_CLICK_LOGGING
419 return pPageView->OnLButtonDblClk(CFX_PointF(page_x, page_y), modifier);
420}
421
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400422FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
423 FPDF_PAGE page,
424 int modifier,
425 double page_x,
426 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
428 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700429 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000430#ifdef PDF_ENABLE_CLICK_LOGGING
431 fprintf(stderr, "mousedown,right,%d,%d\n", static_cast<int>(round(page_x)),
432 static_cast<int>(round(page_y)));
433#endif // PDF_ENABLE_CLICK_LOGGING
Dan Sinclairf528eee2017-02-14 11:52:07 -0500434 return pPageView->OnRButtonDown(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700435}
436
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400437FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
438 FPDF_PAGE page,
439 int modifier,
440 double page_x,
441 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
443 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700444 return false;
Ryan Harrison9ce75b82018-06-06 19:45:14 +0000445#ifdef PDF_ENABLE_CLICK_LOGGING
446 fprintf(stderr, "mouseup,right,%d,%d\n", static_cast<int>(round(page_x)),
447 static_cast<int>(round(page_y)));
448#endif // PDF_ENABLE_CLICK_LOGGING
Lei Zhang1fed5a22017-06-01 11:52:22 -0700449 return pPageView->OnRButtonUp(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700450}
451
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400452FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
453 FPDF_PAGE page,
454 int nKeyCode,
455 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
457 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700458 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700460}
461
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400462FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
463 FPDF_PAGE page,
464 int nKeyCode,
465 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
467 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700468 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700470}
471
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400472FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnChar(FPDF_FORMHANDLE hHandle,
473 FPDF_PAGE page,
474 int nChar,
475 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
477 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700478 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480}
481
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400482FPDF_EXPORT unsigned long FPDF_CALLCONV
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000483FORM_GetFocusedText(FPDF_FORMHANDLE hHandle,
484 FPDF_PAGE page,
485 void* buffer,
486 unsigned long buflen) {
487 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
488 if (!pPageView)
489 return 0;
490
491 return Utf16EncodeMaybeCopyAndReturnLength(pPageView->GetFocusedFormText(),
492 buffer, buflen);
493}
494
495FPDF_EXPORT unsigned long FPDF_CALLCONV
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400496FORM_GetSelectedText(FPDF_FORMHANDLE hHandle,
497 FPDF_PAGE page,
498 void* buffer,
499 unsigned long buflen) {
Diana Gagedce2d722017-06-20 11:17:11 -0700500 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
501 if (!pPageView)
502 return 0;
503
Lei Zhanga4c7ac42018-04-17 15:12:58 +0000504 return Utf16EncodeMaybeCopyAndReturnLength(pPageView->GetSelectedText(),
505 buffer, buflen);
Diana Gagedce2d722017-06-20 11:17:11 -0700506}
507
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400508FPDF_EXPORT void FPDF_CALLCONV FORM_ReplaceSelection(FPDF_FORMHANDLE hHandle,
509 FPDF_PAGE page,
510 FPDF_WIDESTRING wsText) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700511 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
512 if (!pPageView)
513 return;
Diana Gageab390972017-07-28 17:07:39 -0700514
Lei Zhangb46a7632019-01-09 02:56:16 +0000515 pPageView->ReplaceSelection(WideStringFromFPDFWideString(wsText));
Diana Gage1c7f1422017-07-24 11:19:52 -0700516}
517
Lei Zhangee967722018-04-19 20:55:54 +0000518FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_CanUndo(FPDF_FORMHANDLE hHandle,
519 FPDF_PAGE page) {
520 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
521 if (!pPageView)
522 return false;
523 return pPageView->CanUndo();
524}
525
526FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_CanRedo(FPDF_FORMHANDLE hHandle,
527 FPDF_PAGE page) {
528 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
529 if (!pPageView)
530 return false;
531 return pPageView->CanRedo();
532}
533
534FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_Undo(FPDF_FORMHANDLE hHandle,
535 FPDF_PAGE page) {
536 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
537 if (!pPageView)
538 return false;
539 return pPageView->Undo();
540}
541
542FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_Redo(FPDF_FORMHANDLE hHandle,
543 FPDF_PAGE page) {
544 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
545 if (!pPageView)
546 return false;
547 return pPageView->Redo();
548}
549
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400550FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
551FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700552 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000553 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700554 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700555 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700556 return pFormFillEnv->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700557}
558
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400559FPDF_EXPORT void FPDF_CALLCONV FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
560 FPDF_BITMAP bitmap,
561 FPDF_PAGE page,
562 int start_x,
563 int start_y,
564 int size_x,
565 int size_y,
566 int rotate,
567 int flags) {
Cary Clark399be5b2016-03-14 16:51:29 -0400568 FFLCommon(hHandle, bitmap, nullptr, page, start_x, start_y, size_x, size_y,
569 rotate, flags);
570}
571
572#ifdef _SKIA_SUPPORT_
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400573FPDF_EXPORT void FPDF_CALLCONV FPDF_FFLRecord(FPDF_FORMHANDLE hHandle,
574 FPDF_RECORDER recorder,
575 FPDF_PAGE page,
576 int start_x,
577 int start_y,
578 int size_x,
579 int size_y,
580 int rotate,
581 int flags) {
Cary Clark399be5b2016-03-14 16:51:29 -0400582 FFLCommon(hHandle, nullptr, recorder, page, start_x, start_y, size_x, size_y,
583 rotate, flags);
584}
585#endif
586
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400587FPDF_EXPORT void FPDF_CALLCONV
588FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
589 int fieldType,
590 unsigned long color) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000591 CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
592 if (!pForm)
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000593 return;
594
595 Optional<FormFieldType> cast_input = IntToFormFieldType(fieldType);
Ryan Harrison2056fac2018-01-16 16:06:45 +0000596 if (!cast_input)
Ryan Harrison9baf31f2018-01-12 18:36:30 +0000597 return;
598
Lei Zhang073ecf42018-10-11 16:56:00 +0000599 if (cast_input.value() == FormFieldType::kUnknown)
600 pForm->SetAllHighlightColors(color);
601 else
602 pForm->SetHighlightColor(color, cast_input.value());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700603}
604
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400605FPDF_EXPORT void FPDF_CALLCONV
606FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000607 if (CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle))
608 pForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700609}
610
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400611FPDF_EXPORT void FPDF_CALLCONV
612FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
Lei Zhang073ecf42018-10-11 16:56:00 +0000613 if (CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle))
614 pForm->RemoveAllHighLights();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700615}
616
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400617FPDF_EXPORT void FPDF_CALLCONV FORM_OnAfterLoadPage(FPDF_PAGE page,
618 FPDF_FORMHANDLE hHandle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
tsepez4cf55152016-11-02 14:37:54 -0700620 pPageView->SetValid(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700621}
622
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400623FPDF_EXPORT void FPDF_CALLCONV FORM_OnBeforeClosePage(FPDF_PAGE page,
624 FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700625 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000626 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700627 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700629
Tom Sepez101535f2018-06-12 13:36:05 +0000630 IPDF_Page* pPage = IPDFPageFromFPDFPage(page);
Tom Sepez540c4362015-11-24 13:33:57 -0800631 if (!pPage)
632 return;
633
dsinclair7cbe68e2016-10-12 11:56:23 -0700634 CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700635 if (pPageView) {
tsepez4cf55152016-11-02 14:37:54 -0700636 pPageView->SetValid(false);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800637 // RemovePageView() takes care of the delete for us.
dsinclair7cbe68e2016-10-12 11:56:23 -0700638 pFormFillEnv->RemovePageView(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700639 }
640}
641
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400642FPDF_EXPORT void FPDF_CALLCONV
643FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700644 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000645 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
Tom Sepezf5ca90c2018-02-01 02:15:44 +0000646 if (pFormFillEnv && pFormFillEnv->IsJSPlatformPresent())
dsinclair7cbe68e2016-10-12 11:56:23 -0700647 pFormFillEnv->ProcJavascriptFun();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648}
649
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400650FPDF_EXPORT void FPDF_CALLCONV
651FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700652 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000653 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
Tom Sepezf5ca90c2018-02-01 02:15:44 +0000654 if (pFormFillEnv && pFormFillEnv->IsJSPlatformPresent())
dsinclair7cbe68e2016-10-12 11:56:23 -0700655 pFormFillEnv->ProcOpenAction();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656}
657
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400658FPDF_EXPORT void FPDF_CALLCONV FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
659 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700660 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000661 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700662 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 return;
664
dsinclair7cbe68e2016-10-12 11:56:23 -0700665 CPDF_Document* pDoc = pFormFillEnv->GetPDFDocument();
Tom Sepez22923602019-07-15 21:33:35 +0000666 CPDF_Dictionary* pDict = pDoc->GetRoot();
Lei Zhang1fed5a22017-06-01 11:52:22 -0700667 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700669
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000670 CPDF_AAction aa(pDict->GetDictFor("AA"));
Lei Zhang1fed5a22017-06-01 11:52:22 -0700671 auto type = static_cast<CPDF_AAction::AActionType>(aaType);
672 if (aa.ActionExist(type)) {
673 CPDF_Action action = aa.GetAction(type);
Lei Zhang7db136a2018-10-10 21:34:17 +0000674 pFormFillEnv->GetActionHandler()->DoAction_Document(action, type,
675 pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 }
677}
678
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400679FPDF_EXPORT void FPDF_CALLCONV FORM_DoPageAAction(FPDF_PAGE page,
680 FPDF_FORMHANDLE hHandle,
681 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700682 CPDFSDK_FormFillEnvironment* pFormFillEnv =
Tom Sepez2563fc32018-08-01 19:13:06 +0000683 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle);
dsinclairf3fbe832016-10-11 13:08:04 -0700684 if (!pFormFillEnv)
tonikitoo401d4f22016-08-10 11:37:45 -0700685 return;
686
Tom Sepez101535f2018-06-12 13:36:05 +0000687 IPDF_Page* pPage = IPDFPageFromFPDFPage(page);
Tom Sepez540c4362015-11-24 13:33:57 -0800688 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000689 if (!pPDFPage)
690 return;
691
692 if (!pFormFillEnv->GetPageView(pPage, false))
tonikitoo401d4f22016-08-10 11:37:45 -0700693 return;
694
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700695 CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHandler();
Henrique Nakashima888af472018-06-07 19:43:42 +0000696 CPDF_Dictionary* pPageDict = pPDFPage->GetDict();
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000697 CPDF_AAction aa(pPageDict->GetDictFor("AA"));
Lei Zhang1fed5a22017-06-01 11:52:22 -0700698 CPDF_AAction::AActionType type = aaType == FPDFPAGE_AACTION_OPEN
Hans Wennborgc320e9c2018-10-22 18:28:52 +0000699 ? CPDF_AAction::kOpenPage
700 : CPDF_AAction::kClosePage;
Henrique Nakashima7b094f82018-05-08 20:32:08 +0000701 if (aa.ActionExist(type)) {
702 CPDF_Action action = aa.GetAction(type);
703 pActionHandler->DoAction_Page(action, type, pFormFillEnv);
704 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700705}
rycsmitha5230e22019-02-21 17:33:03 +0000706
707FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
708FORM_SetIndexSelected(FPDF_FORMHANDLE hHandle,
709 FPDF_PAGE page,
710 int index,
711 FPDF_BOOL selected) {
712 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
713 if (!pPageView)
714 return false;
715 return pPageView->SetIndexSelected(index, selected);
716}
717
718FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
719FORM_IsIndexSelected(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int index) {
720 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
721 if (!pPageView)
722 return false;
723 return pPageView->IsIndexSelected(index);
724}