blob: e9de9e4000bb1796855c4fa6e8242115ce7bf4d4 [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 Sepez50d12ad2015-11-24 09:50:51 -0800393CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 CPDFDoc_Environment* pEnv)
395 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 m_pFocusAnnot(nullptr),
397 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700398 m_bChangeMask(FALSE),
Tom Sepez50d12ad2015-11-24 09:50:51 -0800399 m_bBeingDestroyed(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700400
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700401CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700402 m_bBeingDestroyed = TRUE;
403
404 for (auto& it : m_pageMap)
405 it.second->KillFocusAnnotIfNeeded();
406
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407 for (auto& it : m_pageMap)
408 delete it.second;
409 m_pageMap.clear();
Bo Xufdc00a72014-10-28 23:03:33 -0700410}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411
Tom Sepez50d12ad2015-11-24 09:50:51 -0800412CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
413 UnderlyingPageType* pUnderlyingPage,
414 FX_BOOL ReNew) {
415 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700416 if (it != m_pageMap.end())
417 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 if (!ReNew)
420 return nullptr;
421
Tom Sepez50d12ad2015-11-24 09:50:51 -0800422 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
423 m_pageMap[pUnderlyingPage] = pPageView;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 // Delay to load all the annotations, to avoid endless loop.
425 pPageView->LoadFXAnnots();
426 return pPageView;
427}
428
429CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800430 UnderlyingPageType* pPage =
431 UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700432 return pPage ? GetPageView(pPage, TRUE) : nullptr;
433}
434
435CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800436 UnderlyingPageType* pTempPage =
437 UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700438 if (!pTempPage)
439 return nullptr;
440
441 auto it = m_pageMap.find(pTempPage);
442 return it->second;
443}
444
445void CPDFSDK_Document::ProcJavascriptFun() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800446 CPDF_Document* pPDFDoc = GetPDFDocument();
447 CPDF_DocJSActions docJS(pPDFDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700448 int iCount = docJS.CountJSActions();
449 if (iCount < 1)
450 return;
451 for (int i = 0; i < iCount; i++) {
452 CFX_ByteString csJSName;
453 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
454 if (m_pEnv->GetActionHander())
455 m_pEnv->GetActionHander()->DoAction_JavaScript(
456 jsAction, CFX_WideString::FromLocal(csJSName), this);
457 }
458}
459
460FX_BOOL CPDFSDK_Document::ProcOpenAction() {
461 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700462 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700463
Tom Sepez50d12ad2015-11-24 09:50:51 -0800464 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700465 if (!pRoot)
466 return FALSE;
467
Wei Li9b761132016-01-29 15:44:20 -0800468 CPDF_Object* pOpenAction = pRoot->GetDictBy("OpenAction");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700469 if (!pOpenAction)
Wei Li9b761132016-01-29 15:44:20 -0800470 pOpenAction = pRoot->GetArrayBy("OpenAction");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471
472 if (!pOpenAction)
473 return FALSE;
474
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400475 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 return TRUE;
477
Dan Sinclairf1251c12015-10-20 16:24:45 -0400478 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 CPDF_Action action(pDict);
480 if (m_pEnv->GetActionHander())
481 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
482 return TRUE;
483 }
484 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700485}
486
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
488 if (!m_pOccontent)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800489 m_pOccontent.reset(new CPDF_OCContext(GetPDFDocument()));
Oliver Chang24752492015-10-30 16:08:20 -0700490 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491}
Bo Xufdc00a72014-10-28 23:03:33 -0700492
Tom Sepez50d12ad2015-11-24 09:50:51 -0800493void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) {
494 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 if (it == m_pageMap.end())
496 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700497
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 CPDFSDK_PageView* pPageView = it->second;
499 if (pPageView->IsLocked())
500 return;
501
Oliver Chang972b78d2015-10-30 12:59:29 -0700502 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 delete pPageView;
504 m_pageMap.erase(it);
505}
506
Tom Sepez50d12ad2015-11-24 09:50:51 -0800507UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
508 return UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700509}
510
511CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
512 if (!m_pInterForm)
Oliver Chang24752492015-10-30 16:08:20 -0700513 m_pInterForm.reset(new CPDFSDK_InterForm(this));
514 return m_pInterForm.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700515}
516
517void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
518 CPDFSDK_Annot* pAnnot) {
519 for (const auto& it : m_pageMap) {
520 CPDFSDK_PageView* pPageView = it.second;
521 if (pPageView != pSender) {
522 pPageView->UpdateView(pAnnot);
523 }
524 }
525}
526
527CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
528 return m_pFocusAnnot;
529}
530
531FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700532 if (m_bBeingDestroyed)
533 return FALSE;
534
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700535 if (m_pFocusAnnot == pAnnot)
536 return TRUE;
537
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700538 if (m_pFocusAnnot) {
539 if (!KillFocusAnnot(nFlag))
540 return FALSE;
541 }
Tom Sepez540c4362015-11-24 13:33:57 -0800542
543 if (!pAnnot)
544 return FALSE;
545
Tom Sepez51da0932015-11-25 16:05:49 -0800546#ifdef PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800547 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800548#endif // PDF_ENABLE_XFA
Tom Sepez540c4362015-11-24 13:33:57 -0800549 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
550 if (pPageView && pPageView->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700551 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700552 if (!m_pFocusAnnot) {
Tom Sepez51da0932015-11-25 16:05:49 -0800553#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700555 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800556#endif // PDF_ENABLE_XFA
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;
Bo Xufdc00a72014-10-28 23:03:33 -0700573
Tom Sepez40e9ff32015-11-30 12:39:54 -0800574#ifdef PDF_ENABLE_XFA
Lei Zhanga0601462015-10-30 14:54:37 -0700575 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
576 return FALSE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800577#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -0700578
Lei Zhanga0601462015-10-30 14:54:37 -0700579 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
Lei Zhangd983b092015-12-14 16:58:33 -0800580 if (pFocusAnnot->GetType() == "Widget") {
Lei Zhanga0601462015-10-30 14:54:37 -0700581 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 return m_pEnv->GetJSRuntime();
608}
609
610CFX_WideString CPDFSDK_Document::GetPath() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611 return m_pEnv->JS_docGetFilePath();
612}
613
614CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
Tom Sepez540c4362015-11-24 13:33:57 -0800615 UnderlyingPageType* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700616 : m_page(page),
617 m_pSDKDoc(pSDKDoc),
618 m_CaptureWidget(nullptr),
Tom Sepezc46d0002015-11-30 15:46:36 -0800619#ifndef PDF_ENABLE_XFA
620 m_bTakeOverPage(FALSE),
621#endif // PDF_ENABLE_XFA
Lei Zhangbf60b292015-10-26 12:14:35 -0700622 m_bEnterWidget(FALSE),
623 m_bExitWidget(FALSE),
624 m_bOnWidget(FALSE),
625 m_bValid(FALSE),
Tom Sepez40e9ff32015-11-30 12:39:54 -0800626 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700627 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
628 if (pInterForm) {
629 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800630#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631 if (page->GetPDFPage())
632 pPDFInterForm->FixPageFields(page->GetPDFPage());
Tom Sepez40e9ff32015-11-30 12:39:54 -0800633#else // PDF_ENABLE_XFA
634 pPDFInterForm->FixPageFields(page);
635#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 }
Tom Sepez51da0932015-11-25 16:05:49 -0800637#ifndef PDF_ENABLE_XFA
638 m_page->SetPrivateData((void*)m_page, (void*)this, nullptr);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800639#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700640}
641
642CPDFSDK_PageView::~CPDFSDK_PageView() {
643 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700644 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700645 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647
Tom Sepez40e9ff32015-11-30 12:39:54 -0800648 m_fxAnnotArray.clear();
Lei Zhangbf60b292015-10-26 12:14:35 -0700649 m_pAnnotList.reset();
Tom Sepez51da0932015-11-25 16:05:49 -0800650#ifndef PDF_ENABLE_XFA
Tom Sepez51da0932015-11-25 16:05:49 -0800651 m_page->RemovePrivateData((void*)m_page);
652 if (m_bTakeOverPage) {
653 delete m_page;
654 }
Tom Sepez40e9ff32015-11-30 12:39:54 -0800655#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700656}
657
658void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
Tom Sepez60d909e2015-12-10 15:34:55 -0800659 CFX_Matrix* pUser2Device,
Tom Sepez40e9ff32015-11-30 12:39:54 -0800660#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 CPDF_RenderOptions* pOptions,
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800662 const FX_RECT& pClip) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800663#else
664 CPDF_RenderOptions* pOptions) {
665#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700667 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800668
Tom Sepez51da0932015-11-25 16:05:49 -0800669#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700670 CPDFXFA_Page* pPage = GetPDFXFAPage();
Jun Fang2592eaa2015-12-02 14:09:28 +0800671 if (!pPage)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 return;
673
Tom Sepezd3116dc2015-11-24 15:58:06 -0800674 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 CFX_Graphics gs;
676 gs.Create(pDevice);
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800677 CFX_RectF rectClip;
678 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
679 static_cast<FX_FLOAT>(pClip.top),
680 static_cast<FX_FLOAT>(pClip.Width()),
681 static_cast<FX_FLOAT>(pClip.Height()));
682 gs.SetClipRect(rectClip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700683 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
684 if (!pRenderContext)
685 return;
686 CXFA_RenderOptions renderOptions;
687 renderOptions.m_bHighlight = TRUE;
Jun Fang2592eaa2015-12-02 14:09:28 +0800688 IXFA_PageView* xfaView = pPage->GetXFAPageView();
689 pRenderContext->StartRender(xfaView, &gs, *pUser2Device, renderOptions);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 pRenderContext->DoRender();
691 pRenderContext->StopRender();
692 pRenderContext->Release();
Jun Fang2592eaa2015-12-02 14:09:28 +0800693 IXFA_DocView* docView = xfaView->GetDocView();
694 if (!docView)
695 return;
696 CPDFSDK_Annot* annot = GetFocusAnnot();
697 if (!annot)
698 return;
699 // Render the focus widget
700 docView->GetWidgetHandler()->RenderWidget(annot->GetXFAWidget(), &gs,
701 pUser2Device, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 return;
703 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700704#endif // PDF_ENABLE_XFA
705
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700707 CPDFSDK_AnnotIterator annotIterator(this, true);
708 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700709 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 }
712}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700713
Lei Zhang1b700c32015-10-30 23:55:35 -0700714const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
715 FX_FLOAT pageY) {
716 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717 CFX_FloatRect annotRect;
718 pAnnot->GetRect(annotRect);
719 if (annotRect.Contains(pageX, pageY))
720 return pAnnot;
721 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700722 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723}
724
Lei Zhang1b700c32015-10-30 23:55:35 -0700725const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
726 FX_FLOAT pageY) {
727 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 if (pAnnot->GetSubType() == "Widget") {
729 CFX_FloatRect annotRect;
730 pAnnot->GetRect(annotRect);
731 if (annotRect.Contains(pageX, pageY))
732 return pAnnot;
733 }
734 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700735 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700736}
737
738CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
739 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700740 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
741 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700742 CPDFSDK_AnnotIterator annotIterator(this, false);
743 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700744 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
745 if (rc.Contains(pageX, pageY))
746 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700747 }
748
Lei Zhangbf60b292015-10-26 12:14:35 -0700749 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750}
751
752CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
753 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700754 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
755 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700756 CPDFSDK_AnnotIterator annotIterator(this, false);
757 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Tom Sepez1b246282015-11-25 15:15:31 -0800758 bool bHitTest = pSDKAnnot->GetType() == "Widget";
Tom Sepez40e9ff32015-11-30 12:39:54 -0800759#ifdef PDF_ENABLE_XFA
Tom Sepez1b246282015-11-25 15:15:31 -0800760 bHitTest = bHitTest || pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800761#endif // PDF_ENABLE_XFA
Tom Sepez1b246282015-11-25 15:15:31 -0800762 if (bHitTest) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
764 CPDF_Point point(pageX, pageY);
765 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
766 return pSDKAnnot;
767 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700768 }
769
Lei Zhangbf60b292015-10-26 12:14:35 -0700770 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771}
772
Oliver Chang972b78d2015-10-30 12:59:29 -0700773void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
774 // if there is a focused annot on the page, we should kill the focus first.
775 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
Lei Zhangcd2bb302015-12-22 13:49:44 -0800776 if (pdfium::ContainsValue(m_fxAnnotArray, focusedAnnot))
Oliver Chang972b78d2015-10-30 12:59:29 -0700777 KillFocusAnnot();
778 }
779}
780
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700781FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
782 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
783 if (pAnnotDic)
784 return pAnnotDic->KeyExist("AS");
785 return FALSE;
786}
787
788CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
789 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
790 ASSERT(pEnv);
791 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700792 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700793 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700794 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700795
Lei Zhangbf60b292015-10-26 12:14:35 -0700796 m_fxAnnotArray.push_back(pSDKAnnot);
797 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 return pSDKAnnot;
799}
800
Tom Sepez51da0932015-11-25 16:05:49 -0800801#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
803 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700804 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700805
806 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
807 if (pSDKAnnot)
808 return pSDKAnnot;
809
810 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700811 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700812 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700813 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700814 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700815
Lei Zhangbf60b292015-10-26 12:14:35 -0700816 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700817 return pSDKAnnot;
818}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800819#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700820
821CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
Wei Li9b761132016-01-29 15:44:20 -0800822 return pDict ? AddAnnot(pDict->GetStringBy("Subtype"), pDict) : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823}
824
825CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
826 CPDF_Dictionary* pDict) {
827 return NULL;
828}
829
830FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800831#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700832 if (!pAnnot)
833 return FALSE;
834 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
835 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
Tom Sepezd3116dc2015-11-24 15:58:06 -0800836 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNAMIC_XFA))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700837 return FALSE;
838
Lei Zhangbf60b292015-10-26 12:14:35 -0700839 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
840 if (it != m_fxAnnotArray.end())
841 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700843 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844
845 return TRUE;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800846#else // PDF_ENABLE_XFA
847 return FALSE;
848#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700849}
850
851CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
852 if (m_page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -0800853#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700854 return m_page->GetDocument()->GetPDFDoc();
Tom Sepez40e9ff32015-11-30 12:39:54 -0800855#else // PDF_ENABLE_XFA
856 return m_page->m_pDocument;
857#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858 }
859 return NULL;
860}
861
Tom Sepez40e9ff32015-11-30 12:39:54 -0800862#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700863CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
864 if (m_page) {
865 return m_page->GetPDFPage();
866 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700867 return NULL;
868}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800869#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870
Lei Zhang50218532015-10-30 14:03:43 -0700871size_t CPDFSDK_PageView::CountAnnots() const {
872 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873}
874
Lei Zhangbf60b292015-10-26 12:14:35 -0700875CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
876 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700877}
878
879CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700880 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
881 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700882 return pAnnot;
883 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700884 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700885}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800886
Tom Sepez51da0932015-11-25 16:05:49 -0800887#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700888CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700889 if (!hWidget)
890 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700891
Lei Zhangbf60b292015-10-26 12:14:35 -0700892 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 if (pAnnot->GetXFAWidget() == hWidget)
894 return pAnnot;
895 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700896 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700897}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800898#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700899
900FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
901 FX_UINT nFlag) {
902 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
903 ASSERT(pEnv);
904 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
905 if (!pFXAnnot) {
906 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700907 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 }
Lei Zhanga0601462015-10-30 14:54:37 -0700909
910 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
911 FX_BOOL bRet =
912 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
913 if (bRet)
914 SetFocusAnnot(pFXAnnot);
915 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700916}
917
Tom Sepez51da0932015-11-25 16:05:49 -0800918#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700919FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
920 FX_UINT nFlag) {
921 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
922 ASSERT(pEnv);
923 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
924 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700925
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700926 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
927
928 if (pFXAnnot == NULL)
929 return FALSE;
930
931 FX_BOOL bRet =
932 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
933 if (bRet) {
934 SetFocusAnnot(pFXAnnot);
935 }
936 return TRUE;
937}
938
939FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
940 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
941 ASSERT(pEnv);
942 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943
944 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
945
946 if (pFXAnnot == NULL)
947 return FALSE;
948
949 FX_BOOL bRet =
950 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
951 if (bRet) {
952 SetFocusAnnot(pFXAnnot);
953 }
954 return TRUE;
955}
Tom Sepez40e9ff32015-11-30 12:39:54 -0800956#endif // PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700957
958FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
959 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
960 ASSERT(pEnv);
961 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700962 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
963 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
964 FX_BOOL bRet = FALSE;
965 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
966 // Last focus Annot gets a chance to handle the event.
967 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
968 }
Lei Zhanga0601462015-10-30 14:54:37 -0700969 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700970 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 return bRet;
972}
973
974FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
975 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
976 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
978 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
979 m_bExitWidget = TRUE;
980 m_bEnterWidget = FALSE;
981 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
982 }
983 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
984 m_bOnWidget = TRUE;
985 if (!m_bEnterWidget) {
986 m_bEnterWidget = TRUE;
987 m_bExitWidget = FALSE;
988 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
989 }
990 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
991 return TRUE;
992 }
993 if (m_bOnWidget) {
994 m_bOnWidget = FALSE;
995 m_bExitWidget = TRUE;
996 m_bEnterWidget = FALSE;
997 if (m_CaptureWidget) {
998 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
999 m_CaptureWidget = NULL;
1000 }
1001 }
1002 return FALSE;
1003}
1004
1005FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
1006 double deltaY,
1007 const CPDF_Point& point,
1008 int nFlag) {
1009 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001010 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1011 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
1013 (int)deltaY, point);
1014 }
1015 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001016}
1017
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
1019 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001020 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001021 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001022 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
1023 }
Bo Xufdc00a72014-10-28 23:03:33 -07001024
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001025 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001026}
1027
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001028FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1029 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001030 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1033 }
1034 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001035}
1036
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001038 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001039}
1040
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001041void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez51da0932015-11-25 16:05:49 -08001043#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001044 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001045#else
Tom Sepez51da0932015-11-25 16:05:49 -08001046 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();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001052#endif // PDF_ENABLE_XFA
1053
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 SetLock(TRUE);
Tom Sepez40e9ff32015-11-30 12:39:54 -08001055
1056#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 m_page->AddRef();
Tom Sepezd3116dc2015-11-24 15:58:06 -08001058 if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059 IXFA_PageView* pageView = NULL;
1060 pageView = m_page->GetXFAPageView();
1061 ASSERT(pageView != NULL);
1062
1063 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1064 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1065 XFA_WIDGETFILTER_Viewable |
1066 XFA_WIDGETFILTER_AllType);
1067 if (!pWidgetHander) {
1068 m_page->Release();
1069 SetLock(FALSE);
1070 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001071 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001072
Lei Zhangbf60b292015-10-26 12:14:35 -07001073 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001074 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001075 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001076 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001077
Lei Zhangbf60b292015-10-26 12:14:35 -07001078 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001080 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001081 pWidgetHander->Release();
1082 } else {
1083 CPDF_Page* pPage = m_page->GetPDFPage();
1084 ASSERT(pPage != NULL);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1086 // Disable the default AP construction.
1087 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001088 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001089 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1090
Lei Zhang1b700c32015-10-30 23:55:35 -07001091 const size_t nCount = m_pAnnotList->Count();
1092 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001093 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001094 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001095
1096 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1097 if (!pAnnot)
1098 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001099 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001100 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1101 }
1102 }
1103 m_page->Release();
Tom Sepez40e9ff32015-11-30 12:39:54 -08001104#else // PDF_ENABLE_XFA
1105 for (size_t i = 0; i < nCount; ++i) {
1106 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
1107 CPDF_Document* pDoc = GetPDFDocument();
1108 CheckUnSupportAnnot(pDoc, pPDFAnnot);
1109 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
1110 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1111 if (!pAnnot)
1112 continue;
1113 m_fxAnnotArray.push_back(pAnnot);
1114 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1115 }
1116#endif // PDF_ENABLE_XFA
1117
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001118 SetLock(FALSE);
1119}
1120
Jun Fang75239542016-01-20 08:04:47 +08001121void CPDFSDK_PageView::ClearFXAnnots() {
1122 SetLock(TRUE);
1123 if (m_pSDKDoc && GetFocusAnnot())
1124 m_pSDKDoc->SetFocusAnnot(nullptr);
1125 m_CaptureWidget = nullptr;
1126 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
1127 m_pSDKDoc->GetEnv()->GetAnnotHandlerMgr()->ReleaseAnnot(pAnnot);
1128 m_fxAnnotArray.clear();
1129 m_pAnnotList.reset();
1130 SetLock(FALSE);
1131}
1132
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001133void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1134 for (int i = 0; i < rects.GetSize(); i++) {
1135 CPDF_Rect rc = rects.GetAt(i);
1136 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1137 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1138 }
1139}
1140
1141void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1142 CPDF_Rect rcWindow = pAnnot->GetRect();
1143 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1144 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1145 rcWindow.bottom);
1146}
1147
1148int CPDFSDK_PageView::GetPageIndex() {
1149 if (m_page) {
Tom Sepez40e9ff32015-11-30 12:39:54 -08001150#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001151 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
Tom Sepez40e9ff32015-11-30 12:39:54 -08001152#else // PDF_ENABLE_XFA
1153 CPDF_Dictionary* pDic = m_page->m_pFormDict;
1154#endif // PDF_ENABLE_XFA
Tom Sepez50d12ad2015-11-24 09:50:51 -08001155 CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001156 if (pDoc && pDic) {
Tom Sepez50d12ad2015-11-24 09:50:51 -08001157 return pDoc->GetPageIndex(pDic->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001158 }
1159 }
1160 return -1;
1161}
1162
Lei Zhang1b700c32015-10-30 23:55:35 -07001163bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001164 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001165 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001166
Lei Zhang1b700c32015-10-30 23:55:35 -07001167 const auto& annots = m_pAnnotList->All();
Lei Zhangcd2bb302015-12-22 13:49:44 -08001168 return pdfium::ContainsValue(annots, p);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001169}
1170
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001171CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1172 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1173 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001174 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001175
Lei Zhangbf60b292015-10-26 12:14:35 -07001176 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001177 if (pAnnot == pFocusAnnot)
1178 return pAnnot;
1179 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001180 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001181}