blob: a891db90196ca70c7686149c67b16b9be72f5a6e [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 Sepezab277682016-02-17 10:07:21 -080010#include <vector>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080011
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair69d9c682016-10-04 12:18:35 -070014#include "core/fpdfapi/render/cpdf_renderoptions.h"
dsinclair1727aee2016-09-29 13:12:56 -070015#include "core/fpdfdoc/cpdf_formcontrol.h"
16#include "core/fpdfdoc/cpdf_formfield.h"
17#include "core/fpdfdoc/cpdf_interform.h"
18#include "core/fpdfdoc/cpdf_occontext.h"
dsinclair74a34fc2016-09-29 16:41:42 -070019#include "core/fxge/cfx_fxgedevice.h"
dsinclair735606d2016-10-05 15:47:02 -070020#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
dsinclair114e46a2016-09-29 17:18:21 -070021#include "fpdfsdk/cpdfsdk_interform.h"
22#include "fpdfsdk/cpdfsdk_pageview.h"
23#include "fpdfsdk/fsdk_actionhandler.h"
24#include "fpdfsdk/fsdk_define.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080025#include "public/fpdfview.h"
tsepez36eb4bd2016-10-03 15:24:27 -070026#include "third_party/base/ptr_util.h"
Tom Sepezab277682016-02-17 10:07:21 -080027#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Tom Sepez40e9ff32015-11-30 12:39:54 -080029#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070030#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070031#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
dsinclair5b493092016-09-29 20:20:24 -070032#include "xfa/fxfa/xfa_ffdocview.h"
33#include "xfa/fxfa/xfa_ffpageview.h"
34#include "xfa/fxfa/xfa_ffwidget.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080035#endif // PDF_ENABLE_XFA
36
Tom Sepezdcbc02f2015-07-17 09:16:17 -070037namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
dsinclair735606d2016-10-05 15:47:02 -070039CPDFSDK_FormFillEnvironment* HandleToCPDFSDKEnvironment(
40 FPDF_FORMHANDLE handle) {
41 return static_cast<CPDFSDK_FormFillEnvironment*>(handle);
dsinclair79db6092016-09-14 07:27:21 -070042}
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -070045 CPDFSDK_FormFillEnvironment* pFormFillEnv =
46 HandleToCPDFSDKEnvironment(hHandle);
dsinclair7cbe68e2016-10-12 11:56:23 -070047 return pFormFillEnv ? pFormFillEnv->GetInterForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070048}
Bo Xufdc00a72014-10-28 23:03:33 -070049
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
51 FPDF_PAGE page) {
Tom Sepez540c4362015-11-24 13:33:57 -080052 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
53 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -070055
dsinclairf3fbe832016-10-11 13:08:04 -070056 CPDFSDK_FormFillEnvironment* pFormFillEnv =
57 HandleToCPDFSDKEnvironment(hHandle);
dsinclair7cbe68e2016-10-12 11:56:23 -070058 return pFormFillEnv ? pFormFillEnv->GetPageView(pPage, true) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070059}
Bo Xufdc00a72014-10-28 23:03:33 -070060
Tom Sepezab277682016-02-17 10:07:21 -080061#ifdef PDF_ENABLE_XFA
62std::vector<CFX_ByteString>* FromFPDFStringHandle(FPDF_STRINGHANDLE handle) {
63 return reinterpret_cast<std::vector<CFX_ByteString>*>(handle);
64}
65
66FPDF_STRINGHANDLE ToFPDFStringHandle(std::vector<CFX_ByteString>* strings) {
67 return reinterpret_cast<FPDF_STRINGHANDLE>(strings);
68}
69#endif // PDF_ENABLE_XFA
70
thestigbefa4502016-05-26 20:15:19 -070071void FFLCommon(FPDF_FORMHANDLE hHandle,
72 FPDF_BITMAP bitmap,
73 FPDF_RECORDER recorder,
74 FPDF_PAGE page,
75 int start_x,
76 int start_y,
77 int size_x,
78 int size_y,
79 int rotate,
80 int flags) {
81 if (!hHandle)
82 return;
83
84 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
85 if (!pPage)
86 return;
87
jaepark738766e2016-09-02 18:51:44 -070088#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070089 CPDFXFA_Context* pContext = pPage->GetContext();
90 if (!pContext)
thestigbefa4502016-05-26 20:15:19 -070091 return;
dsinclair521b7502016-11-02 13:02:28 -070092 CPDF_Document* pPDFDoc = pContext->GetPDFDoc();
thestigbefa4502016-05-26 20:15:19 -070093 if (!pPDFDoc)
94 return;
dsinclairf3fbe832016-10-11 13:08:04 -070095 CPDFSDK_FormFillEnvironment* pFormFillEnv =
96 HandleToCPDFSDKEnvironment(hHandle);
97 if (!pFormFillEnv)
thestigbefa4502016-05-26 20:15:19 -070098 return;
99#endif // PDF_ENABLE_XFA
100
Dan Sinclair1b08df12017-02-09 09:17:20 -0500101 CFX_Matrix matrix =
102 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
thestigbefa4502016-05-26 20:15:19 -0700103 FX_RECT clip(start_x, start_y, start_x + size_x, start_y + size_y);
104
105 std::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
106#ifdef _SKIA_SUPPORT_
107 pDevice->AttachRecorder(static_cast<SkPictureRecorder*>(recorder));
108#endif
109 pDevice->Attach(CFXBitmapFromFPDFBitmap(bitmap), false, nullptr, false);
110 pDevice->SaveState();
111 pDevice->SetClip_Rect(clip);
112
thestigbefa4502016-05-26 20:15:19 -0700113 CPDF_RenderOptions options;
114 if (flags & FPDF_LCD_TEXT)
115 options.m_Flags |= RENDER_CLEARTYPE;
116 else
117 options.m_Flags &= ~RENDER_CLEARTYPE;
118
119 // Grayscale output
120 if (flags & FPDF_GRAYSCALE) {
121 options.m_ColorMode = RENDER_COLOR_GRAY;
122 options.m_ForeColor = 0;
123 options.m_BackColor = 0xffffff;
124 }
125 options.m_AddFlags = flags >> 8;
jaepark75f84a52016-09-09 15:39:09 -0700126 options.m_bDrawAnnots = flags & FPDF_ANNOT;
thestigbefa4502016-05-26 20:15:19 -0700127
jaepark738766e2016-09-02 18:51:44 -0700128#ifdef PDF_ENABLE_XFA
Tom Sepezf716f0b2017-01-30 10:04:07 -0800129 options.m_pOCContext =
130 pdfium::MakeRetain<CPDF_OCContext>(pPDFDoc, CPDF_OCContext::View);
dsinclair7cbe68e2016-10-12 11:56:23 -0700131 if (CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, true))
thestigbefa4502016-05-26 20:15:19 -0700132 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
jaepark738766e2016-09-02 18:51:44 -0700133#else // PDF_ENABLE_XFA
Tom Sepezf716f0b2017-01-30 10:04:07 -0800134 options.m_pOCContext = pdfium::MakeRetain<CPDF_OCContext>(
135 pPage->m_pDocument, CPDF_OCContext::View);
jaepark738766e2016-09-02 18:51:44 -0700136 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, pPage))
137 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options);
thestigbefa4502016-05-26 20:15:19 -0700138#endif // PDF_ENABLE_XFA
139
caryclark8f875502016-12-06 13:49:34 -0800140 pDevice->RestoreState(false);
141#ifdef _SKIA_SUPPORT_PATHS_
142 pDevice->Flush();
caryclark687fbde2016-11-22 12:44:25 -0800143 CFXBitmapFromFPDFBitmap(bitmap)->UnPreMultiply();
144#endif
thestigbefa4502016-05-26 20:15:19 -0700145}
146
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700147} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -0700148
Lei Zhangbdf72c32015-08-14 19:24:08 -0700149DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
150 FPDF_PAGE page,
151 double page_x,
152 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700153 if (!hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700154 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700155 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 if (pPage) {
thestigdb1a24e2016-05-23 16:55:09 -0700157 CPDF_InterForm interform(pPage->m_pDocument);
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800158 CPDF_FormControl* pFormCtrl =
159 interform.GetControlAtPoint(pPage, static_cast<FX_FLOAT>(page_x),
160 static_cast<FX_FLOAT>(page_y), nullptr);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700161 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 CPDF_FormField* pFormField = pFormCtrl->GetField();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800164 return pFormField ? pFormField->GetFieldType() : -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 }
166
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800167#ifdef PDF_ENABLE_XFA
168 CPDFXFA_Page* pXFAPage = UnderlyingFromFPDFPage(page);
169 if (!pXFAPage)
170 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171
dsinclairdf4bc592016-03-31 20:34:43 -0700172 CXFA_FFPageView* pPageView = pXFAPage->GetXFAPageView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800173 if (!pPageView)
174 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175
dsinclairdf4bc592016-03-31 20:34:43 -0700176 CXFA_FFDocView* pDocView = pPageView->GetDocView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800177 if (!pDocView)
178 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179
dsinclairdf4bc592016-03-31 20:34:43 -0700180 CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800181 if (!pWidgetHandler)
182 return -1;
183
tsepezcc4d6d82016-05-16 13:21:03 -0700184 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
dsinclair935d8d52016-05-17 10:32:18 -0700185 pPageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form,
186 XFA_WidgetStatus_Viewable));
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800187 if (!pWidgetIterator)
188 return -1;
189
dsinclairdf4bc592016-03-31 20:34:43 -0700190 CXFA_FFWidget* pXFAAnnot = pWidgetIterator->MoveToNext();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800191 while (pXFAAnnot) {
Dan Sinclair1b08df12017-02-09 09:17:20 -0500192 CFX_RectF rcBBox = pXFAAnnot->GetBBox(0);
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800193 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
194 rcBBox.top + rcBBox.height);
195 rcWidget.left -= 1.0f;
196 rcWidget.right += 1.0f;
197 rcWidget.bottom -= 1.0f;
198 rcWidget.top += 1.0f;
199
200 if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x),
201 static_cast<FX_FLOAT>(page_y))) {
202 return FPDF_FORMFIELD_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 }
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800204 pXFAAnnot = pWidgetIterator->MoveToNext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800206#endif // PDF_ENABLE_XFA
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800207 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208}
209
Lei Zhangbdf72c32015-08-14 19:24:08 -0700210DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
211 FPDF_PAGE page,
212 double page_x,
213 double page_y) {
214 return FPDFPage_HasFormFieldAtPoint(hHandle, page, page_x, page_y);
215}
216
217DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
218 FPDF_PAGE page,
219 double page_x,
220 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700221 if (!hHandle)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700222 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700223 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
224 if (!pPage)
225 return -1;
thestigdb1a24e2016-05-23 16:55:09 -0700226 CPDF_InterForm interform(pPage->m_pDocument);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700227 int z_order = -1;
228 (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
229 &z_order);
230 return z_order;
231}
232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233DLLEXPORT FPDF_FORMHANDLE STDCALL
234FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
235 FPDF_FORMFILLINFO* formInfo) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800236#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800237 const int kRequiredVersion = 2;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800238#else // PDF_ENABLE_XFA
239 const int kRequiredVersion = 1;
240#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800241 if (!formInfo || formInfo->version != kRequiredVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243
Tom Sepez540c4362015-11-24 13:33:57 -0800244 UnderlyingDocumentType* pDocument = UnderlyingFromFPDFDocument(document);
245 if (!pDocument)
246 return nullptr;
247
Tom Sepez40e9ff32015-11-30 12:39:54 -0800248#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700249 // If the CPDFXFA_Context has a FormFillEnvironment already then we've done
dsinclair8779fa82016-10-12 12:05:44 -0700250 // this and can just return the old Env. Otherwise, we'll end up setting a new
251 // environment into the XFADocument and, that could get weird.
dsinclair655fcca2016-10-11 13:53:37 -0700252 if (pDocument->GetFormFillEnv())
253 return pDocument->GetFormFillEnv();
dsinclaira939bfe2016-09-22 13:18:45 -0700254#endif
255
dsinclairf3fbe832016-10-11 13:08:04 -0700256 CPDFSDK_FormFillEnvironment* pFormFillEnv =
dsinclair735606d2016-10-05 15:47:02 -0700257 new CPDFSDK_FormFillEnvironment(pDocument, formInfo);
dsinclaira939bfe2016-09-22 13:18:45 -0700258
259#ifdef PDF_ENABLE_XFA
dsinclair655fcca2016-10-11 13:53:37 -0700260 pDocument->SetFormFillEnv(pFormFillEnv);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800261#endif // PDF_ENABLE_XFA
dsinclaira939bfe2016-09-22 13:18:45 -0700262
dsinclairf3fbe832016-10-11 13:08:04 -0700263 return pFormFillEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266DLLEXPORT void STDCALL
267FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
268 if (!hHandle)
269 return;
dsinclaira939bfe2016-09-22 13:18:45 -0700270
dsinclairf3fbe832016-10-11 13:08:04 -0700271 CPDFSDK_FormFillEnvironment* pFormFillEnv =
272 HandleToCPDFSDKEnvironment(hHandle);
dsinclaira939bfe2016-09-22 13:18:45 -0700273
Tom Sepez40e9ff32015-11-30 12:39:54 -0800274#ifdef PDF_ENABLE_XFA
dsinclair21161052016-10-05 15:46:15 -0700275 // Reset the focused annotations and remove the SDK document from the
276 // XFA document.
dsinclair7cbe68e2016-10-12 11:56:23 -0700277 pFormFillEnv->ClearAllFocusedAnnots();
dsinclairf3fbe832016-10-11 13:08:04 -0700278 // If the document was closed first, it's possible the XFA document
279 // is now a nullptr.
dsinclair521b7502016-11-02 13:02:28 -0700280 if (pFormFillEnv->GetXFAContext())
281 pFormFillEnv->GetXFAContext()->SetFormFillEnv(nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800282#endif // PDF_ENABLE_XFA
dsinclaira939bfe2016-09-22 13:18:45 -0700283
dsinclairf3fbe832016-10-11 13:08:04 -0700284 delete pFormFillEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285}
286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
288 FPDF_PAGE page,
289 int modifier,
290 double page_x,
291 double page_y) {
292 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
293 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700294 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500295 return pPageView->OnMouseMove(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296}
297
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
299 FPDF_PAGE page,
300 int modifier,
301 double page_x,
302 double page_y) {
303 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
304 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700305 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500306 return pPageView->OnLButtonDown(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
310 FPDF_PAGE page,
311 int modifier,
312 double page_x,
313 double page_y) {
314 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
315 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700316 return false;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700317
Dan Sinclairf528eee2017-02-14 11:52:07 -0500318 CFX_PointF pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 return pPageView->OnLButtonUp(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320}
321
Tom Sepez51da0932015-11-25 16:05:49 -0800322#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
324 FPDF_PAGE page,
325 int modifier,
326 double page_x,
327 double page_y) {
328 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
329 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700330 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500331 return pPageView->OnRButtonDown(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700332}
333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
335 FPDF_PAGE page,
336 int modifier,
337 double page_x,
338 double page_y) {
339 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
340 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700341 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700342
Dan Sinclairf528eee2017-02-14 11:52:07 -0500343 CFX_PointF pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 return pPageView->OnRButtonUp(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700345}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800346#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
349 FPDF_PAGE page,
350 int nKeyCode,
351 int modifier) {
352 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
353 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700354 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700355
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700357}
358
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
360 FPDF_PAGE page,
361 int nKeyCode,
362 int modifier) {
363 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
364 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700365 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368}
369
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle,
371 FPDF_PAGE page,
372 int nChar,
373 int modifier) {
374 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
375 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700376 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700379}
380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700382 CPDFSDK_FormFillEnvironment* pFormFillEnv =
383 HandleToCPDFSDKEnvironment(hHandle);
384 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700385 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700386 return pFormFillEnv->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387}
388
Cary Clark399be5b2016-03-14 16:51:29 -0400389DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
390 FPDF_BITMAP bitmap,
391 FPDF_PAGE page,
392 int start_x,
393 int start_y,
394 int size_x,
395 int size_y,
396 int rotate,
397 int flags) {
398 FFLCommon(hHandle, bitmap, nullptr, page, start_x, start_y, size_x, size_y,
399 rotate, flags);
400}
401
402#ifdef _SKIA_SUPPORT_
403DLLEXPORT void STDCALL FPDF_FFLRecord(FPDF_FORMHANDLE hHandle,
404 FPDF_RECORDER recorder,
405 FPDF_PAGE page,
406 int start_x,
407 int start_y,
408 int size_x,
409 int size_y,
410 int rotate,
411 int flags) {
412 FFLCommon(hHandle, nullptr, recorder, page, start_x, start_y, size_x, size_y,
413 rotate, flags);
414}
415#endif
416
Tom Sepez40e9ff32015-11-30 12:39:54 -0800417#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document,
419 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700420 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700422
dsinclair521b7502016-11-02 13:02:28 -0700423 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
424 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
425 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700427
dsinclair221caf62016-04-04 12:08:40 -0700428 static_cast<CXFA_FFWidget*>(hWidget)->Undo();
Bo Xufdc00a72014-10-28 23:03:33 -0700429}
dsinclair521b7502016-11-02 13:02:28 -0700430
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document,
432 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700433 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700435
dsinclair521b7502016-11-02 13:02:28 -0700436 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
437 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
438 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700440
dsinclair221caf62016-04-04 12:08:40 -0700441 static_cast<CXFA_FFWidget*>(hWidget)->Redo();
Bo Xufdc00a72014-10-28 23:03:33 -0700442}
443
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700444DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document,
445 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700446 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700448
dsinclair521b7502016-11-02 13:02:28 -0700449 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
450 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
451 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700453
dsinclair221caf62016-04-04 12:08:40 -0700454 static_cast<CXFA_FFWidget*>(hWidget)->SelectAll();
Bo Xufdc00a72014-10-28 23:03:33 -0700455}
dsinclair521b7502016-11-02 13:02:28 -0700456
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700457DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document,
458 FPDF_WIDGET hWidget,
459 FPDF_WIDESTRING wsText,
460 FPDF_DWORD* size) {
dsinclair221caf62016-04-04 12:08:40 -0700461 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700463
dsinclair521b7502016-11-02 13:02:28 -0700464 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
465 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
466 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700468
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 CFX_WideString wsCpText;
dsinclair221caf62016-04-04 12:08:40 -0700470 static_cast<CXFA_FFWidget*>(hWidget)->Copy(wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700471
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -0700473 uint32_t len = bsCpText.GetLength() / sizeof(unsigned short);
thestigbefa4502016-05-26 20:15:19 -0700474 if (!wsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 *size = len;
476 return;
477 }
Bo Xufdc00a72014-10-28 23:03:33 -0700478
weili47ca6922016-03-31 15:08:27 -0700479 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 if (real_size > 0) {
481 FXSYS_memcpy((void*)wsText,
482 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
483 real_size * sizeof(unsigned short));
484 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
485 }
486 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700487}
Tom Sepezab277682016-02-17 10:07:21 -0800488
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700489DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document,
490 FPDF_WIDGET hWidget,
491 FPDF_WIDESTRING wsText,
492 FPDF_DWORD* size) {
thestigbefa4502016-05-26 20:15:19 -0700493 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 return;
thestigbefa4502016-05-26 20:15:19 -0700495
dsinclair521b7502016-11-02 13:02:28 -0700496 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
497 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
498 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700500
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501 CFX_WideString wsCpText;
dsinclair221caf62016-04-04 12:08:40 -0700502 static_cast<CXFA_FFWidget*>(hWidget)->Cut(wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700503
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -0700505 uint32_t len = bsCpText.GetLength() / sizeof(unsigned short);
dsinclair221caf62016-04-04 12:08:40 -0700506 if (!wsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 *size = len;
508 return;
509 }
Bo Xufdc00a72014-10-28 23:03:33 -0700510
weili47ca6922016-03-31 15:08:27 -0700511 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 if (real_size > 0) {
513 FXSYS_memcpy((void*)wsText,
514 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
515 real_size * sizeof(unsigned short));
516 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
517 }
518 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700519}
Tom Sepezab277682016-02-17 10:07:21 -0800520
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document,
522 FPDF_WIDGET hWidget,
523 FPDF_WIDESTRING wsText,
524 FPDF_DWORD size) {
dsinclair221caf62016-04-04 12:08:40 -0700525 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700526 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700527
dsinclair521b7502016-11-02 13:02:28 -0700528 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
529 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
530 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700531 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700532
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size);
dsinclair221caf62016-04-04 12:08:40 -0700534 static_cast<CXFA_FFWidget*>(hWidget)->Paste(wstr);
Bo Xufdc00a72014-10-28 23:03:33 -0700535}
Tom Sepezab277682016-02-17 10:07:21 -0800536
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537DLLEXPORT void STDCALL
538FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document,
539 FPDF_WIDGET hWidget,
540 float x,
541 float y,
542 FPDF_BYTESTRING bsText) {
dsinclair221caf62016-04-04 12:08:40 -0700543 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700545
dsinclair521b7502016-11-02 13:02:28 -0700546 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
547 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
548 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700550
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 CFX_PointF ptPopup;
552 ptPopup.x = x;
553 ptPopup.y = y;
554 CFX_ByteStringC bs(bsText);
dsinclair221caf62016-04-04 12:08:40 -0700555 static_cast<CXFA_FFWidget*>(hWidget)->ReplaceSpellCheckWord(ptPopup, bs);
Bo Xufdc00a72014-10-28 23:03:33 -0700556}
Tom Sepezab277682016-02-17 10:07:21 -0800557
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558DLLEXPORT void STDCALL
559FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document,
560 FPDF_WIDGET hWidget,
561 float x,
562 float y,
563 FPDF_STRINGHANDLE* stringHandle) {
Tom Sepezab277682016-02-17 10:07:21 -0800564 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700566
dsinclair521b7502016-11-02 13:02:28 -0700567 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
568 if (pContext->GetDocType() != XFA_DOCTYPE_Dynamic &&
569 pContext->GetDocType() != XFA_DOCTYPE_Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700571
Tom Sepezab277682016-02-17 10:07:21 -0800572 std::vector<CFX_ByteString>* sSuggestWords = new std::vector<CFX_ByteString>;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700573 CFX_PointF ptPopup;
574 ptPopup.x = x;
575 ptPopup.y = y;
dsinclair221caf62016-04-04 12:08:40 -0700576 static_cast<CXFA_FFWidget*>(hWidget)
577 ->GetSuggestWords(ptPopup, *sSuggestWords);
Tom Sepezab277682016-02-17 10:07:21 -0800578 *stringHandle = ToFPDFStringHandle(sSuggestWords);
Bo Xufdc00a72014-10-28 23:03:33 -0700579}
Tom Sepezab277682016-02-17 10:07:21 -0800580
581DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE sHandle) {
582 std::vector<CFX_ByteString>* sSuggestWords = FromFPDFStringHandle(sHandle);
583 return sSuggestWords ? pdfium::CollectionSize<int>(*sSuggestWords) : -1;
Bo Xufdc00a72014-10-28 23:03:33 -0700584}
Tom Sepezab277682016-02-17 10:07:21 -0800585
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586DLLEXPORT FPDF_BOOL STDCALL
Tom Sepezab277682016-02-17 10:07:21 -0800587FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE sHandle,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 int index,
589 FPDF_BYTESTRING bsText,
590 FPDF_DWORD* size) {
Tom Sepezab277682016-02-17 10:07:21 -0800591 if (!sHandle || !size)
tsepez4cf55152016-11-02 14:37:54 -0700592 return false;
Tom Sepezab277682016-02-17 10:07:21 -0800593
594 int count = FPDF_StringHandleCounts(sHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700595 if (index < 0 || index >= count)
tsepez4cf55152016-11-02 14:37:54 -0700596 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700597
Tom Sepezab277682016-02-17 10:07:21 -0800598 std::vector<CFX_ByteString>* sSuggestWords = FromFPDFStringHandle(sHandle);
weili47ca6922016-03-31 15:08:27 -0700599 uint32_t len = (*sSuggestWords)[index].GetLength();
Tom Sepezab277682016-02-17 10:07:21 -0800600 if (!bsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 *size = len;
tsepez4cf55152016-11-02 14:37:54 -0700602 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700603 }
604
weili47ca6922016-03-31 15:08:27 -0700605 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 if (real_size > 0)
tsepezb4c9f3f2016-04-13 15:41:21 -0700607 FXSYS_memcpy((void*)bsText, (*sSuggestWords)[index].c_str(), real_size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 *size = real_size;
tsepez4cf55152016-11-02 14:37:54 -0700609 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700610}
Tom Sepezab277682016-02-17 10:07:21 -0800611
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700612DLLEXPORT void STDCALL
613FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle) {
Tom Sepezab277682016-02-17 10:07:21 -0800614 delete FromFPDFStringHandle(stringHandle);
Bo Xufdc00a72014-10-28 23:03:33 -0700615}
616
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617DLLEXPORT FPDF_BOOL STDCALL
618FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle,
619 FPDF_BYTESTRING bsText,
620 FPDF_DWORD size) {
Tom Sepezab277682016-02-17 10:07:21 -0800621 if (!stringHandle || !bsText || size == 0)
tsepez4cf55152016-11-02 14:37:54 -0700622 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700623
Tom Sepezab277682016-02-17 10:07:21 -0800624 FromFPDFStringHandle(stringHandle)->push_back(CFX_ByteString(bsText, size));
tsepez4cf55152016-11-02 14:37:54 -0700625 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700626}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800627#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700628
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700629DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
630 int fieldType,
631 unsigned long color) {
632 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
633 pInterForm->SetHighlightColor(color, fieldType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700634}
635
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle,
637 unsigned char alpha) {
638 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
639 pInterForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700640}
641
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
643 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
644 pInterForm->RemoveAllHighLight();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700645}
646
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page,
648 FPDF_FORMHANDLE hHandle) {
649 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
tsepez4cf55152016-11-02 14:37:54 -0700650 pPageView->SetValid(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700651}
652
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page,
654 FPDF_FORMHANDLE hHandle) {
Tom Sepez540c4362015-11-24 13:33:57 -0800655 if (!hHandle)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700657
dsinclairf3fbe832016-10-11 13:08:04 -0700658 CPDFSDK_FormFillEnvironment* pFormFillEnv =
659 HandleToCPDFSDKEnvironment(hHandle);
660 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700662
Tom Sepez540c4362015-11-24 13:33:57 -0800663 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
664 if (!pPage)
665 return;
666
dsinclair7cbe68e2016-10-12 11:56:23 -0700667 CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 if (pPageView) {
tsepez4cf55152016-11-02 14:37:54 -0700669 pPageView->SetValid(false);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800670 // RemovePageView() takes care of the delete for us.
dsinclair7cbe68e2016-10-12 11:56:23 -0700671 pFormFillEnv->RemovePageView(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 }
673}
674
675DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700676 CPDFSDK_FormFillEnvironment* pFormFillEnv =
677 HandleToCPDFSDKEnvironment(hHandle);
678 if (pFormFillEnv && pFormFillEnv->IsJSInitiated())
dsinclair7cbe68e2016-10-12 11:56:23 -0700679 pFormFillEnv->ProcJavascriptFun();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680}
681
682DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700683 CPDFSDK_FormFillEnvironment* pFormFillEnv =
684 HandleToCPDFSDKEnvironment(hHandle);
685 if (pFormFillEnv && pFormFillEnv->IsJSInitiated())
dsinclair7cbe68e2016-10-12 11:56:23 -0700686 pFormFillEnv->ProcOpenAction();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687}
688
689DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
690 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700691 CPDFSDK_FormFillEnvironment* pFormFillEnv =
692 HandleToCPDFSDKEnvironment(hHandle);
693 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 return;
695
dsinclair7cbe68e2016-10-12 11:56:23 -0700696 CPDF_Document* pDoc = pFormFillEnv->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 CPDF_Dictionary* pDic = pDoc->GetRoot();
698 if (!pDic)
699 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700700
dsinclair38fd8442016-09-15 10:15:32 -0700701 CPDF_AAction aa(pDic->GetDictFor("AA"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 if (aa.ActionExist((CPDF_AAction::AActionType)aaType)) {
703 CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
704 CPDFSDK_ActionHandler* pActionHandler =
dsinclair79db6092016-09-14 07:27:21 -0700705 HandleToCPDFSDKEnvironment(hHandle)->GetActionHander();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706 pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType,
dsinclairf3fbe832016-10-11 13:08:04 -0700707 pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 }
709}
710
711DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page,
712 FPDF_FORMHANDLE hHandle,
713 int aaType) {
Tom Sepez540c4362015-11-24 13:33:57 -0800714 if (!hHandle)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 return;
tonikitoo401d4f22016-08-10 11:37:45 -0700716
dsinclairf3fbe832016-10-11 13:08:04 -0700717 CPDFSDK_FormFillEnvironment* pFormFillEnv =
718 HandleToCPDFSDKEnvironment(hHandle);
719 if (!pFormFillEnv)
tonikitoo401d4f22016-08-10 11:37:45 -0700720 return;
721
Tom Sepez540c4362015-11-24 13:33:57 -0800722 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
723 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
Tom Sepezde4791d2015-10-30 12:13:10 -0700724 if (!pPDFPage)
725 return;
tonikitoo401d4f22016-08-10 11:37:45 -0700726
dsinclair7cbe68e2016-10-12 11:56:23 -0700727 if (!pFormFillEnv->GetPageView(pPage, false))
tonikitoo401d4f22016-08-10 11:37:45 -0700728 return;
729
dsinclairf3fbe832016-10-11 13:08:04 -0700730 CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHander();
tonikitoo401d4f22016-08-10 11:37:45 -0700731 CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict;
dsinclair38fd8442016-09-15 10:15:32 -0700732 CPDF_AAction aa(pPageDict->GetDictFor("AA"));
tonikitoo401d4f22016-08-10 11:37:45 -0700733 if (FPDFPAGE_AACTION_OPEN == aaType) {
734 if (aa.ActionExist(CPDF_AAction::OpenPage)) {
735 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
dsinclairf3fbe832016-10-11 13:08:04 -0700736 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage,
737 pFormFillEnv);
tonikitoo401d4f22016-08-10 11:37:45 -0700738 }
739 } else {
740 if (aa.ActionExist(CPDF_AAction::ClosePage)) {
741 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
dsinclairf3fbe832016-10-11 13:08:04 -0700742 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage,
743 pFormFillEnv);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700744 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700746}