blob: c421673f15152d4d9a9f8b780096e7ef6ccd86c4 [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 Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangbf60b292015-10-26 12:14:35 -07007#include <algorithm>
8
Tom Sepez1ed8a212015-05-11 15:25:39 -07009#include "../../public/fpdf_ext.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070010#include "../../third_party/base/nonstd_unique_ptr.h"
Tom Sepez37458412015-10-06 11:33:46 -070011#include "../include/formfiller/FFL_FormFiller.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070012#include "../include/fsdk_define.h"
13#include "../include/fsdk_mgr.h"
14#include "../include/javascript/IJavaScript.h"
15
Tom Sepez37458412015-10-06 11:33:46 -070016#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070017#include "../include/fpdfxfa/fpdfxfa_doc.h"
18#include "../include/fpdfxfa/fpdfxfa_page.h"
19#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
21#if _FX_OS_ == _FX_ANDROID_
22#include "time.h"
23#else
24#include <ctime>
25#endif
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027class CFX_SystemHandler : public IFX_SystemHandler {
28 public:
Lei Zhangbf60b292015-10-26 12:14:35 -070029 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
30 : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070031 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070034 // IFX_SystemHandler
35 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
36 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
37 FX_BOOL IsSelectionImplemented() override;
38 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
39 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 return FALSE;
41 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070042 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
43 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
44 void SetCursor(int32_t nCursorType) override;
45 FX_HMENU CreatePopupMenu() override { return NULL; }
46 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
47 int32_t nIDNewItem,
48 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return FALSE;
50 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
52 int32_t nIDItem,
53 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 return FALSE;
55 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070056 int32_t TrackPopupMenu(FX_HMENU hMenu,
57 int32_t x,
58 int32_t y,
59 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 return -1;
61 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070062 void DestroyMenu(FX_HMENU hMenu) override {}
63 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
64 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
65 CFX_ByteString sFontFaceName) override;
66 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
67 CFX_ByteString sFontFaceName,
68 uint8_t nCharset) override;
69 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
70 void KillTimer(int32_t nID) override;
71 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
73 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070074 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
76 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070077 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return m_pEnv->FFI_IsALTKeyDown(nFlag);
79 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070080 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
82 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070083 FX_SYSTEMTIME GetLocalTime() override;
84 int32_t GetCharSet() override { return m_nCharSet; }
85 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086
87 private:
88 CPDFDoc_Environment* m_pEnv;
89 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090};
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
93 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
97 // g_pFormFillApp->FFI_Invalidate();
98 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
99 CPDFXFA_Page* pPage = NULL;
100 CPDFSDK_PageView* pPageView = NULL;
101 pPageView = pSDKAnnot->GetPageView();
102 pPage = pSDKAnnot->GetPDFXFAPage();
103 if (!pPage || !pPageView)
104 return;
105 CPDF_Matrix page2device;
106 pPageView->GetCurrentMatrix(page2device);
107 CPDF_Matrix device2page;
108 device2page.SetReverse(page2device);
109 FX_FLOAT left, top, right, bottom;
110 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
111 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
112 bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 CPDF_Rect rcPDF(left, bottom, right, top);
114 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
117 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
120 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
121 if (pFFL) {
122 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
123 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
124 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
125 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
Tom Sepez468e5892015-10-13 15:49:36 -0700126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
128 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700129 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 ASSERT(pPage);
131 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
132 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
136 if (m_pEnv) {
137 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
138 if (pInfo && pInfo->FFI_OutputSelectedRect)
139 return TRUE;
140 }
141 return FALSE;
142}
143
144CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
145 return "";
146}
147
148FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
149 int32_t nCharset,
150 CFX_ByteString sFontFaceName) {
151 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
Lei Zhang122ee212015-10-21 17:07:24 -0700152 if (!pFontMgr)
153 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154
Lei Zhang122ee212015-10-21 17:07:24 -0700155 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
156 if (!pFontMapper)
157 return FALSE;
158
159 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
160 if (nSize == 0) {
161 pFontMapper->LoadInstalledFonts();
162 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
163 }
164
165 for (int i = 0; i < nSize; ++i) {
166 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
167 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 }
169
170 return FALSE;
171}
172
173static int CharSet2CP(int charset) {
174 if (charset == 128)
175 return 932;
176 if (charset == 134)
177 return 936;
178 if (charset == 129)
179 return 949;
180 if (charset == 136)
181 return 950;
182 return 0;
183}
184CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
185 CPDF_Document* pDoc,
186 CFX_ByteString sFontFaceName,
187 uint8_t nCharset) {
188 if (pDoc) {
189 CFX_Font* pFXFont = new CFX_Font();
190 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
191 FALSE);
192 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
193 delete pFXFont;
194 return pFont;
195 }
196
197 return NULL;
198}
199
200int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
201 TimerCallback lpTimerFunc) {
202 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
203}
204void CFX_SystemHandler::KillTimer(int32_t nID) {
205 m_pEnv->FFI_KillTimer(nID);
206}
207
208FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
209 return m_pEnv->FFI_GetLocalTime();
210}
211
212CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
213 FPDF_FORMFILLINFO* pFFinfo)
214 : m_pAnnotHandlerMgr(NULL),
215 m_pActionHandler(NULL),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 m_pInfo(pFFinfo),
217 m_pSDKDoc(NULL),
218 m_pXFADoc(pDoc),
219 m_pIFormFiller(NULL) {
220 m_pSysHandler = new CFX_SystemHandler(this);
221}
222
223CPDFDoc_Environment::~CPDFDoc_Environment() {
224 delete m_pIFormFiller;
225 m_pIFormFiller = NULL;
226
227 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700228 if (pProvider->m_pEnvList.GetSize() == 0)
229 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230
231 delete m_pSysHandler;
232 m_pSysHandler = NULL;
233
234 delete m_pAnnotHandlerMgr;
235 m_pAnnotHandlerMgr = NULL;
236
237 delete m_pActionHandler;
238 m_pActionHandler = NULL;
239}
240
241int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
242 const FX_WCHAR* Title,
243 FX_UINT Type,
244 FX_UINT Icon) {
245 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
246 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
247 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
248 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
249 FPDF_WIDESTRING pTitle =
250 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
251 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
252 pTitle, Type, Icon);
253 bsMsg.ReleaseBuffer();
254 bsTitle.ReleaseBuffer();
255 return ret;
256 }
257 return -1;
258}
259
260int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
261 const FX_WCHAR* Title,
262 const FX_WCHAR* Default,
263 const FX_WCHAR* cLabel,
264 FPDF_BOOL bPassword,
265 void* response,
266 int length) {
267 if (m_pInfo && m_pInfo->m_pJsPlatform &&
268 m_pInfo->m_pJsPlatform->app_response) {
269 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
270 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
271 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
272 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
273 FPDF_WIDESTRING pQuestion =
274 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
275 FPDF_WIDESTRING pTitle =
276 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
277 FPDF_WIDESTRING pDefault =
278 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
279 FPDF_WIDESTRING pLabel =
280 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
281 int ret = m_pInfo->m_pJsPlatform->app_response(
282 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
283 response, length);
284 bsQuestion.ReleaseBuffer();
285 bsTitle.ReleaseBuffer();
286 bsDefault.ReleaseBuffer();
287 bsLabel.ReleaseBuffer();
288 return ret;
289 }
290 return -1;
291}
292
293CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
294 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
295 !m_pInfo->m_pJsPlatform->Field_browse) {
296 return L"";
297 }
298
299 const int nRequiredLen =
300 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
301 if (nRequiredLen <= 0)
302 return L"";
303
304 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
305 memset(pBuff.get(), 0, nRequiredLen);
306 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
307 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
308 if (nActualLen <= 0 || nActualLen > nRequiredLen)
309 return L"";
310
311 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
312 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
313 return wsRet;
314}
315
316CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
317 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
318 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
319 return L"";
320 }
321
322 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
323 m_pInfo->m_pJsPlatform, nullptr, 0);
324 if (nRequiredLen <= 0)
325 return L"";
326
327 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
328 memset(pBuff.get(), 0, nRequiredLen);
329 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
330 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
331 if (nActualLen <= 0 || nActualLen > nRequiredLen)
332 return L"";
333
334 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
335 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
336 return wsRet;
337}
338
339void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
340 int length,
341 const FX_WCHAR* URL) {
342 if (m_pInfo && m_pInfo->m_pJsPlatform &&
343 m_pInfo->m_pJsPlatform->Doc_submitForm) {
344 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
345 FPDF_WIDESTRING pDestination =
346 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
347 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
348 length, pDestination);
349 bsDestination.ReleaseBuffer();
350 }
351}
352
353void CPDFDoc_Environment::JS_docmailForm(void* mailData,
354 int length,
355 FPDF_BOOL bUI,
356 const FX_WCHAR* To,
357 const FX_WCHAR* Subject,
358 const FX_WCHAR* CC,
359 const FX_WCHAR* BCC,
360 const FX_WCHAR* Msg) {
361 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
362 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
363 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
364 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
365 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
366 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
367 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
368 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
369 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
370 FPDF_WIDESTRING pSubject =
371 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
372 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
373 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
374 bUI, pTo, pSubject, pCC, pBcc, pMsg);
375 bsTo.ReleaseBuffer();
376 bsCC.ReleaseBuffer();
377 bsBcc.ReleaseBuffer();
378 bsSubject.ReleaseBuffer();
379 bsMsg.ReleaseBuffer();
380 }
381}
382
Tom Sepezba038bc2015-10-08 12:03:00 -0700383IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700384 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700385 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700387 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700388 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389}
390
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
392 if (!m_pAnnotHandlerMgr)
393 m_pAnnotHandlerMgr = new CPDFSDK_AnnotHandlerMgr(this);
394 return m_pAnnotHandlerMgr;
Bo Xufdc00a72014-10-28 23:03:33 -0700395}
396
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
398 if (!m_pActionHandler)
Tom Sepez3b5f1242015-09-01 14:06:55 -0700399 m_pActionHandler = new CPDFSDK_ActionHandler();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 return m_pActionHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700401}
402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
404 if (!m_pIFormFiller)
405 m_pIFormFiller = new CFFL_IFormFiller(this);
406 return m_pIFormFiller;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407}
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
410 CPDFDoc_Environment* pEnv)
411 : m_pDoc(pDoc),
412 m_pInterForm(nullptr),
413 m_pFocusAnnot(nullptr),
414 m_pEnv(pEnv),
415 m_pOccontent(nullptr),
416 m_bChangeMask(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418CPDFSDK_Document::~CPDFSDK_Document() {
419 for (auto& it : m_pageMap)
420 delete it.second;
421 m_pageMap.clear();
422
423 delete m_pInterForm;
424 m_pInterForm = nullptr;
425
426 delete m_pOccontent;
427 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700428}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700430CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
431 FX_BOOL ReNew) {
432 auto it = m_pageMap.find(pPDFXFAPage);
433 if (it != m_pageMap.end())
434 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700435
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700436 if (!ReNew)
437 return nullptr;
438
439 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
440 m_pageMap[pPDFXFAPage] = pPageView;
441 // Delay to load all the annotations, to avoid endless loop.
442 pPageView->LoadFXAnnots();
443 return pPageView;
444}
445
446CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
447 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
448 return pPage ? GetPageView(pPage, TRUE) : nullptr;
449}
450
451CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
452 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
453 if (!pTempPage)
454 return nullptr;
455
456 auto it = m_pageMap.find(pTempPage);
457 return it->second;
458}
459
460void CPDFSDK_Document::ProcJavascriptFun() {
461 CPDFXFA_Document* pPDFDoc = GetDocument();
462 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
463 int iCount = docJS.CountJSActions();
464 if (iCount < 1)
465 return;
466 for (int i = 0; i < iCount; i++) {
467 CFX_ByteString csJSName;
468 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
469 if (m_pEnv->GetActionHander())
470 m_pEnv->GetActionHander()->DoAction_JavaScript(
471 jsAction, CFX_WideString::FromLocal(csJSName), this);
472 }
473}
474
475FX_BOOL CPDFSDK_Document::ProcOpenAction() {
476 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700477 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478
479 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
480 if (!pRoot)
481 return FALSE;
482
483 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
484 if (!pOpenAction)
485 pOpenAction = pRoot->GetArray("OpenAction");
486
487 if (!pOpenAction)
488 return FALSE;
489
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400490 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 return TRUE;
492
Dan Sinclairf1251c12015-10-20 16:24:45 -0400493 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 CPDF_Action action(pDict);
495 if (m_pEnv->GetActionHander())
496 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
497 return TRUE;
498 }
499 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700500}
501
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
503 if (!m_pOccontent)
504 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
505 return m_pOccontent;
506}
Bo Xufdc00a72014-10-28 23:03:33 -0700507
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
509 auto it = m_pageMap.find(pPDFXFAPage);
510 if (it == m_pageMap.end())
511 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700512
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700513 CPDFSDK_PageView* pPageView = it->second;
514 if (pPageView->IsLocked())
515 return;
516
517 delete pPageView;
518 m_pageMap.erase(it);
519}
520
521CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
522 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
523 if (!pTempPage)
524 return NULL;
525 return pTempPage;
526}
527
528CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
529 if (!m_pInterForm)
530 m_pInterForm = new CPDFSDK_InterForm(this);
531 return m_pInterForm;
532}
533
534void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
535 CPDFSDK_Annot* pAnnot) {
536 for (const auto& it : m_pageMap) {
537 CPDFSDK_PageView* pPageView = it.second;
538 if (pPageView != pSender) {
539 pPageView->UpdateView(pAnnot);
540 }
541 }
542}
543
544CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
545 return m_pFocusAnnot;
546}
547
548FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
549 if (m_pFocusAnnot == pAnnot)
550 return TRUE;
551
552 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
553
554 if (m_pFocusAnnot) {
555 if (!KillFocusAnnot(nFlag))
556 return FALSE;
557 }
558 CPDFSDK_PageView* pPageView = NULL;
559 if (pAnnot)
560 pPageView = pAnnot->GetPageView();
561 if (pAnnot && pPageView->IsValid()) {
562 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
563
564 if (pAnnotHandler && !m_pFocusAnnot) {
565 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700566 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700567
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
569 return FALSE;
570 if (!m_pFocusAnnot) {
571 m_pFocusAnnot = pAnnot;
572 return TRUE;
573 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700574 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 }
576 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700577}
578
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
580 if (m_pFocusAnnot) {
581 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
582 if (pAnnotHandler) {
583 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
584 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700585
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700587 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700588
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700589 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
590 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
591 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
592 int nFieldType = pWidget->GetFieldType();
593 if (FIELDTYPE_TEXTFIELD == nFieldType ||
594 FIELDTYPE_COMBOBOX == nFieldType)
595 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
596 }
597
598 if (!m_pFocusAnnot)
599 return TRUE;
600 } else {
601 m_pFocusAnnot = pFocusAnnot;
602 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700603 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604 }
605 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700606}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700607
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
609 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
610 return FALSE;
611 }
612
613 CPDFXFA_Page* pTempPage = NULL;
614 for (int i = nCount - 1; i >= 0; i--) {
615 pTempPage = GetPage(nStart + i);
616 if (pTempPage != NULL) {
617 ReMovePageView(pTempPage);
618 }
619 }
620 return TRUE;
621}
622
623void CPDFSDK_Document::OnCloseDocument() {
624 KillFocusAnnot();
625}
626
627FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
628 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
629 return dwPermissions & nFlag;
630}
631
Tom Sepezba038bc2015-10-08 12:03:00 -0700632IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700633 ASSERT(m_pEnv != NULL);
634 return m_pEnv->GetJSRuntime();
635}
636
637CFX_WideString CPDFSDK_Document::GetPath() {
638 ASSERT(m_pEnv != NULL);
639 return m_pEnv->JS_docGetFilePath();
640}
641
642CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
643 CPDFXFA_Page* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700644 : m_page(page),
645 m_pSDKDoc(pSDKDoc),
646 m_CaptureWidget(nullptr),
647 m_bEnterWidget(FALSE),
648 m_bExitWidget(FALSE),
649 m_bOnWidget(FALSE),
650 m_bValid(FALSE),
651 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
653 if (pInterForm) {
654 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
655 if (page->GetPDFPage())
656 pPDFInterForm->FixPageFields(page->GetPDFPage());
657 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700658}
659
660CPDFSDK_PageView::~CPDFSDK_PageView() {
Tom Sepez396e8722015-09-09 10:16:08 -0700661 // if there is a focused annot on the page, we should kill the focus first.
662 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700663 auto it =
664 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
665 if (it != m_fxAnnotArray.end())
666 KillFocusAnnot();
Tom Sepez396e8722015-09-09 10:16:08 -0700667 }
668
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700670 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700671 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700673 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674
Lei Zhangbf60b292015-10-26 12:14:35 -0700675 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700676}
677
678void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
679 CPDF_Matrix* pUser2Device,
680 CPDF_RenderOptions* pOptions,
681 FX_RECT* pClip) {
682 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
684 CPDFXFA_Page* pPage = GetPDFXFAPage();
685
686 if (pPage == NULL)
687 return;
688
Tom Sepeza8a39e22015-10-12 15:47:07 -0700689#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
691 CFX_Graphics gs;
692 gs.Create(pDevice);
693 if (pClip) {
694 CFX_RectF rectClip;
695 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
696 static_cast<FX_FLOAT>(pClip->top),
697 static_cast<FX_FLOAT>(pClip->Width()),
698 static_cast<FX_FLOAT>(pClip->Height()));
699 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700700 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
702 if (!pRenderContext)
703 return;
704 CXFA_RenderOptions renderOptions;
705 renderOptions.m_bHighlight = TRUE;
706 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
707 renderOptions);
708 pRenderContext->DoRender();
709 pRenderContext->StopRender();
710 pRenderContext->Release();
711 return;
712 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700713#endif // PDF_ENABLE_XFA
714
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700715 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700716 CPDFSDK_AnnotIterator annotIterator(this, true);
717 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720 }
721}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700722
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
724 FX_FLOAT pageY) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700725 int nCount = CountAnnots();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726 for (int i = 0; i < nCount; i++) {
727 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
728 CFX_FloatRect annotRect;
729 pAnnot->GetRect(annotRect);
730 if (annotRect.Contains(pageX, pageY))
731 return pAnnot;
732 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700733 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700734}
735
736CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
737 FX_FLOAT pageY) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700738 int nCount = CountAnnots();
739 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700740 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
741 if (pAnnot->GetSubType() == "Widget") {
742 CFX_FloatRect annotRect;
743 pAnnot->GetRect(annotRect);
744 if (annotRect.Contains(pageX, pageY))
745 return pAnnot;
746 }
747 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700748 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749}
750
751CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
752 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700753 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
754 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700755 CPDFSDK_AnnotIterator annotIterator(this, false);
756 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
758 if (rc.Contains(pageX, pageY))
759 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760 }
761
Lei Zhangbf60b292015-10-26 12:14:35 -0700762 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763}
764
765CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
766 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
768 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700769 CPDFSDK_AnnotIterator annotIterator(this, false);
770 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771 if (pSDKAnnot->GetType() == "Widget" ||
772 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
773 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
774 CPDF_Point point(pageX, pageY);
775 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
776 return pSDKAnnot;
777 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778 }
779
Lei Zhangbf60b292015-10-26 12:14:35 -0700780 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781}
782
783FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
784 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
785 if (pAnnotDic)
786 return pAnnotDic->KeyExist("AS");
787 return FALSE;
788}
789
790CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
791 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
792 ASSERT(pEnv);
793 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700794 if (!pAnnotHandler)
795 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796
Lei Zhangbf60b292015-10-26 12:14:35 -0700797 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700799 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800
Lei Zhangbf60b292015-10-26 12:14:35 -0700801 m_fxAnnotArray.push_back(pSDKAnnot);
802 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803 return pSDKAnnot;
804}
805
806CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
807 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700808 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809
810 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
811 if (pSDKAnnot)
812 return pSDKAnnot;
813
814 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
816
Lei Zhangbf60b292015-10-26 12:14:35 -0700817 if (pAnnotHandler)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700819 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700820 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821
Lei Zhangbf60b292015-10-26 12:14:35 -0700822 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 return pSDKAnnot;
824}
825
826CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
827 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
828}
829
830CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
831 CPDF_Dictionary* pDict) {
832 return NULL;
833}
834
835FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
836 if (!pAnnot)
837 return FALSE;
838 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
839 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
840 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
841 return FALSE;
842
Lei Zhangbf60b292015-10-26 12:14:35 -0700843 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
844 if (it != m_fxAnnotArray.end())
845 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700847 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848
849 return TRUE;
850}
851
852CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
853 if (m_page) {
854 return m_page->GetDocument()->GetPDFDoc();
855 }
856 return NULL;
857}
858
859CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
860 if (m_page) {
861 return m_page->GetPDFPage();
862 }
863
864 return NULL;
865}
866
Lei Zhangbf60b292015-10-26 12:14:35 -0700867int CPDFSDK_PageView::CountAnnots() const {
868 return m_pAnnotList->Count();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700869}
870
Lei Zhangbf60b292015-10-26 12:14:35 -0700871CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
872 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873}
874
875CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700876 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
877 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 return pAnnot;
879 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700880 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881}
882CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700883 if (!hWidget)
884 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885
Lei Zhangbf60b292015-10-26 12:14:35 -0700886 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700887 if (pAnnot->GetXFAWidget() == hWidget)
888 return pAnnot;
889 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700890 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891}
892
893FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
894 FX_UINT nFlag) {
895 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
896 ASSERT(pEnv);
897 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
898 if (!pFXAnnot) {
899 KillFocusAnnot(nFlag);
900 } else {
901 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
902 ASSERT(pAnnotHandlerMgr);
903
904 FX_BOOL bRet =
905 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
906 if (bRet) {
907 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700908 }
909 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910 }
911 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700912}
913
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700914FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
915 FX_UINT nFlag) {
916 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
917 ASSERT(pEnv);
918 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
919 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700920
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700921 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
922
923 if (pFXAnnot == NULL)
924 return FALSE;
925
926 FX_BOOL bRet =
927 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
928 if (bRet) {
929 SetFocusAnnot(pFXAnnot);
930 }
931 return TRUE;
932}
933
934FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
935 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
936 ASSERT(pEnv);
937 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
938 ASSERT(pAnnotHandlerMgr);
939
940 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
941
942 if (pFXAnnot == NULL)
943 return FALSE;
944
945 FX_BOOL bRet =
946 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
947 if (bRet) {
948 SetFocusAnnot(pFXAnnot);
949 }
950 return TRUE;
951}
952
953FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
954 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
955 ASSERT(pEnv);
956 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
957 ASSERT(pAnnotHandlerMgr);
958 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
959 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
960 FX_BOOL bRet = FALSE;
961 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
962 // Last focus Annot gets a chance to handle the event.
963 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
964 }
965 if (pFXAnnot && !bRet) {
966 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
967 return bRet;
968 }
969 return bRet;
970}
971
972FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
973 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
974 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
975 ASSERT(pAnnotHandlerMgr);
976 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
977 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
978 m_bExitWidget = TRUE;
979 m_bEnterWidget = FALSE;
980 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
981 }
982 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
983 m_bOnWidget = TRUE;
984 if (!m_bEnterWidget) {
985 m_bEnterWidget = TRUE;
986 m_bExitWidget = FALSE;
987 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
988 }
989 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
990 return TRUE;
991 }
992 if (m_bOnWidget) {
993 m_bOnWidget = FALSE;
994 m_bExitWidget = TRUE;
995 m_bEnterWidget = FALSE;
996 if (m_CaptureWidget) {
997 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
998 m_CaptureWidget = NULL;
999 }
1000 }
1001 return FALSE;
1002}
1003
1004FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1005 double deltaY,
1006 const CPDF_Point& point,
1007 int nFlag) {
1008 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001009 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1010 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1011 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1013 (int)deltaY, point);
1014 }
1015 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001016}
1017
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1019 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001020 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001021 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 ASSERT(pAnnotHandlerMgr);
1023 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1024 }
Bo Xufdc00a72014-10-28 23:03:33 -07001025
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001027}
1028
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1030 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001031 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1033 ASSERT(pAnnotHandlerMgr);
1034 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1035 }
1036 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001037}
1038
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001041}
1042
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046
1047 SetLock(TRUE);
1048 m_page->AddRef();
1049 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1050 IXFA_PageView* pageView = NULL;
1051 pageView = m_page->GetXFAPageView();
1052 ASSERT(pageView != NULL);
1053
1054 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1055 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1056 XFA_WIDGETFILTER_Viewable |
1057 XFA_WIDGETFILTER_AllType);
1058 if (!pWidgetHander) {
1059 m_page->Release();
1060 SetLock(FALSE);
1061 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001062 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001063
Lei Zhangbf60b292015-10-26 12:14:35 -07001064 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001066 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001067 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001068
Lei Zhangbf60b292015-10-26 12:14:35 -07001069 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001070 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001071 }
1072
1073 pWidgetHander->Release();
1074 } else {
1075 CPDF_Page* pPage = m_page->GetPDFPage();
1076 ASSERT(pPage != NULL);
1077
1078 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1079 // Disable the default AP construction.
1080 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001081 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1083
1084 int nCount = m_pAnnotList->Count();
1085 for (int i = 0; i < nCount; i++) {
1086 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1087 CPDF_Document* pDoc = GetPDFDocument();
1088
1089 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1090
1091 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1092 if (!pAnnot)
1093 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001094 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095
1096 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1097 }
1098 }
1099 m_page->Release();
1100 SetLock(FALSE);
1101}
1102
1103void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1104 for (int i = 0; i < rects.GetSize(); i++) {
1105 CPDF_Rect rc = rects.GetAt(i);
1106 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1107 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1108 }
1109}
1110
1111void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1112 CPDF_Rect rcWindow = pAnnot->GetRect();
1113 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1114 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1115 rcWindow.bottom);
1116}
1117
1118int CPDFSDK_PageView::GetPageIndex() {
1119 if (m_page) {
1120 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1121 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1122 if (pDoc && pDic) {
1123 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1124 }
1125 }
1126 return -1;
1127}
1128
Lei Zhangbf60b292015-10-26 12:14:35 -07001129FX_BOOL CPDFSDK_PageView::IsValidAnnot(CPDF_Annot* p) const {
1130 if (!p)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001131 return FALSE;
Lei Zhangbf60b292015-10-26 12:14:35 -07001132
1133 int nCount = CountAnnots();
1134 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001135 if (m_pAnnotList->GetAt(i) == p)
1136 return TRUE;
1137 }
1138 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001139}
1140
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001141CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1142 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1143 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001144 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001145
Lei Zhangbf60b292015-10-26 12:14:35 -07001146 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001147 if (pAnnot == pFocusAnnot)
1148 return pAnnot;
1149 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001150 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001151}