blob: 76e0d2de2087c8c2cf66c42ae3a20cc6ed1d540b [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>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -08008#include <memory>
Lei Zhangbf60b292015-10-26 12:14:35 -07009
Lei Zhangbde53d22015-11-12 22:21:30 -080010#include "fpdfsdk/include/fsdk_mgr.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080011
Lei Zhangbde53d22015-11-12 22:21:30 -080012#include "fpdfsdk/include/formfiller/FFL_FormFiller.h"
13#include "fpdfsdk/include/fsdk_define.h"
14#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080015#include "public/fpdf_ext.h"
Lei Zhangcd2bb302015-12-22 13:49:44 -080016#include "third_party/base/stl_util.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070017
Tom Sepez51da0932015-11-25 16:05:49 -080018#ifdef PDF_ENABLE_XFA
Lei Zhang875b9c92016-01-08 13:51:10 -080019#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
20#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
21#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
22#include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080023#endif // PDF_ENABLE_XFA
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
25#if _FX_OS_ == _FX_ANDROID_
26#include "time.h"
27#else
28#include <ctime>
29#endif
30
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031class CFX_SystemHandler : public IFX_SystemHandler {
32 public:
Lei Zhangbf60b292015-10-26 12:14:35 -070033 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
34 : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070035 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070038 // IFX_SystemHandler
39 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
40 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
41 FX_BOOL IsSelectionImplemented() override;
42 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
43 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 return FALSE;
45 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070046 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
47 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
48 void SetCursor(int32_t nCursorType) override;
49 FX_HMENU CreatePopupMenu() override { return NULL; }
50 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
51 int32_t nIDNewItem,
52 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 return FALSE;
54 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070055 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
56 int32_t nIDItem,
57 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 return FALSE;
59 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070060 int32_t TrackPopupMenu(FX_HMENU hMenu,
61 int32_t x,
62 int32_t y,
63 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 return -1;
65 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070066 void DestroyMenu(FX_HMENU hMenu) override {}
67 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
68 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
69 CFX_ByteString sFontFaceName) override;
70 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
71 CFX_ByteString sFontFaceName,
72 uint8_t nCharset) override;
73 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
74 void KillTimer(int32_t nID) override;
75 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
77 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
80 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070081 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 return m_pEnv->FFI_IsALTKeyDown(nFlag);
83 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070084 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
86 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070087 FX_SYSTEMTIME GetLocalTime() override;
88 int32_t GetCharSet() override { return m_nCharSet; }
89 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090
91 private:
92 CPDFDoc_Environment* m_pEnv;
93 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094};
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
97 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
Tom Sepez50d12ad2015-11-24 09:50:51 -0800102 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
103 UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (!pPage || !pPageView)
105 return;
Tom Sepez60d909e2015-12-10 15:34:55 -0800106 CFX_Matrix page2device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 pPageView->GetCurrentMatrix(page2device);
Tom Sepez60d909e2015-12-10 15:34:55 -0800108 CFX_Matrix device2page;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 device2page.SetReverse(page2device);
110 FX_FLOAT left, top, right, bottom;
111 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
112 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
113 bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 CPDF_Rect rcPDF(left, bottom, right, top);
115 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
118 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
121 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
122 if (pFFL) {
123 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
124 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
125 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
126 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
127 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800128 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 ASSERT(pPage);
130 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
131 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132}
133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
135 if (m_pEnv) {
136 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
137 if (pInfo && pInfo->FFI_OutputSelectedRect)
138 return TRUE;
139 }
140 return FALSE;
141}
142
143CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
144 return "";
145}
146
147FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
148 int32_t nCharset,
149 CFX_ByteString sFontFaceName) {
150 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
Lei Zhang122ee212015-10-21 17:07:24 -0700151 if (!pFontMgr)
152 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153
Lei Zhang122ee212015-10-21 17:07:24 -0700154 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
155 if (!pFontMapper)
156 return FALSE;
157
158 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
159 if (nSize == 0) {
160 pFontMapper->LoadInstalledFonts();
161 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
162 }
163
164 for (int i = 0; i < nSize; ++i) {
165 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
166 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 }
168
169 return FALSE;
170}
171
172static int CharSet2CP(int charset) {
173 if (charset == 128)
174 return 932;
175 if (charset == 134)
176 return 936;
177 if (charset == 129)
178 return 949;
179 if (charset == 136)
180 return 950;
181 return 0;
182}
183CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
184 CPDF_Document* pDoc,
185 CFX_ByteString sFontFaceName,
186 uint8_t nCharset) {
187 if (pDoc) {
188 CFX_Font* pFXFont = new CFX_Font();
189 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
190 FALSE);
191 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
192 delete pFXFont;
193 return pFont;
194 }
195
196 return NULL;
197}
198
199int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
200 TimerCallback lpTimerFunc) {
201 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
202}
203void CFX_SystemHandler::KillTimer(int32_t nID) {
204 m_pEnv->FFI_KillTimer(nID);
205}
206
207FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
208 return m_pEnv->FFI_GetLocalTime();
209}
210
Tom Sepez50d12ad2015-11-24 09:50:51 -0800211CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 FPDF_FORMFILLINFO* pFFinfo)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800213 : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pUnderlyingDoc(pDoc) {
Oliver Changde6088d2015-10-30 16:30:28 -0700214 m_pSysHandler.reset(new CFX_SystemHandler(this));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215}
216
217CPDFDoc_Environment::~CPDFDoc_Environment() {
Tom Sepez51da0932015-11-25 16:05:49 -0800218#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700220 if (pProvider->m_pEnvList.GetSize() == 0)
221 pProvider->SetJavaScriptInitialized(FALSE);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800222#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223}
224
225int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
226 const FX_WCHAR* Title,
227 FX_UINT Type,
228 FX_UINT Icon) {
229 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
230 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
231 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
232 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
233 FPDF_WIDESTRING pTitle =
234 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
235 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
236 pTitle, Type, Icon);
237 bsMsg.ReleaseBuffer();
238 bsTitle.ReleaseBuffer();
239 return ret;
240 }
241 return -1;
242}
243
244int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
245 const FX_WCHAR* Title,
246 const FX_WCHAR* Default,
247 const FX_WCHAR* cLabel,
248 FPDF_BOOL bPassword,
249 void* response,
250 int length) {
251 if (m_pInfo && m_pInfo->m_pJsPlatform &&
252 m_pInfo->m_pJsPlatform->app_response) {
253 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
254 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
255 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
256 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
257 FPDF_WIDESTRING pQuestion =
258 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
259 FPDF_WIDESTRING pTitle =
260 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
261 FPDF_WIDESTRING pDefault =
262 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
263 FPDF_WIDESTRING pLabel =
264 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
265 int ret = m_pInfo->m_pJsPlatform->app_response(
266 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
267 response, length);
268 bsQuestion.ReleaseBuffer();
269 bsTitle.ReleaseBuffer();
270 bsDefault.ReleaseBuffer();
271 bsLabel.ReleaseBuffer();
272 return ret;
273 }
274 return -1;
275}
276
277CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
278 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
279 !m_pInfo->m_pJsPlatform->Field_browse) {
280 return L"";
281 }
282
283 const int nRequiredLen =
284 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
285 if (nRequiredLen <= 0)
286 return L"";
287
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800288 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 memset(pBuff.get(), 0, nRequiredLen);
290 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
291 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
292 if (nActualLen <= 0 || nActualLen > nRequiredLen)
293 return L"";
294
295 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
296 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
297 return wsRet;
298}
299
300CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
301 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
302 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
303 return L"";
304 }
305
306 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
307 m_pInfo->m_pJsPlatform, nullptr, 0);
308 if (nRequiredLen <= 0)
309 return L"";
310
Lei Zhangaa8bf7e2015-12-24 19:13:32 -0800311 std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 memset(pBuff.get(), 0, nRequiredLen);
313 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
314 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
315 if (nActualLen <= 0 || nActualLen > nRequiredLen)
316 return L"";
317
318 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
319 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
320 return wsRet;
321}
322
323void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
324 int length,
325 const FX_WCHAR* URL) {
326 if (m_pInfo && m_pInfo->m_pJsPlatform &&
327 m_pInfo->m_pJsPlatform->Doc_submitForm) {
328 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
329 FPDF_WIDESTRING pDestination =
330 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
331 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
332 length, pDestination);
333 bsDestination.ReleaseBuffer();
334 }
335}
336
337void CPDFDoc_Environment::JS_docmailForm(void* mailData,
338 int length,
339 FPDF_BOOL bUI,
340 const FX_WCHAR* To,
341 const FX_WCHAR* Subject,
342 const FX_WCHAR* CC,
343 const FX_WCHAR* BCC,
344 const FX_WCHAR* Msg) {
345 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
346 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
347 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
348 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
349 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
350 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
351 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
352 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
353 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
354 FPDF_WIDESTRING pSubject =
355 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
356 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
357 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
358 bUI, pTo, pSubject, pCC, pBcc, pMsg);
359 bsTo.ReleaseBuffer();
360 bsCC.ReleaseBuffer();
361 bsBcc.ReleaseBuffer();
362 bsSubject.ReleaseBuffer();
363 bsMsg.ReleaseBuffer();
364 }
365}
366
Tom Sepezba038bc2015-10-08 12:03:00 -0700367IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700369 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700371 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700372 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700373}
374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
376 if (!m_pAnnotHandlerMgr)
Oliver Chang24752492015-10-30 16:08:20 -0700377 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this));
378 return m_pAnnotHandlerMgr.get();
Bo Xufdc00a72014-10-28 23:03:33 -0700379}
380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
382 if (!m_pActionHandler)
Oliver Chang24752492015-10-30 16:08:20 -0700383 m_pActionHandler.reset(new CPDFSDK_ActionHandler());
384 return m_pActionHandler.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385}
386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
388 if (!m_pIFormFiller)
Oliver Chang24752492015-10-30 16:08:20 -0700389 m_pIFormFiller.reset(new CFFL_IFormFiller(this));
390 return m_pIFormFiller.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700391}
392
Tom Sepezfe351db2016-01-29 16:26:27 -0800393// static
394CPDFSDK_Document* CPDFSDK_Document::FromFPDFFormHandle(
395 FPDF_FORMHANDLE hHandle) {
396 CPDFDoc_Environment* pEnv = static_cast<CPDFDoc_Environment*>(hHandle);
397 return pEnv ? pEnv->GetSDKDocument() : nullptr;
398}
399
Tom Sepez50d12ad2015-11-24 09:50:51 -0800400CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401 CPDFDoc_Environment* pEnv)
402 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 m_pFocusAnnot(nullptr),
404 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700405 m_bChangeMask(FALSE),
Tom Sepez50d12ad2015-11-24 09:50:51 -0800406 m_bBeingDestroyed(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700407
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700408CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700409 m_bBeingDestroyed = TRUE;
410
411 for (auto& it : m_pageMap)
412 it.second->KillFocusAnnotIfNeeded();
413
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 for (auto& it : m_pageMap)
415 delete it.second;
416 m_pageMap.clear();
Bo Xufdc00a72014-10-28 23:03:33 -0700417}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418
Tom Sepez50d12ad2015-11-24 09:50:51 -0800419CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
420 UnderlyingPageType* pUnderlyingPage,
421 FX_BOOL ReNew) {
422 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 if (it != m_pageMap.end())
424 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700425
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 if (!ReNew)
427 return nullptr;
428
Tom Sepez50d12ad2015-11-24 09:50:51 -0800429 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
430 m_pageMap[pUnderlyingPage] = pPageView;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700431 // Delay to load all the annotations, to avoid endless loop.
432 pPageView->LoadFXAnnots();
433 return pPageView;
434}
435
436CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800437 UnderlyingPageType* pPage =
438 UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 return pPage ? GetPageView(pPage, TRUE) : nullptr;
440}
441
442CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800443 UnderlyingPageType* pTempPage =
444 UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700445 if (!pTempPage)
446 return nullptr;
447
448 auto it = m_pageMap.find(pTempPage);
449 return it->second;
450}
451
452void CPDFSDK_Document::ProcJavascriptFun() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800453 CPDF_Document* pPDFDoc = GetPDFDocument();
454 CPDF_DocJSActions docJS(pPDFDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 int iCount = docJS.CountJSActions();
456 if (iCount < 1)
457 return;
458 for (int i = 0; i < iCount; i++) {
459 CFX_ByteString csJSName;
460 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
461 if (m_pEnv->GetActionHander())
462 m_pEnv->GetActionHander()->DoAction_JavaScript(
463 jsAction, CFX_WideString::FromLocal(csJSName), this);
464 }
465}
466
467FX_BOOL CPDFSDK_Document::ProcOpenAction() {
468 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700469 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470
Tom Sepez50d12ad2015-11-24 09:50:51 -0800471 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 if (!pRoot)
473 return FALSE;
474
Wei Li9b761132016-01-29 15:44:20 -0800475 CPDF_Object* pOpenAction = pRoot->GetDictBy("OpenAction");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 if (!pOpenAction)
Wei Li9b761132016-01-29 15:44:20 -0800477 pOpenAction = pRoot->GetArrayBy("OpenAction");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478
479 if (!pOpenAction)
480 return FALSE;
481
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400482 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700483 return TRUE;
484
Dan Sinclairf1251c12015-10-20 16:24:45 -0400485 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486 CPDF_Action action(pDict);
487 if (m_pEnv->GetActionHander())
488 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
489 return TRUE;
490 }
491 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700492}
493
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700494CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
495 if (!m_pOccontent)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800496 m_pOccontent.reset(new CPDF_OCContext(GetPDFDocument()));
Oliver Chang24752492015-10-30 16:08:20 -0700497 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498}
Bo Xufdc00a72014-10-28 23:03:33 -0700499
Tom Sepez50d12ad2015-11-24 09:50:51 -0800500void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) {
501 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 if (it == m_pageMap.end())
503 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700504
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700505 CPDFSDK_PageView* pPageView = it->second;
506 if (pPageView->IsLocked())
507 return;
508
Oliver Chang972b78d2015-10-30 12:59:29 -0700509 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 delete pPageView;
511 m_pageMap.erase(it);
512}
513
Tom Sepez50d12ad2015-11-24 09:50:51 -0800514UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
515 return UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516}
517
518CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
519 if (!m_pInterForm)
Oliver Chang24752492015-10-30 16:08:20 -0700520 m_pInterForm.reset(new CPDFSDK_InterForm(this));
521 return m_pInterForm.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700522}
523
524void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
525 CPDFSDK_Annot* pAnnot) {
526 for (const auto& it : m_pageMap) {
527 CPDFSDK_PageView* pPageView = it.second;
528 if (pPageView != pSender) {
529 pPageView->UpdateView(pAnnot);
530 }
531 }
532}
533
534CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
535 return m_pFocusAnnot;
536}
537
538FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700539 if (m_bBeingDestroyed)
540 return FALSE;
541
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542 if (m_pFocusAnnot == pAnnot)
543 return TRUE;
544
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700545 if (m_pFocusAnnot) {
546 if (!KillFocusAnnot(nFlag))
547 return FALSE;
548 }
Tom Sepez540c4362015-11-24 13:33:57 -0800549
550 if (!pAnnot)
551 return FALSE;
552
Tom Sepez51da0932015-11-25 16:05:49 -0800553#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800554 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800555#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800556 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
557 if (pPageView && pPageView->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700558 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700559 if (!m_pFocusAnnot) {
Tom Sepez51da0932015-11-25 16:05:49 -0800560#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700562 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800563#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
565 return FALSE;
566 if (!m_pFocusAnnot) {
567 m_pFocusAnnot = pAnnot;
568 return TRUE;
569 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700570 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700571 }
572 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700573}
574
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
576 if (m_pFocusAnnot) {
577 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700578 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
579 m_pFocusAnnot = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700580
Tom Sepez40e9ff32015-11-30 12:39:54 -0800581#ifdef PDF_ENABLE_XFA
Lei Zhanga0601462015-10-30 14:54:37 -0700582 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
583 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800584#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700585
Lei Zhanga0601462015-10-30 14:54:37 -0700586 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
Lei Zhangd983b092015-12-14 16:58:33 -0800587 if (pFocusAnnot->GetType() == "Widget") {
Lei Zhanga0601462015-10-30 14:54:37 -0700588 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
589 int nFieldType = pWidget->GetFieldType();
590 if (FIELDTYPE_TEXTFIELD == nFieldType ||
591 FIELDTYPE_COMBOBOX == nFieldType) {
592 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700593 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 }
Lei Zhanga0601462015-10-30 14:54:37 -0700595
596 if (!m_pFocusAnnot)
597 return TRUE;
598 } else {
599 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700600 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601 }
602 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700603}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700604
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700605void CPDFSDK_Document::OnCloseDocument() {
606 KillFocusAnnot();
607}
608
609FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
Tom Sepez540c4362015-11-24 13:33:57 -0800610 return GetPDFDocument()->GetUserPermissions() & nFlag;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611}
612
Tom Sepezba038bc2015-10-08 12:03:00 -0700613IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 return m_pEnv->GetJSRuntime();
615}
616
617CFX_WideString CPDFSDK_Document::GetPath() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 return m_pEnv->JS_docGetFilePath();
619}
620
621CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
Tom Sepez540c4362015-11-24 13:33:57 -0800622 UnderlyingPageType* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700623 : m_page(page),
624 m_pSDKDoc(pSDKDoc),
625 m_CaptureWidget(nullptr),
Tom Sepezc46d0002015-11-30 15:46:36 -0800626#ifndef PDF_ENABLE_XFA
627 m_bTakeOverPage(FALSE),
628#endif // PDF_ENABLE_XFA
Lei Zhangbf60b292015-10-26 12:14:35 -0700629 m_bEnterWidget(FALSE),
630 m_bExitWidget(FALSE),
631 m_bOnWidget(FALSE),
632 m_bValid(FALSE),
Tom Sepez40e9ff32015-11-30 12:39:54 -0800633 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
635 if (pInterForm) {
636 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800637#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 if (page->GetPDFPage())
639 pPDFInterForm->FixPageFields(page->GetPDFPage());
Tom Sepez40e9ff32015-11-30 12:39:54 -0800640#else // PDF_ENABLE_XFA
641 pPDFInterForm->FixPageFields(page);
642#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643 }
Tom Sepez51da0932015-11-25 16:05:49 -0800644#ifndef PDF_ENABLE_XFA
645 m_page->SetPrivateData((void*)m_page, (void*)this, nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800646#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647}
648
649CPDFSDK_PageView::~CPDFSDK_PageView() {
650 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700651 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700652 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700653 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654
Tom Sepez40e9ff32015-11-30 12:39:54 -0800655 m_fxAnnotArray.clear();
Lei Zhangbf60b292015-10-26 12:14:35 -0700656 m_pAnnotList.reset();
Tom Sepez51da0932015-11-25 16:05:49 -0800657#ifndef PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800658 m_page->RemovePrivateData((void*)m_page);
659 if (m_bTakeOverPage) {
660 delete m_page;
661 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800662#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700663}
664
665void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800666 CFX_Matrix* pUser2Device,
Tom Sepez40e9ff32015-11-30 12:39:54 -0800667#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 CPDF_RenderOptions* pOptions,
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800669 const FX_RECT& pClip) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800670#else
671 CPDF_RenderOptions* pOptions) {
672#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800675
Tom Sepez51da0932015-11-25 16:05:49 -0800676#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 CPDFXFA_Page* pPage = GetPDFXFAPage();
Jun Fang2592eaa2015-12-02 14:09:28 +0800678 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 return;
680
Tom Sepezd3116dc2015-11-24 15:58:06 -0800681 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700682 CFX_Graphics gs;
683 gs.Create(pDevice);
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800684 CFX_RectF rectClip;
685 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
686 static_cast<FX_FLOAT>(pClip.top),
687 static_cast<FX_FLOAT>(pClip.Width()),
688 static_cast<FX_FLOAT>(pClip.Height()));
689 gs.SetClipRect(rectClip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
691 if (!pRenderContext)
692 return;
693 CXFA_RenderOptions renderOptions;
694 renderOptions.m_bHighlight = TRUE;
Jun Fang2592eaa2015-12-02 14:09:28 +0800695 IXFA_PageView* xfaView = pPage->GetXFAPageView();
696 pRenderContext->StartRender(xfaView, &gs, *pUser2Device, renderOptions);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 pRenderContext->DoRender();
698 pRenderContext->StopRender();
699 pRenderContext->Release();
Jun Fang2592eaa2015-12-02 14:09:28 +0800700 IXFA_DocView* docView = xfaView->GetDocView();
701 if (!docView)
702 return;
703 CPDFSDK_Annot* annot = GetFocusAnnot();
704 if (!annot)
705 return;
706 // Render the focus widget
707 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
708 pUser2Device, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709 return;
710 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700711#endif // PDF_ENABLE_XFA
712
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700714 CPDFSDK_AnnotIterator annotIterator(this, true);
715 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700716 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 }
719}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700720
Lei Zhang1b700c32015-10-30 23:55:35 -0700721const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
722 FX_FLOAT pageY) {
723 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700724 CFX_FloatRect annotRect;
725 pAnnot->GetRect(annotRect);
726 if (annotRect.Contains(pageX, pageY))
727 return pAnnot;
728 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700729 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730}
731
Lei Zhang1b700c32015-10-30 23:55:35 -0700732const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
733 FX_FLOAT pageY) {
734 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 if (pAnnot->GetSubType() == "Widget") {
736 CFX_FloatRect annotRect;
737 pAnnot->GetRect(annotRect);
738 if (annotRect.Contains(pageX, pageY))
739 return pAnnot;
740 }
741 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700742 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743}
744
745CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
746 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
748 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700749 CPDFSDK_AnnotIterator annotIterator(this, false);
750 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
752 if (rc.Contains(pageX, pageY))
753 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 }
755
Lei Zhangbf60b292015-10-26 12:14:35 -0700756 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757}
758
759CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
760 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700761 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
762 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700763 CPDFSDK_AnnotIterator annotIterator(this, false);
764 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Tom Sepez1b246282015-11-25 15:15:31 -0800765 bool bHitTest = pSDKAnnot->GetType() == "Widget";
Tom Sepez40e9ff32015-11-30 12:39:54 -0800766#ifdef PDF_ENABLE_XFA
Tom Sepez1b246282015-11-25 15:15:31 -0800767 bHitTest = bHitTest || pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800768#endif // PDF_ENABLE_XFA
Tom Sepez1b246282015-11-25 15:15:31 -0800769 if (bHitTest) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700770 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
771 CPDF_Point point(pageX, pageY);
772 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
773 return pSDKAnnot;
774 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700775 }
776
Lei Zhangbf60b292015-10-26 12:14:35 -0700777 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778}
779
Oliver Chang972b78d2015-10-30 12:59:29 -0700780void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
781 // if there is a focused annot on the page, we should kill the focus first.
782 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
Lei Zhangcd2bb302015-12-22 13:49:44 -0800783 if (pdfium::ContainsValue(m_fxAnnotArray, focusedAnnot))
Oliver Chang972b78d2015-10-30 12:59:29 -0700784 KillFocusAnnot();
785 }
786}
787
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
789 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
790 if (pAnnotDic)
791 return pAnnotDic->KeyExist("AS");
792 return FALSE;
793}
794
795CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
796 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
797 ASSERT(pEnv);
798 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700799 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700801 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802
Lei Zhangbf60b292015-10-26 12:14:35 -0700803 m_fxAnnotArray.push_back(pSDKAnnot);
804 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700805 return pSDKAnnot;
806}
807
Tom Sepez51da0932015-11-25 16:05:49 -0800808#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700809CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
810 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700811 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700812
813 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
814 if (pSDKAnnot)
815 return pSDKAnnot;
816
817 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700818 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700819 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700821 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700822
Lei Zhangbf60b292015-10-26 12:14:35 -0700823 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824 return pSDKAnnot;
825}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800826#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700827
828CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
Wei Li9b761132016-01-29 15:44:20 -0800829 return pDict ? AddAnnot(pDict->GetStringBy("Subtype"), pDict) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700830}
831
832CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
833 CPDF_Dictionary* pDict) {
834 return NULL;
835}
836
837FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800838#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700839 if (!pAnnot)
840 return FALSE;
841 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
842 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
Tom Sepezd3116dc2015-11-24 15:58:06 -0800843 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNAMIC_XFA))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844 return FALSE;
845
Lei Zhangbf60b292015-10-26 12:14:35 -0700846 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
847 if (it != m_fxAnnotArray.end())
848 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700850 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700851
852 return TRUE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800853#else // PDF_ENABLE_XFA
854 return FALSE;
855#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856}
857
858CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
859 if (m_page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800860#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861 return m_page->GetDocument()->GetPDFDoc();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800862#else // PDF_ENABLE_XFA
863 return m_page->m_pDocument;
864#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700865 }
866 return NULL;
867}
868
Tom Sepez40e9ff32015-11-30 12:39:54 -0800869#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
871 if (m_page) {
872 return m_page->GetPDFPage();
873 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874 return NULL;
875}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800876#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877
Lei Zhang50218532015-10-30 14:03:43 -0700878size_t CPDFSDK_PageView::CountAnnots() const {
879 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700880}
881
Lei Zhangbf60b292015-10-26 12:14:35 -0700882CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
883 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884}
885
886CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700887 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
888 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700889 return pAnnot;
890 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700891 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700892}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800893
Tom Sepez51da0932015-11-25 16:05:49 -0800894#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895CPDFSDK_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}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800905#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700906
907FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
908 FX_UINT nFlag) {
909 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
910 ASSERT(pEnv);
911 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
912 if (!pFXAnnot) {
913 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700914 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700915 }
Lei Zhanga0601462015-10-30 14:54:37 -0700916
917 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
918 FX_BOOL bRet =
919 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
920 if (bRet)
921 SetFocusAnnot(pFXAnnot);
922 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700923}
924
Tom Sepez51da0932015-11-25 16:05:49 -0800925#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
927 FX_UINT nFlag) {
928 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
929 ASSERT(pEnv);
930 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
931 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700932
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700933 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
934
935 if (pFXAnnot == NULL)
936 return FALSE;
937
938 FX_BOOL bRet =
939 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
940 if (bRet) {
941 SetFocusAnnot(pFXAnnot);
942 }
943 return TRUE;
944}
945
946FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
947 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
948 ASSERT(pEnv);
949 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700950
951 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
952
953 if (pFXAnnot == NULL)
954 return FALSE;
955
956 FX_BOOL bRet =
957 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
958 if (bRet) {
959 SetFocusAnnot(pFXAnnot);
960 }
961 return TRUE;
962}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800963#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964
965FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
966 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
967 ASSERT(pEnv);
968 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700969 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
970 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
971 FX_BOOL bRet = FALSE;
972 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
973 // Last focus Annot gets a chance to handle the event.
974 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
975 }
Lei Zhanga0601462015-10-30 14:54:37 -0700976 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 return bRet;
979}
980
981FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
982 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
983 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
985 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
986 m_bExitWidget = TRUE;
987 m_bEnterWidget = FALSE;
988 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
989 }
990 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
991 m_bOnWidget = TRUE;
992 if (!m_bEnterWidget) {
993 m_bEnterWidget = TRUE;
994 m_bExitWidget = FALSE;
995 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
996 }
997 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
998 return TRUE;
999 }
1000 if (m_bOnWidget) {
1001 m_bOnWidget = FALSE;
1002 m_bExitWidget = TRUE;
1003 m_bEnterWidget = FALSE;
1004 if (m_CaptureWidget) {
1005 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
1006 m_CaptureWidget = NULL;
1007 }
1008 }
1009 return FALSE;
1010}
1011
1012FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1013 double deltaY,
1014 const CPDF_Point& point,
1015 int nFlag) {
1016 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001017 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1018 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001019 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1020 (int)deltaY, point);
1021 }
1022 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001023}
1024
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1026 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001027 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001028 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1030 }
Bo Xufdc00a72014-10-28 23:03:33 -07001031
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001033}
1034
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1036 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001037 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1040 }
1041 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001042}
1043
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001045 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001046}
1047
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001049 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez51da0932015-11-25 16:05:49 -08001050#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001051 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001052#else
Tom Sepez51da0932015-11-25 16:05:49 -08001053 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1054 // Disable the default AP construction.
1055 CPDF_InterForm::EnableUpdateAP(FALSE);
1056 m_pAnnotList.reset(new CPDF_AnnotList(m_page));
1057 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1058 const size_t nCount = m_pAnnotList->Count();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001059#endif // PDF_ENABLE_XFA
1060
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001061 SetLock(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001062
1063#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064 m_page->AddRef();
Tom Sepezd3116dc2015-11-24 15:58:06 -08001065 if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 IXFA_PageView* pageView = NULL;
1067 pageView = m_page->GetXFAPageView();
1068 ASSERT(pageView != NULL);
1069
1070 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1071 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1072 XFA_WIDGETFILTER_Viewable |
1073 XFA_WIDGETFILTER_AllType);
1074 if (!pWidgetHander) {
1075 m_page->Release();
1076 SetLock(FALSE);
1077 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001078 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079
Lei Zhangbf60b292015-10-26 12:14:35 -07001080 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001082 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001084
Lei Zhangbf60b292015-10-26 12:14:35 -07001085 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001086 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001087 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001088 pWidgetHander->Release();
1089 } else {
1090 CPDF_Page* pPage = m_page->GetPDFPage();
1091 ASSERT(pPage != NULL);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001092 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1093 // Disable the default AP construction.
1094 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001095 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001096 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1097
Lei Zhang1b700c32015-10-30 23:55:35 -07001098 const size_t nCount = m_pAnnotList->Count();
1099 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001101 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001102
1103 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1104 if (!pAnnot)
1105 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001106 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1108 }
1109 }
1110 m_page->Release();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001111#else // PDF_ENABLE_XFA
1112 for (size_t i = 0; i < nCount; ++i) {
1113 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1114 CPDF_Document* pDoc = GetPDFDocument();
1115 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1116 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1117 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1118 if (!pAnnot)
1119 continue;
1120 m_fxAnnotArray.push_back(pAnnot);
1121 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1122 }
1123#endif // PDF_ENABLE_XFA
1124
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001125 SetLock(FALSE);
1126}
1127
Jun Fang75239542016-01-20 08:04:47 +08001128void CPDFSDK_PageView::ClearFXAnnots() {
1129 SetLock(TRUE);
1130 if (m_pSDKDoc && GetFocusAnnot())
1131 m_pSDKDoc->SetFocusAnnot(nullptr);
1132 m_CaptureWidget = nullptr;
1133 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
1134 m_pSDKDoc->GetEnv()->GetAnnotHandlerMgr()->ReleaseAnnot(pAnnot);
1135 m_fxAnnotArray.clear();
1136 m_pAnnotList.reset();
1137 SetLock(FALSE);
1138}
1139
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001140void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1141 for (int i = 0; i < rects.GetSize(); i++) {
1142 CPDF_Rect rc = rects.GetAt(i);
1143 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1144 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1145 }
1146}
1147
1148void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1149 CPDF_Rect rcWindow = pAnnot->GetRect();
1150 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1151 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1152 rcWindow.bottom);
1153}
1154
1155int CPDFSDK_PageView::GetPageIndex() {
1156 if (m_page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -08001157#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001159#else // PDF_ENABLE_XFA
1160 CPDF_Dictionary* pDic = m_page->m_pFormDict;
1161#endif // PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -08001162 CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001163 if (pDoc && pDic) {
Tom Sepez50d12ad2015-11-24 09:50:51 -08001164 return pDoc->GetPageIndex(pDic->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001165 }
1166 }
1167 return -1;
1168}
1169
Lei Zhang1b700c32015-10-30 23:55:35 -07001170bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001171 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001172 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001173
Lei Zhang1b700c32015-10-30 23:55:35 -07001174 const auto& annots = m_pAnnotList->All();
Lei Zhangcd2bb302015-12-22 13:49:44 -08001175 return pdfium::ContainsValue(annots, p);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001176}
1177
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001178CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1179 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1180 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001181 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001182
Lei Zhangbf60b292015-10-26 12:14:35 -07001183 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001184 if (pAnnot == pFocusAnnot)
1185 return pAnnot;
1186 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001187 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001188}