blob: f12e8fb59565013df6f2ad1752413002d3242ccb [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhang60f507b2015-06-13 00:41:00 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangbf60b292015-10-26 12:14:35 -07007#include <algorithm>
8
Tom Sepez1ed8a212015-05-11 15:25:39 -07009#include "../../public/fpdf_ext.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070010#include "../../third_party/base/nonstd_unique_ptr.h"
Tom Sepez37458412015-10-06 11:33:46 -070011#include "../include/formfiller/FFL_FormFiller.h"
Tom Sepezdfbf8e72015-10-14 14:17:26 -070012#include "../include/fsdk_define.h"
13#include "../include/fsdk_mgr.h"
14#include "../include/javascript/IJavaScript.h"
15
Tom Sepez37458412015-10-06 11:33:46 -070016#include "../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070017#include "../include/fpdfxfa/fpdfxfa_doc.h"
18#include "../include/fpdfxfa/fpdfxfa_page.h"
19#include "../include/fpdfxfa/fpdfxfa_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
21#if _FX_OS_ == _FX_ANDROID_
22#include "time.h"
23#else
24#include <ctime>
25#endif
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027class CFX_SystemHandler : public IFX_SystemHandler {
28 public:
Lei Zhangbf60b292015-10-26 12:14:35 -070029 explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
30 : m_pEnv(pEnv), m_nCharSet(-1) {}
Lei Zhang2b1a2d52015-08-14 22:16:22 -070031 ~CFX_SystemHandler() override {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 public:
Lei Zhang2b1a2d52015-08-14 22:16:22 -070034 // IFX_SystemHandler
35 void InvalidateRect(FX_HWND hWnd, FX_RECT rect) override;
36 void OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) override;
37 FX_BOOL IsSelectionImplemented() override;
38 CFX_WideString GetClipboardText(FX_HWND hWnd) override { return L""; }
39 FX_BOOL SetClipboardText(FX_HWND hWnd, CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 return FALSE;
41 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070042 void ClientToScreen(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
43 void ScreenToClient(FX_HWND hWnd, int32_t& x, int32_t& y) override {}
44 void SetCursor(int32_t nCursorType) override;
45 FX_HMENU CreatePopupMenu() override { return NULL; }
46 FX_BOOL AppendMenuItem(FX_HMENU hMenu,
47 int32_t nIDNewItem,
48 CFX_WideString string) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 return FALSE;
50 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070051 FX_BOOL EnableMenuItem(FX_HMENU hMenu,
52 int32_t nIDItem,
53 FX_BOOL bEnabled) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 return FALSE;
55 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070056 int32_t TrackPopupMenu(FX_HMENU hMenu,
57 int32_t x,
58 int32_t y,
59 FX_HWND hParent) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 return -1;
61 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070062 void DestroyMenu(FX_HMENU hMenu) override {}
63 CFX_ByteString GetNativeTrueTypeFont(int32_t nCharset) override;
64 FX_BOOL FindNativeTrueTypeFont(int32_t nCharset,
65 CFX_ByteString sFontFaceName) override;
66 CPDF_Font* AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
67 CFX_ByteString sFontFaceName,
68 uint8_t nCharset) override;
69 int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) override;
70 void KillTimer(int32_t nID) override;
71 FX_BOOL IsSHIFTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 return m_pEnv->FFI_IsSHIFTKeyDown(nFlag);
73 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070074 FX_BOOL IsCTRLKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return m_pEnv->FFI_IsCTRLKeyDown(nFlag);
76 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070077 FX_BOOL IsALTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return m_pEnv->FFI_IsALTKeyDown(nFlag);
79 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070080 FX_BOOL IsINSERTKeyDown(FX_DWORD nFlag) override {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 return m_pEnv->FFI_IsINSERTKeyDown(nFlag);
82 }
Lei Zhang2b1a2d52015-08-14 22:16:22 -070083 FX_SYSTEMTIME GetLocalTime() override;
84 int32_t GetCharSet() override { return m_nCharSet; }
85 void SetCharSet(int32_t nCharSet) override { m_nCharSet = nCharSet; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086
87 private:
88 CPDFDoc_Environment* m_pEnv;
89 int m_nCharSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090};
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092void CFX_SystemHandler::SetCursor(int32_t nCursorType) {
93 m_pEnv->FFI_SetCursor(nCursorType);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
97 // g_pFormFillApp->FFI_Invalidate();
98 CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
99 CPDFXFA_Page* pPage = NULL;
100 CPDFSDK_PageView* pPageView = NULL;
101 pPageView = pSDKAnnot->GetPageView();
102 pPage = pSDKAnnot->GetPDFXFAPage();
103 if (!pPage || !pPageView)
104 return;
105 CPDF_Matrix page2device;
106 pPageView->GetCurrentMatrix(page2device);
107 CPDF_Matrix device2page;
108 device2page.SetReverse(page2device);
109 FX_FLOAT left, top, right, bottom;
110 device2page.Transform((FX_FLOAT)rect.left, (FX_FLOAT)rect.top, left, top);
111 device2page.Transform((FX_FLOAT)rect.right, (FX_FLOAT)rect.bottom, right,
112 bottom);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 CPDF_Rect rcPDF(left, bottom, right, top);
114 rcPDF.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 m_pEnv->FFI_Invalidate(pPage, rcPDF.left, rcPDF.top, rcPDF.right,
117 rcPDF.bottom);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119void CFX_SystemHandler::OutputSelectedRect(void* pFormFiller, CPDF_Rect& rect) {
120 CFFL_FormFiller* pFFL = (CFFL_FormFiller*)pFormFiller;
121 if (pFFL) {
122 CPDF_Point leftbottom = CPDF_Point(rect.left, rect.bottom);
123 CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
124 CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
125 CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
Tom Sepez468e5892015-10-13 15:49:36 -0700126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
128 ASSERT(pAnnot);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700129 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 ASSERT(pPage);
131 m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
132 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135FX_BOOL CFX_SystemHandler::IsSelectionImplemented() {
136 if (m_pEnv) {
137 FPDF_FORMFILLINFO* pInfo = m_pEnv->GetFormFillInfo();
138 if (pInfo && pInfo->FFI_OutputSelectedRect)
139 return TRUE;
140 }
141 return FALSE;
142}
143
144CFX_ByteString CFX_SystemHandler::GetNativeTrueTypeFont(int32_t nCharset) {
145 return "";
146}
147
148FX_BOOL CFX_SystemHandler::FindNativeTrueTypeFont(
149 int32_t nCharset,
150 CFX_ByteString sFontFaceName) {
151 CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
Lei Zhang122ee212015-10-21 17:07:24 -0700152 if (!pFontMgr)
153 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154
Lei Zhang122ee212015-10-21 17:07:24 -0700155 CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
156 if (!pFontMapper)
157 return FALSE;
158
159 int nSize = pFontMapper->m_InstalledTTFonts.GetSize();
160 if (nSize == 0) {
161 pFontMapper->LoadInstalledFonts();
162 nSize = pFontMapper->m_InstalledTTFonts.GetSize();
163 }
164
165 for (int i = 0; i < nSize; ++i) {
166 if (pFontMapper->m_InstalledTTFonts[i].Compare(sFontFaceName))
167 return TRUE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 }
169
170 return FALSE;
171}
172
173static int CharSet2CP(int charset) {
174 if (charset == 128)
175 return 932;
176 if (charset == 134)
177 return 936;
178 if (charset == 129)
179 return 949;
180 if (charset == 136)
181 return 950;
182 return 0;
183}
184CPDF_Font* CFX_SystemHandler::AddNativeTrueTypeFontToPDF(
185 CPDF_Document* pDoc,
186 CFX_ByteString sFontFaceName,
187 uint8_t nCharset) {
188 if (pDoc) {
189 CFX_Font* pFXFont = new CFX_Font();
190 pFXFont->LoadSubst(sFontFaceName, TRUE, 0, 0, 0, CharSet2CP(nCharset),
191 FALSE);
192 CPDF_Font* pFont = pDoc->AddFont(pFXFont, nCharset, FALSE);
193 delete pFXFont;
194 return pFont;
195 }
196
197 return NULL;
198}
199
200int32_t CFX_SystemHandler::SetTimer(int32_t uElapse,
201 TimerCallback lpTimerFunc) {
202 return m_pEnv->FFI_SetTimer(uElapse, lpTimerFunc);
203}
204void CFX_SystemHandler::KillTimer(int32_t nID) {
205 m_pEnv->FFI_KillTimer(nID);
206}
207
208FX_SYSTEMTIME CFX_SystemHandler::GetLocalTime() {
209 return m_pEnv->FFI_GetLocalTime();
210}
211
212CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
213 FPDF_FORMFILLINFO* pFFinfo)
Oliver Changde6088d2015-10-30 16:30:28 -0700214 : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pXFADoc(pDoc) {
215 m_pSysHandler.reset(new CFX_SystemHandler(this));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216}
217
218CPDFDoc_Environment::~CPDFDoc_Environment() {
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);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222}
223
224int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
225 const FX_WCHAR* Title,
226 FX_UINT Type,
227 FX_UINT Icon) {
228 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
229 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
230 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
231 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
232 FPDF_WIDESTRING pTitle =
233 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
234 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
235 pTitle, Type, Icon);
236 bsMsg.ReleaseBuffer();
237 bsTitle.ReleaseBuffer();
238 return ret;
239 }
240 return -1;
241}
242
243int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
244 const FX_WCHAR* Title,
245 const FX_WCHAR* Default,
246 const FX_WCHAR* cLabel,
247 FPDF_BOOL bPassword,
248 void* response,
249 int length) {
250 if (m_pInfo && m_pInfo->m_pJsPlatform &&
251 m_pInfo->m_pJsPlatform->app_response) {
252 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
253 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
254 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
255 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
256 FPDF_WIDESTRING pQuestion =
257 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
258 FPDF_WIDESTRING pTitle =
259 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
260 FPDF_WIDESTRING pDefault =
261 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
262 FPDF_WIDESTRING pLabel =
263 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
264 int ret = m_pInfo->m_pJsPlatform->app_response(
265 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
266 response, length);
267 bsQuestion.ReleaseBuffer();
268 bsTitle.ReleaseBuffer();
269 bsDefault.ReleaseBuffer();
270 bsLabel.ReleaseBuffer();
271 return ret;
272 }
273 return -1;
274}
275
276CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
277 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
278 !m_pInfo->m_pJsPlatform->Field_browse) {
279 return L"";
280 }
281
282 const int nRequiredLen =
283 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
284 if (nRequiredLen <= 0)
285 return L"";
286
287 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
288 memset(pBuff.get(), 0, nRequiredLen);
289 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
290 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
291 if (nActualLen <= 0 || nActualLen > nRequiredLen)
292 return L"";
293
294 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
295 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
296 return wsRet;
297}
298
299CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
300 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
301 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
302 return L"";
303 }
304
305 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
306 m_pInfo->m_pJsPlatform, nullptr, 0);
307 if (nRequiredLen <= 0)
308 return L"";
309
310 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
311 memset(pBuff.get(), 0, nRequiredLen);
312 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
313 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
314 if (nActualLen <= 0 || nActualLen > nRequiredLen)
315 return L"";
316
317 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
318 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
319 return wsRet;
320}
321
322void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
323 int length,
324 const FX_WCHAR* URL) {
325 if (m_pInfo && m_pInfo->m_pJsPlatform &&
326 m_pInfo->m_pJsPlatform->Doc_submitForm) {
327 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
328 FPDF_WIDESTRING pDestination =
329 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
330 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
331 length, pDestination);
332 bsDestination.ReleaseBuffer();
333 }
334}
335
336void CPDFDoc_Environment::JS_docmailForm(void* mailData,
337 int length,
338 FPDF_BOOL bUI,
339 const FX_WCHAR* To,
340 const FX_WCHAR* Subject,
341 const FX_WCHAR* CC,
342 const FX_WCHAR* BCC,
343 const FX_WCHAR* Msg) {
344 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
345 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
346 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
347 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
348 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
349 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
350 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
351 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
352 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
353 FPDF_WIDESTRING pSubject =
354 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
355 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
356 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
357 bUI, pTo, pSubject, pCC, pBcc, pMsg);
358 bsTo.ReleaseBuffer();
359 bsCC.ReleaseBuffer();
360 bsBcc.ReleaseBuffer();
361 bsSubject.ReleaseBuffer();
362 bsMsg.ReleaseBuffer();
363 }
364}
365
Tom Sepezba038bc2015-10-08 12:03:00 -0700366IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700368 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700370 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700371 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372}
373
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
375 if (!m_pAnnotHandlerMgr)
Oliver Chang24752492015-10-30 16:08:20 -0700376 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this));
377 return m_pAnnotHandlerMgr.get();
Bo Xufdc00a72014-10-28 23:03:33 -0700378}
379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
381 if (!m_pActionHandler)
Oliver Chang24752492015-10-30 16:08:20 -0700382 m_pActionHandler.reset(new CPDFSDK_ActionHandler());
383 return m_pActionHandler.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700384}
385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
387 if (!m_pIFormFiller)
Oliver Chang24752492015-10-30 16:08:20 -0700388 m_pIFormFiller.reset(new CFFL_IFormFiller(this));
389 return m_pIFormFiller.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700390}
391
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
393 CPDFDoc_Environment* pEnv)
394 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 m_pFocusAnnot(nullptr),
396 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700397 m_bChangeMask(FALSE),
398 m_bBeingDestroyed(FALSE) {
399}
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
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
413 FX_BOOL ReNew) {
414 auto it = m_pageMap.find(pPDFXFAPage);
415 if (it != m_pageMap.end())
416 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 if (!ReNew)
419 return nullptr;
420
421 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
422 m_pageMap[pPDFXFAPage] = pPageView;
423 // Delay to load all the annotations, to avoid endless loop.
424 pPageView->LoadFXAnnots();
425 return pPageView;
426}
427
428CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
429 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
430 return pPage ? GetPageView(pPage, TRUE) : nullptr;
431}
432
433CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
434 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
435 if (!pTempPage)
436 return nullptr;
437
438 auto it = m_pageMap.find(pTempPage);
439 return it->second;
440}
441
442void CPDFSDK_Document::ProcJavascriptFun() {
443 CPDFXFA_Document* pPDFDoc = GetDocument();
444 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
445 int iCount = docJS.CountJSActions();
446 if (iCount < 1)
447 return;
448 for (int i = 0; i < iCount; i++) {
449 CFX_ByteString csJSName;
450 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
451 if (m_pEnv->GetActionHander())
452 m_pEnv->GetActionHander()->DoAction_JavaScript(
453 jsAction, CFX_WideString::FromLocal(csJSName), this);
454 }
455}
456
457FX_BOOL CPDFSDK_Document::ProcOpenAction() {
458 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700459 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700460
461 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
462 if (!pRoot)
463 return FALSE;
464
465 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
466 if (!pOpenAction)
467 pOpenAction = pRoot->GetArray("OpenAction");
468
469 if (!pOpenAction)
470 return FALSE;
471
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400472 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 return TRUE;
474
Dan Sinclairf1251c12015-10-20 16:24:45 -0400475 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 CPDF_Action action(pDict);
477 if (m_pEnv->GetActionHander())
478 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
479 return TRUE;
480 }
481 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700482}
483
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
485 if (!m_pOccontent)
Oliver Chang24752492015-10-30 16:08:20 -0700486 m_pOccontent.reset(new CPDF_OCContext(m_pDoc->GetPDFDoc()));
487 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488}
Bo Xufdc00a72014-10-28 23:03:33 -0700489
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
491 auto it = m_pageMap.find(pPDFXFAPage);
492 if (it == m_pageMap.end())
493 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700494
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 CPDFSDK_PageView* pPageView = it->second;
496 if (pPageView->IsLocked())
497 return;
498
Oliver Chang972b78d2015-10-30 12:59:29 -0700499 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700500 delete pPageView;
501 m_pageMap.erase(it);
502}
503
504CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
505 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
506 if (!pTempPage)
507 return NULL;
508 return pTempPage;
509}
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
538 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
539
540 if (m_pFocusAnnot) {
541 if (!KillFocusAnnot(nFlag))
542 return FALSE;
543 }
544 CPDFSDK_PageView* pPageView = NULL;
545 if (pAnnot)
546 pPageView = pAnnot->GetPageView();
547 if (pAnnot && pPageView->IsValid()) {
548 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700549 if (!m_pFocusAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700550 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700551 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700552
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
554 return FALSE;
555 if (!m_pFocusAnnot) {
556 m_pFocusAnnot = pAnnot;
557 return TRUE;
558 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700559 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 }
561 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700562}
563
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700564FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
565 if (m_pFocusAnnot) {
566 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700567 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
568 m_pFocusAnnot = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700569
Lei Zhanga0601462015-10-30 14:54:37 -0700570 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
571 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700572
Lei Zhanga0601462015-10-30 14:54:37 -0700573 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
574 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
575 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
576 int nFieldType = pWidget->GetFieldType();
577 if (FIELDTYPE_TEXTFIELD == nFieldType ||
578 FIELDTYPE_COMBOBOX == nFieldType) {
579 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700580 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 }
Lei Zhanga0601462015-10-30 14:54:37 -0700582
583 if (!m_pFocusAnnot)
584 return TRUE;
585 } else {
586 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700587 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 }
589 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700590}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700591
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
593 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
594 return FALSE;
595 }
596
597 CPDFXFA_Page* pTempPage = NULL;
598 for (int i = nCount - 1; i >= 0; i--) {
599 pTempPage = GetPage(nStart + i);
600 if (pTempPage != NULL) {
601 ReMovePageView(pTempPage);
602 }
603 }
604 return TRUE;
605}
606
607void CPDFSDK_Document::OnCloseDocument() {
608 KillFocusAnnot();
609}
610
611FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
612 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
613 return dwPermissions & nFlag;
614}
615
Tom Sepezba038bc2015-10-08 12:03:00 -0700616IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700617 ASSERT(m_pEnv != NULL);
618 return m_pEnv->GetJSRuntime();
619}
620
621CFX_WideString CPDFSDK_Document::GetPath() {
622 ASSERT(m_pEnv != NULL);
623 return m_pEnv->JS_docGetFilePath();
624}
625
626CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
627 CPDFXFA_Page* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700628 : m_page(page),
629 m_pSDKDoc(pSDKDoc),
630 m_CaptureWidget(nullptr),
631 m_bEnterWidget(FALSE),
632 m_bExitWidget(FALSE),
633 m_bOnWidget(FALSE),
634 m_bValid(FALSE),
635 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700636 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
637 if (pInterForm) {
638 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
639 if (page->GetPDFPage())
640 pPDFInterForm->FixPageFields(page->GetPDFPage());
641 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700642}
643
644CPDFSDK_PageView::~CPDFSDK_PageView() {
645 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700646 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700647 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700648 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700649 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650
Lei Zhangbf60b292015-10-26 12:14:35 -0700651 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652}
653
654void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
655 CPDF_Matrix* pUser2Device,
656 CPDF_RenderOptions* pOptions,
657 FX_RECT* pClip) {
658 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700659 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
660 CPDFXFA_Page* pPage = GetPDFXFAPage();
661
662 if (pPage == NULL)
663 return;
664
Tom Sepeza8a39e22015-10-12 15:47:07 -0700665#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700666 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
667 CFX_Graphics gs;
668 gs.Create(pDevice);
669 if (pClip) {
670 CFX_RectF rectClip;
671 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
672 static_cast<FX_FLOAT>(pClip->top),
673 static_cast<FX_FLOAT>(pClip->Width()),
674 static_cast<FX_FLOAT>(pClip->Height()));
675 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700676 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700677 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
678 if (!pRenderContext)
679 return;
680 CXFA_RenderOptions renderOptions;
681 renderOptions.m_bHighlight = TRUE;
682 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
683 renderOptions);
684 pRenderContext->DoRender();
685 pRenderContext->StopRender();
686 pRenderContext->Release();
687 return;
688 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700689#endif // PDF_ENABLE_XFA
690
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700691 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700692 CPDFSDK_AnnotIterator annotIterator(this, true);
693 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700695 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696 }
697}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700698
Lei Zhang1b700c32015-10-30 23:55:35 -0700699const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
700 FX_FLOAT pageY) {
701 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700702 CFX_FloatRect annotRect;
703 pAnnot->GetRect(annotRect);
704 if (annotRect.Contains(pageX, pageY))
705 return pAnnot;
706 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700707 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708}
709
Lei Zhang1b700c32015-10-30 23:55:35 -0700710const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
711 FX_FLOAT pageY) {
712 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700713 if (pAnnot->GetSubType() == "Widget") {
714 CFX_FloatRect annotRect;
715 pAnnot->GetRect(annotRect);
716 if (annotRect.Contains(pageX, pageY))
717 return pAnnot;
718 }
719 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700720 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700721}
722
723CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
724 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700725 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
726 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700727 CPDFSDK_AnnotIterator annotIterator(this, false);
728 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
730 if (rc.Contains(pageX, pageY))
731 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700732 }
733
Lei Zhangbf60b292015-10-26 12:14:35 -0700734 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735}
736
737CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
738 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700739 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
740 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700741 CPDFSDK_AnnotIterator annotIterator(this, false);
742 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700743 if (pSDKAnnot->GetType() == "Widget" ||
744 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
745 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
746 CPDF_Point point(pageX, pageY);
747 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
748 return pSDKAnnot;
749 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700750 }
751
Lei Zhangbf60b292015-10-26 12:14:35 -0700752 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700753}
754
Oliver Chang972b78d2015-10-30 12:59:29 -0700755void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
756 // if there is a focused annot on the page, we should kill the focus first.
757 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
758 auto it =
759 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
760 if (it != m_fxAnnotArray.end())
761 KillFocusAnnot();
762 }
763}
764
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700765FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
766 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
767 if (pAnnotDic)
768 return pAnnotDic->KeyExist("AS");
769 return FALSE;
770}
771
772CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
773 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
774 ASSERT(pEnv);
775 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700776 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700777 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700778 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700779
Lei Zhangbf60b292015-10-26 12:14:35 -0700780 m_fxAnnotArray.push_back(pSDKAnnot);
781 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700782 return pSDKAnnot;
783}
784
785CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
786 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700787 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788
789 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
790 if (pSDKAnnot)
791 return pSDKAnnot;
792
793 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700795 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700797 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798
Lei Zhangbf60b292015-10-26 12:14:35 -0700799 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 return pSDKAnnot;
801}
802
803CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
804 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
805}
806
807CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
808 CPDF_Dictionary* pDict) {
809 return NULL;
810}
811
812FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
813 if (!pAnnot)
814 return FALSE;
815 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
816 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
817 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
818 return FALSE;
819
Lei Zhangbf60b292015-10-26 12:14:35 -0700820 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
821 if (it != m_fxAnnotArray.end())
822 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700824 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700825
826 return TRUE;
827}
828
829CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
830 if (m_page) {
831 return m_page->GetDocument()->GetPDFDoc();
832 }
833 return NULL;
834}
835
836CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
837 if (m_page) {
838 return m_page->GetPDFPage();
839 }
840
841 return NULL;
842}
843
Lei Zhang50218532015-10-30 14:03:43 -0700844size_t CPDFSDK_PageView::CountAnnots() const {
845 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700846}
847
Lei Zhangbf60b292015-10-26 12:14:35 -0700848CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
849 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700850}
851
852CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700853 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
854 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700855 return pAnnot;
856 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700857 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700858}
859CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700860 if (!hWidget)
861 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862
Lei Zhangbf60b292015-10-26 12:14:35 -0700863 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700864 if (pAnnot->GetXFAWidget() == hWidget)
865 return pAnnot;
866 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700867 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868}
869
870FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
871 FX_UINT nFlag) {
872 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
873 ASSERT(pEnv);
874 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
875 if (!pFXAnnot) {
876 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700877 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700878 }
Lei Zhanga0601462015-10-30 14:54:37 -0700879
880 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
881 FX_BOOL bRet =
882 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
883 if (bRet)
884 SetFocusAnnot(pFXAnnot);
885 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700886}
887
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700888FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
889 FX_UINT nFlag) {
890 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
891 ASSERT(pEnv);
892 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
893 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700894
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700895 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
896
897 if (pFXAnnot == NULL)
898 return FALSE;
899
900 FX_BOOL bRet =
901 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
902 if (bRet) {
903 SetFocusAnnot(pFXAnnot);
904 }
905 return TRUE;
906}
907
908FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
909 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
910 ASSERT(pEnv);
911 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700912
913 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
914
915 if (pFXAnnot == NULL)
916 return FALSE;
917
918 FX_BOOL bRet =
919 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
920 if (bRet) {
921 SetFocusAnnot(pFXAnnot);
922 }
923 return TRUE;
924}
925
926FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
927 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
928 ASSERT(pEnv);
929 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700930 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
931 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
932 FX_BOOL bRet = FALSE;
933 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
934 // Last focus Annot gets a chance to handle the event.
935 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
936 }
Lei Zhanga0601462015-10-30 14:54:37 -0700937 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700938 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700939 return bRet;
940}
941
942FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
943 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
944 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
946 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
947 m_bExitWidget = TRUE;
948 m_bEnterWidget = FALSE;
949 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
950 }
951 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
952 m_bOnWidget = TRUE;
953 if (!m_bEnterWidget) {
954 m_bEnterWidget = TRUE;
955 m_bExitWidget = FALSE;
956 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
957 }
958 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
959 return TRUE;
960 }
961 if (m_bOnWidget) {
962 m_bOnWidget = FALSE;
963 m_bExitWidget = TRUE;
964 m_bEnterWidget = FALSE;
965 if (m_CaptureWidget) {
966 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
967 m_CaptureWidget = NULL;
968 }
969 }
970 return FALSE;
971}
972
973FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
974 double deltaY,
975 const CPDF_Point& point,
976 int nFlag) {
977 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700978 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
979 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700980 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
981 (int)deltaY, point);
982 }
983 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700984}
985
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
987 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700988 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700989 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700990 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
991 }
Bo Xufdc00a72014-10-28 23:03:33 -0700992
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700993 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700994}
995
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
997 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -0700998 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001000 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1001 }
1002 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001003}
1004
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001007}
1008
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012
1013 SetLock(TRUE);
1014 m_page->AddRef();
1015 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1016 IXFA_PageView* pageView = NULL;
1017 pageView = m_page->GetXFAPageView();
1018 ASSERT(pageView != NULL);
1019
1020 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1021 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1022 XFA_WIDGETFILTER_Viewable |
1023 XFA_WIDGETFILTER_AllType);
1024 if (!pWidgetHander) {
1025 m_page->Release();
1026 SetLock(FALSE);
1027 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001028 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029
Lei Zhangbf60b292015-10-26 12:14:35 -07001030 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001032 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001033 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034
Lei Zhangbf60b292015-10-26 12:14:35 -07001035 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001036 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 }
1038
1039 pWidgetHander->Release();
1040 } else {
1041 CPDF_Page* pPage = m_page->GetPDFPage();
1042 ASSERT(pPage != NULL);
1043
1044 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1045 // Disable the default AP construction.
1046 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001047 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001048 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1049
Lei Zhang1b700c32015-10-30 23:55:35 -07001050 const size_t nCount = m_pAnnotList->Count();
1051 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001053 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054
1055 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1056 if (!pAnnot)
1057 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001058 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001059
1060 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1061 }
1062 }
1063 m_page->Release();
1064 SetLock(FALSE);
1065}
1066
1067void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1068 for (int i = 0; i < rects.GetSize(); i++) {
1069 CPDF_Rect rc = rects.GetAt(i);
1070 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1071 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1072 }
1073}
1074
1075void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1076 CPDF_Rect rcWindow = pAnnot->GetRect();
1077 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1078 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1079 rcWindow.bottom);
1080}
1081
1082int CPDFSDK_PageView::GetPageIndex() {
1083 if (m_page) {
1084 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1085 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1086 if (pDoc && pDic) {
1087 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1088 }
1089 }
1090 return -1;
1091}
1092
Lei Zhang1b700c32015-10-30 23:55:35 -07001093bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001094 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001095 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001096
Lei Zhang1b700c32015-10-30 23:55:35 -07001097 const auto& annots = m_pAnnotList->All();
1098 return std::find(annots.begin(), annots.end(), p) != annots.end();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001099}
1100
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001101CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1102 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1103 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001104 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105
Lei Zhangbf60b292015-10-26 12:14:35 -07001106 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001107 if (pAnnot == pFocusAnnot)
1108 return pAnnot;
1109 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001110 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001111}