blob: 1b09714dfafd1c62b1ad684df0b98d2b3ee119cf [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,
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800657 const FX_RECT& pClip) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700658 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);
Lei Zhangf0e2e1b2015-11-02 13:27:54 -0800669 CFX_RectF rectClip;
670 rectClip.Set(static_cast<FX_FLOAT>(pClip.left),
671 static_cast<FX_FLOAT>(pClip.top),
672 static_cast<FX_FLOAT>(pClip.Width()),
673 static_cast<FX_FLOAT>(pClip.Height()));
674 gs.SetClipRect(rectClip);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675 IXFA_RenderContext* pRenderContext = XFA_RenderContext_Create();
676 if (!pRenderContext)
677 return;
678 CXFA_RenderOptions renderOptions;
679 renderOptions.m_bHighlight = TRUE;
680 pRenderContext->StartRender(pPage->GetXFAPageView(), &gs, *pUser2Device,
681 renderOptions);
682 pRenderContext->DoRender();
683 pRenderContext->StopRender();
684 pRenderContext->Release();
685 return;
686 }
Tom Sepeza8a39e22015-10-12 15:47:07 -0700687#endif // PDF_ENABLE_XFA
688
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700689 // for pdf/static xfa.
Lei Zhangbf60b292015-10-26 12:14:35 -0700690 CPDFSDK_AnnotIterator annotIterator(this, true);
691 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700692 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700694 }
695}
Tom Sepezdfbf8e72015-10-14 14:17:26 -0700696
Lei Zhang1b700c32015-10-30 23:55:35 -0700697const CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
698 FX_FLOAT pageY) {
699 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700 CFX_FloatRect annotRect;
701 pAnnot->GetRect(annotRect);
702 if (annotRect.Contains(pageX, pageY))
703 return pAnnot;
704 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700705 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700706}
707
Lei Zhang1b700c32015-10-30 23:55:35 -0700708const CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
709 FX_FLOAT pageY) {
710 for (const CPDF_Annot* pAnnot : m_pAnnotList->All()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 if (pAnnot->GetSubType() == "Widget") {
712 CFX_FloatRect annotRect;
713 pAnnot->GetRect(annotRect);
714 if (annotRect.Contains(pageX, pageY))
715 return pAnnot;
716 }
717 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700718 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700719}
720
721CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
722 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700723 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
724 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700725 CPDFSDK_AnnotIterator annotIterator(this, false);
726 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
728 if (rc.Contains(pageX, pageY))
729 return pSDKAnnot;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700730 }
731
Lei Zhangbf60b292015-10-26 12:14:35 -0700732 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700733}
734
735CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
736 FX_FLOAT pageY) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700737 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
738 CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700739 CPDFSDK_AnnotIterator annotIterator(this, false);
740 while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700741 if (pSDKAnnot->GetType() == "Widget" ||
742 pSDKAnnot->GetType() == FSDK_XFAWIDGET_TYPENAME) {
743 pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
744 CPDF_Point point(pageX, pageY);
745 if (pAnnotMgr->Annot_OnHitTest(this, pSDKAnnot, point))
746 return pSDKAnnot;
747 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700748 }
749
Lei Zhangbf60b292015-10-26 12:14:35 -0700750 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700751}
752
Oliver Chang972b78d2015-10-30 12:59:29 -0700753void CPDFSDK_PageView::KillFocusAnnotIfNeeded() {
754 // if there is a focused annot on the page, we should kill the focus first.
755 if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
756 auto it =
757 std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
758 if (it != m_fxAnnotArray.end())
759 KillFocusAnnot();
760 }
761}
762
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700763FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
764 CPDF_Dictionary* pAnnotDic = pAnnot->GetAnnotDict();
765 if (pAnnotDic)
766 return pAnnotDic->KeyExist("AS");
767 return FALSE;
768}
769
770CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Annot* pPDFAnnot) {
771 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
772 ASSERT(pEnv);
773 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhangbf60b292015-10-26 12:14:35 -0700774 CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700775 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700776 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700777
Lei Zhangbf60b292015-10-26 12:14:35 -0700778 m_fxAnnotArray.push_back(pSDKAnnot);
779 pAnnotHandler->Annot_OnCreate(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700780 return pSDKAnnot;
781}
782
783CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(IXFA_Widget* pPDFAnnot) {
784 if (!pPDFAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700785 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700786
787 CPDFSDK_Annot* pSDKAnnot = GetAnnotByXFAWidget(pPDFAnnot);
788 if (pSDKAnnot)
789 return pSDKAnnot;
790
791 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700792 CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
Lei Zhanga0601462015-10-30 14:54:37 -0700793 pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700794 if (!pSDKAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700795 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700796
Lei Zhangbf60b292015-10-26 12:14:35 -0700797 m_fxAnnotArray.push_back(pSDKAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700798 return pSDKAnnot;
799}
800
801CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(CPDF_Dictionary* pDict) {
802 return pDict ? AddAnnot(pDict->GetString("Subtype"), pDict) : nullptr;
803}
804
805CPDFSDK_Annot* CPDFSDK_PageView::AddAnnot(const FX_CHAR* lpSubType,
806 CPDF_Dictionary* pDict) {
807 return NULL;
808}
809
810FX_BOOL CPDFSDK_PageView::DeleteAnnot(CPDFSDK_Annot* pAnnot) {
811 if (!pAnnot)
812 return FALSE;
813 CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
814 if (!pPage || (pPage->GetDocument()->GetDocType() != DOCTYPE_STATIC_XFA &&
815 pPage->GetDocument()->GetDocType() != DOCTYPE_DYNIMIC_XFA))
816 return FALSE;
817
Lei Zhangbf60b292015-10-26 12:14:35 -0700818 auto it = std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), pAnnot);
819 if (it != m_fxAnnotArray.end())
820 m_fxAnnotArray.erase(it);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700821 if (m_CaptureWidget == pAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -0700822 m_CaptureWidget = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700823
824 return TRUE;
825}
826
827CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
828 if (m_page) {
829 return m_page->GetDocument()->GetPDFDoc();
830 }
831 return NULL;
832}
833
834CPDF_Page* CPDFSDK_PageView::GetPDFPage() {
835 if (m_page) {
836 return m_page->GetPDFPage();
837 }
838
839 return NULL;
840}
841
Lei Zhang50218532015-10-30 14:03:43 -0700842size_t CPDFSDK_PageView::CountAnnots() const {
843 return m_fxAnnotArray.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700844}
845
Lei Zhangbf60b292015-10-26 12:14:35 -0700846CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
847 return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700848}
849
850CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700851 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
852 if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700853 return pAnnot;
854 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700855 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700856}
857CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByXFAWidget(IXFA_Widget* hWidget) {
Lei Zhangbf60b292015-10-26 12:14:35 -0700858 if (!hWidget)
859 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700860
Lei Zhangbf60b292015-10-26 12:14:35 -0700861 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700862 if (pAnnot->GetXFAWidget() == hWidget)
863 return pAnnot;
864 }
Lei Zhangbf60b292015-10-26 12:14:35 -0700865 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700866}
867
868FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
869 FX_UINT nFlag) {
870 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
871 ASSERT(pEnv);
872 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
873 if (!pFXAnnot) {
874 KillFocusAnnot(nFlag);
Lei Zhanga0601462015-10-30 14:54:37 -0700875 return FALSE;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700876 }
Lei Zhanga0601462015-10-30 14:54:37 -0700877
878 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
879 FX_BOOL bRet =
880 pAnnotHandlerMgr->Annot_OnLButtonDown(this, pFXAnnot, nFlag, point);
881 if (bRet)
882 SetFocusAnnot(pFXAnnot);
883 return bRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700884}
885
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700886FX_BOOL CPDFSDK_PageView::OnRButtonDown(const CPDF_Point& point,
887 FX_UINT nFlag) {
888 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
889 ASSERT(pEnv);
890 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
891 ASSERT(pAnnotHandlerMgr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700892
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700893 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
894
895 if (pFXAnnot == NULL)
896 return FALSE;
897
898 FX_BOOL bRet =
899 pAnnotHandlerMgr->Annot_OnRButtonDown(this, pFXAnnot, nFlag, point);
900 if (bRet) {
901 SetFocusAnnot(pFXAnnot);
902 }
903 return TRUE;
904}
905
906FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
907 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
908 ASSERT(pEnv);
909 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700910
911 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
912
913 if (pFXAnnot == NULL)
914 return FALSE;
915
916 FX_BOOL bRet =
917 pAnnotHandlerMgr->Annot_OnRButtonUp(this, pFXAnnot, nFlag, point);
918 if (bRet) {
919 SetFocusAnnot(pFXAnnot);
920 }
921 return TRUE;
922}
923
924FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CPDF_Point& point, FX_UINT nFlag) {
925 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
926 ASSERT(pEnv);
927 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700928 CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y);
929 CPDFSDK_Annot* pFocusAnnot = GetFocusAnnot();
930 FX_BOOL bRet = FALSE;
931 if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
932 // Last focus Annot gets a chance to handle the event.
933 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFocusAnnot, nFlag, point);
934 }
Lei Zhanga0601462015-10-30 14:54:37 -0700935 if (pFXAnnot && !bRet)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700936 bRet = pAnnotHandlerMgr->Annot_OnLButtonUp(this, pFXAnnot, nFlag, point);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700937 return bRet;
938}
939
940FX_BOOL CPDFSDK_PageView::OnMouseMove(const CPDF_Point& point, int nFlag) {
941 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
942 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700943 if (CPDFSDK_Annot* pFXAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
944 if (m_CaptureWidget && m_CaptureWidget != pFXAnnot) {
945 m_bExitWidget = TRUE;
946 m_bEnterWidget = FALSE;
947 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
948 }
949 m_CaptureWidget = (CPDFSDK_Widget*)pFXAnnot;
950 m_bOnWidget = TRUE;
951 if (!m_bEnterWidget) {
952 m_bEnterWidget = TRUE;
953 m_bExitWidget = FALSE;
954 pAnnotHandlerMgr->Annot_OnMouseEnter(this, pFXAnnot, nFlag);
955 }
956 pAnnotHandlerMgr->Annot_OnMouseMove(this, pFXAnnot, nFlag, point);
957 return TRUE;
958 }
959 if (m_bOnWidget) {
960 m_bOnWidget = FALSE;
961 m_bExitWidget = TRUE;
962 m_bEnterWidget = FALSE;
963 if (m_CaptureWidget) {
964 pAnnotHandlerMgr->Annot_OnMouseExit(this, m_CaptureWidget, nFlag);
965 m_CaptureWidget = NULL;
966 }
967 }
968 return FALSE;
969}
970
971FX_BOOL CPDFSDK_PageView::OnMouseWheel(double deltaX,
972 double deltaY,
973 const CPDF_Point& point,
974 int nFlag) {
975 if (CPDFSDK_Annot* pAnnot = GetFXWidgetAtPoint(point.x, point.y)) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700976 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
977 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700978 return pAnnotHandlerMgr->Annot_OnMouseWheel(this, pAnnot, nFlag,
979 (int)deltaY, point);
980 }
981 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700982}
983
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700984FX_BOOL CPDFSDK_PageView::OnChar(int nChar, FX_UINT nFlag) {
985 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700986 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700987 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700988 return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
989 }
Bo Xufdc00a72014-10-28 23:03:33 -0700990
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700991 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700992}
993
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700994FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
995 if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
Lei Zhang60f507b2015-06-13 00:41:00 -0700996 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700997 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700998 return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
999 }
1000 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001001}
1002
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001003FX_BOOL CPDFSDK_PageView::OnKeyUp(int nKeyCode, int nFlag) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001004 return FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001005}
1006
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001007void CPDFSDK_PageView::LoadFXAnnots() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001008 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001009 CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001010
1011 SetLock(TRUE);
1012 m_page->AddRef();
1013 if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
1014 IXFA_PageView* pageView = NULL;
1015 pageView = m_page->GetXFAPageView();
1016 ASSERT(pageView != NULL);
1017
1018 IXFA_WidgetIterator* pWidgetHander = pageView->CreateWidgetIterator(
1019 XFA_TRAVERSEWAY_Form, XFA_WIDGETFILTER_Visible |
1020 XFA_WIDGETFILTER_Viewable |
1021 XFA_WIDGETFILTER_AllType);
1022 if (!pWidgetHander) {
1023 m_page->Release();
1024 SetLock(FALSE);
1025 return;
Tom Sepezdcbc02f2015-07-17 09:16:17 -07001026 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001027
Lei Zhangbf60b292015-10-26 12:14:35 -07001028 while (IXFA_Widget* pXFAAnnot = pWidgetHander->MoveToNext()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001029 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pXFAAnnot, this);
Lei Zhangbf60b292015-10-26 12:14:35 -07001030 if (!pAnnot)
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001031 continue;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001032
Lei Zhangbf60b292015-10-26 12:14:35 -07001033 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001034 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001035 }
1036
1037 pWidgetHander->Release();
1038 } else {
1039 CPDF_Page* pPage = m_page->GetPDFPage();
1040 ASSERT(pPage != NULL);
1041
1042 FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
1043 // Disable the default AP construction.
1044 CPDF_InterForm::EnableUpdateAP(FALSE);
Lei Zhangbf60b292015-10-26 12:14:35 -07001045 m_pAnnotList.reset(new CPDF_AnnotList(pPage));
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001046 CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
1047
Lei Zhang1b700c32015-10-30 23:55:35 -07001048 const size_t nCount = m_pAnnotList->Count();
1049 for (size_t i = 0; i < nCount; ++i) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001050 CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
Lei Zhang50218532015-10-30 14:03:43 -07001051 CheckUnSupportAnnot(GetPDFDocument(), pPDFAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001052
1053 CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
1054 if (!pAnnot)
1055 continue;
Lei Zhangbf60b292015-10-26 12:14:35 -07001056 m_fxAnnotArray.push_back(pAnnot);
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001057
1058 pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
1059 }
1060 }
1061 m_page->Release();
1062 SetLock(FALSE);
1063}
1064
1065void CPDFSDK_PageView::UpdateRects(CFX_RectArray& rects) {
1066 for (int i = 0; i < rects.GetSize(); i++) {
1067 CPDF_Rect rc = rects.GetAt(i);
1068 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1069 pEnv->FFI_Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
1070 }
1071}
1072
1073void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
1074 CPDF_Rect rcWindow = pAnnot->GetRect();
1075 CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1076 pEnv->FFI_Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
1077 rcWindow.bottom);
1078}
1079
1080int CPDFSDK_PageView::GetPageIndex() {
1081 if (m_page) {
1082 CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
1083 CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
1084 if (pDoc && pDic) {
1085 return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
1086 }
1087 }
1088 return -1;
1089}
1090
Lei Zhang1b700c32015-10-30 23:55:35 -07001091bool CPDFSDK_PageView::IsValidAnnot(const CPDF_Annot* p) const {
Lei Zhangbf60b292015-10-26 12:14:35 -07001092 if (!p)
Lei Zhang1b700c32015-10-30 23:55:35 -07001093 return false;
Lei Zhangbf60b292015-10-26 12:14:35 -07001094
Lei Zhang1b700c32015-10-30 23:55:35 -07001095 const auto& annots = m_pAnnotList->All();
1096 return std::find(annots.begin(), annots.end(), p) != annots.end();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001097}
1098
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001099CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
1100 CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
1101 if (!pFocusAnnot)
Lei Zhangbf60b292015-10-26 12:14:35 -07001102 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001103
Lei Zhangbf60b292015-10-26 12:14:35 -07001104 for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -07001105 if (pAnnot == pFocusAnnot)
1106 return pAnnot;
1107 }
Lei Zhangbf60b292015-10-26 12:14:35 -07001108 return nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001109}