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