1 // 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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../public/fpdf_formfill.h"
8 #include "../../public/fpdfview.h"
9 #include "../include/fsdk_define.h"
10 #include "../include/fsdk_mgr.h"
13 #include "../include/javascript/IJavaScript.h"
16 DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, FPDF_PAGE page,double page_x, double page_y)
20 CPDF_Page * pPage = (CPDF_Page*) page;
22 CPDF_InterForm * pInterForm = NULL;
23 pInterForm = new CPDF_InterForm(pPage->m_pDocument,FALSE);
26 CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y);
32 CPDF_FormField* pFormField = pFormCtrl->GetField();
39 int nType = pFormField->GetFieldType();
44 DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo)
46 if(!document || !formInfo || formInfo->version!=1)
48 CPDF_Document * pDocument = (CPDF_Document*) document;
49 CPDFDoc_Environment * pEnv = NULL;
50 pEnv = new CPDFDoc_Environment(pDocument);
53 pEnv->RegAppHandle(formInfo);
55 if(pEnv->GetPDFDocument())
57 CPDFSDK_Document* pSDKDoc = new CPDFSDK_Document(pEnv->GetPDFDocument(), pEnv);
59 pEnv->SetCurrentDoc(pSDKDoc);
64 DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle)
68 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
71 ((CPDFDoc_Environment*)hHandle)->SetCurrentDoc(NULL);
74 delete (CPDFDoc_Environment*)hHandle;
78 DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
80 if (!hHandle || !page)
82 // CPDF_Page * pPage = (CPDF_Page*) page;
83 // CPDF_Document * pDoc = pPage->m_pDocument;
84 // CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
85 CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
88 CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDF_Page*)page);
94 // pEnv->FFI_DeviceToPage(page, point_x, point_y, &page_x, &page_y);
95 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
96 return pPageView->OnMouseMove(pt, modifier);
99 DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
101 if (!hHandle || !page)
103 CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
106 CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDF_Page*)page);
109 // double page_x = 0;
110 // double page_y = 0;
111 // pEnv->FFI_DeviceToPage(page, point_x, point_y, &page_x, &page_y);
112 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
113 return pPageView->OnLButtonDown(pt, modifier);
116 DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
118 if (!hHandle || !page)
120 CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
123 CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDF_Page*)page);
126 // double page_x = 0;
127 // double page_y = 0;
128 // pEnv->FFI_DeviceToPage(page, point_x, point_y, &page_x, &page_y);
129 CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
130 return pPageView->OnLButtonUp(pt, modifier);
133 DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
135 if (!hHandle || !page)
137 CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
140 CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDF_Page*)page);
145 return pPageView->OnKeyDown(nKeyCode, modifier);
148 DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
150 if (!hHandle || !page)
152 CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
155 CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDF_Page*)page);
160 return pPageView->OnKeyUp(nKeyCode, modifier);
164 DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nChar, int modifier)
166 if (!hHandle || !page)
168 CPDFSDK_Document* pFXDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
171 CPDFSDK_PageView* pPageView = pFXDoc->GetPageView((CPDF_Page*)page);
174 return pPageView->OnChar(nChar, modifier);
178 DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle)
182 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
185 //Kill the current focus.
186 return pSDKDoc->KillFocusAnnot(0);
189 DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle, FPDF_BITMAP bitmap, FPDF_PAGE page, int start_x, int start_y,
190 int size_x, int size_y, int rotate, int flags)
192 if (!hHandle || !page)
194 CPDF_Page* pPage = (CPDF_Page*)page;
196 CPDF_RenderOptions options;
197 if (flags & FPDF_LCD_TEXT)
198 options.m_Flags |= RENDER_CLEARTYPE;
200 options.m_Flags &= ~RENDER_CLEARTYPE;
203 if (flags & FPDF_GRAYSCALE)
205 options.m_ColorMode = RENDER_COLOR_GRAY;
206 options.m_ForeColor = 0;
207 options.m_BackColor = 0xffffff;
210 options.m_AddFlags = flags >> 8;
211 options.m_pOCContext = new CPDF_OCContext(pPage->m_pDocument);
213 CFX_AffineMatrix matrix;
214 pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
218 clip.right = start_x + size_x;
220 clip.bottom = start_y + size_y;
222 #ifdef _SKIA_SUPPORT_
223 CFX_SkiaDevice* pDevice = new CFX_SkiaDevice;
225 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
227 pDevice->Attach((CFX_DIBitmap*)bitmap);
228 pDevice->SaveState();
229 pDevice->SetClip_Rect(&clip);
231 CPDF_RenderContext* pContext = new CPDF_RenderContext;
232 CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
233 CPDFSDK_Document* pFXDoc = pEnv->GetCurrentDoc();
242 if(CPDFSDK_PageView* pPageView = pFXDoc->GetPageView(pPage))
244 pPageView->PageView_OnDraw(pDevice, &matrix, &options);
246 pDevice->RestoreState();
248 if(options.m_pOCContext)
250 delete options.m_pOCContext;
251 options.m_pOCContext = NULL;
266 DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle, int fieldType, unsigned long color)
270 // CPDFDoc_Environment* pEnv = (CPDFDoc_Environment* )hHandle;
271 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
274 if(CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm())
276 pInterForm->SetHighlightColor(color, fieldType);
283 DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha)
287 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
290 if(CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm())
291 pInterForm->SetHighlightAlpha(alpha);
295 DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle)
299 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
302 if(CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm())
303 pInterForm->RemoveAllHighLight();
307 DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
309 if(!hHandle || !page)
311 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
314 CPDF_Page* pPage = (CPDF_Page*)page;
315 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, TRUE);
318 pPageView->SetValid(TRUE);
322 DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
324 if(!hHandle || !page)
326 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
327 CPDF_Page* pPage = (CPDF_Page*)page;
328 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
331 pPageView->SetValid(FALSE);
332 // ReMovePageView() takes care of the delete for us.
333 pSDKDoc->ReMovePageView(pPage);
336 DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle)
340 if( CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc())
342 pSDKDoc->InitPageView();
343 if(((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
344 pSDKDoc->ProcJavascriptFun();
348 DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle)
352 if( CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc())
354 if(((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
355 pSDKDoc->ProcOpenAction();
358 DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle, int aaType)
362 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
365 CPDF_Document* pDoc = pSDKDoc->GetDocument();
366 CPDF_Dictionary* pDic = pDoc->GetRoot();
369 CPDF_AAction aa = pDic->GetDict(FX_BSTRC("AA"));
371 if(aa.ActionExist((CPDF_AAction::AActionType)aaType))
373 CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
374 CPDFSDK_ActionHandler *pActionHandler = ((CPDFDoc_Environment*)hHandle)->GetActionHander();
375 ASSERT(pActionHandler != NULL);
376 pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType, pSDKDoc);
380 DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page, FPDF_FORMHANDLE hHandle, int aaType)
382 if(!hHandle || !page)
384 CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetCurrentDoc();
385 CPDF_Page* pPage = (CPDF_Page*)page;
386 CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
389 CPDFDoc_Environment *pEnv = pSDKDoc->GetEnv();
390 ASSERT(pEnv != NULL);
392 CPDFSDK_ActionHandler *pActionHandler = pEnv->GetActionHander();
393 ASSERT(pActionHandler != NULL);
395 CPDF_Dictionary *pPageDict = pPage->m_pFormDict;
396 ASSERT(pPageDict != NULL);
398 CPDF_AAction aa = pPageDict->GetDict(FX_BSTRC("AA"));
400 FX_BOOL bExistOAAction = FALSE;
401 FX_BOOL bExistCAAction = FALSE;
402 if (FPDFPAGE_AACTION_OPEN == aaType)
404 bExistOAAction = aa.ActionExist(CPDF_AAction::OpenPage);
407 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
408 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
413 bExistCAAction = aa.ActionExist(CPDF_AAction::ClosePage);
416 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
417 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);