blob: 7954d2620ca54bd127962c768fc46f3bda956a35 [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),
Oliver Chang972b78d2015-10-30 12:59:29 -0700416 m_bChangeMask(FALSE),
417 m_bBeingDestroyed(FALSE) {
418}
Bo Xufdc00a72014-10-28 23:03:33 -0700419
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700421 m_bBeingDestroyed = TRUE;
422
423 for (auto& it : m_pageMap)
424 it.second->KillFocusAnnotIfNeeded();
425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 for (auto& it : m_pageMap)
427 delete it.second;
428 m_pageMap.clear();
429
430 delete m_pInterForm;
431 m_pInterForm = nullptr;
432
433 delete m_pOccontent;
434 m_pOccontent = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700435}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700436
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
438 FX_BOOL ReNew) {
439 auto it = m_pageMap.find(pPDFXFAPage);
440 if (it != m_pageMap.end())
441 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700442
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 if (!ReNew)
444 return nullptr;
445
446 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
447 m_pageMap[pPDFXFAPage] = pPageView;
448 // Delay to load all the annotations, to avoid endless loop.
449 pPageView->LoadFXAnnots();
450 return pPageView;
451}
452
453CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
454 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
455 return pPage ? GetPageView(pPage, TRUE) : nullptr;
456}
457
458CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
459 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
460 if (!pTempPage)
461 return nullptr;
462
463 auto it = m_pageMap.find(pTempPage);
464 return it->second;
465}
466
467void CPDFSDK_Document::ProcJavascriptFun() {
468 CPDFXFA_Document* pPDFDoc = GetDocument();
469 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
470 int iCount = docJS.CountJSActions();
471 if (iCount < 1)
472 return;
473 for (int i = 0; i < iCount; i++) {
474 CFX_ByteString csJSName;
475 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
476 if (m_pEnv->GetActionHander())
477 m_pEnv->GetActionHander()->DoAction_JavaScript(
478 jsAction, CFX_WideString::FromLocal(csJSName), this);
479 }
480}
481
482FX_BOOL CPDFSDK_Document::ProcOpenAction() {
483 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700484 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485
486 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
487 if (!pRoot)
488 return FALSE;
489
490 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
491 if (!pOpenAction)
492 pOpenAction = pRoot->GetArray("OpenAction");
493
494 if (!pOpenAction)
495 return FALSE;
496
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400497 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 return TRUE;
499
Dan Sinclairf1251c12015-10-20 16:24:45 -0400500 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700501 CPDF_Action action(pDict);
502 if (m_pEnv->GetActionHander())
503 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
504 return TRUE;
505 }
506 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700507}
508
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
510 if (!m_pOccontent)
511 m_pOccontent = new CPDF_OCContext(m_pDoc->GetPDFDoc());
512 return m_pOccontent;
513}
Bo Xufdc00a72014-10-28 23:03:33 -0700514
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
516 auto it = m_pageMap.find(pPDFXFAPage);
517 if (it == m_pageMap.end())
518 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700519
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 CPDFSDK_PageView* pPageView = it->second;
521 if (pPageView->IsLocked())
522 return;
523
Oliver Chang972b78d2015-10-30 12:59:29 -0700524 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700525 delete pPageView;
526 m_pageMap.erase(it);
527}
528
529CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
530 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
531 if (!pTempPage)
532 return NULL;
533 return pTempPage;
534}
535
536CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
537 if (!m_pInterForm)
538 m_pInterForm = new CPDFSDK_InterForm(this);
539 return m_pInterForm;
540}
541
542void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
543 CPDFSDK_Annot* pAnnot) {
544 for (const auto& it : m_pageMap) {
545 CPDFSDK_PageView* pPageView = it.second;
546 if (pPageView != pSender) {
547 pPageView->UpdateView(pAnnot);
548 }
549 }
550}
551
552CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
553 return m_pFocusAnnot;
554}
555
556FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700557 if (m_bBeingDestroyed)
558 return FALSE;
559
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 if (m_pFocusAnnot == pAnnot)
561 return TRUE;
562
563 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
564
565 if (m_pFocusAnnot) {
566 if (!KillFocusAnnot(nFlag))
567 return FALSE;
568 }
569 CPDFSDK_PageView* pPageView = NULL;
570 if (pAnnot)
571 pPageView = pAnnot->GetPageView();
572 if (pAnnot && pPageView->IsValid()) {
573 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
574
575 if (pAnnotHandler && !m_pFocusAnnot) {
576 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700577 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700578
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700579 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
580 return FALSE;
581 if (!m_pFocusAnnot) {
582 m_pFocusAnnot = pAnnot;
583 return TRUE;
584 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700585 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 }
587 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700588}
589
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
591 if (m_pFocusAnnot) {
592 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
593 if (pAnnotHandler) {
594 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
595 m_pFocusAnnot = NULL;
Bo Xufdc00a72014-10-28 23:03:33 -0700596
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597 if (!pAnnotHandler->Annot_OnChangeFocus(NULL, pFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700598 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700599
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
601 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
602 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
603 int nFieldType = pWidget->GetFieldType();
604 if (FIELDTYPE_TEXTFIELD == nFieldType ||
605 FIELDTYPE_COMBOBOX == nFieldType)
606 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
607 }
608
609 if (!m_pFocusAnnot)
610 return TRUE;
611 } else {
612 m_pFocusAnnot = pFocusAnnot;
613 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700614 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 }
616 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700617}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700618
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
620 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
621 return FALSE;
622 }
623
624 CPDFXFA_Page* pTempPage = NULL;
625 for (int i = nCount - 1; i >= 0; i--) {
626 pTempPage = GetPage(nStart + i);
627 if (pTempPage != NULL) {
628 ReMovePageView(pTempPage);
629 }
630 }
631 return TRUE;
632}
633
634void CPDFSDK_Document::OnCloseDocument() {
635 KillFocusAnnot();
636}
637
638FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
639 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
640 return dwPermissions & nFlag;
641}
642
Tom Sepezba038bc2015-10-08 12:03:00 -0700643IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644 ASSERT(m_pEnv != NULL);
645 return m_pEnv->GetJSRuntime();
646}
647
648CFX_WideString CPDFSDK_Document::GetPath() {
649 ASSERT(m_pEnv != NULL);
650 return m_pEnv->JS_docGetFilePath();
651}
652
653CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
654 CPDFXFA_Page* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700655 : m_page(page),
656 m_pSDKDoc(pSDKDoc),
657 m_CaptureWidget(nullptr),
658 m_bEnterWidget(FALSE),
659 m_bExitWidget(FALSE),
660 m_bOnWidget(FALSE),
661 m_bValid(FALSE),
662 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
664 if (pInterForm) {
665 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
666 if (page->GetPDFPage())
667 pPDFInterForm->FixPageFields(page->GetPDFPage());
668 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669}
670
671CPDFSDK_PageView::~CPDFSDK_PageView() {
672 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700673 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700674 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700676 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677
Lei Zhangbf60b292015-10-26 12:14:35 -0700678 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679}
680
681void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
682 CPDF_Matrix* pUser2Device,
683 CPDF_RenderOptions* pOptions,
684 FX_RECT* pClip) {
685 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
687 CPDFXFA_Page* pPage = GetPDFXFAPage();
688
689 if (pPage == NULL)
690 return;
691
Tom Sepeza8a39e22015-10-12 15:47:07 -0700692#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
694 CFX_Graphics gs;
695 gs.Create(pDevice);
696 if (pClip) {
697 CFX_RectF rectClip;
698 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
699 static_cast<FX_FLOAT>(pClip->top),
700 static_cast<FX_FLOAT>(pClip->Width()),
701 static_cast<FX_FLOAT>(pClip->Height()));
702 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700703 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700704 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
705 if (!pRenderContext)
706 return;
707 CXFA_RenderOptions renderOptions;
708 renderOptions.m_bHighlight = TRUE;
709 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
710 renderOptions);
711 pRenderContext->DoRender();
712 pRenderContext->StopRender();
713 pRenderContext->Release();
714 return;
715 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700716#endif // PDF_ENABLE_XFA
717
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700719 CPDFSDK_AnnotIterator annotIterator(this, true);
720 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700722 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 }
724}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700725
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700726CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
727 FX_FLOAT pageY) {
Lei Zhang50218532015-10-30 14:03:43 -0700728 const int nCount = m_pAnnotList->Count();
729 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
731 CFX_FloatRect annotRect;
732 pAnnot->GetRect(annotRect);
733 if (annotRect.Contains(pageX, pageY))
734 return pAnnot;
735 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700736 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737}
738
739CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
740 FX_FLOAT pageY) {
Lei Zhang50218532015-10-30 14:03:43 -0700741 const int nCount = m_pAnnotList->Count();
Lei Zhangbf60b292015-10-26 12:14:35 -0700742 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
744 if (pAnnot->GetSubType() == "Widget") {
745 CFX_FloatRect annotRect;
746 pAnnot->GetRect(annotRect);
747 if (annotRect.Contains(pageX, pageY))
748 return pAnnot;
749 }
750 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700751 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700752}
753
754CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
755 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
757 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700758 CPDFSDK_AnnotIterator annotIterator(this, false);
759 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
761 if (rc.Contains(pageX, pageY))
762 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 }
764
Lei Zhangbf60b292015-10-26 12:14:35 -0700765 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766}
767
768CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
769 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
771 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700772 CPDFSDK_AnnotIterator annotIterator(this, false);
773 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700774 if (pSDKAnnot->GetType() == "Widget" ||
775 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
776 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
777 CPDF_Point point(pageX, pageY);
778 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
779 return pSDKAnnot;
780 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781 }
782
Lei Zhangbf60b292015-10-26 12:14:35 -0700783 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700784}
785
Oliver Chang972b78d2015-10-30 12:59:29 -0700786void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
787 // if there is a focused annot on the page, we should kill the focus first.
788 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
789 auto it =
790 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
791 if (it != m_fxAnnotArray.end())
792 KillFocusAnnot();
793 }
794}
795
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
797 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
798 if (pAnnotDic)
799 return pAnnotDic->KeyExist("AS");
800 return FALSE;
801}
802
803CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
804 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
805 ASSERT(pEnv);
806 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700807 if (!pAnnotHandler)
808 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809
Lei Zhangbf60b292015-10-26 12:14:35 -0700810 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700812 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813
Lei Zhangbf60b292015-10-26 12:14:35 -0700814 m_fxAnnotArray.push_back(pSDKAnnot);
815 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816 return pSDKAnnot;
817}
818
819CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
820 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700821 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822
823 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
824 if (pSDKAnnot)
825 return pSDKAnnot;
826
827 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700828 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
829
Lei Zhangbf60b292015-10-26 12:14:35 -0700830 if (pAnnotHandler)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700833 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700834
Lei Zhangbf60b292015-10-26 12:14:35 -0700835 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836 return pSDKAnnot;
837}
838
839CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
840 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
841}
842
843CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
844 CPDF_Dictionary* pDict) {
845 return NULL;
846}
847
848FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
849 if (!pAnnot)
850 return FALSE;
851 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
852 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
853 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
854 return FALSE;
855
Lei Zhangbf60b292015-10-26 12:14:35 -0700856 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
857 if (it != m_fxAnnotArray.end())
858 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700859 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700860 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861
862 return TRUE;
863}
864
865CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
866 if (m_page) {
867 return m_page->GetDocument()->GetPDFDoc();
868 }
869 return NULL;
870}
871
872CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
873 if (m_page) {
874 return m_page->GetPDFPage();
875 }
876
877 return NULL;
878}
879
Lei Zhang50218532015-10-30 14:03:43 -0700880size_t CPDFSDK_PageView::CountAnnots() const {
881 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882}
883
Lei Zhangbf60b292015-10-26 12:14:35 -0700884CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
885 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886}
887
888CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700889 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
890 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 return pAnnot;
892 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700893 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894}
895CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700896 if (!hWidget)
897 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700898
Lei Zhangbf60b292015-10-26 12:14:35 -0700899 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700900 if (pAnnot->GetXFAWidget() == hWidget)
901 return pAnnot;
902 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700903 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700904}
905
906FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
907 FX_UINT nFlag) {
908 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
909 ASSERT(pEnv);
910 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
911 if (!pFXAnnot) {
912 KillFocusAnnot(nFlag);
913 } else {
914 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
915 ASSERT(pAnnotHandlerMgr);
916
917 FX_BOOL bRet =
918 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
919 if (bRet) {
920 SetFocusAnnot(pFXAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700921 }
922 return bRet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 }
924 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700925}
926
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700927FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
928 FX_UINT nFlag) {
929 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
930 ASSERT(pEnv);
931 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
932 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700933
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700934 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
935
936 if (pFXAnnot == NULL)
937 return FALSE;
938
939 FX_BOOL bRet =
940 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
941 if (bRet) {
942 SetFocusAnnot(pFXAnnot);
943 }
944 return TRUE;
945}
946
947FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
948 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
949 ASSERT(pEnv);
950 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
951 ASSERT(pAnnotHandlerMgr);
952
953 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
954
955 if (pFXAnnot == NULL)
956 return FALSE;
957
958 FX_BOOL bRet =
959 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
960 if (bRet) {
961 SetFocusAnnot(pFXAnnot);
962 }
963 return TRUE;
964}
965
966FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
967 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
968 ASSERT(pEnv);
969 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
970 ASSERT(pAnnotHandlerMgr);
971 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
972 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
973 FX_BOOL bRet = FALSE;
974 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
975 // Last focus Annot gets a chance to handle the event.
976 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
977 }
978 if (pFXAnnot && !bRet) {
979 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
980 return bRet;
981 }
982 return bRet;
983}
984
985FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
986 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
987 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
988 ASSERT(pAnnotHandlerMgr);
989 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
990 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
991 m_bExitWidget = TRUE;
992 m_bEnterWidget = FALSE;
993 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
994 }
995 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
996 m_bOnWidget = TRUE;
997 if (!m_bEnterWidget) {
998 m_bEnterWidget = TRUE;
999 m_bExitWidget = FALSE;
1000 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
1001 }
1002 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
1003 return TRUE;
1004 }
1005 if (m_bOnWidget) {
1006 m_bOnWidget = FALSE;
1007 m_bExitWidget = TRUE;
1008 m_bEnterWidget = FALSE;
1009 if (m_CaptureWidget) {
1010 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1011 m_CaptureWidget = NULL;
1012 }
1013 }
1014 return FALSE;
1015}
1016
1017FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1018 double deltaY,
1019 const CPDF_Point& point,
1020 int nFlag) {
1021 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001022 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1023 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1024 ASSERT(pAnnotHandlerMgr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1026 (int)deltaY, point);
1027 }
1028 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001029}
1030
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1032 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001033 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001034 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 ASSERT(pAnnotHandlerMgr);
1036 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1037 }
Bo Xufdc00a72014-10-28 23:03:33 -07001038
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001040}
1041
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1043 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001044 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1046 ASSERT(pAnnotHandlerMgr);
1047 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1048 }
1049 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001050}
1051
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001054}
1055
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001056void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001058 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059
1060 SetLock(TRUE);
1061 m_page->AddRef();
1062 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1063 IXFA_PageView* pageView = NULL;
1064 pageView = m_page->GetXFAPageView();
1065 ASSERT(pageView != NULL);
1066
1067 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1068 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1069 XFA_WIDGETFILTER_Viewable |
1070 XFA_WIDGETFILTER_AllType);
1071 if (!pWidgetHander) {
1072 m_page->Release();
1073 SetLock(FALSE);
1074 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001075 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076
Lei Zhangbf60b292015-10-26 12:14:35 -07001077 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001079 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081
Lei Zhangbf60b292015-10-26 12:14:35 -07001082 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084 }
1085
1086 pWidgetHander->Release();
1087 } else {
1088 CPDF_Page* pPage = m_page->GetPDFPage();
1089 ASSERT(pPage != NULL);
1090
1091 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1092 // Disable the default AP construction.
1093 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001094 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1096
Lei Zhang50218532015-10-30 14:03:43 -07001097 const int nCount = m_pAnnotList->Count();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001098 for (int i = 0; i < nCount; i++) {
1099 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001100 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101
1102 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1103 if (!pAnnot)
1104 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001105 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001106
1107 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1108 }
1109 }
1110 m_page->Release();
1111 SetLock(FALSE);
1112}
1113
1114void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1115 for (int i = 0; i < rects.GetSize(); i++) {
1116 CPDF_Rect rc = rects.GetAt(i);
1117 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1118 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1119 }
1120}
1121
1122void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1123 CPDF_Rect rcWindow = pAnnot->GetRect();
1124 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1125 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1126 rcWindow.bottom);
1127}
1128
1129int CPDFSDK_PageView::GetPageIndex() {
1130 if (m_page) {
1131 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1132 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1133 if (pDoc && pDic) {
1134 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1135 }
1136 }
1137 return -1;
1138}
1139
Lei Zhangbf60b292015-10-26 12:14:35 -07001140FX_BOOL CPDFSDK_PageView::IsValidAnnot(CPDF_Annot* p) const {
1141 if (!p)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001142 return FALSE;
Lei Zhangbf60b292015-10-26 12:14:35 -07001143
Lei Zhang50218532015-10-30 14:03:43 -07001144 const int nCount = m_pAnnotList->Count();
Lei Zhangbf60b292015-10-26 12:14:35 -07001145 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001146 if (m_pAnnotList->GetAt(i) == p)
1147 return TRUE;
1148 }
1149 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001150}
1151
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001152CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1153 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1154 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001155 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156
Lei Zhangbf60b292015-10-26 12:14:35 -07001157 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 if (pAnnot == pFocusAnnot)
1159 return pAnnot;
1160 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001161 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001162}