blob: d7c0e141caab0614176d26a2eee0276d63d0ff1d [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 Chang24752492015-10-30 16:08:20 -0700214 : m_pInfo(pFFinfo),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 m_pSDKDoc(NULL),
216 m_pXFADoc(pDoc),
Oliver Chang24752492015-10-30 16:08:20 -0700217 m_pSysHandler(new CFX_SystemHandler(this)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218}
219
220CPDFDoc_Environment::~CPDFDoc_Environment() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700222 if (pProvider->m_pEnvList.GetSize() == 0)
223 pProvider->SetJavaScriptInitialized(FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224}
225
226int CPDFDoc_Environment::JS_appAlert(const FX_WCHAR* Msg,
227 const FX_WCHAR* Title,
228 FX_UINT Type,
229 FX_UINT Icon) {
230 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->app_alert) {
231 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
232 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
233 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
234 FPDF_WIDESTRING pTitle =
235 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
236 int ret = m_pInfo->m_pJsPlatform->app_alert(m_pInfo->m_pJsPlatform, pMsg,
237 pTitle, Type, Icon);
238 bsMsg.ReleaseBuffer();
239 bsTitle.ReleaseBuffer();
240 return ret;
241 }
242 return -1;
243}
244
245int CPDFDoc_Environment::JS_appResponse(const FX_WCHAR* Question,
246 const FX_WCHAR* Title,
247 const FX_WCHAR* Default,
248 const FX_WCHAR* cLabel,
249 FPDF_BOOL bPassword,
250 void* response,
251 int length) {
252 if (m_pInfo && m_pInfo->m_pJsPlatform &&
253 m_pInfo->m_pJsPlatform->app_response) {
254 CFX_ByteString bsQuestion = CFX_WideString(Question).UTF16LE_Encode();
255 CFX_ByteString bsTitle = CFX_WideString(Title).UTF16LE_Encode();
256 CFX_ByteString bsDefault = CFX_WideString(Default).UTF16LE_Encode();
257 CFX_ByteString bsLabel = CFX_WideString(cLabel).UTF16LE_Encode();
258 FPDF_WIDESTRING pQuestion =
259 (FPDF_WIDESTRING)bsQuestion.GetBuffer(bsQuestion.GetLength());
260 FPDF_WIDESTRING pTitle =
261 (FPDF_WIDESTRING)bsTitle.GetBuffer(bsTitle.GetLength());
262 FPDF_WIDESTRING pDefault =
263 (FPDF_WIDESTRING)bsDefault.GetBuffer(bsDefault.GetLength());
264 FPDF_WIDESTRING pLabel =
265 (FPDF_WIDESTRING)bsLabel.GetBuffer(bsLabel.GetLength());
266 int ret = m_pInfo->m_pJsPlatform->app_response(
267 m_pInfo->m_pJsPlatform, pQuestion, pTitle, pDefault, pLabel, bPassword,
268 response, length);
269 bsQuestion.ReleaseBuffer();
270 bsTitle.ReleaseBuffer();
271 bsDefault.ReleaseBuffer();
272 bsLabel.ReleaseBuffer();
273 return ret;
274 }
275 return -1;
276}
277
278CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() {
279 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
280 !m_pInfo->m_pJsPlatform->Field_browse) {
281 return L"";
282 }
283
284 const int nRequiredLen =
285 m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, nullptr, 0);
286 if (nRequiredLen <= 0)
287 return L"";
288
289 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
290 memset(pBuff.get(), 0, nRequiredLen);
291 const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
292 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
293 if (nActualLen <= 0 || nActualLen > nRequiredLen)
294 return L"";
295
296 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
297 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
298 return wsRet;
299}
300
301CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() {
302 if (!m_pInfo || !m_pInfo->m_pJsPlatform ||
303 !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
304 return L"";
305 }
306
307 const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
308 m_pInfo->m_pJsPlatform, nullptr, 0);
309 if (nRequiredLen <= 0)
310 return L"";
311
312 nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
313 memset(pBuff.get(), 0, nRequiredLen);
314 const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
315 m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
316 if (nActualLen <= 0 || nActualLen > nRequiredLen)
317 return L"";
318
319 CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
320 CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
321 return wsRet;
322}
323
324void CPDFDoc_Environment::JS_docSubmitForm(void* formData,
325 int length,
326 const FX_WCHAR* URL) {
327 if (m_pInfo && m_pInfo->m_pJsPlatform &&
328 m_pInfo->m_pJsPlatform->Doc_submitForm) {
329 CFX_ByteString bsDestination = CFX_WideString(URL).UTF16LE_Encode();
330 FPDF_WIDESTRING pDestination =
331 (FPDF_WIDESTRING)bsDestination.GetBuffer(bsDestination.GetLength());
332 m_pInfo->m_pJsPlatform->Doc_submitForm(m_pInfo->m_pJsPlatform, formData,
333 length, pDestination);
334 bsDestination.ReleaseBuffer();
335 }
336}
337
338void CPDFDoc_Environment::JS_docmailForm(void* mailData,
339 int length,
340 FPDF_BOOL bUI,
341 const FX_WCHAR* To,
342 const FX_WCHAR* Subject,
343 const FX_WCHAR* CC,
344 const FX_WCHAR* BCC,
345 const FX_WCHAR* Msg) {
346 if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_mail) {
347 CFX_ByteString bsTo = CFX_WideString(To).UTF16LE_Encode();
348 CFX_ByteString bsCC = CFX_WideString(Subject).UTF16LE_Encode();
349 CFX_ByteString bsBcc = CFX_WideString(BCC).UTF16LE_Encode();
350 CFX_ByteString bsSubject = CFX_WideString(Subject).UTF16LE_Encode();
351 CFX_ByteString bsMsg = CFX_WideString(Msg).UTF16LE_Encode();
352 FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
353 FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
354 FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
355 FPDF_WIDESTRING pSubject =
356 (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
357 FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
358 m_pInfo->m_pJsPlatform->Doc_mail(m_pInfo->m_pJsPlatform, mailData, length,
359 bUI, pTo, pSubject, pCC, pBcc, pMsg);
360 bsTo.ReleaseBuffer();
361 bsCC.ReleaseBuffer();
362 bsBcc.ReleaseBuffer();
363 bsSubject.ReleaseBuffer();
364 bsMsg.ReleaseBuffer();
365 }
366}
367
Tom Sepezba038bc2015-10-08 12:03:00 -0700368IJS_Runtime* CPDFDoc_Environment::GetJSRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 if (!IsJSInitiated())
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700370 return NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 if (!m_pJSRuntime)
Tom Sepezba038bc2015-10-08 12:03:00 -0700372 m_pJSRuntime.reset(IJS_Runtime::Create(this));
Tom Sepez854a7f62015-09-21 17:07:49 -0700373 return m_pJSRuntime.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374}
375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376CPDFSDK_AnnotHandlerMgr* CPDFDoc_Environment::GetAnnotHandlerMgr() {
377 if (!m_pAnnotHandlerMgr)
Oliver Chang24752492015-10-30 16:08:20 -0700378 m_pAnnotHandlerMgr.reset(new CPDFSDK_AnnotHandlerMgr(this));
379 return m_pAnnotHandlerMgr.get();
Bo Xufdc00a72014-10-28 23:03:33 -0700380}
381
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382CPDFSDK_ActionHandler* CPDFDoc_Environment::GetActionHander() {
383 if (!m_pActionHandler)
Oliver Chang24752492015-10-30 16:08:20 -0700384 m_pActionHandler.reset(new CPDFSDK_ActionHandler());
385 return m_pActionHandler.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386}
387
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700388CFFL_IFormFiller* CPDFDoc_Environment::GetIFormFiller() {
389 if (!m_pIFormFiller)
Oliver Chang24752492015-10-30 16:08:20 -0700390 m_pIFormFiller.reset(new CFFL_IFormFiller(this));
391 return m_pIFormFiller.get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392}
393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
395 CPDFDoc_Environment* pEnv)
396 : m_pDoc(pDoc),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 m_pFocusAnnot(nullptr),
398 m_pEnv(pEnv),
Oliver Chang972b78d2015-10-30 12:59:29 -0700399 m_bChangeMask(FALSE),
400 m_bBeingDestroyed(FALSE) {
401}
Bo Xufdc00a72014-10-28 23:03:33 -0700402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403CPDFSDK_Document::~CPDFSDK_Document() {
Oliver Chang972b78d2015-10-30 12:59:29 -0700404 m_bBeingDestroyed = TRUE;
405
406 for (auto& it : m_pageMap)
407 it.second->KillFocusAnnotIfNeeded();
408
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 for (auto& it : m_pageMap)
410 delete it.second;
411 m_pageMap.clear();
Bo Xufdc00a72014-10-28 23:03:33 -0700412}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700413
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700414CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
415 FX_BOOL ReNew) {
416 auto it = m_pageMap.find(pPDFXFAPage);
417 if (it != m_pageMap.end())
418 return it->second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700419
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 if (!ReNew)
421 return nullptr;
422
423 CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
424 m_pageMap[pPDFXFAPage] = pPageView;
425 // Delay to load all the annotations, to avoid endless loop.
426 pPageView->LoadFXAnnots();
427 return pPageView;
428}
429
430CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
431 CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
432 return pPage ? GetPageView(pPage, TRUE) : nullptr;
433}
434
435CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
436 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
437 if (!pTempPage)
438 return nullptr;
439
440 auto it = m_pageMap.find(pTempPage);
441 return it->second;
442}
443
444void CPDFSDK_Document::ProcJavascriptFun() {
445 CPDFXFA_Document* pPDFDoc = GetDocument();
446 CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
447 int iCount = docJS.CountJSActions();
448 if (iCount < 1)
449 return;
450 for (int i = 0; i < iCount; i++) {
451 CFX_ByteString csJSName;
452 CPDF_Action jsAction = docJS.GetJSAction(i, csJSName);
453 if (m_pEnv->GetActionHander())
454 m_pEnv->GetActionHander()->DoAction_JavaScript(
455 jsAction, CFX_WideString::FromLocal(csJSName), this);
456 }
457}
458
459FX_BOOL CPDFSDK_Document::ProcOpenAction() {
460 if (!m_pDoc)
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700461 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700462
463 CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
464 if (!pRoot)
465 return FALSE;
466
467 CPDF_Object* pOpenAction = pRoot->GetDict("OpenAction");
468 if (!pOpenAction)
469 pOpenAction = pRoot->GetArray("OpenAction");
470
471 if (!pOpenAction)
472 return FALSE;
473
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400474 if (pOpenAction->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700475 return TRUE;
476
Dan Sinclairf1251c12015-10-20 16:24:45 -0400477 if (CPDF_Dictionary* pDict = pOpenAction->AsDictionary()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 CPDF_Action action(pDict);
479 if (m_pEnv->GetActionHander())
480 m_pEnv->GetActionHander()->DoAction_DocOpen(action, this);
481 return TRUE;
482 }
483 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484}
485
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700486CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
487 if (!m_pOccontent)
Oliver Chang24752492015-10-30 16:08:20 -0700488 m_pOccontent.reset(new CPDF_OCContext(m_pDoc->GetPDFDoc()));
489 return m_pOccontent.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490}
Bo Xufdc00a72014-10-28 23:03:33 -0700491
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700492void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
493 auto it = m_pageMap.find(pPDFXFAPage);
494 if (it == m_pageMap.end())
495 return;
Bo Xufdc00a72014-10-28 23:03:33 -0700496
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700497 CPDFSDK_PageView* pPageView = it->second;
498 if (pPageView->IsLocked())
499 return;
500
Oliver Chang972b78d2015-10-30 12:59:29 -0700501 pPageView->KillFocusAnnotIfNeeded();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 delete pPageView;
503 m_pageMap.erase(it);
504}
505
506CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
507 CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
508 if (!pTempPage)
509 return NULL;
510 return pTempPage;
511}
512
513CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
514 if (!m_pInterForm)
Oliver Chang24752492015-10-30 16:08:20 -0700515 m_pInterForm.reset(new CPDFSDK_InterForm(this));
516 return m_pInterForm.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700517}
518
519void CPDFSDK_Document::UpdateAllViews(CPDFSDK_PageView* pSender,
520 CPDFSDK_Annot* pAnnot) {
521 for (const auto& it : m_pageMap) {
522 CPDFSDK_PageView* pPageView = it.second;
523 if (pPageView != pSender) {
524 pPageView->UpdateView(pAnnot);
525 }
526 }
527}
528
529CPDFSDK_Annot* CPDFSDK_Document::GetFocusAnnot() {
530 return m_pFocusAnnot;
531}
532
533FX_BOOL CPDFSDK_Document::SetFocusAnnot(CPDFSDK_Annot* pAnnot, FX_UINT nFlag) {
Oliver Chang972b78d2015-10-30 12:59:29 -0700534 if (m_bBeingDestroyed)
535 return FALSE;
536
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700537 if (m_pFocusAnnot == pAnnot)
538 return TRUE;
539
540 CPDFSDK_Annot* pLastFocusAnnot = m_pFocusAnnot;
541
542 if (m_pFocusAnnot) {
543 if (!KillFocusAnnot(nFlag))
544 return FALSE;
545 }
546 CPDFSDK_PageView* pPageView = NULL;
547 if (pAnnot)
548 pPageView = pAnnot->GetPageView();
549 if (pAnnot && pPageView->IsValid()) {
550 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700551 if (!m_pFocusAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700552 if (!pAnnotHandler->Annot_OnChangeFocus(pAnnot, pLastFocusAnnot))
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700553 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700554
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700555 if (!pAnnotHandler->Annot_OnSetFocus(pAnnot, nFlag))
556 return FALSE;
557 if (!m_pFocusAnnot) {
558 m_pFocusAnnot = pAnnot;
559 return TRUE;
560 }
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700561 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700562 }
563 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700564}
565
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566FX_BOOL CPDFSDK_Document::KillFocusAnnot(FX_UINT nFlag) {
567 if (m_pFocusAnnot) {
568 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700569 CPDFSDK_Annot* pFocusAnnot = m_pFocusAnnot;
570 m_pFocusAnnot = nullptr;
Bo Xufdc00a72014-10-28 23:03:33 -0700571
Lei Zhanga0601462015-10-30 14:54:37 -0700572 if (!pAnnotHandler->Annot_OnChangeFocus(nullptr, pFocusAnnot))
573 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700574
Lei Zhanga0601462015-10-30 14:54:37 -0700575 if (pAnnotHandler->Annot_OnKillFocus(pFocusAnnot, nFlag)) {
576 if (pFocusAnnot->GetType() == FX_BSTRC("Widget")) {
577 CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pFocusAnnot;
578 int nFieldType = pWidget->GetFieldType();
579 if (FIELDTYPE_TEXTFIELD == nFieldType ||
580 FIELDTYPE_COMBOBOX == nFieldType) {
581 m_pEnv->FFI_OnSetFieldInputFocus(NULL, NULL, 0, FALSE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700582 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700583 }
Lei Zhanga0601462015-10-30 14:54:37 -0700584
585 if (!m_pFocusAnnot)
586 return TRUE;
587 } else {
588 m_pFocusAnnot = pFocusAnnot;
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700589 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590 }
591 return FALSE;
Bo Xufdc00a72014-10-28 23:03:33 -0700592}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700593
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
595 if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
596 return FALSE;
597 }
598
599 CPDFXFA_Page* pTempPage = NULL;
600 for (int i = nCount - 1; i >= 0; i--) {
601 pTempPage = GetPage(nStart + i);
602 if (pTempPage != NULL) {
603 ReMovePageView(pTempPage);
604 }
605 }
606 return TRUE;
607}
608
609void CPDFSDK_Document::OnCloseDocument() {
610 KillFocusAnnot();
611}
612
613FX_BOOL CPDFSDK_Document::GetPermissions(int nFlag) {
614 FX_DWORD dwPermissions = m_pDoc->GetPDFDoc()->GetUserPermissions();
615 return dwPermissions & nFlag;
616}
617
Tom Sepezba038bc2015-10-08 12:03:00 -0700618IJS_Runtime* CPDFSDK_Document::GetJsRuntime() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 ASSERT(m_pEnv != NULL);
620 return m_pEnv->GetJSRuntime();
621}
622
623CFX_WideString CPDFSDK_Document::GetPath() {
624 ASSERT(m_pEnv != NULL);
625 return m_pEnv->JS_docGetFilePath();
626}
627
628CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
629 CPDFXFA_Page* page)
Lei Zhangbf60b292015-10-26 12:14:35 -0700630 : m_page(page),
631 m_pSDKDoc(pSDKDoc),
632 m_CaptureWidget(nullptr),
633 m_bEnterWidget(FALSE),
634 m_bExitWidget(FALSE),
635 m_bOnWidget(FALSE),
636 m_bValid(FALSE),
637 m_bLocked(FALSE) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700638 CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
639 if (pInterForm) {
640 CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
641 if (page->GetPDFPage())
642 pPDFInterForm->FixPageFields(page->GetPDFPage());
643 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700644}
645
646CPDFSDK_PageView::~CPDFSDK_PageView() {
647 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepez396e8722015-09-09 10:16:08 -0700648 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700649 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650 pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
Lei Zhangbf60b292015-10-26 12:14:35 -0700651 m_fxAnnotArray.clear();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700652
Lei Zhangbf60b292015-10-26 12:14:35 -0700653 m_pAnnotList.reset();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654}
655
656void CPDFSDK_PageView::PageView_OnDraw(CFX_RenderDevice* pDevice,
657 CPDF_Matrix* pUser2Device,
658 CPDF_RenderOptions* pOptions,
659 FX_RECT* pClip) {
660 m_curMatrix = *pUser2Device;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700661 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
662 CPDFXFA_Page* pPage = GetPDFXFAPage();
663
664 if (pPage == NULL)
665 return;
666
Tom Sepeza8a39e22015-10-12 15:47:07 -0700667#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 if (pPage->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
669 CFX_Graphics gs;
670 gs.Create(pDevice);
671 if (pClip) {
672 CFX_RectF rectClip;
673 rectClip.Set(static_cast<FX_FLOAT>(pClip->left),
674 static_cast<FX_FLOAT>(pClip->top),
675 static_cast<FX_FLOAT>(pClip->Width()),
676 static_cast<FX_FLOAT>(pClip->Height()));
677 gs.SetClipRect(rectClip);
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700678 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700679 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
680 if (!pRenderContext)
681 return;
682 CXFA_RenderOptions renderOptions;
683 renderOptions.m_bHighlight = TRUE;
684 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
685 renderOptions);
686 pRenderContext->DoRender();
687 pRenderContext->StopRender();
688 pRenderContext->Release();
689 return;
690 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700691#endif // PDF_ENABLE_XFA
692
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700694 CPDFSDK_AnnotIterator annotIterator(this, true);
695 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700696 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698 }
699}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700700
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700701CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
702 FX_FLOAT pageY) {
Lei Zhang50218532015-10-30 14:03:43 -0700703 const int nCount = m_pAnnotList->Count();
704 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700705 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
706 CFX_FloatRect annotRect;
707 pAnnot->GetRect(annotRect);
708 if (annotRect.Contains(pageX, pageY))
709 return pAnnot;
710 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700711 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700712}
713
714CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
715 FX_FLOAT pageY) {
Lei Zhang50218532015-10-30 14:03:43 -0700716 const int nCount = m_pAnnotList->Count();
Lei Zhangbf60b292015-10-26 12:14:35 -0700717 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700718 CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
719 if (pAnnot->GetSubType() == "Widget") {
720 CFX_FloatRect annotRect;
721 pAnnot->GetRect(annotRect);
722 if (annotRect.Contains(pageX, pageY))
723 return pAnnot;
724 }
725 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700726 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727}
728
729CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
730 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700731 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
732 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700733 CPDFSDK_AnnotIterator annotIterator(this, false);
734 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700735 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
736 if (rc.Contains(pageX, pageY))
737 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700738 }
739
Lei Zhangbf60b292015-10-26 12:14:35 -0700740 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741}
742
743CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
744 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700745 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
746 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700747 CPDFSDK_AnnotIterator annotIterator(this, false);
748 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700749 if (pSDKAnnot->GetType() == "Widget" ||
750 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
751 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
752 CPDF_Point point(pageX, pageY);
753 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
754 return pSDKAnnot;
755 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700756 }
757
Lei Zhangbf60b292015-10-26 12:14:35 -0700758 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700759}
760
Oliver Chang972b78d2015-10-30 12:59:29 -0700761void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
762 // if there is a focused annot on the page, we should kill the focus first.
763 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
764 auto it =
765 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
766 if (it != m_fxAnnotArray.end())
767 KillFocusAnnot();
768 }
769}
770
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700771FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
772 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
773 if (pAnnotDic)
774 return pAnnotDic->KeyExist("AS");
775 return FALSE;
776}
777
778CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
779 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
780 ASSERT(pEnv);
781 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700782 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700783 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700784 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700785
Lei Zhangbf60b292015-10-26 12:14:35 -0700786 m_fxAnnotArray.push_back(pSDKAnnot);
787 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700788 return pSDKAnnot;
789}
790
791CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
792 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700793 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794
795 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
796 if (pSDKAnnot)
797 return pSDKAnnot;
798
799 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700800 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700801 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700802 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700803 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700804
Lei Zhangbf60b292015-10-26 12:14:35 -0700805 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700806 return pSDKAnnot;
807}
808
809CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
810 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
811}
812
813CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
814 CPDF_Dictionary* pDict) {
815 return NULL;
816}
817
818FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
819 if (!pAnnot)
820 return FALSE;
821 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
822 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
823 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
824 return FALSE;
825
Lei Zhangbf60b292015-10-26 12:14:35 -0700826 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
827 if (it != m_fxAnnotArray.end())
828 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700829 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700830 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700831
832 return TRUE;
833}
834
835CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
836 if (m_page) {
837 return m_page->GetDocument()->GetPDFDoc();
838 }
839 return NULL;
840}
841
842CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
843 if (m_page) {
844 return m_page->GetPDFPage();
845 }
846
847 return NULL;
848}
849
Lei Zhang50218532015-10-30 14:03:43 -0700850size_t CPDFSDK_PageView::CountAnnots() const {
851 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700852}
853
Lei Zhangbf60b292015-10-26 12:14:35 -0700854CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
855 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856}
857
858CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700859 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
860 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700861 return pAnnot;
862 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700863 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700864}
865CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700866 if (!hWidget)
867 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700868
Lei Zhangbf60b292015-10-26 12:14:35 -0700869 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700870 if (pAnnot->GetXFAWidget() == hWidget)
871 return pAnnot;
872 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700873 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700874}
875
876FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
877 FX_UINT nFlag) {
878 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
879 ASSERT(pEnv);
880 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
881 if (!pFXAnnot) {
882 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700883 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700884 }
Lei Zhanga0601462015-10-30 14:54:37 -0700885
886 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
887 FX_BOOL bRet =
888 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
889 if (bRet)
890 SetFocusAnnot(pFXAnnot);
891 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700892}
893
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700894FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
895 FX_UINT nFlag) {
896 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
897 ASSERT(pEnv);
898 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
899 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700900
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700901 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
902
903 if (pFXAnnot == NULL)
904 return FALSE;
905
906 FX_BOOL bRet =
907 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
908 if (bRet) {
909 SetFocusAnnot(pFXAnnot);
910 }
911 return TRUE;
912}
913
914FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
915 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
916 ASSERT(pEnv);
917 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700918
919 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
920
921 if (pFXAnnot == NULL)
922 return FALSE;
923
924 FX_BOOL bRet =
925 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
926 if (bRet) {
927 SetFocusAnnot(pFXAnnot);
928 }
929 return TRUE;
930}
931
932FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
933 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
934 ASSERT(pEnv);
935 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
937 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
938 FX_BOOL bRet = FALSE;
939 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
940 // Last focus Annot gets a chance to handle the event.
941 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
942 }
Lei Zhanga0601462015-10-30 14:54:37 -0700943 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700944 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700945 return bRet;
946}
947
948FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
949 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
950 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700951 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
952 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
953 m_bExitWidget = TRUE;
954 m_bEnterWidget = FALSE;
955 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
956 }
957 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
958 m_bOnWidget = TRUE;
959 if (!m_bEnterWidget) {
960 m_bEnterWidget = TRUE;
961 m_bExitWidget = FALSE;
962 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
963 }
964 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
965 return TRUE;
966 }
967 if (m_bOnWidget) {
968 m_bOnWidget = FALSE;
969 m_bExitWidget = TRUE;
970 m_bEnterWidget = FALSE;
971 if (m_CaptureWidget) {
972 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
973 m_CaptureWidget = NULL;
974 }
975 }
976 return FALSE;
977}
978
979FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
980 double deltaY,
981 const CPDF_Point& point,
982 int nFlag) {
983 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700984 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
985 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700986 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
987 (int)deltaY, point);
988 }
989 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700990}
991
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700992FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
993 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700994 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700995 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700996 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
997 }
Bo Xufdc00a72014-10-28 23:03:33 -0700998
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700999 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001000}
1001
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001002FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
1003 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -07001004 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001005 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001006 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
1007 }
1008 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001009}
1010
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001011FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001012 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001013}
1014
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001015void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001016 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001017 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001018
1019 SetLock(TRUE);
1020 m_page->AddRef();
1021 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1022 IXFA_PageView* pageView = NULL;
1023 pageView = m_page->GetXFAPageView();
1024 ASSERT(pageView != NULL);
1025
1026 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1027 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1028 XFA_WIDGETFILTER_Viewable |
1029 XFA_WIDGETFILTER_AllType);
1030 if (!pWidgetHander) {
1031 m_page->Release();
1032 SetLock(FALSE);
1033 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001034 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035
Lei Zhangbf60b292015-10-26 12:14:35 -07001036 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001037 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001038 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001039 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001040
Lei Zhangbf60b292015-10-26 12:14:35 -07001041 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001042 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001043 }
1044
1045 pWidgetHander->Release();
1046 } else {
1047 CPDF_Page* pPage = m_page->GetPDFPage();
1048 ASSERT(pPage != NULL);
1049
1050 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1051 // Disable the default AP construction.
1052 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001053 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001054 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1055
Lei Zhang50218532015-10-30 14:03:43 -07001056 const int nCount = m_pAnnotList->Count();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057 for (int i = 0; i < nCount; i++) {
1058 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001059 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001060
1061 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1062 if (!pAnnot)
1063 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001064 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001065
1066 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1067 }
1068 }
1069 m_page->Release();
1070 SetLock(FALSE);
1071}
1072
1073void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1074 for (int i = 0; i < rects.GetSize(); i++) {
1075 CPDF_Rect rc = rects.GetAt(i);
1076 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1077 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1078 }
1079}
1080
1081void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1082 CPDF_Rect rcWindow = pAnnot->GetRect();
1083 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1084 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1085 rcWindow.bottom);
1086}
1087
1088int CPDFSDK_PageView::GetPageIndex() {
1089 if (m_page) {
1090 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1091 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1092 if (pDoc && pDic) {
1093 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1094 }
1095 }
1096 return -1;
1097}
1098
Lei Zhangbf60b292015-10-26 12:14:35 -07001099FX_BOOL CPDFSDK_PageView::IsValidAnnot(CPDF_Annot* p) const {
1100 if (!p)
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001101 return FALSE;
Lei Zhangbf60b292015-10-26 12:14:35 -07001102
Lei Zhang50218532015-10-30 14:03:43 -07001103 const int nCount = m_pAnnotList->Count();
Lei Zhangbf60b292015-10-26 12:14:35 -07001104 for (int i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 if (m_pAnnotList->GetAt(i) == p)
1106 return TRUE;
1107 }
1108 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001109}
1110
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001111CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1112 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1113 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001114 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001115
Lei Zhangbf60b292015-10-26 12:14:35 -07001116 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001117 if (pAnnot == pFocusAnnot)
1118 return pAnnot;
1119 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001120 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001121}