blob: 07b58c1a688d7a3abb4d121fde8a56a28d155d1e [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"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009#include "../include/fsdk_define.h"
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_doc.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070011#include "../include/fsdk_mgr.h"
Bo Xufdc00a72014-10-28 23:03:33 -070012#include "../include/fpdfxfa/fpdfxfa_page.h"
13#include "../include/fpdfxfa/fpdfxfa_app.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015#include "../include/javascript/IJavaScript.h"
16
Tom Sepezdcbc02f2015-07-17 09:16:17 -070017namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019CPDFSDK_Document* FormHandleToSDKDoc(FPDF_FORMHANDLE hHandle) {
20 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
21 return pEnv ? pEnv->GetSDKDocument() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070022}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
25 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
26 return pSDKDoc ? pSDKDoc->GetInterForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070027}
Bo Xufdc00a72014-10-28 23:03:33 -070028
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
30 FPDF_PAGE page) {
31 if (!page)
32 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
35 return pSDKDoc ? pSDKDoc->GetPageView((CPDFXFA_Page*)page, TRUE) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070036}
Bo Xufdc00a72014-10-28 23:03:33 -070037
Tom Sepezdcbc02f2015-07-17 09:16:17 -070038} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -070039
Lei Zhangbdf72c32015-08-14 19:24:08 -070040DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
41 FPDF_PAGE page,
42 double page_x,
43 double page_y) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 if (!page || !hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -070045 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -070046
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
48 if (pPage) {
Lei Zhangbdf72c32015-08-14 19:24:08 -070049 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
50 CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
51 pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y, nullptr);
52 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -070054
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 CPDF_FormField* pFormField = pFormCtrl->GetField();
Lei Zhangbdf72c32015-08-14 19:24:08 -070056 if (!pFormField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058
59 int nType = pFormField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 return nType;
61 }
62
63 IXFA_PageView* pPageView = ((CPDFXFA_Page*)page)->GetXFAPageView();
64 if (pPageView) {
65 IXFA_WidgetHandler* pWidgetHandler = NULL;
66 IXFA_DocView* pDocView = pPageView->GetDocView();
67 if (!pDocView)
68 return -1;
69
70 pWidgetHandler = pDocView->GetWidgetHandler();
71 if (!pWidgetHandler)
72 return -1;
73
74 IXFA_Widget* pXFAAnnot = NULL;
75 IXFA_WidgetIterator* pWidgetIterator = pPageView->CreateWidgetIterator(
76 XFA_TRAVERSEWAY_Form,
77 XFA_WIDGETFILTER_Viewable | XFA_WIDGETFILTER_AllType);
78 if (!pWidgetIterator)
79 return -1;
80 pXFAAnnot = pWidgetIterator->MoveToNext();
81 while (pXFAAnnot) {
82 CFX_RectF rcBBox;
83 pWidgetHandler->GetBBox(pXFAAnnot, rcBBox, 0);
84 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top,
85 rcBBox.left + rcBBox.width,
86 rcBBox.top + rcBBox.height);
87 rcWidget.left -= 1.0f;
88 rcWidget.right += 1.0f;
89 rcWidget.bottom -= 1.0f;
90 rcWidget.top += 1.0f;
91
92 if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x),
93 static_cast<FX_FLOAT>(page_y))) {
94 pWidgetIterator->Release();
95 return FPDF_FORMFIELD_XFA;
96 }
97 pXFAAnnot = pWidgetIterator->MoveToNext();
98 }
99
100 pWidgetIterator->Release();
101 }
102
103 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
Lei Zhangbdf72c32015-08-14 19:24:08 -0700106DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
107 FPDF_PAGE page,
108 double page_x,
109 double page_y) {
110 return FPDFPage_HasFormFieldAtPoint(hHandle, page, page_x, page_y);
111}
112
113DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
114 FPDF_PAGE page,
115 double page_x,
116 double page_y) {
117 if (!page || !hHandle)
118 return -1;
119
120 CPDF_Page* pPage = (CPDF_Page*)page;
121 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
122 int z_order = -1;
123 (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
124 &z_order);
125 return z_order;
126}
127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128DLLEXPORT FPDF_FORMHANDLE STDCALL
129FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
130 FPDF_FORMFILLINFO* formInfo) {
131 if (!document || !formInfo || formInfo->version != 2)
132 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
135 CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo);
136 pEnv->SetSDKDocument(pDocument->GetSDKDocument(pEnv));
Bo Xufdc00a72014-10-28 23:03:33 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
139 pApp->AddFormFillEnv(pEnv);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 return pEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144DLLEXPORT void STDCALL
145FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
146 if (!hHandle)
147 return;
148 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
149 pApp->RemoveFormFillEnv((CPDFDoc_Environment*)hHandle);
150 delete (CPDFDoc_Environment*)hHandle;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151}
152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
154 FPDF_PAGE page,
155 int modifier,
156 double page_x,
157 double page_y) {
158 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
159 if (!pPageView)
160 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
163 return pPageView->OnMouseMove(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164}
165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
167 FPDF_PAGE page,
168 int modifier,
169 double page_x,
170 double page_y) {
171 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
172 if (!pPageView)
173 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
176 return pPageView->OnLButtonDown(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177}
178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
180 FPDF_PAGE page,
181 int modifier,
182 double page_x,
183 double page_y) {
184 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
185 if (!pPageView)
186 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
189 return pPageView->OnLButtonUp(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
193 FPDF_PAGE page,
194 int modifier,
195 double page_x,
196 double page_y) {
197 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
198 if (!pPageView)
199 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
202 return pPageView->OnRButtonDown(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
206 FPDF_PAGE page,
207 int modifier,
208 double page_x,
209 double page_y) {
210 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
211 if (!pPageView)
212 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
215 return pPageView->OnRButtonUp(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700216}
217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
219 FPDF_PAGE page,
220 int nKeyCode,
221 int modifier) {
222 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
223 if (!pPageView)
224 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227}
228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
230 FPDF_PAGE page,
231 int nKeyCode,
232 int modifier) {
233 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
234 if (!pPageView)
235 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle,
241 FPDF_PAGE page,
242 int nChar,
243 int modifier) {
244 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
245 if (!pPageView)
246 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
252 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
253 if (!pSDKDoc)
254 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700255
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 return pSDKDoc->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257}
258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
260 FPDF_BITMAP bitmap,
261 FPDF_PAGE page,
262 int start_x,
263 int start_y,
264 int size_x,
265 int size_y,
266 int rotate,
267 int flags) {
268 if (!hHandle || !page)
269 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
272 CPDFXFA_Document* pDocument = pPage->GetDocument();
273 if (!pDocument)
274 return;
275 CPDF_Document* pPDFDoc = pDocument->GetPDFDoc();
276 if (!pPDFDoc)
277 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
280 CPDFSDK_Document* pFXDoc = pEnv->GetSDKDocument();
281 if (!pFXDoc)
282 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 CFX_AffineMatrix matrix;
285 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 FX_RECT clip;
288 clip.left = start_x;
289 clip.right = start_x + size_x;
290 clip.top = start_y;
291 clip.bottom = start_y + size_y;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292
293#ifdef _SKIA_SUPPORT_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 nonstd::unique_ptr<CFX_SkiaDevice> pDevice(new CFX_SkiaDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 nonstd::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297#endif
298
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 if (!pDevice)
300 return;
301 pDevice->Attach((CFX_DIBitmap*)bitmap);
302 pDevice->SaveState();
303 pDevice->SetClip_Rect(&clip);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 CPDF_RenderOptions options;
306 if (flags & FPDF_LCD_TEXT)
307 options.m_Flags |= RENDER_CLEARTYPE;
308 else
309 options.m_Flags &= ~RENDER_CLEARTYPE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311 // Grayscale output
312 if (flags & FPDF_GRAYSCALE) {
313 options.m_ColorMode = RENDER_COLOR_GRAY;
314 options.m_ForeColor = 0;
315 options.m_BackColor = 0xffffff;
316 }
317 options.m_AddFlags = flags >> 8;
Tom Sepezae51c812015-08-05 12:34:06 -0700318 options.m_pOCContext = new CPDF_OCContext(pPDFDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page))
321 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, &clip);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 pDevice->RestoreState();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324
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}