blob: 20beedd84a9f3f8dca9dbe3f7303a3c22d61077e [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
15
16#include "../include/javascript/IJavaScript.h"
17
Tom Sepezdcbc02f2015-07-17 09:16:17 -070018namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Tom Sepezdcbc02f2015-07-17 09:16:17 -070020CPDFSDK_Document* FormHandleToSDKDoc(FPDF_FORMHANDLE hHandle)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070022 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
23 return pEnv ? pEnv->GetSDKDocument() : nullptr;
24}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Tom Sepezdcbc02f2015-07-17 09:16:17 -070026CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle)
27{
28 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
29 return pSDKDoc ? pSDKDoc->GetInterForm() : nullptr;
30}
Bo Xufdc00a72014-10-28 23:03:33 -070031
Tom Sepezdcbc02f2015-07-17 09:16:17 -070032CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle, FPDF_PAGE page)
33{
34 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
35 return pSDKDoc ? pSDKDoc->GetPageView((CPDFXFA_Page*)page, TRUE) : nullptr;
36}
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
Tom Sepezdcbc02f2015-07-17 09:16:17 -070040DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(
41 FPDF_FORMHANDLE hHandle, FPDF_PAGE page, double page_x, double page_y)
42{
43 if(!page || !hHandle)
44 return -1;
45 CPDF_Page * pPage = ((CPDFXFA_Page*) page)->GetPDFPage();
46 if (pPage)
47 {
48 CPDF_InterForm * pInterForm = NULL;
49 pInterForm = new CPDF_InterForm(pPage->m_pDocument,FALSE);
50 if (!pInterForm)
51 return -1;
52 CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y);
53 if(!pFormCtrl)
54 {
55 delete pInterForm;
56 return -1;
57 }
58 CPDF_FormField* pFormField = pFormCtrl->GetField();
59 if(!pFormField)
60 {
61 delete pInterForm;
62 return -1;
63 }
Bo Xufdc00a72014-10-28 23:03:33 -070064
Tom Sepezdcbc02f2015-07-17 09:16:17 -070065 int nType = pFormField->GetFieldType();
66 delete pInterForm;
67 return nType;
68 }
Bo Xufdc00a72014-10-28 23:03:33 -070069
Tom Sepezdcbc02f2015-07-17 09:16:17 -070070 IXFA_PageView* pPageView = ((CPDFXFA_Page*)page)->GetXFAPageView();
71 if (pPageView)
72 {
73 IXFA_WidgetHandler* pWidgetHandler = NULL;
74 IXFA_DocView* pDocView = pPageView->GetDocView();
75 if (!pDocView)
76 return -1;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070077
Tom Sepezdcbc02f2015-07-17 09:16:17 -070078 pWidgetHandler = pDocView->GetWidgetHandler();
79 if (!pWidgetHandler)
80 return -1;
81
82 IXFA_Widget* pXFAAnnot = NULL;
83 IXFA_WidgetIterator* pWidgetIterator = pPageView->CreateWidgetIterator(XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Viewable|XFA_WIDGETFILTER_AllType);
84 if (!pWidgetIterator)
85 return -1;
86 pXFAAnnot = pWidgetIterator->MoveToNext();
87 while (pXFAAnnot) {
88 CFX_RectF rcBBox;
89 pWidgetHandler->GetBBox (pXFAAnnot, rcBBox, 0);
90 CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, rcBBox.left + rcBBox.width, rcBBox.top+rcBBox.height);
91 rcWidget.left -= 1.0f;
92 rcWidget.right += 1.0f;
93 rcWidget.bottom -= 1.0f;
94 rcWidget.top += 1.0f;
95
96 if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x),
97 static_cast<FX_FLOAT>(page_y))) {
98 pWidgetIterator->Release();
99 return FPDF_FORMFIELD_XFA;
100 }
101 pXFAAnnot = pWidgetIterator->MoveToNext();
102 }
103
104 pWidgetIterator->Release();
105 }
106
107 return -1;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108}
109
Bo Xu2b7a49d2014-11-14 17:40:50 -0800110DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700112 if (!document || !formInfo || formInfo->version != 2)
113 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700115 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
116 CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo);
117 pEnv->SetSDKDocument(pDocument->GetSDKDocument(pEnv));
Bo Xufdc00a72014-10-28 23:03:33 -0700118
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700119 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
120 pApp->AddFormFillEnv(pEnv);
121
122 return pEnv;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}
124
Bo Xu2b7a49d2014-11-14 17:40:50 -0800125DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700127 if (!hHandle)
128 return;
129 CPDFXFA_App* pApp = CPDFXFA_App::GetInstance();
130 pApp->RemoveFormFillEnv((CPDFDoc_Environment*)hHandle);
131 delete (CPDFDoc_Environment*)hHandle;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
134DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700135{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700136 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
137 if (!pPageView)
138 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700139
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700140 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
141 return pPageView->OnMouseMove(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
144DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
145{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700146 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
147 if (!pPageView)
148 return FALSE;
149
150 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
151 return pPageView->OnLButtonDown(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
154DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
155{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700156 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
157 if (!pPageView)
158 return FALSE;
159
160 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
161 return pPageView->OnLButtonUp(pt, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Bo Xufdc00a72014-10-28 23:03:33 -0700164DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
165{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700166 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
167 if (!pPageView)
168 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700169
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700170 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
171 return pPageView->OnRButtonDown(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700172}
173
174DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
175{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700176 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
177 if (!pPageView)
178 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700179
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700180 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
181 return pPageView->OnRButtonUp(pt, modifier);
Bo Xufdc00a72014-10-28 23:03:33 -0700182}
183
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
185{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700186 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
187 if (!pPageView)
188 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700189
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700190 return pPageView->OnKeyDown(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
193DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
194{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700195 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
196 if (!pPageView)
197 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700198
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700199 return pPageView->OnKeyUp(nKeyCode, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
201
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nChar, int modifier)
203{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700204 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
205 if (!pPageView)
206 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700208 return pPageView->OnChar(nChar, modifier);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209}
210
211DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle)
212{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700213 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
214 if (!pSDKDoc)
215 return FALSE;
216
217 return pSDKDoc->KillFocusAnnot(0);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700220DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle, FPDF_BITMAP bitmap, FPDF_PAGE page,
221 int start_x, int start_y, int size_x, int size_y, int rotate, int flags)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700223 if (!hHandle || !page)
224 return ;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700225
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700226 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
227 CPDFXFA_Document* pDocument = pPage->GetDocument();
228 if (!pDocument)
229 return;
230 CPDF_Document* pPDFDoc = pDocument->GetPDFDoc();
231 if (!pPDFDoc)
232 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700234 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
235 CPDFSDK_Document* pFXDoc = pEnv->GetSDKDocument();
236 if (!pFXDoc)
237 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700238
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700239 CFX_AffineMatrix matrix;
240 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700241
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700242 FX_RECT clip;
243 clip.left = start_x;
244 clip.right = start_x + size_x;
245 clip.top = start_y;
246 clip.bottom = start_y + size_y;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
248#ifdef _SKIA_SUPPORT_
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700249 nonstd::unique_ptr<CFX_SkiaDevice> pDevice(new CFX_SkiaDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250#else
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700251 nonstd::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252#endif
253
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700254 if (!pDevice)
255 return;
256 pDevice->Attach((CFX_DIBitmap*)bitmap);
257 pDevice->SaveState();
258 pDevice->SetClip_Rect(&clip);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700259
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700260 CPDF_RenderOptions options;
261 if (flags & FPDF_LCD_TEXT)
262 options.m_Flags |= RENDER_CLEARTYPE;
263 else
264 options.m_Flags &= ~RENDER_CLEARTYPE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700266 //Grayscale output
267 if (flags & FPDF_GRAYSCALE)
268 {
269 options.m_ColorMode = RENDER_COLOR_GRAY;
270 options.m_ForeColor = 0;
271 options.m_BackColor = 0xffffff;
272 }
273 options.m_AddFlags = flags >> 8;
274 options.m_pOCContext = FX_NEW CPDF_OCContext(pPDFDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700276 if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDFXFA_Page*)page))
277 pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, &clip);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700279 pDevice->RestoreState();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700281 delete options.m_pOCContext;
282 options.m_pOCContext = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
Bo Xufdc00a72014-10-28 23:03:33 -0700284DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
285{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700286 if (NULL == hWidget || NULL == document)
287 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700288
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700289 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
290 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
291 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700292
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700293 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
294 if (pXFAMenuHander == NULL)
295 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700296
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700297 pXFAMenuHander->Undo((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700298
299}
300DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
301{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700302 if (NULL == hWidget || NULL == document)
303 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700304
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700305 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
306 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
307 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700308
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700309 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
310 if (pXFAMenuHander == NULL)
311 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700312
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700313 pXFAMenuHander->Redo((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700314}
315
316DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document, FPDF_WIDGET hWidget)
317{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700318 if (NULL == hWidget || NULL == document)
319 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700320
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700321 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
322 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
323 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700324
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700325 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
326 if (pXFAMenuHander == NULL)
327 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700328
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700329 pXFAMenuHander->SelectAll((IXFA_Widget*)hWidget);
Bo Xufdc00a72014-10-28 23:03:33 -0700330}
331DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, FPDF_WIDESTRING wsText, FPDF_DWORD* size)
332{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700333 if (NULL == hWidget || NULL == document)
334 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700335
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700336 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
337 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
338 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700339
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700340 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
341 if (pXFAMenuHander == NULL)
342 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700343
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700344 CFX_WideString wsCpText;
345 pXFAMenuHander->Copy((IXFA_Widget*)hWidget, wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700346
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700347 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
348 int len = bsCpText.GetLength()/sizeof(unsigned short);
349 if (wsText == NULL) {
350 *size = len;
351 return;
352 }
Bo Xufdc00a72014-10-28 23:03:33 -0700353
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700354 int real_size = len < *size ? len : *size;
355 if (real_size > 0)
356 {
357 FXSYS_memcpy((void*)wsText,bsCpText.GetBuffer(real_size*sizeof(unsigned short)),real_size*sizeof(unsigned short));
358 bsCpText.ReleaseBuffer(real_size*sizeof(unsigned short));
359 }
360 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700361}
362DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, FPDF_WIDESTRING wsText, FPDF_DWORD* size)
363{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700364 if (NULL == hWidget || NULL == document)
365 return;
366 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
367 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
368 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700369
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700370 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
371 if (pXFAMenuHander == NULL)
372 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700373
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700374 CFX_WideString wsCpText;
375 pXFAMenuHander->Cut((IXFA_Widget*)hWidget, wsCpText);
Bo Xufdc00a72014-10-28 23:03:33 -0700376
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700377 CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode();
378 int len = bsCpText.GetLength()/sizeof(unsigned short);
379 if (wsText == NULL) {
380 *size = len;
381 return;
382 }
Bo Xufdc00a72014-10-28 23:03:33 -0700383
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700384 int real_size = len < *size ? len : *size;
385 if (real_size > 0)
386 {
387 FXSYS_memcpy((void*)wsText,bsCpText.GetBuffer(real_size*sizeof(unsigned short)),real_size*sizeof(unsigned short));
388 bsCpText.ReleaseBuffer(real_size*sizeof(unsigned short));
389 }
390 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700391}
392DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, FPDF_WIDESTRING wsText, FPDF_DWORD size)
393{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700394 if (NULL == hWidget || NULL == document)
395 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700396
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700397 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
398 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
399 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700400
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700401 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
402 if (pXFAMenuHander == NULL)
403 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700404
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700405 CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size);
406 pXFAMenuHander->Paste((IXFA_Widget*)hWidget, wstr);
Bo Xufdc00a72014-10-28 23:03:33 -0700407
408}
409DLLEXPORT void STDCALL FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, float x, float y, FPDF_BYTESTRING bsText)
410{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700411 if (NULL == hWidget || NULL == document)
412 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700413
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700414 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
415 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
416 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700417
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700418 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
419 if (pXFAMenuHander == NULL)
420 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700421
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700422 CFX_PointF ptPopup;
423 ptPopup.x = x;
424 ptPopup.y = y;
425 CFX_ByteStringC bs(bsText);
426 pXFAMenuHander->ReplaceSpellCheckWord((IXFA_Widget*)hWidget,ptPopup,bs);
Bo Xufdc00a72014-10-28 23:03:33 -0700427
428}
429DLLEXPORT void STDCALL FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document, FPDF_WIDGET hWidget, float x, float y, FPDF_STRINGHANDLE* stringHandle)
430{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700431 if (NULL == hWidget || NULL == document)
432 return;
Tom Sepezbdeeb8a2015-05-27 12:25:00 -0700433
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700434 CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document;
435 if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && pDocument->GetDocType() != XFA_DOCTYPE_Static)
436 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700437
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700438 IXFA_MenuHandler* pXFAMenuHander = CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler();
439 if (pXFAMenuHander == NULL)
440 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700441
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700442 CFX_ByteStringArray* sSuggestWords = FX_NEW CFX_ByteStringArray;
443 CFX_PointF ptPopup;
444 ptPopup.x = x;
445 ptPopup.y = y;
446 pXFAMenuHander->GetSuggestWords((IXFA_Widget*)hWidget, ptPopup, *sSuggestWords);
447 *stringHandle = (FPDF_STRINGHANDLE)sSuggestWords;
Bo Xufdc00a72014-10-28 23:03:33 -0700448}
449DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE stringHandle)
450{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700451 if (stringHandle == NULL)
452 return -1;
453 CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
454 return sSuggestWords->GetSize();
Bo Xufdc00a72014-10-28 23:03:33 -0700455}
456DLLEXPORT FPDF_BOOL STDCALL FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE stringHandle, int index, FPDF_BYTESTRING bsText, FPDF_DWORD* size)
457{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700458 if (stringHandle == NULL || size == NULL)
459 return FALSE;
460 int count = FPDF_StringHandleCounts(stringHandle);
461 if (index < 0|| index >= count)
462 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700463
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700464 CFX_ByteStringArray sSuggestWords = *(CFX_ByteStringArray*)stringHandle;
465 int len = sSuggestWords[index].GetLength();
Bo Xufdc00a72014-10-28 23:03:33 -0700466
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700467 if (bsText == NULL) {
468 *size = len;
469 return TRUE;
470 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700471
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700472 int real_size = len < *size ? len : *size;
473 if (real_size > 0)
474 FXSYS_memcpy((void*)bsText, (const FX_CHAR*)(sSuggestWords[index]), real_size);
475 *size = real_size;
Bo Xufdc00a72014-10-28 23:03:33 -0700476
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700477 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700478}
479DLLEXPORT void STDCALL FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle)
480{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700481 if (stringHandle == NULL)
482 return;
483 CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle;
484 delete sSuggestWords;
Bo Xufdc00a72014-10-28 23:03:33 -0700485}
486
487DLLEXPORT FPDF_BOOL STDCALL FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle, FPDF_BYTESTRING bsText, FPDF_DWORD size)
488{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700489 if (stringHandle == NULL || bsText == NULL || size <= 0)
490 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700491
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700492 CFX_ByteStringArray* stringArr = (CFX_ByteStringArray*)stringHandle;
493 CFX_ByteString bsStr(bsText, size);
Bo Xufdc00a72014-10-28 23:03:33 -0700494
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700495 stringArr->Add(bsStr);
496 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700497}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700498
499DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle, int fieldType, unsigned long color)
500{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700501 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
502 pInterForm->SetHighlightColor(color, fieldType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700503}
504
505DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha)
506{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700507 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
508 pInterForm->SetHighlightAlpha(alpha);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700509}
510
511DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle)
512{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700513 if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
514 pInterForm->RemoveAllHighLight();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700515}
516
517DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
518{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700519 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
520 pPageView->SetValid(TRUE);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700521}
522
523DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
524{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700525 if (!hHandle || !page)
526 return;
527
528 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
529 if (!pSDKDoc)
530 return;
531
532 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
533 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
534 if (pPageView)
535 {
536 pPageView->SetValid(FALSE);
537 // ReMovePageView() takes care of the delete for us.
538 pSDKDoc->ReMovePageView(pPage);
539 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700540}
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700541
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700542DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle)
543{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700544 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
545 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
546 pSDKDoc->ProcJavascriptFun();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700547}
548
549DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle)
550{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700551 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
552 if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
553 pSDKDoc->ProcOpenAction();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700554}
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700555
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700556DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle, int aaType)
557{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700558 CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
559 if (!pSDKDoc)
560 return;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700561
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700562 CPDF_Document* pDoc = pSDKDoc->GetDocument()->GetPDFDoc();
563 CPDF_Dictionary* pDic = pDoc->GetRoot();
564 if (!pDic)
565 return;
566 CPDF_AAction aa = pDic->GetDict(FX_BSTRC("AA"));
567 if(aa.ActionExist((CPDF_AAction::AActionType)aaType))
568 {
569 CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
570 CPDFSDK_ActionHandler *pActionHandler = ((CPDFDoc_Environment*)hHandle)->GetActionHander();
571 ASSERT(pActionHandler != NULL);
572 pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType, pSDKDoc);
573 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700574}
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700575
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700576DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page, FPDF_FORMHANDLE hHandle, int aaType)
577{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700578 if(!hHandle || !page)
579 return;
580 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
581 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
582 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
583 if(pPageView)
584 {
585 CPDFDoc_Environment *pEnv = pSDKDoc->GetEnv();
586 CPDFSDK_ActionHandler *pActionHandler = pEnv->GetActionHander();
587 CPDF_Dictionary *pPageDict = pPage->GetPDFPage()->m_pFormDict;
588 CPDF_AAction aa = pPageDict->GetDict(FX_BSTRC("AA"));
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700589
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700590 FX_BOOL bExistOAAction = FALSE;
591 FX_BOOL bExistCAAction = FALSE;
592 if (FPDFPAGE_AACTION_OPEN == aaType)
593 {
594 bExistOAAction = aa.ActionExist(CPDF_AAction::OpenPage);
595 if (bExistOAAction)
596 {
597 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
598 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
599 }
600 }
601 else
602 {
603 bExistCAAction = aa.ActionExist(CPDF_AAction::ClosePage);
604 if (bExistCAAction)
605 {
606 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
607 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);
608 }
609 }
610 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700611}