Revert "FX Bool considered harmful, part 3"
[pdfium.git] / fpdfsdk / src / fpdfformfill.cpp
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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../public/fpdf_formfill.h"
8 #include "../../public/fpdfview.h"
9 #include "../../third_party/base/nonstd_unique_ptr.h"
10 #include "../include/fsdk_define.h"
11 #include "../include/fsdk_mgr.h"
12 #include "../include/javascript/IJavaScript.h"
13
14 namespace {
15
16 CPDFSDK_Document* FormHandleToSDKDoc(FPDF_FORMHANDLE hHandle)
17 {
18     CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
19     return pEnv ? pEnv->GetSDKDocument() : nullptr;
20 }
21
22 CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle)
23 {
24     CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
25     return pSDKDoc ? pSDKDoc->GetInterForm() : nullptr;
26 }
27
28 CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle, FPDF_PAGE page)
29 {
30     CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
31     return pSDKDoc ? pSDKDoc->GetPageView((CPDF_Page*)page, TRUE) : nullptr;
32 }
33
34 }  // namespace
35
36 DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(
37     FPDF_FORMHANDLE hHandle, FPDF_PAGE page, double page_x, double page_y)
38 {
39     if (!page || !hHandle)
40         return -1;
41     CPDF_Page* pPage = (CPDF_Page*) page;
42
43     nonstd::unique_ptr<CPDF_InterForm> pInterForm(
44         new CPDF_InterForm(pPage->m_pDocument, FALSE));
45     CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(
46         pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y);
47     if (!pFormCtrl)
48         return -1;
49
50     CPDF_FormField* pFormField = pFormCtrl->GetField();
51     if(!pFormField)
52         return -1;
53
54     return pFormField->GetFieldType();
55 }
56
57 DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(
58     FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo)
59 {
60     if (!document || !formInfo || formInfo->version != 1)
61         return nullptr;
62
63     CPDF_Document* pDocument = (CPDF_Document*)document;
64     CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo);
65     pEnv->SetSDKDocument(new CPDFSDK_Document(pDocument, pEnv));
66     return pEnv;
67 }
68
69 DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle)
70 {
71     if (!hHandle)
72         return;
73
74     CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle;
75     if (CPDFSDK_Document* pSDKDoc = pEnv->GetSDKDocument())
76     {
77         pEnv->SetSDKDocument(NULL);
78         delete pSDKDoc;
79     }
80     delete pEnv;
81 }
82
83 DLLEXPORT FPDF_BOOL STDCALL FORM_OnMouseMove(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
84 {
85     CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
86     if (!pPageView)
87         return FALSE;
88
89     CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
90     return pPageView->OnMouseMove(pt, modifier);
91 }
92
93 DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
94 {
95     CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
96     if (!pPageView)
97         return FALSE;
98
99     CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
100     return pPageView->OnLButtonDown(pt, modifier);
101 }
102
103 DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int modifier, double page_x, double page_y)
104 {
105     CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
106     if (!pPageView)
107         return FALSE;
108
109     CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y);
110     return pPageView->OnLButtonUp(pt, modifier);
111 }
112
113 DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
114 {
115     CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
116     if (!pPageView)
117         return FALSE;
118
119     return pPageView->OnKeyDown(nKeyCode, modifier);
120 }
121
122 DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyUp(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, int modifier)
123 {
124     CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
125     if (!pPageView)
126         return FALSE;
127
128     return pPageView->OnKeyUp(nKeyCode, modifier);
129 }
130
131 DLLEXPORT FPDF_BOOL STDCALL FORM_OnChar(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nChar,  int modifier)
132 {
133     CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
134     if (!pPageView)
135         return FALSE;
136
137     return pPageView->OnChar(nChar, modifier);
138 }
139
140 DLLEXPORT FPDF_BOOL STDCALL FORM_ForceToKillFocus(FPDF_FORMHANDLE hHandle)
141 {
142     CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
143     if (!pSDKDoc)
144         return FALSE;
145
146     return pSDKDoc->KillFocusAnnot(0);
147 }
148
149 DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle, FPDF_BITMAP bitmap, FPDF_PAGE page,
150                                     int start_x, int start_y, int size_x, int size_y, int rotate, int flags)
151 {
152     if (!hHandle || !page)
153         return;
154
155     CPDF_Page* pPage = (CPDF_Page*)page;
156     CPDF_RenderOptions options;
157     if (flags & FPDF_LCD_TEXT)
158         options.m_Flags |= RENDER_CLEARTYPE;
159     else
160         options.m_Flags &= ~RENDER_CLEARTYPE;
161
162     //Grayscale output
163     if (flags & FPDF_GRAYSCALE)
164     {
165         options.m_ColorMode = RENDER_COLOR_GRAY;
166         options.m_ForeColor = 0;
167         options.m_BackColor = 0xffffff;
168     }
169
170     options.m_AddFlags = flags >> 8;
171     options.m_pOCContext = new CPDF_OCContext(pPage->m_pDocument);
172
173     CFX_AffineMatrix matrix;
174     pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
175
176     FX_RECT clip;
177     clip.left = start_x;
178     clip.right = start_x + size_x;
179     clip.top = start_y;
180     clip.bottom = start_y + size_y;
181
182 #ifdef _SKIA_SUPPORT_
183     nonstd::unique_ptr<CFX_SkiaDevice> pDevice(new CFX_SkiaDevice);
184 #else
185     nonstd::unique_ptr<CFX_FxgeDevice> pDevice(new CFX_FxgeDevice);
186 #endif
187     pDevice->Attach((CFX_DIBitmap*)bitmap);
188     pDevice->SaveState();
189     pDevice->SetClip_Rect(&clip);
190
191     if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, pPage))
192         pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options);
193
194     pDevice->RestoreState();
195     delete options.m_pOCContext;
196 }
197
198 DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle, int fieldType, unsigned long color)
199 {
200     if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
201         pInterForm->SetHighlightColor(color, fieldType);
202 }
203
204 DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightAlpha(FPDF_FORMHANDLE hHandle, unsigned char alpha)
205 {
206     if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
207         pInterForm->SetHighlightAlpha(alpha);
208 }
209
210 DLLEXPORT void STDCALL FPDF_RemoveFormFieldHighlight(FPDF_FORMHANDLE hHandle)
211 {
212     if (CPDFSDK_InterForm* pInterForm = FormHandleToInterForm(hHandle))
213         pInterForm->RemoveAllHighLight();
214 }
215
216 DLLEXPORT void STDCALL FORM_OnAfterLoadPage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
217 {
218     if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
219         pPageView->SetValid(TRUE);
220 }
221
222 DLLEXPORT void STDCALL FORM_OnBeforeClosePage(FPDF_PAGE page, FPDF_FORMHANDLE hHandle)
223 {
224     if (!hHandle || !page)
225         return;
226
227     CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
228     if (!pSDKDoc)
229         return;
230
231     CPDF_Page* pPage = (CPDF_Page*)page;
232     CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
233     if (pPageView)
234     {
235         pPageView->SetValid(FALSE);
236         // ReMovePageView() takes care of the delete for us.
237         pSDKDoc->ReMovePageView(pPage);
238     }
239 }
240
241 DLLEXPORT void STDCALL FORM_DoDocumentJSAction(FPDF_FORMHANDLE hHandle)
242 {
243     CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
244     if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
245         pSDKDoc->ProcJavascriptFun();
246 }
247
248 DLLEXPORT void STDCALL FORM_DoDocumentOpenAction(FPDF_FORMHANDLE hHandle)
249 {
250     CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
251     if (pSDKDoc && ((CPDFDoc_Environment*)hHandle)->IsJSInitiated())
252         pSDKDoc->ProcOpenAction();
253 }
254
255 DLLEXPORT void STDCALL FORM_DoDocumentAAction(FPDF_FORMHANDLE hHandle, int aaType)
256 {
257     CPDFSDK_Document* pSDKDoc = FormHandleToSDKDoc(hHandle);
258     if (!pSDKDoc)
259         return;
260
261     CPDF_Document* pDoc = pSDKDoc->GetDocument();
262     CPDF_Dictionary* pDic = pDoc->GetRoot();
263     if (!pDic)
264         return;
265
266     CPDF_AAction aa = pDic->GetDict(FX_BSTRC("AA"));
267     if (aa.ActionExist((CPDF_AAction::AActionType)aaType))
268     {
269         CPDF_Action action = aa.GetAction((CPDF_AAction::AActionType)aaType);
270         CPDFSDK_ActionHandler *pActionHandler = ((CPDFDoc_Environment*)hHandle)->GetActionHander();
271         ASSERT(pActionHandler != NULL);
272         pActionHandler->DoAction_Document(action, (CPDF_AAction::AActionType)aaType, pSDKDoc);
273     }
274 }
275
276 DLLEXPORT void STDCALL FORM_DoPageAAction(FPDF_PAGE page, FPDF_FORMHANDLE hHandle, int aaType)
277 {
278     if(!hHandle || !page)
279         return;
280     CPDFSDK_Document* pSDKDoc = ((CPDFDoc_Environment*)hHandle)->GetSDKDocument();
281     CPDF_Page* pPage = (CPDF_Page*)page;
282     CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
283     if(pPageView)
284     {
285         CPDFDoc_Environment *pEnv = pSDKDoc->GetEnv();
286         ASSERT(pEnv != NULL);
287
288         CPDFSDK_ActionHandler *pActionHandler = pEnv->GetActionHander();
289         ASSERT(pActionHandler != NULL);
290
291         CPDF_Dictionary *pPageDict = pPage->m_pFormDict;
292         ASSERT(pPageDict != NULL);
293
294         CPDF_AAction aa = pPageDict->GetDict(FX_BSTRC("AA"));
295
296         FX_BOOL bExistOAAction = FALSE;
297         FX_BOOL bExistCAAction = FALSE;
298         if (FPDFPAGE_AACTION_OPEN == aaType)
299         {
300             bExistOAAction = aa.ActionExist(CPDF_AAction::OpenPage);
301             if (bExistOAAction)
302             {
303                 CPDF_Action action = aa.GetAction(CPDF_AAction::OpenPage);
304                 pActionHandler->DoAction_Page(action, CPDF_AAction::OpenPage, pSDKDoc);
305             }
306         }
307         else
308         {
309             bExistCAAction = aa.ActionExist(CPDF_AAction::ClosePage);
310             if (bExistCAAction)
311             {
312                 CPDF_Action action = aa.GetAction(CPDF_AAction::ClosePage);
313                 pActionHandler->DoAction_Page(action, CPDF_AAction::ClosePage, pSDKDoc);
314             }
315         }
316     }
317 }