blob: 2e03989d6c3ec0cf76158a55999b758bd44e3936 [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
Lei Zhangbde53d22015-11-12 22:21:30 -08009#include "fpdfsdk/include/fsdk_mgr.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080010
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/formfiller/FFL_FormFiller.h"
12#include "fpdfsdk/include/fsdk_define.h"
13#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_ext.h"
Lei Zhang8241df72015-11-06 14:38:48 -080015#include "third_party/base/nonstd_unique_ptr.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070016
Tom Sepez51da0932015-11-25 16:05:49 -080017#ifdef PDF_ENABLE_XFA
Tom Sepez37458412015-10-06 11:33:46 -070018#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070019#include "../include/fpdfxfa/fpdfxfa_doc.h"
20#include "../include/fpdfxfa/fpdfxfa_page.h"
21#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Tom Sepez51da0932015-11-25 16:05:49 -080023#endif
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024#if _FX_OS_ == _FX_ANDROID_
25#include "time.h"
26#else
27#include <ctime>
28#endif
29
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030class CFX_SystemHandler : public IFX_SystemHandler {
31 public:
Lei Zhangbf60b292015-10-26 12:14:35 -070032 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
33 : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070034 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070037 // IFX_SystemHandler
38 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
39 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
40 FX_BOOL IsSelectionImplemented() override;
41 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
42 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 return FALSE;
44 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070045 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
46 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
47 void SetCursor(int32_t nCursorType) override;
48 FX_HMENU CreatePopupMenu() override { return NULL; }
49 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
50 int32_t nIDNewItem,
51 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 return FALSE;
53 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070054 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
55 int32_t nIDItem,
56 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 return FALSE;
58 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070059 int32_t TrackPopupMenu(FX_HMENU hMenu,
60 int32_t x,
61 int32_t y,
62 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 return -1;
64 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070065 void DestroyMenu(FX_HMENU hMenu) override {}
66 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
67 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
68 CFX_ByteString sFontFaceName) override;
69 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
70 CFX_ByteString sFontFaceName,
71 uint8_t nCharset) override;
72 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
73 void KillTimer(int32_t nID) override;
74 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
76 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070077 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
79 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070080 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 return m_pEnv->FFI_IsALTKeyDown(nFlag);
82 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070083 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
85 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070086 FX_SYSTEMTIME GetLocalTime() override;
87 int32_t GetCharSet() override { return m_nCharSet; }
88 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089
90 private:
91 CPDFDoc_Environment* m_pEnv;
92 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093};
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
96 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800101 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
102 UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 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);
126 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800127 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 ASSERT(pPage);
129 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
130 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
134 if (m_pEnv) {
135 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
136 if (pInfo && pInfo->FFI_OutputSelectedRect)
137 return TRUE;
138 }
139 return FALSE;
140}
141
142CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
143 return "";
144}
145
146FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
147 int32_t nCharset,
148 CFX_ByteString sFontFaceName) {
149 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
Lei Zhang122ee212015-10-21 17:07:24 -0700150 if (!pFontMgr)
151 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152
Lei Zhang122ee212015-10-21 17:07:24 -0700153 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
154 if (!pFontMapper)
155 return FALSE;
156
157 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
158 if (nSize == 0) {
159 pFontMapper->LoadInstalledFonts();
160 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
161 }
162
163 for (int i = 0; i < nSize; ++i) {
164 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
165 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 }
167
168 return FALSE;
169}
170
171static int CharSet2CP(int charset) {
172 if (charset == 128)
173 return 932;
174 if (charset == 134)
175 return 936;
176 if (charset == 129)
177 return 949;
178 if (charset == 136)
179 return 950;
180 return 0;
181}
182CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
183 CPDF_Document* pDoc,
184 CFX_ByteString sFontFaceName,
185 uint8_t nCharset) {
186 if (pDoc) {
187 CFX_Font* pFXFont = new CFX_Font();
188 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
189 FALSE);
190 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
191 delete pFXFont;
192 return pFont;
193 }
194
195 return NULL;
196}
197
198int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
199 TimerCallback lpTimerFunc) {
200 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
201}
202void CFX_SystemHandler::KillTimer(int32_t nID) {
203 m_pEnv->FFI_KillTimer(nID);
204}
205
206FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
207 return m_pEnv->FFI_GetLocalTime();
208}
209
Tom Sepez50d12ad2015-11-24 09:50:51 -0800210CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 FPDF_FORMFILLINFO* pFFinfo)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800212 : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pUnderlyingDoc(pDoc) {
Oliver Changde6088d2015-10-30 16:30:28 -0700213 m_pSysHandler.reset(new CFX_SystemHandler(this));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214}
215
216CPDFDoc_Environment::~CPDFDoc_Environment() {
Tom Sepez51da0932015-11-25 16:05:49 -0800217#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700219 if (pProvider->m_pEnvList.GetSize() == 0)
220 pProvider->SetJavaScriptInitialized(FALSE);
Tom Sepez51da0932015-11-25 16:05:49 -0800221#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222}
223
224int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
225 const FX_WCHAR* Title,
226 FX_UINT Type,
227 FX_UINT Icon) {
228 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
229 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
230 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
231 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
232 FPDF_WIDESTRING pTitle =
233 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
234 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
235 pTitle, Type, Icon);
236 bsMsg.ReleaseBuffer();
237 bsTitle.ReleaseBuffer();
238 return ret;
239 }
240 return -1;
241}
242
243int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
244 const FX_WCHAR* Title,
245 const FX_WCHAR* Default,
246 const FX_WCHAR* cLabel,
247 FPDF_BOOL bPassword,
248 void* response,
249 int length) {
250 if (m_pInfo && m_pInfo->m_pJsPlatform &&
251 m_pInfo->m_pJsPlatform->app_response) {
252 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
253 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
254 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
255 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
256 FPDF_WIDESTRING pQuestion =
257 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
258 FPDF_WIDESTRING pTitle =
259 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
260 FPDF_WIDESTRING pDefault =
261 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
262 FPDF_WIDESTRING pLabel =
263 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
264 int ret = m_pInfo->m_pJsPlatform->app_response(
265 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
266 response, length);
267 bsQuestion.ReleaseBuffer();
268 bsTitle.ReleaseBuffer();
269 bsDefault.ReleaseBuffer();
270 bsLabel.ReleaseBuffer();
271 return ret;
272 }
273 return -1;
274}
275
276CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
277 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
278 !m_pInfo->m_pJsPlatform->Field_browse) {
279 return L"";
280 }
281
282 const int nRequiredLen =
283 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
284 if (nRequiredLen <= 0)
285 return L"";
286
287 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
288 memset(pBuff.get(), 0, nRequiredLen);
289 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
290 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
291 if (nActualLen <= 0 || nActualLen > nRequiredLen)
292 return L"";
293
294 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
295 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
296 return wsRet;
297}
298
299CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
300 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
301 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
302 return L"";
303 }
304
305 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
306 m_pInfo->m_pJsPlatform, nullptr, 0);
307 if (nRequiredLen <= 0)
308 return L"";
309
310 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
311 memset(pBuff.get(), 0, nRequiredLen);
312 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
313 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
314 if (nActualLen <= 0 || nActualLen > nRequiredLen)
315 return L"";
316
317 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
318 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
319 return wsRet;
320}
321
322void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
323 int length,
324 const FX_WCHAR* URL) {
325 if (m_pInfo && m_pInfo->m_pJsPlatform &&
326 m_pInfo->m_pJsPlatform->Doc_submitForm) {
327 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
328 FPDF_WIDESTRING pDestination =
329 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
330 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
331 length, pDestination);
332 bsDestination.ReleaseBuffer();
333 }
334}
335
336void CPDFDoc_Environment::JS_docmailForm(void* mailData,
337 int length,
338 FPDF_BOOL bUI,
339 const FX_WCHAR* To,
340 const FX_WCHAR* Subject,
341 const FX_WCHAR* CC,
342 const FX_WCHAR* BCC,
343 const FX_WCHAR* Msg) {
344 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
345 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
346 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
347 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
348 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
349 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
350 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
351 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
352 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
353 FPDF_WIDESTRING pSubject =
354 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
355 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
356 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
357 bUI, pTo, pSubject, pCC, pBcc, pMsg);
358 bsTo.ReleaseBuffer();
359 bsCC.ReleaseBuffer();
360 bsBcc.ReleaseBuffer();
361 bsSubject.ReleaseBuffer();
362 bsMsg.ReleaseBuffer();
363 }
364}
365
Tom Sepezba038bc2015-10-08 12:03:00 -0700366IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700368 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700370 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700371 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372}
373
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
375 if (!m_pAnnotHandlerMgr)
Oliver Chang24752492015-10-30 16:08:20 -0700376 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this));
377 return m_pAnnotHandlerMgr.get();
Bo Xufdc00a72014-10-28 23:03:33 -0700378}
379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
381 if (!m_pActionHandler)
Oliver Chang24752492015-10-30 16:08:20 -0700382 m_pActionHandler.reset(new CPDFSDK_ActionHandler());
383 return m_pActionHandler.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384}
385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
387 if (!m_pIFormFiller)
Oliver Chang24752492015-10-30 16:08:20 -0700388 m_pIFormFiller.reset(new CFFL_IFormFiller(this));
389 return m_pIFormFiller.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390}
391
Tom Sepez50d12ad2015-11-24 09:50:51 -0800392CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 CPDFDoc_Environment* pEnv)
394 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 m_pFocusAnnot(nullptr),
396 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700397 m_bChangeMask(FALSE),
Tom Sepez50d12ad2015-11-24 09:50:51 -0800398 m_bBeingDestroyed(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700399
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700401 m_bBeingDestroyed = TRUE;
402
403 for (auto& it : m_pageMap)
404 it.second->KillFocusAnnotIfNeeded();
405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 for (auto& it : m_pageMap)
407 delete it.second;
408 m_pageMap.clear();
Bo Xufdc00a72014-10-28 23:03:33 -0700409}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700410
Tom Sepez50d12ad2015-11-24 09:50:51 -0800411CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
412 UnderlyingPageType* pUnderlyingPage,
413 FX_BOOL ReNew) {
414 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700415 if (it != m_pageMap.end())
416 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 if (!ReNew)
419 return nullptr;
420
Tom Sepez50d12ad2015-11-24 09:50:51 -0800421 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
422 m_pageMap[pUnderlyingPage] = pPageView;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 // Delay to load all the annotations, to avoid endless loop.
424 pPageView->LoadFXAnnots();
425 return pPageView;
426}
427
428CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800429 UnderlyingPageType* pPage =
430 UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 return pPage ? GetPageView(pPage, TRUE) : nullptr;
432}
433
434CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800435 UnderlyingPageType* pTempPage =
436 UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700437 if (!pTempPage)
438 return nullptr;
439
440 auto it = m_pageMap.find(pTempPage);
441 return it->second;
442}
443
444void CPDFSDK_Document::ProcJavascriptFun() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800445 CPDF_Document* pPDFDoc = GetPDFDocument();
446 CPDF_DocJSActions docJS(pPDFDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 int iCount = docJS.CountJSActions();
448 if (iCount < 1)
449 return;
450 for (int i = 0; i < iCount; i++) {
451 CFX_ByteString csJSName;
452 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
453 if (m_pEnv->GetActionHander())
454 m_pEnv->GetActionHander()->DoAction_JavaScript(
455 jsAction, CFX_WideString::FromLocal(csJSName), this);
456 }
457}
458
459FX_BOOL CPDFSDK_Document::ProcOpenAction() {
460 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700461 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462
Tom Sepez50d12ad2015-11-24 09:50:51 -0800463 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 if (!pRoot)
465 return FALSE;
466
467 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
468 if (!pOpenAction)
469 pOpenAction = pRoot->GetArray("OpenAction");
470
471 if (!pOpenAction)
472 return FALSE;
473
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400474 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 return TRUE;
476
Dan Sinclairf1251c12015-10-20 16:24:45 -0400477 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 CPDF_Action action(pDict);
479 if (m_pEnv->GetActionHander())
480 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
481 return TRUE;
482 }
483 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484}
485
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
487 if (!m_pOccontent)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800488 m_pOccontent.reset(new CPDF_OCContext(GetPDFDocument()));
Oliver Chang24752492015-10-30 16:08:20 -0700489 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490}
Bo Xufdc00a72014-10-28 23:03:33 -0700491
Tom Sepez50d12ad2015-11-24 09:50:51 -0800492void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) {
493 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494 if (it == m_pageMap.end())
495 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700496
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 CPDFSDK_PageView* pPageView = it->second;
498 if (pPageView->IsLocked())
499 return;
500
Oliver Chang972b78d2015-10-30 12:59:29 -0700501 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 delete pPageView;
503 m_pageMap.erase(it);
504}
505
Tom Sepez50d12ad2015-11-24 09:50:51 -0800506UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
507 return UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508}
509
510CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
511 if (!m_pInterForm)
Oliver Chang24752492015-10-30 16:08:20 -0700512 m_pInterForm.reset(new CPDFSDK_InterForm(this));
513 return m_pInterForm.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514}
515
516void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
517 CPDFSDK_Annot* pAnnot) {
518 for (const auto& it : m_pageMap) {
519 CPDFSDK_PageView* pPageView = it.second;
520 if (pPageView != pSender) {
521 pPageView->UpdateView(pAnnot);
522 }
523 }
524}
525
526CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
527 return m_pFocusAnnot;
528}
529
530FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700531 if (m_bBeingDestroyed)
532 return FALSE;
533
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700534 if (m_pFocusAnnot == pAnnot)
535 return TRUE;
536
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537 if (m_pFocusAnnot) {
538 if (!KillFocusAnnot(nFlag))
539 return FALSE;
540 }
Tom Sepez540c4362015-11-24 13:33:57 -0800541
542 if (!pAnnot)
543 return FALSE;
544
Tom Sepez51da0932015-11-25 16:05:49 -0800545#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800546 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
Tom Sepez51da0932015-11-25 16:05:49 -0800547#endif
Tom Sepez540c4362015-11-24 13:33:57 -0800548 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
549 if (pPageView && pPageView->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700551 if (!m_pFocusAnnot) {
Tom Sepez51da0932015-11-25 16:05:49 -0800552#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700554 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700555
Tom Sepez51da0932015-11-25 16:05:49 -0800556#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700557 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
558 return FALSE;
559 if (!m_pFocusAnnot) {
560 m_pFocusAnnot = pAnnot;
561 return TRUE;
562 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700563 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 }
565 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700566}
567
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
569 if (m_pFocusAnnot) {
570 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700571 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
572 m_pFocusAnnot = nullptr;
Tom Sepez51da0932015-11-25 16:05:49 -0800573#ifdef PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700574
Lei Zhanga0601462015-10-30 14:54:37 -0700575 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
576 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700577
Tom Sepez51da0932015-11-25 16:05:49 -0800578#endif
Lei Zhanga0601462015-10-30 14:54:37 -0700579 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
580 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
581 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
582 int nFieldType = pWidget->GetFieldType();
583 if (FIELDTYPE_TEXTFIELD == nFieldType ||
584 FIELDTYPE_COMBOBOX == nFieldType) {
585 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587 }
Lei Zhanga0601462015-10-30 14:54:37 -0700588
589 if (!m_pFocusAnnot)
590 return TRUE;
591 } else {
592 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700593 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 }
595 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700596}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700597
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598void CPDFSDK_Document::OnCloseDocument() {
599 KillFocusAnnot();
600}
601
602FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
Tom Sepez540c4362015-11-24 13:33:57 -0800603 return GetPDFDocument()->GetUserPermissions() & nFlag;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700604}
605
Tom Sepezba038bc2015-10-08 12:03:00 -0700606IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700607 ASSERT(m_pEnv != NULL);
608 return m_pEnv->GetJSRuntime();
609}
610
611CFX_WideString CPDFSDK_Document::GetPath() {
612 ASSERT(m_pEnv != NULL);
613 return m_pEnv->JS_docGetFilePath();
614}
615
616CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
Tom Sepez540c4362015-11-24 13:33:57 -0800617 UnderlyingPageType* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700618 : m_page(page),
619 m_pSDKDoc(pSDKDoc),
620 m_CaptureWidget(nullptr),
621 m_bEnterWidget(FALSE),
622 m_bExitWidget(FALSE),
623 m_bOnWidget(FALSE),
624 m_bValid(FALSE),
Tom Sepez51da0932015-11-25 16:05:49 -0800625#ifndef PDF_ENABLE_XFA
626 m_bLocked(FALSE),
627 m_bTakeOverPage(FALSE) {
628#else
Lei Zhangbf60b292015-10-26 12:14:35 -0700629 m_bLocked(FALSE) {
Tom Sepez51da0932015-11-25 16:05:49 -0800630#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
632 if (pInterForm) {
633 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
Tom Sepez51da0932015-11-25 16:05:49 -0800634#ifndef PDF_ENABLE_XFA
635 pPDFInterForm->FixPageFields(page);
636#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700637 if (page->GetPDFPage())
638 pPDFInterForm->FixPageFields(page->GetPDFPage());
Tom Sepez51da0932015-11-25 16:05:49 -0800639#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640 }
Tom Sepez51da0932015-11-25 16:05:49 -0800641#ifndef PDF_ENABLE_XFA
642 m_page->SetPrivateData((void*)m_page, (void*)this, nullptr);
643#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644}
645
646CPDFSDK_PageView::~CPDFSDK_PageView() {
647 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700648 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700649 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700651 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652
Lei Zhangbf60b292015-10-26 12:14:35 -0700653 m_pAnnotList.reset();
Tom Sepez51da0932015-11-25 16:05:49 -0800654#ifndef PDF_ENABLE_XFA
655
656 m_page->RemovePrivateData((void*)m_page);
657 if (m_bTakeOverPage) {
658 delete m_page;
659 }
660#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661}
662
663void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
664 CPDF_Matrix* pUser2Device,
Tom Sepez51da0932015-11-25 16:05:49 -0800665#ifndef PDF_ENABLE_XFA
666 CPDF_RenderOptions* pOptions) {
667#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 CPDF_RenderOptions* pOptions,
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800669 const FX_RECT& pClip) {
Tom Sepez51da0932015-11-25 16:05:49 -0800670#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700671 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez51da0932015-11-25 16:05:49 -0800673#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 CPDFXFA_Page* pPage = GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 if (pPage == NULL)
676 return;
677
Tom Sepeza8a39e22015-10-12 15:47:07 -0700678#ifdef PDF_ENABLE_XFA
Tom Sepezd3116dc2015-11-24 15:58:06 -0800679 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680 CFX_Graphics gs;
681 gs.Create(pDevice);
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800682 CFX_RectF rectClip;
683 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
684 static_cast<FX_FLOAT>(pClip.top),
685 static_cast<FX_FLOAT>(pClip.Width()),
686 static_cast<FX_FLOAT>(pClip.Height()));
687 gs.SetClipRect(rectClip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
689 if (!pRenderContext)
690 return;
691 CXFA_RenderOptions renderOptions;
692 renderOptions.m_bHighlight = TRUE;
693 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
694 renderOptions);
695 pRenderContext->DoRender();
696 pRenderContext->StopRender();
697 pRenderContext->Release();
698 return;
699 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700700#endif // PDF_ENABLE_XFA
701
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 // for pdf/static xfa.
Tom Sepez51da0932015-11-25 16:05:49 -0800703#endif
Lei Zhangbf60b292015-10-26 12:14:35 -0700704 CPDFSDK_AnnotIterator annotIterator(this, true);
705 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 }
709}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700710
Lei Zhang1b700c32015-10-30 23:55:35 -0700711const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
712 FX_FLOAT pageY) {
713 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714 CFX_FloatRect annotRect;
715 pAnnot->GetRect(annotRect);
716 if (annotRect.Contains(pageX, pageY))
717 return pAnnot;
718 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700719 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700720}
721
Lei Zhang1b700c32015-10-30 23:55:35 -0700722const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
723 FX_FLOAT pageY) {
724 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 if (pAnnot->GetSubType() == "Widget") {
726 CFX_FloatRect annotRect;
727 pAnnot->GetRect(annotRect);
728 if (annotRect.Contains(pageX, pageY))
729 return pAnnot;
730 }
731 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700732 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700733}
734
735CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
736 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
738 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700739 CPDFSDK_AnnotIterator annotIterator(this, false);
740 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
742 if (rc.Contains(pageX, pageY))
743 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744 }
745
Lei Zhangbf60b292015-10-26 12:14:35 -0700746 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747}
748
749CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
750 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
752 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700753 CPDFSDK_AnnotIterator annotIterator(this, false);
754 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Tom Sepez51da0932015-11-25 16:05:49 -0800755#ifndef PDF_ENABLE_XFA
756 if (pSDKAnnot->GetType() == "Widget") {
757#else
Tom Sepez1b246282015-11-25 15:15:31 -0800758 bool bHitTest = pSDKAnnot->GetType() == "Widget";
759 bHitTest = bHitTest || pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME;
760 if (bHitTest) {
Tom Sepez51da0932015-11-25 16:05:49 -0800761#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700762 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
763 CPDF_Point point(pageX, pageY);
764 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
765 return pSDKAnnot;
766 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700767 }
768
Lei Zhangbf60b292015-10-26 12:14:35 -0700769 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770}
771
Oliver Chang972b78d2015-10-30 12:59:29 -0700772void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
773 // if there is a focused annot on the page, we should kill the focus first.
774 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
775 auto it =
776 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
777 if (it != m_fxAnnotArray.end())
778 KillFocusAnnot();
779 }
780}
781
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
783 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
784 if (pAnnotDic)
785 return pAnnotDic->KeyExist("AS");
786 return FALSE;
787}
788
789CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
790 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
791 ASSERT(pEnv);
792 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700793 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700795 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796
Lei Zhangbf60b292015-10-26 12:14:35 -0700797 m_fxAnnotArray.push_back(pSDKAnnot);
798 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700799 return pSDKAnnot;
800}
801
Tom Sepez51da0932015-11-25 16:05:49 -0800802#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
804 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700805 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806
807 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
808 if (pSDKAnnot)
809 return pSDKAnnot;
810
811 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700813 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700814 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700815 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700816
Lei Zhangbf60b292015-10-26 12:14:35 -0700817 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 return pSDKAnnot;
819}
820
Tom Sepez51da0932015-11-25 16:05:49 -0800821#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
823 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
824}
825
826CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
827 CPDF_Dictionary* pDict) {
828 return NULL;
829}
830
831FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepez51da0932015-11-25 16:05:49 -0800832#ifndef PDF_ENABLE_XFA
833 return FALSE;
834#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700835 if (!pAnnot)
836 return FALSE;
837 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
838 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
Tom Sepezd3116dc2015-11-24 15:58:06 -0800839 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNAMIC_XFA))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840 return FALSE;
841
Lei Zhangbf60b292015-10-26 12:14:35 -0700842 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
843 if (it != m_fxAnnotArray.end())
844 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700845 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700846 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700847
848 return TRUE;
Tom Sepez51da0932015-11-25 16:05:49 -0800849#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850}
851
852CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
853 if (m_page) {
Tom Sepez51da0932015-11-25 16:05:49 -0800854#ifndef PDF_ENABLE_XFA
855 return m_page->m_pDocument;
856#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700857 return m_page->GetDocument()->GetPDFDoc();
858 }
859 return NULL;
860}
861
862CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
863 if (m_page) {
864 return m_page->GetPDFPage();
Tom Sepez51da0932015-11-25 16:05:49 -0800865#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867 return NULL;
868}
869
Lei Zhang50218532015-10-30 14:03:43 -0700870size_t CPDFSDK_PageView::CountAnnots() const {
871 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700872}
873
Lei Zhangbf60b292015-10-26 12:14:35 -0700874CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
875 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700876}
877
878CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700879 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
880 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700881 return pAnnot;
882 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700883 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884}
Tom Sepez51da0932015-11-25 16:05:49 -0800885#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700887 if (!hWidget)
888 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889
Lei Zhangbf60b292015-10-26 12:14:35 -0700890 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891 if (pAnnot->GetXFAWidget() == hWidget)
892 return pAnnot;
893 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700894 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895}
Tom Sepez51da0932015-11-25 16:05:49 -0800896#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897
898FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
899 FX_UINT nFlag) {
900 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
901 ASSERT(pEnv);
902 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
903 if (!pFXAnnot) {
904 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700905 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906 }
Lei Zhanga0601462015-10-30 14:54:37 -0700907
908 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
909 FX_BOOL bRet =
910 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
911 if (bRet)
912 SetFocusAnnot(pFXAnnot);
913 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700914}
915
Tom Sepez51da0932015-11-25 16:05:49 -0800916#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
918 FX_UINT nFlag) {
919 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
920 ASSERT(pEnv);
921 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
922 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700923
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700924 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
925
926 if (pFXAnnot == NULL)
927 return FALSE;
928
929 FX_BOOL bRet =
930 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
931 if (bRet) {
932 SetFocusAnnot(pFXAnnot);
933 }
934 return TRUE;
935}
936
937FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
938 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
939 ASSERT(pEnv);
940 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700941
942 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
943
944 if (pFXAnnot == NULL)
945 return FALSE;
946
947 FX_BOOL bRet =
948 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
949 if (bRet) {
950 SetFocusAnnot(pFXAnnot);
951 }
952 return TRUE;
953}
954
Tom Sepez51da0932015-11-25 16:05:49 -0800955#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700956FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
957 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
958 ASSERT(pEnv);
959 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700960 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
961 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
962 FX_BOOL bRet = FALSE;
963 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
964 // Last focus Annot gets a chance to handle the event.
965 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
966 }
Lei Zhanga0601462015-10-30 14:54:37 -0700967 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 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();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700975 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
976 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
977 m_bExitWidget = TRUE;
978 m_bEnterWidget = FALSE;
979 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
980 }
981 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
982 m_bOnWidget = TRUE;
983 if (!m_bEnterWidget) {
984 m_bEnterWidget = TRUE;
985 m_bExitWidget = FALSE;
986 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
987 }
988 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
989 return TRUE;
990 }
991 if (m_bOnWidget) {
992 m_bOnWidget = FALSE;
993 m_bExitWidget = TRUE;
994 m_bEnterWidget = FALSE;
995 if (m_CaptureWidget) {
996 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
997 m_CaptureWidget = NULL;
998 }
999 }
1000 return FALSE;
1001}
1002
1003FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1004 double deltaY,
1005 const CPDF_Point& point,
1006 int nFlag) {
1007 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001008 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1009 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1011 (int)deltaY, point);
1012 }
1013 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001014}
1015
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1017 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001018 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001019 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001020 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1021 }
Bo Xufdc00a72014-10-28 23:03:33 -07001022
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001023 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001024}
1025
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1027 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001028 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1031 }
1032 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001033}
1034
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001037}
1038
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez51da0932015-11-25 16:05:49 -08001041#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Tom Sepez51da0932015-11-25 16:05:49 -08001043#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044
Tom Sepez51da0932015-11-25 16:05:49 -08001045#ifndef PDF_ENABLE_XFA
1046 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1047 // Disable the default AP construction.
1048 CPDF_InterForm::EnableUpdateAP(FALSE);
1049 m_pAnnotList.reset(new CPDF_AnnotList(m_page));
1050 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1051 const size_t nCount = m_pAnnotList->Count();
1052#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001053 SetLock(TRUE);
Tom Sepez51da0932015-11-25 16:05:49 -08001054#ifndef PDF_ENABLE_XFA
1055 for (size_t i = 0; i < nCount; ++i) {
1056 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1057 CPDF_Document* pDoc = GetPDFDocument();
1058#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 m_page->AddRef();
Tom Sepezd3116dc2015-11-24 15:58:06 -08001060 if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 IXFA_PageView* pageView = NULL;
1062 pageView = m_page->GetXFAPageView();
1063 ASSERT(pageView != NULL);
1064
1065 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1066 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1067 XFA_WIDGETFILTER_Viewable |
1068 XFA_WIDGETFILTER_AllType);
1069 if (!pWidgetHander) {
1070 m_page->Release();
1071 SetLock(FALSE);
1072 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001073 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074
Lei Zhangbf60b292015-10-26 12:14:35 -07001075 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001077 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001078 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079
Lei Zhangbf60b292015-10-26 12:14:35 -07001080 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001082 }
Tom Sepez51da0932015-11-25 16:05:49 -08001083#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084
Tom Sepez51da0932015-11-25 16:05:49 -08001085#ifndef PDF_ENABLE_XFA
1086 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1087#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 pWidgetHander->Release();
1089 } else {
1090 CPDF_Page* pPage = m_page->GetPDFPage();
1091 ASSERT(pPage != NULL);
Tom Sepez51da0932015-11-25 16:05:49 -08001092#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093
Tom Sepez51da0932015-11-25 16:05:49 -08001094#ifndef PDF_ENABLE_XFA
1095 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1096 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1097 if (!pAnnot)
1098 continue;
1099 m_fxAnnotArray.push_back(pAnnot);
1100#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1102 // Disable the default AP construction.
1103 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001104 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1106
Lei Zhang1b700c32015-10-30 23:55:35 -07001107 const size_t nCount = m_pAnnotList->Count();
1108 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001109 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001110 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111
1112 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1113 if (!pAnnot)
1114 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001115 m_fxAnnotArray.push_back(pAnnot);
Tom Sepez51da0932015-11-25 16:05:49 -08001116#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117
Tom Sepez51da0932015-11-25 16:05:49 -08001118#ifndef PDF_ENABLE_XFA
1119 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1120#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001121 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1122 }
Tom Sepez51da0932015-11-25 16:05:49 -08001123#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001124 }
Tom Sepez51da0932015-11-25 16:05:49 -08001125#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001126 m_page->Release();
Tom Sepez51da0932015-11-25 16:05:49 -08001127#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001128 SetLock(FALSE);
1129}
1130
1131void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1132 for (int i = 0; i < rects.GetSize(); i++) {
1133 CPDF_Rect rc = rects.GetAt(i);
1134 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1135 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1136 }
1137}
1138
1139void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1140 CPDF_Rect rcWindow = pAnnot->GetRect();
1141 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1142 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1143 rcWindow.bottom);
1144}
1145
1146int CPDFSDK_PageView::GetPageIndex() {
1147 if (m_page) {
Tom Sepez51da0932015-11-25 16:05:49 -08001148#ifndef PDF_ENABLE_XFA
1149 CPDF_Dictionary* pDic = m_page->m_pFormDict;
1150#else
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
Tom Sepez51da0932015-11-25 16:05:49 -08001152#endif
Tom Sepez50d12ad2015-11-24 09:50:51 -08001153 CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001154 if (pDoc && pDic) {
Tom Sepez50d12ad2015-11-24 09:50:51 -08001155 return pDoc->GetPageIndex(pDic->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 }
1157 }
1158 return -1;
1159}
1160
Lei Zhang1b700c32015-10-30 23:55:35 -07001161bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001162 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001163 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001164
Lei Zhang1b700c32015-10-30 23:55:35 -07001165 const auto& annots = m_pAnnotList->All();
1166 return std::find(annots.begin(), annots.end(), p) != annots.end();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001167}
1168
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001169CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1170 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1171 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001172 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001173
Lei Zhangbf60b292015-10-26 12:14:35 -07001174 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175 if (pAnnot == pFocusAnnot)
1176 return pAnnot;
1177 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001178 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001179}