blob: 3d821b601ff389e77e48c7581a98ceb48c31ab92 [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"
Dan Sinclaira5085d42017-05-11 16:26:50 -040019#include "core/fxge/cfx_defaultrenderdevice.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"
Dan Sinclair80c48782017-03-23 12:11:20 -040032#include "xfa/fxfa/cxfa_ffdocview.h"
33#include "xfa/fxfa/cxfa_ffpageview.h"
34#include "xfa/fxfa/cxfa_ffwidget.h"
Dan Sinclaircdba7472017-03-23 09:17:10 -040035
36static_assert(static_cast<int>(XFA_DocType::PDF) == DOCTYPE_PDF,
37 "PDF doctype must match");
38static_assert(static_cast<int>(XFA_DocType::Dynamic) == DOCTYPE_DYNAMIC_XFA,
39 "Dynamic XFA doctype must match");
40static_assert(static_cast<int>(XFA_DocType::Static) == DOCTYPE_STATIC_XFA,
41 "Static XFA doctype must match");
Tom Sepez40e9ff32015-11-30 12:39:54 -080042#endif // PDF_ENABLE_XFA
43
Tom Sepezdcbc02f2015-07-17 09:16:17 -070044namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
dsinclair735606d2016-10-05 15:47:02 -070046CPDFSDK_FormFillEnvironment* HandleToCPDFSDKEnvironment(
47 FPDF_FORMHANDLE handle) {
48 return static_cast<CPDFSDK_FormFillEnvironment*>(handle);
dsinclair79db6092016-09-14 07:27:21 -070049}
50
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -070052 CPDFSDK_FormFillEnvironment* pFormFillEnv =
53 HandleToCPDFSDKEnvironment(hHandle);
dsinclair7cbe68e2016-10-12 11:56:23 -070054 return pFormFillEnv ? pFormFillEnv->GetInterForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070055}
Bo Xufdc00a72014-10-28 23:03:33 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
58 FPDF_PAGE page) {
Tom Sepez540c4362015-11-24 13:33:57 -080059 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
60 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -070062
dsinclairf3fbe832016-10-11 13:08:04 -070063 CPDFSDK_FormFillEnvironment* pFormFillEnv =
64 HandleToCPDFSDKEnvironment(hHandle);
dsinclair7cbe68e2016-10-12 11:56:23 -070065 return pFormFillEnv ? pFormFillEnv->GetPageView(pPage, true) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070066}
Bo Xufdc00a72014-10-28 23:03:33 -070067
Tom Sepezab277682016-02-17 10:07:21 -080068#ifdef PDF_ENABLE_XFA
Ryan Harrison275e2602017-09-18 14:23:18 -040069std::vector<ByteString>* FromFPDFStringHandle(FPDF_STRINGHANDLE handle) {
70 return static_cast<std::vector<ByteString>*>(handle);
Tom Sepezab277682016-02-17 10:07:21 -080071}
72
Ryan Harrison275e2602017-09-18 14:23:18 -040073FPDF_STRINGHANDLE ToFPDFStringHandle(std::vector<ByteString>* strings) {
Nicolas Pena46abb662017-05-17 17:23:22 -040074 return static_cast<FPDF_STRINGHANDLE>(strings);
Tom Sepezab277682016-02-17 10:07:21 -080075}
76#endif // PDF_ENABLE_XFA
77
thestigbefa4502016-05-26 20:15:19 -070078void FFLCommon(FPDF_FORMHANDLE hHandle,
79 FPDF_BITMAP bitmap,
80 FPDF_RECORDER recorder,
81 FPDF_PAGE page,
82 int start_x,
83 int start_y,
84 int size_x,
85 int size_y,
86 int rotate,
87 int flags) {
88 if (!hHandle)
89 return;
90
91 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
92 if (!pPage)
93 return;
94
jaepark738766e2016-09-02 18:51:44 -070095#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070096 CPDFXFA_Context* pContext = pPage->GetContext();
97 if (!pContext)
thestigbefa4502016-05-26 20:15:19 -070098 return;
dsinclair521b7502016-11-02 13:02:28 -070099 CPDF_Document* pPDFDoc = pContext->GetPDFDoc();
thestigbefa4502016-05-26 20:15:19 -0700100 if (!pPDFDoc)
101 return;
dsinclairf3fbe832016-10-11 13:08:04 -0700102 CPDFSDK_FormFillEnvironment* pFormFillEnv =
103 HandleToCPDFSDKEnvironment(hHandle);
104 if (!pFormFillEnv)
thestigbefa4502016-05-26 20:15:19 -0700105 return;
106#endif // PDF_ENABLE_XFA
107
Dan Sinclair1b08df12017-02-09 09:17:20 -0500108 CFX_Matrix matrix =
109 pPage->GetDisplayMatrix(start_x, start_y, size_x, size_y, rotate);
thestigbefa4502016-05-26 20:15:19 -0700110 FX_RECT clip(start_x, start_y, start_x + size_x, start_y + size_y);
111
Dan Sinclaira5085d42017-05-11 16:26:50 -0400112 auto pDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
thestigbefa4502016-05-26 20:15:19 -0700113#ifdef _SKIA_SUPPORT_
114 pDevice->AttachRecorder(static_cast<SkPictureRecorder*>(recorder));
115#endif
Dan Sinclair0b950422017-09-21 15:49:49 -0400116 RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap));
Tom Sepezf0799fe2017-03-28 09:31:32 -0700117 pDevice->Attach(holder, false, nullptr, false);
Tom Sepez1629f602017-04-21 14:11:26 -0700118 {
119 CFX_RenderDevice::StateRestorer restorer(pDevice.get());
120 pDevice->SetClip_Rect(clip);
thestigbefa4502016-05-26 20:15:19 -0700121
Tom Sepez1629f602017-04-21 14:11:26 -0700122 CPDF_RenderOptions options;
123 if (flags & FPDF_LCD_TEXT)
124 options.m_Flags |= RENDER_CLEARTYPE;
125 else
126 options.m_Flags &= ~RENDER_CLEARTYPE;
thestigbefa4502016-05-26 20:15:19 -0700127
Tom Sepez1629f602017-04-21 14:11:26 -0700128 // Grayscale output
Dan Sinclairf55e72e2017-07-13 14:53:28 -0400129 if (flags & FPDF_GRAYSCALE)
130 options.m_ColorMode = CPDF_RenderOptions::kGray;
131
Tom Sepez1629f602017-04-21 14:11:26 -0700132 options.m_bDrawAnnots = flags & FPDF_ANNOT;
thestigbefa4502016-05-26 20:15:19 -0700133
jaepark738766e2016-09-02 18:51:44 -0700134#ifdef PDF_ENABLE_XFA
Tom Sepez1629f602017-04-21 14:11:26 -0700135 options.m_pOCContext =
136 pdfium::MakeRetain<CPDF_OCContext>(pPDFDoc, CPDF_OCContext::View);
137 if (CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, true))
138 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
jaepark738766e2016-09-02 18:51:44 -0700139#else // PDF_ENABLE_XFA
Tom Sepez1629f602017-04-21 14:11:26 -0700140 options.m_pOCContext = pdfium::MakeRetain<CPDF_OCContext>(
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700141 pPage->m_pDocument.Get(), CPDF_OCContext::View);
Tom Sepez1629f602017-04-21 14:11:26 -0700142 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, pPage))
143 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options);
thestigbefa4502016-05-26 20:15:19 -0700144#endif // PDF_ENABLE_XFA
Tom Sepez1629f602017-04-21 14:11:26 -0700145 }
caryclark8f875502016-12-06 13:49:34 -0800146#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -0400147 pDevice->Flush(true);
Tom Sepezf0799fe2017-03-28 09:31:32 -0700148 holder->UnPreMultiply();
caryclark687fbde2016-11-22 12:44:25 -0800149#endif
thestigbefa4502016-05-26 20:15:19 -0700150}
151
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700152} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -0700153
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400154FPDF_EXPORT int FPDF_CALLCONV
155FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
156 FPDF_PAGE page,
157 double page_x,
158 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700159 if (!hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700160 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700161 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 if (pPage) {
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700163 CPDF_InterForm interform(pPage->m_pDocument.Get());
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500164 CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400165 pPage,
166 CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500167 nullptr);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700168 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 CPDF_FormField* pFormField = pFormCtrl->GetField();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800171 return pFormField ? pFormField->GetFieldType() : -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 }
173
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800174#ifdef PDF_ENABLE_XFA
175 CPDFXFA_Page* pXFAPage = UnderlyingFromFPDFPage(page);
176 if (!pXFAPage)
177 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178
dsinclairdf4bc592016-03-31 20:34:43 -0700179 CXFA_FFPageView* pPageView = pXFAPage->GetXFAPageView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800180 if (!pPageView)
181 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182
dsinclairdf4bc592016-03-31 20:34:43 -0700183 CXFA_FFDocView* pDocView = pPageView->GetDocView();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800184 if (!pDocView)
185 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186
dsinclairdf4bc592016-03-31 20:34:43 -0700187 CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800188 if (!pWidgetHandler)
189 return -1;
190
tsepezcc4d6d82016-05-16 13:21:03 -0700191 std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
dsinclair935d8d52016-05-17 10:32:18 -0700192 pPageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form,
193 XFA_WidgetStatus_Viewable));
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800194 if (!pWidgetIterator)
195 return -1;
196
dsinclairdf4bc592016-03-31 20:34:43 -0700197 CXFA_FFWidget* pXFAAnnot = pWidgetIterator->MoveToNext();
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800198 while (pXFAAnnot) {
Dan Sinclair1b08df12017-02-09 09:17:20 -0500199 CFX_RectF rcBBox = pXFAAnnot->GetBBox(0);
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800200 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width,
201 rcBBox.top + rcBBox.height);
Lei Zhang1fed5a22017-06-01 11:52:22 -0700202 rcWidget.Inflate(1.0f, 1.0f);
Dan Sinclair05df0752017-03-14 14:43:42 -0400203 if (rcWidget.Contains(CFX_PointF(static_cast<float>(page_x),
204 static_cast<float>(page_y)))) {
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800205 return FPDF_FORMFIELD_XFA;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 }
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800207 pXFAAnnot = pWidgetIterator->MoveToNext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800209#endif // PDF_ENABLE_XFA
Lei Zhang7f9fdde2016-02-22 20:47:13 -0800210 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
212
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400213FPDF_EXPORT int FPDF_CALLCONV
214FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
215 FPDF_PAGE page,
216 double page_x,
217 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700218 if (!hHandle)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700219 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700220 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
221 if (!pPage)
222 return -1;
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700223 CPDF_InterForm interform(pPage->m_pDocument.Get());
Lei Zhangbdf72c32015-08-14 19:24:08 -0700224 int z_order = -1;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500225 (void)interform.GetControlAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400226 pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500227 &z_order);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700228 return z_order;
229}
230
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400231FPDF_EXPORT FPDF_FORMHANDLE FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
233 FPDF_FORMFILLINFO* formInfo) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800234#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800235 const int kRequiredVersion = 2;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800236#else // PDF_ENABLE_XFA
237 const int kRequiredVersion = 1;
238#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800239 if (!formInfo || formInfo->version != kRequiredVersion)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241
Tom Sepez540c4362015-11-24 13:33:57 -0800242 UnderlyingDocumentType* pDocument = UnderlyingFromFPDFDocument(document);
243 if (!pDocument)
244 return nullptr;
245
Tom Sepez40e9ff32015-11-30 12:39:54 -0800246#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -0700247 // If the CPDFXFA_Context has a FormFillEnvironment already then we've done
dsinclair8779fa82016-10-12 12:05:44 -0700248 // this and can just return the old Env. Otherwise, we'll end up setting a new
249 // environment into the XFADocument and, that could get weird.
dsinclair655fcca2016-10-11 13:53:37 -0700250 if (pDocument->GetFormFillEnv())
251 return pDocument->GetFormFillEnv();
dsinclaira939bfe2016-09-22 13:18:45 -0700252#endif
253
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700254 auto pFormFillEnv =
255 pdfium::MakeUnique<CPDFSDK_FormFillEnvironment>(pDocument, formInfo);
dsinclaira939bfe2016-09-22 13:18:45 -0700256
257#ifdef PDF_ENABLE_XFA
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700258 pDocument->SetFormFillEnv(pFormFillEnv.get());
Tom Sepez40e9ff32015-11-30 12:39:54 -0800259#endif // PDF_ENABLE_XFA
dsinclaira939bfe2016-09-22 13:18:45 -0700260
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700261 return pFormFillEnv.release(); // Caller takes ownership.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400264FPDF_EXPORT void FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700266 CPDFSDK_FormFillEnvironment* pFormFillEnv =
267 HandleToCPDFSDKEnvironment(hHandle);
Lei Zhang1fed5a22017-06-01 11:52:22 -0700268 if (!pFormFillEnv)
269 return;
dsinclaira939bfe2016-09-22 13:18:45 -0700270
Tom Sepez40e9ff32015-11-30 12:39:54 -0800271#ifdef PDF_ENABLE_XFA
dsinclair21161052016-10-05 15:46:15 -0700272 // Reset the focused annotations and remove the SDK document from the
273 // XFA document.
dsinclair7cbe68e2016-10-12 11:56:23 -0700274 pFormFillEnv->ClearAllFocusedAnnots();
dsinclairf3fbe832016-10-11 13:08:04 -0700275 // If the document was closed first, it's possible the XFA document
276 // is now a nullptr.
dsinclair521b7502016-11-02 13:02:28 -0700277 if (pFormFillEnv->GetXFAContext())
278 pFormFillEnv->GetXFAContext()->SetFormFillEnv(nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800279#endif // PDF_ENABLE_XFA
dsinclairf3fbe832016-10-11 13:08:04 -0700280 delete pFormFillEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
282
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400283FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
284 FPDF_PAGE page,
285 int modifier,
286 double page_x,
287 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
289 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700290 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500291 return pPageView->OnMouseMove(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292}
293
Lei Zhang63b01262017-08-31 08:54:46 -0700294FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnFocus(FPDF_FORMHANDLE hHandle,
295 FPDF_PAGE page,
296 int modifier,
297 double page_x,
298 double page_y) {
299 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
300 if (!pPageView)
301 return false;
302 return pPageView->OnFocus(CFX_PointF(page_x, page_y), modifier);
303}
304
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400305FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
306 FPDF_PAGE page,
307 int modifier,
308 double page_x,
309 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
311 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700312 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500313 return pPageView->OnLButtonDown(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700314}
315
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400316FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
317 FPDF_PAGE page,
318 int modifier,
319 double page_x,
320 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
322 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700323 return false;
Lei Zhang1fed5a22017-06-01 11:52:22 -0700324 return pPageView->OnLButtonUp(CFX_PointF(page_x, page_y), modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325}
326
Tom Sepez51da0932015-11-25 16:05:49 -0800327#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400328FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
329 FPDF_PAGE page,
330 int modifier,
331 double page_x,
332 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
334 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700335 return false;
Dan Sinclairf528eee2017-02-14 11:52:07 -0500336 return pPageView->OnRButtonDown(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700337}
338
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400339FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
340 FPDF_PAGE page,
341 int modifier,
342 double page_x,
343 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
345 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700346 return false;
Lei Zhang1fed5a22017-06-01 11:52:22 -0700347 return pPageView->OnRButtonUp(CFX_PointF(page_x, page_y), modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700348}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800349#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700350
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400351FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
352 FPDF_PAGE page,
353 int nKeyCode,
354 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
356 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700357 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700359}
360
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400361FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
362 FPDF_PAGE page,
363 int nKeyCode,
364 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
366 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700367 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700369}
370
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400371FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FORM_OnChar(FPDF_FORMHANDLE hHandle,
372 FPDF_PAGE page,
373 int nChar,
374 int modifier) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
376 if (!pPageView)
tsepez4cf55152016-11-02 14:37:54 -0700377 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700379}
380
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400381FPDF_EXPORT unsigned long FPDF_CALLCONV
382FORM_GetSelectedText(FPDF_FORMHANDLE hHandle,
383 FPDF_PAGE page,
384 void* buffer,
385 unsigned long buflen) {
Diana Gagedce2d722017-06-20 11:17:11 -0700386 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
387 if (!pPageView)
388 return 0;
389
Ryan Harrison275e2602017-09-18 14:23:18 -0400390 WideString wide_str_form_text = pPageView->GetSelectedText();
391 ByteString encoded_form_text = wide_str_form_text.UTF16LE_Encode();
Diana Gagedce2d722017-06-20 11:17:11 -0700392 unsigned long form_text_len = encoded_form_text.GetLength();
393
394 if (buffer && buflen >= form_text_len)
395 memcpy(buffer, encoded_form_text.c_str(), form_text_len);
396
397 return form_text_len;
398}
399
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400400FPDF_EXPORT void FPDF_CALLCONV FORM_ReplaceSelection(FPDF_FORMHANDLE hHandle,
401 FPDF_PAGE page,
402 FPDF_WIDESTRING wsText) {
Diana Gage1c7f1422017-07-24 11:19:52 -0700403 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
404 if (!pPageView)
405 return;
Diana Gageab390972017-07-28 17:07:39 -0700406
Ryan Harrison875e98c2017-09-27 10:53:11 -0400407 size_t len = WideString::WStringLength(wsText);
Ryan Harrison275e2602017-09-18 14:23:18 -0400408 WideString wide_str_text = WideString::FromUTF16LE(wsText, len);
Diana Gageab390972017-07-28 17:07:39 -0700409
410 pPageView->ReplaceSelection(wide_str_text);
Diana Gage1c7f1422017-07-24 11:19:52 -0700411}
412
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400413FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
414FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700415 CPDFSDK_FormFillEnvironment* pFormFillEnv =
416 HandleToCPDFSDKEnvironment(hHandle);
417 if (!pFormFillEnv)
tsepez4cf55152016-11-02 14:37:54 -0700418 return false;
dsinclair7cbe68e2016-10-12 11:56:23 -0700419 return pFormFillEnv->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420}
421
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400422FPDF_EXPORT void FPDF_CALLCONV FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
423 FPDF_BITMAP bitmap,
424 FPDF_PAGE page,
425 int start_x,
426 int start_y,
427 int size_x,
428 int size_y,
429 int rotate,
430 int flags) {
Cary Clark399be5b2016-03-14 16:51:29 -0400431 FFLCommon(hHandle, bitmap, nullptr, page, start_x, start_y, size_x, size_y,
432 rotate, flags);
433}
434
435#ifdef _SKIA_SUPPORT_
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400436FPDF_EXPORT void FPDF_CALLCONV FPDF_FFLRecord(FPDF_FORMHANDLE hHandle,
437 FPDF_RECORDER recorder,
438 FPDF_PAGE page,
439 int start_x,
440 int start_y,
441 int size_x,
442 int size_y,
443 int rotate,
444 int flags) {
Cary Clark399be5b2016-03-14 16:51:29 -0400445 FFLCommon(hHandle, nullptr, recorder, page, start_x, start_y, size_x, size_y,
446 rotate, flags);
447}
448#endif
449
Tom Sepez40e9ff32015-11-30 12:39:54 -0800450#ifdef PDF_ENABLE_XFA
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400451FPDF_EXPORT void FPDF_CALLCONV FPDF_Widget_Undo(FPDF_DOCUMENT document,
452 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700453 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700454 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700455
dsinclair521b7502016-11-02 13:02:28 -0700456 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400457 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
458 pContext->GetDocType() != XFA_DocType::Static) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700459 return;
Dan Sinclaircdba7472017-03-23 09:17:10 -0400460 }
Bo Xufdc00a72014-10-28 23:03:33 -0700461
dsinclair221caf62016-04-04 12:08:40 -0700462 static_cast<CXFA_FFWidget*>(hWidget)->Undo();
Bo Xufdc00a72014-10-28 23:03:33 -0700463}
dsinclair521b7502016-11-02 13:02:28 -0700464
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400465FPDF_EXPORT void FPDF_CALLCONV FPDF_Widget_Redo(FPDF_DOCUMENT document,
466 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700467 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700469
dsinclair521b7502016-11-02 13:02:28 -0700470 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400471 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
472 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700474
dsinclair221caf62016-04-04 12:08:40 -0700475 static_cast<CXFA_FFWidget*>(hWidget)->Redo();
Bo Xufdc00a72014-10-28 23:03:33 -0700476}
477
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400478FPDF_EXPORT void FPDF_CALLCONV FPDF_Widget_SelectAll(FPDF_DOCUMENT document,
479 FPDF_WIDGET hWidget) {
dsinclair221caf62016-04-04 12:08:40 -0700480 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700482
dsinclair521b7502016-11-02 13:02:28 -0700483 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400484 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
485 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700487
dsinclair221caf62016-04-04 12:08:40 -0700488 static_cast<CXFA_FFWidget*>(hWidget)->SelectAll();
Bo Xufdc00a72014-10-28 23:03:33 -0700489}
dsinclair521b7502016-11-02 13:02:28 -0700490
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400491FPDF_EXPORT void FPDF_CALLCONV FPDF_Widget_Copy(FPDF_DOCUMENT document,
492 FPDF_WIDGET hWidget,
493 FPDF_WIDESTRING wsText,
494 FPDF_DWORD* size) {
dsinclair221caf62016-04-04 12:08:40 -0700495 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700497
dsinclair521b7502016-11-02 13:02:28 -0700498 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400499 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
500 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700502
Ryan Harrison275e2602017-09-18 14:23:18 -0400503 WideString wsCpText;
dsinclair221caf62016-04-04 12:08:40 -0700504 static_cast<CXFA_FFWidget*>(hWidget)->Copy(wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700505
Ryan Harrison275e2602017-09-18 14:23:18 -0400506 ByteString bsCpText = wsCpText.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -0700507 uint32_t len = bsCpText.GetLength() / sizeof(unsigned short);
thestigbefa4502016-05-26 20:15:19 -0700508 if (!wsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509 *size = len;
510 return;
511 }
Bo Xufdc00a72014-10-28 23:03:33 -0700512
weili47ca6922016-03-31 15:08:27 -0700513 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 if (real_size > 0) {
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400515 memcpy((void*)wsText,
516 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
517 real_size * sizeof(unsigned short));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
519 }
520 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700521}
Tom Sepezab277682016-02-17 10:07:21 -0800522
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400523FPDF_EXPORT void FPDF_CALLCONV FPDF_Widget_Cut(FPDF_DOCUMENT document,
524 FPDF_WIDGET hWidget,
525 FPDF_WIDESTRING wsText,
526 FPDF_DWORD* size) {
thestigbefa4502016-05-26 20:15:19 -0700527 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 return;
thestigbefa4502016-05-26 20:15:19 -0700529
dsinclair521b7502016-11-02 13:02:28 -0700530 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400531 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
532 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700534
Ryan Harrison275e2602017-09-18 14:23:18 -0400535 WideString wsCpText;
dsinclair221caf62016-04-04 12:08:40 -0700536 static_cast<CXFA_FFWidget*>(hWidget)->Cut(wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700537
Ryan Harrison275e2602017-09-18 14:23:18 -0400538 ByteString bsCpText = wsCpText.UTF16LE_Encode();
weili47ca6922016-03-31 15:08:27 -0700539 uint32_t len = bsCpText.GetLength() / sizeof(unsigned short);
dsinclair221caf62016-04-04 12:08:40 -0700540 if (!wsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 *size = len;
542 return;
543 }
Bo Xufdc00a72014-10-28 23:03:33 -0700544
weili47ca6922016-03-31 15:08:27 -0700545 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 if (real_size > 0) {
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400547 memcpy((void*)wsText,
548 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
549 real_size * sizeof(unsigned short));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
551 }
552 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700553}
Tom Sepezab277682016-02-17 10:07:21 -0800554
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400555FPDF_EXPORT void FPDF_CALLCONV FPDF_Widget_Paste(FPDF_DOCUMENT document,
556 FPDF_WIDGET hWidget,
557 FPDF_WIDESTRING wsText,
558 FPDF_DWORD size) {
dsinclair221caf62016-04-04 12:08:40 -0700559 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700561
dsinclair521b7502016-11-02 13:02:28 -0700562 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400563 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
564 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700565 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700566
Ryan Harrison275e2602017-09-18 14:23:18 -0400567 WideString wstr = WideString::FromUTF16LE(wsText, size);
dsinclair221caf62016-04-04 12:08:40 -0700568 static_cast<CXFA_FFWidget*>(hWidget)->Paste(wstr);
Bo Xufdc00a72014-10-28 23:03:33 -0700569}
Tom Sepezab277682016-02-17 10:07:21 -0800570
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400571FPDF_EXPORT void FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document,
573 FPDF_WIDGET hWidget,
574 float x,
575 float y,
576 FPDF_BYTESTRING bsText) {
dsinclair221caf62016-04-04 12:08:40 -0700577 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700579
dsinclair521b7502016-11-02 13:02:28 -0700580 CPDFXFA_Context* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400581 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
582 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700584
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585 CFX_PointF ptPopup;
586 ptPopup.x = x;
587 ptPopup.y = y;
Ryan Harrison275e2602017-09-18 14:23:18 -0400588 ByteStringView bs(bsText);
dsinclair221caf62016-04-04 12:08:40 -0700589 static_cast<CXFA_FFWidget*>(hWidget)->ReplaceSpellCheckWord(ptPopup, bs);
Bo Xufdc00a72014-10-28 23:03:33 -0700590}
Tom Sepezab277682016-02-17 10:07:21 -0800591
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400592FPDF_EXPORT void FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document,
594 FPDF_WIDGET hWidget,
595 float x,
596 float y,
597 FPDF_STRINGHANDLE* stringHandle) {
Tom Sepezab277682016-02-17 10:07:21 -0800598 if (!hWidget || !document)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700599 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700600
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700601 auto* pContext = static_cast<CPDFXFA_Context*>(document);
Dan Sinclaircdba7472017-03-23 09:17:10 -0400602 if (pContext->GetDocType() != XFA_DocType::Dynamic &&
603 pContext->GetDocType() != XFA_DocType::Static)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700605
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 CFX_PointF ptPopup;
607 ptPopup.x = x;
608 ptPopup.y = y;
Ryan Harrison275e2602017-09-18 14:23:18 -0400609 auto sSuggestWords = pdfium::MakeUnique<std::vector<ByteString>>();
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700610 static_cast<CXFA_FFWidget*>(hWidget)->GetSuggestWords(ptPopup,
611 sSuggestWords.get());
612
613 // Caller takes ownership.
614 *stringHandle = ToFPDFStringHandle(sSuggestWords.release());
Bo Xufdc00a72014-10-28 23:03:33 -0700615}
Tom Sepezab277682016-02-17 10:07:21 -0800616
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400617FPDF_EXPORT int FPDF_CALLCONV
618FPDF_StringHandleCounts(FPDF_STRINGHANDLE sHandle) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400619 std::vector<ByteString>* sSuggestWords = FromFPDFStringHandle(sHandle);
Tom Sepezab277682016-02-17 10:07:21 -0800620 return sSuggestWords ? pdfium::CollectionSize<int>(*sSuggestWords) : -1;
Bo Xufdc00a72014-10-28 23:03:33 -0700621}
Tom Sepezab277682016-02-17 10:07:21 -0800622
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400623FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Tom Sepezab277682016-02-17 10:07:21 -0800624FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE sHandle,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700625 int index,
626 FPDF_BYTESTRING bsText,
627 FPDF_DWORD* size) {
Tom Sepezab277682016-02-17 10:07:21 -0800628 if (!sHandle || !size)
tsepez4cf55152016-11-02 14:37:54 -0700629 return false;
Tom Sepezab277682016-02-17 10:07:21 -0800630
631 int count = FPDF_StringHandleCounts(sHandle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632 if (index < 0 || index >= count)
tsepez4cf55152016-11-02 14:37:54 -0700633 return false;
Bo Xufdc00a72014-10-28 23:03:33 -0700634
Ryan Harrison275e2602017-09-18 14:23:18 -0400635 std::vector<ByteString>* sSuggestWords = FromFPDFStringHandle(sHandle);
weili47ca6922016-03-31 15:08:27 -0700636 uint32_t len = (*sSuggestWords)[index].GetLength();
Tom Sepezab277682016-02-17 10:07:21 -0800637 if (!bsText) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 *size = len;
tsepez4cf55152016-11-02 14:37:54 -0700639 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 }
641
weili47ca6922016-03-31 15:08:27 -0700642 uint32_t real_size = len < *size ? len : *size;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 if (real_size > 0)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400644 memcpy((void*)bsText, (*sSuggestWords)[index].c_str(), real_size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700645 *size = real_size;
tsepez4cf55152016-11-02 14:37:54 -0700646 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700647}
Tom Sepezab277682016-02-17 10:07:21 -0800648
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400649FPDF_EXPORT void FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle) {
Tom Sepezab277682016-02-17 10:07:21 -0800651 delete FromFPDFStringHandle(stringHandle);
Bo Xufdc00a72014-10-28 23:03:33 -0700652}
653
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400654FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle,
656 FPDF_BYTESTRING bsText,
657 FPDF_DWORD size) {
Tom Sepezab277682016-02-17 10:07:21 -0800658 if (!stringHandle || !bsText || size == 0)
tsepez4cf55152016-11-02 14:37:54 -0700659 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700660
Ryan Harrison275e2602017-09-18 14:23:18 -0400661 FromFPDFStringHandle(stringHandle)->push_back(ByteString(bsText, size));
tsepez4cf55152016-11-02 14:37:54 -0700662 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700663}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800664#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700665
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400666FPDF_EXPORT void FPDF_CALLCONV
667FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
668 int fieldType,
669 unsigned long color) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
671 pInterForm->SetHighlightColor(color, fieldType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700672}
673
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400674FPDF_EXPORT void FPDF_CALLCONV
675FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
677 pInterForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700678}
679
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400680FPDF_EXPORT void FPDF_CALLCONV
681FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
683 pInterForm->RemoveAllHighLight();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700684}
685
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400686FPDF_EXPORT void FPDF_CALLCONV FORM_OnAfterLoadPage(FPDF_PAGE page,
687 FPDF_FORMHANDLE hHandle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
tsepez4cf55152016-11-02 14:37:54 -0700689 pPageView->SetValid(true);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700690}
691
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400692FPDF_EXPORT void FPDF_CALLCONV FORM_OnBeforeClosePage(FPDF_PAGE page,
693 FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700694 CPDFSDK_FormFillEnvironment* pFormFillEnv =
695 HandleToCPDFSDKEnvironment(hHandle);
696 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700698
Tom Sepez540c4362015-11-24 13:33:57 -0800699 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
700 if (!pPage)
701 return;
702
dsinclair7cbe68e2016-10-12 11:56:23 -0700703 CPDFSDK_PageView* pPageView = pFormFillEnv->GetPageView(pPage, false);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700704 if (pPageView) {
tsepez4cf55152016-11-02 14:37:54 -0700705 pPageView->SetValid(false);
Tom Sepez50d12ad2015-11-24 09:50:51 -0800706 // RemovePageView() takes care of the delete for us.
dsinclair7cbe68e2016-10-12 11:56:23 -0700707 pFormFillEnv->RemovePageView(pPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 }
709}
710
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400711FPDF_EXPORT void FPDF_CALLCONV
712FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700713 CPDFSDK_FormFillEnvironment* pFormFillEnv =
714 HandleToCPDFSDKEnvironment(hHandle);
715 if (pFormFillEnv && pFormFillEnv->IsJSInitiated())
dsinclair7cbe68e2016-10-12 11:56:23 -0700716 pFormFillEnv->ProcJavascriptFun();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717}
718
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400719FPDF_EXPORT void FPDF_CALLCONV
720FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
dsinclairf3fbe832016-10-11 13:08:04 -0700721 CPDFSDK_FormFillEnvironment* pFormFillEnv =
722 HandleToCPDFSDKEnvironment(hHandle);
723 if (pFormFillEnv && pFormFillEnv->IsJSInitiated())
dsinclair7cbe68e2016-10-12 11:56:23 -0700724 pFormFillEnv->ProcOpenAction();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725}
726
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400727FPDF_EXPORT void FPDF_CALLCONV FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
728 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700729 CPDFSDK_FormFillEnvironment* pFormFillEnv =
730 HandleToCPDFSDKEnvironment(hHandle);
731 if (!pFormFillEnv)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 return;
733
dsinclair7cbe68e2016-10-12 11:56:23 -0700734 CPDF_Document* pDoc = pFormFillEnv->GetPDFDocument();
Lei Zhang01581062017-08-30 14:19:26 -0700735 const CPDF_Dictionary* pDict = pDoc->GetRoot();
Lei Zhang1fed5a22017-06-01 11:52:22 -0700736 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700738
Lei Zhang1fed5a22017-06-01 11:52:22 -0700739 CPDF_AAction aa(pDict->GetDictFor("AA"));
740 auto type = static_cast<CPDF_AAction::AActionType>(aaType);
741 if (aa.ActionExist(type)) {
742 CPDF_Action action = aa.GetAction(type);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 CPDFSDK_ActionHandler* pActionHandler =
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700744 HandleToCPDFSDKEnvironment(hHandle)->GetActionHandler();
Lei Zhang1fed5a22017-06-01 11:52:22 -0700745 pActionHandler->DoAction_Document(action, type, pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700746 }
747}
748
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400749FPDF_EXPORT void FPDF_CALLCONV FORM_DoPageAAction(FPDF_PAGE page,
750 FPDF_FORMHANDLE hHandle,
751 int aaType) {
dsinclairf3fbe832016-10-11 13:08:04 -0700752 CPDFSDK_FormFillEnvironment* pFormFillEnv =
753 HandleToCPDFSDKEnvironment(hHandle);
754 if (!pFormFillEnv)
tonikitoo401d4f22016-08-10 11:37:45 -0700755 return;
756
Tom Sepez540c4362015-11-24 13:33:57 -0800757 UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
758 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
Tom Sepezde4791d2015-10-30 12:13:10 -0700759 if (!pPDFPage)
760 return;
tonikitoo401d4f22016-08-10 11:37:45 -0700761
dsinclair7cbe68e2016-10-12 11:56:23 -0700762 if (!pFormFillEnv->GetPageView(pPage, false))
tonikitoo401d4f22016-08-10 11:37:45 -0700763 return;
764
Lei Zhangcddc8ed2017-06-20 17:26:44 -0700765 CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHandler();
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700766 CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict.Get();
dsinclair38fd8442016-09-15 10:15:32 -0700767 CPDF_AAction aa(pPageDict->GetDictFor("AA"));
Lei Zhang1fed5a22017-06-01 11:52:22 -0700768 CPDF_AAction::AActionType type = aaType == FPDFPAGE_AACTION_OPEN
769 ? CPDF_AAction::OpenPage
770 : CPDF_AAction::ClosePage;
771 if (aa.ActionExist(type)) {
772 CPDF_Action action = aa.GetAction(type);
773 pActionHandler->DoAction_Page(action, type, pFormFillEnv);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700774 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700775}