blob: e86a47be5551c91992bc1039fb19bdec5dfb30cc [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
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_formfill.h"
8#include "../../public/fpdfview.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -07009#include "../../third_party/base/nonstd_unique_ptr.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010#include "../include/fsdk_define.h"
11#include "../include/fsdk_mgr.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070012#include "../include/fpdfxfa/fpdfxfa_doc.h"
Bo Xufdc00a72014-10-28 23:03:33 -070013#include "../include/fpdfxfa/fpdfxfa_page.h"
14#include "../include/fpdfxfa/fpdfxfa_app.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016#include "../include/javascript/IJavaScript.h"
17
Tom Sepezdcbc02f2015-07-17 09:16:17 -070018namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020CPDFSDK_Document* FormHandleToSDKDoc(FPDF_FORMHANDLE hHandle) {
21 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
22 return pEnv ? pEnv->GetSDKDocument() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070023}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
26 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
27 return pSDKDoc ? pSDKDoc->GetInterForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070028}
Bo Xufdc00a72014-10-28 23:03:33 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
31 FPDF_PAGE page) {
32 if (!page)
33 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -070034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
36 return pSDKDoc ? pSDKDoc->GetPageView((CPDFXFA_Page*)page, TRUE) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070037}
Bo Xufdc00a72014-10-28 23:03:33 -070038
Tom Sepezdcbc02f2015-07-17 09:16:17 -070039} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -070040
Lei Zhangbdf72c32015-08-14 19:24:08 -070041DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
42 FPDF_PAGE page,
43 double page_x,
44 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 if (!page || !hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -070046 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
49 if (pPage) {
Lei Zhangbdf72c32015-08-14 19:24:08 -070050 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
51 CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
52 pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y, nullptr);
53 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 CPDF_FormField* pFormField = pFormCtrl->GetField();
Lei Zhangbdf72c32015-08-14 19:24:08 -070057 if (!pFormField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059
60 int nType = pFormField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 return nType;
62 }
63
64 IXFA_PageView* pPageView = ((CPDFXFA_Page*)page)->GetXFAPageView();
65 if (pPageView) {
66 IXFA_WidgetHandler* pWidgetHandler = NULL;
67 IXFA_DocView* pDocView = pPageView->GetDocView();
68 if (!pDocView)
69 return -1;
70
71 pWidgetHandler = pDocView->GetWidgetHandler();
72 if (!pWidgetHandler)
73 return -1;
74
75 IXFA_Widget* pXFAAnnot = NULL;
76 IXFA_WidgetIterator* pWidgetIterator = pPageView->CreateWidgetIterator(
77 XFA_TRAVERSEWAY_Form,
78 XFA_WIDGETFILTER_Viewable | XFA_WIDGETFILTER_AllType);
79 if (!pWidgetIterator)
80 return -1;
81 pXFAAnnot = pWidgetIterator->MoveToNext();
82 while (pXFAAnnot) {
83 CFX_RectF rcBBox;
84 pWidgetHandler->GetBBox(pXFAAnnot, rcBBox, 0);
85 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top,
86 rcBBox.left + rcBBox.width,
87 rcBBox.top + rcBBox.height);
88 rcWidget.left -= 1.0f;
89 rcWidget.right += 1.0f;
90 rcWidget.bottom -= 1.0f;
91 rcWidget.top += 1.0f;
92
93 if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x),
94 static_cast<FX_FLOAT>(page_y))) {
95 pWidgetIterator->Release();
96 return FPDF_FORMFIELD_XFA;
97 }
98 pXFAAnnot = pWidgetIterator->MoveToNext();
99 }
100
101 pWidgetIterator->Release();
102 }
103
104 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}
106
Lei Zhangbdf72c32015-08-14 19:24:08 -0700107DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
108 FPDF_PAGE page,
109 double page_x,
110 double page_y) {
111 return FPDFPage_HasFormFieldAtPoint(hHandle, page, page_x, page_y);
112}
113
114DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
115 FPDF_PAGE page,
116 double page_x,
117 double page_y) {
118 if (!page || !hHandle)
119 return -1;
120
121 CPDF_Page* pPage = (CPDF_Page*)page;
122 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
123 int z_order = -1;
124 (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
125 &z_order);
126 return z_order;
127}
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129DLLEXPORT FPDF_FORMHANDLE STDCALL
130FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
131 FPDF_FORMFILLINFO* formInfo) {
132 if (!document || !formInfo || formInfo->version != 2)
133 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
136 CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo);
137 pEnv->SetSDKDocument(pDocument->GetSDKDocument(pEnv));
Bo Xufdc00a72014-10-28 23:03:33 -0700138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
140 pApp->AddFormFillEnv(pEnv);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 return pEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145DLLEXPORT void STDCALL
146FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
147 if (!hHandle)
148 return;
149 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
150 pApp->RemoveFormFillEnv((CPDFDoc_Environment*)hHandle);
151 delete (CPDFDoc_Environment*)hHandle;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
155 FPDF_PAGE page,
156 int modifier,
157 double page_x,
158 double page_y) {
159 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
160 if (!pPageView)
161 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
164 return pPageView->OnMouseMove(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165}
166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
168 FPDF_PAGE page,
169 int modifier,
170 double page_x,
171 double page_y) {
172 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
173 if (!pPageView)
174 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
177 return pPageView->OnLButtonDown(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
181 FPDF_PAGE page,
182 int modifier,
183 double page_x,
184 double page_y) {
185 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
186 if (!pPageView)
187 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
190 return pPageView->OnLButtonUp(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
194 FPDF_PAGE page,
195 int modifier,
196 double page_x,
197 double page_y) {
198 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
199 if (!pPageView)
200 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
203 return pPageView->OnRButtonDown(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700204}
205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
207 FPDF_PAGE page,
208 int modifier,
209 double page_x,
210 double page_y) {
211 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
212 if (!pPageView)
213 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
216 return pPageView->OnRButtonUp(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700217}
218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
220 FPDF_PAGE page,
221 int nKeyCode,
222 int modifier) {
223 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
224 if (!pPageView)
225 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228}
229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
231 FPDF_PAGE page,
232 int nKeyCode,
233 int modifier) {
234 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
235 if (!pPageView)
236 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle,
242 FPDF_PAGE page,
243 int nChar,
244 int modifier) {
245 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
246 if (!pPageView)
247 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
253 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
254 if (!pSDKDoc)
255 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 return pSDKDoc->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
261 FPDF_BITMAP bitmap,
262 FPDF_PAGE page,
263 int start_x,
264 int start_y,
265 int size_x,
266 int size_y,
267 int rotate,
268 int flags) {
269 if (!hHandle || !page)
270 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
273 CPDFXFA_Document* pDocument = pPage->GetDocument();
274 if (!pDocument)
275 return;
276 CPDF_Document* pPDFDoc = pDocument->GetPDFDoc();
277 if (!pPDFDoc)
278 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
281 CPDFSDK_Document* pFXDoc = pEnv->GetSDKDocument();
282 if (!pFXDoc)
283 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 CFX_AffineMatrix matrix;
286 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 FX_RECT clip;
289 clip.left = start_x;
290 clip.right = start_x + size_x;
291 clip.top = start_y;
292 clip.bottom = start_y + size_y;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700293
294#ifdef _SKIA_SUPPORT_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 nonstd::unique_ptr<CFX_SkiaDevice> pDevice(new CFX_SkiaDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 nonstd::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298#endif
299
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 if (!pDevice)
301 return;
302 pDevice->Attach((CFX_DIBitmap*)bitmap);
303 pDevice->SaveState();
304 pDevice->SetClip_Rect(&clip);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 CPDF_RenderOptions options;
307 if (flags & FPDF_LCD_TEXT)
308 options.m_Flags |= RENDER_CLEARTYPE;
309 else
310 options.m_Flags &= ~RENDER_CLEARTYPE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 // Grayscale output
313 if (flags & FPDF_GRAYSCALE) {
314 options.m_ColorMode = RENDER_COLOR_GRAY;
315 options.m_ForeColor = 0;
316 options.m_BackColor = 0xffffff;
317 }
318 options.m_AddFlags = flags >> 8;
Tom Sepezae51c812015-08-05 12:34:06 -0700319 options.m_pOCContext = new CPDF_OCContext(pPDFDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page))
322 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, &clip);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 delete options.m_pOCContext;
326 options.m_pOCContext = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document,
329 FPDF_WIDGET hWidget) {
330 if (NULL == hWidget || NULL == document)
331 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700332
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
334 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
335 pDocument->GetDocType() != XFA_DOCTYPE_Static)
336 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700337
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 IXFA_MenuHandler* pXFAMenuHander =
339 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
340 if (pXFAMenuHander == NULL)
341 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700342
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 pXFAMenuHander->Undo((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700344}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document,
346 FPDF_WIDGET hWidget) {
347 if (NULL == hWidget || NULL == document)
348 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700349
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
351 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
352 pDocument->GetDocType() != XFA_DOCTYPE_Static)
353 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700354
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 IXFA_MenuHandler* pXFAMenuHander =
356 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
357 if (pXFAMenuHander == NULL)
358 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700359
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 pXFAMenuHander->Redo((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700361}
362
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document,
364 FPDF_WIDGET hWidget) {
365 if (NULL == hWidget || NULL == document)
366 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700367
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
369 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
370 pDocument->GetDocType() != XFA_DOCTYPE_Static)
371 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 IXFA_MenuHandler* pXFAMenuHander =
374 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
375 if (pXFAMenuHander == NULL)
376 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 pXFAMenuHander->SelectAll((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700379}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document,
381 FPDF_WIDGET hWidget,
382 FPDF_WIDESTRING wsText,
383 FPDF_DWORD* size) {
384 if (NULL == hWidget || NULL == document)
385 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
388 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
389 pDocument->GetDocType() != XFA_DOCTYPE_Static)
390 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 IXFA_MenuHandler* pXFAMenuHander =
393 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
394 if (pXFAMenuHander == NULL)
395 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700396
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 CFX_WideString wsCpText;
398 pXFAMenuHander->Copy((IXFA_Widget*)hWidget, wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
401 int len = bsCpText.GetLength() / sizeof(unsigned short);
402 if (wsText == NULL) {
403 *size = len;
404 return;
405 }
Bo Xufdc00a72014-10-28 23:03:33 -0700406
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 int real_size = len < *size ? len : *size;
408 if (real_size > 0) {
409 FXSYS_memcpy((void*)wsText,
410 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
411 real_size * sizeof(unsigned short));
412 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
413 }
414 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700415}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document,
417 FPDF_WIDGET hWidget,
418 FPDF_WIDESTRING wsText,
419 FPDF_DWORD* size) {
420 if (NULL == hWidget || NULL == document)
421 return;
422 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
423 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
424 pDocument->GetDocType() != XFA_DOCTYPE_Static)
425 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700426
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 IXFA_MenuHandler* pXFAMenuHander =
428 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
429 if (pXFAMenuHander == NULL)
430 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700431
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 CFX_WideString wsCpText;
433 pXFAMenuHander->Cut((IXFA_Widget*)hWidget, wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700434
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700435 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
436 int len = bsCpText.GetLength() / sizeof(unsigned short);
437 if (wsText == NULL) {
438 *size = len;
439 return;
440 }
Bo Xufdc00a72014-10-28 23:03:33 -0700441
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 int real_size = len < *size ? len : *size;
443 if (real_size > 0) {
444 FXSYS_memcpy((void*)wsText,
445 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
446 real_size * sizeof(unsigned short));
447 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
448 }
449 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700450}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700451DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document,
452 FPDF_WIDGET hWidget,
453 FPDF_WIDESTRING wsText,
454 FPDF_DWORD size) {
455 if (NULL == hWidget || NULL == document)
456 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700457
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
459 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
460 pDocument->GetDocType() != XFA_DOCTYPE_Static)
461 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700462
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463 IXFA_MenuHandler* pXFAMenuHander =
464 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
465 if (pXFAMenuHander == NULL)
466 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700467
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size);
469 pXFAMenuHander->Paste((IXFA_Widget*)hWidget, wstr);
Bo Xufdc00a72014-10-28 23:03:33 -0700470}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471DLLEXPORT void STDCALL
472FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document,
473 FPDF_WIDGET hWidget,
474 float x,
475 float y,
476 FPDF_BYTESTRING bsText) {
477 if (NULL == hWidget || NULL == document)
478 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700479
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
481 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
482 pDocument->GetDocType() != XFA_DOCTYPE_Static)
483 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 IXFA_MenuHandler* pXFAMenuHander =
486 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
487 if (pXFAMenuHander == NULL)
488 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700489
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 CFX_PointF ptPopup;
491 ptPopup.x = x;
492 ptPopup.y = y;
493 CFX_ByteStringC bs(bsText);
494 pXFAMenuHander->ReplaceSpellCheckWord((IXFA_Widget*)hWidget, ptPopup, bs);
Bo Xufdc00a72014-10-28 23:03:33 -0700495}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700496DLLEXPORT void STDCALL
497FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document,
498 FPDF_WIDGET hWidget,
499 float x,
500 float y,
501 FPDF_STRINGHANDLE* stringHandle) {
502 if (NULL == hWidget || NULL == document)
503 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
506 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
507 pDocument->GetDocType() != XFA_DOCTYPE_Static)
508 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700509
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 IXFA_MenuHandler* pXFAMenuHander =
511 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
512 if (pXFAMenuHander == NULL)
513 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700514
Tom Sepezae51c812015-08-05 12:34:06 -0700515 CFX_ByteStringArray* sSuggestWords = new CFX_ByteStringArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 CFX_PointF ptPopup;
517 ptPopup.x = x;
518 ptPopup.y = y;
519 pXFAMenuHander->GetSuggestWords((IXFA_Widget*)hWidget, ptPopup,
520 *sSuggestWords);
521 *stringHandle = (FPDF_STRINGHANDLE)sSuggestWords;
Bo Xufdc00a72014-10-28 23:03:33 -0700522}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700523DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE stringHandle) {
524 if (stringHandle == NULL)
525 return -1;
526 CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
527 return sSuggestWords->GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700528}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700529DLLEXPORT FPDF_BOOL STDCALL
530FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE stringHandle,
531 int index,
532 FPDF_BYTESTRING bsText,
533 FPDF_DWORD* size) {
534 if (stringHandle == NULL || size == NULL)
535 return FALSE;
536 int count = FPDF_StringHandleCounts(stringHandle);
537 if (index < 0 || index >= count)
538 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700539
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540 CFX_ByteStringArray sSuggestWords = *(CFX_ByteStringArray*)stringHandle;
541 int len = sSuggestWords[index].GetLength();
Bo Xufdc00a72014-10-28 23:03:33 -0700542
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 if (bsText == NULL) {
544 *size = len;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700545 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 }
547
548 int real_size = len < *size ? len : *size;
549 if (real_size > 0)
550 FXSYS_memcpy((void*)bsText, (const FX_CHAR*)(sSuggestWords[index]),
551 real_size);
552 *size = real_size;
553
554 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700555}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556DLLEXPORT void STDCALL
557FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle) {
558 if (stringHandle == NULL)
559 return;
560 CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
561 delete sSuggestWords;
Bo Xufdc00a72014-10-28 23:03:33 -0700562}
563
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564DLLEXPORT FPDF_BOOL STDCALL
565FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle,
566 FPDF_BYTESTRING bsText,
567 FPDF_DWORD size) {
568 if (stringHandle == NULL || bsText == NULL || size <= 0)
569 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700570
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 CFX_ByteStringArray* stringArr = (CFX_ByteStringArray*)stringHandle;
572 CFX_ByteString bsStr(bsText, size);
Bo Xufdc00a72014-10-28 23:03:33 -0700573
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700574 stringArr->Add(bsStr);
575 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700576}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700577
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
579 int fieldType,
580 unsigned long color) {
581 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
582 pInterForm->SetHighlightColor(color, fieldType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700583}
584
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700585DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle,
586 unsigned char alpha) {
587 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
588 pInterForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700589}
590
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
592 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
593 pInterForm->RemoveAllHighLight();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700594}
595
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page,
597 FPDF_FORMHANDLE hHandle) {
598 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
599 pPageView->SetValid(TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700600}
601
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700602DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page,
603 FPDF_FORMHANDLE hHandle) {
604 if (!hHandle || !page)
605 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700606
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
608 if (!pSDKDoc)
609 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700610
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
612 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
613 if (pPageView) {
614 pPageView->SetValid(FALSE);
615 // ReMovePageView() takes care of the delete for us.
616 pSDKDoc->ReMovePageView(pPage);
617 }
618}
619
620DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
621 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
622 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
623 pSDKDoc->ProcJavascriptFun();
624}
625
626DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
627 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
628 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
629 pSDKDoc->ProcOpenAction();
630}
631
632DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
633 int aaType) {
634 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
635 if (!pSDKDoc)
636 return;
637
638 CPDF_Document* pDoc = pSDKDoc->GetDocument()->GetPDFDoc();
639 CPDF_Dictionary* pDic = pDoc->GetRoot();
640 if (!pDic)
641 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700642
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 CPDF_AAction aa = pDic->GetDict(FX_BSTRC("AA"));
644 if (aa.ActionExist((CPDF_AAction::AActionType)aaType)) {
645 CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
646 CPDFSDK_ActionHandler* pActionHandler =
647 ((CPDFDoc_Environment*)hHandle)->GetActionHander();
648 ASSERT(pActionHandler != NULL);
649 pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType,
650 pSDKDoc);
651 }
652}
653
654DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page,
655 FPDF_FORMHANDLE hHandle,
656 int aaType) {
657 if (!hHandle || !page)
658 return;
659 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
660 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
661 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
662 if (pPageView) {
663 CPDFDoc_Environment* pEnv = pSDKDoc->GetEnv();
664 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
665 CPDF_Dictionary* pPageDict = pPage->GetPDFPage()->m_pFormDict;
666 CPDF_AAction aa = pPageDict->GetDict(FX_BSTRC("AA"));
667
668 FX_BOOL bExistOAAction = FALSE;
669 FX_BOOL bExistCAAction = FALSE;
670 if (FPDFPAGE_AACTION_OPEN == aaType) {
671 bExistOAAction = aa.ActionExist(CPDF_AAction::OpenPage);
672 if (bExistOAAction) {
673 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
674 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
675 }
676 } else {
677 bExistCAAction = aa.ActionExist(CPDF_AAction::ClosePage);
678 if (bExistCAAction) {
679 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
680 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);
681 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700682 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700684}