blob: f6a4f0a4880f3582481fb7dc221afa605f06badd [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
Tom Sepezdcbc02f2015-07-17 09:16:17 -070016namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018CPDFSDK_Document* FormHandleToSDKDoc(FPDF_FORMHANDLE hHandle) {
19 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
20 return pEnv ? pEnv->GetSDKDocument() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070021}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) {
24 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
25 return pSDKDoc ? pSDKDoc->GetInterForm() : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070026}
Bo Xufdc00a72014-10-28 23:03:33 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
29 FPDF_PAGE page) {
30 if (!page)
31 return nullptr;
Tom Sepez0b133982015-07-28 11:23:22 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
34 return pSDKDoc ? pSDKDoc->GetPageView((CPDFXFA_Page*)page, TRUE) : nullptr;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070035}
Bo Xufdc00a72014-10-28 23:03:33 -070036
Tom Sepezdcbc02f2015-07-17 09:16:17 -070037} // namespace
Bo Xufdc00a72014-10-28 23:03:33 -070038
Lei Zhangbdf72c32015-08-14 19:24:08 -070039DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
40 FPDF_PAGE page,
41 double page_x,
42 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -070043 if (!hHandle)
Tom Sepezdcbc02f2015-07-17 09:16:17 -070044 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -070045 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 if (pPage) {
Lei Zhangbdf72c32015-08-14 19:24:08 -070047 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
48 CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
49 pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y, nullptr);
50 if (!pFormCtrl)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -070052
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 CPDF_FormField* pFormField = pFormCtrl->GetField();
Lei Zhangbdf72c32015-08-14 19:24:08 -070054 if (!pFormField)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return -1;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056
57 int nType = pFormField->GetFieldType();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 return nType;
59 }
60
61 IXFA_PageView* pPageView = ((CPDFXFA_Page*)page)->GetXFAPageView();
62 if (pPageView) {
63 IXFA_WidgetHandler* pWidgetHandler = NULL;
64 IXFA_DocView* pDocView = pPageView->GetDocView();
65 if (!pDocView)
66 return -1;
67
68 pWidgetHandler = pDocView->GetWidgetHandler();
69 if (!pWidgetHandler)
70 return -1;
71
72 IXFA_Widget* pXFAAnnot = NULL;
73 IXFA_WidgetIterator* pWidgetIterator = pPageView->CreateWidgetIterator(
74 XFA_TRAVERSEWAY_Form,
75 XFA_WIDGETFILTER_Viewable | XFA_WIDGETFILTER_AllType);
76 if (!pWidgetIterator)
77 return -1;
78 pXFAAnnot = pWidgetIterator->MoveToNext();
79 while (pXFAAnnot) {
80 CFX_RectF rcBBox;
81 pWidgetHandler->GetBBox(pXFAAnnot, rcBBox, 0);
82 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top,
83 rcBBox.left + rcBBox.width,
84 rcBBox.top + rcBBox.height);
85 rcWidget.left -= 1.0f;
86 rcWidget.right += 1.0f;
87 rcWidget.bottom -= 1.0f;
88 rcWidget.top += 1.0f;
89
90 if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x),
91 static_cast<FX_FLOAT>(page_y))) {
92 pWidgetIterator->Release();
93 return FPDF_FORMFIELD_XFA;
94 }
95 pXFAAnnot = pWidgetIterator->MoveToNext();
96 }
97
98 pWidgetIterator->Release();
99 }
100
101 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Lei Zhangbdf72c32015-08-14 19:24:08 -0700104DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
105 FPDF_PAGE page,
106 double page_x,
107 double page_y) {
108 return FPDFPage_HasFormFieldAtPoint(hHandle, page, page_x, page_y);
109}
110
111DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
112 FPDF_PAGE page,
113 double page_x,
114 double page_y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700115 if (!hHandle)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700116 return -1;
Tom Sepezdb0be962015-10-16 14:00:21 -0700117 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
118 if (!pPage)
119 return -1;
Lei Zhangbdf72c32015-08-14 19:24:08 -0700120 CPDF_InterForm interform(pPage->m_pDocument, FALSE);
121 int z_order = -1;
122 (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
123 &z_order);
124 return z_order;
125}
126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127DLLEXPORT FPDF_FORMHANDLE STDCALL
128FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document,
129 FPDF_FORMFILLINFO* formInfo) {
130 if (!document || !formInfo || formInfo->version != 2)
131 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
134 CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo);
135 pEnv->SetSDKDocument(pDocument->GetSDKDocument(pEnv));
Bo Xufdc00a72014-10-28 23:03:33 -0700136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
138 pApp->AddFormFillEnv(pEnv);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 return pEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143DLLEXPORT void STDCALL
144FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) {
145 if (!hHandle)
146 return;
147 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
148 pApp->RemoveFormFillEnv((CPDFDoc_Environment*)hHandle);
149 delete (CPDFDoc_Environment*)hHandle;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150}
151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle,
153 FPDF_PAGE page,
154 int modifier,
155 double page_x,
156 double page_y) {
157 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
158 if (!pPageView)
159 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
162 return pPageView->OnMouseMove(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle,
166 FPDF_PAGE page,
167 int modifier,
168 double page_x,
169 double page_y) {
170 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
171 if (!pPageView)
172 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
175 return pPageView->OnLButtonDown(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle,
179 FPDF_PAGE page,
180 int modifier,
181 double page_x,
182 double page_y) {
183 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
184 if (!pPageView)
185 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
188 return pPageView->OnLButtonUp(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle,
192 FPDF_PAGE page,
193 int modifier,
194 double page_x,
195 double page_y) {
196 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
197 if (!pPageView)
198 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
201 return pPageView->OnRButtonDown(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700202}
203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle,
205 FPDF_PAGE page,
206 int modifier,
207 double page_x,
208 double page_y) {
209 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
210 if (!pPageView)
211 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
214 return pPageView->OnRButtonUp(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700215}
216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle,
218 FPDF_PAGE page,
219 int nKeyCode,
220 int modifier) {
221 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
222 if (!pPageView)
223 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle,
229 FPDF_PAGE page,
230 int nKeyCode,
231 int modifier) {
232 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
233 if (!pPageView)
234 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle,
240 FPDF_PAGE page,
241 int nChar,
242 int modifier) {
243 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
244 if (!pPageView)
245 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700248}
249
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle) {
251 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
252 if (!pSDKDoc)
253 return FALSE;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 return pSDKDoc->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle,
259 FPDF_BITMAP bitmap,
260 FPDF_PAGE page,
261 int start_x,
262 int start_y,
263 int size_x,
264 int size_y,
265 int rotate,
266 int flags) {
267 if (!hHandle || !page)
268 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
271 CPDFXFA_Document* pDocument = pPage->GetDocument();
272 if (!pDocument)
273 return;
274 CPDF_Document* pPDFDoc = pDocument->GetPDFDoc();
275 if (!pPDFDoc)
276 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
279 CPDFSDK_Document* pFXDoc = pEnv->GetSDKDocument();
280 if (!pFXDoc)
281 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 CFX_AffineMatrix matrix;
284 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 FX_RECT clip;
287 clip.left = start_x;
288 clip.right = start_x + size_x;
289 clip.top = start_y;
290 clip.bottom = start_y + size_y;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291
292#ifdef _SKIA_SUPPORT_
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 nonstd::unique_ptr<CFX_SkiaDevice> pDevice(new CFX_SkiaDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 nonstd::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296#endif
297
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 if (!pDevice)
299 return;
300 pDevice->Attach((CFX_DIBitmap*)bitmap);
301 pDevice->SaveState();
302 pDevice->SetClip_Rect(&clip);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700303
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 CPDF_RenderOptions options;
305 if (flags & FPDF_LCD_TEXT)
306 options.m_Flags |= RENDER_CLEARTYPE;
307 else
308 options.m_Flags &= ~RENDER_CLEARTYPE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 // Grayscale output
311 if (flags & FPDF_GRAYSCALE) {
312 options.m_ColorMode = RENDER_COLOR_GRAY;
313 options.m_ForeColor = 0;
314 options.m_BackColor = 0xffffff;
315 }
316 options.m_AddFlags = flags >> 8;
Tom Sepezae51c812015-08-05 12:34:06 -0700317 options.m_pOCContext = new CPDF_OCContext(pPDFDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page))
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800320 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 pDevice->RestoreState();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 delete options.m_pOCContext;
324 options.m_pOCContext = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700325}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document,
327 FPDF_WIDGET hWidget) {
328 if (NULL == hWidget || NULL == document)
329 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700330
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
332 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
333 pDocument->GetDocType() != XFA_DOCTYPE_Static)
334 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 IXFA_MenuHandler* pXFAMenuHander =
337 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
338 if (pXFAMenuHander == NULL)
339 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700340
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 pXFAMenuHander->Undo((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700342}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document,
344 FPDF_WIDGET hWidget) {
345 if (NULL == hWidget || NULL == document)
346 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700347
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
349 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
350 pDocument->GetDocType() != XFA_DOCTYPE_Static)
351 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 IXFA_MenuHandler* pXFAMenuHander =
354 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
355 if (pXFAMenuHander == NULL)
356 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700357
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 pXFAMenuHander->Redo((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700359}
360
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document,
362 FPDF_WIDGET hWidget) {
363 if (NULL == hWidget || NULL == document)
364 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700365
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
367 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
368 pDocument->GetDocType() != XFA_DOCTYPE_Static)
369 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700370
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 IXFA_MenuHandler* pXFAMenuHander =
372 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
373 if (pXFAMenuHander == NULL)
374 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 pXFAMenuHander->SelectAll((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700377}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document,
379 FPDF_WIDGET hWidget,
380 FPDF_WIDESTRING wsText,
381 FPDF_DWORD* size) {
382 if (NULL == hWidget || NULL == document)
383 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700384
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
386 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
387 pDocument->GetDocType() != XFA_DOCTYPE_Static)
388 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700389
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 IXFA_MenuHandler* pXFAMenuHander =
391 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
392 if (pXFAMenuHander == NULL)
393 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700394
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 CFX_WideString wsCpText;
396 pXFAMenuHander->Copy((IXFA_Widget*)hWidget, wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700397
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
399 int len = bsCpText.GetLength() / sizeof(unsigned short);
400 if (wsText == NULL) {
401 *size = len;
402 return;
403 }
Bo Xufdc00a72014-10-28 23:03:33 -0700404
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405 int real_size = len < *size ? len : *size;
406 if (real_size > 0) {
407 FXSYS_memcpy((void*)wsText,
408 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
409 real_size * sizeof(unsigned short));
410 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
411 }
412 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700413}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document,
415 FPDF_WIDGET hWidget,
416 FPDF_WIDESTRING wsText,
417 FPDF_DWORD* size) {
418 if (NULL == hWidget || NULL == document)
419 return;
420 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
421 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
422 pDocument->GetDocType() != XFA_DOCTYPE_Static)
423 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700424
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 IXFA_MenuHandler* pXFAMenuHander =
426 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
427 if (pXFAMenuHander == NULL)
428 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430 CFX_WideString wsCpText;
431 pXFAMenuHander->Cut((IXFA_Widget*)hWidget, wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700432
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
434 int len = bsCpText.GetLength() / sizeof(unsigned short);
435 if (wsText == NULL) {
436 *size = len;
437 return;
438 }
Bo Xufdc00a72014-10-28 23:03:33 -0700439
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700440 int real_size = len < *size ? len : *size;
441 if (real_size > 0) {
442 FXSYS_memcpy((void*)wsText,
443 bsCpText.GetBuffer(real_size * sizeof(unsigned short)),
444 real_size * sizeof(unsigned short));
445 bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short));
446 }
447 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700448}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document,
450 FPDF_WIDGET hWidget,
451 FPDF_WIDESTRING wsText,
452 FPDF_DWORD size) {
453 if (NULL == hWidget || NULL == document)
454 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700455
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
457 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
458 pDocument->GetDocType() != XFA_DOCTYPE_Static)
459 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700460
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700461 IXFA_MenuHandler* pXFAMenuHander =
462 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
463 if (pXFAMenuHander == NULL)
464 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700465
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700466 CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size);
467 pXFAMenuHander->Paste((IXFA_Widget*)hWidget, wstr);
Bo Xufdc00a72014-10-28 23:03:33 -0700468}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469DLLEXPORT void STDCALL
470FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document,
471 FPDF_WIDGET hWidget,
472 float x,
473 float y,
474 FPDF_BYTESTRING bsText) {
475 if (NULL == hWidget || NULL == document)
476 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700477
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
479 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
480 pDocument->GetDocType() != XFA_DOCTYPE_Static)
481 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700482
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 IXFA_MenuHandler* pXFAMenuHander =
484 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
485 if (pXFAMenuHander == NULL)
486 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700487
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 CFX_PointF ptPopup;
489 ptPopup.x = x;
490 ptPopup.y = y;
491 CFX_ByteStringC bs(bsText);
492 pXFAMenuHander->ReplaceSpellCheckWord((IXFA_Widget*)hWidget, ptPopup, bs);
Bo Xufdc00a72014-10-28 23:03:33 -0700493}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494DLLEXPORT void STDCALL
495FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document,
496 FPDF_WIDGET hWidget,
497 float x,
498 float y,
499 FPDF_STRINGHANDLE* stringHandle) {
500 if (NULL == hWidget || NULL == document)
501 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700502
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
504 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic &&
505 pDocument->GetDocType() != XFA_DOCTYPE_Static)
506 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 IXFA_MenuHandler* pXFAMenuHander =
509 CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
510 if (pXFAMenuHander == NULL)
511 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700512
Tom Sepezae51c812015-08-05 12:34:06 -0700513 CFX_ByteStringArray* sSuggestWords = new CFX_ByteStringArray;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 CFX_PointF ptPopup;
515 ptPopup.x = x;
516 ptPopup.y = y;
517 pXFAMenuHander->GetSuggestWords((IXFA_Widget*)hWidget, ptPopup,
518 *sSuggestWords);
519 *stringHandle = (FPDF_STRINGHANDLE)sSuggestWords;
Bo Xufdc00a72014-10-28 23:03:33 -0700520}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700521DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE stringHandle) {
522 if (stringHandle == NULL)
523 return -1;
524 CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
525 return sSuggestWords->GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700526}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700527DLLEXPORT FPDF_BOOL STDCALL
528FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE stringHandle,
529 int index,
530 FPDF_BYTESTRING bsText,
531 FPDF_DWORD* size) {
532 if (stringHandle == NULL || size == NULL)
533 return FALSE;
534 int count = FPDF_StringHandleCounts(stringHandle);
535 if (index < 0 || index >= count)
536 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700537
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538 CFX_ByteStringArray sSuggestWords = *(CFX_ByteStringArray*)stringHandle;
539 int len = sSuggestWords[index].GetLength();
Bo Xufdc00a72014-10-28 23:03:33 -0700540
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700541 if (bsText == NULL) {
542 *size = len;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700543 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 }
545
546 int real_size = len < *size ? len : *size;
547 if (real_size > 0)
548 FXSYS_memcpy((void*)bsText, (const FX_CHAR*)(sSuggestWords[index]),
549 real_size);
550 *size = real_size;
551
552 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700553}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554DLLEXPORT void STDCALL
555FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle) {
556 if (stringHandle == NULL)
557 return;
558 CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
559 delete sSuggestWords;
Bo Xufdc00a72014-10-28 23:03:33 -0700560}
561
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562DLLEXPORT FPDF_BOOL STDCALL
563FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle,
564 FPDF_BYTESTRING bsText,
565 FPDF_DWORD size) {
566 if (stringHandle == NULL || bsText == NULL || size <= 0)
567 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700568
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700569 CFX_ByteStringArray* stringArr = (CFX_ByteStringArray*)stringHandle;
570 CFX_ByteString bsStr(bsText, size);
Bo Xufdc00a72014-10-28 23:03:33 -0700571
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 stringArr->Add(bsStr);
573 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700574}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700575
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle,
577 int fieldType,
578 unsigned long color) {
579 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
580 pInterForm->SetHighlightColor(color, fieldType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700581}
582
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle,
584 unsigned char alpha) {
585 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
586 pInterForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700587}
588
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle) {
590 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
591 pInterForm->RemoveAllHighLight();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700592}
593
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page,
595 FPDF_FORMHANDLE hHandle) {
596 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
597 pPageView->SetValid(TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700598}
599
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page,
601 FPDF_FORMHANDLE hHandle) {
602 if (!hHandle || !page)
603 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700604
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
606 if (!pSDKDoc)
607 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700608
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
610 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
611 if (pPageView) {
612 pPageView->SetValid(FALSE);
613 // ReMovePageView() takes care of the delete for us.
614 pSDKDoc->ReMovePageView(pPage);
615 }
616}
617
618DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle) {
619 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
620 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
621 pSDKDoc->ProcJavascriptFun();
622}
623
624DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle) {
625 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
626 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
627 pSDKDoc->ProcOpenAction();
628}
629
630DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle,
631 int aaType) {
632 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
633 if (!pSDKDoc)
634 return;
635
636 CPDF_Document* pDoc = pSDKDoc->GetDocument()->GetPDFDoc();
637 CPDF_Dictionary* pDic = pDoc->GetRoot();
638 if (!pDic)
639 return;
Tom Sepez468e5892015-10-13 15:49:36 -0700640
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700641 CPDF_AAction aa = pDic->GetDict(FX_BSTRC("AA"));
642 if (aa.ActionExist((CPDF_AAction::AActionType)aaType)) {
643 CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
644 CPDFSDK_ActionHandler* pActionHandler =
645 ((CPDFDoc_Environment*)hHandle)->GetActionHander();
646 ASSERT(pActionHandler != NULL);
647 pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType,
648 pSDKDoc);
649 }
650}
651
652DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page,
653 FPDF_FORMHANDLE hHandle,
654 int aaType) {
655 if (!hHandle || !page)
656 return;
657 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
658 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
Tom Sepezde4791d2015-10-30 12:13:10 -0700659 CPDF_Page* pPDFPage = pPage->GetPDFPage();
660 if (!pPDFPage)
661 return;
662 if (pSDKDoc->GetPageView(pPage, FALSE)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 CPDFDoc_Environment* pEnv = pSDKDoc->GetEnv();
664 CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
Tom Sepezde4791d2015-10-30 12:13:10 -0700665 CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 CPDF_AAction aa = pPageDict->GetDict(FX_BSTRC("AA"));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 if (FPDFPAGE_AACTION_OPEN == aaType) {
Tom Sepezde4791d2015-10-30 12:13:10 -0700668 if (aa.ActionExist(CPDF_AAction::OpenPage)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
670 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
671 }
672 } else {
Tom Sepezde4791d2015-10-30 12:13:10 -0700673 if (aa.ActionExist(CPDF_AAction::ClosePage)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
675 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);
676 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700677 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700679}