blob: 27cfe514c8d463671eba1a6eac4dd37a0a3c1eb0 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangbf60b292015-10-26 12:14:35 -07007#include <algorithm>
8
Lei Zhangbde53d22015-11-12 22:21:30 -08009#include "fpdfsdk/include/fsdk_mgr.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080010
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/formfiller/FFL_FormFiller.h"
12#include "fpdfsdk/include/fsdk_define.h"
13#include "fpdfsdk/include/javascript/IJavaScript.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080014#include "public/fpdf_ext.h"
Lei Zhang8241df72015-11-06 14:38:48 -080015#include "third_party/base/nonstd_unique_ptr.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070016
Tom Sepez37458412015-10-06 11:33:46 -070017#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070018#include "../include/fpdfxfa/fpdfxfa_doc.h"
19#include "../include/fpdfxfa/fpdfxfa_page.h"
20#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
22#if _FX_OS_ == _FX_ANDROID_
23#include "time.h"
24#else
25#include <ctime>
26#endif
27
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028class CFX_SystemHandler : public IFX_SystemHandler {
29 public:
Lei Zhangbf60b292015-10-26 12:14:35 -070030 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
31 : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070032 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070035 // IFX_SystemHandler
36 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
37 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
38 FX_BOOL IsSelectionImplemented() override;
39 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
40 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 return FALSE;
42 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070043 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
44 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
45 void SetCursor(int32_t nCursorType) override;
46 FX_HMENU CreatePopupMenu() override { return NULL; }
47 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
48 int32_t nIDNewItem,
49 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 return FALSE;
51 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070052 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
53 int32_t nIDItem,
54 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 return FALSE;
56 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070057 int32_t TrackPopupMenu(FX_HMENU hMenu,
58 int32_t x,
59 int32_t y,
60 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 return -1;
62 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070063 void DestroyMenu(FX_HMENU hMenu) override {}
64 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
65 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
66 CFX_ByteString sFontFaceName) override;
67 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
68 CFX_ByteString sFontFaceName,
69 uint8_t nCharset) override;
70 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
71 void KillTimer(int32_t nID) override;
72 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
74 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070075 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
77 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070078 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 return m_pEnv->FFI_IsALTKeyDown(nFlag);
80 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070081 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
83 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070084 FX_SYSTEMTIME GetLocalTime() override;
85 int32_t GetCharSet() override { return m_nCharSet; }
86 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087
88 private:
89 CPDFDoc_Environment* m_pEnv;
90 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091};
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
94 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
Tom Sepez50d12ad2015-11-24 09:50:51 -080099 CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
100 UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (!pPage || !pPageView)
102 return;
103 CPDF_Matrix page2device;
104 pPageView->GetCurrentMatrix(page2device);
105 CPDF_Matrix device2page;
106 device2page.SetReverse(page2device);
107 FX_FLOAT left, top, right, bottom;
108 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
109 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
110 bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 CPDF_Rect rcPDF(left, bottom, right, top);
112 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
115 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
118 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
119 if (pFFL) {
120 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
121 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
122 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
123 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
124 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
Tom Sepez50d12ad2015-11-24 09:50:51 -0800125 UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 ASSERT(pPage);
127 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
128 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
132 if (m_pEnv) {
133 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
134 if (pInfo && pInfo->FFI_OutputSelectedRect)
135 return TRUE;
136 }
137 return FALSE;
138}
139
140CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
141 return "";
142}
143
144FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
145 int32_t nCharset,
146 CFX_ByteString sFontFaceName) {
147 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
Lei Zhang122ee212015-10-21 17:07:24 -0700148 if (!pFontMgr)
149 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150
Lei Zhang122ee212015-10-21 17:07:24 -0700151 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
152 if (!pFontMapper)
153 return FALSE;
154
155 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
156 if (nSize == 0) {
157 pFontMapper->LoadInstalledFonts();
158 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
159 }
160
161 for (int i = 0; i < nSize; ++i) {
162 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
163 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 }
165
166 return FALSE;
167}
168
169static int CharSet2CP(int charset) {
170 if (charset == 128)
171 return 932;
172 if (charset == 134)
173 return 936;
174 if (charset == 129)
175 return 949;
176 if (charset == 136)
177 return 950;
178 return 0;
179}
180CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
181 CPDF_Document* pDoc,
182 CFX_ByteString sFontFaceName,
183 uint8_t nCharset) {
184 if (pDoc) {
185 CFX_Font* pFXFont = new CFX_Font();
186 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
187 FALSE);
188 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
189 delete pFXFont;
190 return pFont;
191 }
192
193 return NULL;
194}
195
196int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
197 TimerCallback lpTimerFunc) {
198 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
199}
200void CFX_SystemHandler::KillTimer(int32_t nID) {
201 m_pEnv->FFI_KillTimer(nID);
202}
203
204FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
205 return m_pEnv->FFI_GetLocalTime();
206}
207
Tom Sepez50d12ad2015-11-24 09:50:51 -0800208CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 FPDF_FORMFILLINFO* pFFinfo)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800210 : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pUnderlyingDoc(pDoc) {
Oliver Changde6088d2015-10-30 16:30:28 -0700211 m_pSysHandler.reset(new CFX_SystemHandler(this));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212}
213
214CPDFDoc_Environment::~CPDFDoc_Environment() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700216 if (pProvider->m_pEnvList.GetSize() == 0)
217 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218}
219
220int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
221 const FX_WCHAR* Title,
222 FX_UINT Type,
223 FX_UINT Icon) {
224 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
225 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
226 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
227 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
228 FPDF_WIDESTRING pTitle =
229 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
230 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
231 pTitle, Type, Icon);
232 bsMsg.ReleaseBuffer();
233 bsTitle.ReleaseBuffer();
234 return ret;
235 }
236 return -1;
237}
238
239int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
240 const FX_WCHAR* Title,
241 const FX_WCHAR* Default,
242 const FX_WCHAR* cLabel,
243 FPDF_BOOL bPassword,
244 void* response,
245 int length) {
246 if (m_pInfo && m_pInfo->m_pJsPlatform &&
247 m_pInfo->m_pJsPlatform->app_response) {
248 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
249 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
250 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
251 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
252 FPDF_WIDESTRING pQuestion =
253 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
254 FPDF_WIDESTRING pTitle =
255 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
256 FPDF_WIDESTRING pDefault =
257 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
258 FPDF_WIDESTRING pLabel =
259 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
260 int ret = m_pInfo->m_pJsPlatform->app_response(
261 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
262 response, length);
263 bsQuestion.ReleaseBuffer();
264 bsTitle.ReleaseBuffer();
265 bsDefault.ReleaseBuffer();
266 bsLabel.ReleaseBuffer();
267 return ret;
268 }
269 return -1;
270}
271
272CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
273 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
274 !m_pInfo->m_pJsPlatform->Field_browse) {
275 return L"";
276 }
277
278 const int nRequiredLen =
279 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
280 if (nRequiredLen <= 0)
281 return L"";
282
283 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
284 memset(pBuff.get(), 0, nRequiredLen);
285 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
286 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
287 if (nActualLen <= 0 || nActualLen > nRequiredLen)
288 return L"";
289
290 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
291 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
292 return wsRet;
293}
294
295CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
296 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
297 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
298 return L"";
299 }
300
301 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
302 m_pInfo->m_pJsPlatform, nullptr, 0);
303 if (nRequiredLen <= 0)
304 return L"";
305
306 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
307 memset(pBuff.get(), 0, nRequiredLen);
308 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
309 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
310 if (nActualLen <= 0 || nActualLen > nRequiredLen)
311 return L"";
312
313 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
314 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
315 return wsRet;
316}
317
318void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
319 int length,
320 const FX_WCHAR* URL) {
321 if (m_pInfo && m_pInfo->m_pJsPlatform &&
322 m_pInfo->m_pJsPlatform->Doc_submitForm) {
323 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
324 FPDF_WIDESTRING pDestination =
325 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
326 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
327 length, pDestination);
328 bsDestination.ReleaseBuffer();
329 }
330}
331
332void CPDFDoc_Environment::JS_docmailForm(void* mailData,
333 int length,
334 FPDF_BOOL bUI,
335 const FX_WCHAR* To,
336 const FX_WCHAR* Subject,
337 const FX_WCHAR* CC,
338 const FX_WCHAR* BCC,
339 const FX_WCHAR* Msg) {
340 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
341 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
342 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
343 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
344 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
345 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
346 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
347 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
348 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
349 FPDF_WIDESTRING pSubject =
350 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
351 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
352 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
353 bUI, pTo, pSubject, pCC, pBcc, pMsg);
354 bsTo.ReleaseBuffer();
355 bsCC.ReleaseBuffer();
356 bsBcc.ReleaseBuffer();
357 bsSubject.ReleaseBuffer();
358 bsMsg.ReleaseBuffer();
359 }
360}
361
Tom Sepezba038bc2015-10-08 12:03:00 -0700362IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700364 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700366 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700367 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700368}
369
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
371 if (!m_pAnnotHandlerMgr)
Oliver Chang24752492015-10-30 16:08:20 -0700372 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this));
373 return m_pAnnotHandlerMgr.get();
Bo Xufdc00a72014-10-28 23:03:33 -0700374}
375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
377 if (!m_pActionHandler)
Oliver Chang24752492015-10-30 16:08:20 -0700378 m_pActionHandler.reset(new CPDFSDK_ActionHandler());
379 return m_pActionHandler.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380}
381
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
383 if (!m_pIFormFiller)
Oliver Chang24752492015-10-30 16:08:20 -0700384 m_pIFormFiller.reset(new CFFL_IFormFiller(this));
385 return m_pIFormFiller.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386}
387
Tom Sepez50d12ad2015-11-24 09:50:51 -0800388CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 CPDFDoc_Environment* pEnv)
390 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 m_pFocusAnnot(nullptr),
392 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700393 m_bChangeMask(FALSE),
Tom Sepez50d12ad2015-11-24 09:50:51 -0800394 m_bBeingDestroyed(FALSE) {}
Bo Xufdc00a72014-10-28 23:03:33 -0700395
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700397 m_bBeingDestroyed = TRUE;
398
399 for (auto& it : m_pageMap)
400 it.second->KillFocusAnnotIfNeeded();
401
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402 for (auto& it : m_pageMap)
403 delete it.second;
404 m_pageMap.clear();
Bo Xufdc00a72014-10-28 23:03:33 -0700405}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700406
Tom Sepez50d12ad2015-11-24 09:50:51 -0800407CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
408 UnderlyingPageType* pUnderlyingPage,
409 FX_BOOL ReNew) {
410 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 if (it != m_pageMap.end())
412 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700413
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414 if (!ReNew)
415 return nullptr;
416
Tom Sepez50d12ad2015-11-24 09:50:51 -0800417 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
418 m_pageMap[pUnderlyingPage] = pPageView;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 // Delay to load all the annotations, to avoid endless loop.
420 pPageView->LoadFXAnnots();
421 return pPageView;
422}
423
424CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800425 UnderlyingPageType* pPage =
426 UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 return pPage ? GetPageView(pPage, TRUE) : nullptr;
428}
429
430CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800431 UnderlyingPageType* pTempPage =
432 UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700433 if (!pTempPage)
434 return nullptr;
435
436 auto it = m_pageMap.find(pTempPage);
437 return it->second;
438}
439
440void CPDFSDK_Document::ProcJavascriptFun() {
Tom Sepez50d12ad2015-11-24 09:50:51 -0800441 CPDF_Document* pPDFDoc = GetPDFDocument();
442 CPDF_DocJSActions docJS(pPDFDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700443 int iCount = docJS.CountJSActions();
444 if (iCount < 1)
445 return;
446 for (int i = 0; i < iCount; i++) {
447 CFX_ByteString csJSName;
448 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
449 if (m_pEnv->GetActionHander())
450 m_pEnv->GetActionHander()->DoAction_JavaScript(
451 jsAction, CFX_WideString::FromLocal(csJSName), this);
452 }
453}
454
455FX_BOOL CPDFSDK_Document::ProcOpenAction() {
456 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700457 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700458
Tom Sepez50d12ad2015-11-24 09:50:51 -0800459 CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460 if (!pRoot)
461 return FALSE;
462
463 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
464 if (!pOpenAction)
465 pOpenAction = pRoot->GetArray("OpenAction");
466
467 if (!pOpenAction)
468 return FALSE;
469
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400470 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700471 return TRUE;
472
Dan Sinclairf1251c12015-10-20 16:24:45 -0400473 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700474 CPDF_Action action(pDict);
475 if (m_pEnv->GetActionHander())
476 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
477 return TRUE;
478 }
479 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700480}
481
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700482CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
483 if (!m_pOccontent)
Tom Sepez50d12ad2015-11-24 09:50:51 -0800484 m_pOccontent.reset(new CPDF_OCContext(GetPDFDocument()));
Oliver Chang24752492015-10-30 16:08:20 -0700485 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486}
Bo Xufdc00a72014-10-28 23:03:33 -0700487
Tom Sepez50d12ad2015-11-24 09:50:51 -0800488void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) {
489 auto it = m_pageMap.find(pUnderlyingPage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 if (it == m_pageMap.end())
491 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700492
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700493 CPDFSDK_PageView* pPageView = it->second;
494 if (pPageView->IsLocked())
495 return;
496
Oliver Chang972b78d2015-10-30 12:59:29 -0700497 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 delete pPageView;
499 m_pageMap.erase(it);
500}
501
Tom Sepez50d12ad2015-11-24 09:50:51 -0800502UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
503 return UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700504}
505
506CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
507 if (!m_pInterForm)
Oliver Chang24752492015-10-30 16:08:20 -0700508 m_pInterForm.reset(new CPDFSDK_InterForm(this));
509 return m_pInterForm.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510}
511
512void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
513 CPDFSDK_Annot* pAnnot) {
514 for (const auto& it : m_pageMap) {
515 CPDFSDK_PageView* pPageView = it.second;
516 if (pPageView != pSender) {
517 pPageView->UpdateView(pAnnot);
518 }
519 }
520}
521
522CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
523 return m_pFocusAnnot;
524}
525
526FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700527 if (m_bBeingDestroyed)
528 return FALSE;
529
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700530 if (m_pFocusAnnot == pAnnot)
531 return TRUE;
532
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700533 if (m_pFocusAnnot) {
534 if (!KillFocusAnnot(nFlag))
535 return FALSE;
536 }
Tom Sepez540c4362015-11-24 13:33:57 -0800537
538 if (!pAnnot)
539 return FALSE;
540
541 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
542 CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
543 if (pPageView && pPageView->IsValid()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700544 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700545 if (!m_pFocusAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700547 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700548
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700549 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
550 return FALSE;
551 if (!m_pFocusAnnot) {
552 m_pFocusAnnot = pAnnot;
553 return TRUE;
554 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700555 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700556 }
557 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700558}
559
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
561 if (m_pFocusAnnot) {
562 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700563 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
564 m_pFocusAnnot = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700565
Lei Zhanga0601462015-10-30 14:54:37 -0700566 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
567 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700568
Lei Zhanga0601462015-10-30 14:54:37 -0700569 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
570 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
571 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
572 int nFieldType = pWidget->GetFieldType();
573 if (FIELDTYPE_TEXTFIELD == nFieldType ||
574 FIELDTYPE_COMBOBOX == nFieldType) {
575 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700576 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700577 }
Lei Zhanga0601462015-10-30 14:54:37 -0700578
579 if (!m_pFocusAnnot)
580 return TRUE;
581 } else {
582 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700583 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700584 }
585 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700586}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700587
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588void CPDFSDK_Document::OnCloseDocument() {
589 KillFocusAnnot();
590}
591
592FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
Tom Sepez540c4362015-11-24 13:33:57 -0800593 return GetPDFDocument()->GetUserPermissions() & nFlag;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594}
595
Tom Sepezba038bc2015-10-08 12:03:00 -0700596IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700597 ASSERT(m_pEnv != NULL);
598 return m_pEnv->GetJSRuntime();
599}
600
601CFX_WideString CPDFSDK_Document::GetPath() {
602 ASSERT(m_pEnv != NULL);
603 return m_pEnv->JS_docGetFilePath();
604}
605
606CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
Tom Sepez540c4362015-11-24 13:33:57 -0800607 UnderlyingPageType* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700608 : m_page(page),
609 m_pSDKDoc(pSDKDoc),
610 m_CaptureWidget(nullptr),
611 m_bEnterWidget(FALSE),
612 m_bExitWidget(FALSE),
613 m_bOnWidget(FALSE),
614 m_bValid(FALSE),
615 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
617 if (pInterForm) {
618 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
619 if (page->GetPDFPage())
620 pPDFInterForm->FixPageFields(page->GetPDFPage());
621 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700622}
623
624CPDFSDK_PageView::~CPDFSDK_PageView() {
625 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700626 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700627 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700628 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700629 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630
Lei Zhangbf60b292015-10-26 12:14:35 -0700631 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700632}
633
634void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
635 CPDF_Matrix* pUser2Device,
636 CPDF_RenderOptions* pOptions,
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800637 const FX_RECT& pClip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700639 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
640 CPDFXFA_Page* pPage = GetPDFXFAPage();
641
642 if (pPage == NULL)
643 return;
644
Tom Sepeza8a39e22015-10-12 15:47:07 -0700645#ifdef PDF_ENABLE_XFA
Tom Sepezd3116dc2015-11-24 15:58:06 -0800646 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647 CFX_Graphics gs;
648 gs.Create(pDevice);
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800649 CFX_RectF rectClip;
650 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
651 static_cast<FX_FLOAT>(pClip.top),
652 static_cast<FX_FLOAT>(pClip.Width()),
653 static_cast<FX_FLOAT>(pClip.Height()));
654 gs.SetClipRect(rectClip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700655 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
656 if (!pRenderContext)
657 return;
658 CXFA_RenderOptions renderOptions;
659 renderOptions.m_bHighlight = TRUE;
660 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
661 renderOptions);
662 pRenderContext->DoRender();
663 pRenderContext->StopRender();
664 pRenderContext->Release();
665 return;
666 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700667#endif // PDF_ENABLE_XFA
668
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700669 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700670 CPDFSDK_AnnotIterator annotIterator(this, true);
671 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700672 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700673 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700674 }
675}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700676
Lei Zhang1b700c32015-10-30 23:55:35 -0700677const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
678 FX_FLOAT pageY) {
679 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680 CFX_FloatRect annotRect;
681 pAnnot->GetRect(annotRect);
682 if (annotRect.Contains(pageX, pageY))
683 return pAnnot;
684 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700685 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700686}
687
Lei Zhang1b700c32015-10-30 23:55:35 -0700688const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
689 FX_FLOAT pageY) {
690 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691 if (pAnnot->GetSubType() == "Widget") {
692 CFX_FloatRect annotRect;
693 pAnnot->GetRect(annotRect);
694 if (annotRect.Contains(pageX, pageY))
695 return pAnnot;
696 }
697 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700698 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700699}
700
701CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
702 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700703 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
704 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700705 CPDFSDK_AnnotIterator annotIterator(this, false);
706 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
708 if (rc.Contains(pageX, pageY))
709 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700710 }
711
Lei Zhangbf60b292015-10-26 12:14:35 -0700712 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713}
714
715CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
716 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700717 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
718 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700719 CPDFSDK_AnnotIterator annotIterator(this, false);
720 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721 if (pSDKAnnot->GetType() == "Widget" ||
722 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
723 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
724 CPDF_Point point(pageX, pageY);
725 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
726 return pSDKAnnot;
727 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700728 }
729
Lei Zhangbf60b292015-10-26 12:14:35 -0700730 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731}
732
Oliver Chang972b78d2015-10-30 12:59:29 -0700733void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
734 // if there is a focused annot on the page, we should kill the focus first.
735 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
736 auto it =
737 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
738 if (it != m_fxAnnotArray.end())
739 KillFocusAnnot();
740 }
741}
742
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
744 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
745 if (pAnnotDic)
746 return pAnnotDic->KeyExist("AS");
747 return FALSE;
748}
749
750CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
751 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
752 ASSERT(pEnv);
753 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700754 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700755 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700756 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700757
Lei Zhangbf60b292015-10-26 12:14:35 -0700758 m_fxAnnotArray.push_back(pSDKAnnot);
759 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700760 return pSDKAnnot;
761}
762
763CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
764 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700765 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700766
767 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
768 if (pSDKAnnot)
769 return pSDKAnnot;
770
771 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700772 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700773 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700774 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700775 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700776
Lei Zhangbf60b292015-10-26 12:14:35 -0700777 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700778 return pSDKAnnot;
779}
780
781CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
782 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
783}
784
785CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
786 CPDF_Dictionary* pDict) {
787 return NULL;
788}
789
790FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
791 if (!pAnnot)
792 return FALSE;
793 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
794 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
Tom Sepezd3116dc2015-11-24 15:58:06 -0800795 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNAMIC_XFA))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796 return FALSE;
797
Lei Zhangbf60b292015-10-26 12:14:35 -0700798 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
799 if (it != m_fxAnnotArray.end())
800 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700801 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700802 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700803
804 return TRUE;
805}
806
807CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
808 if (m_page) {
809 return m_page->GetDocument()->GetPDFDoc();
810 }
811 return NULL;
812}
813
814CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
815 if (m_page) {
816 return m_page->GetPDFPage();
817 }
818
819 return NULL;
820}
821
Lei Zhang50218532015-10-30 14:03:43 -0700822size_t CPDFSDK_PageView::CountAnnots() const {
823 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700824}
825
Lei Zhangbf60b292015-10-26 12:14:35 -0700826CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
827 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700828}
829
830CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700831 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
832 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700833 return pAnnot;
834 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700835 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700836}
837CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700838 if (!hWidget)
839 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700840
Lei Zhangbf60b292015-10-26 12:14:35 -0700841 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700842 if (pAnnot->GetXFAWidget() == hWidget)
843 return pAnnot;
844 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700845 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846}
847
848FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
849 FX_UINT nFlag) {
850 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
851 ASSERT(pEnv);
852 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
853 if (!pFXAnnot) {
854 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700855 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856 }
Lei Zhanga0601462015-10-30 14:54:37 -0700857
858 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
859 FX_BOOL bRet =
860 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
861 if (bRet)
862 SetFocusAnnot(pFXAnnot);
863 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700864}
865
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
867 FX_UINT nFlag) {
868 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
869 ASSERT(pEnv);
870 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
871 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700872
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700873 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
874
875 if (pFXAnnot == NULL)
876 return FALSE;
877
878 FX_BOOL bRet =
879 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
880 if (bRet) {
881 SetFocusAnnot(pFXAnnot);
882 }
883 return TRUE;
884}
885
886FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
887 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
888 ASSERT(pEnv);
889 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700890
891 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
892
893 if (pFXAnnot == NULL)
894 return FALSE;
895
896 FX_BOOL bRet =
897 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
898 if (bRet) {
899 SetFocusAnnot(pFXAnnot);
900 }
901 return TRUE;
902}
903
904FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
905 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
906 ASSERT(pEnv);
907 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700908 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
909 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
910 FX_BOOL bRet = FALSE;
911 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
912 // Last focus Annot gets a chance to handle the event.
913 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
914 }
Lei Zhanga0601462015-10-30 14:54:37 -0700915 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700916 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700917 return bRet;
918}
919
920FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
921 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
922 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700923 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
924 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
925 m_bExitWidget = TRUE;
926 m_bEnterWidget = FALSE;
927 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
928 }
929 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
930 m_bOnWidget = TRUE;
931 if (!m_bEnterWidget) {
932 m_bEnterWidget = TRUE;
933 m_bExitWidget = FALSE;
934 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
935 }
936 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
937 return TRUE;
938 }
939 if (m_bOnWidget) {
940 m_bOnWidget = FALSE;
941 m_bExitWidget = TRUE;
942 m_bEnterWidget = FALSE;
943 if (m_CaptureWidget) {
944 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
945 m_CaptureWidget = NULL;
946 }
947 }
948 return FALSE;
949}
950
951FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
952 double deltaY,
953 const CPDF_Point& point,
954 int nFlag) {
955 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700956 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
957 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700958 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
959 (int)deltaY, point);
960 }
961 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700962}
963
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700964FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
965 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700966 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700967 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700968 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
969 }
Bo Xufdc00a72014-10-28 23:03:33 -0700970
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700971 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700972}
973
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700974FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
975 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -0700976 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700977 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
979 }
980 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700981}
982
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700983FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700985}
986
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700987void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700988 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700989 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990
991 SetLock(TRUE);
992 m_page->AddRef();
Tom Sepezd3116dc2015-11-24 15:58:06 -0800993 if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994 IXFA_PageView* pageView = NULL;
995 pageView = m_page->GetXFAPageView();
996 ASSERT(pageView != NULL);
997
998 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
999 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1000 XFA_WIDGETFILTER_Viewable |
1001 XFA_WIDGETFILTER_AllType);
1002 if (!pWidgetHander) {
1003 m_page->Release();
1004 SetLock(FALSE);
1005 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001006 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007
Lei Zhangbf60b292015-10-26 12:14:35 -07001008 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001010 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012
Lei Zhangbf60b292015-10-26 12:14:35 -07001013 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001014 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015 }
1016
1017 pWidgetHander->Release();
1018 } else {
1019 CPDF_Page* pPage = m_page->GetPDFPage();
1020 ASSERT(pPage != NULL);
1021
1022 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1023 // Disable the default AP construction.
1024 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001025 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001026 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1027
Lei Zhang1b700c32015-10-30 23:55:35 -07001028 const size_t nCount = m_pAnnotList->Count();
1029 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001030 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001031 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032
1033 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1034 if (!pAnnot)
1035 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001036 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037
1038 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1039 }
1040 }
1041 m_page->Release();
1042 SetLock(FALSE);
1043}
1044
1045void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1046 for (int i = 0; i < rects.GetSize(); i++) {
1047 CPDF_Rect rc = rects.GetAt(i);
1048 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1049 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1050 }
1051}
1052
1053void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1054 CPDF_Rect rcWindow = pAnnot->GetRect();
1055 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1056 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1057 rcWindow.bottom);
1058}
1059
1060int CPDFSDK_PageView::GetPageIndex() {
1061 if (m_page) {
1062 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
Tom Sepez50d12ad2015-11-24 09:50:51 -08001063 CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001064 if (pDoc && pDic) {
Tom Sepez50d12ad2015-11-24 09:50:51 -08001065 return pDoc->GetPageIndex(pDic->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001066 }
1067 }
1068 return -1;
1069}
1070
Lei Zhang1b700c32015-10-30 23:55:35 -07001071bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001072 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001073 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001074
Lei Zhang1b700c32015-10-30 23:55:35 -07001075 const auto& annots = m_pAnnotList->All();
1076 return std::find(annots.begin(), annots.end(), p) != annots.end();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001077}
1078
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001079CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1080 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1081 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001082 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001083
Lei Zhangbf60b292015-10-26 12:14:35 -07001084 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001085 if (pAnnot == pFocusAnnot)
1086 return pAnnot;
1087 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001088 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001089}